Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
devcrafter opened this issue Aug 3, 2022 · 4 comments · Fixed by #62457
Closed

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

devcrafter opened this issue Aug 3, 2022 · 4 comments · Fixed by #62457

Comments

@devcrafter
Copy link
Member

devcrafter commented Aug 3, 2022

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

@devcrafter
Copy link
Member Author

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                                  │
└────────────────────────────────────────────────────┘

@kitaisreal kitaisreal self-assigned this Aug 4, 2022
@kitaisreal
Copy link
Contributor

Related to #39855.

@devcrafter
Copy link
Member Author

Related to #39855.

The link is on this issue itself

@kitaisreal
Copy link
Contributor

Related to #23194.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants