Skip to content

where clause bug with setting prefer_column_name_to_alias = 1  #54954

Closed

Description

@HSEhomework

You have to provide the following information whenever possible.

Wrong behavior for the filter using prefer_column_name_to_alias = 1

CREATE TABLE loans (loan_number int, security_id text) ENGINE=Memory;

INSERT INTO loans VALUES (1, 'AAA');
INSERT INTO loans VALUES (1, 'AAA');
INSERT INTO loans VALUES (1, 'AAA');
INSERT INTO loans VALUES (1, 'AAA');
INSERT INTO loans VALUES (1, 'AAA');
INSERT INTO loans VALUES (1, 'BBB');
INSERT INTO loans VALUES (1, 'BBB');
INSERT INTO loans VALUES (1, 'BBB');
INSERT INTO loans VALUES (1, 'BBB');
INSERT INTO loans VALUES (1, 'BBB');
INSERT INTO loans VALUES (1, 'BBB');


with block_0 as (
  select * from loans
),
block_1 as (
  select sum(loan_number) as loan_number from block_0 group by security_id
)
select loan_number from block_1 where loan_number > 3 settings prefer_column_name_to_alias = 1 

In this query filter loan_number > 3 for some reason will not operate on the aggregation.
By the logic original column name here should not be available, as the context after aggregation should be different and alias should be used.

https://fiddle.clickhouse.com/aa990c59-25a1-4562-9bea-17f43bd9f78f

A link to reproducer in https://fiddle.clickhouse.com/.

Does it reproduce on recent release?

yes

Expected behavior

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

Expected output for the query above is
5
6

Please help with this bug.

Activity

  1. added
    potential bug
    on Sep 24, 2023
  2. added
    bug
    and removed
    potential bug
    on Sep 25, 2023
  3. added a commit that references this issue on Apr 10, 2024
  4. added
    comp-ddl
    on Mar 27, 2026
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-ddl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions