-
Notifications
You must be signed in to change notification settings - Fork 47
Hybrid layout design for HashJoin/Sort #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ruochenj123
wants to merge
6
commits into
bytedance:main
Choose a base branch
from
ruochenj123:hybrid-design
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,203
β160
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
36a3177
WIP hybrid design
ruochenj123 fe56c10
implement hybrid for spill
ruochenj123 31e04b1
add fast path
ruochenj123 6513688
clang-format
ruochenj123 6e64cfb
fix lisence
ruochenj123 dfe7dce
fix empty container case
ruochenj123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,13 @@ HashBuild::HashBuild( | |
|
|
||
| // Identify the non-key build side columns and make a decoder for each. | ||
| const int32_t numDependents = inputType->size() - numKeys; | ||
| std::vector<std::string> dependentNames; | ||
| std::vector<TypePtr> dependentTypes; | ||
|
|
||
| hybridJoin_ = operatorCtx_->driverCtx()->queryConfig().hybridJoinEnabled() && | ||
| numDependents > 0 && !joinNode_->isLeftSemiFilterJoin() && | ||
| !joinNode_->isLeftSemiProjectJoin() && !joinNode_->isAntiJoin(); | ||
|
|
||
| if (!dropDuplicates_ && numDependents > 0) { | ||
| // Number of join keys (numKeys) may be less then number of input columns | ||
| // (inputType->size()). In this case numDependents is negative and cannot be | ||
|
|
@@ -153,6 +160,8 @@ HashBuild::HashBuild( | |
| // u.k AND t.k2 = u.k. | ||
| dependentChannels_.reserve(numDependents); | ||
| decoders_.reserve(numDependents); | ||
| dependentNames.reserve(numDependents); | ||
| dependentTypes.reserve(numDependents); | ||
| } | ||
| if (!dropDuplicates_) { | ||
| // For left semi and anti join with no extra filter, hash table does not | ||
|
|
@@ -163,18 +172,30 @@ HashBuild::HashBuild( | |
| decoders_.emplace_back(std::make_unique<DecodedVector>()); | ||
| names.emplace_back(inputType->nameOf(i)); | ||
| types.emplace_back(inputType->childAt(i)); | ||
| dependentNames.emplace_back(inputType->nameOf(i)); | ||
| dependentTypes.emplace_back(inputType->childAt(i)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| tableType_ = ROW(std::move(names), std::move(types)); | ||
| dependentTypes_ = ROW(std::move(dependentNames), std::move(dependentTypes)); | ||
| driverId_ = driverCtx->driverId; | ||
| if (hybridJoin_) { | ||
| BOLT_CHECK_LE( | ||
| driverId_, | ||
| 255, | ||
| "driverId {} exceeds maximum 255 for hybrid join mode", | ||
| driverId_); | ||
| } | ||
| setupTable(); | ||
| setupSpiller(); | ||
| intermediateStateCleared_ = false; | ||
|
|
||
| LOG(INFO) << name() << " HashBuild created for " << operatorCtx_->toString() | ||
| << ", spill enabled: " << spillEnabled() | ||
| << ", maxHashTableSize = " << maxHashTableBucketCount_; | ||
| << ", maxHashTableSize = " << maxHashTableBucketCount_ | ||
| << ", hybrid mode " << (hybridJoin_ ? "enabled" : "disbaled"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo disabled |
||
| } | ||
|
|
||
| void HashBuild::initialize() { | ||
|
|
@@ -215,7 +236,8 @@ void HashBuild::setupTable() { | |
| : BaseHashTable::HashMode::kArray, | ||
| queryConfig.minTableRowsForParallelJoinBuild(), | ||
| pool(), | ||
| queryConfig.enableJitRowEqVectors()); | ||
| queryConfig.enableJitRowEqVectors(), | ||
| hybridJoin_); | ||
| } else { | ||
| // Right semi join needs to tag build rows that were probed. | ||
| const bool needProbedFlag = joinNode_->isRightSemiFilterJoin(); | ||
|
|
@@ -231,7 +253,8 @@ void HashBuild::setupTable() { | |
| : BaseHashTable::HashMode::kArray, | ||
| queryConfig.minTableRowsForParallelJoinBuild(), | ||
| pool(), | ||
| queryConfig.enableJitRowEqVectors()); | ||
| queryConfig.enableJitRowEqVectors(), | ||
| hybridJoin_); | ||
| } else { | ||
| // Ignore null keys | ||
| table_ = HashTable<true>::createForJoin( | ||
|
|
@@ -243,13 +266,27 @@ void HashBuild::setupTable() { | |
| : BaseHashTable::HashMode::kArray, | ||
| queryConfig.minTableRowsForParallelJoinBuild(), | ||
| pool(), | ||
| queryConfig.enableJitRowEqVectors()); | ||
| queryConfig.enableJitRowEqVectors(), | ||
| hybridJoin_); | ||
| } | ||
| } | ||
| lookup_ = std::make_unique<HashLookup>( | ||
| table_->hashers(), queryConfig.enableJitRowEqVectors()); | ||
| lookup_->reset(1); | ||
| analyzeKeys_ = table_->hashMode() != BaseHashTable::HashMode::kHash; | ||
|
|
||
| if (hybridJoin_) { | ||
| table_->hybridData()->setId(static_cast<uint8_t>(driverId_)); | ||
| // Initialize allContainers_ with itself so spilling can work before table | ||
| // merge. | ||
| std::unordered_map<uint8_t, HybridContainer*> selfContainer; | ||
| selfContainer[static_cast<uint8_t>(driverId_)] = table_->hybridData(); | ||
| table_->hybridData()->setAllContainers(selfContainer); | ||
| // Set reorder flag from query config - can be disabled for deterministic | ||
| // testing. | ||
| table_->hybridData()->setReorderEnabled( | ||
| queryConfig.hybridJoinReorderEnabled()); | ||
| } | ||
| } | ||
|
|
||
| void HashBuild::setupSpiller( | ||
|
|
@@ -264,7 +301,8 @@ void HashBuild::setupSpiller( | |
|
|
||
| const auto& spillConfig = spillConfig_.value(); | ||
| bool canUseRowBasedSpill = joinBridge_->numBuilders() == 1 && | ||
| operatorCtx_->task()->numDrivers(operatorCtx_->driverCtx()) == 1; // TODO | ||
| operatorCtx_->task()->numDrivers(operatorCtx_->driverCtx()) == 1 && | ||
| !hybridJoin_; // Disable row-based spill for hybrid join mode | ||
| if (canUseRowBasedSpill) { | ||
| *const_cast<common::RowBasedSpillMode*>(&spillConfig.rowBasedSpillMode) = | ||
| common::strToRowBasedSpillMode( | ||
|
|
@@ -349,6 +387,12 @@ void HashBuild::setupSpiller( | |
| spiller_->setSkewThreshold( | ||
| skewFileSizeRatioThreshold_, skewRowCountRatioThreshold_); | ||
|
|
||
| // Enable hybrid mode for spiller if hybrid join is enabled. | ||
| // Works for both initial spill and repartition spill during restoration. | ||
| if (hybridJoin_ && table_->hybridData()) { | ||
| spiller_->setHybridMode(true, table_->hybridData()); | ||
| } | ||
|
|
||
| const int32_t numPartitions = spiller_->hashBits().numPartitions(); | ||
| spillLevel_ = spillConfig.joinSpillLevel(offsetTojoinBits_); | ||
| spillInputIndicesBuffers_.resize(numPartitions); | ||
|
|
@@ -572,21 +616,46 @@ void HashBuild::addInput(RowVectorPtr input) { | |
| } | ||
| auto rows = table_->rows(); | ||
| auto nextOffset = rows->nextOffset(); | ||
| activeRows_.applyToSelected([&](auto rowIndex) { | ||
| char* newRow = rows->newRow(); | ||
| if (nextOffset) { | ||
| *reinterpret_cast<char**>(newRow + nextOffset) = nullptr; | ||
| } | ||
| // Store the columns for each row in sequence. At probe time | ||
| // strings of the row will probably be in consecutive places, so | ||
| // reading one will prime the cache for the next. | ||
| for (auto i = 0; i < hashers.size(); ++i) { | ||
| rows->store(hashers[i]->decodedVector(), rowIndex, newRow, i); | ||
| } | ||
| for (auto i = 0; i < dependentChannels_.size(); ++i) { | ||
| rows->store(*decoders_[i], rowIndex, newRow, i + hashers.size()); | ||
| } | ||
| }); | ||
|
|
||
| if (hybridJoin_) { | ||
| activeRows_.applyToSelected([&](auto rowIndex) { | ||
| char* newRow = rows->newRow(); | ||
| if (nextOffset) { | ||
| *reinterpret_cast<char**>(newRow + nextOffset) = nullptr; | ||
| } | ||
| // Store the columns for each row in sequence. At probe time | ||
| // strings of the row will probably be in consecutive places, so | ||
| // reading one will prime the cache for the next. | ||
| for (auto i = 0; i < hashers.size(); ++i) { | ||
| rows->store(hashers[i]->decodedVector(), rowIndex, newRow, i); | ||
| } | ||
| // Store RowId | ||
| auto baseRow = table_->hybridData()->getNumRows(); | ||
| uint64_t encodedId = (static_cast<uint64_t>(driverId_) | ||
| << 56) | // top 8 bits: driverId [0, 255] | ||
| (static_cast<uint64_t>(rowIndex + baseRow) & ((1ULL << 56) - 1)); | ||
| rows->storeSingleRowId(encodedId, newRow); | ||
| }); | ||
| auto payloadInput = wrapColumns( | ||
| input->as<RowVector>(), dependentChannels_, dependentTypes_, pool()); | ||
| table_->hybridData()->addPayload(std::move(payloadInput)); | ||
| } else { | ||
| activeRows_.applyToSelected([&](auto rowIndex) { | ||
| char* newRow = rows->newRow(); | ||
| if (nextOffset) { | ||
| *reinterpret_cast<char**>(newRow + nextOffset) = nullptr; | ||
| } | ||
| // Store the columns for each row in sequence. At probe time | ||
| // strings of the row will probably be in consecutive places, so | ||
| // reading one will prime the cache for the next. | ||
| for (auto i = 0; i < hashers.size(); ++i) { | ||
| rows->store(hashers[i]->decodedVector(), rowIndex, newRow, i); | ||
| } | ||
| for (auto i = 0; i < dependentChannels_.size(); ++i) { | ||
| rows->store(*decoders_[i], rowIndex, newRow, i + hashers.size()); | ||
| } | ||
| }); | ||
| } | ||
| spillRowBasedInput(); | ||
| } | ||
|
|
||
|
|
@@ -897,6 +966,11 @@ void HashBuild::runSpill(const std::vector<Operator*>& spillOperators) { | |
| // run in parallel. | ||
| for (auto& spillOp : spillOperators) { | ||
| HashBuild* build = dynamic_cast<HashBuild*>(spillOp); | ||
| // Coalesce batches before spilling to ensure hybrid data is properly laid | ||
| // out. | ||
| if (build->hybridJoin_ && build->table_->hybridData()) { | ||
| build->table_->hybridData()->coalesceBatches(); | ||
| } | ||
| build->spiller_->spill(); | ||
| build->table_->clear(); | ||
| build->pool()->release(); | ||
|
|
@@ -922,6 +996,13 @@ void HashBuild::noMoreInput() { | |
| } | ||
|
|
||
| void HashBuild::noMoreInputInternal() { | ||
| // Coalesce batches in this driver's HybridContainer before merging with | ||
| // peers. This handles both the normal path (from noMoreInput) and spill | ||
| // restore path (from processSpillInput). Each driver does this independently. | ||
| if (hybridJoin_ && table_->hybridData()) { | ||
| table_->hybridData()->coalesceBatches(); | ||
| } | ||
|
|
||
| if (spillEnabled()) { | ||
| spillGroup_->operatorStopped(*this); | ||
| } | ||
|
|
@@ -1516,6 +1597,11 @@ void HashBuild::reclaim( | |
| spillTasks.push_back( | ||
| std::make_shared<AsyncSource<SpillResult>>([buildOp]() { | ||
| try { | ||
| // Coalesce batches before spilling to ensure hybrid data is | ||
| // properly laid out. | ||
| if (buildOp->hybridJoin_ && buildOp->table_->hybridData()) { | ||
| buildOp->table_->hybridData()->coalesceBatches(); | ||
| } | ||
| buildOp->spiller_->spill(); | ||
| buildOp->table_->clear(); | ||
| // Release the minimum reserved memory. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why hardcode limit to 255?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we store a BIGINT (64 bits) of rowId where the top 8 bits represents the driverId and the remaining 56 bits represents the rowId for each driver. So the max # of driver it supports is 255. Maybe we can make it as a config.