docs: clarify difference between try_cast_literal_to_type and ScalarValue::cast_to#22592
Merged
Merged
Conversation
…alue::cast_to These two casts are easy to confuse. Document on each how it differs: - `ScalarValue::cast_to` / `cast_to_with_options`: general-purpose, Arrow cast-kernel based, returns `Result`, and may be lossy or cross-kind (parsing strings to numbers, formatting, truncating decimals, Date -> Timestamp, ...). Points readers to try_cast_literal_to_type for the value-preserving variant. - `try_cast_literal_to_type`: value-preserving cast used to unwrap `CAST(col AS T) = literal` comparisons; returns `None` rather than performing a lossy/cross-kind conversion. Documents exactly which casts it attempts and the timestamp-unit truncation exception. Towards apache#22577. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
alamb
commented
May 28, 2026
| use datafusion_common::ScalarValue; | ||
|
|
||
| /// Convert a literal value from one data type to another | ||
| /// Convert a literal [`ScalarValue`] to `target_type`, preserving the exact value. |
Contributor
Author
There was a problem hiding this comment.
the exact value part was very unclear to me at first
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?
ScalarValuecast implementations).Rationale for this change
I have been confused about the difference between
ScalarValue::cast_toandtry_cast_literal_to_type-- so after some research I would like to make the difference clearerWhat changes are included in this PR?
Document on each function how it differs from the other, so the choice is obvious from the docs alone.
Are these changes tested?
by CI
Are there any user-facing changes?
Doc comments only. No code or API changes.
Partially 🤖 Generated with Claude Code