-
Notifications
You must be signed in to change notification settings - Fork 0
21322: feat: add cast_to_type UDF for type-based casting #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7fc781f
421be8e
7013469
7891a1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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(), | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value:useful; category:bug; feedback: The Gemini AI reviewer is correct! There was an issue in the |
||||||
| }) | ||||||
| }; | ||||||
|
Comment on lines
+140
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check how DataFusion handles Cast expression nullability and whether
# into_nullable_field_ref vs computed nullability causes issues
# Search for how Cast's field nullability affects schema derivation
rg -n -A5 'impl.*Cast' --type rust
echo "---"
# Check if there are any tests that verify runtime nullability matches declared schema
rg -n 'into_nullable_field_ref' --type rust -C3Repository: martin-augment/datafusion Length of output: 36845 🏁 Script executed: cat -n datafusion/functions/src/core/cast_to_type.rsRepository: martin-augment/datafusion Length of output: 6289 🏁 Script executed: rg -n "impl.*Expr.*Cast" datafusion/expr/src/expr.rs -A 20 | head -100Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "pub struct Cast" datafusion/expr/src/expr.rs -A 10Repository: martin-augment/datafusion Length of output: 393 🏁 Script executed: rg -n "fn.*data_type.*Cast\|Cast.*data_type" datafusion/expr/src/expr.rs -B 2 -A 8 | head -80Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "Cast =>" datafusion/expr/src/expr.rs -A 5 | head -60Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "expr.data_type\|field.data_type\|field.is_nullable" datafusion/expr/src/expr.rs -A 2 | head -100Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: ast-grep --pattern 'impl Expr {
$$$
pub fn data_type($_) -> $_ {
$$$
}
$$$
}'Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "fn data_type" datafusion/expr/src/expr.rs | head -20Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "pub fn data_type\|pub fn field" datafusion/expr/src/ -A 3 | grep -A 3 "data_type\|field" | head -80Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "Expr::Cast" datafusion/expr/src/expr.rs -B 2 -A 5 | head -100Repository: martin-augment/datafusion Length of output: 3983 🏁 Script executed: rg -n "get_data_type\|get_field\|output_ordering" datafusion/expr/src/ -A 3 | grep -B 2 -A 5 "Cast" | head -80Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "DynScalarUDF\|get_data_type" datafusion/expr/src/expr.rs -B 2 -A 8 | head -150Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "Cast.*field\|field.*Cast" datafusion/core/src/ --type rust -A 3 | head -100Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: rg -n "fn data_type\|fn field" datafusion/physical-expr/src/expressions/cast.rs -B 2 -A 10 | head -150Repository: martin-augment/datafusion Length of output: 51 🏁 Script executed: cat -n datafusion/physical-expr/src/expressions/cast.rs | head -250Repository: martin-augment/datafusion Length of output: 10920 🏁 Script executed: cat -n datafusion/physical-expr/src/expressions/cast.rs | sed -n '241,260p'Repository: martin-augment/datafusion Length of output: 1133 Fix nullability inconsistency between type inference and simplification.
The let nullable = source_field.is_nullable() || target_type == DataType::Null;
let field = Field::new("", target_type, nullable).into();This ensures the declared schema and the simplified Cast expression produce the same nullability. 🤖 Prompt for AI Agents
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value:useful; category:bug; feedback: The CodeRabbit AI reviewer is correct! There was an issue in the return_field_from_args() method that the PR author improved in the last commit in the PR. But it seems the author didn't improve the same in the simplify() function. Prevents a wrong behavior and wrong result in the information schema for a view that uses this user defined function |
||||||
| Ok(ExprSimplifyResult::Simplified(new_expr)) | ||||||
| } | ||||||
|
|
||||||
| fn documentation(&self) -> Option<&Documentation> { | ||||||
| self.doc() | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| let arg = args.pop().unwrap(); | ||
|
|
||
| let source_type = info.get_data_type(&arg)?; | ||
| let new_expr = if source_type == target_type { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Severity: medium 🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage. |
||
| arg | ||
| } else { | ||
| Expr::TryCast(datafusion_expr::TryCast { | ||
| expr: Box::new(arg), | ||
| field: target_type.into_nullable_field_ref(), | ||
| }) | ||
| }; | ||
| Ok(ExprSimplifyResult::Simplified(new_expr)) | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
datafusion/functions/src/core/cast_to_type.rs:141:return_field_from_argsspecial-casesDataType::Nullto forcenullable=true, butsimplifyrewrites toExpr::Cast, whose field nullability is derived from the input expression. If the reference arg is a bareNULL(typeNull) and the source is non-nullable, this rewrite can yield a non-nullableNulloutput schema (and diverge from the UDF’s declared schema).Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.