libcudf  24.02.00
io/json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "types.hpp"
20 
22 #include <cudf/types.hpp>
23 
25 
26 #include <map>
27 #include <string>
28 #include <variant>
29 #include <vector>
30 
31 namespace cudf {
32 namespace io {
39 class json_reader_options_builder;
40 
50 
54  std::map<std::string, schema_element> child_types;
55 };
56 
61  FAIL,
63 };
64 
89  source_info _source;
90 
91  // Data types of the column; empty to infer dtypes
92  std::variant<std::vector<data_type>,
93  std::map<std::string, data_type>,
94  std::map<std::string, schema_element>>
95  _dtypes;
96  // Specify the compression format of the source or infer from file extension
98 
99  // Read the file as a json object per line
100  bool _lines = false;
101  // Parse mixed types as a string column
102  bool _mixed_types_as_string = false;
103 
104  // Bytes to skip from the start
105  size_t _byte_range_offset = 0;
106  // Bytes to read; always reads complete rows
107  size_t _byte_range_size = 0;
108 
109  // Whether to parse dates as DD/MM versus MM/DD
110  bool _dayfirst = false;
111 
112  // Whether to use the legacy reader
113  bool _legacy = false;
114 
115  // Whether to keep the quote characters of string values
116  bool _keep_quotes = false;
117 
118  // Normalize single quotes
119  bool _normalize_single_quotes = false;
120 
121  // Whether to recover after an invalid JSON line
123 
129  explicit json_reader_options(source_info src) : _source{std::move(src)} {}
130 
132 
133  public:
139  json_reader_options() = default;
140 
148 
154  [[nodiscard]] source_info const& get_source() const { return _source; }
155 
161  std::variant<std::vector<data_type>,
162  std::map<std::string, data_type>,
163  std::map<std::string, schema_element>> const&
164  get_dtypes() const
165  {
166  return _dtypes;
167  }
168 
174  compression_type get_compression() const { return _compression; }
175 
181  size_t get_byte_range_offset() const { return _byte_range_offset; }
182 
188  size_t get_byte_range_size() const { return _byte_range_size; }
189 
196  {
197  if (_byte_range_size == 0) {
198  return 0;
199  } else {
200  return _byte_range_size + get_byte_range_padding();
201  }
202  }
203 
209  size_t get_byte_range_padding() const
210  {
211  auto const num_columns = std::visit([](auto const& dtypes) { return dtypes.size(); }, _dtypes);
212 
213  auto const max_row_bytes = 16 * 1024; // 16KB
214  auto const column_bytes = 64;
215  auto const base_padding = 1024; // 1KB
216 
217  if (num_columns == 0) {
218  // Use flat size if the number of columns is not known
219  return max_row_bytes;
220  }
221 
222  // Expand the size based on the number of columns, if available
223  return base_padding + num_columns * column_bytes;
224  }
225 
231  bool is_enabled_lines() const { return _lines; }
232 
238  bool is_enabled_mixed_types_as_string() const { return _mixed_types_as_string; }
239 
245  bool is_enabled_dayfirst() const { return _dayfirst; }
246 
252  bool is_enabled_legacy() const { return _legacy; }
253 
259  bool is_enabled_keep_quotes() const { return _keep_quotes; }
260 
266  bool is_enabled_normalize_single_quotes() const { return _normalize_single_quotes; }
267 
273  json_recovery_mode_t recovery_mode() const { return _recovery_mode; }
274 
280  void set_dtypes(std::vector<data_type> types) { _dtypes = std::move(types); }
281 
287  void set_dtypes(std::map<std::string, data_type> types) { _dtypes = std::move(types); }
288 
294  void set_dtypes(std::map<std::string, schema_element> types) { _dtypes = std::move(types); }
295 
301  void set_compression(compression_type comp_type) { _compression = comp_type; }
302 
308  void set_byte_range_offset(size_type offset) { _byte_range_offset = offset; }
309 
315  void set_byte_range_size(size_type size) { _byte_range_size = size; }
316 
322  void enable_lines(bool val) { _lines = val; }
323 
329  void enable_mixed_types_as_string(bool val) { _mixed_types_as_string = val; }
330 
336  void enable_dayfirst(bool val) { _dayfirst = val; }
337 
343  void enable_legacy(bool val) { _legacy = val; }
344 
351  void enable_keep_quotes(bool val) { _keep_quotes = val; }
352 
359  void enable_normalize_single_quotes(bool val) { _normalize_single_quotes = val; }
360 
366  void set_recovery_mode(json_recovery_mode_t val) { _recovery_mode = val; }
367 };
368 
373  json_reader_options options;
374 
375  public:
381  explicit json_reader_options_builder() = default;
382 
388  explicit json_reader_options_builder(source_info src) : options{std::move(src)} {}
389 
396  json_reader_options_builder& dtypes(std::vector<data_type> types)
397  {
398  options._dtypes = std::move(types);
399  return *this;
400  }
401 
408  json_reader_options_builder& dtypes(std::map<std::string, data_type> types)
409  {
410  options._dtypes = std::move(types);
411  return *this;
412  }
413 
420  json_reader_options_builder& dtypes(std::map<std::string, schema_element> types)
421  {
422  options._dtypes = std::move(types);
423  return *this;
424  }
425 
433  {
434  options._compression = comp_type;
435  return *this;
436  }
437 
445  {
446  options._byte_range_offset = offset;
447  return *this;
448  }
449 
457  {
458  options._byte_range_size = size;
459  return *this;
460  }
461 
469  {
470  options._lines = val;
471  return *this;
472  }
473 
481  {
482  options._mixed_types_as_string = val;
483  return *this;
484  }
485 
493  {
494  options._dayfirst = val;
495  return *this;
496  }
497 
505  {
506  options._legacy = val;
507  return *this;
508  }
509 
518  {
519  options._keep_quotes = val;
520  return *this;
521  }
522 
531  {
532  options._normalize_single_quotes = val;
533  return *this;
534  }
535 
543  {
544  options._recovery_mode = val;
545  return *this;
546  }
547 
551  operator json_reader_options&&() { return std::move(options); }
552 
560  json_reader_options&& build() { return std::move(options); }
561 };
562 
581  json_reader_options options,
584  // end of group
586 
597 
602  // Specify the sink to use for writer output
603  sink_info _sink;
604  // Set of columns to output
605  table_view _table;
606  // string to use for null entries
607  std::string _na_rep = "";
608  // Indicates whether to output nulls as 'null' or exclude the field
609  bool _include_nulls = false;
610  // Indicates whether to use JSON lines for records format
611  bool _lines = false;
612  // maximum number of rows to write in each chunk (limits memory use)
613  size_type _rows_per_chunk = std::numeric_limits<size_type>::max();
614  // string to use for values != 0 in INT8 types (default 'true')
615  std::string _true_value = std::string{"true"};
616  // string to use for values == 0 in INT8 types (default 'false')
617  std::string _false_value = std::string{"false"};
618  // Names of all columns; if empty, writer will generate column names
619  std::optional<table_metadata> _metadata; // Optional column names
620 
627  explicit json_writer_options(sink_info const& sink, table_view const& table)
628  : _sink(sink), _table(table), _rows_per_chunk(table.num_rows())
629  {
630  }
631 
633 
634  public:
640  explicit json_writer_options() = default;
641 
651 
657  [[nodiscard]] sink_info const& get_sink() const { return _sink; }
658 
664  [[nodiscard]] table_view const& get_table() const { return _table; }
665 
671  [[nodiscard]] std::optional<table_metadata> const& get_metadata() const { return _metadata; }
672 
678  [[nodiscard]] std::string const& get_na_rep() const { return _na_rep; }
679 
685  [[nodiscard]] bool is_enabled_include_nulls() const { return _include_nulls; }
686 
692  [[nodiscard]] bool is_enabled_lines() const { return _lines; }
693 
699  [[nodiscard]] size_type get_rows_per_chunk() const { return _rows_per_chunk; }
700 
706  [[nodiscard]] std::string const& get_true_value() const { return _true_value; }
707 
713  [[nodiscard]] std::string const& get_false_value() const { return _false_value; }
714 
715  // Setter
716 
722  void set_table(table_view tbl) { _table = tbl; }
723 
729  void set_metadata(table_metadata metadata) { _metadata = std::move(metadata); }
730 
736  void set_na_rep(std::string val) { _na_rep = std::move(val); }
737 
743  void enable_include_nulls(bool val) { _include_nulls = val; }
744 
750  void enable_lines(bool val) { _lines = val; }
751 
757  void set_rows_per_chunk(size_type val) { _rows_per_chunk = val; }
758 
764  void set_true_value(std::string val) { _true_value = std::move(val); }
765 
771  void set_false_value(std::string val) { _false_value = std::move(val); }
772 };
773 
778  json_writer_options options;
779 
780  public:
786  explicit json_writer_options_builder() = default;
787 
795  : options{sink, table}
796  {
797  }
798 
806  {
807  options._table = tbl;
808  return *this;
809  }
810 
818  {
819  options._metadata = std::move(metadata);
820  return *this;
821  }
822 
830  {
831  options._na_rep = std::move(val);
832  return *this;
833  };
834 
842  {
843  options._include_nulls = val;
844  return *this;
845  }
846 
854  {
855  options._lines = val;
856  return *this;
857  }
858 
866  {
867  options._rows_per_chunk = val;
868  return *this;
869  }
870 
878  {
879  options._true_value = std::move(val);
880  return *this;
881  }
882 
890  {
891  options._false_value = std::move(val);
892  return *this;
893  }
894 
898  operator json_writer_options&&() { return std::move(options); }
899 
907  json_writer_options&& build() { return std::move(options); }
908 };
909 
928 void write_json(json_writer_options const& options,
931  // end of group
933 } // namespace io
934 } // namespace cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:241
Builds settings to use for read_json().
Definition: io/json.hpp:372
json_reader_options_builder & normalize_single_quotes(bool val)
Set whether the reader should normalize single quotes around strings.
Definition: io/json.hpp:530
json_reader_options_builder & keep_quotes(bool val)
Set whether the reader should keep quotes of string values.
Definition: io/json.hpp:517
json_reader_options_builder & dayfirst(bool val)
Set whether to parse dates as DD/MM versus MM/DD.
Definition: io/json.hpp:492
json_reader_options_builder & recovery_mode(json_recovery_mode_t val)
Specifies the JSON reader's behavior on invalid JSON lines.
Definition: io/json.hpp:542
json_reader_options_builder & lines(bool val)
Set whether to read the file as a json object per line.
Definition: io/json.hpp:468
json_reader_options_builder & dtypes(std::vector< data_type > types)
Set data types for columns to be read.
Definition: io/json.hpp:396
json_reader_options && build()
move json_reader_options member once it's built.
Definition: io/json.hpp:560
json_reader_options_builder & mixed_types_as_string(bool val)
Set whether to parse mixed types as a string column.
Definition: io/json.hpp:480
json_reader_options_builder & compression(compression_type comp_type)
Set the compression type.
Definition: io/json.hpp:432
json_reader_options_builder(source_info src)
Constructor from source info.
Definition: io/json.hpp:388
json_reader_options_builder & byte_range_size(size_type size)
Set number of bytes to read.
Definition: io/json.hpp:456
json_reader_options_builder & dtypes(std::map< std::string, schema_element > types)
Set data types for columns to be read.
Definition: io/json.hpp:420
json_reader_options_builder & legacy(bool val)
Set whether to use the legacy reader.
Definition: io/json.hpp:504
json_reader_options_builder & byte_range_offset(size_type offset)
Set number of bytes to skip from source start.
Definition: io/json.hpp:444
json_reader_options_builder()=default
Default constructor.
json_reader_options_builder & dtypes(std::map< std::string, data_type > types)
Set data types for columns to be read.
Definition: io/json.hpp:408
Input arguments to the read_json interface.
Definition: io/json.hpp:88
void enable_mixed_types_as_string(bool val)
Set whether to parse mixed types as a string column.
Definition: io/json.hpp:329
void set_compression(compression_type comp_type)
Set the compression type.
Definition: io/json.hpp:301
void set_dtypes(std::vector< data_type > types)
Set data types for columns to be read.
Definition: io/json.hpp:280
void enable_normalize_single_quotes(bool val)
Set whether the reader should enable normalization of single quotes around strings.
Definition: io/json.hpp:359
bool is_enabled_keep_quotes() const
Whether the reader should keep quotes of string values.
Definition: io/json.hpp:259
size_t get_byte_range_offset() const
Returns number of bytes to skip from source start.
Definition: io/json.hpp:181
source_info const & get_source() const
Returns source info.
Definition: io/json.hpp:154
void enable_legacy(bool val)
Set whether to use the legacy reader.
Definition: io/json.hpp:343
void set_dtypes(std::map< std::string, data_type > types)
Set data types for columns to be read.
Definition: io/json.hpp:287
bool is_enabled_lines() const
Whether to read the file as a json object per line.
Definition: io/json.hpp:231
void set_byte_range_size(size_type size)
Set number of bytes to read.
Definition: io/json.hpp:315
bool is_enabled_mixed_types_as_string() const
Whether to parse mixed types as a string column.
Definition: io/json.hpp:238
json_reader_options()=default
Default constructor.
void enable_dayfirst(bool val)
Set whether to parse dates as DD/MM versus MM/DD.
Definition: io/json.hpp:336
std::variant< std::vector< data_type >, std::map< std::string, data_type >, std::map< std::string, schema_element > > const & get_dtypes() const
Returns data types of the columns.
Definition: io/json.hpp:164
size_t get_byte_range_size_with_padding() const
Returns number of bytes to read with padding.
Definition: io/json.hpp:195
void set_recovery_mode(json_recovery_mode_t val)
Specifies the JSON reader's behavior on invalid JSON lines.
Definition: io/json.hpp:366
void enable_lines(bool val)
Set whether to read the file as a json object per line.
Definition: io/json.hpp:322
void enable_keep_quotes(bool val)
Set whether the reader should keep quotes of string values.
Definition: io/json.hpp:351
bool is_enabled_normalize_single_quotes() const
Whether the reader should normalize single quotes around strings.
Definition: io/json.hpp:266
compression_type get_compression() const
Returns compression format of the source.
Definition: io/json.hpp:174
void set_dtypes(std::map< std::string, schema_element > types)
Set data types for a potentially nested column hierarchy.
Definition: io/json.hpp:294
size_t get_byte_range_size() const
Returns number of bytes to read.
Definition: io/json.hpp:188
json_recovery_mode_t recovery_mode() const
Queries the JSON reader's behavior on invalid JSON lines.
Definition: io/json.hpp:273
static json_reader_options_builder builder(source_info src)
create json_reader_options_builder which will build json_reader_options.
void set_byte_range_offset(size_type offset)
Set number of bytes to skip from source start.
Definition: io/json.hpp:308
bool is_enabled_legacy() const
Whether the legacy reader should be used.
Definition: io/json.hpp:252
bool is_enabled_dayfirst() const
Whether to parse dates as DD/MM versus MM/DD.
Definition: io/json.hpp:245
size_t get_byte_range_padding() const
Returns number of bytes to pad when reading.
Definition: io/json.hpp:209
Builder to build options for writer_json()
Definition: io/json.hpp:777
json_writer_options_builder & include_nulls(bool val)
Enables/Disables output of nulls as 'null'.
Definition: io/json.hpp:841
json_writer_options_builder & table(table_view tbl)
Sets table to be written to output.
Definition: io/json.hpp:805
json_writer_options_builder()=default
Default constructor.
json_writer_options_builder & rows_per_chunk(int val)
Sets maximum number of rows to process for each file write.
Definition: io/json.hpp:865
json_writer_options_builder & true_value(std::string val)
Sets string used for values != 0 in INT8 types.
Definition: io/json.hpp:877
json_writer_options_builder & false_value(std::string val)
Sets string used for values == 0 in INT8 types.
Definition: io/json.hpp:889
json_writer_options_builder(sink_info const &sink, table_view const &table)
Constructor from sink and table.
Definition: io/json.hpp:794
json_writer_options_builder & na_rep(std::string val)
Sets string to used for null entries.
Definition: io/json.hpp:829
json_writer_options_builder & metadata(table_metadata metadata)
Sets optional metadata (with column names).
Definition: io/json.hpp:817
json_writer_options && build()
move json_writer_options member once it's built.
Definition: io/json.hpp:907
json_writer_options_builder & lines(bool val)
Enables/Disables JSON lines for records format.
Definition: io/json.hpp:853
Settings to use for write_json().
Definition: io/json.hpp:601
table_view const & get_table() const
Returns table that would be written to output.
Definition: io/json.hpp:664
void set_false_value(std::string val)
Sets string used for values == 0 in INT8 types.
Definition: io/json.hpp:771
void enable_include_nulls(bool val)
Enables/Disables output of nulls as 'null'.
Definition: io/json.hpp:743
bool is_enabled_include_nulls() const
Whether to output nulls as 'null'.
Definition: io/json.hpp:685
void enable_lines(bool val)
Enables/Disables JSON lines for records format.
Definition: io/json.hpp:750
void set_na_rep(std::string val)
Sets string to used for null entries.
Definition: io/json.hpp:736
static json_writer_options_builder builder(sink_info const &sink, table_view const &table)
Create builder to create json_writer_options.
json_writer_options()=default
Default constructor.
void set_true_value(std::string val)
Sets string used for values != 0 in INT8 types.
Definition: io/json.hpp:764
sink_info const & get_sink() const
Returns sink used for writer output.
Definition: io/json.hpp:657
void set_rows_per_chunk(size_type val)
Sets maximum number of rows to process for each file write.
Definition: io/json.hpp:757
std::string const & get_true_value() const
Returns string used for values != 0 in INT8 types.
Definition: io/json.hpp:706
void set_table(table_view tbl)
Sets table to be written to output.
Definition: io/json.hpp:722
std::string const & get_false_value() const
Returns string used for values == 0 in INT8 types.
Definition: io/json.hpp:713
bool is_enabled_lines() const
Whether to use JSON lines for records format.
Definition: io/json.hpp:692
size_type get_rows_per_chunk() const
Returns maximum number of rows to process for each file write.
Definition: io/json.hpp:699
std::optional< table_metadata > const & get_metadata() const
Returns metadata information.
Definition: io/json.hpp:671
std::string const & get_na_rep() const
Returns string to used for null entries.
Definition: io/json.hpp:678
void set_metadata(table_metadata metadata)
Sets metadata.
Definition: io/json.hpp:729
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:187
A set of cudf::column's of the same size.
Definition: table.hpp:40
size_type num_rows() const noexcept
Returns the number of rows.
Definition: table.hpp:93
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
table_with_metadata read_json(json_reader_options options, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Reads a JSON dataset into a set of columns.
json_recovery_mode_t
Control the error recovery behavior of the json parser.
Definition: io/json.hpp:60
@ RECOVER_WITH_NULL
Recovers from an error, replacing invalid records with null.
@ FAIL
Does not recover from an error when encountering an invalid format.
compression_type
Compression algorithms.
Definition: io/types.hpp:56
@ AUTO
Automatically detect or select compression format.
void write_json(json_writer_options const &options, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Writes a set of columns to JSON format.
device_memory_resource * get_current_device_resource()
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:93
cuDF interfaces
Definition: aggregation.hpp:34
Allows specifying the target types for nested JSON data via json_reader_options' set_dtypes method.
Definition: io/json.hpp:45
data_type type
The type that this column should be converted to.
Definition: io/json.hpp:49
std::map< std::string, schema_element > child_types
Allows specifying this column's child columns target type.
Definition: io/json.hpp:54
Destination information for write interfaces.
Definition: io/types.hpp:469
Source information for read interfaces.
Definition: io/types.hpp:294
Table metadata returned by IO readers.
Definition: io/types.hpp:237
Table with table metadata used by io readers to return the metadata by value.
Definition: io/types.hpp:249
Class definitions for (mutable)_table_view
Type declarations for libcudf.