Skip to content

Remove sorting from query plan stream properties#68410

Merged
KochetovNicolai merged 33 commits intomasterfrom
remove-sorting-from-stream-properties
Sep 27, 2024
Merged

Remove sorting from query plan stream properties#68410
KochetovNicolai merged 33 commits intomasterfrom
remove-sorting-from-stream-properties

Conversation

@KochetovNicolai
Copy link
Copy Markdown
Member

@KochetovNicolai KochetovNicolai commented Aug 15, 2024

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Because it is too complicated to support.

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

CI Settings (Only check the boxes if you know what you are doing):

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64

  • Run only fuzzers related jobs (libFuzzer fuzzers, AST fuzzers, etc.)
  • Exclude: AST fuzzers

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

@robot-ch-test-poll robot-ch-test-poll added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 15, 2024
@robot-clickhouse
Copy link
Copy Markdown
Member

robot-clickhouse commented Aug 15, 2024

This is an automated comment for commit dfe8eb0 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Successful checks
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help✅ success
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

Sorting (Stream): __table1.a ASC, __table1.b ASC
Sorting (Stream): __table1.a ASC, __table1.b ASC
Sorting (Stream): a ASC, b ASC
Sorting: __table1.a ASC
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 think this is correct because for a query select distinct b, a from distinct_in_order_explain order by a you don't get (a, b) order after sorting, even if read-in-order with (a, b).

This is because the sorting step does not know anything about b and will use a stable sort (e.g. will prefer the left most input vs input with smallest b) which will break order.

Sorting (None)
Sorting (Sorting for ORDER BY)
Sorting (Global): plus(a, 1) ASC
Sorting (Chunk): a ASC
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.

Imho, Chunk sorting does not give any helpful information, so I did remove it:

  • First of all, I can't guarantee that reading from MergeTree always keeps chunks sorted by PK. Even if so, it is very fragile, e.g. somebody may want to read from different parts in one read task (afiak this is already happening)
  • So far, Chunk may be used for Sorting (or, maybe, Distinct optimization, but I think that's wrong), but in both cases we do in-order optimization and request Stream sorting.

Expression (Projection)
Distinct
Sorting (Sorting for ORDER BY)
Distinct (Preliminary DISTINCT)
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.

The reason of the change is that Preliminary DISTINCT was pushed down over UNION.
I am not sure whether we should remove it in this case, but this was done by remove_redundant_distinct optimization. (why?)

@maxknv maxknv force-pushed the remove-sorting-from-stream-properties branch from 0f50d2a to a6d53c2 Compare August 16, 2024 11:21
@devcrafter devcrafter self-assigned this Aug 16, 2024
@KochetovNicolai KochetovNicolai force-pushed the remove-sorting-from-stream-properties branch from feb8219 to 1d3027b Compare August 16, 2024 12:06
@KochetovNicolai KochetovNicolai marked this pull request as ready for review August 19, 2024 08:28
Copy link
Copy Markdown
Member

@devcrafter devcrafter left a comment

Choose a reason for hiding this comment

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

Please see the comments. It's not everything, but can't anymore

}

/// merge sorted
if (input_sort_mode == DataStream::SortScope::Stream && input_sort_desc.hasPrefix(result_description))
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.

This case may not be covered with new implementation

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.

Fixed

settings.max_block_size,
settings.aggregation_in_order_max_block_bytes,
std::move(group_by_sort_description),
SortDescription{},
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.

We can delete this parameter from MergingAggregatedStep


DataStream ITransformingStep::createOutputStream(
const DataStream & input_stream,
[[maybe_unused]] const DataStream & input_stream,
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.

Should we delete these parameters at all?

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 would like to remove it later, in the next PR.
There are too many steps affected.

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.

Then probably it makes sense to add TODO comment

const AggregateDescriptions & aggregates,
bool should_produce_results_in_order_of_bucket_number,
SortDescription group_by_sort_description)
[[maybe_unused]] SortDescription group_by_sort_description)
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.

Remove?

@@ -199,8 +199,6 @@ void optimizeTreeSecondPass(const QueryPlanOptimizationSettings & optimization_s
}
}

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.

Why it was deleted?

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.

It was moved to applyOrder.cpp, the branch for MergingAggregatedStep


output_stream.sort_description = std::move(output_sort_description);
output_stream.sort_scope = DataStream::SortScope::Stream;
// output_stream.sort_description = std::move(output_sort_description);
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.

Remove please

}

void ReadFromRemote::enforceSorting(SortDescription output_sort_description)
void ReadFromRemote::enforceSorting([[maybe_unused]] SortDescription output_sort_description)
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.

Remove param, and rename probably to enableMemoryBoundMerging()

}

void ReadFromParallelRemoteReplicasStep::enforceSorting(SortDescription output_sort_description)
void ReadFromParallelRemoteReplicasStep::enforceSorting([[maybe_unused]] SortDescription output_sort_description)
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.

The same

bool use_buffering = order_info->limit == 0;
sorting->convertToFinishSorting(order_info->sort_description_for_merging, use_buffering);
updateStepsDataStreams(steps_to_update);
// updateStepsDataStreams(steps_to_update);
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.

Remove

AggregationInputOrder buildInputOrderInfo(AggregatingStep & aggregating, QueryPlan::Node & node)
{
QueryPlan::Node * reading_node = findReadingStep(node, backward_path);
QueryPlan::Node * reading_node = findReadingStep(node, false);
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.

comment on parameter

const Names & columns_,
bool pre_distinct_, /// If is enabled, execute distinct for separate streams. Otherwise, merge streams.
bool optimize_distinct_in_order_);
/// If is enabled, execute distinct for separate streams. Otherwise, merge streams.
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.

Probably

Suggested change
/// If is enabled, execute distinct for separate streams. Otherwise, merge streams.
/// If is enabled, execute distinct for separate streams, otherwise for merged streams.


DataStream ITransformingStep::createOutputStream(
const DataStream & input_stream,
[[maybe_unused]] const DataStream & input_stream,
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.

Then probably it makes sense to add TODO comment

global_ctx->deduplicate_by_columns,
false /*pre_distinct*/,
true /*optimize_distinct_in_order TODO: looks like it should be enabled*/);
false /*pre_distinct*/);
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.

In this case, we do not apply in order optimization anymore - which would lead to increased memory consumption during merges, at least

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.

Oh, that's a good catch!
Looks like we did not have a test for this :(

@KochetovNicolai KochetovNicolai added this pull request to the merge queue Sep 27, 2024
Merged via the queue into master with commit a8fe70f Sep 27, 2024
@KochetovNicolai KochetovNicolai deleted the remove-sorting-from-stream-properties branch September 27, 2024 10:39
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2024
baibaichen added a commit to Kyligence/gluten that referenced this pull request Sep 28, 2024
baibaichen added a commit to apache/gluten that referenced this pull request Sep 28, 2024
* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20240928)

* Fix build due to ClickHouse/ClickHouse#65625

* Fix build due to ClickHouse/ClickHouse#68410

---------

Co-authored-by: kyligence-git <gluten@kyligence.io>
Co-authored-by: Chang Chen <baibaichen@gmail.com>
@novikd
Copy link
Copy Markdown
Member

novikd commented Sep 30, 2024

This PR fixed push_down_limit performance test but broke optimize_sorting_for_input_stream.

@KochetovNicolai
Copy link
Copy Markdown
Member Author

KochetovNicolai commented Sep 30, 2024

Ok, it is only with optimize_read_in_order=0, which is 1 by default.
Will fix it later.

UPD
I have decided not to fix two failed tests.
They are based on the assumption that reading from MergeTree returns chunks ordered by sorting key

Sorting (Chunk): CounterID ASC, EventDate ASC

I believe this assumption is not valid (or, at least, very fragile). We can easily have a situation when the reading task reads from 2 parts and forms a single chunk from it.
We can enforce returning sorted chunks if needed. But why do we need it if we already have a read-in-order feature enabled by default?

@KochetovNicolai KochetovNicolai mentioned this pull request Oct 1, 2024
20 tasks
sharkdtu pushed a commit to sharkdtu/gluten that referenced this pull request Nov 11, 2024
)

* [GLUTEN-1632][CH]Daily Update Clickhouse Version (20240928)

* Fix build due to ClickHouse/ClickHouse#65625

* Fix build due to ClickHouse/ClickHouse#68410

---------

Co-authored-by: kyligence-git <gluten@kyligence.io>
Co-authored-by: Chang Chen <baibaichen@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants