Repository navigation
SortingStep: deduce way to sort based on input stream sort description - #38719
Conversation
dbbc28f to
cd1c677
Compare
|
@Mergifyio update |
✅ Branch has been successfully updated |
… description in during transformation + perf test
+ code polishing
791b083 to
e0776b1
Compare
basically I returned code. Both plans with Finish sorting, need to check sorting prefix
|
@KochetovNicolai Please check the fix. It addresses the case you provided |
|
@KochetovNicolai @nickitat Please could you review it once more? It should be ready. I've added comments regarding possible follow-ups |
| $CLICKHOUSE_CLIENT -nq "$MAKE_OUTPUT_STABLE;EXPLAIN PLAN sortmode=1 SELECT a FROM (SELECT sipHash64(a) AS a FROM (SELECT a FROM optimize_sorting ORDER BY a)) ORDER BY a" | eval $FIND_SORTMODE | ||
|
|
||
| # FIXME: we still do full sort here, - it's because, for most inner subqueury, sorting description contains original column names but header contains only aliases on those columns: | ||
| #| Header: x Int32 │ |
There was a problem hiding this comment.
Follow up: update output stream sort description to cover this case
| Sort Mode: Chunk: a ASC, b ASC | ||
| -- PLAN: actions chain breaks sorting order: input(column a)->sipHash64(column a)->alias(sipHash64(column a), a)->plus(alias a, 1) | ||
| Sort Mode: Stream: plus(a, 1) ASC | ||
| Sort Mode: None |
There was a problem hiding this comment.
To discuss:
(1) in this case, sorting actually is preserved, - plus(a,1)(a is alias to sipHash64(column a)) is presented in the header as plus(sipHash64(column a), 1) but information about the meaning is not propagated, so we can't detect it just based on names
EXPLAIN actions = 1, header = 1, sortmode = 1
SELECT
a,
z
FROM
(
SELECT
sipHash64(a) AS a,
a + 1 AS z
FROM
(
SELECT a
FROM optimize_sorting
ORDER BY a + 1 ASC
)
)
ORDER BY a + 1 ASC
Query id: f18a6c58-2a3a-47a6-8eda-3965869071b1
┌─explain────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Expression ((Projection + (Before ORDER BY + (Projection + (Before ORDER BY + Projection))) [lifted up part])) │
│ Header: a UInt64 │
│ z UInt64 │
│ Sort Mode: None │
│ Actions: COLUMN Const(UInt8) -> 1 UInt8 : 0 │
│ INPUT : 0 -> sipHash64(a) UInt64 : 1 │
│ INPUT :: 1 -> a UInt64 : 2 │
│ FUNCTION plus(sipHash64(a) :: 1, 1 :: 0) -> plus(sipHash64(a), 1) UInt64 : 3 │
│ ALIAS plus(sipHash64(a), 1) :: 3 -> z UInt64 : 0 │
│ Positions: 2 0 │
│ Sorting (Sorting for ORDER BY) │
│ Header: sipHash64(a) UInt64 │
│ a UInt64 │
│ plus(a, 1) UInt64 │
│ plus(a, 1) UInt64 │
│ Sort Mode: Stream: plus(a, 1) ASC │
│ Sort description: plus(a, 1) ASC │
│ Expression ((Before ORDER BY + (Projection + (Before ORDER BY + Projection)))) │
│ Header: sipHash64(a) UInt64 │
│ a UInt64 │
│ plus(a, 1) UInt64 -- = plus(sipHash64(column a)), 1)
│ plus(a, 1) UInt64 │
│ Sort Mode: None │
│ Actions: INPUT : 0 -> a UInt64 : 0 │
│ COLUMN Const(UInt8) -> 1 UInt8 : 1 │
│ FUNCTION sipHash64(a :: 0) -> sipHash64(a) UInt64 : 2 │
│ ALIAS sipHash64(a) : 2 -> a UInt64 : 0 │
│ FUNCTION plus(a : 0, 1 :: 1) -> plus(a, 1) UInt64 : 3 │
│ Positions: 2 0 3 │
│ Sorting (Sorting for ORDER BY) │
│ Header: a UInt64 │
│ plus(a, 1) UInt64 │
│ Sort Mode: Stream: plus(a, 1) ASC │
│ Sort description: plus(a, 1) ASC │
│ Expression (Before ORDER BY) │
│ Header: a UInt64 │
│ plus(a, 1) UInt64 │
│ Sort Mode: Chunk: a ASC │
│ Actions: INPUT : 0 -> a UInt64 : 0 │
│ COLUMN Const(UInt8) -> 1 UInt8 : 1 │
│ FUNCTION plus(a : 0, 1 :: 1) -> plus(a, 1) UInt64 : 2 │
│ Positions: 0 2 │
│ ReadFromMergeTree (default.optimize_sorting) │
│ Header: a UInt64 │
│ Sort Mode: Chunk: a ASC │
│ ReadType: Default │
│ Parts: 1 │
│ Granules: 1 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
(2) the same plan as above. It contains ExpressionStep where ActionsDAG has 3 outputs but header 4 columns (latest 2 looks like duplicates)
│ Expression ((Before ORDER BY + (Projection + (Before ORDER BY + Projection)))) │ │ Header: sipHash64(a) UInt64 │ │ a UInt64 │ │ plus(a, 1) UInt64 │ │ plus(a, 1) UInt64 -- why is it here? │ │ Sort Mode: None │ │ Actions: INPUT : 0 -> a UInt64 : 0 │ │ COLUMN Const(UInt8) -> 1 UInt8 : 1 │ │ FUNCTION sipHash64(a :: 0) -> sipHash64(a) UInt64 : 2 │ │ ALIAS sipHash64(a) : 2 -> a UInt64 : 0 │ │ FUNCTION plus(a : 0, 1 :: 1) -> plus(a, 1) UInt64 : 3 │ │ Positions: 2 0 3 │
There was a problem hiding this comment.
(1) I think it's ok. Would be perfect to propagate this information, but it's not a error if we can't.
(2) Hard to say why it is happening. Probably some logic which removes columns uses only names. For a query plan (and ActionsDAG) it is allowed to have duplicate columns (duplicates are resolved in-order).
There was a problem hiding this comment.
I'll try to address it later as follow-up tasks
There was a problem hiding this comment.
btw, we should be careful with functions like plus: 18446744073709551614 < 18446744073709551615, but if we apply plus(*, 1) it became false.
~ change formatting for sorting ~ rename sortmode option -> sorting
| { | ||
| if (step.hasOutputStream()) | ||
| { | ||
| settings.out << prefix << "Sort Mode: " << step.getOutputStream().sort_mode; |
There was a problem hiding this comment.
"global" is a good name. "stream", I am afraid, will lead to confusion between DataStream and streams on pipeline level. imo "port" is ok. and "stream" also ok for me.
It looks to me as opposite. Regarding parallel processing, we're saying that we have several data streams processed in parallel - if each stream is sorted, then sorting scope (we call it sort mode) is stream. Port is pipeline level implementation detail. |
See proposal for renaming #40305 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Deduce way to sort based on input stream sort description. Skip sorting if input stream is already sorted