Skip to content

GH-20395: [R] Allow all cast options to be specified - #55839

Open
ChanithaAbey wants to merge 2 commits into
apache:mainfrom
ChanithaAbey:GH-20395-r-cast-options
Open

ChanithaAbey wants to merge 2 commits into
apache:mainfrom
ChanithaAbey:GH-20395-r-cast-options

Conversation

@ChanithaAbey

@ChanithaAbey ChanithaAbey commented Nov 29, 2026 •

Copy link
Copy Markdown

Rationale for this change

make_cast_options() in r/src/compute.cpp only forwarded three of the six CastOptions fields (allow_int_overflow, allow_time_truncate, allow_float_truncate) to C++, and only when they were TRUE. allow_time_overflow, allow_decimal_truncate and allow_invalid_utf8 were silently ignored, so for example cast(x, int32(), allow_decimal_truncate = TRUE) still errored on decimals with a fractional part, and safe = FALSE did not fully disable safety checks.

What changes are included in this PR?

  • make_cast_options() now forwards all six boolean cast options, and respects an explicit FALSE (previously only TRUE was applied).
  • cast_options(safe = FALSE) now sets all six options to TRUE, matching CastOptions::Unsafe() in C++.
  • Updated the cast_options() documentation to list all options.

Are these changes tested?

Yes. Added tests in test-Array.R covering allow_decimal_truncate, allow_invalid_utf8 and allow_time_overflow (default error, option set to TRUE, safe = FALSE, and explicit FALSE overriding safe = FALSE), plus a test for the list returned by cast_options(). The new cast test fails without the C++ change. Locally (Windows, R 4.6.2), test-Array.R, test-dplyr-funcs-type.R, test-chunked-array.R, test-scalar.R, test-Table.R, test-RecordBatch.R and test-expression.R pass.

Are there any user-facing changes?

Yes:

  • allow_time_overflow, allow_decimal_truncate and allow_invalid_utf8 passed to $cast() / cast() now take effect.
  • safe = FALSE now also allows time overflow, decimal truncation and invalid UTF-8.
  • In dplyr queries, as.integer() and bit64::as.integer64() already passed allow_decimal_truncate = TRUE; that is now honoured, so decimals with a fractional part are truncated instead of raising an error.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

make_cast_options() only forwarded allow_int_overflow,
allow_time_truncate and allow_float_truncate to C++, and only when
they were TRUE. allow_time_overflow, allow_decimal_truncate and
allow_invalid_utf8 were silently ignored.

Forward all six CastOptions fields, honour explicit FALSE values, and
have cast_options(safe = FALSE) enable every option.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[R] Allow all cast options to be specified

1 participant