Copilot commented on code in PR #13216: URL: https://github.com/apache/gluten/pull/13216#discussion_r4312928794
########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,248 @@ +/* + * 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)); + } + auto table = exec::HashTable<false>::createForJoin(std::move(hashers), {}, true, false, false, 1'000, pool); + table->prepareJoinTable({}, exec::BaseHashTable::kNoSpillInputStartPartitionBit, 1'000'000, false, nullptr); + return table; +} + Review Comment: `makeEmptyJoinTable()` hard-codes both the hasher channel indices (`i`) and the hash table template (`HashTable<false>`). The hasher channel should correspond to the actual build-side column index (e.g., `buildType->getChildIdxIfExists(joinKey)`), not the ordinal position in `joinKeys`. Also, using `HashTable<false>` can change the “ignore null keys” behavior observed by downstream code (e.g., JNI `getIgnoreNullKeys()` uses a `dynamic_cast<HashTable<true>>`). Even if this table is only used as a fallback, it can still be serialized/broadcast or consulted for flags, so the shape/type should match what Velox `HashBuild` would have produced for this join/spec. Update the fallback to (1) build hashers with correct input channels, and (2) pick `HashTable<true>` vs `HashTable<false>` consistently with the join’s null-key handling (and ideally align any other flags that affect row layout/serialization like allowDuplicates / nextOffset). ########## cpp/velox/jni/JniHashTable.cc: ########## @@ -116,139 +120,87 @@ std::shared_ptr<HashTableBuilder> nativeHashTableBuild( VELOX_NYI("Unsupported Join type: {}", std::to_string(sJoin)); } - std::vector<std::shared_ptr<const facebook::velox::core::FieldAccessTypedExpr>> joinKeyTypes; - joinKeyTypes.reserve(joinKeys.size()); - for (const auto& name : joinKeys) { - joinKeyTypes.emplace_back( - std::make_shared<facebook::velox::core::FieldAccessTypedExpr>(rowType->findChild(name), name)); - } - - std::vector<uint32_t> filterInputChannels; - filterInputChannels.reserve(filterBuildColumns.size()); + using facebook::velox::core::QueryConfig; + HashTableTaskBuildSpec spec; + spec.joinType = vJoin; + spec.nullAware = isNullAwareAntiJoin; + spec.withFilter = hasMixedJoinCondition; for (const auto& name : filterBuildColumns) { - if (const auto idx = rowType->getChildIdxIfExists(name)) { - filterInputChannels.push_back(*idx); + if (rowType->containsChild(name) && + std::find(spec.filterBuildColumns.begin(), spec.filterBuildColumns.end(), name) == + spec.filterBuildColumns.end()) { + spec.filterBuildColumns.push_back(name); } } Review Comment: This de-duplicates `filterBuildColumns` using a linear `std::find` on a growing vector, making it O(n²). If `filterBuildColumns` can be large (or this is called frequently), consider using a `folly::F14FastSet`/`std::unordered_set` for membership checks while preserving insertion order (e.g., keep the vector for order but guard pushes with a set). ########## cpp/velox/jni/JniHashTable.cc: ########## @@ -116,139 +120,87 @@ std::shared_ptr<HashTableBuilder> nativeHashTableBuild( VELOX_NYI("Unsupported Join type: {}", std::to_string(sJoin)); } - std::vector<std::shared_ptr<const facebook::velox::core::FieldAccessTypedExpr>> joinKeyTypes; - joinKeyTypes.reserve(joinKeys.size()); - for (const auto& name : joinKeys) { - joinKeyTypes.emplace_back( - std::make_shared<facebook::velox::core::FieldAccessTypedExpr>(rowType->findChild(name), name)); - } - - std::vector<uint32_t> filterInputChannels; - filterInputChannels.reserve(filterBuildColumns.size()); + using facebook::velox::core::QueryConfig; + HashTableTaskBuildSpec spec; + spec.joinType = vJoin; + spec.nullAware = isNullAwareAntiJoin; + spec.withFilter = hasMixedJoinCondition; for (const auto& name : filterBuildColumns) { - if (const auto idx = rowType->getChildIdxIfExists(name)) { - filterInputChannels.push_back(*idx); + if (rowType->containsChild(name) && + std::find(spec.filterBuildColumns.begin(), spec.filterBuildColumns.end(), name) == + spec.filterBuildColumns.end()) { + spec.filterBuildColumns.push_back(name); } } - std::sort(filterInputChannels.begin(), filterInputChannels.end()); - filterInputChannels.erase( - std::unique(filterInputChannels.begin(), filterInputChannels.end()), filterInputChannels.end()); - - auto hashTableBuilder = std::make_shared<HashTableBuilder>( - vJoin, - isNullAwareAntiJoin, - hasMixedJoinCondition, - bloomFilterPushdownSize, - joinKeyTypes, - filterInputChannels, - filterPropagatesNulls, - rowType, - memoryPool.get(), - minTableRowsForParallelJoinBuild, - joinBuildVectorHasherMaxNumDistinct, - abandonHashBuildDedupMinRows, - abandonHashBuildDedupMinPct); - - for (auto i = 0; i < batches.size(); i++) { - auto rowVector = VeloxColumnarBatch::from(memoryPool.get(), batches[i])->getRowVector(); - hashTableBuilder->addInput(rowVector); - if (hashTableBuilder->noMoreInput()) { - break; - } + spec.filterPropagatesNulls = filterPropagatesNulls; + spec.buildType = rowType; + spec.joinKeys = joinKeys; + spec.queryConfigs = { + {QueryConfig::kMinTableRowsForParallelJoinBuild, std::to_string(minTableRowsForParallelJoinBuild)}, + {QueryConfig::kJoinBuildVectorHasherMaxNumDistinct, std::to_string(joinBuildVectorHasherMaxNumDistinct)}, + {QueryConfig::kAbandonDedupHashMapMinRows, std::to_string(abandonHashBuildDedupMinRows)}, + {QueryConfig::kAbandonDedupHashMapMinPct, std::to_string(abandonHashBuildDedupMinPct)}, + {QueryConfig::kHashProbeBloomFilterPushdownMaxSize, + std::to_string(std::max<int64_t>(bloomFilterPushdownSize, 0))}, Review Comment: The new path clamps `bloomFilterPushdownSize` to `>= 0` before passing it via `QueryConfig`. If callers previously used a negative value (e.g., `-1`) with a special meaning (commonly “disabled”), this changes semantics to `0`, which might not be interpreted the same way by Velox config parsing. To preserve behavior, pass the value through unchanged (or map it using the same rule Velox expects for this specific config). -- 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]
