File: csv.rst

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 (378 lines) | stat: -rw-r--r-- 15,310 bytes parent folder | download | duplicates (6)
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
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements.  See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership.  The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License.  You may obtain a copy of the License at

..   http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
.. software distributed under the License is distributed on an
.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
.. KIND, either express or implied.  See the License for the
.. specific language governing permissions and limitations
.. under the License.

.. default-domain:: cpp
.. highlight:: cpp

.. cpp:namespace:: arrow::csv

=============================
Reading and Writing CSV files
=============================

Arrow provides a fast CSV reader allowing ingestion of external data
to create Arrow Tables or a stream of Arrow RecordBatches.

.. seealso::
   :ref:`CSV reader/writer API reference <cpp-api-csv>`.

Reading CSV files
=================

Data in a CSV file can either be read in as a single Arrow Table using
:class:`~arrow::csv::TableReader` or streamed as RecordBatches using
:class:`~arrow::csv::StreamingReader`. See :ref:`Tradeoffs <cpp-csv-tradeoffs>` for a
discussion of the tradeoffs between the two methods.

Both these readers require an :class:`arrow::io::InputStream` instance
representing the input file. Their behavior can be customized using a
combination of :class:`~arrow::csv::ReadOptions`,
:class:`~arrow::csv::ParseOptions`, and :class:`~arrow::csv::ConvertOptions`.

TableReader
-----------

.. code-block:: cpp

   #include "arrow/csv/api.h"

   {
      // ...
      arrow::io::IOContext io_context = arrow::io::default_io_context();
      std::shared_ptr<arrow::io::InputStream> input = ...;

      auto read_options = arrow::csv::ReadOptions::Defaults();
      auto parse_options = arrow::csv::ParseOptions::Defaults();
      auto convert_options = arrow::csv::ConvertOptions::Defaults();

      // Instantiate TableReader from input stream and options
      auto maybe_reader =
        arrow::csv::TableReader::Make(io_context,
                                      input,
                                      read_options,
                                      parse_options,
                                      convert_options);
      if (!maybe_reader.ok()) {
        // Handle TableReader instantiation error...
      }
      std::shared_ptr<arrow::csv::TableReader> reader = *maybe_reader;

      // Read table from CSV file
      auto maybe_table = reader->Read();
      if (!maybe_table.ok()) {
        // Handle CSV read error
        // (for example a CSV syntax error or failed type conversion)
      }
      std::shared_ptr<arrow::Table> table = *maybe_table;
   }

StreamingReader
---------------

.. code-block:: cpp

   #include "arrow/csv/api.h"

   {
      // ...
      arrow::io::IOContext io_context = arrow::io::default_io_context();
      std::shared_ptr<arrow::io::InputStream> input = ...;

      auto read_options = arrow::csv::ReadOptions::Defaults();
      auto parse_options = arrow::csv::ParseOptions::Defaults();
      auto convert_options = arrow::csv::ConvertOptions::Defaults();

      // Instantiate StreamingReader from input stream and options
      auto maybe_reader =
        arrow::csv::StreamingReader::Make(io_context,
                                          input,
                                          read_options,
                                          parse_options,
                                          convert_options);
      if (!maybe_reader.ok()) {
        // Handle StreamingReader instantiation error...
      }
      std::shared_ptr<arrow::csv::StreamingReader> reader = *maybe_reader;

      // Set aside a RecordBatch pointer for re-use while streaming
      std::shared_ptr<RecordBatch> batch;

      while (true) {
          // Attempt to read the first RecordBatch
          arrow::Status status = reader->ReadNext(&batch);

          if (!status.ok()) {
            // Handle read error
          }

          if (batch == NULL) {
            // Handle end of file
            break;
          }

          // Do something with the batch
      }
   }

.. _cpp-csv-tradeoffs:

Tradeoffs
---------

The choice between using :class:`~arrow::csv::TableReader` or
:class:`~arrow::csv::StreamingReader` will ultimately depend on the use case
but there are a few tradeoffs to be aware of:

1. **Memory usage:** :class:`~arrow::csv::TableReader` loads all of the data
   into memory at once and, depending on the amount of data, may require
   considerably more memory than :class:`~arrow::csv::StreamingReader` which
   only loads one :class:`~arrow::RecordBatch` at a time. This is likely to be
   the most significant tradeoff for users.
2. **Speed:** When reading the entire contents of a CSV,
   :class:`~arrow::csv::TableReader` will tend to be faster than
   :class:`~arrow::csv::StreamingReader` because it makes better use of
   available cores. See :ref:`Performance <cpp-csv-performance>` for more
   details.
3. **Flexibility:** :class:`~arrow::csv::StreamingReader` might be considered
   less flexible than :class:`~arrow::csv::TableReader` because it performs type
   inference only on the first block that's read in, after which point the types
   are frozen and any data in subsequent blocks that cannot be converted to
   those types will cause an error. Note that this can be remedied either by
   setting :member:`ReadOptions::block_size` to a large enough value or by using
   :member:`ConvertOptions::column_types` to set the desired data types
   explicitly.

Writing CSV files
=================

A CSV file is written to a :class:`~arrow::io::OutputStream`.

.. code-block:: cpp

   #include <arrow/csv/api.h>
   {
       // Oneshot write
       // ...
       std::shared_ptr<arrow::io::OutputStream> output = ...;
       auto write_options = arrow::csv::WriteOptions::Defaults();
       if (WriteCSV(table, write_options, output.get()).ok()) {
           // Handle writer error...
       }
   }
   {
       // Write incrementally
       // ...
       std::shared_ptr<arrow::io::OutputStream> output = ...;
       auto write_options = arrow::csv::WriteOptions::Defaults();
       auto maybe_writer = arrow::csv::MakeCSVWriter(output, schema, write_options);
       if (!maybe_writer.ok()) {
           // Handle writer instantiation error...
       }
       std::shared_ptr<arrow::ipc::RecordBatchWriter> writer = *maybe_writer;

       // Write batches...
       if (!writer->WriteRecordBatch(*batch).ok()) {
           // Handle write error...
       }

       if (!writer->Close().ok()) {
           // Handle close error...
       }
       if (!output->Close().ok()) {
           // Handle file close error...
       }
   }

.. note:: The writer does not yet support all Arrow types.

Column names
============

There are three possible ways to infer column names from the CSV file:

* By default, the column names are read from the first row in the CSV file
* If :member:`ReadOptions::column_names` is set, it forces the column
  names in the table to these values (the first row in the CSV file is
  read as data)
* If :member:`ReadOptions::autogenerate_column_names` is true, column names
  will be autogenerated with the pattern "f0", "f1"... (the first row in the
  CSV file is read as data)

Column selection
================

By default, Arrow reads all columns in the CSV file.  You can narrow the
selection of columns with the :member:`ConvertOptions::include_columns`
option.  If some columns in :member:`ConvertOptions::include_columns`
are missing from the CSV file, an error will be emitted unless
:member:`ConvertOptions::include_missing_columns` is true, in which case
the missing columns are assumed to contain all-null values.

Interaction with column names
-----------------------------

If both :member:`ReadOptions::column_names` and
:member:`ConvertOptions::include_columns` are specified,
the :member:`ReadOptions::column_names` are assumed to map to CSV columns,
and :member:`ConvertOptions::include_columns` is a subset of those column
names that will part of the Arrow Table.

Data types
==========

By default, the CSV reader infers the most appropriate data type for each
column.  Type inference considers the following data types, in order:

* Null
* Int64
* Boolean
* Date32
* Time32 (with seconds unit)
* Timestamp (with seconds unit)
* Timestamp (with nanoseconds unit)
* Float64
* Dictionary<String> (if :member:`ConvertOptions::auto_dict_encode` is true)
* Dictionary<Binary> (if :member:`ConvertOptions::auto_dict_encode` is true)
* String
* Binary

It is possible to override type inference for select columns by setting
the :member:`ConvertOptions::column_types` option.  Explicit data types
can be chosen from the following list:

* Null
* All Integer types
* Float32 and Float64
* Decimal128
* Boolean
* Date32 and Date64
* Time32 and Time64
* Timestamp
* Binary and Large Binary
* String and Large String (with optional UTF8 input validation)
* Fixed-Size Binary
* Duration (numeric strings matching the schema unit, e.g., "60000" for duration[ms])
* Dictionary with index type Int32 and value type one of the following:
  Binary, String, LargeBinary, LargeString,  Int32, UInt32, Int64, UInt64,
  Float32, Float64, Decimal128

Other data types do not support conversion from CSV values and will error out.

Dictionary inference
--------------------

If type inference is enabled and :member:`ConvertOptions::auto_dict_encode`
is true, the CSV reader first tries to convert string-like columns to a
dictionary-encoded string-like array.  It switches to a plain string-like
array when the threshold in :member:`ConvertOptions::auto_dict_max_cardinality`
is reached.

Timestamp inference/parsing
---------------------------

If type inference is enabled, the CSV reader first tries to interpret
string-like columns as timestamps. If all rows have some zone offset
(e.g. ``Z`` or ``+0100``), even if the offsets are inconsistent, then the
inferred type will be UTC timestamp. If no rows have a zone offset, then the
inferred type will be timestamp without timezone. A mix of rows with/without
offsets will result in a string column.

If the type is explicitly specified as a timestamp with/without timezone, then
the reader will error on values without/with zone offsets in that column. Note
that this means it isn't currently possible to have the reader parse a column
of timestamps without zone offsets as local times in a particular timezone;
instead, parse the column as timestamp without timezone, then convert the
values afterwards using the ``assume_timezone`` compute function.

+-------------------+------------------------------+-------------------+
| Specified Type    | Input CSV                    | Result Type       |
+===================+==============================+===================+
| (inferred)        | ``2021-01-01T00:00:00``      | timestamp[s]      |
|                   +------------------------------+-------------------+
|                   | ``2021-01-01T00:00:00Z``     | timestamp[s, UTC] |
|                   +------------------------------+                   |
|                   | ``2021-01-01T00:00:00+0100`` |                   |
|                   +------------------------------+-------------------+
|                   | ::                           | string            |
|                   |                              |                   |
|                   |     2021-01-01T00:00:00      |                   |
|                   |     2021-01-01T00:00:00Z     |                   |
+-------------------+------------------------------+-------------------+
| timestamp[s]      | ``2021-01-01T00:00:00``      | timestamp[s]      |
|                   +------------------------------+-------------------+
|                   | ``2021-01-01T00:00:00Z``     | (error)           |
|                   +------------------------------+                   |
|                   | ``2021-01-01T00:00:00+0100`` |                   |
|                   +------------------------------+                   |
|                   | ::                           |                   |
|                   |                              |                   |
|                   |     2021-01-01T00:00:00      |                   |
|                   |     2021-01-01T00:00:00Z     |                   |
+-------------------+------------------------------+-------------------+
| timestamp[s, UTC] | ``2021-01-01T00:00:00``      | (error)           |
|                   +------------------------------+-------------------+
|                   | ``2021-01-01T00:00:00Z``     | timestamp[s, UTC] |
|                   +------------------------------+                   |
|                   | ``2021-01-01T00:00:00+0100`` |                   |
|                   +------------------------------+-------------------+
|                   | ::                           | (error)           |
|                   |                              |                   |
|                   |     2021-01-01T00:00:00      |                   |
|                   |     2021-01-01T00:00:00Z     |                   |
+-------------------+------------------------------+-------------------+
| timestamp[s,      | ``2021-01-01T00:00:00``      | (error)           |
| America/New_York] +------------------------------+-------------------+
|                   | ``2021-01-01T00:00:00Z``     | timestamp[s,      |
|                   +------------------------------+ America/New_York] |
|                   | ``2021-01-01T00:00:00+0100`` |                   |
|                   +------------------------------+-------------------+
|                   | ::                           | (error)           |
|                   |                              |                   |
|                   |     2021-01-01T00:00:00      |                   |
|                   |     2021-01-01T00:00:00Z     |                   |
+-------------------+------------------------------+-------------------+

Nulls
-----

Null values are recognized from the spellings stored in
:member:`ConvertOptions::null_values`.  The :func:`ConvertOptions::Defaults`
factory method will initialize a number of conventional null spellings such
as ``N/A``.

Character encoding
------------------

CSV files are expected to be encoded in UTF8.  However, non-UTF8 data
is accepted for Binary columns.

Write Options
=============

The format of written CSV files can be customized via :class:`~arrow::csv::WriteOptions`.
Currently few options are available; more will be added in future releases.

.. _cpp-csv-performance:

Performance
===========

By default, :class:`~arrow::csv::TableReader` will parallelize reads in order to
exploit all CPU cores on your machine.  You can change this setting in
:member:`ReadOptions::use_threads`.  A reasonable expectation is at least
100 MB/s per core on a performant desktop or laptop computer (measured in
source CSV bytes, not target Arrow data bytes).