Skip to content

Commit 603e860

Browse files
committed
Add and update unit tests
Signed-off-by: Tai Le Manh <[email protected]>
1 parent 1dd639d commit 603e860

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

datafusion/optimizer/src/unwrap_cast_in_comparison.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,26 @@ impl TreeNodeRewriter for UnwrapCastExprRewriter {
159159
expr: right_expr, ..
160160
}),
161161
) => {
162-
// if the left_lit_value can be casted to the type of expr
162+
// if the left_lit_value can be cast to the type of expr
163163
// we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
164164
let Ok(expr_type) = right_expr.get_type(&self.schema) else {
165165
return Ok(Transformed::no(expr));
166166
};
167-
let Some(value) =
168-
try_cast_literal_to_type(left_lit_value, &expr_type)
169-
else {
170-
return Ok(Transformed::no(expr));
171-
};
172-
**left = lit(value);
173-
// unwrap the cast/try_cast for the right expr
174-
**right = mem::take(right_expr);
175-
Ok(Transformed::yes(expr))
167+
match expr_type {
168+
// https://github.com/apache/datafusion/issues/12180
169+
DataType::Utf8View => Ok(Transformed::no(expr)),
170+
_ => {
171+
let Some(value) =
172+
try_cast_literal_to_type(left_lit_value, &expr_type)
173+
else {
174+
return Ok(Transformed::no(expr));
175+
};
176+
**left = lit(value);
177+
// unwrap the cast/try_cast for the right expr
178+
**right = mem::take(right_expr);
179+
Ok(Transformed::yes(expr))
180+
}
181+
}
176182
}
177183
(
178184
Expr::TryCast(TryCast {
@@ -183,20 +189,26 @@ impl TreeNodeRewriter for UnwrapCastExprRewriter {
183189
}),
184190
Expr::Literal(right_lit_value),
185191
) => {
186-
// if the right_lit_value can be casted to the type of expr
192+
// if the right_lit_value can be cast to the type of expr
187193
// we need to unwrap the cast for cast/try_cast expr, and add cast to the literal
188194
let Ok(expr_type) = left_expr.get_type(&self.schema) else {
189195
return Ok(Transformed::no(expr));
190196
};
191-
let Some(value) =
192-
try_cast_literal_to_type(right_lit_value, &expr_type)
193-
else {
194-
return Ok(Transformed::no(expr));
195-
};
196-
// unwrap the cast/try_cast for the left expr
197-
**left = mem::take(left_expr);
198-
**right = lit(value);
199-
Ok(Transformed::yes(expr))
197+
match expr_type {
198+
// https://github.com/apache/datafusion/issues/12180
199+
DataType::Utf8View => Ok(Transformed::no(expr)),
200+
_ => {
201+
let Some(value) =
202+
try_cast_literal_to_type(right_lit_value, &expr_type)
203+
else {
204+
return Ok(Transformed::no(expr));
205+
};
206+
// unwrap the cast/try_cast for the left expr
207+
**left = mem::take(left_expr);
208+
**right = lit(value);
209+
Ok(Transformed::yes(expr))
210+
}
211+
}
200212
}
201213
_ => Ok(Transformed::no(expr)),
202214
}

datafusion/sqllogictest/test_files/string_view.slt

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,43 +1226,83 @@ NULL NULL NULL
12261226
# `~` operator (regex match)
12271227
query TT
12281228
EXPLAIN SELECT
1229-
column1_utf8view ~ 'foo' AS c1
1229+
column1_utf8view ~ 'an' AS c1
12301230
FROM test;
12311231
----
12321232
logical_plan
1233-
01)Projection: CAST(test.column1_utf8view AS Utf8) LIKE Utf8("%foo%") AS c1
1233+
01)Projection: CAST(test.column1_utf8view AS Utf8) LIKE Utf8("%an%") AS c1
12341234
02)--TableScan: test projection=[column1_utf8view]
12351235

1236+
query B
1237+
SELECT
1238+
column1_utf8view ~ 'an' AS c1
1239+
FROM test;
1240+
----
1241+
false
1242+
true
1243+
false
1244+
NULL
1245+
12361246
# `~*` operator (regex match case-insensitive)
12371247
query TT
12381248
EXPLAIN SELECT
1239-
column1_utf8view ~* 'foo' AS c1
1249+
column1_utf8view ~* '^a.{3}e' AS c1
12401250
FROM test;
12411251
----
12421252
logical_plan
1243-
01)Projection: CAST(test.column1_utf8view AS Utf8) ILIKE Utf8("%foo%") AS c1
1253+
01)Projection: CAST(test.column1_utf8view AS Utf8) ~* Utf8("^a.{3}e") AS c1
12441254
02)--TableScan: test projection=[column1_utf8view]
12451255

1256+
query B
1257+
SELECT
1258+
column1_utf8view ~* '^a.{3}e' AS c1
1259+
FROM test;
1260+
----
1261+
true
1262+
false
1263+
false
1264+
NULL
1265+
12461266
# `!~~` operator (not like match)
12471267
query TT
12481268
EXPLAIN SELECT
1249-
column1_utf8view !~~ 'an' AS c1
1269+
column1_utf8view !~~ 'xia_g%g' AS c1
12501270
FROM test;
12511271
----
12521272
logical_plan
1253-
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~ Utf8("an") AS c1
1273+
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~ Utf8("xia_g%g") AS c1
12541274
02)--TableScan: test projection=[column1_utf8view]
12551275

1276+
query B
1277+
SELECT
1278+
column1_utf8view !~~ 'xia_g%g' AS c1
1279+
FROM test;
1280+
----
1281+
true
1282+
true
1283+
true
1284+
NULL
1285+
12561286
# `!~~*` operator (not like match case-insensitive)
12571287
query TT
12581288
EXPLAIN SELECT
1259-
column1_utf8view !~~* 'an' AS c1
1289+
column1_utf8view !~~* 'xia_g%g' AS c1
12601290
FROM test;
12611291
----
12621292
logical_plan
1263-
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~* Utf8("an") AS c1
1293+
01)Projection: CAST(test.column1_utf8view AS Utf8) !~~* Utf8("xia_g%g") AS c1
12641294
02)--TableScan: test projection=[column1_utf8view]
12651295

1296+
query B
1297+
SELECT
1298+
column1_utf8view !~~* 'xia_g%g' AS c1
1299+
FROM test;
1300+
----
1301+
true
1302+
false
1303+
true
1304+
NULL
1305+
12661306
statement ok
12671307
drop table test;
12681308

0 commit comments

Comments
 (0)