Skip to content
Open
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
9 changes: 7 additions & 2 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,13 @@ fn get_valid_types(
default_casted_type.default_cast_for(current_type)?;
new_types.push(casted_type);
} else {
return internal_err!(
"Expect {} but received NativeType::{}, DataType: {}",
let hint = if matches!(current_native_type, NativeType::Binary) {
"\n\nHint: Binary types are not automatically coerced to String. Use CAST(column AS VARCHAR) to convert Binary data to String."
} else {
""
};
return plan_err!(
Copy link
Contributor

Choose a reason for hiding this comment

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

this looks good to me -- thank you @lemorage

Couuld you please add a test (for this new error) to the slt tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

"Function '{function_name}' requires {}, but received {} (DataType: {}).{hint}",
param.desired_type(),
current_native_type,
current_type
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/arrow_typeof.slt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SELECT arrow_cast('1', 'Int16')
query error
SELECT arrow_cast('1')

query error Expect TypeSignatureClass::Native\(LogicalType\(Native\(String\), String\)\) but received NativeType::Int64, DataType: Int64
query error DataFusion error: Error during planning: Function 'arrow_cast' requires TypeSignatureClass::Native\(LogicalType\(Native\(String\), String\)\), but received Int64 \(DataType: Int64\)
SELECT arrow_cast('1', 43)

query error DataFusion error: Execution error: arrow_cast requires its second argument to be a non\-empty constant string
Expand Down
10 changes: 10 additions & 0 deletions datafusion/sqllogictest/test_files/binary.slt
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,13 @@ Foo foo Foo foo
NULL NULL NULL NULL
Bar Bar Bar Bar
FooBar fooBar FooBar fooBar

# show helpful error msg when Binary type is used with string functions
query error DataFusion error: Error during planning: Function 'split_part' requires TypeSignatureClass::Native\(LogicalType\(Native\(String\), String\)\), but received Binary \(DataType: Binary\)\.\n\nHint: Binary types are not automatically coerced to String\. Use CAST\(column AS VARCHAR\) to convert Binary data to String\.
SELECT split_part(binary, '~', 2) FROM t WHERE binary IS NOT NULL LIMIT 1;

# ensure the suggested CAST workaround works
query T
SELECT split_part(CAST(binary AS VARCHAR), 'o', 2) FROM t WHERE binary = X'466f6f';
----
(empty)
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/datetime/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3064,7 +3064,7 @@ NULL
query error DataFusion error: Error during planning: Function 'make_date' expects 3 arguments but received 1
select make_date(1);

query error Expect TypeSignatureClass::Native\(LogicalType\(Native\(Int32\), Int32\)\) but received NativeType::Interval\(MonthDayNano\), DataType: Interval\(MonthDayNano\)
query error DataFusion error: Error during planning: Function 'make_date' requires TypeSignatureClass::Native\(LogicalType\(Native\(Int32\), Int32\)\), but received Interval\(MonthDayNano\) \(DataType: Interval\(MonthDayNano\)\)
select make_date(interval '1 day', '2001-05-21'::timestamp, '2001-05-21'::timestamp);

##########
Expand Down Expand Up @@ -3337,7 +3337,7 @@ select make_time(22, '', 27);
query error Cannot cast string '' to value of Int32 type
select make_time(22, 1, '');

query error Expect TypeSignatureClass::Native\(LogicalType\(Native\(Int32\), Int32\)\) but received NativeType::Float64, DataType: Float64
query error DataFusion error: Error during planning: Function 'make_time' requires TypeSignatureClass::Native\(LogicalType\(Native\(Int32\), Int32\)\), but received Float64 \(DataType: Float64\)
select make_time(arrow_cast(22, 'Float64'), 1, '');

##########
Expand Down Expand Up @@ -3952,7 +3952,7 @@ statement error
select to_local_time('2024-04-01T00:00:20Z'::timestamp, 'some string');

# invalid argument data type
statement error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Timestamp but received NativeType::String, DataType: Utf8
statement error DataFusion error: Error during planning: Function 'to_local_time' requires TypeSignatureClass::Timestamp, but received String \(DataType: Utf8\)
select to_local_time('2024-04-01T00:00:20Z');

# invalid timezone
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/encoding.slt
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ CREATE TABLE test(
;

# errors
query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Binary but received NativeType::Int64, DataType: Int64
query error DataFusion error: Error during planning: Function 'encode' requires TypeSignatureClass::Binary, but received Int64 \(DataType: Int64\)
select encode(12, 'hex');

query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Binary but received NativeType::Int64, DataType: Int64
query error DataFusion error: Error during planning: Function 'decode' requires TypeSignatureClass::Binary, but received Int64 \(DataType: Int64\)
select decode(12, 'hex');

query error DataFusion error: Error during planning: There is no built\-in encoding named 'non_encoding', currently supported encodings are: base64, hex
Expand All @@ -73,7 +73,7 @@ select decode('', null) from test;
query error DataFusion error: This feature is not implemented: Encoding must be a scalar; array specified encoding is not yet supported
select decode('', hex_field) from test;

query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Integer but received NativeType::String, DataType: Utf8View
query error DataFusion error: Error during planning: Function 'to_hex' requires TypeSignatureClass::Integer, but received String \(DataType: Utf8View\)
select to_hex(hex_field) from test;

query error DataFusion error: Execution error: Failed to decode value using base64
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ select repeat('-1.2', arrow_cast(3, 'Int32'));
----
-1.2-1.2-1.2

query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Native\(LogicalType\(Native\(Int64\), Int64\)\) but received NativeType::Float64, DataType: Float64
query error DataFusion error: Error during planning: Function 'repeat' requires TypeSignatureClass::Native\(LogicalType\(Native\(Int64\), Int64\)\), but received Float64 \(DataType: Float64\)
select repeat('-1.2', 3.2);

query T
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ select position('' in '')
----
1

query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Native\(LogicalType\(Native\(String\), String\)\) but received NativeType::Int64, DataType: Int64
query error DataFusion error: Error during planning: Function 'strpos' requires TypeSignatureClass::Native\(LogicalType\(Native\(String\), String\)\), but received Int64 \(DataType: Int64\)
select position(1 in 1)

query I
Expand Down