Copilot commented on code in PR #13216: URL: https://github.com/apache/gluten/pull/13216#discussion_r4561969906
########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,253 @@ +/* + * 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: The `VectorHasher` is constructed with `channel = i` (the join-key ordinal), but the hasher channel is expected to be the *column index* in the input row vector. If join keys are not the first N columns in `spec.buildType` (common case), this creates hashers that reference the wrong build-side channels, which can break probing/serialization assumptions even for the ‘empty table’ fallback. Use the actual child index for each `joinKeys[i]` in `spec.buildType` when constructing the hasher (or use the same hasher factory logic Velox uses for join builds). ########## cpp/velox/jni/JniHashTable.cc: ########## @@ -123,48 +127,37 @@ 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 new de-dup logic relies on `std::find` (and turns the previous sort/unique approach into an O(n^2) loop). Two concrete improvements: (1) ensure `<algorithm>` is directly included in this translation unit to avoid relying on transitive includes for `std::find`; (2) consider preserving the previous `sort` + `unique` pattern or using a set to dedupe to keep the behavior scalable and simpler to audit. ########## cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc: ########## @@ -0,0 +1,253 @@ +/* + * 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])); Review Comment: The placeholder filter construction is hard to reason about because it progressively turns a boolean expression into VARCHAR (`toVarchar(filter)`) and then compares it to a (casted) column. Even though the filter is ‘never evaluated’, it is still parsed/compiled by Velox, and this cross-type chaining makes the intent less obvious and increases the chance of hitting type-resolution/cast edge cases. A more straightforward (and clearer) approach is to build a boolean expression that references each `filterBuildColumns[i]` independently (e.g., a conjunction of per-column self-equality checks) without mixing boolean→varchar casts between steps. -- 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]
