Skip to content

Don't leak ARRAY_AGG's inline ORDER BY into the generated NULL filter [CLAUDE]#7907

Closed
chuenchen309 wants to merge 1 commit into
tobymao:mainfrom
chuenchen309:fix/arrayagg-orderby-b10
Closed

Don't leak ARRAY_AGG's inline ORDER BY into the generated NULL filter [CLAUDE]#7907
chuenchen309 wants to merge 1 commit into
tobymao:mainfrom
chuenchen309:fix/arrayagg-orderby-b10

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

When ARRAY_AGG is transpiled from a null-excluding dialect (e.g. Spark) to one that includes nulls, sqlglot adds a FILTER(WHERE col IS NOT NULL) to preserve the semantics. If the ARRAY_AGG has an inline ORDER BY, ArrayAgg.this is an exp.Order, so the filter was generated as FILTER(WHERE x ORDER BY y ... IS NOT NULL) — invalid SQL that no target dialect can parse back.

The fix unwraps the Order so the filter is built from the column itself, which is what _add_arrayagg_null_filter documents it expects ("before ORDER BY wrapping") and what the DuckDB path already does. Cases without an inline ORDER BY are unchanged.

Verified against the full unit suite (1243 tests) with an added Spark→Presto/Postgres test.

Disclosure: found, fixed, tested and described by an AI agent (Claude Code) running on this account. The account holder reviews every change and is accountable for it.

… [CLAUDE]

When transpiling ARRAY_AGG from a null-excluding dialect (e.g. Spark) to one that
includes nulls, a NULL FILTER is added. With an inline ORDER BY, ArrayAgg.this is
an exp.Order, so the filter was built as `FILTER(WHERE x ORDER BY y ... IS NOT
NULL)`, which no dialect can parse. Unwrap the Order to filter on the column
itself, matching _add_arrayagg_null_filter's documented "before ORDER BY
wrapping" contract and the existing DuckDB path.

Co-authored-by AI: Claude (Claude Code). [CLAUDE]
@georgesittas

Copy link
Copy Markdown
Collaborator

Garbage in -> garbage out, see this.

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