Repository navigation
Conversation
A Buffer table whose declared column type differs from its destination's converts the parent column correctly but mishandles any requested subcolumn of it. StorageBuffer::read treats "the destination cannot resolve this name" as "the column is absent" rather than "it is derivable from a parent the Buffer converts anyway", which produces two distinct wrong behaviours. The name is erased from columns_intersection, so a subcolumn-only request empties the intersection and the destination is not read at all: with rows in both the destination and the in-memory buffer, only the buffer's rows come back. When the parent is also requested the destination is read, but addMissingDefaults runs before makeConvertingActions (the inverse order of StorageMerge), so the subcolumn is filled with a type default before the parent is converted, and is never revisited. Both shapes are reachable without any subcolumn syntax: at the default optimize_functions_to_subcolumns = 1 the analyzer folds length(arr) and empty(arr) into arr.size0, so with a destination arr String, a Buffer arr Array(UInt8) and one row '[1,2,3,4,5,6]', SELECT sum(length(arr)) returns 0 instead of 6. Nullable.null, Tuple elements, Map.keys/.values and nested arrays behave the same way. The parent is now read from the destination even when only the subcolumn was requested, derivable subcolumn names are excluded from the defaults list and from both conversion targets (addMissingDefaults would zero the name, and makeConvertingActions would throw THERE_IS_NO_COLUMN for a result column with no matching input), and the subcolumn is supplied by merging an extraction DAG into the existing conversion step. The extraction DAG is constructed from the post-conversion block, where the parent already has the Buffer's declared type and therefore exposes the subcolumn; built from the pre-conversion plan header it would come back empty and the fix would silently do nothing, so that case now throws instead. addMissingDefaults and createSubcolumnsExtractionActions are used unmodified. Two limitations are deliberately left. A filter predicate on the derived subcolumn still fails with NOT_FOUND_COLUMN_IN_BLOCK: restricting supportedPrewhereColumns would reject valid dynamic JSON/Dynamic subcolumn PREWHERE queries, which cannot be enumerated. Over a Distributed destination the query runs above FetchColumns with no post-read conversion point, so a requested subcolumn now raises an explicit NOT_IMPLEMENTED naming the column instead of an opaque UNKNOWN_IDENTIFIER; length(arr) returning the string length there is a separate root cause and is unchanged. One consequence of no longer skipping the destination: subcolumn-only queries now undergo the same pre-filter parent conversion that parent-requesting queries already undergo on master, where that conversion is merged into the row-policy actions. On a fixture whose policy-hidden row holds an unconvertible value, such a query now fails with the same error master already returns for SELECT arr on that fixture. Both are errors, never a wrong answer, and deferring the conversion past the filter would restructure that block for every Buffer query. Related: ClickHouse#110812
Three rows of 04709 asserted values that pristine master already produces, so they would have survived a full revert of the fix and proved nothing. The Map fixture declared the destination column as a Map too, so the destination resolved m.keys and m.values through its own type system and tryGetDerivableSubcolumn returned early. The destination parent is now Array(Tuple(String, UInt64)), which casts to the Buffer's Map but exposes no such subcolumn, so the derivation runs. The expected values are unchanged, since they were already the correct answer; what changes is that master no longer produces them. The Nullable carrier is genuinely affected but its value oracle cannot move: the correct .null for a non-NULL row is 0, exactly the type default master fills in, and a non-zero value would need a NULL in a destination column that is non-Nullable by construction. A row-count assertion is added beside the existing row, which loses its only row on master. The value row is kept as a guard. The Distributed refusal asserted only the error code, but StorageBuffer::read throws an unrelated NOT_IMPLEMENTED on the same engine combination, so a regression that relocated the failure there would still have passed. Since TestHint has no message-matching form, the message is now asserted from a companion shell test, which also fails if the diagnostic stops naming the column. Every row was measured on both binaries: nine differ and six are identical by design. Both tests pass 50 randomized runs at -j1 and at -j4, so neither needs a no-parallel tag. Related: ClickHouse#110812
…mn admission Master `e62b2d51` (`Admit subcolumns into PREWHERE only for storages that can resolve them`) gave `StorageBuffer` a `supportedPrewhereColumns` contract that deliberately admits type-mismatched columns, plus `supportedPrewhereColumnsIncludeSubcolumns` forwarding to the destination. A plain `WHERE` on a derived subcolumn of a mistyped `Buffer` is therefore now auto-moved into `PREWHERE`, which is the path this change interacts with.
A subcolumn derived after the destination read needs its parent column in the read's output block. StorageBuffer::read requests the parent, but when the forwarded PREWHERE or row-level filter consumes it the parent is neither an input nor an output of the filter's own ActionsDAG, so ActionsDAG::updateHeader drops it: it matches a DAG input, is marked for removal, and is re-inserted only if it is also an output. addMissingDefaults then fabricates a type default for the now-absent parent, and the extraction step casts that default instead of the data. On a fixture where every array is valid that surfaced as CANNOT_READ_ARRAY_FROM_TEXT, and for a Map parent as a silently empty result, while the parent's own value converted correctly in the same row. Restore each derivable parent as an output of the merged filter actions before trimming them, mirroring restoreDAGInputs in ReadFromMergeTree, which does the same for sorting-key, merging-params and sampling columns; addReadColumn there documents this exact mechanism. Deduplicate on both sides: two subcolumns can share one parent, and the planner already adds selected read columns to the PREWHERE outputs, so an unconditional append would duplicate a column in the read's header. tryRestoreColumn returning false means the parent is nowhere in the DAG, which would silently reintroduce the bug, so it throws instead. The loop body never runs when no subcolumn is derivable, and EXPLAIN header is byte-identical to before the change for a Buffer whose types match. The previously wrong shapes now return the correct value: a filter consuming the parent, a plain WHERE on the derived subcolumn, a row policy on a parent expression, and two subcolumns of one parent.
…filters StorageBuffer::read forwards the row-level filter and the PREWHERE filter through merge_converting_prefix, which is therefore invoked twice, and both merged DAGs run as consecutive PrewhereExprSteps over the same block. Keeping the parent alive in both prefixes made each of them carry the converting half's CAST as an output, so the first step already emitted the parent at this table's type while the second step's CAST was still bound to the destination type at build time. Reading a derivable subcolumn with both a row policy and a PREWHERE present then failed with CANNOT_READ_ARRAY_FROM_TEXT, including on shapes that returned the correct value before the parent was kept alive at all. Track which parents an earlier filter already converted and re-declare those source columns at this table's type for the next invocation, so makeConvertingActions finds the types equal and adds no CAST node. The conversion happens exactly once per parent regardless of how many filters are forwarded, and the restore itself is unchanged because suppressing it in the first prefix makes the second prefix miss a column it needs. The set stays empty when no subcolumn is derivable, so a mistyped column that no filter references still loses both its output and its input to removeUnusedActions and is converted only after the read, as before. Tests cover both filters together across the parent-consuming, ordinary, matched-types and no-subcolumn-requested shapes, asserting the parent beside the subcolumn plus a row count rather than an aggregate, since a bare sum cannot distinguish row loss from a defaulted value.
Internal second-model review (4 review rounds, 2 replans; click to expand)Each round: an independent review by a model that did not write the change, plus an independent Round 4 (
Round 4 - disagreed, with evidence ❌ "The new ❌ "A row policy whose predicate references the derived subcolumn is a new unsupported carrier." Earlier rounds, in brief Round 1 ( Rounds 2 and 3 each found one blocker that was a REGRESSION invisible against master alone, which Validation - the new rows were A/B'd against the immediately preceding commit, not only Session id: cron:clickhouse-review-slot-8:20260804-035400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-8:20260804-035400 |
|
cc @novikd @Avogar, could you review this? A |
|
@groeneai isn't there a PR already fixing this? |
|
No. I rechecked at publication and again now. The measured table in the description comes from a binary built from master #112326 is the closest of them. It constrains which #110844 is related in the opposite direction: this PR is its prerequisite, not a duplicate (I noted the partial discharge there in #110844 (comment)). #112975 and #113057 are the same defect class in If you had a different PR in mind, point me at it and I will close this one. |
|
Workflow [PR], commit [6216edc] Summary: ❌
AI ReviewSummaryThis PR fixes a real Final Verdict
|
CI finish ledger - ff0c980CI is fully finished on this head: 175 checks reported, 155 success, 20 skipped, 0 failures.
Session id: cron:our-pr-ci-monitor:20260804-120000 |
|
The separately-tracked defect that description mentions, a row policy and a |
|
One measurement worth adding to root cause 1's description, from re-testing this area on master Defaulting a derivable subcolumn instead of deriving it is also an access-control consequence, not CREATE TABLE dst (k UInt64, arr String) ENGINE = Log;
CREATE TABLE buf (k UInt64, arr Array(UInt8))
ENGINE = Buffer(currentDatabase(), dst, 1, 100, 100, 10, 100, 10000, 10000000);
INSERT INTO dst VALUES (9, '[4,5]');
INSERT INTO buf VALUES (1, [1,2,3]);
SELECT k, arr, arr.size0 FROM buf ORDER BY k SETTINGS optimize_functions_to_subcolumns = 0;
-- 1 [1,2,3] 3
-- 9 [4,5] 0 <- real size is 2
CREATE ROW POLICY p ON buf USING arr.size0 = 0 TO ALL;
SELECT k FROM buf ORDER BY k SETTINGS optimize_functions_to_subcolumns = 0;
-- 9 <- no row has size 0, so the policy should hide bothI have not built this branch, so I am reporting the master behaviour only, not a before/after. If Separately, the limitation this description discloses (an explicit |
|
Dear @Avogar, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…alyzer arms Master moved under this branch in two ways that made its own tests fail in the merge queue. Neither is a defect in this change. optimize_functions_to_subcolumns rewrites a predicate over a Map to read the keys subcolumn. A destination that declares the column as the equivalent Array(Tuple(...)) has no such subcolumn, so 04741 died with NOT_FOUND_COLUMN_IN_BLOCK on an arm that is only a control here. That reproduces byte-identically on a pristine master binary, so it is independent of this change, and ClickHouse#113231 fixes it. The setting is randomized by the test runner too, so the test pins it and stays on the forwarded filter's own conversion, which is what it is for. enable_analyzer can no longer be set to 0: the analyzer is mandatory since v26.9 and such a query returns SETTING_CONSTRAINT_VIOLATION. The arms that repeated an assertion under the old analyzer go away with their reference lines. The two-filter arm on the MergeTree destination asserted rows only, and the planner can apply a row policy above the read instead of forwarding it into the read, which returns the same rows through the single-PREWHERE path. It now asserts the plan the way the File and URL tests already do. All four tests still fail on a pristine master binary, which is what makes them witnesses: 04741 aborts it with 'Bad cast from type DB::ColumnMap to DB::ColumnArray', 04743 returns TYPE_MISMATCH, 04812 and 04849 kill the server.
ClickHouse#113900 rewrote the forwarded-filter prefix in `StorageBuffer::read` so that an output which merely passes a destination column through keeps the destination's type, and only the predicate is converted. That makes this branch's previous two-filter handling (a per-invocation converting DAG plus a set of parents converted by an earlier filter) unnecessary: with every pass-through at the destination type, consecutive forwarded filters agree on the parent's type by construction. Resolution, on top of ClickHouse#113900's shape: - keep master's single converting DAG, with the derivable subcolumns excluded from its target (the destination cannot produce them); - a parent kept alive for a derivable subcolumn is registered as a pass-through output, so ClickHouse#113900 re-points it to the destination-typed input instead of the converted one, and it is converted once, after the read, together with the subcolumn extraction. Also drop the `enable_analyzer = 0` row from the test: the old analyzer is removed on master.
The design narration (why the extraction binds to the converted parent, why derivable names stay out of the conversion targets) is in the PR description and the earlier commit messages. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With optimize_functions_to_subcolumns randomized to 0 the query reads the parent, which master already converts correctly, so the row passed without the fix (7 on both). Pinned to 1, master returns 4 and the fix 7. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+28.64 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 6216edcEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None of these touches the Session id: cron:our-pr-ci-monitor:20261003-183140 |
|
Fixing PR for 05237_with_expression_alias_scope_descendant (DBReplicated): #123774 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results and silent row loss when reading a subcolumn (for example
arr.size0orc.null) from aBuffertable whose declared column type differs from the destination table's. Such a subcolumn is now computed from the converted parent column instead of being filled with a default value or causing the destination table to be skipped.Description
Related: #110812
On 26.8.10.6,
SELECT sum(length(arr)) FROM bufover aBufferdeclaringarr Array(UInt8)above a destination columnarr Stringreturns 0 instead of 3, on default settings.A
Bufferwhose declared column type differs from its destination's converts the parent column correctly but mishandles any subcolumn of it: a subcolumn-only select loses the destination's rows, and selecting both returns a correct parent beside a defaulted subcolumn.optimize_functions_to_subcolumnsfoldslength(arr)intoarr.size0by default, hence the result above.Nullable.null,TupleandMapsubcolumns are affected.Two root causes, both fixed:
StorageBuffer::readtreats "the destination cannot resolve this name" as "the column is absent" rather than "derivable from a parent the Buffer converts anyway", so the name is either erased from the column intersection (emptying it, which skips the destination) or defaulted byaddMissingDefaults, which runs before the conversion. The fix reads the parent, keeps derivable names out of the defaults list and both conversion targets, and supplies them from an extraction DAG over the converted block.A forwarded
PREWHEREor row-level filter consumes the parent, and a column matched to a filter DAG's input is dropped unless it is also an output, so the derivation read back a fabricated default. Each derivable parent is now kept as a pass-through output of the merged filter actions, so, like the other pass-throughs since Keep a Buffer's forwarded filter from retyping the destination's own columns #113900, it leaves the read at the destination's type and is converted with the rest of the block. The loop is inert when nothing is derivable.Two limitations remain. An explicit
PREWHEREon the derived subcolumn still fails withNOT_FOUND_COLUMN_IN_BLOCK, though a plainWHEREon it is fixed. Over a Distributed destination there is no post-read conversion point, so a requested subcolumn now raises an explicitNOT_IMPLEMENTEDnaming the column rather than an opaqueUNKNOWN_IDENTIFIER.Measured on master
3ade111d(contains #113900) vs. with the fix (same script, same machine)Fixture: destination
arr String, bufferarr Array(UInt8).SELECT count() FROM (SELECT arr.size0 FROM buf)SELECT arr, arr.size0 FROM buf(optimize_functions_to_subcolumns = 0)[1,2,3] 0[1,2,3] 3... 3SELECT sum(length(arr)) FROM buf(default settings)SELECT k, arr.size0 FROM buf PREWHERE has(arr, 2)1 01 31 3SELECT k, arr.size0 FROM buf WHERE arr.size0 = 31 31 3SELECT m.keys, m.values FROM mbuf PREWHERE mapContains(m, 'k')[] []['k'] [7]['k'] [7]SELECT arr, arr.size0over a Distributed destinationCode: 47Code: 48 NOT_IMPLEMENTEDUnchanged: equal types, destination missing the column entirely (defaults and warning preserved), a
mismatch on an ordinary column with no subcolumn requested, a dynamic
JSONsubcolumnPREWHERE, anda matched-types Buffer-over-Distributed subcolumn query.
A row policy and a
PREWHEREboth over the same parent was a separate defect (wrong values, and a debug-build abort over aMapbuffer), fixed on master by #113900. The restored parent takes its pass-through path, and its tests pass with this change.Workflow [PR]
Sync PR [sync-upstream/pr/113231]