@@ -157,8 +157,11 @@ DecimalType <- R6Class("DecimalType",
157157 scale = function () DecimalType__scale(self )
158158 )
159159)
160+
160161Decimal128Type <- R6Class(" Decimal128Type" , inherit = DecimalType )
161162
163+ Decimal256Type <- R6Class(" Decimal256Type" , inherit = DecimalType )
164+
162165NestedType <- R6Class(" NestedType" , inherit = DataType )
163166
164167# ' Apache Arrow data types
@@ -188,7 +191,7 @@ NestedType <- R6Class("NestedType", inherit = DataType)
188191# ' `bit64::integer64` object) by setting `options(arrow.int64_downcast =
189192# ' FALSE)`.
190193# '
191- # ' `decimal128()` creates a `decimal128` type . Arrow decimals are fixed-point
194+ # ' `decimal128()` creates a `Decimal128Type` . Arrow decimals are fixed-point
192195# ' decimal numbers encoded as a scalar integer. The `precision` is the number of
193196# ' significant digits that the decimal type can represent; the `scale` is the
194197# ' number of digits after the decimal point. For example, the number 1234.567
@@ -204,21 +207,30 @@ NestedType <- R6Class("NestedType", inherit = DataType)
204207# ' negative, `scale` causes the number to be expressed using scientific notation
205208# ' and power of 10.
206209# '
207- # ' `decimal()` is identical to `decimal128()`, defined for backward compatibility.
208- # ' Use `decimal128()` as the name is more informative and `decimal()` might be
209- # ' deprecated in the future.
210+ # ' `decimal256()` creates a `Decimal256Type`, which allows for higher maximum
211+ # ' precision. For most use cases, the maximum precision offered by `Decimal128Type`
212+ # ' is sufficient, and it will result in a more compact and more efficient encoding.
213+ # '
214+ # ' #' `decimal()` creates either a `Decimal128Type` or a `Decimal256Type`
215+ # ' depending on the value for `precision`. If `precision` is greater than 38 a
216+ # ' `Decimal256Type` is returned, otherwise a `Decimal128Type`.
217+ # '
218+ # ' Use `decimal128()` or `decimal256()` as the names are more informative than
219+ # ' `decimal()`.
210220# '
211221# ' @param unit For time/timestamp types, the time unit. `time32()` can take
212222# ' either "s" or "ms", while `time64()` can be "us" or "ns". `timestamp()` can
213223# ' take any of those four values.
214224# ' @param timezone For `timestamp()`, an optional time zone string.
215225# ' @param byte_width byte width for `FixedSizeBinary` type.
216226# ' @param list_size list size for `FixedSizeList` type.
217- # ' @param precision For `decimal()`, `decimal128()` the number of significant
218- # ' digits the arrow `decimal` type can represent. The maximum precision for
219- # ' `decimal()` and `decimal128()` is 38 significant digits.
220- # ' @param scale For `decimal()` and `decimal128()`, the number of digits after
221- # ' the decimal point. It can be negative.
227+ # ' @param precision For `decimal()`, `decimal128()`, and `decimal256()` the
228+ # ' number of significant digits the arrow `decimal` type can represent. The
229+ # ' maximum precision for `decimal128()` is 38 significant digits, while for
230+ # ' `decimal256()` it is 76 digits. `decimal()` will use it to choose which
231+ # ' type of decimal to return.
232+ # ' @param scale For `decimal()`, `decimal128()`, and `decimal256()` the number
233+ # ' of digits after the decimal point. It can be negative.
222234# ' @param type For `list_of()`, a data type to make a list-of-type
223235# ' @param ... For `struct()`, a named list of types to define the struct columns
224236# '
@@ -399,25 +411,49 @@ timestamp <- function(unit = c("s", "ms", "us", "ns"), timezone = "") {
399411 Timestamp__initialize(unit , timezone )
400412}
401413
414+ # ' @rdname data-type
415+ # ' @export
416+ decimal <- function (precision , scale ) {
417+ args <- check_decimal_args(precision , scale )
418+
419+ if (args $ precision > 38 ) {
420+ decimal256(args $ precision , args $ scale )
421+ } else {
422+ decimal128(args $ precision , args $ scale )
423+ }
424+ }
425+
402426# ' @rdname data-type
403427# ' @export
404428decimal128 <- function (precision , scale ) {
429+ args <- check_decimal_args(precision , scale )
430+ Decimal128Type__initialize(args $ precision , args $ scale )
431+ }
432+
433+ # ' @rdname data-type
434+ # ' @export
435+ decimal256 <- function (precision , scale ) {
436+ args <- check_decimal_args(precision , scale )
437+ Decimal256Type__initialize(args $ precision , args $ scale )
438+ }
439+
440+ check_decimal_args <- function (precision , scale ) {
405441 if (is.numeric(precision )) {
406- precision <- as.integer(precision )
442+ precision <- vec_cast(precision , to = integer())
443+ vctrs :: vec_assert(precision , size = 1L )
407444 } else {
408- stop(' " precision" must be an integer' , call. = FALSE )
445+ stop(" ` precision` must be an integer" , call. = FALSE )
409446 }
447+
410448 if (is.numeric(scale )) {
411- scale <- as.integer(scale )
449+ scale <- vec_cast(scale , to = integer())
450+ vctrs :: vec_assert(scale , size = 1L )
412451 } else {
413- stop(' " scale" must be an integer' , call. = FALSE )
452+ stop(" ` scale` must be an integer" , call. = FALSE )
414453 }
415- Decimal128Type__initialize(precision , scale )
416- }
417454
418- # ' @rdname data-type
419- # ' @export
420- decimal <- decimal128
455+ list (precision = precision , scale = scale )
456+ }
421457
422458StructType <- R6Class(" StructType" ,
423459 inherit = NestedType ,
@@ -520,6 +556,7 @@ canonical_type_str <- function(type_str) {
520556 null = " null" ,
521557 timestamp = " timestamp" ,
522558 decimal128 = " decimal128" ,
559+ decimal256 = " decimal256" ,
523560 struct = " struct" ,
524561 list_of = " list" ,
525562 list = " list" ,
0 commit comments