Skip to content

The group by field is not correctly identified during aggregation query #32639

Closed

Description

@h1k

Describe what's wrong
The group by field is not correctly identified during aggregation query

Does it reproduce on recent release?
21.8.12.29-lts
How to reproduce

  • Which ClickHouse server version to use
    21.8.10.19-lts , 21.8.12.29-lts
create table groupby_test(id Int8)
engine=MergeTree
Order by tuple();
insert into groupby_test values (1),(2);
select multiIf(1=2 and id>0,1,2) as a ,sum(id) from  groupby_test GROUP BY a;

Expected behavior
| 2 | 3 |
Error message and/or stacktrace
Column id is not under aggregate function and not in GROUP BY: While processing mulitIf((1=2) AND id>0,1,2) as a,sum(id).

Activity

  1. added
    potential bug
    on Dec 13, 2021
  2. added
    bug
    and removed
    potential bug
    on Dec 13, 2021
  3. alexey-milovidov commented on Dec 13, 2021

    @alexey-milovidov
    Member

    Minimal reproducing example:

    SELECT 0 AND id ? 1 : 2 AS a, sum(id) FROM (SELECT 1 AS id) GROUP BY a;
    
  4. KochetovNicolai commented on Dec 15, 2021

    @KochetovNicolai
    Member

    My understanding what is going on:

    • expression 0 AND id ? 1 : 2 AS a is detected as constant
    • GROUP BY is removed because a is constant and there is sum function
    • query now looks like SELECT 0 AND id ? 1 : 2 AS a, sum(id) FROM (SELECT 1 AS id)
    • now, 0 AND id ? 1 : 2 AS a is not a key and depend on id which is not under aggregate function

    It probably works for other cases if AST optimization can detect that expression (in case of 0 AND id ? 1 : 2 AS a it can't).

    I have no idea how to fix it.
    The possible way - remove group-by-constant-key optimisation completely (or make it somewhere else)

  5. 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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions