Skip to content

Join by dynamic shards of PK. - #74733

Merged
KochetovNicolai merged 46 commits into
masterfrom
join-by-layers
Mar 21, 2025
Merged

KochetovNicolai merged 46 commits into
masterfrom
join-by-layers

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented Jan 17, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Use dynamic sharding for JOIN if the JOIN key is a prefix of PK for both parts. This optimization is enabled with query_plan_join_shard_by_pk_ranges setting (disabled by default).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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)

All builds in Builds_1 and Builds_2 stages are always mandatory and will run independently of the checks below:

  • Only: Stateless tests
  • Only: Stateful tests
  • Only: Integration tests
  • Only: Performance tests

  • Skip: Style check
  • Skip: Fast test

  • Non-blocking CI mode (Resource-intensive. All test jobs execute in parallel).
  • Disable CI cache

@robot-clickhouse robot-clickhouse added the pr-feature Pull request with new product feature label Jan 17, 2025
@robot-ch-test-poll4

robot-ch-test-poll4 commented Jan 22, 2025 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 107aabb 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
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❌ failure
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report❌ failure
Successful checks
Check nameDescriptionStatus
Docs checkBuilds and tests the documentation✅ success

@clickhouse-gh

clickhouse-gh Bot commented Feb 11, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [55fe191]

return std::make_shared<ConcurrentHashJoin>(context, table_join_, slots, right_sample_block_, stats_collecting_params);
}

std::shared_ptr<IJoin> cloneNoParallel(const std::shared_ptr<TableJoin> & table_join_, const Block &, const Block & right_sample_block_) const override

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.

This is needed to automatically replace parallel_hash to hash with sharding, cause hash is more performant in this case.

Probably, the sharding optimization can be done on top of the JoinLogical step to choose the algorithm after the sharding. However, currently it works fine with the physical join step.

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 downside that the change is not visible in EXPLAIN

@vdimir vdimir self-assigned this Feb 19, 2025

@vdimir vdimir left a comment

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 optimizeJoinByLayers.cpp was quite hard for me to follow, otherwise look pretty good

select explain e from (explain actions = 1 select * from tab_l l inner join tab_m m on l.a * 2 = m.c + m.d and l.d = m.a and l.b + l.c = m.b * 2 inner join tab_r r on l.a * 2 = r.a * 2 and l.d = r.d)
where e like '%ReadFromMergeTree%' or e like '%Expression%' or e like '%Join%' or e like '%Clauses%' or e like '%Sharding%';

--- three tables, med table matches noting, so right table can match both keys

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.

Middle table mathces nothing?

Suggested change
--- three tables, med table matches noting, so right table can match both keys
--- three tables, table `tab_m` matches noting, so right table can match both keys

-- { echo On }

-- two tables
select * from tab_l l inner join tab_m m on l.a * 2 = m.c + m.d and l.d = m.a and l.b + l.c = m.b * 2;

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.

Is ORDER BY omitted intentionally? Or we can get shards in different order in the result?

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.

No, it was not intentional.
Actually, I guess here we have only one shard, because the amount of data is small.

Probably I need a better test.

Comment on lines +5 to +11
create table tab_l (a UInt32, b UInt32, c UInt32, d UInt32) engine = MergeTree order by (a * 2, b + c);
create table tab_m (a UInt32, b UInt32, c UInt32, d UInt32) engine = MergeTree order by (c + d, b * 2);
create table tab_r (a UInt32, b UInt32, c UInt32, d UInt32) engine = MergeTree order by (a * 2, c * 2);

insert into tab_l select number, number, number, number from numbers(10);
insert into tab_m select number, number, number, number from numbers(10);
insert into tab_r select number, number, number, number from numbers(10);

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.

Do we have several shards for such data? Shouln't we change granularity or make tables larger or similar?

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.

Yes, it makes sense.

return this->read(
std::move(parts),
column_names,
ReadType::Default,

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.

Shouldn't this be in order as well?

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.

No, it should not, because by default sharding does not assume sorting

Comment thread src/Processors/QueryPlan/SortingStep.h Resolved

for (size_t i = 0; i < num_streams; ++i)
{
auto squashing = std::make_shared<SimpleSquashingChunksTransform>(left->getHeader(), 0, min_block_size_bytes);

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.

I remeber that we inserted squashing because of parallel_hash, since it not used probably we can omit it. On the other hand partitioning may result the same small blocks as parallel_hash.

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.

Thank you!
Actually, I think partitions should not affect block size.
I think it's better to remove it.

// std::cerr << frame.results.front()->dag.dumpDAG() << std::endl;
// std::cerr << frame.results.back()->dag.dumpDAG() << std::endl;

sharding = findCommonPromaryKeyPrefixByJoinKey(

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.

Suggested change
sharding = findCommonPromaryKeyPrefixByJoinKey(
sharding = findCommonPrimaryKeyPrefixByJoinKey(

Comment on lines +187 to +191
std::list<JoinAndSharding> joins;
std::list<JoinStep *> joins_to_keep_in_order;
std::list<ReadFromMergeTree *> sources;
std::list<SortingStep *> sorting_steps;
size_t common_prefix = std::numeric_limits<size_t>::max();

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.

It's not really easy to understand in which order do we expect have steps in these lists and how they correspond to each other, so comment may be helpful.

// std::cerr << frame.results.back()->dag.dumpDAG() << std::endl;

sharding = findCommonPromaryKeyPrefixByJoinKey(
frame.results.front()->joins.sources.front(), frame.results.front()->dag,

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 do we take particulary front source? Is it left-most source?

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.

yes, apparently, it's the left-most. It's because that the right one we fill in the memory and can't enable partitioning (at least for now).
Also, technically, we can allow reordering in the future somehow based on sharding. But now it seems to be too complicated.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-feature Pull request with new product feature labels Mar 19, 2025
@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Mar 21, 2025
Merged via the queue into master with commit d67983c Mar 21, 2025
@KochetovNicolai
KochetovNicolai deleted the join-by-layers branch March 21, 2025 12:29
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

5 participants