-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextScanOperator.hpp
349 lines (303 loc) · 11.9 KB
/
TextScanOperator.hpp
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
/**
* 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.
**/
#ifndef QUICKSTEP_RELATIONAL_OPERATORS_TEXT_SCAN_OPERATOR_HPP_
#define QUICKSTEP_RELATIONAL_OPERATORS_TEXT_SCAN_OPERATOR_HPP_
#include <cctype>
#include <cstddef>
#include <exception>
#include <string>
#include <vector>
#include "catalog/CatalogRelation.hpp"
#include "catalog/CatalogTypedefs.hpp"
#include "query_execution/QueryContext.hpp"
#include "relational_operators/RelationalOperator.hpp"
#include "relational_operators/WorkOrder.hpp"
#include "types/containers/Tuple.hpp"
#include "utility/BulkIoConfiguration.hpp"
#include "utility/Macros.hpp"
#include "glog/logging.h"
#include "tmb/id_typedefs.h"
namespace tmb { class MessageBus; }
namespace quickstep {
class CatalogRelationSchema;
class InsertDestination;
class StorageManager;
class WorkOrderProtosContainer;
class WorkOrdersContainer;
namespace serialization { class WorkOrder; }
/** \addtogroup RelationalOperators
* @{
*/
/**
* @brief Exception thrown when a text file can't be opened for reading.
**/
class TextScanReadError : public std::exception {
public:
explicit TextScanReadError(const std::string &filename)
: message_("TextScanReadError: Unable to read file ") {
message_.append(filename);
message_.append(" for text scan");
}
~TextScanReadError() throw() {}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when improperly formatted data is encountered while
* scanning a text file.
**/
class TextScanFormatError : public std::exception {
public:
explicit TextScanFormatError(const std::string reason)
: message_("TextScanFormatError: Malformed data encountered during text scan (") {
message_.append(reason);
message_.push_back(')');
}
~TextScanFormatError() throw() {}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief An operator which reads tuples from a text file and inserts them into
* a relation.
**/
class TextScanOperator : public RelationalOperator {
public:
/**
* @brief Constructor.
*
* @param query_id The ID of the query to which this operator belongs.
* @param file_pattern The glob-like file pattern of the sources to load. The
* pattern could include * (wildcard for multiple chars) and ?
* (wildcard for single char). It defaults to single file load, if a
* file is specified.
* @param options The options that specify the detailed format of the input
file(s).
* @param output_relation The output relation.
* @param output_destination_index The index of the InsertDestination in the
* QueryContext to insert tuples.
**/
TextScanOperator(const std::size_t query_id,
const std::string &file_pattern,
const BulkIoConfigurationPtr &options,
const CatalogRelation &output_relation,
const QueryContext::insert_destination_id output_destination_index)
: RelationalOperator(query_id, 1u, output_relation.getNumPartitions() != 1u /* has_repartition */,
output_relation.getNumPartitions()),
file_pattern_(file_pattern),
options_(options),
output_relation_(output_relation),
output_destination_index_(output_destination_index),
work_generated_(false) {}
~TextScanOperator() override {}
OperatorType getOperatorType() const override {
return kTextScan;
}
std::string getName() const override {
return "TextScanOperator";
}
bool getAllWorkOrders(WorkOrdersContainer *container,
QueryContext *query_context,
StorageManager *storage_manager,
const tmb::client_id scheduler_client_id,
tmb::MessageBus *bus) override;
bool getAllWorkOrderProtos(WorkOrderProtosContainer *container) override;
QueryContext::insert_destination_id getInsertDestinationID() const override {
return output_destination_index_;
}
const relation_id getOutputRelationID() const override {
return output_relation_.getID();
}
private:
serialization::WorkOrder* createWorkOrderProto(const std::string &filename,
const std::size_t text_offset,
const std::size_t text_segment_size);
const std::string file_pattern_;
const BulkIoConfigurationPtr options_;
const CatalogRelation &output_relation_;
const QueryContext::insert_destination_id output_destination_index_;
bool work_generated_;
DISALLOW_COPY_AND_ASSIGN(TextScanOperator);
};
/**
* @brief A WorkOrder produced by TextScanOperator
**/
class TextScanWorkOrder : public WorkOrder {
public:
/**
* @brief Constructor
*
* @param query_id The ID of the query to which this WorkOrder belongs.
* @param filename The name of the text file to bulk insert.
* @param text_offset The start position in the text file to start text scan.
* @param text_segment_size The size of text segment to be scanned.
* @param field_terminator The character which separates attribute values in
* the text file.
* @param process_escape_sequences Whether to decode escape sequences in the
* text file.
* @param output_destination The InsertDestination to insert tuples.
* @param hdfs The HDFS connector via libhdfs3.
**/
TextScanWorkOrder(
const std::size_t query_id,
const std::string &filename,
const std::size_t text_offset,
const std::size_t text_segment_size,
const char field_terminator,
const bool process_escape_sequences,
InsertDestination *output_destination,
void *hdfs = nullptr)
: WorkOrder(query_id),
filename_(filename),
text_offset_(text_offset),
text_segment_size_(text_segment_size),
field_terminator_(field_terminator),
process_escape_sequences_(process_escape_sequences),
output_destination_(DCHECK_NOTNULL(output_destination)),
hdfs_(hdfs) {}
~TextScanWorkOrder() override {}
// FIXME(chasseur): Rollback if an exception is thrown after some tuples have
// already been inserted.
/**
* @exception TextScanReadError The text file could not be opened for
* reading.
* @exception TextScanFormatError Malformed data was encountered in the
* text file.
* @exception TupleTooLargeForBlock A tuple in the text file was too large
* to fit in a StorageBlock.
**/
void execute() override;
private:
/**
* @brief Extract a field string starting at \p *field_ptr. This method also
* expands escape sequences if \p process_escape_sequences_ is true.
* Throws TextScanFormatError if text was malformed.
*
* @param field_ptr \p *field_ptr points to the current position of the input
* char stream for parsing. The overall char stream must end with a
* newline character. After the call, \p *field_ptr will be modified to
* the start position of the NEXT field string.
* @param is_null_literal OUTPUT parameter. Set to true if the NULL-literal
* string "\N" was found.
* @param has_reached_end_of_line OUTPUT parameter. Set to true if the newline
* character was encountered.
* @param field_string OUTPUT parameter. Set to the extracted field string.
*/
void extractFieldString(const char **field_ptr,
bool *is_null_literal,
bool *has_reached_end_of_line,
std::string *field_string) const;
/**
* @brief This method helps incorporate fault tolerance while ingesting data.
* It is called whenever a faulty row is encountered and it is
* required to move \p *field_ptr to the next row.
*
* @param field_ptr \p *field_ptr points to the current position of the input
* char stream while parsing a faulty row. After the call, \p *field_ptr
* will be modified to the start position of the NEXT record in the
* stream.
*/
void skipFaultyRow(const char **field_ptr) const;
/**
* @brief Make a tuple by parsing all of the individual fields from a char stream.
*
* @param \p *row_ptr points to the current position of the input char stream
* for parsing. The overall char stream must end with a newline character.
* After the call, \p *row_ptr will be modified to the start position of
* the NEXT text row.
* @param relation The relation schema for the tuple.
* @param is_faulty OUTPUT parameter. Set to true if the row is faulty,
* @return The tuple parsed from the char stream.
*/
std::vector<TypedValue> parseRow(const char **row_ptr,
const CatalogRelationSchema &relation,
bool *is_faulty) const;
/**
* @brief Parse up to three octal digits (0-7) starting at \p *literal_ptr as
* a char literal. \p *literal_ptr will be modified to the last position
* of the parsed octal digits.
*
* @param literal_ptr \p *literal_ptr points to the current position of the
* input char stream for parsing. The overall char stream must end with
* a newline character.
* @return The char literal from the parsed octal digits.
*/
inline static char ParseOctalLiteral(const char **literal_ptr) {
int value = 0;
const char *ptr = *literal_ptr;
for (int i = 0; i < 3; ++i, ++ptr) {
const int char_value = *ptr - '0';
if ((char_value >= 0) && (char_value < 8)) {
value = value * 8 + char_value;
} else {
break;
}
}
*literal_ptr = ptr - 1;
return value;
}
/**
* @brief Parse up to two hexadecimal digits (0-F, case insensitive) starting
* at \p *literal_ptr as a char literal. \p *literal_ptr will be modified
* to the last position of the parsed octal digits.
*
* @param literal_ptr \p *literal_ptr points to the current position of the
* input char stream for parsing. The overall char stream must end with
* a newline character.
* @return The char literal from the parsed hexadecimal digits.
*/
inline static char ParseHexLiteral(const char **literal_ptr) {
int value = 0;
const char *ptr = *literal_ptr;
for (int i = 0; i < 2; ++i, ++ptr) {
const char c = *ptr;
int char_value;
if (std::isdigit(c)) {
char_value = c - '0';
} else if (c >= 'a' && c <= 'f') {
char_value = c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
char_value = c - 'A' + 10;
} else {
break;
}
value = value * 16 + char_value;
}
*literal_ptr = ptr - 1;
return value;
}
const std::string filename_;
const std::size_t text_offset_;
const std::size_t text_segment_size_;
const char field_terminator_;
const bool process_escape_sequences_;
InsertDestination *output_destination_;
// Not owned.
void *hdfs_;
DISALLOW_COPY_AND_ASSIGN(TextScanWorkOrder);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_RELATIONAL_OPERATORS_TEXT_SCAN_OPERATOR_HPP_