Skip to content

[Not for merge] Fix order of bucket processing in wide combine spilling #11471

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions ydb/library/yql/minikql/comp_nodes/mkql_wide_combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <util/string/cast.h>

#include <contrib/libs/xxhash/xxhash.h>

namespace NKikimr {
namespace NMiniKQL {

Expand Down Expand Up @@ -485,7 +487,8 @@ class TSpillingSupportState : public TComputationValue<TSpillingSupportState> {
}

auto hash = Hasher(ViewForKeyAndState.data());
auto bucketId = hash % SpilledBucketCount;
XXH64_hash_t hashed_hash = XXH64(&hash, sizeof(hash), 0);
auto bucketId = hashed_hash % SpilledBucketCount;
auto& bucket = SpilledBuckets[bucketId];

if (bucket.BucketState == TSpilledBucket::EBucketState::InMemory) {
Expand Down Expand Up @@ -592,14 +595,19 @@ class TSpillingSupportState : public TComputationValue<TSpillingSupportState> {
SplitStateSpillingBucket = -1;
}
while (const auto keyAndState = static_cast<NUdf::TUnboxedValue *>(InMemoryProcessingState.Extract())) {
auto hash = Hasher(keyAndState); //Hasher uses only key for hashing
auto bucketId = hash % SpilledBucketCount;
auto hash = Hasher(keyAndState); //Hasher uses only key for hashing
XXH64_hash_t hashed_hash = XXH64(&hash, sizeof(hash), 0);
auto bucketId = hashed_hash % SpilledBucketCount;
auto& bucket = SpilledBuckets[bucketId];

bucket.LineCount++;

if (bucket.BucketState != TSpilledBucket::EBucketState::InMemory) {
bucket.BucketState = TSpilledBucket::EBucketState::SpillingState;
if (bucket.BucketState != TSpilledBucket::EBucketState::SpillingState) {
bucket.BucketState = TSpilledBucket::EBucketState::SpillingState;
SpillingBucketsCount++;
}

bucket.AsyncWriteOperation = bucket.SpilledState->WriteWideItem({keyAndState, KeyAndStateType->GetElementsCount()});
for (size_t i = 0; i < KeyAndStateType->GetElementsCount(); ++i) {
//releasing values stored in unsafe TUnboxedValue buffer
Expand Down Expand Up @@ -628,10 +636,11 @@ class TSpillingSupportState : public TComputationValue<TSpillingSupportState> {
ui32 bucketNumToSpill = GetLargestInMemoryBucketNumber();

SplitStateSpillingBucket = bucketNumToSpill;
InMemoryBucketsCount--;

auto& bucket = SpilledBuckets[bucketNumToSpill];
bucket.BucketState = TSpilledBucket::EBucketState::SpillingState;
SpillingBucketsCount++;
InMemoryBucketsCount--;

while (const auto keyAndState = static_cast<NUdf::TUnboxedValue*>(bucket.InMemoryProcessingState->Extract())) {
bucket.AsyncWriteOperation = bucket.SpilledState->WriteWideItem({keyAndState, KeyAndStateType->GetElementsCount()});
Expand Down Expand Up @@ -661,6 +670,7 @@ class TSpillingSupportState : public TComputationValue<TSpillingSupportState> {
bucket.InMemoryProcessingState->ReadMore<false>();

bucket.BucketState = TSpilledBucket::EBucketState::SpillingData;
SpillingBucketsCount--;
}
}

Expand Down Expand Up @@ -842,6 +852,12 @@ class TSpillingSupportState : public TComputationValue<TSpillingSupportState> {
break;
}
case EOperatingMode::ProcessSpilled: {
std::sort(SpilledBuckets.begin(), SpilledBuckets.end(), [](const TSpilledBucket& lhs, const TSpilledBucket& rhs) {
bool lhs_in_memory = lhs.BucketState == TSpilledBucket::EBucketState::InMemory;
bool rhs_in_memory = rhs.BucketState == TSpilledBucket::EBucketState::InMemory;
return lhs_in_memory > rhs_in_memory;
});

YQL_LOG(INFO) << "switching Memory mode to ProcessSpilled";
MKQL_ENSURE(EOperatingMode::Spilling == Mode, "Internal logic error");
MKQL_ENSURE(SpilledBuckets.size() == SpilledBucketCount, "Internal logic error");
Expand Down
Loading