Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid sql generated for clickhouse when using a table alias with explicit column names #3726

Closed
cpcloud opened this issue Jul 2, 2024 · 3 comments · Fixed by #3727
Closed

Comments

@cpcloud
Copy link
Contributor

cpcloud commented Jul 2, 2024

In [8]: import sqlglot as sg

In [9]: sg.__version__
Out[9]: '25.4.1'

In [10]: print(sg.parse_one("SELECT a, b FROM (SELECT * FROM x) AS t(a, b)", read="duckdb").sql('clickhouse', pretty=1))
SELECT
  a,
  b
FROM (
  SELECT
    *
  FROM x
) AS t(a, b)

Official Documentation

There are no syntax diagrams in their documentation, so it's hard to tell with ClickHouse other than to try to run the query:

localhost :) create or replace view x as select 1 a, 'a' b union all select 2 a, 'b' b union all select 3 a, 'c' b;

CREATE OR REPLACE VIEW x
AS SELECT
    1 AS a,
    'a' AS b
UNION ALL
SELECT
    2 AS a,
    'b' AS b
UNION ALL
SELECT
    3 AS a,
    'c' AS b

Query id: 46155cf8-28fd-4e69-8193-1f8cc51223c0

Ok.

0 rows in set. Elapsed: 0.001 sec.

localhost :) select * from x;

SELECT *
FROM x

Query id: dcddf522-4bee-428a-808a-7a79d801c32c

   ┌─a─┬─b─┐
1. │ 3 │ c │
   └───┴───┘
   ┌─a─┬─b─┐
2. │ 2 │ b │
   └───┴───┘
   ┌─a─┬─b─┐
3. │ 1 │ a │
   └───┴───┘

3 rows in set. Elapsed: 0.002 sec.

localhost :) select a, b from (select * from x) as t (a, b);

Syntax error: failed at position 41 ('('):

select a, b from (select * from x) as t (a, b);

Expected one of: FINAL, SAMPLE, table, table function, subquery or list of joined tables, array join, LEFT ARRAY JOIN, INNER, ARRAY JOIN, JOIN, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, INTO OUTFILE, FORMAT, end of query
@georgesittas
Copy link
Collaborator

Ah, so Clickhouse doesn't support naming columns in table aliases?

@cpcloud
Copy link
Contributor Author

cpcloud commented Jul 2, 2024

Yeah, it would seem so. Not exactly easy to tell from their docs 😞

@georgesittas
Copy link
Collaborator

Gotcha, let me give it a whirl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants