Skip to content

docs: clarify difference between try_cast_literal_to_type and ScalarValue::cast_to#22592

Merged
adriangb merged 3 commits into
apache:mainfrom
alamb:alamb/document-cast-differences
May 28, 2026
Merged

docs: clarify difference between try_cast_literal_to_type and ScalarValue::cast_to#22592
adriangb merged 3 commits into
apache:mainfrom
alamb:alamb/document-cast-differences

Conversation

@alamb
Copy link
Copy Markdown
Contributor

@alamb alamb commented May 28, 2026

Which issue does this PR close?

Rationale for this change

I have been confused about the difference between ScalarValue::cast_to and try_cast_literal_to_type -- so after some research I would like to make the difference clearer

What 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

alamb and others added 3 commits May 27, 2026 16:16
…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 alamb added the documentation Improvements or additions to documentation label May 28, 2026
@alamb alamb marked this pull request as ready for review May 28, 2026 14:58
@alamb alamb requested a review from adriangb May 28, 2026 18:46
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.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the exact value part was very unclear to me at first

Copy link
Copy Markdown
Contributor

@adriangb adriangb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning up after me @alamb !

@adriangb adriangb added this pull request to the merge queue May 28, 2026
Merged via the queue into apache:main with commit eedae11 May 28, 2026
38 of 39 checks passed
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants