Skip to content

Commit 0d762d0

Browse files
committed
Add runtime filters only for supported join algorithms (i.e. hash joins)
1 parent 11c7f7a commit 0d762d0

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/Processors/QueryPlan/JoinStepLogical.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class JoinStepLogical final : public IQueryPlanStep
8484

8585
const SortingStep::Settings & getSortingSettings() const { return sorting_settings; }
8686
const JoinSettings & getJoinSettings() const { return join_settings; }
87+
JoinSettings & getJoinSettings() { return join_settings; }
8788
const JoinOperator & getJoinOperator() const { return join_operator; }
8889
JoinOperator & getJoinOperator() { return join_operator; }
8990

src/Processors/QueryPlan/Optimizations/joinRuntimeFilter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ const ActionsDAG::Node & createRuntimeFilterCondition(ActionsDAG & actions_dag,
4444
return condition;
4545
}
4646

47+
static bool supportsRuntimeFilter(JoinAlgorithm join_algorithm)
48+
{
49+
/// Runtime filter can only be applied to join algorithms that first read the right side and only after that read the left side.
50+
return
51+
join_algorithm == JoinAlgorithm::HASH ||
52+
join_algorithm == JoinAlgorithm::PARALLEL_HASH;
53+
}
54+
4755
bool tryAddJoinRuntimeFilter(QueryPlan::Node & node, QueryPlan::Nodes & nodes, const QueryPlanOptimizationSettings & optimization_settings)
4856
{
4957
/// Is this a join step?
@@ -57,12 +65,14 @@ bool tryAddJoinRuntimeFilter(QueryPlan::Node & node, QueryPlan::Nodes & nodes, c
5765

5866
/// Check if join can do runtime filtering on left table
5967
const auto & join_operator = join_step->getJoinOperator();
68+
auto & join_algorithms = join_step->getJoinSettings().join_algorithms;
6069
const bool can_use_runtime_filter =
6170
(
6271
(join_operator.kind == JoinKind::Inner && (join_operator.strictness == JoinStrictness::All || join_operator.strictness == JoinStrictness::Any)) ||
6372
((join_operator.kind == JoinKind::Left || join_operator.kind == JoinKind::Right) && join_operator.strictness == JoinStrictness::Semi)
6473
) &&
65-
(join_operator.locality == JoinLocality::Unspecified || join_operator.locality == JoinLocality::Local);
74+
(join_operator.locality == JoinLocality::Unspecified || join_operator.locality == JoinLocality::Local) &&
75+
std::find_if(join_algorithms.begin(), join_algorithms.end(), supportsRuntimeFilter) != join_algorithms.end();
6676

6777
if (!can_use_runtime_filter)
6878
return false;
@@ -183,6 +193,9 @@ bool tryAddJoinRuntimeFilter(QueryPlan::Node & node, QueryPlan::Nodes & nodes, c
183193

184194
node.children = {apply_filter_node, build_filter_node};
185195

196+
/// Remove algorithms that are not compatible with runtime filters
197+
std::erase_if(join_algorithms, [](auto join_algorithm) { return !supportsRuntimeFilter(join_algorithm); });
198+
186199
return true;
187200
}
188201

tests/queries/0_stateless/03580_join_runtime_filter.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ INSERT INTO orders SELECT number, number/10000, number%1000 FROM numbers(1000000
1515
SET enable_analyzer=1;
1616
SET enable_join_runtime_filters=1;
1717
SET enable_parallel_replicas=0;
18+
SET join_algorithm = 'hash,parallel_hash';
1819

1920
SELECT avg(o_totalprice)
2021
FROM orders, customer, nation

tests/queries/0_stateless/03580_join_runtime_filter_prewhere.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ INSERT INTO customer SELECT number, 5 FROM numbers(500);
77

88
SET enable_analyzer=1;
99
SET enable_parallel_replicas=0;
10+
SET join_algorithm = 'hash,parallel_hash';
1011

1112
SELECT '-- Check that filter on c_nationkey is moved to PREWHERE';
1213
SELECT REGEXP_REPLACE(trimLeft(explain), '_runtime_filter_\\d+', '_runtime_filter_UNIQ_ID')

0 commit comments

Comments
 (0)