27 #include <unordered_map>
32 namespace CUDF_EXPORT
cudf {
44 class csv_reader_options_builder;
58 std::size_t _byte_range_offset = 0;
60 std::size_t _byte_range_size = 0;
62 std::vector<std::string> _names;
66 bool _mangle_dupe_cols =
true;
71 std::vector<std::string> _use_cols_names;
73 std::vector<int> _use_cols_indexes;
86 char _lineterminator =
'\n';
88 char _delimiter =
',';
90 char _thousands =
'\0';
95 bool _windowslinetermination =
false;
97 bool _delim_whitespace =
false;
99 bool _skipinitialspace =
false;
101 bool _skip_blank_lines =
true;
105 char _quotechar =
'"';
107 bool _doublequote =
true;
110 bool _detect_whitespace_around_quotes =
false;
112 std::vector<std::string> _parse_dates_names;
114 std::vector<int> _parse_dates_indexes;
116 std::vector<std::string> _parse_hex_names;
118 std::vector<int> _parse_hex_indexes;
123 std::variant<std::vector<data_type>, std::map<std::string, data_type>> _dtypes;
125 std::vector<std::string> _true_values{
"True",
"TRUE",
"true"};
127 std::vector<std::string> _false_values{
"False",
"FALSE",
"false"};
129 std::vector<std::string> _na_values;
131 bool _keep_default_na =
true;
133 bool _na_filter =
true;
135 bool _dayfirst =
false;
137 data_type _timestamp_type{type_id::EMPTY};
199 if (_byte_range_size == 0) {
202 return _byte_range_size + get_byte_range_padding();
213 auto const num_names = _names.size();
214 auto const num_dtypes = std::visit([](
auto const& dtypes) {
return dtypes.size(); }, _dtypes);
215 auto const num_columns = std::max(num_dtypes, num_names);
217 auto const max_row_bytes = 16 * 1024;
218 auto const column_bytes = 64;
219 auto const base_padding = 1024;
221 if (num_columns == 0) {
223 return max_row_bytes;
227 return base_padding + num_columns * column_bytes;
235 [[nodiscard]] std::vector<std::string>
const&
get_names()
const {
return _names; }
242 [[nodiscard]] std::string
get_prefix()
const {
return _prefix; }
258 return _use_cols_names;
388 return _detect_whitespace_around_quotes;
398 return _parse_dates_names;
408 return _parse_dates_indexes;
418 return _parse_hex_names;
433 [[nodiscard]] std::variant<std::vector<data_type>, std::map<std::string, data_type>>
const&
444 [[nodiscard]] std::vector<std::string>
const&
get_true_values()
const {
return _true_values; }
451 [[nodiscard]] std::vector<std::string>
const&
get_false_values()
const {
return _false_values; }
458 [[nodiscard]] std::vector<std::string>
const&
get_na_values()
const {
return _na_values; }
502 if ((offset != 0) and ((_skiprows != 0) or (_skipfooter != 0) or (_nrows != -1))) {
504 "当 skiprows、skipfooter 或 nrows 中有有效值时,offset 不能为非零值"
507 _byte_range_offset = offset;
517 if ((size != 0) and ((_skiprows != 0) or (_skipfooter != 0) or (_nrows != -1))) {
519 "如果 skiprows、skipfooter 或 nrows 中的任何值有效,则 range size 不能为非零。"
522 _byte_range_size = size;
530 void set_names(std::vector<std::string> col_names) { _names = std::move(col_names); }
553 _use_cols_names = std::move(col_names);
563 _use_cols_indexes = std::move(col_indices);
573 CUDF_EXPECTS((nrows == 0) or (_skipfooter == 0),
"不能同时使用 `nrows` 和 `skipfooter`");
574 if ((nrows != -1) and ((_byte_range_offset != 0) or (_byte_range_size != 0))) {
576 "如果已设置 range offset 和/或 range size,nrows 不能为非负值");
589 if ((skiprows != 0) and ((_byte_range_offset != 0) or (_byte_range_size != 0))) {
590 CUDF_FAIL(
"如果已设置 range offset 或 range size,skiprows 必须为零",
591 std::invalid_argument);
593 _skiprows = skiprows;
604 "不能同时使用 `nrows` 和 `skipfooter`",
605 std::invalid_argument);
606 if ((skipfooter != 0) and ((_byte_range_offset != 0) or (_byte_range_size != 0))) {
607 CUDF_FAIL(
"如果已设置 range offset 或 range size,skipfooter 必须为零",
608 std::invalid_argument);
611 _skipfooter = skipfooter;
696 CUDF_EXPECTS(quoting == quote_style::MINIMAL || quoting == quote_style::NONE,
697 "引号只支持 MINIMAL 和 NONE。");
730 _parse_dates_names = std::move(col_names);
740 _parse_dates_indexes = std::move(col_indices);
750 _parse_hex_names = std::move(col_names);
758 void set_parse_hex(std::vector<int> col_indices) { _parse_hex_indexes = std::move(col_indices); }
765 void set_dtypes(std::map<std::string, data_type> types) { _dtypes = std::move(types); }
772 void set_dtypes(std::vector<data_type> types) { _dtypes = std::move(types); }
781 _true_values.insert(_true_values.end(), vals.begin(), vals.end());
791 _false_values.insert(_false_values.end(), vals.begin(), vals.end());
801 if ((!vals.empty()) and (!_na_filter)) {
802 CUDF_FAIL(
"禁用 na_filtering 时不能设置 na_values");
805 _na_values = std::move(vals);
822 if (!val) { _na_values.clear(); }
871 options._compression = comp;
907 options._names = std::move(col_names);
919 options._prefix = std::move(pfx);
931 options._mangle_dupe_cols = val;
943 options._use_cols_names = std::move(col_names);
955 options._use_cols_indexes = std::move(col_indices);
1003 options._header = hdr;
1015 options._lineterminator = term;
1027 options._delimiter = delim;
1039 options._thousands = val;
1051 options._decimal = val;
1063 options._comment = val;
1075 options._windowslinetermination = val;
1087 options._delim_whitespace = val;
1099 options._skipinitialspace = val;
1111 options._skip_blank_lines = val;
1123 options._quoting = style;
1135 options._quotechar = ch;
1147 options._doublequote = val;
1160 options._detect_whitespace_around_quotes = val;
1172 options._parse_dates_names = std::move(col_names);
1184 options._parse_dates_indexes = std::move(col_indices);
1196 options._parse_hex_names = std::move(col_names);
1208 options._parse_hex_indexes = std::move(col_indices);
1220 options._dtypes = std::move(types);
1232 options._dtypes = std::move(types);
1244 options._true_values.insert(options._true_values.end(), vals.begin(), vals.end());
1256 options._false_values.insert(options._false_values.end(), vals.begin(), vals.end());
1304 options._dayfirst = val;
1316 options._timestamp_type = type;
1378 std::string _na_rep =
"";
1380 bool _include_header =
true;
1382 size_type _rows_per_chunk = std::numeric_limits<size_type>::max();
1384 std::string _line_terminator =
"\n";
1386 char _inter_column_delimiter =
',';
1388 std::string _true_value = std::string{
"true"};
1390 std::string _false_value = std::string{
"false"};
1392 std::vector<std::string> _names;
1446 [[nodiscard]] std::vector<std::string>
const&
get_names()
const {
return _names; }
1453 [[nodiscard]] std::string
const&
get_na_rep()
const {
return _na_rep; }
1515 void set_names(std::vector<std::string> names) { _names = std::move(names); }
1585 CUDF_EXPECTS(quoting == quote_style::MINIMAL || quoting == quote_style::NONE,
1586 "Only MINIMAL and NONE are supported for quoting.");
1612 : options{sink,
table}
1624 options._names = names;
1636 options._na_rep = val;
1648 options._include_header = val;
1660 options._rows_per_chunk = val;
1672 options._line_terminator = term;
1684 options._inter_column_delimiter = delim;
1696 options._true_value = val;
1708 options._false_value = val;
csv_reader_options_builder & dtypes(std::vector< data_type > types)
设置每列的数据类型。
csv_reader_options_builder & false_values(std::vector< std::string > vals)
设置要识别为布尔假值的附加值。
csv_reader_options_builder & use_cols_names(std::vector< std::string > col_names)
设置要读取的列的名称。
csv_reader_options_builder & doublequote(bool val)
设置值内部的引号是否使用双引号。
csv_reader_options_builder & parse_hex(std::vector< std::string > col_names)
设置要解析为十六进制的列的名称。
csv_reader_options_builder & byte_range_offset(std::size_t offset)
设置从源开始跳过的字节数。
csv_reader_options_builder & delim_whitespace(bool val)
设置是否将空白字符视作字段分隔符。
csv_reader_options_builder & skiprows(size_type skip)
设置从开始跳过的行数。
csv_reader_options_builder & skip_blank_lines(bool val)
设置是忽略空行还是将行值解析为无效。
csv_reader_options && build()
构建完成后移动 csv_reader_options 成员。
csv_reader_options_builder & dtypes(std::map< std::string, data_type > types)
设置每列的数据类型。
csv_reader_options_builder & quotechar(char ch)
设置引用字符。
csv_reader_options_builder & na_values(std::vector< std::string > vals)
设置要识别为 null 值的附加值。
csv_reader_options_builder & true_values(std::vector< std::string > vals)
设置要识别为布尔真值的附加值。
csv_reader_options_builder & decimal(char val)
设置小数点字符。
csv_reader_options_builder & na_filter(bool val)
设置是否禁用 null 过滤器。
csv_reader_options_builder & thousands(char val)
设置数字数据的千位分隔符。
csv_reader_options_builder & parse_hex(std::vector< int > col_indices)
设置要解析为十六进制的列的索引。
csv_reader_options_builder & detect_whitespace_around_quotes(bool val)
设置是否检测被空格包围的引号,例如 "data"。当 _doubleq... 时此标志无效
csv_reader_options_builder & windowslinetermination(bool val)
设置是否将 \r\n 视作行结束符。
csv_reader_options_builder & parse_dates(std::vector< int > col_indices)
设置要读取为 datetime 的列的索引。
csv_reader_options_builder & nrows(size_type rows)
设置要读取的行数。
csv_reader_options_builder & names(std::vector< std::string > col_names)
设置列的名称。
csv_reader_options_builder & timestamp_type(data_type type)
设置所有时间戳列将转换为的 timestamp_type。
csv_reader_options_builder & mangle_dupe_cols(bool val)
设置是否重命名重复的列名称。
csv_reader_options_builder & skipfooter(size_type skip)
设置从末尾跳过的行数。
csv_reader_options_builder()=default
默认构造函数。
csv_reader_options_builder & byte_range_size(std::size_t size)
设置要读取的字节数。
csv_reader_options_builder & keep_default_na(bool val)
设置是否保留内置的默认 NA 值。
csv_reader_options_builder & quoting(quote_style style)
设置引用风格。
csv_reader_options_builder & lineterminator(char term)
设置行结束符。
csv_reader_options_builder & delimiter(char delim)
设置字段分隔符。
csv_reader_options_builder & use_cols_indexes(std::vector< int > col_indices)
设置要读取的列的索引。
csv_reader_options_builder & parse_dates(std::vector< std::string > col_names)
设置要读取为 datetime 的列的名称。
csv_reader_options_builder(source_info src)
从源信息构造。
csv_reader_options_builder & comment(char val)
设置注释行起始字符。
csv_reader_options_builder & compression(compression_type comp)
设置源的压缩格式。
csv_reader_options_builder & header(size_type hdr)
设置头部行索引。
csv_reader_options_builder & dayfirst(bool val)
设置是否将日期解析为 DD/MM 格式而非 MM/DD 格式。
csv_reader_options_builder & prefix(std::string pfx)
设置用于列 ID 的前缀。
csv_reader_options_builder & skipinitialspace(bool val)
设置是否跳过分隔符后的空白字符。
void enable_doublequote(bool val)
设置值内部的引号是否使用双引号。
void set_use_cols_indexes(std::vector< int > col_indices)
设置要读取的列的索引。
size_type get_skiprows() const
返回从开始跳过的行数。
bool is_enabled_delim_whitespace() const
是否将空白字符视作字段分隔符。
std::vector< int > const & get_parse_dates_indexes() const
返回要读取为 datetime 的列的索引。
void set_byte_range_offset(std::size_t offset)
设置从源开始跳过的字节数。
quote_style get_quoting() const
返回引用风格。
void set_parse_dates(std::vector< std::string > col_names)
设置要读取为 datetime 的列的名称。
void set_parse_dates(std::vector< int > col_indices)
设置要读取为 datetime 的列的索引。
bool is_enabled_doublequote() const
值内部的引号是否使用双引号。
char get_delimiter() const
返回字段分隔符。
char get_lineterminator() const
返回行结束符。
csv_reader_options()=default
默认构造函数。
void enable_detect_whitespace_around_quotes(bool val)
设置是否检测被空格包围的引号,例如 "data"。当 _doubleq... 时此标志无效
void set_false_values(std::vector< std::string > vals)
设置要识别为布尔假值的附加值。
void set_decimal(char val)
设置小数点字符。
std::string get_prefix() const
返回用于列 ID 的前缀。
bool is_enabled_detect_whitespace_around_quotes() const
是否检测被空格包围的引号,例如 "data"。当 _doublequote ... 时此标志无效
std::vector< std::string > const & get_na_values() const
返回要识别为 null 值的附加值。
char get_thousands() const
返回数字数据的千位分隔符。
bool is_enabled_mangle_dupe_cols() const
是否重命名重复的列名称。
void set_dtypes(std::map< std::string, data_type > types)
设置每列的数据类型。
std::size_t get_byte_range_size_with_padding() const
返回读取带填充的字节数。
std::vector< int > const & get_parse_hex_indexes() const
返回要读取为十六进制的列的索引。
std::vector< std::string > const & get_false_values() const
返回要识别为布尔假值的附加值。
void enable_dayfirst(bool val)
设置是否将日期解析为 DD/MM 格式而非 MM/DD 格式。
void set_na_values(std::vector< std::string > vals)
设置要识别为 null 值的附加值。
void set_quoting(quote_style quoting)
设置输入 CSV 数据中使用的预期引用风格。
std::variant< std::vector< data_type >, std::map< std::string, data_type > > const & get_dtypes() const
返回每列的数据类型。
void set_timestamp_type(data_type type)
设置所有时间戳列将转换为的 timestamp_type。
std::size_t get_byte_range_offset() const
返回从源开始跳过的字节数。
data_type get_timestamp_type() const
返回所有时间戳列将转换为的 timestamp_type。
bool is_enabled_na_filter() const
是否禁用 null 过滤器。
bool is_enabled_skip_blank_lines() const
是否忽略空行或将行值解析为无效。
char get_comment() const
返回注释行起始字符。
void set_lineterminator(char term)
设置行结束符。
void set_quotechar(char ch)
设置引用字符。
bool is_enabled_windowslinetermination() const
是否将 \r\n 视作行结束符。
void enable_skip_blank_lines(bool val)
设置是忽略空行还是将行值解析为无效。
void enable_windowslinetermination(bool val)
设置是否将 \r\n 视作行结束符。
void set_skiprows(size_type skiprows)
设置从开始跳过的行数。
void set_compression(compression_type comp)
设置源的压缩格式。
void enable_delim_whitespace(bool val)
设置是否将空白字符视作字段分隔符。
std::vector< std::string > const & get_names() const
返回列名称。
void set_dtypes(std::vector< data_type > types)
设置每列的数据类型。
void set_skipfooter(size_type skipfooter)
设置从末尾跳过的行数。
bool is_enabled_dayfirst() const
是否将日期解析为 DD/MM 格式而非 MM/DD 格式。
std::size_t get_byte_range_padding() const
返回读取时填充的字节数。
void set_names(std::vector< std::string > col_names)
设置列的名称。
source_info const & get_source() const
返回源信息。
void enable_keep_default_na(bool val)
设置是否保留内置的默认 NA 值。
std::vector< std::string > const & get_parse_dates_names() const
返回要读取为 datetime 的列的名称。
void set_prefix(std::string pfx)
设置用于列 ID 的前缀。
static csv_reader_options_builder builder(source_info src)
创建一个 csv_reader_options_builder,它将构建 csv_reader_options。
std::size_t get_byte_range_size() const
返回要读取的字节数。
std::vector< int > const & get_use_cols_indexes() const
返回要读取的列的索引。
std::vector< std::string > const & get_use_cols_names() const
返回要读取的列的名称。
void set_use_cols_names(std::vector< std::string > col_names)
设置要读取的列的名称。
compression_type get_compression() const
返回源的压缩格式。
char get_quotechar() const
返回引用字符。
void set_true_values(std::vector< std::string > vals)
设置要识别为布尔真值的附加值。
bool is_enabled_keep_default_na() const
是否保留内置的默认 NA 值。
void set_header(size_type hdr)
设置头部行索引。
char get_decimal() const
返回小数点字符。
std::vector< std::string > const & get_true_values() const
返回识别为布尔真值的额外值。
void set_parse_hex(std::vector< int > col_indices)
设置要解析为十六进制的列的索引。
void set_thousands(char val)
设置数字数据的千位分隔符。
void enable_na_filter(bool val)
设置是否禁用 null 过滤器。
void set_byte_range_size(std::size_t size)
设置要读取的字节数。
void set_delimiter(char delim)
设置字段分隔符。
void enable_mangle_dupe_cols(bool val)
设置是否重命名重复的列名称。
size_type get_nrows() const
返回要读取的行数。
std::vector< std::string > const & get_parse_hex_names() const
返回要读取为十六进制的列名。
void enable_skipinitialspace(bool val)
设置是否跳过分隔符后的空白字符。
size_type get_skipfooter() const
返回从末尾跳过的行数。
void set_nrows(size_type nrows)
设置要读取的行数。
void set_comment(char val)
设置注释行起始字符。
bool is_enabled_skipinitialspace() const
是否跳过分隔符后的空白字符。
size_type get_header() const
返回标题行索引。
void set_parse_hex(std::vector< std::string > col_names)
设置要解析为十六进制的列的名称。
用于构建 writer_csv() 选项的构建器。
csv_writer_options_builder()=default
默认构造函数。
csv_writer_options && build()
构建完成后移动 csv_writer_options 成员。
csv_writer_options_builder & quoting(quote_style quoting)
设置写入器的引用样式。
csv_writer_options_builder & true_value(std::string val)
设置用于 INT8 类型中值不等于 0 的字符串。
csv_writer_options_builder & include_header(bool val)
启用/禁用将标题写入 CSV。
csv_writer_options_builder & na_rep(std::string val)
设置用于空条目的字符串。
csv_writer_options_builder & line_terminator(std::string term)
设置用于分隔行的字符。
csv_writer_options_builder & false_value(std::string val)
设置用于 INT8 类型中值等于 0 的字符串。
csv_writer_options_builder & names(std::vector< std::string > names)
设置可选的列名。
csv_writer_options_builder & inter_column_delimiter(char delim)
设置用于分隔列值的字符。
csv_writer_options_builder(sink_info const &sink, table_view const &table)
从 sink 和 table 构造的构造函数。
csv_writer_options_builder & rows_per_chunk(int val)
设置每次文件写入要处理的最大行数。
void set_table(table_view const &table)
(重新)设置正在写入的表。
void set_rows_per_chunk(size_type val)
设置每次文件写入要处理的最大行数。
void set_quoting(quote_style quoting)
设置写入器的引用样式。
void set_line_terminator(std::string term)
设置用于分隔行的字符。
csv_writer_options()=default
默认构造函数。
void set_inter_column_delimiter(char delim)
设置用于分隔列值的字符。
void set_true_value(std::string val)
设置用于 INT8 类型中值不等于 0 的字符串。
static csv_writer_options_builder builder(sink_info const &sink, table_view const &table)
创建用于创建 csv_writer_options 的构建器。
table_view const & get_table() const
返回将写入输出的表。
void enable_include_header(bool val)
启用/禁用将标题写入 CSV。
bool is_enabled_include_header() const
是否将标题写入 CSV。
void set_na_rep(std::string val)
设置用于空条目的字符串。
char get_inter_column_delimiter() const
返回用于分隔列值的字符。
std::string const & get_false_value() const
返回用于 INT8 类型中值等于 0 的字符串。
std::string const & get_line_terminator() const
返回用于分隔行的字符。
sink_info const & get_sink() const
返回用于写入器输出的 sink。
std::vector< std::string > const & get_names() const
返回列名称。
quote_style get_quoting() const
返回写入器的引用样式。
std::string const & get_true_value() const
返回用于 INT8 类型中值不等于 0 的字符串。
void set_false_value(std::string val)
设置用于 INT8 类型中值等于 0 的字符串。
size_type get_rows_per_chunk() const
返回每次文件写入要处理的最大行数。
std::string const & get_na_rep() const
返回用于空条目的字符串。
void set_names(std::vector< std::string > names)
设置可选的相关列名。
一组大小相同的 cudf::column_view。
size_type num_rows() const noexcept
返回行数。
rmm::cuda_stream_view const get_default_stream()
获取当前默认流。
table_with_metadata read_csv(csv_reader_options options, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
将 CSV 数据集读取到一组列中。
void write_csv(csv_writer_options const &options, rmm::cuda_stream_view stream=cudf::get_default_stream())
将一组列写入 CSV 格式。
rmm::device_async_resource_ref get_current_device_resource_ref()
获取当前设备内存资源引用。
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
#define CUDF_EXPECTS(...)
用于检查(前置)条件宏,当条件被违反时抛出异常。
#define CUDF_FAIL(...)
表示已执行了错误的代码路径。
int32_t size_type
列和表的行索引类型。
(mutable)_table_view 的类定义