Repository navigation
Join by dynamic shards of PK. - #74733
Conversation
|
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
Successful checks
|
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The downside that the change is not visible in EXPLAIN
vdimir
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Middle table mathces nothing?
| --- 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; |
There was a problem hiding this comment.
Is ORDER BY omitted intentionally? Or we can get shards in different order in the result?
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
Do we have several shards for such data? Shouln't we change granularity or make tables larger or similar?
There was a problem hiding this comment.
Yes, it makes sense.
| return this->read( | ||
| std::move(parts), | ||
| column_names, | ||
| ReadType::Default, |
There was a problem hiding this comment.
Shouldn't this be in order as well?
There was a problem hiding this comment.
No, it should not, because by default sharding does not assume sorting
|
|
||
| for (size_t i = 0; i < num_streams; ++i) | ||
| { | ||
| auto squashing = std::make_shared<SimpleSquashingChunksTransform>(left->getHeader(), 0, min_block_size_bytes); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
| sharding = findCommonPromaryKeyPrefixByJoinKey( | |
| sharding = findCommonPrimaryKeyPrefixByJoinKey( |
| 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(); |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Why do we take particulary front source? Is it left-most source?
There was a problem hiding this comment.
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.
Co-authored-by: Vladimir Cherkasov <[email protected]>
Changelog category (leave one):
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_rangessetting (disabled by default).Documentation entry for user-facing changes
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: