You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "adriangb (via GitHub)" <gi...@apache.org> on 2026/04/02 15:14:10 UTC

[PR] feat: add cast_to_type UDF for type-based casting [datafusion]

adriangb opened a new pull request, #21322:
URL: https://github.com/apache/datafusion/pull/21322

   ## Which issue does this PR close?
   
   N/A — new feature
   
   ## Rationale for this change
   
   DuckDB provides a [`cast_to_type(expression, reference)`](https://duckdb.org/docs/current/sql/expressions/cast#cast_to_type-function) function that casts the first argument to the data type of the second argument. This is useful in macros and generic SQL where types need to be preserved or matched dynamically. This PR adds the equivalent function to DataFusion.
   
   ## What changes are included in this PR?
   
   - New `cast_to_type` scalar UDF in `datafusion/functions/src/core/cast_to_type.rs`
     - Takes two arguments: the expression to cast, and a reference expression whose **type** (not value) determines the target cast type
     - Uses `return_field_from_args` to infer return type from the second argument's data type
     - `simplify()` rewrites to `Expr::Cast` (or no-op if types match), so there is zero runtime overhead
   - Registration in `datafusion/functions/src/core/mod.rs`
   
   ## Are these changes tested?
   
   Yes. New sqllogictest file `cast_to_type.slt` covering:
   - Basic casts (string→int, string→double, int→string, int→double)
   - NULL handling
   - Same-type no-op
   - CASE expression as first argument
   - Arithmetic expression as first argument
   - Nested `cast_to_type` calls
   - Subquery as second argument
   - Column references as second argument
   - Boolean and date casts
   - Error on invalid cast
   
   ## Are there any user-facing changes?
   
   New SQL function `cast_to_type(expression, reference)` is available.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4214868358

   > any thoughts on how this should behave w.r.t. field metadata? Should we set the metadata to the metadata of the first argument, should we merge them or should we preserve only the metadata of the second argument?
   
   Preserving the source metadata is usually incorrect for extension types (e.g., casting a UUID to string would result in an invalid UUID to my reading). I will try to fix in https://github.com/apache/datafusion/pull/21071.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4187124137

   @martin-g would you mind reviewing this change?


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3038165504


##########
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:
   @adriangb Just this has left. I guess you forgot to add it.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3037426317


##########
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:
   Good catch. The intention was different but being a relatively simple feature it ended up being the exact same test. I've done a pass at trying to improve the tests in 954e029dd.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4189695740

   > @martin-g any thoughts on how this should behave w.r.t. field metadata? Should we set the metadata to the metadata of the first argument, should we merge them or should we preserve only the metadata of the second argument?
   
   I think it makes sense to merge, preferring the target type's metadata? I went with that for now.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3037082870


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,147 @@
+// 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 nullable = args.arg_fields.iter().any(|f| f.is_nullable());

Review Comment:
   For DuckDB it is also nullable:
   
   ```sql
   CREATE TABLE test AS SELECT cast_to_type('42', NULL::INTEGER) AS val;
   
   SELECT column_name, data_type, is_nullable
   FROM information_schema.columns
   WHERE table_name = 'test';
   ```
   
   ```
   ┌─────────────┬───────────┬─────────────┐
   │ column_name │ data_type │ is_nullable │
   │   varchar   │  varchar  │   varchar   │
   ├─────────────┼───────────┼─────────────┤
   │ val         │ INTEGER   │ YES         │
   └─────────────┴───────────┴─────────────┘
   ```
   
   But apparently even casting a non-nullable column makes a nullable output, which doesn't make sense, it would error if it can't cast:
   
   
   ```sql
   CREATE TABLE data (x INT NOT NULL);
   INSERT INTO data VALUES (1);
   
   CREATE TABLE test AS SELECT cast(x AS INT) AS val FROM data;
   
   SELECT column_name, data_type, is_nullable
   FROM information_schema.columns
   WHERE table_name = 'test';
   ```
   
   ```
   ┌─────────────┬───────────┬─────────────┐
   │ column_name │ data_type │ is_nullable │
   │   varchar   │  varchar  │   varchar   │
   ├─────────────┼───────────┼─────────────┤
   │ val         │ INTEGER   │ YES         │
   └─────────────┴───────────┴─────────────┘
   ```
   
   So I feel this is more of a limitation of DuckDB than anything.
   
   DataFusion does preserve / compute nullability through similar expressions:
   
   ```
   > create table test as select cast('42' AS INT) AS val;
   0 row(s) fetched.
   Elapsed 0.002 seconds.
   
   > DESCRIBE test;
   +-------------+-----------+-------------+
   | column_name | data_type | is_nullable |
   +-------------+-----------+-------------+
   | val         | Int32     | NO          |
   +-------------+-----------+-------------+
   1 row(s) fetched.
   Elapsed 0.000 seconds.
   
   > drop table test;
   0 row(s) fetched.
   Elapsed 0.000 seconds.
   
   > create table test as select arrow_cast('42', 'UInt16') AS val;
   0 row(s) fetched.
   Elapsed 0.002 seconds.
   
   > DESCRIBE test;
   +-------------+-----------+-------------+
   | column_name | data_type | is_nullable |
   +-------------+-----------+-------------+
   | val         | UInt16    | NO          |
   +-------------+-----------+-------------+
   1 row(s) fetched.
   Elapsed 0.000 seconds.
   ```
   
   I changed this in 7891a1fd0.
   
   Unlike `cast('42' AS NULL)` or `arrow_cast('42', 'Null')` which both fail `cast_to_type('42', null)` will succeed. My reasoning is that these expressions will be used programmatically thus it's more likely to hit an edge case like this and want to proceed instead of failing. I'm not sure why `arrow_cast('42', 'Null')` fails.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #21322:
URL: https://github.com/apache/datafusion/pull/21322


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3058443653


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,146 @@
+// 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` function
+
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::{Result, 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 = "The expression to cast. It 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())

Review Comment:
   Hmm...I thought the logical cast errored when converted to a physical expression but preserved metadata after https://github.com/apache/datafusion/pull/18136 . I will also try to fix in https://github.com/apache/datafusion/pull/21071 since it will make a whole lot more sense what a cast to an extension type should actually do in that case 🙂 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4214908607

   > > any thoughts on how this should behave w.r.t. field metadata? Should we set the metadata to the metadata of the first argument, should we merge them or should we preserve only the metadata of the second argument?
   > 
   > Preserving the source metadata is usually incorrect for extension types (e.g., casting a UUID to string would result in an invalid UUID to my reading). I will try to fix in #21071.
   
   Makes sense. No idea how this should behave then 😢 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3037432122


##########
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:
   This was just copied from the existing implementations:
   
   https://github.com/pydantic/datafusion/blob/954e029dd2e443ff5e1b98f5c91bb61f6f2b3046/datafusion/functions/src/core/arrow_cast.rs#L163
   
   But I do like your version more, I'll update it in these two new functions, in `arrow_cast` and `arrow_try_cast` as well.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3039993157


##########
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:
   tackled both of these in 08b7edb



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4187122914

   > I like this, i just noticed we might need a bit more tests on `ScalarUDFImpl` in the future, but there are slts on that so could be just fine as is.
   
   Generally we prefer SLT tests. They avoid more code to compile, are easier to grok and are closer to real world usage.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4214579995

   Looks like this PR has a bunch of approvals and no outstanding comments, so merging!


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3058360824


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,146 @@
+// 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` function
+
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::{Result, 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 = "The expression to cast. It 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())

Review Comment:
   @adriangb This piece will drop extension metadata from the `to` type. Was this intentional?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3039966856


##########
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:
   Yep sorry!



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "AndreaBozzo (via GitHub)" <gi...@apache.org>.
AndreaBozzo commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4186971799

   I like this, i just noticed we might need a bit more tests on `ScalarUDFImpl` in the future


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3037432122


##########
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:
   This was just copied from the existing implementations:
   
   https://github.com/apache/datafusion/blob/c17c87c575be916c6501df8a588e3c2614a9dc6f/datafusion/functions/src/core/arrow_cast.rs#L162-L164
   
   But I do like your version more, I'll update it in these two new functions, in `arrow_cast` and `arrow_try_cast` as well.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#issuecomment-4189649996

   @martin-g any thoughts on how this should behave w.r.t. field metadata? Should we set the metadata to the metadata of the first argument, should we merge them or should we preserve only the metadata of the second argument?


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "Weijun-H (via GitHub)" <gi...@apache.org>.
Weijun-H commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3035640528


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,147 @@
+// 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 nullable = args.arg_fields.iter().any(|f| f.is_nullable());

Review Comment:
   This looks incorrect to me. `reference` is only used for its type, but its nullability is being propagated to the result schema. That means expressions like `cast_to_type(42, NULL::INTEGER)` become nullable in the logical plan schema.
   
   I think `cast_to_type` should only inherit nullability from the first argument?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "AndreaBozzo (via GitHub)" <gi...@apache.org>.
AndreaBozzo commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3035648100


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,147 @@
+// 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 nullable = args.arg_fields.iter().any(|f| f.is_nullable());

Review Comment:
   `arrow_cast` and `arrow_try_cast` do follow the same pattern so i thought of this coherent, the optimizer likely refines nullability downstream



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3058443653


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,146 @@
+// 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` function
+
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::{Result, 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 = "The expression to cast. It 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())

Review Comment:
   Hmm...I thought the logical cast errored but preserved metadata after https://github.com/apache/datafusion/pull/18136 . I will also try to fix in https://github.com/apache/datafusion/pull/21071 since it will make a whole lot more sense what a cast to an extension type should actually do in that case 🙂 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: add cast_to_type UDF for type-based casting [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #21322:
URL: https://github.com/apache/datafusion/pull/21322#discussion_r3058404727


##########
datafusion/functions/src/core/cast_to_type.rs:
##########
@@ -0,0 +1,146 @@
+// 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` function
+
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::{Result, 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 = "The expression to cast. It 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())

Review Comment:
   The actual implementation does as well, https://github.com/apache/datafusion/pull/21322#issuecomment-4189649996



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org