Copilot commented on code in PR #13216:
URL: https://github.com/apache/gluten/pull/13216#discussion_r4269518745


##########
cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc:
##########
@@ -0,0 +1,262 @@
+/*
+ * 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 output is 
never
+// produced; it only has to be valid for the join type.
+RowTypePtr fakeJoinOutputType(core::JoinType joinType, const RowTypePtr& 
probeType, const RowTypePtr& buildType) {
+  const auto& firstBuildName = buildType->nameOf(0);
+  const auto& firstBuildType = buildType->childAt(0);
+  switch (joinType) {
+    case core::JoinType::kLeftSemiProject: {
+      auto names = probeType->names();
+      auto types = probeType->children();
+      names.emplace_back("__fake_match");
+      types.emplace_back(BOOLEAN());
+      return ROW(std::move(names), std::move(types));
+    }
+    case core::JoinType::kRightSemiFilter:
+      return ROW({firstBuildName}, {firstBuildType});
+    case core::JoinType::kRightSemiProject:
+      return ROW({firstBuildName, "__fake_match"}, {firstBuildType, 
BOOLEAN()});
+    default:
+      return probeType;
+  }
+}
+
+// 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

Review Comment:
   The placeholder filter relies on casting arbitrary build columns (and 
intermediate boolean expressions) to VARCHAR and comparing via `equalto`. This 
can fail during expression compilation if a build column type (or 
BOOLEAN->VARCHAR cast) isn’t supported, even though the probe side is empty and 
the filter is never evaluated. Consider constructing a placeholder that only 
uses universally-supported null checks (e.g., a boolean expression that returns 
NULL when any referenced build column is NULL) without requiring casts/equality 
on the build column types.



##########
cpp/velox/operators/hashjoin/HashTableTaskBuilder.cc:
##########
@@ -0,0 +1,262 @@
+/*
+ * 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 output is 
never
+// produced; it only has to be valid for the join type.
+RowTypePtr fakeJoinOutputType(core::JoinType joinType, const RowTypePtr& 
probeType, const RowTypePtr& buildType) {
+  const auto& firstBuildName = buildType->nameOf(0);
+  const auto& firstBuildType = buildType->childAt(0);
+  switch (joinType) {
+    case core::JoinType::kLeftSemiProject: {
+      auto names = probeType->names();
+      auto types = probeType->children();
+      names.emplace_back("__fake_match");
+      types.emplace_back(BOOLEAN());
+      return ROW(std::move(names), std::move(types));
+    }
+    case core::JoinType::kRightSemiFilter:
+      return ROW({firstBuildName}, {firstBuildType});
+    case core::JoinType::kRightSemiProject:
+      return ROW({firstBuildName, "__fake_match"}, {firstBuildType, 
BOOLEAN()});
+    default:
+      return probeType;
+  }
+}
+
+// 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;
+}
+
+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<HashTableBuilder> 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();
+  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()));
+  }

Review Comment:
   The comment says the build input is ‘fanned out’ to all drivers, but 
`LocalPartitionNode::Type::kRepartition` with `RoundRobinPartitionFunctionSpec` 
partitions the rows across drivers (it doesn’t duplicate/broadcast them). 
Please adjust the comment to reflect the actual behavior 
(partitioning/distribution) to avoid misleading future maintainers.



-- 
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]

Reply via email to