Skip to content

Fix virtual row materializing the IN-set ColumnSet placeholder under WITH FILL INTERPOLATE - #112344

Closed
zlareb1 wants to merge 1 commit into
ClickHouse:masterfrom
clickgapai:fix-virtual-row-column-set
Closed

zlareb1 wants to merge 1 commit into
ClickHouse:masterfrom
clickgapai:fix-virtual-row-column-set

Conversation

@zlareb1

@zlareb1 zlareb1 commented Jul 28, 2026

Copy link
Copy Markdown
Member

Closes: #111831

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 Cannot insert element into Set (NOT_IMPLEMENTED) exception when a query combines read_in_order_use_virtual_row with ORDER BY ... WITH FILL INTERPOLATE and an IN filter whose ColumnSet placeholder is kept alive in the in-order read header.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

setVirtualRow built a default-valued constant for every header column absent from the primary-key block. For columns that hold no data this throws: the ColumnSet placeholder of an IN predicate, kept alive in the in-order read header by INTERPOLATE, is an IColumnDummy, and IColumnDummy::insert fails with Cannot insert element into Set from MergingSortedTransform. The virtual row's non-sort-key columns are never read, so for dummy columns the fix reuses the header placeholder via cloneResized(1) instead of materializing a default value.

Found by the optimizer test framework (tests/optimizer_tester).

🤖 Generated with Claude Code

setVirtualRow built a default-valued constant for every header column absent
from the PK block. For columns that hold no data this throws: the ColumnSet
placeholder of an IN predicate, kept alive in the in-order read header by
INTERPOLATE, is an IColumnDummy, and IColumnDummy::insert fails with
"Cannot insert element into Set" (NOT_IMPLEMENTED) from
MergingSortedTransform. The virtual row's non-sort-key columns are never
read, so reuse the header's own placeholder resized to one row instead.

Closes: ClickHouse#111831

Co-Authored-By: Claude Fable 5 <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


ClickGap AI Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0820919]

Summary: ❌

job_name test_name status info comment
Stress test (amd_msan) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb

AI Review

Summary

This PR fixes the reported ColumnSet virtual-row exception in setVirtualRow and adds a focused stateless regression for that case. The remaining problem is that the new guard is still narrower than the invariant it is relying on: Set is not the only non-creatable helper column that can survive in these headers, so the same virtual-row materialization path can still throw for lambda-backed Function helpers.

Findings

⚠️ Majors

  • [src/Processors/Merges/Algorithms/MergeTreeReadInfo.cpp:88] The fix only special-cases IColumnDummy carriers. Function helper columns introduced by lambda expressions are also non-creatable header columns, so virtual-row materialization still falls through to createColumnConstWithDefaultValue(1) and can throw from ColumnFunction::insert when such a helper survives in the read-in-order header.
    Suggested fix: handle the full isNotCreatable(col.type) class here, or reuse col.column->cloneResized(1) for missing virtual-row columns instead of only the isDummy() subset.
Tests
  • ⚠️ The new stateless test locks in the Set case only. Add one focused regression that keeps a lambda-backed helper column alive through the same read_in_order_use_virtual_row + WITH FILL INTERPOLATE path, so the generalized fix is covered too.
Final Verdict

Changes needed: the current patch fixes the reported Set case, but it still leaves the same virtual-row exception path open for Function helper columns.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 28, 2026
if (const auto * col_const = typeid_cast<const ColumnConst *>(nested))
nested = &col_const->getDataColumn();

if (nested && nested->isDummy())

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.

This guard is still too specific to IColumnDummy carriers. Lambda helpers are represented as DataTypeFunction / ColumnFunction, and those can also survive in these headers via the same constant-input machinery that kept the Set placeholder alive. They are non-creatable too: createColumnConstWithDefaultValue(1) reaches ColumnFunction::insert, which throws.

That means a multi-part read_in_order_use_virtual_row query that keeps a lambda helper in the header will still hit the same user-visible exception here. Please broaden this to the general non-creatable case, for example by checking isNotCreatable(col.type) or by reusing col.column->cloneResized(1) for missing virtual-row columns instead of only the isDummy() subset.

@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.30% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@zlareb1

zlareb1 commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

@groeneai check this PR and see if it is still required. If yes, open a new PR with the fix

@zlareb1 zlareb1 closed this Aug 3, 2026
@groeneai

groeneai commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Still required. It reproduces on today's master (tested 8c925c5bbe97; MergeTreeReadInfo.cpp there is byte-identical to the master tip, blob b9506fd82207, unchanged since May):

Code: 48. DB::Exception: Cannot insert element into Set: While executing MergingSortedTransform. (NOT_IMPLEMENTED)

I will open a new PR with a fix, closing #111831 and crediting this one.

One measurement on the AI review's request to generalize the guard to the whole isNotCreatable class: I could not find a ColumnFunction carrier that is a virtual-row regression. The lambda-helper query it suggests fails with read_in_order_use_virtual_row = 0 as well (Cannot insert into Function), so it is a separate pre-existing WITH FILL problem rather than something the virtual row breaks. I will scope my fix to carriers that pass with the setting off, and report back if I find a Function one that qualifies.

@groeneai

groeneai commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Fix opened as #113264, closing #111831 and crediting this PR.

Reporting back on the Function question as promised: I found no ColumnFunction carrier that
qualifies. Nine shapes do (literal IN, subquery IN, NOT IN, tuple IN, has, reverse read,
read_in_order_use_virtual_row_per_block = 1, SELECT DISTINCT, and the LowCardinality,
Nullable, Array and Map variants), and every one of them passes with
read_in_order_use_virtual_row = 0 and failed with it on. The lambda shape still fails with the
setting off, at VirtualRowTransform rather than MergingSortedTransform, so it stays out of
scope as a separate pre-existing WITH FILL defect. The fix uses the isNotCreatable predicate
because it is the existing name for "types that cannot produce a default-valued column", not
because Function is a carrier, and the description says so.

pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Aug 10, 2026
Closes: ClickHouse#111831
Related: ClickHouse#112344

A MergeTree read-in-order query combining WITH FILL INTERPOLATE with an IN or
has filter failed with "Cannot insert element into Set" (NOT_IMPLEMENTED) when
read_in_order_use_virtual_row was enabled, while the same query succeeded with
the setting off. A plan optimization was breaking a correct query.

setVirtualRow fills every header column absent from the part's primary-key block
with createColumnConstWithDefaultValue(1), whose precondition is a creatable
type. The second argument of an IN predicate is a Set placeholder column, and
INTERPOLATE keeps it in the read header because the interpolate step declares
every current header column as an input, so the placeholder survives column
pruning all the way down into ReadFromMergeTree. DataTypeSet overrides both
createColumn and getDefault successfully, so the failure landed one level deeper
in IColumnDummy::insert.

Reuse the header's own placeholder column resized to one row for types that
cannot produce a default value, using the existing isNotCreatable predicate.
This is sound because the virtual row is never emitted (skip_last_row, then
removeTop without insert) and only its sort columns are ever compared. Reusing
col.column rather than building a fresh one keeps the real FutureSetPtr alive
inside the ColumnSet.

isNotCreatable is used because it is the codebase's existing name for "types
that cannot produce a default-valued column", not because Function is a carrier
of this bug. It is not: the lambda shape fails with the setting off as well, at
VirtualRowTransform rather than MergingSortedTransform, which is a pre-existing
WITH FILL defect and needs its own issue.

Validated on a debug build: the failure reproduces deterministically pre-fix,
and post-fix every shape returns results identical to the
read_in_order_use_virtual_row = 0 ground truth, including LowCardinality,
Nullable, Array, Map, an empty set and a 10000-element set. The new test fails
on the pre-fix binary and passes after, 100/100 green over 50 randomized plus 50
forced-setting runs.

The original analysis is @ zlareb1's, from the closed PR ClickHouse#112344.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read_in_order_use_virtual_row + WITH FILL INTERPOLATE + IN filter throws Cannot insert element into Set (NOT_IMPLEMENTED)

3 participants