martin-g commented on code in PR #21322: URL: https://github.com/apache/datafusion/pull/21322#discussion_r3037302215
########## datafusion/functions/src/core/cast_to_type.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`CastToTypeFunc`]: Implementation of the `cast_to_type` Review Comment: ```suggestion //! [`CastToTypeFunc`]: Implementation of the `cast_to_type` function ``` ########## docs/source/user-guide/sql/scalar_functions.md: ########## @@ -5311,6 +5313,37 @@ arrow_typeof(expression) +---------------------------+------------------------+ ``` +### `cast_to_type` + +Casts the first argument to the data type of the second argument. Only the type of the second argument is used; its value is ignored. + +```sql +cast_to_type(expression, reference) +``` + +#### Arguments + +- **expression**: Expression to cast. The expression can be a constant, column, or function, and any combination of operators. Review Comment: ```suggestion - **expression**: Expression to cast. The expression can be a constant, a column, or a function, and any combination of operators. ``` ########## docs/source/user-guide/sql/scalar_functions.md: ########## @@ -5363,6 +5396,32 @@ get_field(expression, field_name[, field_name2, ...]) +--------+ ``` +### `try_cast_to_type` + +Casts the first argument to the data type of the second argument, returning NULL if the cast fails. Only the type of the second argument is used; its value is ignored. + +```sql +try_cast_to_type(expression, reference) +``` + +#### Arguments + +- **expression**: Expression to cast. The expression can be a constant, column, or function, and any combination of operators. Review Comment: ```suggestion - **expression**: Expression to cast. The expression can be a constant, a column, or a function, and any combination of operators. ``` ########## datafusion/functions/src/core/cast_to_type.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`CastToTypeFunc`]: Implementation of the `cast_to_type` + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{ + Result, datatype::DataTypeExt, internal_err, utils::take_function_args, +}; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs, + ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, +}; +use datafusion_macros::user_doc; + +/// Casts the first argument to the data type of the second argument. +/// +/// Only the type of the second argument is used; its value is ignored. +/// This is useful in macros or generic SQL where you need to preserve +/// or match types dynamically. +/// +/// For example: +/// ```sql +/// select cast_to_type('42', NULL::INTEGER); +/// ``` +#[user_doc( + doc_section(label = "Other Functions"), + description = "Casts the first argument to the data type of the second argument. Only the type of the second argument is used; its value is ignored.", + syntax_example = "cast_to_type(expression, reference)", + sql_example = r#"```sql +> select cast_to_type('42', NULL::INTEGER) as a; ++----+ +| a | ++----+ +| 42 | ++----+ + +> select cast_to_type(1 + 2, NULL::DOUBLE) as b; ++-----+ +| b | ++-----+ +| 3.0 | ++-----+ +```"#, + argument( + name = "expression", + description = "Expression to cast. The expression can be a constant, column, or function, and any combination of operators." + ), + argument( + name = "reference", + description = "Reference expression whose data type determines the target cast type. The value is ignored." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct CastToTypeFunc { + signature: Signature, +} + +impl Default for CastToTypeFunc { + fn default() -> Self { + Self::new() + } +} + +impl CastToTypeFunc { + pub fn new() -> Self { + Self { + signature: Signature::coercible( + vec![ + Coercion::new_exact(TypeSignatureClass::Any), + Coercion::new_exact(TypeSignatureClass::Any), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for CastToTypeFunc { + fn name(&self) -> &str { + "cast_to_type" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + internal_err!("return_field_from_args should be called instead") + } + + fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { + let [source_field, reference_field] = + take_function_args(self.name(), args.arg_fields)?; + let target_type = reference_field.data_type().clone(); + // Nullability is inherited only from the first argument (the value + // being cast). The second argument is used solely for its type, so + // its own nullability is irrelevant. The one exception is when the + // target type is Null – that type is inherently nullable. + let nullable = source_field.is_nullable() || target_type == DataType::Null; + Ok(Field::new(self.name(), target_type, nullable).into()) + } + + fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { + internal_err!("cast_to_type should have been simplified to cast") + } + + fn simplify( + &self, + mut args: Vec<Expr>, + info: &SimplifyContext, + ) -> Result<ExprSimplifyResult> { + let [_, type_arg] = take_function_args(self.name(), &args)?; + let target_type = info.get_data_type(type_arg)?; + + // remove second (reference) argument + args.pop().unwrap(); + let arg = args.pop().unwrap(); + + let source_type = info.get_data_type(&arg)?; + let new_expr = if source_type == target_type { + // the argument's data type is already the correct type + arg + } else { + // Use an actual cast to get the correct type + Expr::Cast(datafusion_expr::Cast { + expr: Box::new(arg), + field: target_type.into_nullable_field_ref(), Review Comment: Shouldn't this use the same logic as at `return_field_from_args()` ? ```suggestion let nullable = source_field.is_nullable() || target_type == DataType::Null; // Use an actual cast to get the correct type Expr::Cast(datafusion_expr::Cast { expr: Box::new(arg), field: Field::new("", target_type, nullable).into(), ``` ########## datafusion/functions/src/core/cast_to_type.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`CastToTypeFunc`]: Implementation of the `cast_to_type` + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{ + Result, datatype::DataTypeExt, internal_err, utils::take_function_args, +}; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs, + ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, +}; +use datafusion_macros::user_doc; + +/// Casts the first argument to the data type of the second argument. +/// +/// Only the type of the second argument is used; its value is ignored. +/// This is useful in macros or generic SQL where you need to preserve +/// or match types dynamically. +/// +/// For example: +/// ```sql +/// select cast_to_type('42', NULL::INTEGER); +/// ``` +#[user_doc( + doc_section(label = "Other Functions"), + description = "Casts the first argument to the data type of the second argument. Only the type of the second argument is used; its value is ignored.", + syntax_example = "cast_to_type(expression, reference)", + sql_example = r#"```sql +> select cast_to_type('42', NULL::INTEGER) as a; ++----+ +| a | ++----+ +| 42 | ++----+ + +> select cast_to_type(1 + 2, NULL::DOUBLE) as b; ++-----+ +| b | ++-----+ +| 3.0 | ++-----+ +```"#, + argument( + name = "expression", + description = "Expression to cast. The expression can be a constant, column, or function, and any combination of operators." + ), + argument( + name = "reference", + description = "Reference expression whose data type determines the target cast type. The value is ignored." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct CastToTypeFunc { + signature: Signature, +} + +impl Default for CastToTypeFunc { + fn default() -> Self { + Self::new() + } +} + +impl CastToTypeFunc { + pub fn new() -> Self { + Self { + signature: Signature::coercible( + vec![ + Coercion::new_exact(TypeSignatureClass::Any), + Coercion::new_exact(TypeSignatureClass::Any), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for CastToTypeFunc { + fn name(&self) -> &str { + "cast_to_type" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + internal_err!("return_field_from_args should be called instead") + } + + fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { + let [source_field, reference_field] = + take_function_args(self.name(), args.arg_fields)?; + let target_type = reference_field.data_type().clone(); + // Nullability is inherited only from the first argument (the value + // being cast). The second argument is used solely for its type, so + // its own nullability is irrelevant. The one exception is when the + // target type is Null – that type is inherently nullable. + let nullable = source_field.is_nullable() || target_type == DataType::Null; + Ok(Field::new(self.name(), target_type, nullable).into()) + } + + fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { + internal_err!("cast_to_type should have been simplified to cast") + } + + fn simplify( + &self, + mut args: Vec<Expr>, + info: &SimplifyContext, + ) -> Result<ExprSimplifyResult> { + let [_, type_arg] = take_function_args(self.name(), &args)?; + let target_type = info.get_data_type(type_arg)?; + + // remove second (reference) argument + args.pop().unwrap(); Review Comment: Why are these `pop`s needed ? You ignore the `source_arg` at `let [_, type_arg] = take_function_args(self.name(), &args)?;` ########## datafusion/sqllogictest/test_files/cast_to_type.slt: ########## @@ -0,0 +1,316 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +####### +## Tests for cast_to_type function +####### + +# Basic string to integer cast +query I +SELECT cast_to_type('42', NULL::INTEGER); +---- +42 + +# String to double cast +query R +SELECT cast_to_type('3.14', NULL::DOUBLE); +---- +3.14 + +# Integer to string cast +query T +SELECT cast_to_type(42, NULL::VARCHAR); +---- +42 + +# Integer to double cast +query R +SELECT cast_to_type(42, NULL::DOUBLE); +---- +42 + +# Same-type is a no-op +query I +SELECT cast_to_type(42, 0::INTEGER); +---- +42 + +# NULL first argument +query I +SELECT cast_to_type(NULL, 0::INTEGER); +---- +NULL + +# NULL reference (type still applies) +query I +SELECT cast_to_type('42', NULL::INTEGER); +---- +42 + +# CASE expression as first argument +query I +SELECT cast_to_type(CASE WHEN true THEN '1' ELSE '2' END, NULL::INTEGER); +---- +1 + +# Arithmetic expression as first argument +query R +SELECT cast_to_type(1 + 2, NULL::DOUBLE); +---- +3 + +# Nested cast_to_type +query T +SELECT cast_to_type(cast_to_type('3.14', NULL::DOUBLE), NULL::VARCHAR); +---- +3.14 + +# Subquery as second argument +query I +SELECT cast_to_type('42', (SELECT NULL::INTEGER)); +---- +42 + +# Column reference as second argument +statement ok +CREATE TABLE t1 (int_col INTEGER, text_col VARCHAR, double_col DOUBLE); + +statement ok +INSERT INTO t1 VALUES (1, 'hello', 3.14), (2, 'world', 2.72); + +query I +SELECT cast_to_type('99', int_col) FROM t1 LIMIT 1; +---- +99 + +query T +SELECT cast_to_type(123, text_col) FROM t1 LIMIT 1; +---- +123 + +query R +SELECT cast_to_type('1.5', double_col) FROM t1 LIMIT 1; +---- +1.5 + +# Use with column values as first argument +query R +SELECT cast_to_type(int_col, NULL::DOUBLE) FROM t1; +---- +1 +2 + +# Cast column to match another column's type +query T +SELECT cast_to_type(int_col, text_col) FROM t1; +---- +1 +2 + +# Boolean cast +query B +SELECT cast_to_type(1, NULL::BOOLEAN); +---- +true + +# String to date cast +query D +SELECT cast_to_type('2024-01-15', NULL::DATE); +---- +2024-01-15 + +# Error on invalid cast +statement error Review Comment: Why there is no regex for the error message here ? ########## datafusion/functions/src/core/cast_to_type.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`CastToTypeFunc`]: Implementation of the `cast_to_type` + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{ + Result, datatype::DataTypeExt, internal_err, utils::take_function_args, +}; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs, + ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, +}; +use datafusion_macros::user_doc; + +/// Casts the first argument to the data type of the second argument. +/// +/// Only the type of the second argument is used; its value is ignored. +/// This is useful in macros or generic SQL where you need to preserve +/// or match types dynamically. +/// +/// For example: +/// ```sql +/// select cast_to_type('42', NULL::INTEGER); +/// ``` +#[user_doc( + doc_section(label = "Other Functions"), + description = "Casts the first argument to the data type of the second argument. Only the type of the second argument is used; its value is ignored.", + syntax_example = "cast_to_type(expression, reference)", + sql_example = r#"```sql +> select cast_to_type('42', NULL::INTEGER) as a; ++----+ +| a | ++----+ +| 42 | ++----+ + +> select cast_to_type(1 + 2, NULL::DOUBLE) as b; ++-----+ +| b | ++-----+ +| 3.0 | ++-----+ +```"#, + argument( + name = "expression", + description = "Expression to cast. The expression can be a constant, column, or function, and any combination of operators." Review Comment: ```suggestion description = "The expression to cast. It can be a constant, column, or function, and any combination of operators." ``` Feel free to ignore the suggestion if it is not better than original! I am not a native English speaker! ########## datafusion/sqllogictest/test_files/cast_to_type.slt: ########## @@ -0,0 +1,316 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +####### +## Tests for cast_to_type function +####### + +# Basic string to integer cast +query I +SELECT cast_to_type('42', NULL::INTEGER); +---- +42 + +# String to double cast +query R +SELECT cast_to_type('3.14', NULL::DOUBLE); +---- +3.14 + +# Integer to string cast +query T +SELECT cast_to_type(42, NULL::VARCHAR); +---- +42 + +# Integer to double cast +query R +SELECT cast_to_type(42, NULL::DOUBLE); +---- +42 + +# Same-type is a no-op +query I +SELECT cast_to_type(42, 0::INTEGER); +---- +42 + +# NULL first argument +query I +SELECT cast_to_type(NULL, 0::INTEGER); +---- +NULL + +# NULL reference (type still applies) +query I +SELECT cast_to_type('42', NULL::INTEGER); Review Comment: This is exactly the same test as at line 24 ########## datafusion/functions/src/core/try_cast_to_type.rs: ########## @@ -0,0 +1,135 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`TryCastToTypeFunc`]: Implementation of the `try_cast_to_type` + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{ + Result, datatype::DataTypeExt, internal_err, utils::take_function_args, +}; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs, + ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, +}; +use datafusion_macros::user_doc; + +/// Like [`cast_to_type`](super::cast_to_type::CastToTypeFunc) but returns NULL +/// on cast failure instead of erroring. +/// +/// This is implemented by simplifying `try_cast_to_type(expr, ref)` into +/// `Expr::TryCast` during optimization. +#[user_doc( + doc_section(label = "Other Functions"), + description = "Casts the first argument to the data type of the second argument, returning NULL if the cast fails. Only the type of the second argument is used; its value is ignored.", + syntax_example = "try_cast_to_type(expression, reference)", + sql_example = r#"```sql +> select try_cast_to_type('123', NULL::INTEGER) as a, + try_cast_to_type('not_a_number', NULL::INTEGER) as b; + ++-----+------+ +| a | b | ++-----+------+ +| 123 | NULL | ++-----+------+ +```"#, + argument( + name = "expression", + description = "Expression to cast. The expression can be a constant, column, or function, and any combination of operators." + ), + argument( + name = "reference", + description = "Reference expression whose data type determines the target cast type. The value is ignored." + ) +)] +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct TryCastToTypeFunc { + signature: Signature, +} + +impl Default for TryCastToTypeFunc { + fn default() -> Self { + Self::new() + } +} + +impl TryCastToTypeFunc { + pub fn new() -> Self { + Self { + signature: Signature::coercible( + vec![ + Coercion::new_exact(TypeSignatureClass::Any), + Coercion::new_exact(TypeSignatureClass::Any), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for TryCastToTypeFunc { + fn name(&self) -> &str { + "try_cast_to_type" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + internal_err!("return_field_from_args should be called instead") + } + + fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { + // TryCast can always return NULL (on cast failure), so always nullable + let [_, reference_field] = take_function_args(self.name(), args.arg_fields)?; + let target_type = reference_field.data_type().clone(); + Ok(Field::new(self.name(), target_type, true).into()) + } + + fn invoke_with_args(&self, _args: ScalarFunctionArgs) -> Result<ColumnarValue> { + internal_err!("try_cast_to_type should have been simplified to try_cast") + } + + fn simplify( + &self, + mut args: Vec<Expr>, + info: &SimplifyContext, + ) -> Result<ExprSimplifyResult> { + let [_, type_arg] = take_function_args(self.name(), &args)?; + let target_type = info.get_data_type(type_arg)?; + + // remove second (reference) argument + args.pop().unwrap(); Review Comment: As in the earlier comment. Don't ignore the `source_arg` returned by `take_function_args()` ########## datafusion/functions/src/core/try_cast_to_type.rs: ########## @@ -0,0 +1,135 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`TryCastToTypeFunc`]: Implementation of the `try_cast_to_type` Review Comment: ```suggestion //! [`TryCastToTypeFunc`]: Implementation of the `try_cast_to_type` function ``` ########## datafusion/sqllogictest/test_files/cast_to_type.slt: ########## @@ -0,0 +1,316 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +####### +## Tests for cast_to_type function +####### + +# Basic string to integer cast +query I +SELECT cast_to_type('42', NULL::INTEGER); +---- +42 + +# String to double cast +query R +SELECT cast_to_type('3.14', NULL::DOUBLE); +---- +3.14 + +# Integer to string cast +query T +SELECT cast_to_type(42, NULL::VARCHAR); +---- +42 + +# Integer to double cast +query R +SELECT cast_to_type(42, NULL::DOUBLE); +---- +42 + +# Same-type is a no-op +query I +SELECT cast_to_type(42, 0::INTEGER); +---- +42 + +# NULL first argument +query I +SELECT cast_to_type(NULL, 0::INTEGER); +---- +NULL + +# NULL reference (type still applies) +query I +SELECT cast_to_type('42', NULL::INTEGER); +---- +42 + +# CASE expression as first argument +query I +SELECT cast_to_type(CASE WHEN true THEN '1' ELSE '2' END, NULL::INTEGER); +---- +1 + +# Arithmetic expression as first argument +query R +SELECT cast_to_type(1 + 2, NULL::DOUBLE); +---- +3 + +# Nested cast_to_type +query T +SELECT cast_to_type(cast_to_type('3.14', NULL::DOUBLE), NULL::VARCHAR); +---- +3.14 + +# Subquery as second argument +query I +SELECT cast_to_type('42', (SELECT NULL::INTEGER)); +---- +42 + +# Column reference as second argument +statement ok +CREATE TABLE t1 (int_col INTEGER, text_col VARCHAR, double_col DOUBLE); + +statement ok +INSERT INTO t1 VALUES (1, 'hello', 3.14), (2, 'world', 2.72); + +query I +SELECT cast_to_type('99', int_col) FROM t1 LIMIT 1; +---- +99 + +query T +SELECT cast_to_type(123, text_col) FROM t1 LIMIT 1; +---- +123 + +query R +SELECT cast_to_type('1.5', double_col) FROM t1 LIMIT 1; +---- +1.5 + +# Use with column values as first argument +query R +SELECT cast_to_type(int_col, NULL::DOUBLE) FROM t1; +---- +1 +2 + +# Cast column to match another column's type +query T +SELECT cast_to_type(int_col, text_col) FROM t1; +---- +1 +2 + +# Boolean cast +query B +SELECT cast_to_type(1, NULL::BOOLEAN); +---- +true + +# String to date cast +query D +SELECT cast_to_type('2024-01-15', NULL::DATE); +---- +2024-01-15 + +# Error on invalid cast +statement error Review Comment: Let's also add a test case for invalid target type, e.g. `NULL::INVALID` ########## datafusion/functions/src/core/try_cast_to_type.rs: ########## @@ -0,0 +1,135 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! [`TryCastToTypeFunc`]: Implementation of the `try_cast_to_type` + +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{ + Result, datatype::DataTypeExt, internal_err, utils::take_function_args, +}; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs, + ScalarUDFImpl, Signature, TypeSignatureClass, Volatility, +}; +use datafusion_macros::user_doc; + +/// Like [`cast_to_type`](super::cast_to_type::CastToTypeFunc) but returns NULL +/// on cast failure instead of erroring. +/// +/// This is implemented by simplifying `try_cast_to_type(expr, ref)` into +/// `Expr::TryCast` during optimization. +#[user_doc( + doc_section(label = "Other Functions"), + description = "Casts the first argument to the data type of the second argument, returning NULL if the cast fails. Only the type of the second argument is used; its value is ignored.", + syntax_example = "try_cast_to_type(expression, reference)", + sql_example = r#"```sql +> select try_cast_to_type('123', NULL::INTEGER) as a, + try_cast_to_type('not_a_number', NULL::INTEGER) as b; + ++-----+------+ +| a | b | ++-----+------+ +| 123 | NULL | ++-----+------+ +```"#, + argument( + name = "expression", + description = "Expression to cast. The expression can be a constant, column, or function, and any combination of operators." Review Comment: ```suggestion description = "The expression to cast. It can be a constant, column, or function, and any combination of operators." ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
