File: data_types.Rmd

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (401 lines) | stat: -rw-r--r-- 19,965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
---
title: "Data types"
description: >
  Learn about fundamental data types in Apache Arrow and how those 
  types are mapped onto corresponding data types in R 
output: rmarkdown::html_vignette
---

Arrow has a rich data type system that includes direct analogs of many R data types, and many data types that do not have a counterpart 
in R. This article describes the Arrow type system, compares it to R data types, and outlines the default mappings used when data are
 transferred from Arrow to R. At the end of the article there are two lookup tables: one describing the default "R to Arrow" type mappings 
 and the other describing the "Arrow to R" mappings.

## Motivating example

To illustrate the conversion that needs to take place, consider the differences between the output when obtain we use `dplyr::glimpse()`
 to inspect the `starwars` data in its original format -- as a data frame in R -- and the output we obtain when we convert it to an Arrow
  Table first by calling `arrow_table()`:

```{r}
library(dplyr, warn.conflicts = FALSE)
library(arrow, warn.conflicts = FALSE)

glimpse(starwars)
glimpse(arrow_table(starwars))
```

The data represented are essentially the same, but the descriptions of the data types for the columns have changed. For example:

- `name` is labelled `<chr>` (character vector) in the data frame; it is labelled `<string>` (a string type, also referred to as utf8 
type) in the Arrow Table 
- `height` is labelled `<int>` (integer vector) in the data frame; it is labelled `<int32>` (32 bit signed integer) in the Arrow Table
- `mass` is labelled `<dbl>` (numeric vector) in the data frame; it is labelled `<double>` (64 bit floating point number) in the Arrow Table

Some of these differences are purely cosmetic: integers in R are in fact 32 bit signed integers, so the underlying data types in Arrow 
and R are direct analogs of one another. In other cases the differences are purely about the implementation: Arrow and R have different
 ways to store a vector of strings, but at a high level of abstraction the R character type and the Arrow string type can be viewed as 
 direct analogs. In some cases, however, there are no clear analogs: while Arrow has an analog of POSIXct (the timestamp type) it does
  not have an analog of POSIXlt; conversely, while R can represent 32 bit signed integers, it does not have an equivalent of a 64 bit 
  unsigned integer.

When the arrow package converts between R data and Arrow data, it will first check to see if a Schema has been provided -- see 
`schema()` for more information -- and if none is available it will attempt to guess the appropriate type by following the default 
mappings. A complete listing of these mappings is provided at the end of the article, but the most common cases are depicted in 
the illustration below:

```{r, echo=FALSE, out.width="100%"}
knitr::include_graphics("./data_types.png")
```

In this image, black boxes refer to R data types and light blue boxes refer to Arrow data types. Directional arrows specify 
conversions (e.g., the bidirectional arrow between the logical R type and the boolean Arrow type means that the logical R 
converts to an Arrow boolean and vice versa). Solid lines indicate that this conversion rule is always the default; dashed lines 
mean that it only sometimes applies (the rules and special cases are described below). 

## Logical/boolean types

Arrow and R both use three-valued logic. In R, logical values can be `TRUE` or `FALSE`, with `NA` used to represent missing data. 
In Arrow, the corresponding boolean type can take values `true`, `false`, or `null`, as shown below:

```{r}
chunked_array(c(TRUE, FALSE, NA), type = boolean()) # default
```

It is not strictly necessary to set `type = boolean()` in this example because the default behavior in arrow is to translate R
 logical vectors to Arrow booleans and vice versa. However, for the sake of clarity we will specify the data types explicitly
 throughout this article. We will likewise use `chunked_array()` to create Arrow data from R objects and `as.vector()` to create 
 R data from Arrow objects, but similar results are obtained if we use other methods. 

## Integer types

Base R natively supports only one type of integer, using 32 bits to represent signed numbers between -2147483648 and 2147483647,
 though R can also support 64 bit integers via the [`bit64`](https://cran.r-project.org/package=bit64) package. Arrow inherits
  signed and unsigned integer types from C++ in 8 bit, 16 bit, 32 bit, and 64 bit versions:

| Description     | Data Type Function | Smallest Value       |        Largest Value |
| --------------- | -----------------: | -------------------: | -------------------: |
| 8 bit unsigned  | `uint8()`          | 0                    |                  255 |
| 16 bit unsigned | `uint16()`         | 0                    |                65535 |
| 32 bit unsigned | `uint32()`         | 0                    |           4294967295 |
| 64 bit unsigned | `uint64()`         | 0                    | 18446744073709551615 |
| 8 bit signed    | `int8()`           | -128                 |                  127 |
| 16 bit signed   | `int16()`          | -32768               |                32767 |
| 32 bit signed   | `int32()`          | -2147483648          |           2147483647 |
| 64 bit signed   | `int64()`          | -9223372036854775808 |  9223372036854775807 |

By default, arrow translates R integers to the int32 type in Arrow, but you can override this by explicitly specifying another integer type:

```{r}
chunked_array(c(10L, 3L, 200L), type = int32()) # default
chunked_array(c(10L, 3L, 200L), type = int64())
```

If the value in R does not fall within the permissible range for the corresponding Arrow type, arrow throws an error:

```{r, error=TRUE}
chunked_array(c(10L, 3L, 200L), type = int8())
```

When translating from Arrow to R, integer types alway translate to R integers unless one of the following exceptions applies:

- If the value of an Arrow uint32 or uint64 falls outside the range allowed for R integers, the result will be a numeric vector in R 
- If the value of an Arrow int64 variable falls outside the range allowed for R integers, the result will be a `bit64::integer64` vector in R
- If the user sets `options(arrow.int64_downcast = FALSE)`, the Arrow int64 type always yields a `bit64::integer64` vector in R
 regardless of the value

## Floating point numeric types

R has one double-precision (64 bit) numeric type, which translates to the Arrow 64 bit floating point type by default. Arrow supports
 both single-precision (32 bit) and double-precision (64 bit) floating point numbers, specified using the `float32()` and `float64()` 
 data type functions. Both of these are translated to doubles in R. Examples are shown below:

```{r}
chunked_array(c(0.1, 0.2, 0.3), type = float64()) # default
chunked_array(c(0.1, 0.2, 0.3), type = float32())

arrow_double <- chunked_array(c(0.1, 0.2, 0.3), type = float64())
as.vector(arrow_double)
```

Note that the Arrow specification also permits half-precision (16 bit) floating point numbers, but these have not yet been implemented. 

## Fixed point decimal types

Arrow also contains `decimal()` data types, in which numeric values are specified in decimal format rather than binary.
Decimals in Arrow come in two varieties, a 128 bit version and a 256 bit version, but in most cases users should be able 
to use the more general `decimal()` data type function rather than the specific `decimal32()`, `decimal64()`, `decimal128()`,
 and `decimal256()` functions. 

The decimal types in Arrow are fixed-precision numbers (rather than floating-point), which means it is necessary to explicitly 
specify the `precision` and `scale` arguments:

- `precision` specifies the number of significant digits to store.
- `scale` specifies the number of digits that should be stored after the decimal point. If you set `scale = 2`, exactly two 
digits will be stored after the decimal point. If you set `scale = 0`, values will be rounded to the nearest whole number. 
Negative scales are also permitted (handy when dealing with extremely large numbers), so `scale = -2` stores the value to the nearest 100.

Because R does not have any way to create decimal types natively, the example below is a little circuitous. First we create 
some floating point numbers as Chunked Arrays, and then explicitly cast these to decimal types within Arrow. 
This is possible because Chunked Array objects possess a `cast()` method:

```{r}
arrow_floating <- chunked_array(c(.01, .1, 1, 10, 100))
arrow_decimals <- arrow_floating$cast(decimal(precision = 5, scale = 2))
arrow_decimals
```

Though not natively used in R, decimal types can be useful in situations where it is especially important to avoid problems that arise
 in floating point arithmetic.

## String/character types

R uses a single character type to represent strings whereas Arrow has two types. In the Arrow C++ library these types are referred to 
as strings and large_strings, but to avoid ambiguity in the arrow R package they are defined using the `utf8()` and `large_utf8()` data
 type functions. The distinction between these two Arrow types is unlikely to be important for R users, though the difference is discussed 
 in the article on [data object layout](./developers/data_object_layout.html). 

The default behavior is to translate R character vectors to the utf8/string type, and to translate both Arrow types to R character vectors:

```{r}
strings <- chunked_array(c("oh", "well", "whatever"))
strings
as.vector(strings)
```

## Factor/dictionary types

The analog of R factors in Arrow is the dictionary type. Factors translate to dictionaries and vice versa. To illustrate this, let's 
create a small factor object in R:

```{r}
fct <- factor(c("cat", "dog", "pig", "dog"))
fct
```

When translated to Arrow, this is the dictionary that results:

```{r}
dict <- chunked_array(fct, type = dictionary())
dict
```

When translated back to R, we recover the original factor:

```{r}
as.vector(dict)
```

Arrow dictionaries are slightly more flexible than R factors: values in a dictionary do not necessarily have to be strings, but labels 
in a factor do. As a consequence, non-string values in an Arrow dictionary are coerced to strings when translated to R.

## Date types

In R, dates are typically represented using the Date class. Internally a Date object is a numeric type whose value counts the number 
of days since the beginning of the Unix epoch (1 January 1970). Arrow supplies two data types that can be used to represent dates:
 the date32 type and the date64 type. The date32 type is similar to the Date class in R: internally it stores a 32 bit integer that 
 counts the number of days since 1 January 1970. The default in arrow is to translate R Date objects to Arrow date32 types:

```{r}
nirvana_album_dates <- as.Date(c("1989-06-15", "1991-09-24", "1993-09-13"))
nirvana_album_dates
nirvana_32 <- chunked_array(nirvana_album_dates, type = date32()) # default
nirvana_32
```

Arrow also supplies a higher-precision date64 type, in which the date is represented as a 64 bit integer that encodes the number of
 *milliseconds* since 1970-01-01 00:00 UTC:

```{r}
nirvana_64 <- chunked_array(nirvana_album_dates, type = date64())
nirvana_64
```

The translation from Arrow to R differs. Internally the date32 type is very similar to an R Date, so these objects are translated to R as Dates:

```{r}
class(as.vector(nirvana_32))
```

However, because date64 types are specified to millisecond-level precision, they are translated to R as POSIXct times to avoid the 
possibility of losing relevant information:

```{r}
class(as.vector(nirvana_64))
```

## Temporal/timestamp types

In R there are two classes used to represent date and time information, POSIXct and POSIXlt. Arrow only has one: the timestamp type. 
Arrow timestamps are loosely analogous to the POSIXct class. Internally, a POSIXct object represents the date with as a numeric variable 
that stores the number of seconds since 1970-01-01 00:00 UTC. Internally, an Arrow timestamp is a 64 bit integer counting the number of
 milliseconds since 1970-01-01 00:00 UTC.

Arrow and R both support timezone information, but display it differently in the printed object. In R, local time is printed with the 
timezone name adjacent to it:

```{r}
sydney_newyear <- as.POSIXct("2000-01-01 00:01", tz = "Australia/Sydney")
sydney_newyear
```

When translated to Arrow, this POSIXct object becomes an Arrow timestamp object. When printed, however, the temporal instant is always 
displayed in UTC rather than local time:

```{r}
sydney_newyear_arrow <- chunked_array(sydney_newyear, type = timestamp())
sydney_newyear_arrow
```

The timezone information is not lost, however, which we can easily see by translating the `sydney_newyear_arrow` object back to an
 R POSIXct object:

```{r}
as.vector(sydney_newyear_arrow)
```

For POSIXlt objects the behaviour is different. Internally a POSIXlt object is a list specifying the "local time" in terms of a 
variety of human-relevant fields. There is no analogous class to this in Arrow, so the default behaviour is to translate it to an Arrow list.

## Time of day types

Base R does not have a class to represent the time of day independent of the date (i.e., it is not possible to specify "3pm" without
 referring to a specific day), but it can be done with the help of the [`hms`](https://hms.tidyverse.org/) package. Internally, 
 hms objects are always stored as the number of seconds since 00:00:00. 

Arrow has two data types for this purposes. For time32 types, data are stored as a 32 bit integer that is interpreted either as the
 number of seconds or the number of milliseconds since 00:00:00. Note the difference between the following:

```{r}
time_of_day <- hms::hms(56, 34, 12)
chunked_array(time_of_day, type = time32(unit = "s"))
chunked_array(time_of_day, type = time32(unit = "ms"))
```

A time64 object is similar, but stores the time of day using a 64 bit integer and can represent the time at higher precision. It is
 possible to choose microseconds (`unit = "us"`) or nanoseconds (`unit = "ns"`), as shown below:

```{r}
chunked_array(time_of_day, type = time64(unit = "us"))
chunked_array(time_of_day, type = time64(unit = "ns"))
```

All versions of time32 and time64 objects in Arrow translate to hms times in R. 

## Duration types

Lengths of time are represented as difftime objects in R. The analogous data type in Arrow is the duration type. A duration type 
is stored as a 64 bit integer, which can represent the number of seconds (the default, `unit = "s"`), milliseconds (`unit = "ms"`),
 microseconds (`unit = "us"`), or nanoseconds (`unit = "ns"`). To illustrate this we'll create a difftime in R corresponding to 278 seconds:

```{r}
len <- as.difftime(278, unit = "secs")
len
```

The translation to Arrow looks like this:

```{r}
chunked_array(len, type = duration(unit = "s")) # default
chunked_array(len, type = duration(unit = "ns"))
```

Regardless of the underlying unit, duration objects in Arrow translate to difftime objects in R. 


## List of default translations

The discussion above covers the most common cases. The two tables in this section provide a more complete list of how arrow
 translates between R data types and Arrow data types. In these table, entries with a `-` are not currently implemented.

### Translations from R to Arrow

| Original R type          | Arrow type after translation |
|--------------------------|------------------------------|
| logical                  | boolean                      |
| integer                  | int32                        |
| double ("numeric")       | float64 ^1^                  |
| character                | utf8 ^2^                     |
| factor                   | dictionary                   |
| raw                      | uint8                        |
| Date                     | date32                       |
| POSIXct                  | timestamp                    |
| POSIXlt                  | struct                       |
| data.frame               | struct                       |
| list ^3^                 | list                         |
| bit64::integer64         | int64                        |
| hms::hms                 | time32                       |
| difftime                 | duration                     |
| vctrs::vctrs_unspecified | null                         |


^1^: `float64` and `double` are the same concept and data type in Arrow C++; 
however, only `float64()` is used in arrow as the function `double()` already 
exists in base R

^2^: If the character vector exceeds 2GB of strings, it will be converted to a 
`large_utf8` Arrow type

^3^: Only lists where all elements are the same type are able to be translated 
to Arrow list type (which is a "list of" some type).

### Translations from Arrow to R

| Original Arrow type | R type after translation     |
|---------------------|------------------------------|
| boolean             | logical                      |
| int8                | integer                      |
| int16               | integer                      |
| int32               | integer                      |
| int64               | integer ^1^                  |
| uint8               | integer                      |
| uint16              | integer                      |
| uint32              | integer ^1^                  |
| uint64              | integer ^1^                  |
| float16             | - ^2^                        |
| float32             | double                       |
| float64             | double                       |
| utf8                | character                    |
| large_utf8          | character                    |
| binary              | arrow_binary ^3^             |
| large_binary        | arrow_large_binary ^3^       |
| fixed_size_binary   | arrow_fixed_size_binary ^3^  |
| date32              | Date                         |
| date64              | POSIXct                      |
| time32              | hms::hms                     |
| time64              | hms::hms                     |
| timestamp           | POSIXct                      |
| duration            | difftime                     |
| decimal             | double                       |
| dictionary          | factor ^4^                   |
| list                | arrow_list ^5^               |
| large_list          | arrow_large_list ^5^         |
| fixed_size_list     | arrow_fixed_size_list ^5^    |
| struct              | data.frame                   |
| null                | vctrs::vctrs_unspecified     |
| map                 | arrow_list ^5^               |
| union               | - ^2^                       |

^1^: These integer types may contain values that exceed the range of R's 
`integer` type (32 bit signed integer). When they do, `uint32` and `uint64` are 
converted to `double` ("numeric") and `int64` is converted to 
`bit64::integer64`. This conversion can be disabled (so that `int64` always
yields a `bit64::integer64` vector) by setting `options(arrow.int64_downcast = FALSE)`.

^2^: Some Arrow data types do not currently have an R equivalent and will raise an error
if cast to or mapped to via a schema.

^3^: `arrow*_binary` classes are implemented as lists of raw vectors. 

^4^: Due to the limitation of R factors, Arrow `dictionary` values are coerced
to string when translated to R if they are not already strings.

^5^: `arrow*_list` classes are implemented as subclasses of `vctrs_list_of` 
with a `ptype` attribute set to what an empty Array of the value type converts to. 



## Further reading 

- To learn more how data types are specified through `schema()` metadata, see the [metadata article](./metadata.html).
- For additional details on data types, see the [data types article](./data_types.html).