question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BigQuery: Option to have L054 ignore GROUP BY/ORDER BY clauses in aggegate functions (e.g. ARRAY_AGG())

See original GitHub issue

Search 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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tunethewebcommented, Aug 15, 2022

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.

0reactions
remilepriolcommented, Oct 13, 2022

Note that this is not specific to BigQuery. Many engines support array_agg (presto, postgre, snowflake …)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found