Skip to content

Wrong results while using an array and sum of the elements to produce new array in one select clause #15395

Closed

Description

@ibeleychev

Describe the bug
Following code snippet calculates percents wrong when all the statements are on one SELECT block and calculates percents wright when arrayMap function is on the other SELECT clause

How to reproduce

select t.1 as cnt,
       t.2 as name,
       t.3 as percent
from (
      select arrayJoin(result) as t
      from (
            select [
                       (79, 'name1'),
                       (62, 'name2'),
                       (44, 'name3')
                       ]                                      as data,
                   arraySum(arrayMap(t -> t.1, data)) as total,
                   arrayMap(t ->
                                tuple(t.1, t.2,
                                      multiIf(total = 0, 0, t.1 > 0 and t.1 < 10, -1.0,
                                              (toFloat32(t.1) / toFloat32(total)) * 100)
                                    ),
                            data
                       )                                      as result
               )
         );
  • Which ClickHouse server version to use
    20.9.2.20
  • Which interface to use, if matters
    Request from intellij idea via jdbc

Expected behavior
Expected result is:

cnt,name,percent
79,name1,42.7027027027027
62,name2,33.513513513513516
44,name3,23.783783783783786

Actual result

cnt,name,percent
79,name1,33.33333333333333
62,name2,33.33333333333333
44,name3,33.33333333333333

Additional context
I found out that this piece of code can work properly with following changes

select t.1 as cnt,
       t.2 as name,
       t.3 as percent
from (
      select arrayJoin(result) t
      from (
            select arrayMap(t ->
                                tuple(t.1, t.2,
                                      multiIf(total = 0, 0, t.1 > 0 and t.1 < 10, - 1.0,
                                              (toFloat32(t.1) / toFloat32(total)) * 100)
                                    ),
                            data
                       ) as result
            from (
                  select [
                             (79, 79),
                             (62, 24),
                             (44, 44)
                             ]                                       as data,
                         arraySum(arrayMap(t -> t.1, data)) as total
                     )
               )
         );

Activity

  1. added
    bug
    on Sep 28, 2020
  2. den-crane commented on Sep 28, 2020

    @den-crane
    Contributor

    it seems this bug existed for very long time.

    18.14.19
    ┌─cnt─┬─name──┬───────────percent─┐
    │  79 │ name1 │ 33.33333333333333 │
    │  62 │ name2 │ 33.33333333333333 │
    │  44 │ name3 │ 33.33333333333333 │
    └─────┴───────┴───────────────────┘
    

    WA: different lambda var. name. t -> replaced with x ->

    SELECT
        t.1 AS cnt,
        t.2 AS name,
        t.3 AS percent
    FROM
    (
        SELECT arrayJoin(result) AS t
        FROM
        (
            SELECT
                [(79, 'name1'), (62, 'name2'), (44, 'name3')] AS data,
                arraySum(arrayMap(t -> t.1, data)) AS total,
                arrayMap(x -> (x.1, x.2, multiIf(total = 0, 0, (x.1 > 0) AND (x.1 < 10), -1., (toFloat32(x.1) / toFloat32(total)) * 100)), data) AS result
        )
    )
    
    ┌─cnt─┬─name──┬────────────percent─┐
    │  79 │ name1 │   42.7027027027027 │
    │  62 │ name2 │ 33.513513513513516 │
    │  44 │ name3 │ 23.783783783783786 │
    └─────┴───────┴────────────────────┘
    
    
  3. added
    st-accepted
    on Sep 28, 2020
  4. alexey-milovidov commented on Jan 25, 2021

    @alexey-milovidov
    Member

    Minimal example:

    SELECT arrayMap(x -> arrayMap(x -> (x.1), [(1, 1), (2, 2), (3, 3)]), [(4, 4), (5, 5), (6, 6)])
    
    ┌─arrayMap(lambda(tuple(x), arrayMap(lambda(tuple(x), tupleElement(x, 1)), array(tuple(1, 1), tuple(2, 2), tuple(3, 3)))), array(tuple(4, 4), tuple(5, 5), tuple(6, 6)))─┐
    │ [[1,2,3],[1,2,3],[1,2,3]]                                                                                                                                              │
    └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    
    
    
    SELECT arrayMap(x -> (x.1, arrayMap(x -> (x.1), [(1, 1), (2, 2), (3, 3)])), [(4, 4), (5, 5), (6, 6)])
    
    ┌─arrayMap(lambda(tuple(x), tuple(tupleElement(x, 1), arrayMap(lambda(tuple(x), tupleElement(x, 1)), array(tuple(1, 1), tuple(2, 2), tuple(3, 3))))), array(tuple(4, 4), tuple(5, 5), tuple(6, 6)))─┐
    │ [(4,[4,4,4]),(5,[5,5,5]),(6,[6,6,6])]                                                                                                                                                             │
    └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    
    
  5. added
    comp-sql-syntax
    on Jan 25, 2021
  6. alexey-milovidov commented on Mar 20, 2023

    @alexey-milovidov
    Member

    It is fixed by enabling the analyzer.

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-sql-syntaxminorst-accepted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions