feat: add cast_to_type UDF for type-based casting#21322
Open
adriangb wants to merge 3 commits intoapache:mainfrom
Open
feat: add cast_to_type UDF for type-based casting#21322adriangb wants to merge 3 commits intoapache:mainfrom
adriangb wants to merge 3 commits intoapache:mainfrom
Conversation
Add a `cast_to_type(expression, reference)` function that casts the first argument to the data type of the second argument, similar to DuckDB's cast_to_type. The second argument's type (not value) determines the target cast type, which is useful in macros and generic SQL where types need to be preserved dynamically. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add `try_cast_to_type(expression, reference)` which works like `cast_to_type` but returns NULL on cast failure instead of erroring, similar to the relationship between arrow_cast and arrow_try_cast. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
N/A — new feature
Rationale for this change
DuckDB provides a
cast_to_type(expression, reference)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, along with a fallibletry_cast_to_typevariant.What changes are included in this PR?
cast_to_typescalar UDF indatafusion/functions/src/core/cast_to_type.rsreturn_field_from_argsto infer return type from the second argument's data typesimplify()rewrites toExpr::Cast(or no-op if types match), so there is zero runtime overheadtry_cast_to_typescalar UDF indatafusion/functions/src/core/try_cast_to_type.rscast_to_typebut returns NULL on cast failure instead of erroringsimplify()rewrites toExpr::TryCastdatafusion/functions/src/core/mod.rsAre these changes tested?
Yes. New sqllogictest file
cast_to_type.sltcovering both functions:cast_to_type) vs NULL on invalid cast (try_cast_to_type)Are there any user-facing changes?
Two new SQL functions:
cast_to_type(expression, reference)— casts expression to the type of referencetry_cast_to_type(expression, reference)— same, but returns NULL on failure🤖 Generated with Claude Code