Skip to content

parser/clickhouse: parametric aggregates move DISTINCT to the wrong group and reject chained calls or subscripts #605

Closed

Description

@ajitpratap0

Summary

In the ClickHouse dialect, the parametric-aggregate path fn(params)(args) keeps the DISTINCT/ORDER BY parsed from the first paren group on the FunctionCall, so they appear on the arguments when rendered. The second group does not accept DISTINCT. Chained call groups and a subscript after a call are also rejected.

Reproduction

Uses gosqlx.ParseWithDialect(sql, keywords.DialectClickHouse), then stmt.SQL() / gosqlx.FormatAST, on main 61ca6c8.

Input Actual Expected
SELECT quantile(0.5)(DISTINCT x) FROM t E2001 at 1:22 unexpected token: DISTINCT parses; DISTINCT applies to args
SELECT uniqExact(DISTINCT x)(y) FROM t renders SELECT uniqExact(x)(DISTINCT y) FROM t (DISTINCT moved; output does not re-parse) DISTINCT stays with its own group, or the input is rejected
SELECT groupArray(DISTINCT 3)(x) FROM t renders groupArray(3)(DISTINCT x) same as above
SELECT quantiles(0.5,0.9)(x)[1] FROM t E2002 at 1:29 got LBRACKET array subscript on the result
SELECT groupArray(x)[1] FROM t E2002 at 1:21 got LBRACKET array subscript on the result
SELECT f(1)(2)(3) FROM t E2002 at 1:15 got LPAREN either parse or give a clear error

SELECT quantile(0.5)(x) FROM t and topK(3)(x) AS k work and round-trip.

Notes

  • pkg/sql/parser/window.go ~180-211: funcCall is built with Distinct: distinct, OrderBy: orderByExprs from the first group, and then Parameters = Arguments. Only Arguments is moved. The second group is parsed with a bare parseExpression loop, so DISTINCT/ORDER BY cannot appear there. A fix could reset Distinct/OrderBy when the first group becomes Parameters, reject them in the parameter group, and parse the second group with the same argument-list routine as the first.
  • A postfix [...] subscript is not applied after a function call (this is not specific to ClickHouse parametric calls).

Found during review of #583.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions