Skip to content
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datafusion/optimizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ chrono = { workspace = true }
datafusion-common = { workspace = true, default-features = true }
datafusion-expr = { workspace = true }
datafusion-expr-common = { workspace = true }
datafusion-functions = { workspace = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

Please let's not add this dependency -- doing so makes it harder for others to extend the datafusion optimizer and functions as the optimizer now assumes the built in functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it, i guess if we were to do this optimization, we'd implement the rewrite inside of fn simplify() { } inside of regexpmatch.rs (or maybe in the like implementation). I'll look into it

datafusion-physical-expr = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
Expand Down
90 changes: 69 additions & 21 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,17 +1660,19 @@ impl TreeNodeRewriter for Simplifier<'_> {
left,
op: op @ (RegexMatch | RegexNotMatch | RegexIMatch | RegexNotIMatch),
right,
}) => Transformed::yes(simplify_regex_expr(left, op, right)?),
}) => simplify_regex_expr(left, op, right)?,

// Rules for Like
Expr::Like(like) => {
// `\` is implicit escape, see https://github.com/apache/datafusion/issues/13291
let escape_char = like.escape_char.unwrap_or('\\');
match as_string_scalar(&like.pattern) {
Some((data_type, pattern_str)) => {

match StringScalar::try_from_expr(&like.pattern) {
Some(string_scalar) => {
let pattern_str = string_scalar.as_str();
match pattern_str {
None => return Ok(Transformed::yes(lit_bool_null())),
Some(pattern_str) if pattern_str == "%" => {
Some("%") => {
// exp LIKE '%' is
// - when exp is not NULL, it's true
// - when exp is NULL, it's NULL
Expand Down Expand Up @@ -1702,10 +1704,9 @@ impl TreeNodeRewriter for Simplifier<'_> {
.replace_all(pattern_str, "%")
.to_string();
Transformed::yes(Expr::Like(Like {
pattern: Box::new(to_string_scalar(
&data_type,
Some(simplified_pattern),
)),
pattern: Box::new(
string_scalar.to_expr(&simplified_pattern),
),
..like
}))
}
Expand Down Expand Up @@ -2126,21 +2127,54 @@ fn is_literal_or_literal_cast(expr: &Expr) -> bool {
}
}

fn as_string_scalar(expr: &Expr) -> Option<(DataType, &Option<String>)> {
match expr {
Expr::Literal(ScalarValue::Utf8(s), _) => Some((DataType::Utf8, s)),
Expr::Literal(ScalarValue::LargeUtf8(s), _) => Some((DataType::LargeUtf8, s)),
Expr::Literal(ScalarValue::Utf8View(s), _) => Some((DataType::Utf8View, s)),
_ => None,
}
/// Helper for working with string scalar values (Utf8, LargeUtf8, Utf8View)
pub(crate) enum StringScalar<'a> {
Utf8(&'a ScalarValue),
LargeUtf8(&'a ScalarValue),
Utf8View(&'a ScalarValue),
}

fn to_string_scalar(data_type: &DataType, value: Option<String>) -> Expr {
match data_type {
DataType::Utf8 => Expr::Literal(ScalarValue::Utf8(value), None),
DataType::LargeUtf8 => Expr::Literal(ScalarValue::LargeUtf8(value), None),
DataType::Utf8View => Expr::Literal(ScalarValue::Utf8View(value), None),
_ => unreachable!(),
impl<'a> StringScalar<'a> {
/// Create a `StringScalar` view from an `Expr` if it is a supported string literal.
/// Returns `None` if the expression is not a string literal.
pub(crate) fn try_from_expr(expr: &'a Expr) -> Option<Self> {
match expr {
Expr::Literal(scalar, _) => Self::try_from_scalar(scalar),
_ => None,
}
}

/// Create a `StringScalar` view from a `ScalarValue` if it is a supported string type.
/// Returns `None` if the scalar value is not a supported string type.
fn try_from_scalar(scalar: &'a ScalarValue) -> Option<Self> {
match scalar {
ScalarValue::Utf8(_) => Some(Self::Utf8(scalar)),
ScalarValue::LargeUtf8(_) => Some(Self::LargeUtf8(scalar)),
ScalarValue::Utf8View(_) => Some(Self::Utf8View(scalar)),
_ => None,
}
}

/// Returns the underlying string slice.
pub(crate) fn as_str(&self) -> Option<&'a str> {
match self {
Self::Utf8(scalar) | Self::LargeUtf8(scalar) | Self::Utf8View(scalar) => {
scalar.try_as_str().flatten()
}
}
}

/// Build a new `Expr` of the same string type with the given value.
pub(crate) fn to_expr(&self, val: &str) -> Expr {
match self {
Self::Utf8(_) => Expr::Literal(ScalarValue::Utf8(Some(val.to_owned())), None),
Self::LargeUtf8(_) => {
Expr::Literal(ScalarValue::LargeUtf8(Some(val.to_owned())), None)
}
Self::Utf8View(_) => {
Expr::Literal(ScalarValue::Utf8View(Some(val.to_owned())), None)
}
}
}
}

Expand Down Expand Up @@ -2332,6 +2366,7 @@ mod tests {
interval_arithmetic::Interval,
*,
};
use datafusion_functions::expr_fn::contains as contains_fn;
use datafusion_functions_window_common::field::WindowUDFFieldArgs;
use datafusion_functions_window_common::partition::PartitionEvaluatorArgs;
use datafusion_physical_expr::PhysicalExpr;
Expand Down Expand Up @@ -3347,6 +3382,19 @@ mod tests {
col("c1").like(lit("%foo%")),
);

// regular expression that matches a substring
assert_change(
regex_match(col("c1"), lit(".*foo.*")),
contains_fn(col("c1"), lit("foo")),
);

assert_change(
regex_not_match(col("c1"), lit(".*foo.*")),
Expr::Not(Box::new(contains_fn(col("c1"), lit("foo")))),
);

assert_change(regex_match(col("c1"), lit(".*.*")), col("c1").is_not_null());

// regular expressions that match an exact literal
assert_change(regex_match(col("c1"), lit("^$")), col("c1").eq(lit("")));
assert_change(
Expand Down
Loading
Loading