Skip to content

Commit 202787f

Browse files
authored
GH-46719: [R] Add 32 and 64 bit Decimal types (#46720)
### Rationale for this change 32 and 64 bit Decimal types were added in C++ in #43957 but haven't been implemented in R yet ### What changes are included in this PR? Implements them in R ### Are these changes tested? Yup ### Are there any user-facing changes? Yeah, new types but also the implicit downcasting so we should think about how to communicate this if at all * GitHub Issue: #46719 Authored-by: Nic Crane <thisisnic@gmail.com> Signed-off-by: Nic Crane <thisisnic@gmail.com>
1 parent a974df4 commit 202787f

File tree

17 files changed

+332
-80
lines changed

17 files changed

+332
-80
lines changed

cpp/src/arrow/compute/kernels/vector_selection_filter_internal.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,8 @@ void PopulateFilterKernels(std::vector<SelectionKernelData>* out) {
10961096
{InputType(match::LargeBinaryLike()), plain_filter, BinaryFilterExec},
10971097
{InputType(null()), plain_filter, NullFilterExec},
10981098
{InputType(Type::FIXED_SIZE_BINARY), plain_filter, PrimitiveFilterExec},
1099+
{InputType(Type::DECIMAL32), plain_filter, PrimitiveFilterExec},
1100+
{InputType(Type::DECIMAL64), plain_filter, PrimitiveFilterExec},
10991101
{InputType(Type::DECIMAL128), plain_filter, PrimitiveFilterExec},
11001102
{InputType(Type::DECIMAL256), plain_filter, PrimitiveFilterExec},
11011103
{InputType(Type::DICTIONARY), plain_filter, DictionaryFilterExec},
@@ -1116,6 +1118,8 @@ void PopulateFilterKernels(std::vector<SelectionKernelData>* out) {
11161118
{InputType(match::LargeBinaryLike()), ree_filter, BinaryFilterExec},
11171119
{InputType(null()), ree_filter, NullFilterExec},
11181120
{InputType(Type::FIXED_SIZE_BINARY), ree_filter, PrimitiveFilterExec},
1121+
{InputType(Type::DECIMAL32), ree_filter, PrimitiveFilterExec},
1122+
{InputType(Type::DECIMAL64), ree_filter, PrimitiveFilterExec},
11191123
{InputType(Type::DECIMAL128), ree_filter, PrimitiveFilterExec},
11201124
{InputType(Type::DECIMAL256), ree_filter, PrimitiveFilterExec},
11211125
{InputType(Type::DICTIONARY), ree_filter, DictionaryFilterExec},

r/NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ export(date64)
319319
export(decimal)
320320
export(decimal128)
321321
export(decimal256)
322+
export(decimal32)
323+
export(decimal64)
322324
export(default_memory_pool)
323325
export(dictionary)
324326
export(duration)

r/R/arrowExports.R

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/R/dplyr-funcs-simple.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ common_type <- function(exprs) {
190190

191191
cast_or_parse <- function(x, type) {
192192
to_type_id <- type$id
193-
if (to_type_id %in% c(Type[["DECIMAL128"]], Type[["DECIMAL256"]])) {
193+
if (to_type_id %in% c(Type[["DECIMAL32"]], Type[["DECIMAL64"]], Type[["DECIMAL128"]], Type[["DECIMAL256"]])) {
194194
# TODO: determine the minimum size of decimal (or integer) required to
195195
# accommodate x
196196
# We would like to keep calculations on decimal if that's what the data has

r/R/dplyr-funcs-type.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ register_bindings_type_inspect <- function() {
186186
is.numeric(x) || (inherits(x, "Expression") && x$type_id() %in% Type[c(
187187
"UINT8", "INT8", "UINT16", "INT16", "UINT32", "INT32",
188188
"UINT64", "INT64", "HALF_FLOAT", "FLOAT", "DOUBLE",
189-
"DECIMAL128", "DECIMAL256"
189+
"DECIMAL32", "DECIMAL64", "DECIMAL128", "DECIMAL256"
190190
)])
191191
})
192192
register_binding("base::is.double", function(x) {

r/R/enums.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,23 @@ Type <- enum("Type::type",
8080
LARGE_BINARY = 35L,
8181
LARGE_LIST = 36L,
8282
INTERVAL_MONTH_DAY_NANO = 37L,
83-
RUN_END_ENCODED = 38L
83+
RUN_END_ENCODED = 38L,
84+
STRING_VIEW = 39L,
85+
BINARY_VIEW = 40L,
86+
LIST_VIEW = 41L,
87+
LARGE_LIST_VIEW = 42L,
88+
DECIMAL32 = 43L,
89+
DECIMAL64 = 44L
8490
)
8591

8692
TYPES_WITH_NAN <- Type[c("HALF_FLOAT", "FLOAT", "DOUBLE")]
8793
TYPES_NUMERIC <- Type[
8894
c(
8995
"INT8", "UINT8", "INT16", "UINT16", "INT32", "UINT32",
9096
"INT64", "UINT64", "HALF_FLOAT", "FLOAT", "DOUBLE",
91-
"DECIMAL128", "DECIMAL256"
92-
)
93-
]
97+
"DECIMAL32", "DECIMAL64", "DECIMAL128", "DECIMAL256"
98+
)
99+
]
94100

95101
#' @rdname enums
96102
#' @export

r/R/type.R

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ DecimalType <- R6Class("DecimalType",
300300
)
301301
)
302302

303+
Decimal32Type <- R6Class("Decimal32Type", inherit = DecimalType)
304+
305+
Decimal64Type <- R6Class("Decimal64Type", inherit = DecimalType)
306+
303307
Decimal128Type <- R6Class("Decimal128Type", inherit = DecimalType)
304308

305309
Decimal256Type <- R6Class("Decimal256Type", inherit = DecimalType)
@@ -586,11 +590,29 @@ decimal <- function(precision, scale) {
586590

587591
if (args$precision > 38) {
588592
decimal256(args$precision, args$scale)
589-
} else {
593+
} else if (args$precision > 18) {
590594
decimal128(args$precision, args$scale)
595+
} else if (args$precision > 9) {
596+
decimal64(args$precision, args$scale)
597+
} else {
598+
decimal32(args$precision, args$scale)
591599
}
592600
}
593601

602+
#' @rdname data-type
603+
#' @export
604+
decimal32 <- function(precision, scale) {
605+
args <- check_decimal_args(precision, scale)
606+
Decimal32Type__initialize(args$precision, args$scale)
607+
}
608+
609+
#' @rdname data-type
610+
#' @export
611+
decimal64 <- function(precision, scale) {
612+
args <- check_decimal_args(precision, scale)
613+
Decimal64Type__initialize(args$precision, args$scale)
614+
}
615+
594616
#' @rdname data-type
595617
#' @export
596618
decimal128 <- function(precision, scale) {
@@ -768,6 +790,8 @@ canonical_type_str <- function(type_str) {
768790
time64 = "time64",
769791
null = "null",
770792
timestamp = "timestamp",
793+
decimal32 = "decimal32",
794+
decimal64 = "decimal64",
771795
decimal128 = "decimal128",
772796
decimal256 = "decimal256",
773797
struct = "struct",

r/man/data-type.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/src/array_to_vector.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,12 @@ std::shared_ptr<Converter> Converter::Make(
13131313
return std::make_shared<arrow::r::Converter_Int64>(chunked_array);
13141314
}
13151315

1316+
case Type::DECIMAL32:
1317+
return std::make_shared<arrow::r::Converter_Decimal<Decimal32Type>>(chunked_array);
1318+
1319+
case Type::DECIMAL64:
1320+
return std::make_shared<arrow::r::Converter_Decimal<Decimal64Type>>(chunked_array);
1321+
13161322
case Type::DECIMAL128:
13171323
return std::make_shared<arrow::r::Converter_Decimal<Decimal128Type>>(chunked_array);
13181324

r/src/arrowExports.cpp

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)