Skip to content

Commit

Permalink
Add and update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tai Le Manh <[email protected]>
  • Loading branch information
tlm365 committed Sep 5, 2024
1 parent 1dd639d commit 603e860
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 28 deletions.
52 changes: 32 additions & 20 deletions datafusion/optimizer/src/unwrap_cast_in_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,26 @@ impl TreeNodeRewriter for UnwrapCastExprRewriter {
expr: right_expr, ..
}),
) => {
// if the left_lit_value can be casted to the type of expr
// if the left_lit_value can be cast to the type of expr
// we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
let Ok(expr_type) = right_expr.get_type(&self.schema) else {
return Ok(Transformed::no(expr));
};
let Some(value) =
try_cast_literal_to_type(left_lit_value, &expr_type)
else {
return Ok(Transformed::no(expr));
};
**left = lit(value);
// unwrap the cast/try_cast for the right expr
**right = mem::take(right_expr);
Ok(Transformed::yes(expr))
match expr_type {
// https://github.com/apache/datafusion/issues/12180
DataType::Utf8View => Ok(Transformed::no(expr)),
_ => {
let Some(value) =
try_cast_literal_to_type(left_lit_value, &expr_type)
else {
return Ok(Transformed::no(expr));
};
**left = lit(value);
// unwrap the cast/try_cast for the right expr
**right = mem::take(right_expr);
Ok(Transformed::yes(expr))
}
}
}
(
Expr::TryCast(TryCast {
Expand All @@ -183,20 +189,26 @@ impl TreeNodeRewriter for UnwrapCastExprRewriter {
}),
Expr::Literal(right_lit_value),
) => {
// if the right_lit_value can be casted to the type of expr
// if the right_lit_value can be cast to the type of expr
// we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
let Ok(expr_type) = left_expr.get_type(&self.schema) else {
return Ok(Transformed::no(expr));
};
let Some(value) =
try_cast_literal_to_type(right_lit_value, &expr_type)
else {
return Ok(Transformed::no(expr));
};
// unwrap the cast/try_cast for the left expr
**left = mem::take(left_expr);
**right = lit(value);
Ok(Transformed::yes(expr))
match expr_type {
// https://github.com/apache/datafusion/issues/12180
DataType::Utf8View => Ok(Transformed::no(expr)),
_ => {
let Some(value) =
try_cast_literal_to_type(right_lit_value, &expr_type)
else {
return Ok(Transformed::no(expr));
};
// unwrap the cast/try_cast for the left expr
**left = mem::take(left_expr);
**right = lit(value);
Ok(Transformed::yes(expr))
}
}
}
_ => Ok(Transformed::no(expr)),
}
Expand Down
56 changes: 48 additions & 8 deletions datafusion/sqllogictest/test_files/string_view.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1226,43 +1226,83 @@ NULL NULL NULL
# `~` operator (regex match)
query TT
EXPLAIN SELECT
column1_utf8view ~ 'foo' AS c1
column1_utf8view ~ 'an' AS c1
FROM test;
----
logical_plan
01)Projection: CAST(test.column1_utf8view AS Utf8) LIKE Utf8("%foo%") AS c1
01)Projection: CAST(test.column1_utf8view AS Utf8) LIKE Utf8("%an%") AS c1
02)--TableScan: test projection=[column1_utf8view]

query B
SELECT
column1_utf8view ~ 'an' AS c1
FROM test;
----
false
true
false
NULL

# `~*` operator (regex match case-insensitive)
query TT
EXPLAIN SELECT
column1_utf8view ~* 'foo' AS c1
column1_utf8view ~* '^a.{3}e' AS c1
FROM test;
----
logical_plan
01)Projection: CAST(test.column1_utf8view AS Utf8) ILIKE Utf8("%foo%") AS c1
01)Projection: CAST(test.column1_utf8view AS Utf8) ~* Utf8("^a.{3}e") AS c1
02)--TableScan: test projection=[column1_utf8view]

query B
SELECT
column1_utf8view ~* '^a.{3}e' AS c1
FROM test;
----
true
false
false
NULL

# `!~~` operator (not like match)
query TT
EXPLAIN SELECT
column1_utf8view !~~ 'an' AS c1
column1_utf8view !~~ 'xia_g%g' AS c1
FROM test;
----
logical_plan
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~ Utf8("an") AS c1
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~ Utf8("xia_g%g") AS c1
02)--TableScan: test projection=[column1_utf8view]

query B
SELECT
column1_utf8view !~~ 'xia_g%g' AS c1
FROM test;
----
true
true
true
NULL

# `!~~*` operator (not like match case-insensitive)
query TT
EXPLAIN SELECT
column1_utf8view !~~* 'an' AS c1
column1_utf8view !~~* 'xia_g%g' AS c1
FROM test;
----
logical_plan
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~* Utf8("an") AS c1
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~* Utf8("xia_g%g") AS c1
02)--TableScan: test projection=[column1_utf8view]

query B
SELECT
column1_utf8view !~~* 'xia_g%g' AS c1
FROM test;
----
true
false
true
NULL

statement ok
drop table test;

Expand Down

0 comments on commit 603e860

Please sign in to comment.