Skip to content

BigQuery: Support inline aggregate WHERE filter (AGG(expr WHERE cond))#2404

Open
moshap-firebolt wants to merge 1 commit into
apache:mainfrom
moshap-firebolt:upstream/bigquery-agg-where-filter
Open

BigQuery: Support inline aggregate WHERE filter (AGG(expr WHERE cond))#2404
moshap-firebolt wants to merge 1 commit into
apache:mainfrom
moshap-firebolt:upstream/bigquery-agg-where-filter

Conversation

@moshap-firebolt

Copy link
Copy Markdown
Contributor

GoogleSQL allows an aggregate call to carry an inline WHERE filter, e.g.
COUNT(* WHERE cond), SUM(x WHERE cond), ARRAY_AGG(x WHERE cond ORDER BY y).
It is the in-argument spelling of the standard AGG(expr) FILTER (WHERE cond).

Changes

  • Add FunctionArgumentClause::Where(Expr).
  • Parse it in parse_function_argument_list for the BigQuery and Generic
    dialects, before ORDER BY / LIMIT / HAVING.
  • Display renders WHERE <expr>; span is computed from the predicate.
  • New test parse_aggregate_with_where_filter in tests/sqlparser_bigquery.rs.

Docs: https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions

GoogleSQL allows an aggregate call to carry an inline WHERE filter, e.g.
`COUNT(* WHERE cond)`, `SUM(x WHERE cond)`,
`ARRAY_AGG(x WHERE cond ORDER BY y)`. It is the in-argument spelling of the
standard `AGG(expr) FILTER (WHERE cond)`.

Add `FunctionArgumentClause::Where(Expr)` and parse it in
`parse_function_argument_list` for the BigQuery and Generic dialects (before
ORDER BY / LIMIT / HAVING), with `Display` and span support. Covered by a new
`parse_aggregate_with_where_filter` test.
Comment thread src/parser/mod.rs
Comment on lines +18875 to +18876
if dialect_of!(self is GenericDialect | BigQueryDialect)
&& self.parse_keyword(Keyword::WHERE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we always have this syntax be accepted by the parser?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but it is not a standard syntax, it is really a BigQuery's shortcut for standard FILTER(WHERE ...) clause. So I don't think it should be enabled for all dialects. Let me know whether you still want to have it unconditionally

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think as long as its not ambiguous we can have it be accepted unconditionally

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 this pull request may close these issues.

2 participants