Skip to content
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

feat: Support dynamic filters for Right Join #12057

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions velox/exec/HashProbe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ void HashProbe::asyncWaitForHashTable() {
} else if (
(isInnerJoin(joinType_) || isLeftSemiFilterJoin(joinType_) ||
isRightSemiFilterJoin(joinType_) ||
(isRightSemiProjectJoin(joinType_) && !nullAware_)) &&
(isRightSemiProjectJoin(joinType_) && !nullAware_) ||
isRightJoin(joinType_)) &&
table_->hashMode() != BaseHashTable::HashMode::kHash && !isSpillInput() &&
!hasMoreSpillData()) {
// Find out whether there are any upstream operators that can accept dynamic
Expand Down Expand Up @@ -632,7 +633,8 @@ void HashProbe::clearDynamicFilters() {
// * hash table has a single key with unique values,
// * build side has no dependent columns.
if (keyChannels_.size() == 1 && !table_->hasDuplicateKeys() &&
tableOutputProjections_.empty() && !filter_ && !dynamicFilters_.empty()) {
tableOutputProjections_.empty() && !filter_ && !dynamicFilters_.empty() &&
!isRightJoin(joinType_)) {
canReplaceWithDynamicFilter_ = true;
}

Expand Down
82 changes: 82 additions & 0 deletions velox/exec/tests/HashJoinTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4453,6 +4453,48 @@ TEST_F(HashJoinTest, dynamicFilters) {
})
.run();
}

// Right join.
op = PlanBuilder(planNodeIdGenerator, pool_.get())
.tableScan(probeType)
.capturePlanNodeId(probeScanId)
.hashJoin(
{"c0"},
{"u_c0"},
buildSide,
"",
{"c0", "c1", "u_c1"},
core::JoinType::kRight)
.capturePlanNodeId(joinId)
.project({"c0", "c1 + 1", "c1 + u_c1"})
.planNode();
{
HashJoinBuilder(*pool_, duckDbQueryRunner_, driverExecutor_.get())
.planNode(std::move(op))
.makeInputSplits(makeInputSplits(probeScanId))
.referenceQuery(
"SELECT t.c0, t.c1 + 1, t.c1 + u.c1 FROM t RIGHT JOIN u ON t.c0 = u.c0")
.verifier([&](const std::shared_ptr<Task>& task, bool hasSpill) {
SCOPED_TRACE(fmt::format("hasSpill:{}", hasSpill));
auto planStats = toPlanStats(task->taskStats());
if (hasSpill) {
// Dynamic filtering should be disabled with spilling triggered.
ASSERT_EQ(0, getFiltersProduced(task, 1).sum);
ASSERT_EQ(0, getFiltersAccepted(task, 0).sum);
ASSERT_EQ(getInputPositions(task, 1), numRowsProbe * numSplits);
ASSERT_TRUE(planStats.at(probeScanId).dynamicFilterStats.empty());
} else {
ASSERT_EQ(1, getFiltersProduced(task, 1).sum);
ASSERT_EQ(1, getFiltersAccepted(task, 0).sum);
ASSERT_EQ(0, getReplacedWithFilterRows(task, 1).sum);
ASSERT_LT(getInputPositions(task, 1), numRowsProbe * numSplits);
ASSERT_EQ(
planStats.at(probeScanId).dynamicFilterStats.producerNodeIds,
std::unordered_set<core::PlanNodeId>({joinId}));
}
})
.run();
}
}

// Basic push-down with column names projected out of the table scan
Expand Down Expand Up @@ -4798,6 +4840,46 @@ TEST_F(HashJoinTest, dynamicFilters) {
})
.run();
}

// Right join.
op =
PlanBuilder(planNodeIdGenerator, pool_.get())
.tableScan(probeType, {"c0 < 200::INTEGER"})
.capturePlanNodeId(probeScanId)
.hashJoin(
{"c0"}, {"u_c0"}, buildSide, "", {"c1"}, core::JoinType::kRight)
.capturePlanNodeId(joinId)
.project({"c1 + 1"})
.planNode();

{
HashJoinBuilder(*pool_, duckDbQueryRunner_, driverExecutor_.get())
.planNode(std::move(op))
.makeInputSplits(makeInputSplits(probeScanId))
.referenceQuery(
"SELECT t.c1 + 1 FROM (SELECT * FROM t WHERE t.c0 < 200) t RIGHT JOIN u ON t.c0 = u.c0")
.verifier([&](const std::shared_ptr<Task>& task, bool hasSpill) {
SCOPED_TRACE(fmt::format("hasSpill:{}", hasSpill));
auto planStats = toPlanStats(task->taskStats());
if (hasSpill) {
// Dynamic filtering should be disabled with spilling triggered.
ASSERT_EQ(0, getFiltersProduced(task, 1).sum);
ASSERT_EQ(0, getFiltersAccepted(task, 0).sum);
ASSERT_EQ(getReplacedWithFilterRows(task, 1).sum, 0);
ASSERT_LT(getInputPositions(task, 1), numRowsProbe * numSplits);
ASSERT_TRUE(planStats.at(probeScanId).dynamicFilterStats.empty());
} else {
ASSERT_EQ(1, getFiltersProduced(task, 1).sum);
ASSERT_EQ(1, getFiltersAccepted(task, 0).sum);
ASSERT_EQ(getReplacedWithFilterRows(task, 1).sum, 0);
ASSERT_LT(getInputPositions(task, 1), numRowsProbe * numSplits);
ASSERT_EQ(
planStats.at(probeScanId).dynamicFilterStats.producerNodeIds,
std::unordered_set<core::PlanNodeId>({joinId}));
}
})
.run();
}
}

// Disable filter push-down by using values in place of scan.
Expand Down
Loading