Skip to content

Errors with alias columns and distributed tables and functions #97899

Closed

Description

@den-crane

Company or project name

https://www.innervate.com/

Describe what's wrong

query fails with the enabled analyzer

Does it reproduce on the most recent release?

Yes

How to reproduce

CREATE TABLE test
(
   x String,
   xx String alias concat(x, 'aaa')
)
ENGINE = MergeTree
ORDER BY x as SELECT 'x';

SELECT x, xx
FROM remote('127.0.0.{1,2}', default, test)
ORDER BY x ASC;

   ┌─x─┬─xx───┐
1. │ x │ xaaa │
2. │ x │ xaaa │
   └───┴──────┘

SELECT  now(), x, xx ---  now()
FROM remote('127.0.0.{1,2}', default, test)
ORDER BY x ASC;

DB::Exception: Cannot parse datetime: Cannot parse DateTime from String

SELECT
    generateUUIDv4(), x, xx 
FROM remote('127.0.0.{1,2}', default, test)
ORDER BY x ASC;

DB::Exception: Cannot parse uuid xaaa: Cannot parse UUID from String:

https://fiddle.clickhouse.com/5122e22a-0657-44d2-939e-4d5370d13ab5

set enable_analyzer=0;

   ┌───────────────now()─┬─x─┬─xx───┐
1. │ 2026-02-24 21:18:55 │ x │ xaaa │
2. │ 2026-02-24 21:18:55 │ x │ xaaa │
   └─────────────────────┴───┴──────┘
   ┌─generateUUIDv4()─────────────────────┬─x─┬─xx───┐
1. │ ca6fff7e-5b62-4361-8139-71ada17d9e63 │ x │ xaaa │
2. │ 344003a7-b043-44d3-ad3b-741ef8c40cca │ x │ xaaa │
   └──────────────────────────────────────┴───┴──────┘

https://fiddle.clickhouse.com/24588f6a-656e-4e10-a588-8bf16fee3030

Expected behavior

No errors

Error message and/or stacktrace

No response

Additional context

No response

Version info

Activity

  1. added
    potential bug
    on Feb 24, 2026
  2. added
    analyzer
    on Feb 25, 2026
  3. spider-yamet commented on Feb 25, 2026

    @spider-yamet
    Contributor

    @den-crane @Algunenano May I open a PR for this?

  4. added
    bug
    and removed
    potential bug
    on Feb 25, 2026
  5. den-crane commented on Feb 25, 2026

    @den-crane
    ContributorAuthor

    @den-crane @Algunenano May I open a PR for this?

    Yes, please. As this is an open-source project, you have the same privileges as all contributors, so you’re welcome to proceed without requesting prior approval.

  6. den-crane commented on Feb 25, 2026

    @den-crane
    ContributorAuthor

    it seems there is a silly workaround, create a column without alias against distributed table.

    alter table someMergeTree add column String alias concat(x, 'aaa');
    alter table someDistributed add column String;
    
  7. KochetovNicolai commented on Feb 27, 2026

    @KochetovNicolai
    Member
  8. UnamedRus commented on Feb 28, 2026

    @UnamedRus
    Contributor

    it seems there is a silly workaround, create a column without alias against distributed table.

    Isn't it essentially the same #88276 ?

    But, it should have been fixed by Misha. #94644 (but prev example still broken in master)

  9. fm4v commented on Jun 15, 2026

    @fm4v
    Member

    @groeneai please fix

  10. vdimir commented on Jun 15, 2026

    @vdimir
    Member

    @groeneai check if issue is fixed by github.com//pull/106404

  11. clickgapai commented on Jun 15, 2026

    @clickgapai
    Contributor

    Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

    TL;DR

    Errors with alias columns and distributed tables and functions

    ClickGap verified: reproduced it with the script below on master; tested 6 version(s) — 6 affected, 0 clean (see Affects); confirmed it predates the oldest available release (pre-existing).

    Reproduction

    -- Two-shard distributed read of a MergeTree table that has an ALIAS column,
    -- with a scalar function in the SELECT list. enable_analyzer=1 is the default.
    CREATE TABLE test
    (
        x  String,
        xx String ALIAS concat(x, 'aaa')
    )
    ENGINE = MergeTree
    ORDER BY x AS SELECT 'x';
    
    -- Works (no scalar function):
    SELECT x, xx
    FROM remote('127.0.0.{1,2}', default, test)
    ORDER BY x ASC;
    --  x | xx
    --  x | xaaa
    
    -- Both FAIL with the analyzer enabled (default): the ALIAS value 'xaaa'
    -- (concat(x,'aaa')) is CAST into the scalar-function result column.
    SELECT now(), x, xx
    FROM remote('127.0.0.{1,2}', default, test)
    ORDER BY x ASC;
    -- DB::Exception: Cannot parse DateTime from String ... while converting source
    -- column `concat(x,'aaa')` to destination column ... DateTime  (CANNOT_PARSE_DATETIME)
    
    SELECT generateUUIDv4(), x, xx
    FROM remote('127.0.0.{1,2}', default, test)
    ORDER BY x ASC;
    -- DB::Exception: Cannot parse uuid xaaa: Cannot parse UUID from String ...
    -- while converting source column `concat(x,'aaa')` to destination column
    -- `generateUUIDv4()`  (CANNOT_PARSE_UUID)
    
    -- With SET enable_analyzer=0 both queries return correct results.
    

    ▶ Run on ClickHouse Fiddle

    Bisect

    • Present since at least v24.8.14.39 — reproduces on every release line tested down to there; the introducer predates the oldest release with a downloadable binary, so a single commit isn't pinnable from release binaries (not a recent regression).

    Affects

    • Reproduces on: master, 26.5, 26.4, 26.3, 26.2, 26.1

    Suggested next step

    Manual triage of the comp-query-analyzer path is needed. The reproducer above should be a fast first check.

    Component: comp-query-analyzer
    Severity: P2


    ClickGap · Finding: h_issue_97899

    Triage metadata

  12. groeneai commented on Jun 15, 2026

    @groeneai
    Collaborator

    @fm4v root cause found, deterministic repro on master (26.6.1, debug build). @vdimir (assignee) could you confirm the fix layer before I open a PR?

    The alias value is misrouted by a position-based header rename, not by alias inlining.

    Trigger (all required): enable_analyzer=1, 2+ shards, ORDER BY present, and a non-storage function column (now(), rand(), generateUUIDv4()) placed before a plain column and its ALIAS column. Single shard, no ORDER BY, or the function placed last all work. rand() fails the same way, so this is not constant-specific.

    For a remote read at WithMergeableState, getQueryPlanForTableExpression (src/Planner/PlannerJoinTree.cpp, the else if (!is_trivial_count_applied) branch around line 1583) inserts a rename from the ReadFromRemote plan header to the outer planner's expected header using ActionsDAG::MatchColumnsMode::Position. The two headers are in different column orders:

    ReadFromRemote header:  [__table1.x,  _CAST(now())::DateTime,  concat(__table1.x,'aaa')::String]
    expected header:        [__table1.x,  __table1.xx::String,     _CAST(now())::DateTime]
    

    Position matching pairs the shard's concat(x,'aaa') (String "xaaa") with the now() slot and emits _CAST(concat(x,'aaa'), DateTime), i.e. parse "xaaa" as DateTime -> CANNOT_PARSE_DATETIME. EXPLAIN actions=1 confirms: FUNCTION _CAST(concat(__table1.x, 'aaa') :: 2, DateTime :: 4) and final Positions: 0 4 5. So xx's value lands in the function column's slot.

    The divergence: the read plan emits the alias column after the function column, the outer planner emits it before. Position was chosen here (the PR #94644 region) because constant columns like now()/randConstant() get value-dependent action-node names that differ between the two planner runs, so a pure Name match would break on those.

    Two fix layers I see:

    1. At the rename site: match real and alias columns by their stable storage identifier (the getColumnIdentifierOrNull map already used a few lines above), falling back to position only for the value-dependent constant columns, instead of pure Position.
    2. Upstream: make the local/ReadFromRemote plan emit columns in the order the outer planner expects when an alias column coexists with a function column.

    I lean toward (1) as the more contained change, but it is in a core analyzer + distributed path you own, so I would rather confirm the direction first. Which layer do you prefer? I have the reproducer ready and will add a tests/queries/0_stateless regression covering now()/rand()/generateUUIDv4 over a 2-shard remote with an alias column. This is distinct from #94644 (alias-vs-alias order) and #88276.

  13. groeneai commented on Jun 15, 2026

    @groeneai
    Collaborator

    @vdimir #106404 does not fix #97899. Re-tested on master including #106404 (eb2c089): the three queries still fail identically.

    SELECT now(), x, xx           FROM remote('127.0.0.{1,2}', db, t) ORDER BY x;  -- CANNOT_PARSE_DATETIME
    SELECT generateUUIDv4(), x, xx FROM remote('127.0.0.{1,2}', db, t) ORDER BY x;  -- CANNOT_PARSE_UUID
    SELECT rand(), x, xx          FROM remote('127.0.0.{1,2}', db, t) ORDER BY x;  -- CANNOT_PARSE_TEXT

    On the same binary, #106404's own scenarios now pass (SELECT a1, toString(x) and SELECT a1, a1 over the distributed table), so it is a distinct bug: #106404 fixes the shard-side output-name dedup in buildQueryTreeDistributed, while #97899 is the initiator-side header rename.

    EXPLAIN header=1 is unchanged. ReadFromRemote emits [__table1.x, _CAST(now())::DateTime, concat(x,'aaa')::String], but the "Change remote column names to local column names" step (PlannerJoinTree.cpp:1583, MatchColumnsMode::Position) expects [__table1.x, __table1.xx::String, _CAST(now())::DateTime]. Position pairs the shard's concat(x,'aaa') ("xaaa") with the now() slot.

    Which fix layer do you prefer:

    1. At the rename site, match real/alias columns by their stable storage identifier (the getColumnIdentifierOrNull map used just above), falling back to Position only for the value-dependent constant columns.
    2. Upstream, make the local/ReadFromRemote plan emit columns in the outer planner's expected order when an alias column coexists with a function column.

    Reproducer and a now()/rand()/generateUUIDv4 regression test are ready.

  14. 1 remaining item

  15. groeneai commented on Jun 15, 2026

    @groeneai
    Collaborator

    Opened #107514 implementing option (2): the local/ReadFromRemote plan columns are reordered to the outer planner's expected order before the MatchColumnsMode::Position convert in PlannerJoinTree.cpp, so the shard's alias value is no longer paired with the function column slot.

    Verified both directions on Build-ID-checked debug builds (2-shard remote('127.0.0.{1,2}', ...)): the repro fails before the change and passes after, with correct values. The new test 04339_distributed_alias_column_with_function covers now()/rand()/generateUUIDv4(), the silent wrong-result UInt32 case, and several alias/function interleavings.

    One separate, pre-existing issue surfaced while testing: SELECT a1, a1 FROM remote(...) ORDER BY x (duplicate alias, no plain column in the projection) throws NUMBER_OF_COLUMNS_DOESNT_MATCH on both patched and unpatched master, so it is a distinct size-mismatch bug outside this position-reorder fix. Left for a follow-up unless you'd prefer it folded in here.

    @vdimir review please.

  16. added 4 commits that reference this issue on Jun 15, 2026
    9443b85
    f7e74f4
    54fc2da
    2cbe1f8
  17. vdimir commented on Jun 17, 2026

    @vdimir
    Member

    @groeneai check that this issue is fixed by #107675

  18. groeneai commented on Jun 17, 2026

    @groeneai
    Collaborator

    Confirmed, #107675 fixes #97899.

    Verified its ActionsChain.cpp change in isolation on clean master (eb2c089, debug), 2-shard remote('127.0.0.{1,2}', ...) with enable_analyzer=1 + ORDER BY:

    • base, no fix (build 4cef252d): now() -> CANNOT_PARSE_DATETIME, generateUUIDv4() -> CANNOT_PARSE_UUID, rand() -> CANNOT_PARSE_TEXT
    • with the fix (build ff8a73b7): all three pass with correct values

    Also correct on the harder shapes: the silent wrong-result UInt32 case, two-alias-two-function, the toDateTime(...) AS f / 7 AS f constants, and ax+1000 AS f, a, ay -> 1107, 7, 207. That last one (a function referencing an alias) is the case my #107514 only handled via the positional fallback, so #107675 is the more general fix. Closing #107514 in favor of it.

  19. added
    comp-query-analyzer
    on Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

analyzerbugcomp-query-analyzer

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions