Skip to content

Preserve constant columns of ActionsDAG input nodes in serialization - #107124

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix/actionsdag-input-constant-serialization
Jun 14, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix/actionsdag-input-constant-serialization

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 11, 2026 •

Copy link
Copy Markdown
Member

ActionsDAG::serialize skipped the column for all INPUT nodes:

const bool has_column = (node.type != ActionType::INPUT && node.column != nullptr);

However, INPUT nodes can legitimately carry a constant column: lambda capture DAGs reference constants from the enclosing DAG as inputs with a pre-set constant column (see addInputConstantColumnIfNecessary in PlannerActionsVisitor). When such a DAG is deserialized, every function node is re-resolved via IFunctionOverloadResolver::build, and functions that require constant arguments — tupleElement produced by tuple element access t.2 inside a lambda being the simplest example — throw:

Code: 43. DB::Exception: Second argument to tupleElement must be a constant Int, UInt or String. (ILLEGAL_TYPE_OF_ARGUMENT)

This is reachable today by any distributed query with serialize_query_plan = 1 whose projection contains a lambda with a constant argument that is evaluated on the shard, e.g.:

SET serialize_query_plan = 1, prefer_localhost_replica = 0;
SELECT arrayMap(t -> t.2, [(number, 'a'), (number + 1, 'b')]) FROM remote('127.0.0.1', numbers(3));

The initiator serializes the plan and ships it to the shard, where the capture DAG is deserialized and tupleElement is re-resolved without its constant index, so the query fails. (The array must depend on the row; a fully constant array is folded away before serialization.) The setting is off by default, so stable releases are not affected unless it is enabled; it is also a prerequisite for the upcoming query plan cache, which round-trips plans containing lambda captures on every cache hit.

The fix serializes the column for any node that has one. The format remains self-describing through the existing per-node column_flags, so old readers handle new bytes and vice versa.

Covered by 04338_actions_dag_input_constant_serialization, a functional test that runs the above query shapes over remote(...) with serialize_query_plan = 1; each one fails without the fix and passes with it.

Related: #99309

Changelog category (leave one):

  • Improvement

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

Fix an ILLEGAL_TYPE_OF_ARGUMENT error for distributed queries with serialize_query_plan = 1 that contain a lambda with a constant argument (e.g. arrayMap(t -> t.2, ...)). Constant columns of ActionsDAG INPUT nodes are now preserved during query plan serialization.

Documentation entry for user-facing changes

  • Documentation is written (no user-facing changes)

Version info

  • Merged into: 26.6.1.784

`ActionsDAG::serialize` skipped the column for all `INPUT` nodes. However,
inputs can legitimately carry a constant column: lambda capture DAGs reference
constants from the enclosing DAG as inputs with a pre-set constant column (see
`addInputConstantColumnIfNecessary` in `PlannerActionsVisitor`). On
deserialization every function node is re-resolved, and functions that require
constant arguments - e.g. `tupleElement` produced by tuple element access
`t.2` inside `arrayMap(t -> t.2, ...)` - threw `ILLEGAL_TYPE_OF_ARGUMENT`
("Second argument to tupleElement must be a constant Int, UInt or String").

Serialize the column for any node that has one. The format stays
self-describing through the existing per-node `column_flags`, so the change is
wire-compatible in both directions.

This blocks the query plan cache for complex queries, which round-trips plans
containing lambda captures; covered by a unit test that fails without the fix.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov
alexey-milovidov force-pushed the fix/actionsdag-input-constant-serialization branch from 337f9a3 to 65ee574 Compare June 12, 2026 19:50
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c0aeba1]

Summary: ✅


AI Review

Summary

This PR preserves constant columns on ActionsDAG INPUT nodes during query-plan serialization, which fixes deserialization of lambda capture DAGs requiring constants, such as tupleElement inside arrayMap. The implementation is narrow and uses the existing per-node column_flags, so the optional column payload remains self-delimiting; the added shard stateless test covers the reported distributed serialize_query_plan = 1 path.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 12, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

We need a functional test, not a unit test.

alexey-milovidov and others added 2 commits June 13, 2026 20:37
Per review feedback, cover the fix with a functional test instead of a
unit test.

`04338_actions_dag_input_constant_serialization` exercises the bug end to
end: with `serialize_query_plan = 1` and a `remote(...)` shard, a lambda
that contains `tupleElement` (e.g. `arrayMap(t -> t.2, ...)`) ships its
capture DAG to the shard, where the plan is deserialized and every
function node is re-resolved via `IFunctionOverloadResolver::build`. The
constant tuple index lives on an `INPUT` node of the capture DAG; before
the fix its column was dropped during serialization, so `tupleElement`
re-resolution on the shard failed with:

    Code: 43. Second argument to tupleElement must be a constant Int,
    UInt or String. (ILLEGAL_TYPE_OF_ARGUMENT)

The array is built from `number` so the lambda is evaluated on the shard
(a fully constant array is folded away before serialization). Verified
that all four queries fail without the fix and pass with it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Replaced the unit test with a functional test 04338_actions_dag_input_constant_serialization.

While writing it I found the bug is not actually masked by a fallback as the description originally claimed — any distributed query with serialize_query_plan = 1 whose projection contains a lambda with a constant argument evaluated on the shard fails today:

SET serialize_query_plan = 1, prefer_localhost_replica = 0;
SELECT arrayMap(t -> t.2, [(number, 'a'), (number + 1, 'b')]) FROM remote('127.0.0.1', numbers(3));
-- Code: 43. Second argument to tupleElement must be a constant Int, UInt or String. (ILLEGAL_TYPE_OF_ARGUMENT)

The test runs four such query shapes over remote(...); each fails without the fix and passes with it. Verified locally against a server with process_query_plan_packet enabled. Updated the PR description accordingly. The branch was also merged with master (it was ~400 commits behind).

@clickhouse-gh

clickhouse-gh Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.30% 92.40% +0.10%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 4/4 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · 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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Jun 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 14, 2026
Merged via the queue into master with commit dde0a5d Jun 14, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix/actionsdag-input-constant-serialization branch June 14, 2026 15:31
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 14, 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