Skip to content

feat(duckdb): transpile BigQuery IN UNNEST(array) to ARRAY_CONTAINS#7892

Merged
geooo109 merged 2 commits into
tobymao:mainfrom
algolia:upstream-pr/in-unnest
Jul 23, 2026
Merged

feat(duckdb): transpile BigQuery IN UNNEST(array) to ARRAY_CONTAINS#7892
geooo109 merged 2 commits into
tobymao:mainfrom
algolia:upstream-pr/in-unnest

Conversation

@sirockin

Copy link
Copy Markdown
Contributor

Summary

BigQuery uses value IN UNNEST(array_expr) to test array membership. DuckDB has no UNNEST in this position but provides ARRAY_CONTAINS.

Changes

  • Add IN UNNEST(arr)COALESCE(ARRAY_CONTAINS(arr, val), FALSE) in DuckDB generator via in_sql override
  • Wrap in COALESCE(..., FALSE) for null safety — ARRAY_CONTAINS(NULL, x) returns NULL in DuckDB, but BigQuery's IN UNNEST(NULL) returns FALSE
  • Add transpilation test for BigQuery → DuckDB

Motivation

We use sqlglot to transpile BigQuery SQL models to DuckDB for local testing via sqlmesh. IN UNNEST(...) is used in several of our production models for array membership checks but was not previously transpiled to DuckDB.

…TAINS

BigQuery uses `value IN UNNEST(array_expr)` to test array membership.
DuckDB has no `UNNEST` in this position but provides `ARRAY_CONTAINS`.

The transpilation wraps the result in `COALESCE(..., FALSE)` to handle
NULL arrays safely — `ARRAY_CONTAINS(NULL, x)` returns NULL in DuckDB,
but BigQuery's `IN UNNEST(NULL)` returns FALSE.

Adds:
- `IN UNNEST(arr)` → `COALESCE(ARRAY_CONTAINS(arr, val), FALSE)` in
  DuckDB generator via `in_sql` override
- Transpilation test for BigQuery → DuckDB
@geooo109 geooo109 self-assigned this Jul 17, 2026
Comment thread sqlglot/generators/duckdb.py Outdated
Comment thread sqlglot/generators/duckdb.py
Replace COALESCE(ARRAY_CONTAINS(arr, x), FALSE) with a CASE expression
that preserves BigQuery's three-valued NULL semantics:
- NULL IN UNNEST([1, 2]) → NULL (not FALSE)
- 3 IN UNNEST([1, NULL]) → NULL (not FALSE)
- 1 IN UNNEST(NULL) → FALSE
- 1 IN UNNEST([]) → FALSE

Use expression API instead of f-string interpolation per review
feedback. Add NOT IN UNNEST test case.

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
geooo109 marked this pull request as ready for review July 21, 2026 11:02
Comment thread sqlglot/generators/duckdb.py
Comment thread sqlglot/generators/duckdb.py
@geooo109

Copy link
Copy Markdown
Collaborator

I will take it to the finish line.

@geooo109
geooo109 merged commit 3cd3e58 into tobymao:main Jul 23, 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