Skip to content

Alias columns incorrect behaviour in join queries #47432

Closed

Description

@phantom943

Alias columns seem not to work in joins

See an example:

create or replace table t1
engine = MergeTree()
order by tuple()
as
select 1 as user_id, 2 as level
;


create or replace table t2
engine = MergeTree()
order by tuple()
as
select 1 as user_id, 'website' as event_source, '2023-01-01 00:00:00'::DateTime as timestamp
;


alter table t2
add column date Date alias toDate(timestamp)
;

SELECT
  any(t2.date) as any_val
FROM t1 AS t1
LEFT JOIN t2 as t2
  ON (t1.user_id = t2.user_id)

Clickhouse 23.2.1.2537 throws the following exception:

SQL Error [47] [07000]: Code: 47. DB::Exception: Unknown column: date, there are only columns user_id. (UNKNOWN_IDENTIFIER) (version 23.2.1.2537 (official build))¶, server ClickHouseNode [uri=http://<url>:<port>/<db>, options={use_server_time_zone=false,profile=dbt_web,use_time_zone=false}]@-106769119

And the expected behaviour would be to return 1 row with 2023-01-01.
Replacing an alias column with a stored one works OK:

SELECT
  any(t2.event_source) as any_val
FROM t1 AS t1
LEFT JOIN t2 as t2
  ON (t1.user_id = t2.user_id)

returns website as expected.
Also, wrapping the table in a subquery (with an explicit setting for * including alias columns) fixes the issue:

SELECT
  any(t2.date) as any_val
FROM user_data_db.t1 AS t1
LEFT JOIN (select * from user_data_db.t2) as t2
  ON (t1.user_id = t2.user_id)
SETTINGS asterisk_include_alias_columns=1

Reproducer link
Reproduces on latest release, too.

Activity

  1. added
    potential bug
    on Mar 10, 2023
  2. kitaisreal commented on Mar 10, 2023

    @kitaisreal
    Contributor

    Related to #42648. Works in new analyzer.

  3. self-assigned this
    on Mar 10, 2023
  4. added a commit that references this issue on Apr 3, 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

    potential bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions