Skip to content

Add unnest as alias of arrayJoin - #105124

Merged
alexey-milovidov merged 2 commits into
masterfrom
alias-unnest
May 17, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
alias-unnest

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 16, 2026 •

Copy link
Copy Markdown
Member

PostgreSQL's unnest(arr) in a SELECT clause behaves the same as ClickHouse's arrayJoin(arr) — one row per array element. Adding a case-insensitive alias spares PostgreSQL-dialect queries a rewrite for the common case.

Note: PostgreSQL's LATERAL / CROSS JOIN UNNEST(...) table-source syntax is not affected by this alias and still requires ARRAY JOIN — that is a parser-level change for a separate PR.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added unnest as a case-insensitive alias of arrayJoin for PostgreSQL compatibility (function-call form).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.724

PostgreSQL's `unnest(arr)` in a `SELECT` clause behaves the same as
ClickHouse's `arrayJoin(arr)` — one row per array element. Expose
`unnest` as a case-insensitive alias to spare PostgreSQL-dialect queries
a rewrite for the common case. Note that PostgreSQL's
`LATERAL`/`CROSS JOIN UNNEST(...)` table-source syntax is not affected
by this alias and still requires `ARRAY JOIN`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cb7fb90]

Summary: ✅


AI Review

Summary

This PR adds unnest as a case-insensitive alias for arrayJoin (function-call form) to improve PostgreSQL query compatibility. The change is small, but there is one correctness gap in the legacy AST execution path when function-name normalization is disabled, so the PR is not ready to approve yet.

Findings

⚠️ Majors

  • [src/Functions/array/arrayJoin.cpp:256] The alias is registered in FunctionFactory, but legacy AST processing still has hard-coded "arrayJoin" checks (for example in ActionsVisitor::visit). With enable_analyzer = 0 and normalize_function_names = 0, unnest([1,2]) is treated as a regular function call and then fails with FUNCTION_IS_SPECIAL instead of doing array join semantics. This breaks the alias contract for a supported settings combination.
    Suggested fix: make legacy special-function handling independent of normalize_function_names (or explicitly recognize unnest in special arrayJoin checks).
Tests
  • ⚠️ Add a focused regression that proves behavior in legacy mode without function normalization:
    SET enable_analyzer = 0, normalize_function_names = 0; SELECT unnest([1, 2]);
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
  1. Fix legacy-path handling so unnest preserves arrayJoin semantics even when normalize_function_names = 0.
  2. Add regression coverage for this settings combination.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 16, 2026
alexey-milovidov added a commit that referenced this pull request May 16, 2026
Recent compatibility PRs added case-insensitive aliases and parser sugar
that make several of the SQLStorm rewrites unnecessary:

  - `STDDEV`            -> `stddevPop`            (#105120)
  - `array_to_string`   -> `arrayStringConcat`    (#105121)
  - `REGEXP_SUBSTR`     -> `regexpExtract`        (#105122)
  - `CARDINALITY`       -> `length`               (#105123)
  - `unnest()` function -> `arrayJoin()`          (#105124)
  - `STRING_AGG`        -> `groupConcat`          (#105125)
  - `date_part(unit,e)` -> `EXTRACT(unit FROM e)` (#105127)
  - `expr OP ANY/ALL(array_literal)`              (#105129)

`ARRAY_AGG`, `TRANSLATE`, and `EXTRACT(EPOCH|DOW|... FROM ...)` were
already supported by ClickHouse before these PRs.

Removed the corresponding rewrite calls and helper functions
(`rewrite_string_agg`, `rewrite_array_agg`, `rewrite_date_part`,
`rewrite_stddev`, `rewrite_extract_epoch`, the EXTRACT(DOW) inline
rewrite, `rewrite_any_comparison`, and the trailing
`unnest(...) -> arrayJoin(...)` substitution). Also dropped the
unreferenced no-op helpers (`rewrite_extract_unit`, `rewrite_fetch_offset`,
`rewrite_interval`, `rewrite_cast_timestamp`, `rewrite_current_timestamp`,
`rewrite_bool_literals`, `rewrite_ilike`, `rewrite_no_supertype`).

The PostgreSQL `LATERAL` / `CROSS JOIN UNNEST(...)` table-source forms,
`arrayJoin(...)` in JOIN position, PG-specific casts, `AT TIME ZONE`,
`STRING_AGGDistinct` (a mangled-name artifact), and the still-unsupported
function rewrites (`string_to_array`, `regexp_split_to_array`, `RANDOM`,
`TO_TIMESTAMP`, `ARRAY_LENGTH`, `SPLIT_PART`, `age`) are still rewritten.

Net change: -329 lines from rewrite_queries.py and -75 lines from the
tests (the `TestRewriteAnyComparison` class is removed since the rewrite
it covered no longer exists).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
/// the same as `arrayJoin(arr)` - one row per array element. Note that
/// PostgreSQL's `LATERAL`/`CROSS JOIN UNNEST(...)` table-source syntax is
/// not supported by this alias and still requires `ARRAY JOIN`.
factory.registerAlias("unnest", "arrayJoin", FunctionFactory::Case::Insensitive);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unnest is registered only as a FunctionFactory alias, but the legacy AST pipeline still has hard-coded checks for "arrayJoin" (for example in ActionsVisitor::visit(const ASTFunction ...)). With enable_analyzer = 0 and normalize_function_names = 0, unnest([1, 2]) is handled as a regular function call instead of an ARRAY JOIN action, then FunctionArrayJoin::executeImpl throws FUNCTION_IS_SPECIAL.

Could we make legacy handling independent of name normalization (or explicitly treat unnest as arrayJoin in special-function checks) and add a regression test for:

SET enable_analyzer = 0, normalize_function_names = 0;
SELECT unnest([1, 2]);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We are not interested in the legacy analysis at all. Ok to skip the comment above.

@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (4/4) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this May 17, 2026
@alexey-milovidov
alexey-milovidov merged commit ed39733 into master May 17, 2026
166 of 167 checks passed
@alexey-milovidov
alexey-milovidov deleted the alias-unnest branch May 17, 2026 23:06
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants