BigQuery: Option to have L054 ignore GROUP BY/ORDER BY clauses in aggegate functions (e.g. ARRAY_AGG())
See original GitHub issueSearch before asking
- I searched the issues and found no similar issues.
What Happened
In this query, an L054 (Inconsistent column references in ‘GROUP BY/ORDER BY’ clauses.) is detected at the order by
within the array_agg
array function on line 4. The L054 documentation says “ORDER BY clauses from WINDOW clauses are ignored by this rule”, so it probably doesn’t take this odd order by
inside an array function into account.
select
user_uuid,
max(started_at) as updated_at,
to_json_string(array_agg(product_id order by started_at desc)) as products
from purchased
group by 1
Expected Behaviour
L054 should probably ignore order by in array functions.
Observed Behaviour
See “What Happened”.
How to reproduce
stick that query in a file 😃
Dialect
bigquery
Version
1.2.1
Configuration
[sqlfluff]
dialect = bigquery
templater = dbt
# https://docs.sqlfluff.com/en/stable/rules.html
# L016: Line is too long
# L034: Use wildcards then simple targets before calculations and aggregates in select statements
exclude_rules = L016,L034
[sqlfluff:rules:L014]
extended_capitalisation_policy = lower
ignore_words = _TABLE_SUFFIX
[sqlfluff:rules:L064]
preferred_quoted_literal_style = single_quotes
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project’s Code of Conduct
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Aggregate functions | BigQuery - Google Cloud
This function ignores NULL input arrays, but respects the NULL elements in non- NULL input arrays. An error is raised, however, if an...
Read more >SQL ARRAY_AGG Function | BigQuery Syntax and Examples
An error is raised if an array in the final query result contains a NULL element. ORDER BY: Specifies the order of the...
Read more >ARRAY_AGG BigQuery Functions 101: Syntax and Usage
This blog aims at teaching you about the ARRAY_AGG BigQuery Functions with the help of key syntax and examples.
Read more >Aggregate function ARRAY_AGG not allowed in SQL function
what you want is actually called aggregate UDF and BigQuery does not support such (at least yet)! so aggregate udf direction looks like...
Read more >User-defined Aggregate Functions [62898282] - Issue Tracker
I know one can define a User Defined Function in order to perform some custom ... a custom user-defined, Aggregation Function to use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
L054 already skips enforcing this role in windowing functions as noted in the docs: https://docs.sqlfluff.com/en/stable/rules.html#sqlfluff.core.rules.Rule_L054 This is done for precisely the reasons you note.
The issue here is an array function is technically not a windowing function, so that skipping logic needs to be increased to include that.
Note that this is not specific to BigQuery. Many engines support array_agg (presto, postgre, snowflake …)