Skip to content

fix(clickhouse): support arrayMap and arrayFilter roundtrips [CODEX]#7794

Merged
georgesittas merged 3 commits into
tobymao:mainfrom
gaoflow:fix/clickhouse-arraymap-arrayfilter-transpile
Jun 26, 2026
Merged

fix(clickhouse): support arrayMap and arrayFilter roundtrips [CODEX]#7794
georgesittas merged 3 commits into
tobymao:mainfrom
gaoflow:fix/clickhouse-arraymap-arrayfilter-transpile

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Problem

ClickHouse higher-order array functions arrayMap(lambda, arr) and arrayFilter(lambda, arr) were parsed as generic/unsupported function expressions instead of SQLGlot's semantic array function nodes.

This made ClickHouse -> ClickHouse roundtrips lossy for these functions, and it also meant arrayMap did not use the existing exp.Transform representation.

Fix

  • Add ClickHouse parser mappings for ARRAYMAP and ARRAYFILTER.
  • Preserve ClickHouse's argument order by mapping arrayMap(lambda, array) to exp.Transform(this=array, expression=lambda) and arrayFilter(lambda, array) to exp.ArrayFilter(this=array, expression=lambda).
  • Add a ClickHouse generator transform for exp.Transform so arrayMap roundtrips back to ClickHouse syntax.

Tests

Added focused ClickHouse identity tests:

  • arrayMap(x -> x + 1, arr) roundtrips and parses as exp.Transform.
  • arrayFilter(x -> x > 0, arr) roundtrips and parses as exp.ArrayFilter.

Local verification:

python -m unittest tests.dialects.test_clickhouse.TestClickhouse.test_functions
python -m ruff check sqlglot/parsers/clickhouse.py sqlglot/generators/clickhouse.py tests/dialects/test_clickhouse.py
python -m ruff format --check sqlglot/parsers/clickhouse.py sqlglot/generators/clickhouse.py tests/dialects/test_clickhouse.py

This pull request was prepared with the assistance of AI, under my direction and review.

…order functions [CLAUDE]

ClickHouse's higher-order array functions arrayMap(lambda, arr) and
arrayFilter(lambda, arr) were being parsed as Anonymous expressions because
the ClickHouse parser lacked entries for their uppercased forms (ARRAYMAP,
ARRAYFILTER). As a result, transpiling to DuckDB produced invalid ARRAYMAP()
and ARRAYFILTER() calls instead of LIST_TRANSFORM() and LIST_FILTER().

Additionally, the ClickHouse generator had no Transform entry, so DuckDB's
LIST_TRANSFORM and Spark's TRANSFORM arrived in ClickHouse as the wrong name.

Note: argument order in ClickHouse is reversed relative to the canonical
exp.Transform/exp.ArrayFilter convention (lambda first, array second).
@geooo109 geooo109 self-assigned this Jun 25, 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.

Nice work, I left some comments.

My suggestion is the following:

Remove the current validate_all tests of this PR, and only use vaildate_identity tests for testing the roundtrip (input clickhouse -> output clickhouse) of the added functions for example self.validate_identity("arrayFilter(x -> x > 0, arr)").assert_is(exp.ArrayFilter). This would make the PR simple and mergable.

Then, if you want you can investigate various inputs - outputs for the transpilation, as you saw the NULL creates semantic issues for the transpilation. After, detecting all this edge cases let's make a following PR that contains the robust transpilation.

Comment thread tests/dialects/test_clickhouse.py Outdated
Comment thread tests/dialects/test_clickhouse.py Outdated
Comment thread sqlglot/parsers/clickhouse.py Outdated
@gaoflow gaoflow changed the title fix(clickhouse): correctly transpile arrayMap and arrayFilter higher-order functions [CLAUDE] fix(clickhouse): support arrayMap and arrayFilter roundtrips [CODEX] Jun 26, 2026
@gaoflow

gaoflow commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 806c7698:

  • removed the validate_all cross-dialect transpilation tests
  • kept only focused ClickHouse validate_identity coverage for arrayMap / arrayFilter, with AST type assertions
  • removed the parser comments to keep the code smaller

I also updated the PR title and description to reflect the narrowed roundtrip scope.

@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.

thank you, great work!

@georgesittas georgesittas merged commit 64e268a into tobymao:main Jun 26, 2026
10 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