Copilot commented on code in PR #13216: URL: https://github.com/apache/gluten/pull/13216#discussion_r4566458620
########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "operators/hashjoin/HashTableTaskBuilder.h" + +#include <atomic> + +#include "velox/exec/HashTableCache.h" +#include "velox/exec/RoundRobinPartitionFunction.h" +#include "velox/exec/Task.h" + +using namespace facebook::velox; + +namespace gluten { +namespace { + +// Output type of the fake join. The probe side is empty, so the join outputs +// nothing and needs no columns, except the match column of a semi project join. +RowTypePtr fakeJoinOutputType(core::JoinType joinType) { + if (core::isLeftSemiProjectJoin(joinType) || core::isRightSemiProjectJoin(joinType)) { + return ROW({"__fake_match"}, {BOOLEAN()}); + } + return ROW({}, {}); +} + +// A stand-in for the join filter with the properties HashBuild looks at, see +// HashTableTaskBuildSpec. It is compiled by HashProbe but never evaluated. +core::TypedExprPtr makePlaceholderFilter(const HashTableTaskBuildSpec& spec) { + if (!spec.withFilter) { + return nullptr; + } + if (!spec.filterPropagatesNulls || spec.filterBuildColumns.empty()) { + // References no build column, so HashBuild drops no build rows for it. + return std::make_shared<core::ConstantTypedExpr>(BOOLEAN(), Variant(true)); + } + // Chains the build columns, cast to VARCHAR to fit any type, with equalto. + // Both have default null behavior, so the result is null-intolerant in each + // of them. + auto toVarchar = [&](const core::TypedExprPtr& expr) -> core::TypedExprPtr { + return std::make_shared<core::CastTypedExpr>(VARCHAR(), expr, false); + }; + auto column = [&](const std::string& name) -> core::TypedExprPtr { + return toVarchar(std::make_shared<core::FieldAccessTypedExpr>(spec.buildType->findChild(name), name)); + }; + auto equalTo = [](const core::TypedExprPtr& left, const core::TypedExprPtr& right) -> core::TypedExprPtr { + return std::make_shared<core::CallTypedExpr>(BOOLEAN(), std::vector<core::TypedExprPtr>{left, right}, "equalto"); + }; + auto filter = equalTo(column(spec.filterBuildColumns[0]), column(spec.filterBuildColumns[0])); + for (auto i = 1; i < spec.filterBuildColumns.size(); ++i) { + filter = equalTo(toVarchar(filter), column(spec.filterBuildColumns[i])); + } + return filter; +} + +// An empty join table with the join keys of 'spec'. +std::shared_ptr<exec::BaseHashTable> makeEmptyJoinTable(const HashTableTaskBuildSpec& spec, memory::MemoryPool* pool) { + std::vector<std::unique_ptr<exec::VectorHasher>> hashers; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + hashers.emplace_back(std::make_unique<exec::VectorHasher>(spec.buildType->findChild(spec.joinKeys[i]), i)); + } + exec::BaseHashTable::Options options; + options.minTableSizeForParallelJoinBuild = 1'000; + options.allowDuplicates = true; + options.hasProbedFlag = false; + options.hasCountFlag = false; + auto table = exec::HashTable<false>::createForJoin(std::move(hashers), {}, pool, options); + table->prepareJoinTable({}, exec::BaseHashTable::kNoSpillInputStartPartitionBit, 1'000'000, false, nullptr); + return table; +} + +std::string nextBuildId() { + static std::atomic<uint64_t> id{0}; + return std::to_string(id++); +} + +// A table and the pool it is allocated from. Members are destroyed in reverse +// order, so 'table' returns its memory before 'pool' goes. +struct TableWithPool { + std::shared_ptr<memory::MemoryPool> pool; + std::shared_ptr<exec::BaseHashTable> table; +}; + +} // namespace + +std::shared_ptr<BroadcastHashTable> buildHashTableWithTask( + const std::string& cacheKey, + const HashTableTaskBuildSpec& spec, + const std::vector<RowVectorPtr>& buildVectors, + uint32_t numDrivers, + folly::Executor* executor, + std::shared_ptr<memory::MemoryPool> queryPool, + memory::MemoryPool* vectorPool) { + VELOX_CHECK_GT(numDrivers, 0); + VELOX_CHECK(numDrivers == 1 || executor != nullptr, "Parallel hash table build requires an executor"); + const auto buildId = nextBuildId(); + LOG(WARNING) << "[BHJ-DEBUG] native build start: key=" << cacheKey << ", buildId=" << buildId + << ", cacheEntryExists=" << exec::HashTableCache::instance()->exist(cacheKey); + auto queryCtx = core::QueryCtx::create( + numDrivers > 1 ? executor : nullptr, + core::QueryConfig(spec.queryConfigs), + {}, + nullptr, + std::move(queryPool), + nullptr, + fmt::format("Gluten_HashTableBuild_{}_{}", cacheKey, buildId)); + + // Probe side: an empty input with one column per join key. + std::vector<std::string> probeNames; + std::vector<TypePtr> probeTypes; + std::vector<core::FieldAccessTypedExprPtr> leftKeys; + std::vector<core::FieldAccessTypedExprPtr> rightKeys; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + const auto& type = spec.buildType->findChild(spec.joinKeys[i]); + probeNames.emplace_back(fmt::format("__fake_probe_k{}", i)); + probeTypes.emplace_back(type); + leftKeys.emplace_back(std::make_shared<core::FieldAccessTypedExpr>(type, probeNames.back())); + rightKeys.emplace_back(std::make_shared<core::FieldAccessTypedExpr>(type, spec.joinKeys[i])); + } + auto probeType = ROW(std::move(probeNames), std::move(probeTypes)); + + int planNodeId = 0; + auto nextPlanNodeId = [&]() { return std::to_string(planNodeId++); }; + core::PlanNodePtr probe = std::make_shared<core::ValuesNode>( + nextPlanNodeId(), std::vector<RowVectorPtr>{BaseVector::create<RowVector>(probeType, 0, vectorPool)}); + + // Build side: the broadcast vectors, renamed to 'buildType'. + std::vector<RowVectorPtr> values; + values.reserve(buildVectors.size()); + for (const auto& vector : buildVectors) { + values.emplace_back(std::make_shared<RowVector>( + vector->pool(), spec.buildType, vector->nulls(), vector->size(), vector->children())); + } + if (values.empty()) { + values.emplace_back(BaseVector::create<RowVector>(spec.buildType, 0, vectorPool)); + } + core::PlanNodePtr build = std::make_shared<core::ValuesNode>(nextPlanNodeId(), std::move(values)); + if (numDrivers > 1) { + // Fan the broadcast vectors out to all HashBuild drivers. + build = std::make_shared<core::LocalPartitionNode>( + nextPlanNodeId(), + core::LocalPartitionNode::Type::kRepartition, + false, + std::make_shared<exec::RoundRobinPartitionFunctionSpec>(), + std::vector<core::PlanNodePtr>{build}); + } + + auto joinNode = core::HashJoinNode::Builder() + .id(nextPlanNodeId()) + .joinType(spec.joinType) + .leftKeys(std::move(leftKeys)) + .rightKeys(std::move(rightKeys)) + .filter(makePlaceholderFilter(spec)) + .left(probe) + .right(build) + .outputType(fakeJoinOutputType(spec.joinType)) + .nullAware(spec.nullAware) + .useHashTableCache(true) + .cacheKey(cacheKey) + .build(); + + std::shared_ptr<exec::Task> task; + if (numDrivers == 1) { + task = exec::Task::create( + queryCtx->queryId(), core::PlanFragment{joinNode}, 0, queryCtx, exec::Task::ExecutionMode::kSerial); + while (true) { + ContinueFuture future = ContinueFuture::makeEmpty(); + if (task->next(&future) == nullptr) { + if (!future.valid()) { + break; + } + future.wait(); + } + } + } else { + task = exec::Task::create( + queryCtx->queryId(), + core::PlanFragment{joinNode}, + 0, + queryCtx, + exec::Task::ExecutionMode::kParallel, + [](RowVectorPtr, bool, ContinueFuture*) { return exec::BlockingReason::kNotBlocked; }); + task->start(numDrivers); + task->taskCompletionFuture().wait(); + } + if (task->state() != exec::TaskState::kFinished) { + if (auto error = task->error()) { + std::rethrow_exception(error); + } + VELOX_FAIL("Hash table build task {} ended in state {}", task->taskId(), exec::taskStateString(task->state())); + } + // Drivers can hold the task, and so the QueryCtx, briefly after it finishes. + // Wait for the task to go, so that releasing the returned table releases the + // QueryCtx and with it the cache entry. + const auto taskId = task->taskId(); + auto taskDeleted = task->taskDeletionFuture(); + task.reset(); + taskDeleted.wait(); + + auto* cache = exec::HashTableCache::instance(); + LOG(WARNING) << "[BHJ-DEBUG] native build task done: key=" << cacheKey << ", buildId=" << buildId + << ", taskId=" << taskId << ", cacheEntryExists=" << cache->exist(cacheKey); + queryCtx->addReleaseCallback([cacheKey, buildId]() { + LOG(WARNING) << "[BHJ-DEBUG] native build QueryCtx released: key=" << cacheKey << ", buildId=" << buildId + << ", cacheEntryExists=" << exec::HashTableCache::instance()->exist(cacheKey); + }); + if (!cache->exist(cacheKey)) { + // HashBuild does not cache the table of a null-aware anti join without + // filter whose build side has a null key. Such a join returns nothing + // whatever the table holds, so cache an empty table flagged with null keys. + VELOX_CHECK( + core::isAntiJoin(spec.joinType) && spec.nullAware && !spec.withFilter, + "Hash table build task did not cache table {}", + cacheKey); + auto tablePool = queryCtx->pool()->addLeafChild(fmt::format("hash_table_build_empty_{}", buildId)); + cache->add(cacheKey, makeEmptyJoinTable(spec, tablePool.get()), true, tablePool); + queryCtx->addReleaseCallback([cacheKey]() { exec::HashTableCache::instance()->drop(cacheKey); }); + } + ContinueFuture future = ContinueFuture::makeEmpty(); + auto entry = cache->get(cacheKey, taskId, queryCtx.get(), &future); + VELOX_CHECK(!future.valid()); + VELOX_CHECK(entry->buildComplete); + // When HashBuild abandons dedup, it allows duplicates in a table whose rows + // have no next-row pointer. The flag no longer matters once the table is + // built, but HashTable::serializeTo() records it as the row layout, so make + // it match for the driver-side build. Nothing probes the table yet. + if (entry->table->rows()->nextOffset() == 0) { + entry->table->setAllowDuplicates(false); + } + + // HashBuild allocated the table from 'entry->tablePool', but cached it as a + // plain pointer that does not keep that pool alive. Probe tasks copy that + // pointer out of the entry and can outlive both the entry and the returned + // table, e.g. when a task fails while Java releases the broadcast table. + // The pool would then go first and the table would free into a dead pool. + // + // So replace the cached pointer with one built by the shared_ptr aliasing + // constructor: it points to the table but shares ownership of a + // TableWithPool, so every copy keeps both the table and its pool alive. Velox + // does the same for cached Nimble tablets, see ReaderBase.cpp. + auto tableWithPool = std::make_shared<TableWithPool>(TableWithPool{entry->tablePool, entry->table}); + entry->table = std::shared_ptr<exec::BaseHashTable>(tableWithPool, tableWithPool->table.get()); + + return std::make_shared<BroadcastHashTable>( + entry->table, entry->hasNullKeys, entry->tablePool->usedBytes(), std::move(queryCtx)); Review Comment: This mutates `entry->table` after the cache entry is published as `buildComplete`. If other threads can concurrently read/copy `entry->table` from the same `HashTableCache` entry, this write can introduce a data race (and a window where other readers still observe the old pointer semantics). Prefer making the cache store the pool-owning shared_ptr at insertion time (or updating the entry under whatever synchronization `HashTableCache` provides), to ensure readers never see an intermediate/unsafe state. ########## backends-velox/src/main/scala/org/apache/gluten/execution/VeloxBroadcastBuildSideCache.scala: ########## @@ -83,14 +83,32 @@ object VeloxBroadcastBuildSideCache } ).build[String, SerializedBroadcastHashTable]() + private def debugTaskInfo(): String = { + val tc = TaskContext.get() + val task = + if (tc == null) "none" + else { + s"stage=${tc.stageId()}.${tc.stageAttemptNumber()}, partition=${tc.partitionId()}, " + + s"TID=${tc.taskAttemptId()}, executionId=${tc.getLocalProperty("spark.sql.execution.id")}" + } + s"$task, thread=${Thread.currentThread().getName}" + } + def getOrBuildBroadcastHashTable( broadcast: Broadcast[BuildSideRelation], broadcastContext: BroadcastHashJoinContext): BroadcastHashTable = { + val debugKey = broadcastContext.buildHashTableId + logWarning( + s"[BHJ-DEBUG] getOrBuild: key=$debugKey, " + + s"cached=${buildSideRelationCache.getIfPresent(debugKey) != null}, " + debugTaskInfo()) Review Comment: These `[BHJ-DEBUG]` messages use `logWarning` in hot paths (`getOrBuildBroadcastHashTable`, cache loader, and removal listener). This can generate large volumes of WARN logs on busy clusters. Recommend switching to `logDebug` (or a feature-flagged logger), and keeping WARN for actionable issues only. ########## backends-velox/src/main/scala/org/apache/gluten/execution/VeloxBroadcastBuildSideCache.scala: ########## @@ -83,14 +83,32 @@ object VeloxBroadcastBuildSideCache } ).build[String, SerializedBroadcastHashTable]() + private def debugTaskInfo(): String = { + val tc = TaskContext.get() + val task = + if (tc == null) "none" + else { + s"stage=${tc.stageId()}.${tc.stageAttemptNumber()}, partition=${tc.partitionId()}, " + + s"TID=${tc.taskAttemptId()}, executionId=${tc.getLocalProperty("spark.sql.execution.id")}" + } + s"$task, thread=${Thread.currentThread().getName}" + } + def getOrBuildBroadcastHashTable( broadcast: Broadcast[BuildSideRelation], broadcastContext: BroadcastHashJoinContext): BroadcastHashTable = { + val debugKey = broadcastContext.buildHashTableId + logWarning( + s"[BHJ-DEBUG] getOrBuild: key=$debugKey, " + + s"cached=${buildSideRelationCache.getIfPresent(debugKey) != null}, " + debugTaskInfo()) buildSideRelationCache .get( broadcastContext.buildHashTableId, (_: String) => { + logWarning( + s"[BHJ-DEBUG] build start: key=${broadcastContext.buildHashTableId}, " + + debugTaskInfo()) Review Comment: These `[BHJ-DEBUG]` messages use `logWarning` in hot paths (`getOrBuildBroadcastHashTable`, cache loader, and removal listener). This can generate large volumes of WARN logs on busy clusters. Recommend switching to `logDebug` (or a feature-flagged logger), and keeping WARN for actionable issues only. ########## backends-velox/src/main/scala/org/apache/gluten/execution/VeloxBroadcastBuildSideCache.scala: ########## @@ -100,6 +118,9 @@ object VeloxBroadcastBuildSideCache broadcastContext.hashTableMemorySizeMetric.foreach( _ += HashJoinBuilder.getHashTableMemoryUsage(pointer)) + logWarning( + s"[BHJ-DEBUG] build end: key=${broadcastContext.buildHashTableId}, " + + s"pointer=$pointer, " + debugTaskInfo()) Review Comment: These `[BHJ-DEBUG]` messages use `logWarning` in hot paths (`getOrBuildBroadcastHashTable`, cache loader, and removal listener). This can generate large volumes of WARN logs on busy clusters. Recommend switching to `logDebug` (or a feature-flagged logger), and keeping WARN for actionable issues only. ########## cpp/velox/operators/hashjoin/BroadcastHashTable.h: ########## @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + Review Comment: This header publicly exposes `std::shared_ptr` and `int64_t` but does not include `<memory>` / `<cstdint>`. It may compile today via transitive includes from Velox headers, but it’s fragile. Add explicit standard includes to stabilize compilation. ########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "operators/hashjoin/HashTableTaskBuilder.h" + +#include <atomic> + +#include "velox/exec/HashTableCache.h" +#include "velox/exec/RoundRobinPartitionFunction.h" +#include "velox/exec/Task.h" + +using namespace facebook::velox; + +namespace gluten { +namespace { + +// Output type of the fake join. The probe side is empty, so the join outputs +// nothing and needs no columns, except the match column of a semi project join. +RowTypePtr fakeJoinOutputType(core::JoinType joinType) { + if (core::isLeftSemiProjectJoin(joinType) || core::isRightSemiProjectJoin(joinType)) { + return ROW({"__fake_match"}, {BOOLEAN()}); + } + return ROW({}, {}); +} + +// A stand-in for the join filter with the properties HashBuild looks at, see +// HashTableTaskBuildSpec. It is compiled by HashProbe but never evaluated. +core::TypedExprPtr makePlaceholderFilter(const HashTableTaskBuildSpec& spec) { + if (!spec.withFilter) { + return nullptr; + } + if (!spec.filterPropagatesNulls || spec.filterBuildColumns.empty()) { + // References no build column, so HashBuild drops no build rows for it. + return std::make_shared<core::ConstantTypedExpr>(BOOLEAN(), Variant(true)); + } + // Chains the build columns, cast to VARCHAR to fit any type, with equalto. + // Both have default null behavior, so the result is null-intolerant in each + // of them. + auto toVarchar = [&](const core::TypedExprPtr& expr) -> core::TypedExprPtr { + return std::make_shared<core::CastTypedExpr>(VARCHAR(), expr, false); + }; + auto column = [&](const std::string& name) -> core::TypedExprPtr { + return toVarchar(std::make_shared<core::FieldAccessTypedExpr>(spec.buildType->findChild(name), name)); + }; + auto equalTo = [](const core::TypedExprPtr& left, const core::TypedExprPtr& right) -> core::TypedExprPtr { + return std::make_shared<core::CallTypedExpr>(BOOLEAN(), std::vector<core::TypedExprPtr>{left, right}, "equalto"); + }; + auto filter = equalTo(column(spec.filterBuildColumns[0]), column(spec.filterBuildColumns[0])); + for (auto i = 1; i < spec.filterBuildColumns.size(); ++i) { + filter = equalTo(toVarchar(filter), column(spec.filterBuildColumns[i])); + } + return filter; +} + +// An empty join table with the join keys of 'spec'. +std::shared_ptr<exec::BaseHashTable> makeEmptyJoinTable(const HashTableTaskBuildSpec& spec, memory::MemoryPool* pool) { + std::vector<std::unique_ptr<exec::VectorHasher>> hashers; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + hashers.emplace_back(std::make_unique<exec::VectorHasher>(spec.buildType->findChild(spec.joinKeys[i]), i)); + } + exec::BaseHashTable::Options options; + options.minTableSizeForParallelJoinBuild = 1'000; + options.allowDuplicates = true; + options.hasProbedFlag = false; + options.hasCountFlag = false; + auto table = exec::HashTable<false>::createForJoin(std::move(hashers), {}, pool, options); + table->prepareJoinTable({}, exec::BaseHashTable::kNoSpillInputStartPartitionBit, 1'000'000, false, nullptr); + return table; +} + +std::string nextBuildId() { + static std::atomic<uint64_t> id{0}; + return std::to_string(id++); +} + +// A table and the pool it is allocated from. Members are destroyed in reverse +// order, so 'table' returns its memory before 'pool' goes. +struct TableWithPool { + std::shared_ptr<memory::MemoryPool> pool; + std::shared_ptr<exec::BaseHashTable> table; +}; + +} // namespace + +std::shared_ptr<BroadcastHashTable> buildHashTableWithTask( + const std::string& cacheKey, + const HashTableTaskBuildSpec& spec, + const std::vector<RowVectorPtr>& buildVectors, + uint32_t numDrivers, + folly::Executor* executor, + std::shared_ptr<memory::MemoryPool> queryPool, + memory::MemoryPool* vectorPool) { + VELOX_CHECK_GT(numDrivers, 0); + VELOX_CHECK(numDrivers == 1 || executor != nullptr, "Parallel hash table build requires an executor"); + const auto buildId = nextBuildId(); + LOG(WARNING) << "[BHJ-DEBUG] native build start: key=" << cacheKey << ", buildId=" << buildId + << ", cacheEntryExists=" << exec::HashTableCache::instance()->exist(cacheKey); + auto queryCtx = core::QueryCtx::create( + numDrivers > 1 ? executor : nullptr, + core::QueryConfig(spec.queryConfigs), + {}, + nullptr, + std::move(queryPool), + nullptr, + fmt::format("Gluten_HashTableBuild_{}_{}", cacheKey, buildId)); + + // Probe side: an empty input with one column per join key. + std::vector<std::string> probeNames; + std::vector<TypePtr> probeTypes; + std::vector<core::FieldAccessTypedExprPtr> leftKeys; + std::vector<core::FieldAccessTypedExprPtr> rightKeys; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + const auto& type = spec.buildType->findChild(spec.joinKeys[i]); + probeNames.emplace_back(fmt::format("__fake_probe_k{}", i)); + probeTypes.emplace_back(type); + leftKeys.emplace_back(std::make_shared<core::FieldAccessTypedExpr>(type, probeNames.back())); + rightKeys.emplace_back(std::make_shared<core::FieldAccessTypedExpr>(type, spec.joinKeys[i])); + } + auto probeType = ROW(std::move(probeNames), std::move(probeTypes)); + + int planNodeId = 0; + auto nextPlanNodeId = [&]() { return std::to_string(planNodeId++); }; + core::PlanNodePtr probe = std::make_shared<core::ValuesNode>( + nextPlanNodeId(), std::vector<RowVectorPtr>{BaseVector::create<RowVector>(probeType, 0, vectorPool)}); + + // Build side: the broadcast vectors, renamed to 'buildType'. + std::vector<RowVectorPtr> values; + values.reserve(buildVectors.size()); + for (const auto& vector : buildVectors) { + values.emplace_back(std::make_shared<RowVector>( + vector->pool(), spec.buildType, vector->nulls(), vector->size(), vector->children())); + } + if (values.empty()) { + values.emplace_back(BaseVector::create<RowVector>(spec.buildType, 0, vectorPool)); + } + core::PlanNodePtr build = std::make_shared<core::ValuesNode>(nextPlanNodeId(), std::move(values)); + if (numDrivers > 1) { + // Fan the broadcast vectors out to all HashBuild drivers. + build = std::make_shared<core::LocalPartitionNode>( + nextPlanNodeId(), + core::LocalPartitionNode::Type::kRepartition, + false, + std::make_shared<exec::RoundRobinPartitionFunctionSpec>(), + std::vector<core::PlanNodePtr>{build}); + } + + auto joinNode = core::HashJoinNode::Builder() + .id(nextPlanNodeId()) + .joinType(spec.joinType) + .leftKeys(std::move(leftKeys)) + .rightKeys(std::move(rightKeys)) + .filter(makePlaceholderFilter(spec)) + .left(probe) + .right(build) + .outputType(fakeJoinOutputType(spec.joinType)) + .nullAware(spec.nullAware) + .useHashTableCache(true) + .cacheKey(cacheKey) + .build(); + + std::shared_ptr<exec::Task> task; + if (numDrivers == 1) { + task = exec::Task::create( + queryCtx->queryId(), core::PlanFragment{joinNode}, 0, queryCtx, exec::Task::ExecutionMode::kSerial); + while (true) { + ContinueFuture future = ContinueFuture::makeEmpty(); + if (task->next(&future) == nullptr) { + if (!future.valid()) { + break; + } + future.wait(); + } + } + } else { + task = exec::Task::create( + queryCtx->queryId(), + core::PlanFragment{joinNode}, + 0, + queryCtx, + exec::Task::ExecutionMode::kParallel, + [](RowVectorPtr, bool, ContinueFuture*) { return exec::BlockingReason::kNotBlocked; }); + task->start(numDrivers); + task->taskCompletionFuture().wait(); + } + if (task->state() != exec::TaskState::kFinished) { + if (auto error = task->error()) { + std::rethrow_exception(error); + } + VELOX_FAIL("Hash table build task {} ended in state {}", task->taskId(), exec::taskStateString(task->state())); + } + // Drivers can hold the task, and so the QueryCtx, briefly after it finishes. + // Wait for the task to go, so that releasing the returned table releases the + // QueryCtx and with it the cache entry. + const auto taskId = task->taskId(); + auto taskDeleted = task->taskDeletionFuture(); + task.reset(); + taskDeleted.wait(); + + auto* cache = exec::HashTableCache::instance(); + LOG(WARNING) << "[BHJ-DEBUG] native build task done: key=" << cacheKey << ", buildId=" << buildId + << ", taskId=" << taskId << ", cacheEntryExists=" << cache->exist(cacheKey); + queryCtx->addReleaseCallback([cacheKey, buildId]() { + LOG(WARNING) << "[BHJ-DEBUG] native build QueryCtx released: key=" << cacheKey << ", buildId=" << buildId + << ", cacheEntryExists=" << exec::HashTableCache::instance()->exist(cacheKey); + }); + if (!cache->exist(cacheKey)) { Review Comment: These `[BHJ-DEBUG]` logs are emitted at WARNING level and will be very noisy in production for broadcast joins (especially because `exist()` is called multiple times). Recommend gating behind a debug flag (e.g., VLOG / conditional config), lowering to INFO/DEBUG, and/or using rate-limited logging (e.g., `LOG_EVERY_N`) to avoid flooding logs and impacting operational observability. ########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "operators/hashjoin/HashTableTaskBuilder.h" + +#include <atomic> + +#include "velox/exec/HashTableCache.h" +#include "velox/exec/RoundRobinPartitionFunction.h" +#include "velox/exec/Task.h" + +using namespace facebook::velox; + +namespace gluten { +namespace { + +// Output type of the fake join. The probe side is empty, so the join outputs +// nothing and needs no columns, except the match column of a semi project join. +RowTypePtr fakeJoinOutputType(core::JoinType joinType) { + if (core::isLeftSemiProjectJoin(joinType) || core::isRightSemiProjectJoin(joinType)) { + return ROW({"__fake_match"}, {BOOLEAN()}); + } + return ROW({}, {}); +} + +// A stand-in for the join filter with the properties HashBuild looks at, see +// HashTableTaskBuildSpec. It is compiled by HashProbe but never evaluated. +core::TypedExprPtr makePlaceholderFilter(const HashTableTaskBuildSpec& spec) { + if (!spec.withFilter) { + return nullptr; + } + if (!spec.filterPropagatesNulls || spec.filterBuildColumns.empty()) { + // References no build column, so HashBuild drops no build rows for it. + return std::make_shared<core::ConstantTypedExpr>(BOOLEAN(), Variant(true)); + } + // Chains the build columns, cast to VARCHAR to fit any type, with equalto. + // Both have default null behavior, so the result is null-intolerant in each + // of them. + auto toVarchar = [&](const core::TypedExprPtr& expr) -> core::TypedExprPtr { + return std::make_shared<core::CastTypedExpr>(VARCHAR(), expr, false); + }; + auto column = [&](const std::string& name) -> core::TypedExprPtr { + return toVarchar(std::make_shared<core::FieldAccessTypedExpr>(spec.buildType->findChild(name), name)); + }; + auto equalTo = [](const core::TypedExprPtr& left, const core::TypedExprPtr& right) -> core::TypedExprPtr { + return std::make_shared<core::CallTypedExpr>(BOOLEAN(), std::vector<core::TypedExprPtr>{left, right}, "equalto"); + }; + auto filter = equalTo(column(spec.filterBuildColumns[0]), column(spec.filterBuildColumns[0])); + for (auto i = 1; i < spec.filterBuildColumns.size(); ++i) { + filter = equalTo(toVarchar(filter), column(spec.filterBuildColumns[i])); + } + return filter; +} + +// An empty join table with the join keys of 'spec'. +std::shared_ptr<exec::BaseHashTable> makeEmptyJoinTable(const HashTableTaskBuildSpec& spec, memory::MemoryPool* pool) { + std::vector<std::unique_ptr<exec::VectorHasher>> hashers; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + hashers.emplace_back(std::make_unique<exec::VectorHasher>(spec.buildType->findChild(spec.joinKeys[i]), i)); Review Comment: Constructing `exec::VectorHasher` directly and hard-coding the channel as `i` makes this helper sensitive to Velox API/layout changes and to assumptions about key channel ordering. Where possible, prefer the standard Velox factory (`VectorHasher::create(...)`) and derive the channel from the row type (e.g., child index lookup) to make the intent explicit and reduce coupling to internal constructor details. ########## backends-velox/src/main/scala/org/apache/gluten/execution/VeloxBroadcastBuildSideCache.scala: ########## @@ -251,5 +280,8 @@ object VeloxBroadcastBuildSideCache HashJoinBuilder.clearHashTable(key, value.pointer) } + logWarning( + s"[BHJ-DEBUG] onRemoval end: key=$key, pointer=${value.pointer}, " + + s"thread=${Thread.currentThread().getName}") Review Comment: These `[BHJ-DEBUG]` messages use `logWarning` in hot paths (`getOrBuildBroadcastHashTable`, cache loader, and removal listener). This can generate large volumes of WARN logs on busy clusters. Recommend switching to `logDebug` (or a feature-flagged logger), and keeping WARN for actionable issues only. ########## backends-velox/src/main/scala/org/apache/gluten/execution/VeloxBroadcastBuildSideCache.scala: ########## @@ -239,6 +265,9 @@ object VeloxBroadcastBuildSideCache key: String, value: BroadcastHashTable, cause: RemovalCause): Unit = { + logWarning( + s"[BHJ-DEBUG] onRemoval start: key=$key, pointer=${value.pointer}, cause=$cause, " + + s"thread=${Thread.currentThread().getName}") Review Comment: These `[BHJ-DEBUG]` messages use `logWarning` in hot paths (`getOrBuildBroadcastHashTable`, cache loader, and removal listener). This can generate large volumes of WARN logs on busy clusters. Recommend switching to `logDebug` (or a feature-flagged logger), and keeping WARN for actionable issues only. ########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "operators/hashjoin/HashTableTaskBuilder.h" + +#include <atomic> + +#include "velox/exec/HashTableCache.h" +#include "velox/exec/RoundRobinPartitionFunction.h" +#include "velox/exec/Task.h" + +using namespace facebook::velox; + +namespace gluten { +namespace { + +// Output type of the fake join. The probe side is empty, so the join outputs +// nothing and needs no columns, except the match column of a semi project join. +RowTypePtr fakeJoinOutputType(core::JoinType joinType) { + if (core::isLeftSemiProjectJoin(joinType) || core::isRightSemiProjectJoin(joinType)) { + return ROW({"__fake_match"}, {BOOLEAN()}); + } + return ROW({}, {}); +} + +// A stand-in for the join filter with the properties HashBuild looks at, see +// HashTableTaskBuildSpec. It is compiled by HashProbe but never evaluated. +core::TypedExprPtr makePlaceholderFilter(const HashTableTaskBuildSpec& spec) { + if (!spec.withFilter) { + return nullptr; + } + if (!spec.filterPropagatesNulls || spec.filterBuildColumns.empty()) { + // References no build column, so HashBuild drops no build rows for it. + return std::make_shared<core::ConstantTypedExpr>(BOOLEAN(), Variant(true)); + } + // Chains the build columns, cast to VARCHAR to fit any type, with equalto. + // Both have default null behavior, so the result is null-intolerant in each + // of them. + auto toVarchar = [&](const core::TypedExprPtr& expr) -> core::TypedExprPtr { + return std::make_shared<core::CastTypedExpr>(VARCHAR(), expr, false); + }; + auto column = [&](const std::string& name) -> core::TypedExprPtr { + return toVarchar(std::make_shared<core::FieldAccessTypedExpr>(spec.buildType->findChild(name), name)); + }; + auto equalTo = [](const core::TypedExprPtr& left, const core::TypedExprPtr& right) -> core::TypedExprPtr { + return std::make_shared<core::CallTypedExpr>(BOOLEAN(), std::vector<core::TypedExprPtr>{left, right}, "equalto"); + }; + auto filter = equalTo(column(spec.filterBuildColumns[0]), column(spec.filterBuildColumns[0])); + for (auto i = 1; i < spec.filterBuildColumns.size(); ++i) { + filter = equalTo(toVarchar(filter), column(spec.filterBuildColumns[i])); + } + return filter; +} + +// An empty join table with the join keys of 'spec'. +std::shared_ptr<exec::BaseHashTable> makeEmptyJoinTable(const HashTableTaskBuildSpec& spec, memory::MemoryPool* pool) { + std::vector<std::unique_ptr<exec::VectorHasher>> hashers; + for (auto i = 0; i < spec.joinKeys.size(); ++i) { + hashers.emplace_back(std::make_unique<exec::VectorHasher>(spec.buildType->findChild(spec.joinKeys[i]), i)); + } + exec::BaseHashTable::Options options; + options.minTableSizeForParallelJoinBuild = 1'000; + options.allowDuplicates = true; + options.hasProbedFlag = false; + options.hasCountFlag = false; + auto table = exec::HashTable<false>::createForJoin(std::move(hashers), {}, pool, options); + table->prepareJoinTable({}, exec::BaseHashTable::kNoSpillInputStartPartitionBit, 1'000'000, false, nullptr); + return table; +} + +std::string nextBuildId() { + static std::atomic<uint64_t> id{0}; + return std::to_string(id++); +} + +// A table and the pool it is allocated from. Members are destroyed in reverse +// order, so 'table' returns its memory before 'pool' goes. +struct TableWithPool { + std::shared_ptr<memory::MemoryPool> pool; + std::shared_ptr<exec::BaseHashTable> table; +}; + +} // namespace + +std::shared_ptr<BroadcastHashTable> buildHashTableWithTask( + const std::string& cacheKey, + const HashTableTaskBuildSpec& spec, + const std::vector<RowVectorPtr>& buildVectors, + uint32_t numDrivers, + folly::Executor* executor, + std::shared_ptr<memory::MemoryPool> queryPool, + memory::MemoryPool* vectorPool) { + VELOX_CHECK_GT(numDrivers, 0); + VELOX_CHECK(numDrivers == 1 || executor != nullptr, "Parallel hash table build requires an executor"); + const auto buildId = nextBuildId(); + LOG(WARNING) << "[BHJ-DEBUG] native build start: key=" << cacheKey << ", buildId=" << buildId + << ", cacheEntryExists=" << exec::HashTableCache::instance()->exist(cacheKey); Review Comment: These `[BHJ-DEBUG]` logs are emitted at WARNING level and will be very noisy in production for broadcast joins (especially because `exist()` is called multiple times). Recommend gating behind a debug flag (e.g., VLOG / conditional config), lowering to INFO/DEBUG, and/or using rate-limited logging (e.g., `LOG_EVERY_N`) to avoid flooding logs and impacting operational observability. ########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.h: ########## @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <folly/Executor.h> + +#include "operators/hashjoin/BroadcastHashTable.h" +#include "velox/core/PlanNode.h" Review Comment: This header uses `std::vector`, `std::string`, `std::unordered_map`, and `std::shared_ptr` in the public API but does not include the corresponding standard headers. Relying on transitive includes is brittle and can break with unrelated include-order changes. Add explicit includes (e.g., `<memory>`, `<string>`, `<unordered_map>`, `<vector>`). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
