Skip to content

Commit

Permalink
Move NOT(comparison) optimization from plan_joinref to the parser, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mytherin committed Apr 11, 2020
1 parent 331cce4 commit d88e21d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/parser/transform/expression/transform_bool_expr.cpp
Expand Up @@ -32,6 +32,12 @@ unique_ptr<ParsedExpression> Transformer::TransformBoolExpr(PGBoolExpr *root) {
// convert COMPARE_IN to COMPARE_NOT_IN
next->type = ExpressionType::COMPARE_NOT_IN;
result = move(next);
} else if (next->type >= ExpressionType::COMPARE_EQUAL &&
next->type <= ExpressionType::COMPARE_GREATERTHANOREQUALTO) {
// NOT on a comparison: we can negate the comparison
// e.g. NOT(x > y) is equivalent to x <= y
next->type = NegateComparisionExpression(next->type);
result = move(next);
} else {
result = make_unique<OperatorExpression>(ExpressionType::OPERATOR_NOT, move(next));
}
Expand Down
22 changes: 0 additions & 22 deletions src/planner/binder/tableref/plan_joinref.cpp
Expand Up @@ -74,28 +74,6 @@ unique_ptr<LogicalOperator> LogicalComparisonJoin::CreateJoin(JoinType type, uni
// successfully created the join condition
continue;
}
} else if (expr->type == ExpressionType::OPERATOR_NOT) {
auto &not_expr = (BoundOperatorExpression &)*expr;
assert(not_expr.children.size() == 1);
ExpressionType child_type = not_expr.children[0]->GetExpressionType();
// the condition is ON NOT (EXPRESSION)
// we can transform this to remove the NOT if the child is a Comparison
// e.g.:
// ON NOT (X = 3) can be turned into ON (X <> 3)
// ON NOT (X > 3) can be turned into ON (X <= 3)
// for non-comparison operators here we just push the filter
if (child_type >= ExpressionType::COMPARE_EQUAL &&
child_type <= ExpressionType::COMPARE_GREATERTHANOREQUALTO) {
// switcheroo the child condition
// our join needs to compare explicit left and right sides. So we
// invert the condition to express NOT, this way we can still use
// equi-joins
not_expr.children[0]->type = NegateComparisionExpression(child_type);
if (CreateJoinCondition(*not_expr.children[0], left_bindings, right_bindings, conditions)) {
// successfully created the join condition
continue;
}
}
}
arbitrary_expressions.push_back(move(expr));
}
Expand Down

0 comments on commit d88e21d

Please sign in to comment.