Skip to content

feat(duckdb): transpile BigQuery ARRAY_CONCAT_AGG to DuckDB - #7891

Merged
geooo109 merged 3 commits into
tobymao:mainfrom
algolia:upstream-pr/array-concat-agg
Jul 21, 2026
Merged

feat(duckdb): transpile BigQuery ARRAY_CONCAT_AGG to DuckDB#7891
geooo109 merged 3 commits into
tobymao:mainfrom
algolia:upstream-pr/array-concat-agg

Conversation

@sirockin

Copy link
Copy Markdown
Contributor

Summary

BigQuery's ARRAY_CONCAT_AGG(expr) concatenates arrays into a single flat array. DuckDB has no direct equivalent, but FLATTEN(LIST(expr)) achieves the same result: LIST collects the arrays into a nested list, then FLATTEN unnests one level.

Changes

  • Add ARRAY_CONCAT_AGGFLATTEN(LIST(...)) mapping in DuckDB.Generator
  • Add transpilation test (BigQuery → DuckDB round-trip)

Motivation

We use sqlglot to transpile BigQuery SQL models to DuckDB for local testing via sqlmesh. ARRAY_CONCAT_AGG is used in several of our production models but was not previously transpiled.

…IST(...))

BigQuery's `ARRAY_CONCAT_AGG(expr)` concatenates arrays into a single
flat array. DuckDB has no direct equivalent, but `FLATTEN(LIST(expr))`
achieves the same result: `LIST` collects the arrays into a nested
list, then `FLATTEN` unnests one level.

Adds:
- `ARRAY_CONCAT_AGG` → `FLATTEN(LIST(...))` mapping in DuckDB generator
- Transpilation test (BigQuery → DuckDB round-trip)
@geooo109 geooo109 self-assigned this Jul 17, 2026

@geooo109 geooo109 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ARRAY_CONCAT_AGG(
  expression
  [ ORDER BY key [ { ASC | DESC } ] [, ... ] ]
  [ LIMIT n ]
)

From the docs ^ of bq, so let's also check what happens in the case of ORDER BY case and the LIMIT n.

Comment thread tests/dialects/test_bigquery.py Outdated
Comment thread sqlglot/generators/duckdb.py Outdated
- Replace FLATTEN(LIST(x)) with FLATTEN(ARRAY_AGG(x) FILTER(WHERE NOT
  x IS NULL)) so all-NULL inputs return NULL (matching BigQuery)
- Use expression API via new arrayconcatagg_sql method instead of
  inline lambda with f-string
- Fix test input to use valid BigQuery syntax (ARRAY_CONCAT_AGG
  requires an array argument, not a scalar)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread sqlglot/generators/duckdb.py Outdated
Comment thread sqlglot/generators/duckdb.py
- Remove ArrayConcatAgg from TRANSFORMS dict (auto-discovered via
  method name convention)
- Use self.func("FLATTEN", ...) instead of self.sql(exp.Flatten(...))

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@georgesittas

Copy link
Copy Markdown
Collaborator

@sirockin is this still a draft? What's pending?

@geooo109

Copy link
Copy Markdown
Collaborator

@georgesittas we should check the ORDER BY and the limit syntax for this.

I think ORDER BY is solved as it is, but for limit we should throw an unsupported for now.
So, @sirockin let's verify what happens for these 2 cases with the current solution.

@geooo109

Copy link
Copy Markdown
Collaborator

I will take this to the finish line. thanks for the PR.

@geooo109
geooo109 marked this pull request as ready for review July 21, 2026 10:42
@geooo109
geooo109 merged commit c402094 into tobymao:main Jul 21, 2026
8 checks passed
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.

3 participants