Skip to content

Commit

Permalink
no casting if the logical types are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashenC committed Nov 15, 2024
1 parent 89988d8 commit 6723ad0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
16 changes: 14 additions & 2 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ pub fn data_types_with_scalar_udf(
current_types: &[DataType],
func: &ScalarUDF,
) -> Result<Vec<DataType>> {
println!("func - {:?}", func);
let signature = func.signature();
println!("sig - {:?}", signature);

if current_types.is_empty() {
if signature.type_signature.supports_zero_argument() {
Expand All @@ -56,6 +58,7 @@ pub fn data_types_with_scalar_udf(

let valid_types =
get_valid_types_with_scalar_udf(&signature.type_signature, current_types, func)?;
println!("validT - {:?}", valid_types);

if valid_types
.iter()
Expand Down Expand Up @@ -544,11 +547,20 @@ fn get_valid_types(
for (current_type, target_type) in
current_types.iter().zip(target_types.iter())
{
println!("curT - {:?}", current_type);
println!("targetT - {:?}", target_type);
let logical_type: NativeType = current_type.into();
let target_logical_type = target_type.native();
println!("cur_logiT - {:?}", logical_type);
println!("target_logiT - {:?}", target_logical_type);
if can_coerce_to(&logical_type, target_logical_type) {
let target_type =
target_logical_type.default_cast_for(current_type)?;
let target_type = if &logical_type == target_logical_type {
current_type.clone()
} else {
target_logical_type.default_cast_for(current_type)?
};
// let target_type = target_logical_type.default_cast_for(current_type)?;
println!("new targetT - {:?}", target_type);
new_types.push(target_type);
}
}
Expand Down
4 changes: 4 additions & 0 deletions datafusion/sqllogictest/test_files/jctest.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query T
SELECT left(arrow_cast('abcde', 'Dictionary(Int32, Utf8)'), -2)
----
abc
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1864,10 +1864,10 @@ query TT
EXPLAIN SELECT letter, letter = LEFT(letter2, 1) FROM simple_string;
----
logical_plan
01)Projection: simple_string.letter, simple_string.letter = left(CAST(simple_string.letter2 AS Utf8View), Int64(1))
01)Projection: simple_string.letter, simple_string.letter = left(simple_string.letter2, Int64(1))
02)--TableScan: simple_string projection=[letter, letter2]
physical_plan
01)ProjectionExec: expr=[letter@0 as letter, letter@0 = left(CAST(letter2@1 AS Utf8View), 1) as simple_string.letter = left(simple_string.letter2,Int64(1))]
01)ProjectionExec: expr=[letter@0 as letter, letter@0 = left(letter2@1, 1) as simple_string.letter = left(simple_string.letter2,Int64(1))]
02)--MemoryExec: partitions=1, partition_sizes=[1]

query TB
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/string/string_view.slt
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ EXPLAIN SELECT
FROM test;
----
logical_plan
01)Projection: lpad(test.column1_utf8view, Int64(12), Utf8View(" ")) AS c1
01)Projection: lpad(test.column1_utf8view, Int64(12), Utf8(" ")) AS c1
02)--TableScan: test projection=[column1_utf8view]

query TT
Expand All @@ -672,7 +672,7 @@ EXPLAIN SELECT
FROM test;
----
logical_plan
01)Projection: lpad(test.column1_utf8view, Int64(12), CAST(test.column2_large_utf8 AS Utf8View)) AS c1
01)Projection: lpad(test.column1_utf8view, Int64(12), test.column2_large_utf8) AS c1
02)--TableScan: test projection=[column2_large_utf8, column1_utf8view]

query TT
Expand Down Expand Up @@ -826,7 +826,7 @@ EXPLAIN SELECT
FROM test;
----
logical_plan
01)Projection: rpad(test.column1_utf8view, Int64(12), CAST(test.column2_large_utf8 AS Utf8View)) AS c1
01)Projection: rpad(test.column1_utf8view, Int64(12), test.column2_large_utf8) AS c1
02)--TableScan: test projection=[column2_large_utf8, column1_utf8view]

query TT
Expand Down

0 comments on commit 6723ad0

Please sign in to comment.