Skip to content

Window function MAX/MIN wrong results when calculated column is part of grouping #57321

Closed

Description

@pavel-naumenko

Version

23.10.5.20

Sample

    suppose 2 rows with integer column ver
        #1: ver=1
        #2: ver=2

    group by ver + get ver_max as overall max for all records

    expectation:
        ver | ver_max
        1, 2
        2, 2

Tests

NOT OK sample
    SELECT
        ver,
        max(ver) OVER() as ver_max
    FROM (
        SELECT 1 ver
        UNION ALL
        SELECT 2 ver
    )
    GROUP BY ver

    result =>
        ver | ver_max
        1, 1
        2, 2
OK sample if removing external group
    SELECT
        ver,
        max(ver) OVER() as ver_max
    FROM (
        SELECT 1 ver
        UNION ALL
        SELECT 2 ver
    )

    result =>
        ver | ver_max
        1, 2
        2, 2

Workaround to get result while keeping group

in real query external grouping is required
(idea is get all columns + aggregated results from last load only where ver >= ver_max)

currently can keep external grouping and normal results if adding some dummy calculation e.g. multiple on 1

    SELECT
        ver,
        max(ver * 1) OVER() ver_max
    FROM (
        SELECT 1 ver
        UNION ALL
        SELECT 2 ver
    )
    GROUP BY ver;

    result =>
        ver | ver_max
        1, 2
        2, 2

Activity

  1. added
    unexpected behaviour
    on Nov 28, 2023
  2. den-crane commented on Nov 28, 2023

    @den-crane
    Contributor

    works with analyzer

    set allow_experimental_analyzer=1;
    
    SELECT
        ver,
        max(ver) OVER () AS ver_max
    FROM
    (
        SELECT 1 AS ver
        UNION ALL
        SELECT 2 AS ver
    )
    GROUP BY ver
    
    ┌─ver─┬─ver_max─┐
    │   1 │       2 │
    │   2 │       2 │
    └─────┴─────────┘

    https://fiddle.clickhouse.com/1a36f47c-7e77-48ac-806d-0f1ebdac0635

  3. added
    comp-window-functions
    on Nov 28, 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

    comp-window-functionsunexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions