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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to