Skip to content

Case: column name is equal to function call on existing column #39855

Closed

Description

@devcrafter

Describe the unexpected behavior
Function in Projection is treated differently, - depending on ORDER BY column

How to reproduce

:) create table x(a UInt64, `sipHash64(a)` UInt64) engine = MergeTree order by a -- `sipHash64(a)` is column name
:) insert into x select number, number from numbers(10)
:) select sipHash64(a) from x order by sipHash64(a)       -- apply sipHash64() to column a    

┌─────────sipHash64(a)─┐
│  9140302661501632497 │
│  9199082625845137542 │
│  9224715256000962398 │
│ 13180436571133193200 │
│ 13251804803629934096 │
│ 13686418376000424449 │
│ 14247234200463187066 │
│ 14454559127837024772 │
│ 14848019510572519333 │
│ 16738165381834614119 │
└──────────────────────┘

10 rows in set. Elapsed: 0.015 sec.

:) select sipHash64(a) from x order by `sipHash64(a)`   -- just returns values of column `sipHash64(a)`

┌─sipHash64(a)─┐
│            0 │
│            1 │
│            2 │
│            3 │
│            4 │
│            5 │
│            6 │
│            7 │
│            8 │
│            9 │
└──────────────┘

10 rows in set. Elapsed: 0.014 sec.

Expected behavior
I'd expect some consistent behavior or error about ambiguity (don't know how should such case be treated according to SQL standard)

Note: initial example from #38719 discussion, see

Activity

  1. added
    unexpected behaviour
    on Aug 3, 2022
  2. devcrafter commented on Aug 3, 2022

    @devcrafter
    MemberAuthor

    EXPLAIN plan

    :) explain plan actions=1, header=1 select sipHash64(a) from x order by sipHash64(a)
    
    EXPLAIN actions = 1, header = 1
    SELECT sipHash64(a)
    FROM x
    ORDER BY sipHash64(a) ASC
    
    Query id: 076231c1-448c-44a8-ad78-d4e711aa7410
    
    ┌─explain────────────────────────────────────────────────────────────┐
    │ Expression (Projection)                                            │
    │ Header: sipHash64(a) UInt64                                        │
    │ Actions: INPUT :: 0 -> sipHash64(a) UInt64 : 0                     │
    │ Positions: 0                                                       │
    │   Sorting (Sorting for ORDER BY)                                   │
    │   Header: sipHash64(a) UInt64                                      │
    │   Sort description: sipHash64(a) ASC                               │
    │     Expression (Before ORDER BY)                                   │
    │     Header: sipHash64(a) UInt64                                    │
    │     Actions: INPUT : 0 -> a UInt64 : 0                             │
    │              FUNCTION sipHash64(a :: 0) -> sipHash64(a) UInt64 : 1 │
    │     Positions: 1                                                   │
    │       ReadFromMergeTree (default.x)                                │
    │       Header: a UInt64                                             │
    │       ReadType: Default                                            │
    │       Parts: 1                                                     │
    │       Granules: 1                                                  │
    └────────────────────────────────────────────────────────────────────┘
    
    :) explain plan actions=1, header=1 select sipHash64(a) from x order by `sipHash64(a)`
    
    EXPLAIN actions = 1, header = 1
    SELECT sipHash64(a)
    FROM x
    ORDER BY `sipHash64(a)` ASC
    
    Query id: 3d694679-cfbd-46fb-9935-d7a1c0e4182c
    
    ┌─explain────────────────────────────────────────────┐
    │ Expression (Projection)                            │
    │ Header: sipHash64(a) UInt64                        │
    │ Actions: INPUT :: 0 -> sipHash64(a) UInt64 : 0     │
    │ Positions: 0                                       │
    │   Sorting (Sorting for ORDER BY)                   │
    │   Header: sipHash64(a) UInt64                      │
    │           a UInt64                                 │
    │   Sort description: sipHash64(a) ASC               │
    │     Expression (Before ORDER BY)                   │
    │     Header: sipHash64(a) UInt64                    │
    │             a UInt64                               │
    │     Actions: INPUT :: 0 -> sipHash64(a) UInt64 : 0 │
    │     Positions: 0                                   │
    │       ReadFromMergeTree (default.x)                │
    │       Header: sipHash64(a) UInt64                  │
    │               a UInt64                             │
    │       ReadType: Default                            │
    │       Parts: 1                                     │
    │       Granules: 1                                  │
    └────────────────────────────────────────────────────┘
    
  3. self-assigned this
    on Aug 4, 2022
  4. kitaisreal commented on Aug 4, 2022

    @kitaisreal
    Contributor

    Related to #39855.

  5. devcrafter commented on Aug 5, 2022

    @devcrafter
    MemberAuthor

    Related to #39855.

    The link is on this issue itself

  6. kitaisreal commented on Aug 5, 2022

    @kitaisreal
    Contributor

    Related to #23194.

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

    unexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions