forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcsv_transformer.cpp
More file actions
173 lines (156 loc) · 6.71 KB
/
Copy pathvcsv_transformer.cpp
File metadata and controls
173 lines (156 loc) · 6.71 KB
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
// 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.
#include "vec/runtime/vcsv_transformer.h"
#include <glog/logging.h>
#include <cstdlib>
#include <cstring>
#include "common/cast_set.h"
#include "common/status.h"
#include "io/fs/file_writer.h"
#include "runtime/primitive_type.h"
#include "runtime/types.h"
#include "util/faststring.h"
#include "vec/columns/column_string.h"
#include "vec/common/string_buffer.hpp"
#include "vec/core/column_with_type_and_name.h"
#include "vec/data_types/serde/data_type_serde.h"
#include "vec/exec/format/csv/csv_reader.h"
#include "vec/exprs/vexpr.h"
#include "vec/exprs/vexpr_context.h"
namespace doris::vectorized {
#include "common/compile_check_begin.h"
static const unsigned char bom[] = {0xEF, 0xBB, 0xBF};
VCSVTransformer::VCSVTransformer(RuntimeState* state, doris::io::FileWriter* file_writer,
const VExprContextSPtrs& output_vexpr_ctxs,
bool output_object_data, std::string_view header_type,
std::string_view header, std::string_view column_separator,
std::string_view line_delimiter, bool with_bom,
TFileCompressType::type compress_type,
const THiveSerDeProperties* hive_serde_properties)
: VFileFormatTransformer(state, output_vexpr_ctxs, output_object_data),
_column_separator(column_separator),
_line_delimiter(line_delimiter),
_file_writer(file_writer),
_with_bom(with_bom),
_compress_type(compress_type),
_is_text_format(hive_serde_properties != nullptr) {
if (!header.empty()) {
_csv_header = header;
if (header_type == BeConsts::CSV_WITH_NAMES_AND_TYPES) {
_csv_header += _gen_csv_header_types();
}
} else {
_csv_header = "";
}
if (_is_text_format) {
_options.field_delim = hive_serde_properties->field_delim;
_options.collection_delim = hive_serde_properties->collection_delim[0];
_options.map_key_delim = hive_serde_properties->mapkv_delim[0];
if (hive_serde_properties->__isset.escape_char) {
_options.escape_char = hive_serde_properties->escape_char[0];
}
_options.null_format = hive_serde_properties->null_format.data();
_options.null_len = cast_set<int>(hive_serde_properties->null_format.length());
// The list of separators + escapeChar are the bytes required to be escaped.
if (_options.escape_char != 0) {
_options.need_escape[_options.escape_char & 0xff] = true;
}
for (int i = 0; i <= 153; i++) {
_options.need_escape[_options.get_collection_delimiter(i) & 0xff] = true;
}
}
}
Status VCSVTransformer::open() {
RETURN_IF_ERROR(get_block_compression_codec(_compress_type, &_compress_codec));
if (_with_bom) {
if (_compress_codec) {
return Status::InternalError("compressed csv with bom is not supported yet");
}
RETURN_IF_ERROR(
_file_writer->append(Slice(reinterpret_cast<const char*>(bom), sizeof(bom))));
}
if (!_csv_header.empty()) {
if (_compress_codec) {
return Status::InternalError("compressed csv with header is not supported yet");
}
return _file_writer->append(Slice(_csv_header.data(), _csv_header.size()));
}
return Status::OK();
}
int64_t VCSVTransformer::written_len() {
return _file_writer->bytes_appended();
}
Status VCSVTransformer::close() {
return _file_writer->close();
}
Status VCSVTransformer::write(const Block& block) {
auto ser_col = ColumnString::create();
ser_col->reserve(block.columns());
VectorBufferWriter buffer_writer(*ser_col.get());
for (int i = 0; i < block.rows(); i++) {
for (size_t col_id = 0; col_id < block.columns(); col_id++) {
if (col_id != 0) {
buffer_writer.write(_column_separator.data(), _column_separator.size());
}
Status st = _is_text_format ? _serdes[col_id]->serialize_one_cell_to_hive_text(
*(block.get_by_position(col_id).column), i,
buffer_writer, _options)
: _serdes[col_id]->serialize_one_cell_to_json(
*(block.get_by_position(col_id).column), i,
buffer_writer, _options);
if (!st.ok()) {
// VectorBufferWriter must do commit before deconstruct,
// or it may throw DCHECK failure.
buffer_writer.commit();
return st;
}
}
buffer_writer.write(_line_delimiter.data(), _line_delimiter.size());
buffer_writer.commit();
}
return _flush_plain_text_outstream(*ser_col.get());
}
Status VCSVTransformer::_flush_plain_text_outstream(ColumnString& ser_col) {
if (ser_col.byte_size() == 0) {
return Status::OK();
}
Slice append_data(ser_col.get_chars().data(), ser_col.get_chars().size());
if (_compress_codec) {
faststring compressed_data;
RETURN_IF_ERROR(_compress_codec->compress(append_data, &compressed_data));
RETURN_IF_ERROR(
_file_writer->append(Slice(compressed_data.data(), compressed_data.size())));
} else {
RETURN_IF_ERROR(_file_writer->append(append_data));
}
// clear the stream
ser_col.clear();
return Status::OK();
}
std::string VCSVTransformer::_gen_csv_header_types() {
std::string types;
int num_columns = (int)_output_vexpr_ctxs.size();
for (int i = 0; i < num_columns; ++i) {
types += type_to_string(_output_vexpr_ctxs[i]->root()->data_type()->get_primitive_type());
if (i < num_columns - 1) {
types += _column_separator;
}
}
types += _line_delimiter;
return types;
}
} // namespace doris::vectorized