Skip to content

Alias hides right table column in join #13210

Closed

Description

@euyuil

Describe the bug

Failure happens when selecting a constant renaming to the same name with an existing column of a joined table which is also in where clause.

How to reproduce

  • Which ClickHouse server version to use: version 20.3.12.112 (official build)

  • Which interface to use, if matters: No matter.

  • Non-default settings, if any: Not any.

  • CREATE TABLE statements for all tables involved

CREATE TABLE test_a_table (
    name String,
    a_col String
)
Engine = MergeTree()
ORDER BY name;

CREATE TABLE test_b_table (
    name String,
    b_col String,
    some_val String
)
Engine = MergeTree()
ORDER BY name;

No data is needed to reproduce the error.

  • Queries to run that lead to unexpected result
SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    'N' some_val
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

Expected behavior
A clear and concise description of what you expected to happen.

The query is expected to be successful.

Error message and/or stacktrace
If applicable, add screenshots to help explain your problem.

Code: 47, e.displayText() = DB::Exception: Unknown identifier: some_val there are columns: name, a_col, b.name, b_col (version 20.3.12.112 (official build))

Additional context
Add any other context about the problem here.

These queries are successful:

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col   -- some_val not selected
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    b.some_val some_val   -- use b.some_val instead of a constant
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    'N' some_val2   -- do not rename to some_val
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    'N' some_val
from test_a_table a
join test_b_table b on a.name = b.name;   -- no where clause

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    if(b.some_val='','N','N') some_val   -- an interesting workaround...
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

However, this won't work either:

SELECT
    b.name name,
    a.a_col a_col,
    b.b_col b_col,
    if(1,'N',b.some_val) some_val   -- b.some_val is not actually evaluated
from test_a_table a
join test_b_table b on a.name = b.name
where b.some_val = 'Y';

Activity

  1. added
    bug
    on Aug 1, 2020
  2. added
    comp-joins
    on Aug 3, 2020
  3. changed the title [-]Code: 47, e.displayText() = DB::Exception: Unknown identifier: XXX there are columns: A, B, ... (version 20.3.12.112 (official build))[/-] [+]Alias hides right table column in join[/+] on Aug 11, 2020
  4. self-assigned this
    on Aug 11, 2020
  5. DouMiaoO-Oo commented on Aug 25, 2020

    @DouMiaoO-Oo

    I have a similar error in ver 20.6.4.44 without join.

    This is my script

    select
    	'2020-07-15' as dt
    from
    	recom_stats_dws.load_clk_log_day as tbl
    where
    	tbl.dt='20200715';

    I got error message:

    DB::Exception: Unknown identifier: dt there are columns: label (version 20.6.4.44 (official build))

    When I rename dt, the script below will run the right way as I expected without error:

    select
    	'2020-07-15' as rename_dt
    from
    	recom_stats_dws.load_clk_log_day as tbl
    where
    	tbl.dt='20200715';
  6. removed their assignment
    on Oct 16, 2020
  7. added
    comp-sql-syntax
    and removed on Aug 15, 2021
  8. added a commit that references this issue on Apr 2, 2024
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-joinscomp-sql-syntax

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions