sanchezzzhak / kak-clickhouse

Yii2 ext. ClickHouse
69 stars 43 forks source link

Multiple UNION ALL, Syntax error: failed at position XXX ('ALL'): ALL SELECT #66

Closed toxaus closed 1 year ago

toxaus commented 1 year ago

In case of multiple UNIONS with HAVING conditions inside, there is an error:

Query error: Code: 62. DB::Exception: Syntax error: failed at position 968 ('ALL'): ALL SELECT a, b, c Expected one of: token, Dot, UUID, DoubleColon, MOD, DIV, NOT, BETWEEN, LIKE, ILIKE, NOT LIKE, NOT ILIKE, IN, NOT IN, GLOBAL IN, GLOBAL NOT IN, IS, AND, OR, QuestionMark, alias, AS, WINDOW, ORDER BY, LIMIT, OFFSET, SETTINGS, UNION, EXCEPT, INTERSECT. (SYNTAX_ERROR) (version 22.7.1.2484 (official build))

It happens because of missing space before UNION keyword. Generated SQL looks like this:

...
SELECT a, b, c
FROM table.page_query_country pdgwt 
WHERE (date >= '2022-01-11' and date <= '2022-04-10') HAVING sum((column)) = 100UNION ALL SELECT a, b, c ...

https://github.com/sanchezzzhak/kak-clickhouse/blob/2cde1433ddc3814d05697b211f5512cef81a15e6/QueryBuilder.php#L305

I propose to change line 305 with: $result .= 'UNION ' . ($union['all'] ? 'ALL ' : '') . '( ' . $unions[$i]['query'] . ' ) ';

sanchezzzhak commented 1 year ago

Fixed: Try version 1.1.1.

new functionality union($query, 'DISTINCT') https://clickhouse.com/docs/en/sql-reference/statements/select/union/

toxaus commented 1 year ago

Fixed: Try version 1.1.1.

new functionality union($query, 'DISTINCT') https://clickhouse.com/docs/en/sql-reference/statements/select/union/

Thank you for your super fast reaction. It works!