Skip to content

feat: add cast_to_type UDF for type-based casting#21322

Open
adriangb wants to merge 3 commits intoapache:mainfrom
pydantic:cast-to
Open

feat: add cast_to_type UDF for type-based casting#21322
adriangb wants to merge 3 commits intoapache:mainfrom
pydantic:cast-to

Conversation

@adriangb
Copy link
Copy Markdown
Contributor

@adriangb adriangb commented Apr 2, 2026

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 fallible try_cast_to_type variant.

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
  • New try_cast_to_type scalar UDF in datafusion/functions/src/core/try_cast_to_type.rs
    • Same as cast_to_type but returns NULL on cast failure instead of erroring
    • simplify() rewrites to Expr::TryCast
    • Output is always nullable
  • Registration of both functions in datafusion/functions/src/core/mod.rs

Are these changes tested?

Yes. New sqllogictest file cast_to_type.slt covering both functions:

  • 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 calls
  • Subquery as second argument
  • Column references as second argument
  • Boolean and date casts
  • Error on invalid cast (cast_to_type) vs NULL on invalid cast (try_cast_to_type)
  • Cross-column type matching

Are there any user-facing changes?

Two new SQL functions:

  • cast_to_type(expression, reference) — casts expression to the type of reference
  • try_cast_to_type(expression, reference) — same, but returns NULL on failure

🤖 Generated with Claude Code

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>
@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Apr 2, 2026
adriangb and others added 2 commits April 2, 2026 10:19
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>
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant