You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "moshap-firebolt (via GitHub)" <gi...@apache.org> on 2026/07/13 16:57:57 UTC

[PR] BigQuery: Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

moshap-firebolt opened a new pull request, #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404

   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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio merged PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] BigQuery: Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "moshap-firebolt (via GitHub)" <gi...@apache.org>.
moshap-firebolt commented on code in PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404#discussion_r3581750337


##########
src/parser/mod.rs:
##########
@@ -18870,6 +18870,14 @@ impl<'a> Parser<'a> {
         let duplicate_treatment = self.parse_duplicate_treatment()?;
         let args = self.parse_comma_separated(Parser::parse_function_args)?;
 
+        // GoogleSQL inline aggregate filter: `AGG(expr WHERE cond [ORDER BY ..])`.
+        // Precedes ORDER BY / LIMIT / HAVING; equivalent to `AGG(expr) FILTER (WHERE cond)`.
+        if dialect_of!(self is GenericDialect | BigQueryDialect)
+            && self.parse_keyword(Keyword::WHERE)

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] BigQuery: Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404#discussion_r3577004066


##########
src/parser/mod.rs:
##########
@@ -18870,6 +18870,14 @@ impl<'a> Parser<'a> {
         let duplicate_treatment = self.parse_duplicate_treatment()?;
         let args = self.parse_comma_separated(Parser::parse_function_args)?;
 
+        // GoogleSQL inline aggregate filter: `AGG(expr WHERE cond [ORDER BY ..])`.
+        // Precedes ORDER BY / LIMIT / HAVING; equivalent to `AGG(expr) FILTER (WHERE cond)`.
+        if dialect_of!(self is GenericDialect | BigQueryDialect)
+            && self.parse_keyword(Keyword::WHERE)

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404#discussion_r3594708381


##########
src/parser/mod.rs:
##########
@@ -18870,6 +18870,14 @@ impl<'a> Parser<'a> {
         let duplicate_treatment = self.parse_duplicate_treatment()?;
         let args = self.parse_comma_separated(Parser::parse_function_args)?;
 
+        // Inline aggregate filter: `AGG(expr WHERE cond [ORDER BY ..])`, popularized by
+        // GoogleSQL. Precedes ORDER BY / LIMIT / HAVING; equivalent to the standard
+        // `AGG(expr) FILTER (WHERE cond)`. Accepted for all dialects: `WHERE` is a reserved
+        // keyword that cannot begin a function argument, so the syntax is unambiguous.

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "moshap-firebolt (via GitHub)" <gi...@apache.org>.
moshap-firebolt commented on code in PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404#discussion_r3588404468


##########
src/parser/mod.rs:
##########
@@ -18870,6 +18870,14 @@ impl<'a> Parser<'a> {
         let duplicate_treatment = self.parse_duplicate_treatment()?;
         let args = self.parse_comma_separated(Parser::parse_function_args)?;
 
+        // GoogleSQL inline aggregate filter: `AGG(expr WHERE cond [ORDER BY ..])`.
+        // Precedes ORDER BY / LIMIT / HAVING; equivalent to `AGG(expr) FILTER (WHERE cond)`.
+        if dialect_of!(self is GenericDialect | BigQueryDialect)
+            && self.parse_keyword(Keyword::WHERE)

Review Comment:
   Done!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] BigQuery: Support inline aggregate WHERE filter (`AGG(expr WHERE cond)`) [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #2404:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2404#discussion_r3585468852


##########
src/parser/mod.rs:
##########
@@ -18870,6 +18870,14 @@ impl<'a> Parser<'a> {
         let duplicate_treatment = self.parse_duplicate_treatment()?;
         let args = self.parse_comma_separated(Parser::parse_function_args)?;
 
+        // GoogleSQL inline aggregate filter: `AGG(expr WHERE cond [ORDER BY ..])`.
+        // Precedes ORDER BY / LIMIT / HAVING; equivalent to `AGG(expr) FILTER (WHERE cond)`.
+        if dialect_of!(self is GenericDialect | BigQueryDialect)
+            && self.parse_keyword(Keyword::WHERE)

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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org