Skip to content

Commit e229abe

Browse files
committed
when colref is equals to alias, will not write as, this is usefull when array join
1 parent dd55ec9 commit e229abe

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/ClickHouseSQLParser.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,11 @@ public static function expr_post_process_change_case_insensitive_function_name($
10741074
return self::walker($expr, $func, false);
10751075
}
10761076

1077-
protected static function check_and_parse_select($tokens)
1077+
protected static function check_and_parse_select($sql, $options)
10781078
{
1079-
if (self::is_token_of($tokens[0],"SELECT")||self::is_token_of($tokens[0],"WITH")) {
1079+
if (preg_match("{^[\\s(]*(?:WITH|SELECT)\\s}si", $sql)) {
1080+
$options["tokens_post_process_check_error_and_remove_blank"] = 1;
1081+
$tokens = self::token_get_all($sql, $options);
10801082
list($expr, $index) = self::get_next_expr($tokens, 0, 0, true);
10811083
if ($index != \count($tokens)) {
10821084
throw new \ErrorException("cannot parse as sql, some token left");
@@ -1095,12 +1097,7 @@ protected static function check_and_parse_select($tokens)
10951097
//expr_post_process_change_case_insensitive_function_name => default(1)
10961098
protected static function parse_impl($sql, $options = array())
10971099
{
1098-
$options["tokens_post_process_check_error_and_remove_blank"] = 1;
1099-
$tokens = self::token_get_all($sql, $options);
1100-
if(count($tokens)==0){
1101-
throw new \ErrorException("cannot parse as sql, empty string");
1102-
}
1103-
if ($expr = self::check_and_parse_select($tokens)) {
1100+
if ($expr = self::check_and_parse_select($sql, $options)) {
11041101
$expr = self::expr_post_process($expr, $options);
11051102
return $expr;
11061103
} else {
@@ -1138,6 +1135,9 @@ protected static function aliasStr($p)
11381135
{
11391136
$s = "";
11401137
if (self::hasAlias($p)) {
1138+
if(self::is_expr_of($p,self::T_IDENTIFIER_COLREF) && \count($p["parts"])==1 && $p["parts"][0]===$p["alias"]){
1139+
return $s;
1140+
}
11411141
$s .= " AS " . self::backquote($p["alias"]);
11421142
}
11431143
return $s;

0 commit comments

Comments
 (0)