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

Simplify EXPR LIKE 'constant' to expr = 'constant' #13061

Closed
wants to merge 5 commits into from
Closed
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
58 changes: 44 additions & 14 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use datafusion_common::{
use datafusion_common::{internal_err, DFSchema, DataFusionError, Result, ScalarValue};
use datafusion_expr::simplify::ExprSimplifyResult;
use datafusion_expr::{
and, lit, or, BinaryExpr, Case, ColumnarValue, Expr, Like, Operator, Volatility,
and, lit, or, BinaryExpr, Case, ColumnarValue, Expr, Operator, Volatility,
WindowFunctionDefinition,
};
use datafusion_expr::{expr::ScalarFunction, interval_arithmetic::NullableInterval};
Expand Down Expand Up @@ -1470,19 +1470,29 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}) => Transformed::yes(simplify_regex_expr(left, op, right)?),

// Rules for Like
Expr::Like(Like {
expr,
pattern,
negated,
escape_char: _,
case_insensitive: _,
}) if !is_null(&expr)
&& matches!(
pattern.as_ref(),
Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) if pattern_str == "%"
) =>
{
Transformed::yes(lit(!negated))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this is correct, but requires updating some test cases.
i am doing this in #13259

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, I'll wait for your work to be merged then continue here

Expr::Like(ref like_expr) => {
if let Expr::Literal(ScalarValue::Utf8(Some(pattern_str))) =
like_expr.pattern.as_ref()
{
if !pattern_str.contains(['%', '_'].as_ref())
&& like_expr.escape_char.is_none()
{
// If the pattern does not contain any wildcards, we can simplify the like expression to an equality expression

// TODO: handle escape characters
// These currently aren't anywhere else in DataFusion

Transformed::yes(Expr::BinaryExpr(BinaryExpr {
left: like_expr.expr.clone(),
op: if like_expr.negated { NotEq } else { Eq },
right: like_expr.pattern.clone(),
}))
} else {
Transformed::no(expr)
}
} else {
Transformed::no(expr)
}
}

// a is not null/unknown --> true (if a is not nullable)
Expand Down Expand Up @@ -3632,6 +3642,26 @@ mod tests {

let expr = not_ilike(null, "%");
assert_eq!(simplify(expr), lit_bool_null());

// test cases that get converted to equality
let expr = like(col("c1"), "a");
assert_eq!(simplify(expr), col("c1").eq(lit("a")));
let expr = not_like(col("c1"), "a");
assert_eq!(simplify(expr), col("c1").not_eq(lit("a")));
let expr = like(col("c1"), "a_");
assert_eq!(simplify(expr), col("c1").like(lit("a_")));
let expr = not_like(col("c1"), "a_");
assert_eq!(simplify(expr), col("c1").not_like(lit("a_")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test cases where pattern is constant NULL (it won't be simplified today, that's fine)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 6c847fa


// null pattern
let expr = Expr::Like(Like {
negated: false,
expr: Box::new(col("c1")),
pattern: Box::new(Expr::Literal(ScalarValue::Utf8(None))),
escape_char: None,
case_insensitive: false,
});
assert_eq!(simplify(expr.clone()), expr);
}

#[test]
Expand Down
Loading