Skip to content

Remove unused rows from ColumnReplicated during function execution - #99564

Merged
Avogar merged 15 commits into
ClickHouse:masterfrom
m-selmi:remove-unused-rows-from-column-replicated
Apr 2, 2026
Merged

Avogar merged 15 commits into
ClickHouse:masterfrom
m-selmi:remove-unused-rows-from-column-replicated

Conversation

@m-selmi

@m-selmi m-selmi commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

ColumnReplicated can have unreferenced rows in the nested column that did not fulfill the join condition. Functions in IFunction.cpp are executed on the nested column directly which can cause failures on values that should not have been in the result (based on discussion in #98883).
This change makes sure to filter out unused rows in the default implementation of functions for ColumnReplicated in IFunction.cpp.

04036_columns_replicated_with_unreferenced_rows.sql before the change:

Received exception from server (version 26.3.1):
Code: 153. DB::Exception: Received from localhost:9000. DB::Exception: Division by zero: while executing 'FUNCTION intDiv(__table1.a :: 0, __table1.b :: 1) -> intDiv(__table1.a, __table1.b) UInt128 : 2'. (ILLEGAL_DIVISION)
(query: SELECT intDiv(t1.a, t1.b) FROM t1 JOIN t2 ON t1.a = t2.a LIMIT 3;)

04036_columns_replicated_with_unreferenced_rows.sql after the change:

0
0
0

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix exception in functions operating on ColumnReplicated with unreferenced rows produced by JOIN.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Mar 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c2e2504]

Summary: ✅


AI Review

Summary

This PR fixes a real correctness issue in ColumnReplicated execution by compacting unreferenced nested rows before running potentially-throwing functions, and adds a stateless regression test for intDiv on JOIN-produced replicated columns. I did not find additional high-confidence correctness, safety, concurrency, or performance issues beyond already-posted review threads; current revision looks ready to merge.

Missing context
  • ⚠️ No CI logs/report artifacts were provided in this review task, so validation here is code-path analysis plus test-diff inspection.
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 ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 16, 2026
@m-selmi
m-selmi requested a review from Copilot March 16, 2026 08:43

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

This PR fixes a correctness issue where ColumnReplicated could wrap a nested column containing rows that were never referenced by the JOIN/replication offsets, which can cause functions to evaluate on “should-have-been-dropped” values (e.g., division by zero).

Changes:

  • Extend convertOffsetsToIndexes to also compute a “referenced rows” filter, enabling removal of unreferenced rows before creating ColumnReplicated.
  • Apply this filtering in JOIN result materialization and ARRAY JOIN / expression replication paths when lazy replication is enabled.
  • Add a stateless regression test demonstrating the previous failure and the corrected output.

Reviewed changes

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

Show a summary per file
File Description
src/Columns/ColumnReplicated.h Introduces OffsetsToIndexesResult (indexes + filter) returned from convertOffsetsToIndexes.
src/Columns/ColumnReplicated.cpp Updates offsets→indexes conversion to support filtering out unreferenced source rows.
src/Interpreters/HashJoin/HashJoinResult.cpp Filters right-side columns before wrapping them in ColumnReplicated for lazy replication.
src/Interpreters/ExpressionActions.cpp Filters columns/inputs before lazy replication during ARRAY JOIN execution.
src/Interpreters/ArrayJoinAction.cpp Filters non-array columns before creating ColumnReplicated when lazy replication is used.
tests/queries/0_stateless/04036_columns_replicated_with_unreferenced_rows.sql Adds regression test for functions operating on lazily replicated JOIN output.
tests/queries/0_stateless/04036_columns_replicated_with_unreferenced_rows.reference Expected output for the new regression test.

Comment thread src/Columns/ColumnReplicated.cpp Resolved
Comment thread src/Columns/ColumnReplicated.h Resolved
@m-selmi
m-selmi requested a review from Avogar March 16, 2026 08:55
@Avogar Avogar self-assigned this Mar 16, 2026
@Avogar

Avogar commented Mar 16, 2026

Copy link
Copy Markdown
Member

I believe we are going to allow unused rows in ColumnReplicated anyway in #98883 to get better performance. So I suggest removing unused rows directly in the default implementation of functions for ColumnReplicated in IFunction.cpp instead of keeping the invariant that ColumnReplicated doesn't contain unused rows.

@m-selmi m-selmi changed the title Remove unused rows from column replicated Remove unused rows from column replicated during function execution Mar 17, 2026
@m-selmi m-selmi changed the title Remove unused rows from column replicated during function execution Remove unused rows from ColumnReplicated during function execution Mar 17, 2026
Comment thread src/Columns/ColumnIndex.cpp Resolved
@m-selmi

m-selmi commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

I believe we are going to allow unused rows in ColumnReplicated anyway in #98883 to get better performance. So I suggest removing unused rows directly in the default implementation of functions for ColumnReplicated in IFunction.cpp instead of keeping the invariant that ColumnReplicated doesn't contain unused rows.

That makes sense, I updated the change to handle in IFunction.

Comment thread src/Functions/IFunction.cpp Outdated
Comment on lines +490 to +494
/// Filter out unreferenced rows from nested columns.
ColumnIndex column_index(IColumn::mutate(common_replicated_indexes));
auto filtered_nested_columns = column_index.removeUnusedRowsInIndexedData(nested_columns);
common_replicated_indexes = column_index.getIndexes();
size_t nested_column_size = filtered_nested_columns.empty() ? 0 : filtered_nested_columns[0]->size();

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.

Let's try to avoid IColumn::mutate(common_replicated_indexes) when there is no unused rows, to avoid unnecessary indexes copy. Maybe we can first call ColumnIndex::buildUsedRowsFilter here to create a filter for used rows and filter nested columns and compact indexes only if the filter is not nullopt?

@m-selmi m-selmi Mar 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I moved it all into ColumnIndex to avoid splitting it into 2 steps

Comment thread src/Columns/ColumnIndex.cpp Outdated
Comment on lines +387 to +388
if (indexed_columns[i]->size() != indexed_data_size)
chassert(indexed_columns[i]->size() == indexed_data_size);

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.

Suggested change
if (indexed_columns[i]->size() != indexed_data_size)
chassert(indexed_columns[i]->size() == indexed_data_size);
chassert(indexed_columns[i]->size() == indexed_data_size);

Comment thread src/Functions/IFunction.cpp Outdated

/// Filter out unreferenced rows from nested columns.
ColumnIndex column_index(common_replicated_indexes);
auto [compact_common_replicated_indexes, filtered_nested_columns] = column_index.buildCompactIndexedColumns(nested_columns);

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.

I have an idea - filter unused rows only when isSuitableForShortCircuitArgumentsExecution returns true. This method is used in short-circuit functions execution when we have similar problem and can't execute function on some rows because they should be filtered. For some functions it's 100% safe to execute on any garbage so we can avoid filtering here for such functions. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, I'm also working on adding a similar canThrow() interface in functions that returns true if the function throws (should be a subset of isSuitableForShortCircuitArgumentsExecution). It's still WIP here #100127. Maybe we can use that instead to allow more functions to skip the filter?

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.

Yes, in this case canThrow interface will be even better.

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

I think we can merge it and add logic with canThrow later. WDYT?

@m-selmi

m-selmi commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

I think we can merge it and add logic with canThrow later. WDYT?

@Avogar Yes that makes sense, canThrow might take some time. I can use isSuitableForShortCircuitArgumentsExecution instead for now and merge this.

@clickhouse-gh

clickhouse-gh Bot commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 91.07% (102/112) | lost baseline coverage: 4 line(s) · Uncovered code

Full report · Diff report

@m-selmi
m-selmi requested a review from Avogar April 2, 2026 13:07
@Avogar
Avogar added this pull request to the merge queue Apr 2, 2026
Merged via the queue into ClickHouse:master with commit efc307d Apr 2, 2026
316 of 318 checks passed
@clickgapai

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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