Skip to content

Commit

Permalink
Issue duckdb#9396: AsOf Inequality Optimisation
Browse files Browse the repository at this point in the history
Don't remove the inequality condition for AsOf joins
even if the optimiser can tell it is always true.

fixes: duckdb#9396
fixes: duckdblabs/duckdb-internal#517
  • Loading branch information
hawkfish committed Oct 23, 2023
1 parent 44d7f80 commit 05dd11b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/optimizer/statistics/operator/propagate_join.cpp
Expand Up @@ -67,6 +67,21 @@ void StatisticsPropagator::PropagateStatistics(LogicalComparisonJoin &join, uniq
break;
case FilterPropagateResult::FILTER_ALWAYS_TRUE:
// filter is always true
// If this is the inequality for an AsOf join,
// then we must leave it in because it also flags
// the semantics of restricting to a single match
// so we can't replace it with an equi-join on the remaining conditions.
if (join.type == LogicalOperatorType::LOGICAL_ASOF_JOIN) {
switch (condition.comparison) {
case ExpressionType::COMPARE_GREATERTHAN:
case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
case ExpressionType::COMPARE_LESSTHAN:
case ExpressionType::COMPARE_LESSTHANOREQUALTO:
continue;
default:
break;
}
}
if (join.conditions.size() > 1) {
// there are multiple conditions: erase this condition
join.conditions.erase(join.conditions.begin() + i);
Expand Down
20 changes: 20 additions & 0 deletions test/sql/join/asof/test_asof_join.test
Expand Up @@ -17,6 +17,26 @@ INSERT INTO events0 VALUES
(8, 3)
;

# Prevent optimiser from removing true inequalities
statement ok
create table prices("when" timestamp, symbol int, price int);

statement ok
insert into prices values ('2020-01-01 00:00:00', 1, 42);

statement ok
create table trades("when" timestamp, symbol int);

statement ok
insert into trades values ('2020-01-01 00:00:03', 1);

query III
SELECT t.*, p.price
FROM trades t ASOF JOIN prices p
ON t.symbol = p.symbol AND t.when >= p.when;
----
2020-01-01 00:00:03 1 42

# Use an ASOF join inside of a correlated subquery


Expand Down

0 comments on commit 05dd11b

Please sign in to comment.