Skip to content

Missing columns: 'a' while processing query with more than 2 tables in cross join #22627

Closed

Description

@UnamedRus

How to reproduce
Clickhouse 21.5

WITH
    x AS
    (
        SELECT 1 AS a
    ),
    xx AS
    (
        SELECT *
        FROM x
        , x AS x1
    )
SELECT *
FROM xx
WHERE a = 1

Query id: f4fc159a-a136-4191-a0f0-c11c5ecdea74

┌─a─┬─x1.a─┐
│ 1 │    1 │
└───┴──────┘

WITH
    x AS
    (
        SELECT 1 AS a
    ),
    xx AS
    (
        SELECT *
        FROM x
        , x AS x1
        , x AS x2
    )
SELECT *
FROM xx
WHERE a = 1

Query id: bee7bab0-1f6a-4f29-bab5-f3b845e7095d


0 rows in set. Elapsed: 0.004 sec.

Received exception from server (version 21.5.1):
Code: 47. DB::Exception: Received from localhost:9000. DB::Exception: Missing columns: 'a' while processing query: 'WITH x AS (SELECT 1 AS a), xx AS (SELECT * FROM x , x AS x1 , x AS x2) SELECT `x.a`, `x1.a`, `x2.a` FROM xx WHERE a = 1', required columns: 'x.a' 'x1.a' 'x2.a' 'a' 'x.a' 'x1.a' 'x2.a' 'a'.

Activity

  1. added
    bug
    on Apr 4, 2021
  2. added
    comp-joins
    on Apr 5, 2021
  3. adrian17 commented on Apr 16, 2021

    @adrian17

    Also reproducible with other join types than CROSS.

    Example, reproducible on 21.3.4:

    with example as (select toDate('2021-01-01') as date, 1 as node, 1 as user)
    SELECT extra_data
    FROM (
        SELECT join1.*
        FROM example
        LEFT JOIN ( SELECT toDate('2021-01-01') as date, 1 as extra_data ) AS join1
        ON example.date = join1.date
        LEFT JOIN ( SELECT toDate('2021-01-01') as date ) AS join2
        ON (example.date = join2.date)
    )
    

    Produces Missing columns: 'extra_data'. Removing the second JOIN appears to fix the issue.

  4. alexey-milovidov commented on Mar 20, 2023

    @alexey-milovidov
    Member

    Also works perfectly with the analyzer! Will be easy to close after the analyzer is merged.

  5. added a commit that references this issue on Apr 3, 2024
  6. vdimir commented on Jul 4, 2024

    @vdimir
    Member

    Probably original query shouln't be valid, since we allow ambiguous identifier only for a single join and single_join_prefer_left_table=true

    :) SELECT a FROM (SELECT 1 AS a) AS x, (SELECT 2 AS a) AS x1, (SELECT 3 AS a) AS x2
    
    
    Received exception from server (version 24.7.1):
    Code: 207. DB::Exception: Received from localhost:9000. DB::Exception: JOIN  (SELECT 1 AS a FROM system.one) AS x, (SELECT 2 AS a FROM system.one) AS x1 ambiguous identifier 'a'. In scope SELECT a FROM (SELECT 1 AS a) AS x, (SELECT 2 AS a) AS x1, (SELECT 3 AS a) AS x2. (AMBIGUOUS_IDENTIFIER)
    

    @UnamedRus do you think we can keep this behavior and keep original query with multiple joins illegal (effectively rolling back support of the query) ?

  7. reopened this on Jul 4, 2024
  8. UnamedRus commented on Jul 4, 2024

    @UnamedRus
    ContributorAuthor

    It's not like big argument, but SQLite does support it in that form(MySQL and PostgreSQL doesn't)

    https://www.db-fiddle.com/f/qM4vp4fu9m5DJ9pbE4CDdu/1

    however, i find this "issue" because, it was expected behavior for someone, (at least, without that far simplication of query) May be, i can find source query.

    Ok, it was opened, at the same time as #22626, which was made for TPC-DS benchmark investigation. I assume this is also related to it

  9. alexey-milovidov commented on Feb 7, 2026

    @alexey-milovidov
    Member

    Closing: explicitly fixed by commit 809461ccdae merged into master. Test added via PR #62185.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcomp-joins

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions