sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

ClickHouse quantile/quantileIf functions fail to parse

Open cpcloud opened this issue 2 years ago • 1 comments

ClickHouse has a somewhat unique way of spelling these:

9bf006fcf914 :) select quantile(0.5)(a) from (select 1 a union all select 2);

SELECT quantile(0.5)(a)
FROM
(
    SELECT 1 AS a
    UNION ALL
    SELECT 2
)

Query id: fe37056a-efe0-45bf-9d34-95de2916ecef

┌─quantile(0.5)(a)─┐
│              1.5 │
└──────────────────┘

1 row in set. Elapsed: 0.002 sec.

9bf006fcf914 :) select quantileIf(0.5)(a, true) from (select 1 a union all select 2);

SELECT quantileIf(0.5)(a, true)
FROM
(
    SELECT 1 AS a
    UNION ALL
    SELECT 2
)

Query id: d3b2e971-c9af-4a6c-8ba3-93caacc246bc

┌─quantileIf(0.5)(a, true)─┐
│                      1.5 │
└──────────────────────────┘

1 row in set. Elapsed: 0.002 sec.

9bf006fcf914 :) select quantileIf(0.5)(a, false) from (select 1 a union all select 2);

SELECT quantileIf(0.5)(a, false)
FROM
(
    SELECT 1 AS a
    UNION ALL
    SELECT 2
)

Query id: 27307fa4-2b25-4382-8e6c-ef3d4eb1b306

┌─quantileIf(0.5)(a, false)─┐
│                       nan │
└───────────────────────────┘

1 row in set. Elapsed: 0.002 sec.

When parsed with sqlglot I get a parse error:

In [1]: import sqlglot as sg

In [2]: sg.__version__
Out[2]: '10.3.2'

In [3]: sg.parse_one("select quantile(0.5)(a) from (select 1 a union all select 2)", read="clickhouse")
...
ParseError: Required keyword: 'quantile' missing for <class 'sqlglot.expressions.Quantile'>. Line 1, Col: 20.
  select quantile(0.5)(a) from (select 1 a union all select 2)

cpcloud avatar Jan 04 '23 12:01 cpcloud

I'll take a look at this soon. Besides the quantile argument missing in the Quantile expression, I don't think SQLGlot can handle the syntax foo(a)(b) yet.

georgesittas avatar Jan 04 '23 13:01 georgesittas