Skip to content

SortingStep: deduce way to sort based on input stream sort description - #38719

Merged
devcrafter merged 68 commits into
masterfrom
skipping_sorting_step
Aug 17, 2022
Merged

devcrafter merged 68 commits into
masterfrom
skipping_sorting_step

Conversation

@devcrafter

@devcrafter devcrafter commented Jul 1, 2022 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-performance Pull request with some performance improvements label Jul 1, 2022
@devcrafter
devcrafter marked this pull request as draft July 1, 2022 19:48
@devcrafter
devcrafter force-pushed the skipping_sorting_step branch 2 times, most recently from dbbc28f to cd1c677 Compare July 5, 2022 22:40
@devcrafter devcrafter changed the title Skip sorting step if input stream is globally sorted Skip sorting step if input stream is sorted Jul 5, 2022
@devcrafter

Copy link
Copy Markdown
Member Author

@Mergifyio update

@mergify

mergify Bot commented Jul 8, 2022

Copy link
Copy Markdown
Contributor

update

✅ Branch has been successfully updated

@devcrafter
devcrafter force-pushed the skipping_sorting_step branch from 791b083 to e0776b1 Compare July 11, 2022 21:00
Comment thread src/Core/SortDescription.cpp Resolved
@devcrafter devcrafter changed the title Skip sorting step if input stream is sorted SortingStep: deduce way to sort based on input stream sort description Jul 12, 2022
@devcrafter devcrafter added do not test disable testing on pull request and removed do not test disable testing on pull request labels Aug 11, 2022
@devcrafter

Copy link
Copy Markdown
Member Author

@KochetovNicolai Please check the fix. It addresses the case you provided

@devcrafter

Copy link
Copy Markdown
Member Author

@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 │

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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                                                                                           │

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to address it later as follow-up tasks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, we should be careful with functions like plus: 18446744073709551614 < 18446744073709551615, but if we apply plus(*, 1) it became false.

Comment thread src/Interpreters/ActionsDAG.cpp Resolved
Comment thread src/Interpreters/ActionsDAG.cpp Resolved
Comment thread src/Core/Settings.h Resolved
Comment thread src/Processors/QueryPlan/QueryPlan.cpp Outdated
{
if (step.hasOutputStream())
{
settings.out << prefix << "Sort Mode: " << step.getOutputStream().sort_mode;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.

Comment thread src/Interpreters/ActionsDAG.cpp Resolved
@devcrafter

Copy link
Copy Markdown
Member Author

"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.

@devcrafter

Copy link
Copy Markdown
Member Author

@devcrafter
devcrafter merged commit 46ed4f6 into master Aug 17, 2022
@devcrafter
devcrafter deleted the skipping_sorting_step branch August 17, 2022 10:58
@devcrafter

Copy link
Copy Markdown
Member Author

"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

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

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants