Skip to content

Fix logical error when a lambda argument is a backquoted dotted identifier - #108735

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:fix-astidentifier-empty-part-lambda-arg
Jul 21, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:fix-astidentifier-empty-part-lambda-arg

Conversation

@groeneai

@groeneai groeneai commented Jun 28, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Fix a logical error (!part.empty()) when a lambda argument is a backquoted identifier whose name contains a dot (for example a lambda argument named __table1.). Such a query previously produced a handled exception in release builds and aborted the server in debug and sanitizer builds while formatting an error message.

Description

A lambda argument name is an atomic identifier: the query tree builder (QueryTreeBuilder) validates that it consists of a single AST identifier part. LambdaNode then built the argument IdentifierNode via Identifier{name}, which invokes the split-on-dot Identifier(String) constructor. For a backquoted name containing a dot this split produced an empty trailing part (["__table1", ""]), inconsistent with the single-part origin.

When the query tree was later converted back to AST for an error message (for example a TYPE_MISMATCH while validating a column ALIAS whose declared type does not match the lambda expression), IdentifierNode::toASTImpl reconstructed an ASTIdentifier from those parts and the ASTIdentifier constructor asserted !part.empty(), aborting the server in debug and sanitizer builds.

The fix builds the argument Identifier from a single part so the atomic name is not split. Resolution is unchanged: lambda arguments are bound by the identifier full name, which is identical either way.

Found by the targeted AST fuzzer (STID 2508-2ef2, Logical error: '!part.empty()'). The bug is pre-existing and independent of any specific PR. It reproduces deterministically:

CREATE TABLE t (x UInt8, c Map(Point, DateTime) ALIAS arrayMap(`__table1.` -> toString(x), [0])) ENGINE = MergeTree ORDER BY x;

No related open issue found.

Regression test: tests/queries/0_stateless/04367_lambda_arg_backquoted_dot_no_logical_error.sql.

Version info

  • Merged into: 26.7.1.1304 (included in 26.7 and later)

…ifier

A lambda argument name is an atomic identifier (the query tree builder
validates it consists of a single AST identifier part). LambdaNode built
the argument IdentifierNode via Identifier{name}, which uses the
split-on-dot Identifier(String) constructor. For a backquoted name that
contains a dot (e.g. `__table1.`) this split produced an empty trailing
part (["__table1", ""]). When the query tree was later converted back to
AST for an error message, the ASTIdentifier constructor asserted
!part.empty() and aborted the server in debug/sanitizer builds.

Build the argument Identifier from a single part so the atomic name is
not split. Resolution is unchanged because lambda arguments are bound by
the identifier full name, which is identical either way.

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

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (x UInt8, c Map(Point, DateTime) ALIAS arrayMap(__table1. -> toString(x), [0])) ENGINE = MergeTree ORDER BY x; aborts on a debug build every time. Also SELECT arrayMap(__table1. -> (__table1., __table1.), [1,2]) :: Map(Point, DateTime);.
b Root cause explained? A lambda argument is validated as a single AST identifier part. LambdaNode built it via Identifier{name} (the split-on-dot Identifier(String) ctor), so a backquoted dotted name like __table1. split into ["__table1", ""] (empty trailing part). On error formatting, IdentifierNode::toASTImpl reconstructs an ASTIdentifier from those parts, whose ctor asserts !part.empty() and aborts in debug/sanitizer builds.
c Fix matches root cause? Yes. Build the argument Identifier from a single part (Identifier{std::vector<std::string>{name}}) so the atomic name is never split. Targets the mechanism directly, not the assertion.
d Test intent preserved / new tests added? New regression test 04367_lambda_arg_backquoted_dot_no_logical_error added: dotted lambda-arg names yield a clean TYPE_MISMATCH (server stays up) and bind correctly in the lambda body.
e Both directions demonstrated? Yes. The test ABORTS the server on the unfixed binary (<Fatal> Logical error: '!part.empty()') and passes 5/5 on the fixed binary. Build IDs verified distinct (unfixed aba0d97e vs fixed 41543ac6).
f Fix is general across code paths? Yes. LambdaNode ctor is the only place that builds a lambda-argument Identifier from a user-supplied name; the lambda body and the synthetic-lambda path (resolveFunction.cpp, names like __function_ref_arg_N) already use the parts ctor / dotless names. Verified arrayMap(plus, ...)/arrayMap(toString, ...) still work.
g Fix generalizes across inputs? Yes. Verified the full dotted-name matrix: trailing dot (x.), leading dot (.x), double dot (a..b), embedded dot (a.b), __table1., plain names, and multi-arg lambdas. Each gives a clean exception or correct result; none abort. A dotted name now also round-trips correctly (rendered as a single backquoted token).
h Backward compatible? Yes. No setting, format, wire, or on-disk change. Resolution is identical because lambda args bind by identifier full name, which is unchanged.
i Invariants and contracts preserved? Yes. Identifier invariant "no empty parts for an atomic name" is now upheld; getFullName() is identical ("__table1.") so the resolver binding key is preserved; ASTIdentifier ctor precondition (!part.empty() for the no-params branch) now holds on the lambda-argument round-trip path.

Session id: cron:clickhouse-worker-slot-3:20260628-100300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @novikd @vdimir — could you review this? A lambda argument is validated as a single identifier part, but LambdaNode built it via the split-on-dot Identifier(String) ctor, so a backquoted dotted name (e.g. __table1.) became ["__table1", ""] with an empty trailing part; converting it back to AST for an error message then tripped ASTIdentifier's !part.empty() assertion. The fix builds the argument Identifier from a single part (binding is unchanged since it keys on the full name).

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [86f7932]

Summary: ✅


AI Review

Summary

This PR fixes the analyzer's handling of backquoted lambda argument names that contain dots by preserving each argument as a single identifier part inside LambdaNode, and it adds focused regression coverage for both the handled TYPE_MISMATCH paths and successful name binding. I did not find a remaining correctness, compatibility, or evidence gap in the touched code paths, and the current Praktika report for PR #108735 is clean.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 399d7a6

Every failure below has an owner. Only CH Inc sync is exempt (it passed here anyway).

Check / test Reason Owner
Stress test (arm_debug) — Unexpected exception in refresh scheduling (STID 2508-34af) flaky / trunk-bug (chronic RefreshTask MV scheduling LOGICAL_ERROR: 33 master hits + 100+ unrelated PRs in 30d; unrelated to this PR's LambdaNode.cpp change) a fix task is investigating this RefreshTask scheduling family at full effort (fixing-PR link to follow here)
Stress test (arm_debug) — Cannot start clickhouse-server / Check failed consequence of the abort above same owner
Mergeable Check / PR aggregator artifact of the one stress failure resolves when the owner lands

This PR's lambda-identifier fix is unrelated to the RefreshTask scheduling subsystem; the stress failure is a pre-existing trunk finding, not caused by this diff.

Session id: cron:our-pr-ci-monitor:20260629-010000

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

Thanks for taking the time to improve the project!

Pick up the revert of ClickHouse#105591 (vector-search rescoring row-positioning)
so 02354_vector_search_rescoring passes. The prior branch base predated
the revert and carried the reverted-then-broken change, causing 02354 to
fail across every sanitizer variant (a stale-base reintroduction of a
reverted master bug, not caused by this LambdaNode fix).
@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 5728754

CI fully finished on this head. Every failure has an owner.

Check / test Reason Owner / resolution
Stateless tests (13 variants) :: 02354_vector_search_rescoring Stale-base reintroduction of a reverted master bug (NOT this PR's diff, which is a LambdaNode analyzer fix). Master fixed it by reverting #105591 ("Apply exact row-positioning for vector search rescoring queries") in e531152; this branch's base predated the revert and still carried the buggy d56213874eb9a merge. 46 master fails in a single 2026-06-29 11:48-13:32 window, then 0 on master for 5 days; 0 master since. Fixed by merging current master @ 089c9850a8cd9 to pick up revert e531152 (2890 commits behind). CI re-runs clean.

The LambdaNode backquoted-dotted-identifier fix (STID 2508-2ef2) and its regression test are unchanged and intact after the merge (net src delta vs master = only src/Analyzer/LambdaNode.cpp, +3/-1). @vdimir already APPROVED the fix.

Session id: cron:our-pr-ci-monitor:20260704-180000

@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 089c985

CI fully finished on this head (the master-merge that picked up the reverted 02354_vector_search_rescoring fix). The LambdaNode fix itself is green. Every failure below has an owner; only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Fast test (arm_darwin) / 04056_npy_large_shape_validation flaky (numpy ModuleNotFoundError, macOS runner) #108859 (external, merged) — provisions numpy/pandas on the Darwin runner; residual is a single un-reprovisioned host draining
Fast test (arm_darwin) / 03321_forwarded_for flaky (pandas ModuleNotFoundError, macOS runner) #108859 (external, merged) — same
Integration tests (amd_tsan, 2/6) / test_storage_s3_queue test_shards_distributed[ordered-2] flaky (2 unrelated PRs / 0 master, 14d) a fix task is created (investigating at full effort; fixing-PR link to follow here)
Performance Comparison (arm_release, master_head, 4/6) flaky (arm perf noise, "Errors while building the report"; 48 unrelated PRs / 7d, all test rows success/unstable) a fix task is moved to pending (investigating at full effort; fixing-PR link to follow here)
Mergeable Check / PR rollup of the above resolves when the owners above land
CH Inc sync private upstream sync CH Inc sync (private, not actionable by us)

None of these are PR-caused: this PR changes one line in src/Analyzer/LambdaNode.cpp (backquoted dotted lambda-argument identifier) plus a stateless regression test; it cannot affect the macOS Python-module runner env, an S3Queue TSAN integration test, or arm perf-report building.

Session id: cron:our-pr-ci-monitor:20260704-223000

@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

Ledger update — test_storage_s3_queue test_shards_distributed[ordered-2] (amd_tsan) owner resolved to fixing PR #107083

The previous ledger entry marked this line "a fix task is created (fixing-PR link to follow here)". The investigation is done and the fixing-PR link follows.

This failure is not an S3Queue bug. The test failed in cluster.shutdown() with Exception: Sanitizer assert found for instance zoo2-1. zoo2's docker.log holds a Keeper ThreadSanitizer report:

SUMMARY: ThreadSanitizer: data race src/Coordination/KeeperCommon.h:51:8
  in DB::KeeperResponseForSession::operator=(DB::KeeperResponseForSession&&)
  Read  (T14): NonblockingBoundedQueue::tryPop  (NonblockingBoundedQueue.h:138) <- KeeperRequestDispatcher::responseThread()
  Write (T34): NonblockingBoundedQueue::tryPush (NonblockingBoundedQueue.h:107) <- KeeperRequestDispatcher::onResponse
  "As if synchronized via sleep"; Location is the queue's Slot heap array

It is the known Vyukov-queue TSAN false positive on slot.value in NonblockingBoundedQueue (introduced by the merged KeeperDispatcher overhaul #101757). The slot.pos acquire/release edges make the race impossible; TSAN just cannot see the happens-before through the retry/backoff. It surfaces at cluster teardown across 25+ unrelated amd_tsan integration tests in 30d (test_keeper_4lw_reconfiguration, test_drop_replica, test_backup_restore_on_cluster, test_zookeeper_fallback_session, ...), so the test_shards_distributed[ordered-2] name is incidental. Zero link to this PR's one-line src/Analyzer/LambdaNode.cpp change.

Owner / fixing PR: #107083 (external, open) — adds NO_SANITIZE_THREAD to tryPush/tryPop in src/Common/NonblockingBoundedQueue.h (keeps atomic instrumentation, so slot.pos happens-before stays visible). The annotation is still absent from current master, so the fix is genuinely pending.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108735&sha=089c9850a8cd9187f4082f446a6b24906fe0a8b9&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%202%2F6%29

Session id: cron:clickhouse-worker-slot-1:20260704-225500

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, the failed tests were fixed on master. Please update the branch.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Branch updated onto current master (891c628). CI re-running.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 891c628

Every failure below has an owner. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_msan, 7/8) / test_replicated_database/test.py::test_replicated_table_structure_alter flaky (chronic: 41 master hits + 20+ unrelated PRs in 30d; a Replicated-DB DDL-log recovery race, unrelated to this PR's one-line LambdaNode.cpp fix) #110030 (ours, open)

Not PR-caused: this PR only changes src/Analyzer/LambdaNode.cpp (+ the 04367 regression test). @vdimir's approval stands and the branch was refreshed onto master per @alexey-milovidov's request.

Session id: cron:our-pr-ci-monitor:20260714-070000

@alexey-milovidov

Copy link
Copy Markdown
Member

Branch updated onto current master (86f7932); the LambdaNode.cpp fix compiles cleanly against the new base. Also changed the changelog category to "Not for changelog" per the AI review: release builds already returned a handled exception, only debug/sanitizer builds aborted. The sole CI red on the previous head was the known flaky test_replicated_table_structure_alter (#110036).

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-bugfix Pull request with bugfix, not backported by default labels Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.10% 92.00% -0.10%
Branches 78.30% 78.30% +0.00%

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

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 21, 2026
Merged via the queue into ClickHouse:master with commit 8519cd1 Jul 21, 2026
180 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 22, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Jul 27, 2026
1 task done
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…empty-part-lambda-arg

Fix logical error when a lambda argument is a backquoted dotted identifier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants