Skip to content

Add function arrayTranspose - #101214

Merged
vitlibar merged 7 commits into
ClickHouse:masterfrom
vitlibar:array-transpose
Apr 10, 2026
Merged

vitlibar merged 7 commits into
ClickHouse:masterfrom
vitlibar:array-transpose

Conversation

@vitlibar

@vitlibar vitlibar commented Mar 30, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

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

Add new function arrayTranspose taking a two-dimensional array (matrix) and transposing it:

SELECT arrayTranspose([[1, 2, 3], [4, 5, 6]])

Output:
[[1, 4], [2, 5], [3, 6]]

Version info

  • Merged into: 26.4.1.767

@nihalzp nihalzp self-assigned this Mar 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8fdbaac]

Summary: ✅


AI Review

Summary

This PR adds a new SQL function arrayTranspose with type-specialized implementations and stateless tests. The implementation currently contains an out-of-bounds index pattern on offsets (... [i - 1] / ... [outer_start - 1]) that can trigger undefined behavior on valid inputs (including rows before the first non-empty inner matrix), so the PR is not safe to merge yet.

Findings
  • ❌ Blockers
    • [src/Functions/array/arrayTranspose.cpp:82,88,119,122,313,320,325,360,367,372,416,423,457,464,468] Offset access uses index - 1 without guarding zero (outer_offsets[i - 1], inner_offsets[outer_start - 1], inner_offsets[k - 1]). This is undefined behavior when i == 0 and also when earlier rows are empty (outer_start == 0 on a later row). Impact: incorrect results, exceptions, or memory safety issues in release builds.
    • Suggested fix: compute row starts safely, e.g. outer_start = (i == 0 ? 0 : outer_offsets[i - 1]), inner_start = (outer_start == 0 ? 0 : inner_offsets[outer_start - 1]), then reuse those guarded starts in all paths.
Tests
  • ⚠️ Add a regression test where initial rows are empty and a later row is non-empty (e.g. [[], [], [[1,2],[3,4]]]-style shape) to ensure offset starts are handled safely when outer_start == 0 beyond row 0.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Blocked by offset-index UB in core execution paths.
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix all zero-based offset boundary accesses in arrayTranspose execution/validation paths.
    • Add a regression test covering empty-leading rows before first non-empty row.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Mar 30, 2026
@vitlibar
vitlibar requested a review from Copilot March 30, 2026 12:39

Copilot AI left a comment

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.

Pull request overview

Adds a new ClickHouse array function arrayTranspose to transpose 2D arrays (matrices), along with stateless tests and expected outputs.

Changes:

  • Implemented arrayTranspose as a new IFunction that validates rectangular inner arrays per row and transposes them.
  • Added stateless SQL queries covering typical, edge, nullable, and error cases.
  • Added reference output for the new stateless test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Functions/array/arrayTranspose.cpp Implements and registers the arrayTranspose function with validation and documentation.
tests/queries/0_stateless/04068_arrayTranspose.sql Adds coverage for normal matrices, degenerate/empty matrices, nullable elements, multi-row inputs, and expected errors.
tests/queries/0_stateless/04068_arrayTranspose.reference Adds expected outputs matching the new stateless test queries.

@vitlibar

vitlibar commented Mar 30, 2026 •

Copy link
Copy Markdown
Member Author

The closest existing alternative is arrayReduce('groupArrayForEach', arr), however it isn't exactly transpose because it ignores nulls:

SELECT arrayReduce('groupArrayForEach', [[1, NULL, 3], [4, 5, NULL]])

Output:
[[1,4],[5],[3]]

New function arrayTranspose moves all elements correctly (in terms of transpose):

SELECT arrayTranspose([[1, NULL, 3], [4, 5, NULL]])

Output:
[[1,4],[NULL, 5],[3, NULL]]

@nihalzp nihalzp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good!

Comment thread src/Functions/array/arrayTranspose.cpp Resolved
@nikitamikhaylov

Copy link
Copy Markdown
Member

Maybe it makes sense to implement the cache oblivious version of the matrix transpose algorithm?

@vitlibar

vitlibar commented Apr 1, 2026 •

Copy link
Copy Markdown
Member Author

Maybe it makes sense to implement the cache oblivious version of the matrix transpose algorithm?

I implemented this. For POD types now it uses the cache oblivious algorithm, for complicated types the generic algorithm is still used. I looked at arrayReverse (which also moves elements of array) and used a similar approach.

@vitlibar
vitlibar requested a review from Copilot April 1, 2026 14:31
Comment thread src/Functions/array/arrayTranspose.cpp Resolved

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/Functions/array/arrayTranspose.cpp Resolved

Copilot AI left a comment

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@nikitamikhaylov

Copy link
Copy Markdown
Member

Clang tidy failure fixed here: #101524

@vitlibar

vitlibar commented Apr 2, 2026 •

Copy link
Copy Markdown
Member Author

Flaky test 03668_shard_join_in_reverse_order is being fixed in #101654

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@vitlibar

vitlibar commented Apr 7, 2026 •

Copy link
Copy Markdown
Member Author

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vitlibar, please link PRs for fixing the TSan failure and fixing the logical error.

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 95.18% (296/311) · Uncovered code

Full report · Diff report

@vitlibar

vitlibar commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

@vitlibar, please link PRs for fixing the TSan failure and fixing the logical error.

done

@vitlibar
vitlibar enabled auto-merge April 9, 2026 18:25
@vitlibar
vitlibar added this pull request to the merge queue Apr 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Neat, thank you!

Merged via the queue into ClickHouse:master with commit 07668f7 Apr 10, 2026
479 of 484 checks passed
@vitlibar
vitlibar deleted the array-transpose branch April 10, 2026 01:48
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

6 participants