forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhive_reader.cpp
More file actions
416 lines (357 loc) · 18.2 KB
/
Copy pathhive_reader.cpp
File metadata and controls
416 lines (357 loc) · 18.2 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// 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 "format/table/hive_reader.h"
#include <vector>
#include "common/status.h"
#include "format/table/hive/hive_orc_nested_column_utils.h"
#include "format/table/hive/hive_parquet_nested_column_utils.h"
#include "format/table/nested_column_access_helper.h"
#include "runtime/runtime_state.h"
namespace doris {
Status HiveOrcReader::on_before_init_reader(ReaderInitContext* ctx) {
_column_descs = ctx->column_descs;
_fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
_fill_partition_values,
&_fill_partition_value_is_null));
for (const auto& desc : *ctx->column_descs) {
if (desc.category == ColumnCategory::REGULAR ||
desc.category == ColumnCategory::GENERATED) {
ctx->column_names.push_back(desc.name);
} else if (desc.category == ColumnCategory::SYNTHESIZED &&
desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
this->register_synthesized_column_handler(
desc.name,
[iter = std::move(topn_row_id_column_iter), this, &desc](
Block* block, size_t rows) -> Status {
return fill_topn_row_id(iter, desc.name, block, rows);
});
continue;
}
}
// Get file type (available because _create_file_reader() runs before this hook)
const orc::Type* orc_type_ptr = nullptr;
RETURN_IF_ERROR(get_file_type(&orc_type_ptr));
bool is_hive_col_name = OrcReader::is_hive1_col_name(orc_type_ptr);
// Build table_info_node based on config
if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(ctx->tuple_descriptor, orc_type_ptr,
ctx->table_info_node, _is_file_slot));
} else {
ctx->table_info_node = std::make_shared<StructNode>();
std::map<std::string, const SlotDescriptor*> slot_map;
for (const auto& slot : ctx->tuple_descriptor->slots()) {
slot_map.emplace(slot->col_name_lower_case(), slot);
}
for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
auto table_column_name = ctx->column_names[idx];
auto file_index = get_scan_params().column_idxs[idx];
if (file_index >= orc_type_ptr->getSubtypeCount()) {
ctx->table_info_node->add_not_exist_children(table_column_name);
} else {
auto field_node = std::make_shared<Node>();
RETURN_IF_ERROR(BuildTableInfoUtil::by_orc_name(
slot_map[table_column_name]->type(), orc_type_ptr->getSubtype(file_index),
field_node));
ctx->table_info_node->add_children(
table_column_name, orc_type_ptr->getFieldName(file_index), field_node);
}
slot_map.erase(table_column_name);
}
for (const auto& [partition_col_name, _] : slot_map) {
ctx->table_info_node->add_not_exist_children(partition_col_name);
}
}
// Compute column_ids
auto column_id_result = ColumnIdResult();
if (get_state()->query_options().hive_orc_use_column_names && !is_hive_col_name) {
column_id_result = _create_column_ids(orc_type_ptr, ctx->tuple_descriptor);
} else {
column_id_result =
_create_column_ids_by_top_level_col_index(orc_type_ptr, ctx->tuple_descriptor);
}
ctx->column_ids = std::move(column_id_result.column_ids);
ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
// _is_acid is false by default, no need to set explicitly
return Status::OK();
}
ColumnIdResult HiveOrcReader::_create_column_ids(const orc::Type* orc_type,
const TupleDescriptor* tuple_descriptor) {
// map top-level table column name (lower-cased) -> orc::Type*
std::unordered_map<std::string, const orc::Type*> table_col_name_to_orc_type_map;
for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
auto orc_sub_type = orc_type->getSubtype(i);
if (!orc_sub_type) continue;
std::string table_col_name = to_lower(orc_type->getFieldName(i));
table_col_name_to_orc_type_map[table_col_name] = orc_sub_type;
}
std::set<uint64_t> column_ids;
std::set<uint64_t> filter_column_ids;
// helper to process access paths for a given top-level orc field
auto process_access_paths = [](const orc::Type* orc_field,
const std::vector<TColumnAccessPath>& access_paths,
std::set<uint64_t>& out_ids) {
process_nested_access_paths(
orc_field, access_paths, out_ids,
[](const orc::Type* type) { return type->getColumnId(); },
[](const orc::Type* type) { return type->getMaximumColumnId(); },
HiveOrcNestedColumnUtils::extract_nested_column_ids);
};
for (const auto* slot : tuple_descriptor->slots()) {
auto it = table_col_name_to_orc_type_map.find(slot->col_name_lower_case());
if (it == table_col_name_to_orc_type_map.end()) {
// Column not found in file
continue;
}
const orc::Type* orc_field = it->second;
// primitive (non-nested) types
if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
slot->col_type() != TYPE_MAP)) {
column_ids.insert(orc_field->getColumnId());
if (slot->is_predicate()) {
filter_column_ids.insert(orc_field->getColumnId());
}
continue;
}
// complex types
const auto& all_access_paths = slot->all_access_paths();
process_access_paths(orc_field, all_access_paths, column_ids);
const auto& predicate_access_paths = slot->predicate_access_paths();
if (!predicate_access_paths.empty()) {
process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
}
}
return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
}
ColumnIdResult HiveOrcReader::_create_column_ids_by_top_level_col_index(
const orc::Type* orc_type, const TupleDescriptor* tuple_descriptor) {
// map top-level table column position -> orc::Type*
std::unordered_map<uint64_t, const orc::Type*> table_col_pos_to_orc_type_map;
for (uint64_t i = 0; i < orc_type->getSubtypeCount(); ++i) {
auto orc_sub_type = orc_type->getSubtype(i);
if (!orc_sub_type) continue;
table_col_pos_to_orc_type_map[i] = orc_sub_type;
}
std::set<uint64_t> column_ids;
std::set<uint64_t> filter_column_ids;
// helper to process access paths for a given top-level orc field
auto process_access_paths = [](const orc::Type* orc_field,
const std::vector<TColumnAccessPath>& access_paths,
std::set<uint64_t>& out_ids) {
process_nested_access_paths(
orc_field, access_paths, out_ids,
[](const orc::Type* type) { return type->getColumnId(); },
[](const orc::Type* type) { return type->getMaximumColumnId(); },
HiveOrcNestedColumnUtils::extract_nested_column_ids);
};
for (const auto* slot : tuple_descriptor->slots()) {
auto it = table_col_pos_to_orc_type_map.find(slot->col_pos());
if (it == table_col_pos_to_orc_type_map.end()) {
// Column not found in file
continue;
}
const orc::Type* orc_field = it->second;
// primitive (non-nested) types
if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
slot->col_type() != TYPE_MAP)) {
column_ids.insert(orc_field->getColumnId());
if (slot->is_predicate()) {
filter_column_ids.insert(orc_field->getColumnId());
}
continue;
}
const auto& all_access_paths = slot->all_access_paths();
// complex types
process_access_paths(orc_field, all_access_paths, column_ids);
const auto& predicate_access_paths = slot->predicate_access_paths();
if (!predicate_access_paths.empty()) {
process_access_paths(orc_field, predicate_access_paths, filter_column_ids);
}
}
return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
}
Status HiveParquetReader::on_before_init_reader(ReaderInitContext* ctx) {
_column_descs = ctx->column_descs;
_fill_col_name_to_block_idx = ctx->col_name_to_block_idx;
RETURN_IF_ERROR(_extract_partition_values(*ctx->range, ctx->tuple_descriptor,
_fill_partition_values,
&_fill_partition_value_is_null));
for (const auto& desc : *ctx->column_descs) {
if (desc.category == ColumnCategory::REGULAR ||
desc.category == ColumnCategory::GENERATED) {
ctx->column_names.push_back(desc.name);
} else if (desc.category == ColumnCategory::SYNTHESIZED &&
desc.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) {
auto topn_row_id_column_iter = _create_topn_row_id_column_iterator();
this->register_synthesized_column_handler(
desc.name,
[iter = std::move(topn_row_id_column_iter), this, &desc](
Block* block, size_t rows) -> Status {
return fill_topn_row_id(iter, desc.name, block, rows);
});
continue;
}
}
// Get file metadata schema (available because _open_file() runs before this hook)
const FieldDescriptor* field_desc = nullptr;
RETURN_IF_ERROR(get_file_metadata_schema(&field_desc));
DCHECK(field_desc != nullptr);
// Build table_info_node based on config
if (get_state()->query_options().hive_parquet_use_column_names) {
RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(ctx->tuple_descriptor, *field_desc,
ctx->table_info_node, _is_file_slot));
} else {
ctx->table_info_node = std::make_shared<StructNode>();
std::map<std::string, const SlotDescriptor*> slot_map;
for (const auto& slot : ctx->tuple_descriptor->slots()) {
slot_map.emplace(slot->col_name_lower_case(), slot);
}
auto parquet_fields_schema = field_desc->get_fields_schema();
for (size_t idx = 0; idx < get_scan_params().column_idxs.size(); idx++) {
auto table_column_name = ctx->column_names[idx];
auto file_index = get_scan_params().column_idxs[idx];
if (file_index >= parquet_fields_schema.size()) {
ctx->table_info_node->add_not_exist_children(table_column_name);
} else {
auto field_node = std::make_shared<Node>();
RETURN_IF_ERROR(BuildTableInfoUtil::by_parquet_name(
slot_map[table_column_name]->type(), parquet_fields_schema[file_index],
field_node));
ctx->table_info_node->add_children(
table_column_name, parquet_fields_schema[file_index].name, field_node);
}
slot_map.erase(table_column_name);
}
for (const auto& [partition_col_name, _] : slot_map) {
ctx->table_info_node->add_not_exist_children(partition_col_name);
}
}
// Compute column_ids for lazy materialization
auto column_id_result = ColumnIdResult();
if (get_state()->query_options().hive_parquet_use_column_names) {
column_id_result = _create_column_ids(field_desc, ctx->tuple_descriptor);
} else {
column_id_result =
_create_column_ids_by_top_level_col_index(field_desc, ctx->tuple_descriptor);
}
ctx->column_ids = std::move(column_id_result.column_ids);
ctx->filter_column_ids = std::move(column_id_result.filter_column_ids);
_filter_groups = true;
return Status::OK();
}
ColumnIdResult HiveParquetReader::_create_column_ids(const FieldDescriptor* field_desc,
const TupleDescriptor* tuple_descriptor) {
// First, assign column IDs to the field descriptor
auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
mutable_field_desc->assign_ids();
// map top-level table column name (lower-cased) -> FieldSchema*
std::unordered_map<std::string, const FieldSchema*> table_col_name_to_field_schema_map;
for (int i = 0; i < field_desc->size(); ++i) {
auto field_schema = field_desc->get_column(i);
if (!field_schema) continue;
table_col_name_to_field_schema_map[field_schema->lower_case_name] = field_schema;
}
std::set<uint64_t> column_ids;
std::set<uint64_t> filter_column_ids;
// helper to process access paths for a given top-level parquet field
auto process_access_paths = [](const FieldSchema* parquet_field,
const std::vector<TColumnAccessPath>& access_paths,
std::set<uint64_t>& out_ids) {
process_nested_access_paths(
parquet_field, access_paths, out_ids,
[](const FieldSchema* field) { return field->get_column_id(); },
[](const FieldSchema* field) { return field->get_max_column_id(); },
HiveParquetNestedColumnUtils::extract_nested_column_ids);
};
for (const auto* slot : tuple_descriptor->slots()) {
auto it = table_col_name_to_field_schema_map.find(slot->col_name_lower_case());
if (it == table_col_name_to_field_schema_map.end()) {
// Column not found in file
continue;
}
auto field_schema = it->second;
// primitive (non-nested) types
if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
slot->col_type() != TYPE_MAP)) {
column_ids.insert(field_schema->column_id);
if (slot->is_predicate()) {
filter_column_ids.insert(field_schema->column_id);
}
continue;
}
// complex types
const auto& all_access_paths = slot->all_access_paths();
process_access_paths(field_schema, all_access_paths, column_ids);
const auto& predicate_access_paths = slot->predicate_access_paths();
if (!predicate_access_paths.empty()) {
process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
}
}
return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
}
ColumnIdResult HiveParquetReader::_create_column_ids_by_top_level_col_index(
const FieldDescriptor* field_desc, const TupleDescriptor* tuple_descriptor) {
// First, assign column IDs to the field descriptor
auto* mutable_field_desc = const_cast<FieldDescriptor*>(field_desc);
mutable_field_desc->assign_ids();
// map top-level table column position -> FieldSchema*
std::unordered_map<uint64_t, const FieldSchema*> table_col_pos_to_field_schema_map;
for (int i = 0; i < field_desc->size(); ++i) {
auto field_schema = field_desc->get_column(i);
if (!field_schema) continue;
table_col_pos_to_field_schema_map[i] = field_schema;
}
std::set<uint64_t> column_ids;
std::set<uint64_t> filter_column_ids;
// helper to process access paths for a given top-level parquet field
auto process_access_paths = [](const FieldSchema* parquet_field,
const std::vector<TColumnAccessPath>& access_paths,
std::set<uint64_t>& out_ids) {
process_nested_access_paths(
parquet_field, access_paths, out_ids,
[](const FieldSchema* field) { return field->get_column_id(); },
[](const FieldSchema* field) { return field->get_max_column_id(); },
HiveParquetNestedColumnUtils::extract_nested_column_ids);
};
for (const auto* slot : tuple_descriptor->slots()) {
auto it = table_col_pos_to_field_schema_map.find(slot->col_pos());
if (it == table_col_pos_to_field_schema_map.end()) {
// Column not found in file
continue;
}
auto field_schema = it->second;
// primitive (non-nested) types
if ((slot->col_type() != TYPE_STRUCT && slot->col_type() != TYPE_ARRAY &&
slot->col_type() != TYPE_MAP)) {
column_ids.insert(field_schema->column_id);
if (slot->is_predicate()) {
filter_column_ids.insert(field_schema->column_id);
}
continue;
}
// complex types
const auto& all_access_paths = slot->all_access_paths();
process_access_paths(field_schema, all_access_paths, column_ids);
const auto& predicate_access_paths = slot->predicate_access_paths();
if (!predicate_access_paths.empty()) {
process_access_paths(field_schema, predicate_access_paths, filter_column_ids);
}
}
return ColumnIdResult(std::move(column_ids), std::move(filter_column_ids));
}
} // namespace doris