be/src/format/parquet/vparquet_group_reader.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "format/parquet/vparquet_group_reader.h" |
19 | | |
20 | | #include <gen_cpp/Exprs_types.h> |
21 | | #include <gen_cpp/Opcodes_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | #include <gen_cpp/parquet_types.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <algorithm> |
27 | | #include <boost/iterator/iterator_facade.hpp> |
28 | | #include <memory> |
29 | | #include <ostream> |
30 | | |
31 | | #include "common/config.h" |
32 | | #include "common/consts.h" |
33 | | #include "common/logging.h" |
34 | | #include "common/object_pool.h" |
35 | | #include "common/status.h" |
36 | | #include "core/assert_cast.h" |
37 | | #include "core/block/block.h" |
38 | | #include "core/block/column_with_type_and_name.h" |
39 | | #include "core/column/column_const.h" |
40 | | #include "core/column/column_nullable.h" |
41 | | #include "core/column/column_string.h" |
42 | | #include "core/column/column_struct.h" |
43 | | #include "core/column/column_vector.h" |
44 | | #include "core/custom_allocator.h" |
45 | | #include "core/data_type/data_type.h" |
46 | | #include "core/data_type/data_type_number.h" |
47 | | #include "core/data_type/data_type_string.h" |
48 | | #include "core/data_type/data_type_struct.h" |
49 | | #include "core/data_type/define_primitive_type.h" |
50 | | #include "core/pod_array.h" |
51 | | #include "core/types.h" |
52 | | #include "exprs/create_predicate_function.h" |
53 | | #include "exprs/hybrid_set.h" |
54 | | #include "exprs/vdirect_in_predicate.h" |
55 | | #include "exprs/vectorized_fn_call.h" |
56 | | #include "exprs/vexpr.h" |
57 | | #include "exprs/vexpr_context.h" |
58 | | #include "exprs/vliteral.h" |
59 | | #include "exprs/vslot_ref.h" |
60 | | #include "format/parquet/schema_desc.h" |
61 | | #include "format/parquet/vparquet_column_reader.h" |
62 | | #include "runtime/descriptors.h" |
63 | | #include "runtime/runtime_state.h" |
64 | | #include "runtime/thread_context.h" |
65 | | #include "storage/segment/column_reader.h" |
66 | | |
67 | | namespace cctz { |
68 | | class time_zone; |
69 | | } // namespace cctz |
70 | | namespace doris { |
71 | | class RuntimeState; |
72 | | |
73 | | namespace io { |
74 | | struct IOContext; |
75 | | } // namespace io |
76 | | } // namespace doris |
77 | | |
78 | | namespace doris { |
79 | | #include "common/compile_check_begin.h" |
80 | | |
81 | | namespace { |
82 | | Status build_iceberg_rowid_column(const DataTypePtr& type, const std::string& file_path, |
83 | | const std::vector<rowid_t>& row_ids, int32_t partition_spec_id, |
84 | | const std::string& partition_data_json, |
85 | 0 | MutableColumnPtr* column_out) { |
86 | 0 | if (type == nullptr || column_out == nullptr) { |
87 | 0 | return Status::InvalidArgument("Invalid iceberg rowid column type or output column"); |
88 | 0 | } |
89 | | |
90 | 0 | MutableColumnPtr column = type->create_column(); |
91 | 0 | ColumnNullable* nullable_col = check_and_get_column<ColumnNullable>(column.get()); |
92 | 0 | ColumnStruct* struct_col = nullptr; |
93 | 0 | if (nullable_col != nullptr) { |
94 | 0 | struct_col = |
95 | 0 | check_and_get_column<ColumnStruct>(nullable_col->get_nested_column_ptr().get()); |
96 | 0 | } else { |
97 | 0 | struct_col = check_and_get_column<ColumnStruct>(column.get()); |
98 | 0 | } |
99 | |
|
100 | 0 | if (struct_col == nullptr || struct_col->tuple_size() < 4) { |
101 | 0 | return Status::InternalError("Invalid iceberg rowid column structure"); |
102 | 0 | } |
103 | | |
104 | 0 | size_t num_rows = row_ids.size(); |
105 | 0 | auto& file_path_col = struct_col->get_column(0); |
106 | 0 | auto& row_pos_col = struct_col->get_column(1); |
107 | 0 | auto& spec_id_col = struct_col->get_column(2); |
108 | 0 | auto& partition_data_col = struct_col->get_column(3); |
109 | |
|
110 | 0 | file_path_col.reserve(num_rows); |
111 | 0 | row_pos_col.reserve(num_rows); |
112 | 0 | spec_id_col.reserve(num_rows); |
113 | 0 | partition_data_col.reserve(num_rows); |
114 | |
|
115 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
116 | 0 | file_path_col.insert_data(file_path.data(), file_path.size()); |
117 | 0 | } |
118 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
119 | 0 | int64_t row_pos = static_cast<int64_t>(row_ids[i]); |
120 | 0 | row_pos_col.insert_data(reinterpret_cast<const char*>(&row_pos), sizeof(row_pos)); |
121 | 0 | } |
122 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
123 | 0 | int32_t spec_id = partition_spec_id; |
124 | 0 | spec_id_col.insert_data(reinterpret_cast<const char*>(&spec_id), sizeof(spec_id)); |
125 | 0 | } |
126 | 0 | for (size_t i = 0; i < num_rows; ++i) { |
127 | 0 | partition_data_col.insert_data(partition_data_json.data(), partition_data_json.size()); |
128 | 0 | } |
129 | |
|
130 | 0 | if (nullable_col != nullptr) { |
131 | 0 | nullable_col->get_null_map_data().resize_fill(num_rows, 0); |
132 | 0 | } |
133 | |
|
134 | 0 | *column_out = std::move(column); |
135 | 0 | return Status::OK(); |
136 | 0 | } |
137 | | } // namespace |
138 | | const std::vector<int64_t> RowGroupReader::NO_DELETE = {}; |
139 | | static constexpr uint32_t MAX_DICT_CODE_PREDICATE_TO_REWRITE = std::numeric_limits<uint32_t>::max(); |
140 | | |
141 | | RowGroupReader::RowGroupReader(io::FileReaderSPtr file_reader, |
142 | | const std::vector<std::string>& read_columns, |
143 | | const int32_t row_group_id, const tparquet::RowGroup& row_group, |
144 | | const cctz::time_zone* ctz, io::IOContext* io_ctx, |
145 | | const PositionDeleteContext& position_delete_ctx, |
146 | | const LazyReadContext& lazy_read_ctx, RuntimeState* state, |
147 | | const std::set<uint64_t>& column_ids, |
148 | | const std::set<uint64_t>& filter_column_ids) |
149 | 37 | : _file_reader(file_reader), |
150 | 37 | _read_table_columns(read_columns), |
151 | 37 | _row_group_id(row_group_id), |
152 | 37 | _row_group_meta(row_group), |
153 | 37 | _remaining_rows(row_group.num_rows), |
154 | 37 | _ctz(ctz), |
155 | 37 | _io_ctx(io_ctx), |
156 | 37 | _position_delete_ctx(position_delete_ctx), |
157 | 37 | _lazy_read_ctx(lazy_read_ctx), |
158 | 37 | _state(state), |
159 | 37 | _obj_pool(new ObjectPool()), |
160 | 37 | _column_ids(column_ids), |
161 | 37 | _filter_column_ids(filter_column_ids) {} |
162 | | |
163 | 37 | RowGroupReader::~RowGroupReader() { |
164 | 37 | _column_readers.clear(); |
165 | 37 | _obj_pool->clear(); |
166 | 37 | } |
167 | | |
168 | | Status RowGroupReader::init( |
169 | | const FieldDescriptor& schema, RowRanges& row_ranges, |
170 | | std::unordered_map<int, tparquet::OffsetIndex>& col_offsets, |
171 | | const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor, |
172 | | const std::unordered_map<std::string, int>* colname_to_slot_id, |
173 | | const VExprContextSPtrs* not_single_slot_filter_conjuncts, |
174 | 37 | const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts) { |
175 | 37 | _tuple_descriptor = tuple_descriptor; |
176 | 37 | _row_descriptor = row_descriptor; |
177 | 37 | _col_name_to_slot_id = colname_to_slot_id; |
178 | 37 | _slot_id_to_filter_conjuncts = slot_id_to_filter_conjuncts; |
179 | 37 | _read_ranges = row_ranges; |
180 | 37 | _filter_read_ranges_by_condition_cache(); |
181 | 37 | _remaining_rows = _read_ranges.count(); |
182 | | |
183 | 37 | if (_read_table_columns.empty()) { |
184 | | // Query task that only select columns in path. |
185 | 1 | return Status::OK(); |
186 | 1 | } |
187 | 36 | const size_t MAX_GROUP_BUF_SIZE = config::parquet_rowgroup_max_buffer_mb << 20; |
188 | 36 | const size_t MAX_COLUMN_BUF_SIZE = config::parquet_column_max_buffer_mb << 20; |
189 | 36 | size_t max_buf_size = |
190 | 36 | std::min(MAX_COLUMN_BUF_SIZE, MAX_GROUP_BUF_SIZE / _read_table_columns.size()); |
191 | 106 | for (const auto& read_table_col : _read_table_columns) { |
192 | 106 | auto read_file_col = _table_info_node_ptr->children_file_column_name(read_table_col); |
193 | 106 | auto* field = schema.get_column(read_file_col); |
194 | 106 | std::unique_ptr<ParquetColumnReader> reader; |
195 | 106 | RETURN_IF_ERROR(ParquetColumnReader::create( |
196 | 106 | _file_reader, field, _row_group_meta, _read_ranges, _ctz, _io_ctx, reader, |
197 | 106 | max_buf_size, col_offsets, _state, false, _column_ids, _filter_column_ids)); |
198 | 106 | if (reader == nullptr) { |
199 | 0 | VLOG_DEBUG << "Init row group(" << _row_group_id << ") reader failed"; |
200 | 0 | return Status::Corruption("Init row group reader failed"); |
201 | 0 | } |
202 | 106 | _column_readers[read_table_col] = std::move(reader); |
203 | 106 | } |
204 | | |
205 | 36 | bool disable_dict_filter = false; |
206 | 36 | if (not_single_slot_filter_conjuncts != nullptr && !not_single_slot_filter_conjuncts->empty()) { |
207 | 0 | disable_dict_filter = true; |
208 | 0 | _filter_conjuncts.insert(_filter_conjuncts.end(), not_single_slot_filter_conjuncts->begin(), |
209 | 0 | not_single_slot_filter_conjuncts->end()); |
210 | 0 | } |
211 | | |
212 | | // Check if single slot can be filtered by dict. |
213 | 36 | if (_slot_id_to_filter_conjuncts && !_slot_id_to_filter_conjuncts->empty()) { |
214 | 6 | const std::vector<std::string>& predicate_col_names = |
215 | 6 | _lazy_read_ctx.predicate_columns.first; |
216 | 6 | const std::vector<int>& predicate_col_slot_ids = _lazy_read_ctx.predicate_columns.second; |
217 | 14 | for (size_t i = 0; i < predicate_col_names.size(); ++i) { |
218 | 8 | const std::string& predicate_col_name = predicate_col_names[i]; |
219 | 8 | int slot_id = predicate_col_slot_ids[i]; |
220 | 8 | auto predicate_file_col_name = |
221 | 8 | _table_info_node_ptr->children_file_column_name(predicate_col_name); |
222 | 8 | auto field = schema.get_column(predicate_file_col_name); |
223 | 8 | if (!disable_dict_filter && !_lazy_read_ctx.has_complex_type && |
224 | 8 | _can_filter_by_dict( |
225 | 8 | slot_id, _row_group_meta.columns[field->physical_column_index].meta_data)) { |
226 | 2 | _dict_filter_cols.emplace_back(std::make_pair(predicate_col_name, slot_id)); |
227 | 6 | } else { |
228 | 6 | if (_slot_id_to_filter_conjuncts->find(slot_id) != |
229 | 6 | _slot_id_to_filter_conjuncts->end()) { |
230 | 6 | for (auto& ctx : _slot_id_to_filter_conjuncts->at(slot_id)) { |
231 | 6 | _filter_conjuncts.push_back(ctx); |
232 | 6 | } |
233 | 6 | } |
234 | 6 | } |
235 | 8 | } |
236 | | // Add predicate_partition_columns in _slot_id_to_filter_conjuncts(single slot conjuncts) |
237 | | // to _filter_conjuncts, others should be added from not_single_slot_filter_conjuncts. |
238 | 6 | for (auto& kv : _lazy_read_ctx.predicate_partition_columns) { |
239 | 4 | auto& [value, slot_desc] = kv.second; |
240 | 4 | auto iter = _slot_id_to_filter_conjuncts->find(slot_desc->id()); |
241 | 4 | if (iter != _slot_id_to_filter_conjuncts->end()) { |
242 | 4 | for (auto& ctx : iter->second) { |
243 | 4 | _filter_conjuncts.push_back(ctx); |
244 | 4 | } |
245 | 4 | } |
246 | 4 | } |
247 | | //For check missing column : missing column == xx, missing column is null,missing column is not null. |
248 | 6 | _filter_conjuncts.insert(_filter_conjuncts.end(), |
249 | 6 | _lazy_read_ctx.missing_columns_conjuncts.begin(), |
250 | 6 | _lazy_read_ctx.missing_columns_conjuncts.end()); |
251 | 6 | RETURN_IF_ERROR(_rewrite_dict_predicates()); |
252 | 6 | } |
253 | | // _state is nullptr in some ut. |
254 | 36 | if (_state && _state->enable_adjust_conjunct_order_by_cost()) { |
255 | 8 | std::ranges::sort(_filter_conjuncts, [](const auto& a, const auto& b) { |
256 | 8 | return a->execute_cost() < b->execute_cost(); |
257 | 8 | }); |
258 | 8 | } |
259 | 36 | return Status::OK(); |
260 | 36 | } |
261 | | |
262 | | bool RowGroupReader::_can_filter_by_dict(int slot_id, |
263 | 8 | const tparquet::ColumnMetaData& column_metadata) { |
264 | 8 | SlotDescriptor* slot = nullptr; |
265 | 8 | const std::vector<SlotDescriptor*>& slots = _tuple_descriptor->slots(); |
266 | 14 | for (auto each : slots) { |
267 | 14 | if (each->id() == slot_id) { |
268 | 8 | slot = each; |
269 | 8 | break; |
270 | 8 | } |
271 | 14 | } |
272 | 8 | if (!is_string_type(slot->type()->get_primitive_type()) && |
273 | 8 | !is_var_len_object(slot->type()->get_primitive_type())) { |
274 | 6 | return false; |
275 | 6 | } |
276 | 2 | if (column_metadata.type != tparquet::Type::BYTE_ARRAY) { |
277 | 0 | return false; |
278 | 0 | } |
279 | | |
280 | 2 | if (!is_dictionary_encoded(column_metadata)) { |
281 | 0 | return false; |
282 | 0 | } |
283 | | |
284 | 2 | if (_slot_id_to_filter_conjuncts->find(slot_id) == _slot_id_to_filter_conjuncts->end()) { |
285 | 0 | return false; |
286 | 0 | } |
287 | | |
288 | | // TODO: The current implementation of dictionary filtering does not take into account |
289 | | // the implementation of NULL values because the dictionary itself does not contain |
290 | | // NULL value encoding. As a result, many NULL-related functions or expressions |
291 | | // cannot work properly, such as is null, is not null, coalesce, etc. |
292 | | // Here we check if the predicate expr is IN or BINARY_PRED. |
293 | | // Implementation of NULL value dictionary filtering will be carried out later. |
294 | 2 | return std::ranges::all_of(_slot_id_to_filter_conjuncts->at(slot_id), [&](const auto& ctx) { |
295 | 2 | return (ctx->root()->node_type() == TExprNodeType::IN_PRED || |
296 | 2 | ctx->root()->node_type() == TExprNodeType::BINARY_PRED) && |
297 | 2 | ctx->root()->children()[0]->node_type() == TExprNodeType::SLOT_REF; |
298 | 2 | }); |
299 | 2 | } |
300 | | |
301 | | // This function is copied from |
302 | | // https://github.com/apache/impala/blob/master/be/src/exec/parquet/hdfs-parquet-scanner.cc#L1717 |
303 | 2 | bool RowGroupReader::is_dictionary_encoded(const tparquet::ColumnMetaData& column_metadata) { |
304 | | // The Parquet spec allows for column chunks to have mixed encodings |
305 | | // where some data pages are dictionary-encoded and others are plain |
306 | | // encoded. For example, a Parquet file writer might start writing |
307 | | // a column chunk as dictionary encoded, but it will switch to plain |
308 | | // encoding if the dictionary grows too large. |
309 | | // |
310 | | // In order for dictionary filters to skip the entire row group, |
311 | | // the conjuncts must be evaluated on column chunks that are entirely |
312 | | // encoded with the dictionary encoding. There are two checks |
313 | | // available to verify this: |
314 | | // 1. The encoding_stats field on the column chunk metadata provides |
315 | | // information about the number of data pages written in each |
316 | | // format. This allows for a specific check of whether all the |
317 | | // data pages are dictionary encoded. |
318 | | // 2. The encodings field on the column chunk metadata lists the |
319 | | // encodings used. If this list contains the dictionary encoding |
320 | | // and does not include unexpected encodings (i.e. encodings not |
321 | | // associated with definition/repetition levels), then it is entirely |
322 | | // dictionary encoded. |
323 | 2 | if (column_metadata.__isset.encoding_stats) { |
324 | | // Condition #1 above |
325 | 4 | for (const tparquet::PageEncodingStats& enc_stat : column_metadata.encoding_stats) { |
326 | 4 | if (enc_stat.page_type == tparquet::PageType::DATA_PAGE && |
327 | 4 | (enc_stat.encoding != tparquet::Encoding::PLAIN_DICTIONARY && |
328 | 2 | enc_stat.encoding != tparquet::Encoding::RLE_DICTIONARY) && |
329 | 4 | enc_stat.count > 0) { |
330 | 0 | return false; |
331 | 0 | } |
332 | 4 | } |
333 | 2 | } else { |
334 | | // Condition #2 above |
335 | 0 | bool has_dict_encoding = false; |
336 | 0 | bool has_nondict_encoding = false; |
337 | 0 | for (const tparquet::Encoding::type& encoding : column_metadata.encodings) { |
338 | 0 | if (encoding == tparquet::Encoding::PLAIN_DICTIONARY || |
339 | 0 | encoding == tparquet::Encoding::RLE_DICTIONARY) { |
340 | 0 | has_dict_encoding = true; |
341 | 0 | } |
342 | | |
343 | | // RLE and BIT_PACKED are used for repetition/definition levels |
344 | 0 | if (encoding != tparquet::Encoding::PLAIN_DICTIONARY && |
345 | 0 | encoding != tparquet::Encoding::RLE_DICTIONARY && |
346 | 0 | encoding != tparquet::Encoding::RLE && encoding != tparquet::Encoding::BIT_PACKED) { |
347 | 0 | has_nondict_encoding = true; |
348 | 0 | break; |
349 | 0 | } |
350 | 0 | } |
351 | | // Not entirely dictionary encoded if: |
352 | | // 1. No dictionary encoding listed |
353 | | // OR |
354 | | // 2. Some non-dictionary encoding is listed |
355 | 0 | if (!has_dict_encoding || has_nondict_encoding) { |
356 | 0 | return false; |
357 | 0 | } |
358 | 0 | } |
359 | | |
360 | 2 | return true; |
361 | 2 | } |
362 | | |
363 | | Status RowGroupReader::next_batch(Block* block, size_t batch_size, size_t* read_rows, |
364 | 49 | bool* batch_eof) { |
365 | 49 | if (_is_row_group_filtered) { |
366 | 2 | *read_rows = 0; |
367 | 2 | *batch_eof = true; |
368 | 2 | return Status::OK(); |
369 | 2 | } |
370 | | |
371 | | // Process external table query task that select columns are all from path. |
372 | 47 | if (_read_table_columns.empty()) { |
373 | 3 | bool modify_row_ids = false; |
374 | 3 | RETURN_IF_ERROR(_read_empty_batch(batch_size, read_rows, batch_eof, &modify_row_ids)); |
375 | | |
376 | 3 | RETURN_IF_ERROR( |
377 | 3 | _fill_partition_columns(block, *read_rows, _lazy_read_ctx.partition_columns)); |
378 | 3 | RETURN_IF_ERROR(_fill_missing_columns(block, *read_rows, _lazy_read_ctx.missing_columns)); |
379 | | |
380 | 3 | RETURN_IF_ERROR(_fill_row_id_columns(block, *read_rows, modify_row_ids)); |
381 | 3 | RETURN_IF_ERROR(_append_iceberg_rowid_column(block, *read_rows, modify_row_ids)); |
382 | | |
383 | 3 | Status st = VExprContext::filter_block(_lazy_read_ctx.conjuncts, block, block->columns()); |
384 | 3 | *read_rows = block->rows(); |
385 | 3 | return st; |
386 | 3 | } |
387 | 44 | if (_lazy_read_ctx.can_lazy_read) { |
388 | | // call _do_lazy_read recursively when current batch is skipped |
389 | 4 | return _do_lazy_read(block, batch_size, read_rows, batch_eof); |
390 | 40 | } else { |
391 | 40 | FilterMap filter_map; |
392 | 40 | int64_t batch_base_row = _total_read_rows; |
393 | 40 | RETURN_IF_ERROR((_read_column_data(block, _lazy_read_ctx.all_read_columns, batch_size, |
394 | 40 | read_rows, batch_eof, filter_map))); |
395 | 40 | RETURN_IF_ERROR( |
396 | 40 | _fill_partition_columns(block, *read_rows, _lazy_read_ctx.partition_columns)); |
397 | 40 | RETURN_IF_ERROR(_fill_missing_columns(block, *read_rows, _lazy_read_ctx.missing_columns)); |
398 | 40 | RETURN_IF_ERROR(_fill_row_id_columns(block, *read_rows, false)); |
399 | 40 | RETURN_IF_ERROR(_append_iceberg_rowid_column(block, *read_rows, false)); |
400 | | |
401 | 40 | #ifndef NDEBUG |
402 | 125 | for (auto col : *block) { |
403 | 125 | col.column->sanity_check(); |
404 | 125 | DCHECK(block->rows() == col.column->size()) |
405 | 0 | << absl::Substitute("block rows = $0 , column rows = $1, col name = $2", |
406 | 0 | block->rows(), col.column->size(), col.name); |
407 | 125 | } |
408 | 40 | #endif |
409 | | |
410 | 40 | if (block->rows() == 0) { |
411 | 0 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
412 | 0 | *read_rows = block->rows(); |
413 | 0 | #ifndef NDEBUG |
414 | 0 | for (auto col : *block) { |
415 | 0 | col.column->sanity_check(); |
416 | 0 | DCHECK(block->rows() == col.column->size()) |
417 | 0 | << absl::Substitute("block rows = $0 , column rows = $1, col name = $2", |
418 | 0 | block->rows(), col.column->size(), col.name); |
419 | 0 | } |
420 | 0 | #endif |
421 | 0 | return Status::OK(); |
422 | 0 | } |
423 | 40 | { |
424 | 40 | SCOPED_RAW_TIMER(&_predicate_filter_time); |
425 | 40 | RETURN_IF_ERROR(_build_pos_delete_filter(*read_rows)); |
426 | | |
427 | 40 | std::vector<uint32_t> columns_to_filter; |
428 | 40 | int column_to_keep = block->columns(); |
429 | 40 | columns_to_filter.resize(column_to_keep); |
430 | 165 | for (uint32_t i = 0; i < column_to_keep; ++i) { |
431 | 125 | columns_to_filter[i] = i; |
432 | 125 | } |
433 | 40 | if (!_lazy_read_ctx.conjuncts.empty()) { |
434 | 6 | std::vector<IColumn::Filter*> filters; |
435 | 6 | if (_position_delete_ctx.has_filter) { |
436 | 0 | filters.push_back(_pos_delete_filter_ptr.get()); |
437 | 0 | } |
438 | 6 | IColumn::Filter result_filter(block->rows(), 1); |
439 | 6 | bool can_filter_all = false; |
440 | | |
441 | 6 | { |
442 | 6 | RETURN_IF_ERROR_OR_CATCH_EXCEPTION(VExprContext::execute_conjuncts( |
443 | 6 | _filter_conjuncts, &filters, block, &result_filter, &can_filter_all)); |
444 | 6 | } |
445 | | |
446 | | // Condition cache MISS: mark granules with surviving rows (non-lazy path) |
447 | 6 | if (!can_filter_all) { |
448 | 3 | _mark_condition_cache_granules(result_filter.data(), block->rows(), |
449 | 3 | batch_base_row); |
450 | 3 | } |
451 | | |
452 | 6 | if (can_filter_all) { |
453 | 9 | for (auto& col : columns_to_filter) { |
454 | 9 | std::move(*block->get_by_position(col).column).assume_mutable()->clear(); |
455 | 9 | } |
456 | 3 | Block::erase_useless_column(block, column_to_keep); |
457 | 3 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
458 | 3 | return Status::OK(); |
459 | 3 | } |
460 | | |
461 | 3 | RETURN_IF_CATCH_EXCEPTION( |
462 | 3 | Block::filter_block_internal(block, columns_to_filter, result_filter)); |
463 | 3 | Block::erase_useless_column(block, column_to_keep); |
464 | 34 | } else { |
465 | 34 | RETURN_IF_CATCH_EXCEPTION( |
466 | 34 | RETURN_IF_ERROR(_filter_block(block, column_to_keep, columns_to_filter))); |
467 | 34 | } |
468 | 37 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
469 | 37 | } |
470 | 37 | #ifndef NDEBUG |
471 | 116 | for (auto col : *block) { |
472 | 116 | col.column->sanity_check(); |
473 | 116 | DCHECK(block->rows() == col.column->size()) |
474 | 0 | << absl::Substitute("block rows = $0 , column rows = $1, col name = $2", |
475 | 0 | block->rows(), col.column->size(), col.name); |
476 | 116 | } |
477 | 37 | #endif |
478 | 37 | *read_rows = block->rows(); |
479 | 37 | return Status::OK(); |
480 | 37 | } |
481 | 44 | } |
482 | | |
483 | | // Maps each batch row to its global parquet file position via _read_ranges, then marks |
484 | | // the corresponding condition cache granule as true if the filter indicates the row survived. |
485 | | // batch_seq_start is the number of rows already read sequentially before this batch |
486 | | // (i.e., _total_read_rows before the batch started). |
487 | | void RowGroupReader::_mark_condition_cache_granules(const uint8_t* filter_data, size_t num_rows, |
488 | 6 | int64_t batch_seq_start) { |
489 | 6 | if (!_condition_cache_ctx || _condition_cache_ctx->is_hit) { |
490 | 6 | return; |
491 | 6 | } |
492 | 0 | auto& cache = *_condition_cache_ctx->filter_result; |
493 | 0 | for (size_t i = 0; i < num_rows; i++) { |
494 | 0 | if (filter_data[i]) { |
495 | | // row-group-relative position of this row |
496 | 0 | int64_t rg_pos = _read_ranges.get_row_index_by_pos(batch_seq_start + i); |
497 | | // global row number in the parquet file |
498 | 0 | size_t granule = (_current_row_group_idx.first_row + rg_pos) / |
499 | 0 | ConditionCacheContext::GRANULE_SIZE; |
500 | 0 | size_t cache_idx = granule - _condition_cache_ctx->base_granule; |
501 | 0 | if (cache_idx < cache.size()) { |
502 | 0 | cache[cache_idx] = true; |
503 | 0 | } |
504 | 0 | } |
505 | 0 | } |
506 | 0 | } |
507 | | |
508 | | // On condition cache HIT, removes row ranges whose granules have no surviving rows from |
509 | | // _read_ranges BEFORE column readers are created. This makes ParquetColumnReader skip I/O |
510 | | // entirely for false-granule rows — both predicate and lazy columns — via its existing |
511 | | // page/row-skipping infrastructure. |
512 | 37 | void RowGroupReader::_filter_read_ranges_by_condition_cache() { |
513 | 37 | if (!_condition_cache_ctx || !_condition_cache_ctx->is_hit) { |
514 | 37 | return; |
515 | 37 | } |
516 | 0 | auto& filter_result = *_condition_cache_ctx->filter_result; |
517 | 0 | if (filter_result.empty()) { |
518 | 0 | return; |
519 | 0 | } |
520 | | |
521 | 0 | auto old_row_count = _read_ranges.count(); |
522 | 0 | _read_ranges = |
523 | 0 | filter_ranges_by_cache(_read_ranges, filter_result, _current_row_group_idx.first_row, |
524 | 0 | _condition_cache_ctx->base_granule); |
525 | 0 | _is_row_group_filtered = _read_ranges.is_empty(); |
526 | 0 | _condition_cache_filtered_rows += old_row_count - _read_ranges.count(); |
527 | 0 | } |
528 | | |
529 | | // Filters read_ranges by removing rows whose cache granule is false. |
530 | | // |
531 | | // Cache index i maps to global granule (base_granule + i), which covers global file |
532 | | // rows [(base_granule+i)*GS, (base_granule+i+1)*GS). Since read_ranges uses |
533 | | // row-group-relative indices and first_row is the global position of the row group's |
534 | | // first row, global granule g maps to row-group-relative range: |
535 | | // [max(0, g*GS - first_row), max(0, (g+1)*GS - first_row)) |
536 | | // |
537 | | // We build a RowRanges of all false-granule regions (in row-group-relative coordinates), |
538 | | // then subtract from read_ranges via ranges_exception. |
539 | | // |
540 | | // Granules beyond cache.size() are kept conservatively (assumed true). |
541 | | // |
542 | | // When base_granule > 0, the cache only covers granules starting from base_granule. |
543 | | // This happens when a Parquet file is split across multiple scan ranges and this reader |
544 | | // only processes row groups starting at a non-zero offset in the file. |
545 | | RowRanges RowGroupReader::filter_ranges_by_cache(const RowRanges& read_ranges, |
546 | | const std::vector<bool>& cache, int64_t first_row, |
547 | 21 | int64_t base_granule) { |
548 | 21 | constexpr int64_t GS = ConditionCacheContext::GRANULE_SIZE; |
549 | 21 | RowRanges filtered_ranges; |
550 | | |
551 | 138 | for (size_t i = 0; i < cache.size(); i++) { |
552 | 117 | if (!cache[i]) { |
553 | 64 | int64_t global_granule = base_granule + static_cast<int64_t>(i); |
554 | 64 | int64_t rg_from = std::max(static_cast<int64_t>(0), global_granule * GS - first_row); |
555 | 64 | int64_t rg_to = |
556 | 64 | std::max(static_cast<int64_t>(0), (global_granule + 1) * GS - first_row); |
557 | 64 | if (rg_from < rg_to) { |
558 | 16 | filtered_ranges.add(RowRange(rg_from, rg_to)); |
559 | 16 | } |
560 | 64 | } |
561 | 117 | } |
562 | | |
563 | 21 | RowRanges result; |
564 | 21 | RowRanges::ranges_exception(read_ranges, filtered_ranges, &result); |
565 | 21 | return result; |
566 | 21 | } |
567 | | |
568 | | Status RowGroupReader::_read_column_data(Block* block, |
569 | | const std::vector<std::string>& table_columns, |
570 | | size_t batch_size, size_t* read_rows, bool* batch_eof, |
571 | 49 | FilterMap& filter_map) { |
572 | 49 | size_t batch_read_rows = 0; |
573 | 49 | bool has_eof = false; |
574 | 123 | for (auto& read_col_name : table_columns) { |
575 | 123 | auto& column_with_type_and_name = |
576 | 123 | block->safe_get_by_position((*_col_name_to_block_idx)[read_col_name]); |
577 | 123 | auto& column_ptr = column_with_type_and_name.column; |
578 | 123 | auto& column_type = column_with_type_and_name.type; |
579 | 123 | bool is_dict_filter = false; |
580 | 123 | for (auto& _dict_filter_col : _dict_filter_cols) { |
581 | 0 | if (_dict_filter_col.first == read_col_name) { |
582 | 0 | MutableColumnPtr dict_column = ColumnInt32::create(); |
583 | 0 | if (!_col_name_to_block_idx->contains(read_col_name)) { |
584 | 0 | return Status::InternalError( |
585 | 0 | "Wrong read column '{}' in parquet file, block: {}", read_col_name, |
586 | 0 | block->dump_structure()); |
587 | 0 | } |
588 | 0 | if (column_type->is_nullable()) { |
589 | 0 | block->get_by_position((*_col_name_to_block_idx)[read_col_name]).type = |
590 | 0 | std::make_shared<DataTypeNullable>(std::make_shared<DataTypeInt32>()); |
591 | 0 | block->replace_by_position( |
592 | 0 | (*_col_name_to_block_idx)[read_col_name], |
593 | 0 | ColumnNullable::create(std::move(dict_column), |
594 | 0 | ColumnUInt8::create(dict_column->size(), 0))); |
595 | 0 | } else { |
596 | 0 | block->get_by_position((*_col_name_to_block_idx)[read_col_name]).type = |
597 | 0 | std::make_shared<DataTypeInt32>(); |
598 | 0 | block->replace_by_position((*_col_name_to_block_idx)[read_col_name], |
599 | 0 | std::move(dict_column)); |
600 | 0 | } |
601 | 0 | is_dict_filter = true; |
602 | 0 | break; |
603 | 0 | } |
604 | 0 | } |
605 | | |
606 | 123 | size_t col_read_rows = 0; |
607 | 123 | bool col_eof = false; |
608 | | // Should reset _filter_map_index to 0 when reading next column. |
609 | | // select_vector.reset(); |
610 | 123 | _column_readers[read_col_name]->reset_filter_map_index(); |
611 | 309 | while (!col_eof && col_read_rows < batch_size) { |
612 | 186 | size_t loop_rows = 0; |
613 | 186 | RETURN_IF_ERROR(_column_readers[read_col_name]->read_column_data( |
614 | 186 | column_ptr, column_type, _table_info_node_ptr->get_children_node(read_col_name), |
615 | 186 | filter_map, batch_size - col_read_rows, &loop_rows, &col_eof, is_dict_filter)); |
616 | 186 | VLOG_DEBUG << "[RowGroupReader] column '" << read_col_name |
617 | 0 | << "' loop_rows=" << loop_rows << " col_read_rows_so_far=" << col_read_rows |
618 | 0 | << std::endl; |
619 | 186 | col_read_rows += loop_rows; |
620 | 186 | } |
621 | 123 | VLOG_DEBUG << "[RowGroupReader] column '" << read_col_name |
622 | 0 | << "' read_rows=" << col_read_rows << std::endl; |
623 | 123 | if (batch_read_rows > 0 && batch_read_rows != col_read_rows) { |
624 | 0 | LOG(WARNING) << "[RowGroupReader] Mismatched read rows among parquet columns. " |
625 | 0 | "previous_batch_read_rows=" |
626 | 0 | << batch_read_rows << ", current_column='" << read_col_name |
627 | 0 | << "', current_col_read_rows=" << col_read_rows; |
628 | 0 | return Status::Corruption("Can't read the same number of rows among parquet columns"); |
629 | 0 | } |
630 | 123 | batch_read_rows = col_read_rows; |
631 | | |
632 | 123 | #ifndef NDEBUG |
633 | 123 | column_ptr->sanity_check(); |
634 | 123 | #endif |
635 | 123 | if (col_eof) { |
636 | 101 | has_eof = true; |
637 | 101 | } |
638 | 123 | } |
639 | | |
640 | 49 | *read_rows = batch_read_rows; |
641 | 49 | *batch_eof = has_eof; |
642 | | |
643 | 49 | return Status::OK(); |
644 | 49 | } |
645 | | |
646 | | Status RowGroupReader::_do_lazy_read(Block* block, size_t batch_size, size_t* read_rows, |
647 | 4 | bool* batch_eof) { |
648 | 4 | std::unique_ptr<FilterMap> filter_map_ptr = nullptr; |
649 | 4 | size_t pre_read_rows; |
650 | 4 | bool pre_eof; |
651 | 4 | std::vector<uint32_t> columns_to_filter; |
652 | 4 | uint32_t origin_column_num = block->columns(); |
653 | 4 | columns_to_filter.resize(origin_column_num); |
654 | 16 | for (uint32_t i = 0; i < origin_column_num; ++i) { |
655 | 12 | columns_to_filter[i] = i; |
656 | 12 | } |
657 | 4 | IColumn::Filter result_filter; |
658 | 4 | size_t pre_raw_read_rows = 0; |
659 | 6 | while (!_state->is_cancelled()) { |
660 | | // read predicate columns |
661 | 6 | pre_read_rows = 0; |
662 | 6 | pre_eof = false; |
663 | 6 | FilterMap filter_map; |
664 | 6 | int64_t batch_base_row = _total_read_rows; |
665 | 6 | RETURN_IF_ERROR(_read_column_data(block, _lazy_read_ctx.predicate_columns.first, batch_size, |
666 | 6 | &pre_read_rows, &pre_eof, filter_map)); |
667 | 6 | if (pre_read_rows == 0) { |
668 | 0 | DCHECK_EQ(pre_eof, true); |
669 | 0 | break; |
670 | 0 | } |
671 | 6 | pre_raw_read_rows += pre_read_rows; |
672 | | |
673 | 6 | RETURN_IF_ERROR(_fill_partition_columns(block, pre_read_rows, |
674 | 6 | _lazy_read_ctx.predicate_partition_columns)); |
675 | 6 | RETURN_IF_ERROR(_fill_missing_columns(block, pre_read_rows, |
676 | 6 | _lazy_read_ctx.predicate_missing_columns)); |
677 | 6 | RETURN_IF_ERROR(_fill_row_id_columns(block, pre_read_rows, false)); |
678 | 6 | RETURN_IF_ERROR(_append_iceberg_rowid_column(block, pre_read_rows, false)); |
679 | | |
680 | 6 | RETURN_IF_ERROR(_build_pos_delete_filter(pre_read_rows)); |
681 | | |
682 | 6 | #ifndef NDEBUG |
683 | 18 | for (auto col : *block) { |
684 | 18 | if (col.column->size() == 0) { // lazy read column. |
685 | 6 | continue; |
686 | 6 | } |
687 | 12 | col.column->sanity_check(); |
688 | 12 | DCHECK(pre_read_rows == col.column->size()) |
689 | 0 | << absl::Substitute("pre_read_rows = $0 , column rows = $1, col name = $2", |
690 | 0 | pre_read_rows, col.column->size(), col.name); |
691 | 12 | } |
692 | 6 | #endif |
693 | | |
694 | 6 | bool can_filter_all = false; |
695 | 6 | bool resize_first_column = _lazy_read_ctx.resize_first_column; |
696 | 6 | if (resize_first_column && _iceberg_rowid_params.enabled) { |
697 | 0 | int row_id_idx = block->get_position_by_name(doris::BeConsts::ICEBERG_ROWID_COL); |
698 | 0 | if (row_id_idx == 0) { |
699 | 0 | resize_first_column = false; |
700 | 0 | } |
701 | 0 | } |
702 | 6 | { |
703 | 6 | SCOPED_RAW_TIMER(&_predicate_filter_time); |
704 | | |
705 | | // generate filter vector |
706 | 6 | if (resize_first_column) { |
707 | | // VExprContext.execute has an optimization, the filtering is executed when block->rows() > 0 |
708 | | // The following process may be tricky and time-consuming, but we have no other way. |
709 | 6 | block->get_by_position(0).column->assume_mutable()->resize(pre_read_rows); |
710 | 6 | } |
711 | 6 | result_filter.assign(pre_read_rows, static_cast<unsigned char>(1)); |
712 | 6 | std::vector<IColumn::Filter*> filters; |
713 | 6 | if (_position_delete_ctx.has_filter) { |
714 | 0 | filters.push_back(_pos_delete_filter_ptr.get()); |
715 | 0 | } |
716 | | |
717 | 6 | VExprContextSPtrs filter_contexts; |
718 | 12 | for (auto& conjunct : _filter_conjuncts) { |
719 | 12 | filter_contexts.emplace_back(conjunct); |
720 | 12 | } |
721 | | |
722 | 6 | { |
723 | 6 | RETURN_IF_ERROR(VExprContext::execute_conjuncts(filter_contexts, &filters, block, |
724 | 6 | &result_filter, &can_filter_all)); |
725 | 6 | } |
726 | | |
727 | | // Condition cache MISS: mark granules with surviving rows |
728 | 6 | if (!can_filter_all) { |
729 | 3 | _mark_condition_cache_granules(result_filter.data(), pre_read_rows, batch_base_row); |
730 | 3 | } |
731 | | |
732 | 6 | if (resize_first_column) { |
733 | | // We have to clean the first column to insert right data. |
734 | 6 | block->get_by_position(0).column->assume_mutable()->clear(); |
735 | 6 | } |
736 | 6 | } |
737 | | |
738 | 0 | const uint8_t* __restrict filter_map_data = result_filter.data(); |
739 | 6 | filter_map_ptr = std::make_unique<FilterMap>(); |
740 | 6 | RETURN_IF_ERROR(filter_map_ptr->init(filter_map_data, pre_read_rows, can_filter_all)); |
741 | 6 | if (filter_map_ptr->filter_all()) { |
742 | 3 | { |
743 | 3 | SCOPED_RAW_TIMER(&_predicate_filter_time); |
744 | 3 | for (const auto& col : _lazy_read_ctx.predicate_columns.first) { |
745 | | // clean block to read predicate columns |
746 | 3 | block->get_by_position((*_col_name_to_block_idx)[col]) |
747 | 3 | .column->assume_mutable() |
748 | 3 | ->clear(); |
749 | 3 | } |
750 | 3 | for (const auto& col : _lazy_read_ctx.predicate_partition_columns) { |
751 | 3 | block->get_by_position((*_col_name_to_block_idx)[col.first]) |
752 | 3 | .column->assume_mutable() |
753 | 3 | ->clear(); |
754 | 3 | } |
755 | 3 | for (const auto& col : _lazy_read_ctx.predicate_missing_columns) { |
756 | 0 | block->get_by_position((*_col_name_to_block_idx)[col.first]) |
757 | 0 | .column->assume_mutable() |
758 | 0 | ->clear(); |
759 | 0 | } |
760 | 3 | if (_row_id_column_iterator_pair.first != nullptr) { |
761 | 0 | block->get_by_position(_row_id_column_iterator_pair.second) |
762 | 0 | .column->assume_mutable() |
763 | 0 | ->clear(); |
764 | 0 | } |
765 | 3 | if (_iceberg_rowid_params.enabled) { |
766 | 0 | int row_id_idx = |
767 | 0 | block->get_position_by_name(doris::BeConsts::ICEBERG_ROWID_COL); |
768 | 0 | if (row_id_idx >= 0) { |
769 | 0 | block->get_by_position(static_cast<size_t>(row_id_idx)) |
770 | 0 | .column->assume_mutable() |
771 | 0 | ->clear(); |
772 | 0 | } |
773 | 0 | } |
774 | 3 | Block::erase_useless_column(block, origin_column_num); |
775 | 3 | } |
776 | | |
777 | 3 | if (!pre_eof) { |
778 | | // If continuous batches are skipped, we can cache them to skip a whole page |
779 | 2 | _cached_filtered_rows += pre_read_rows; |
780 | 2 | if (pre_raw_read_rows >= config::doris_scanner_row_num) { |
781 | 0 | *read_rows = 0; |
782 | 0 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
783 | 0 | return Status::OK(); |
784 | 0 | } |
785 | 2 | } else { // pre_eof |
786 | | // If filter_map_ptr->filter_all() and pre_eof, we can skip whole row group. |
787 | 1 | *read_rows = 0; |
788 | 1 | *batch_eof = true; |
789 | 1 | _lazy_read_filtered_rows += (pre_read_rows + _cached_filtered_rows); |
790 | 1 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
791 | 1 | return Status::OK(); |
792 | 1 | } |
793 | 3 | } else { |
794 | 3 | break; |
795 | 3 | } |
796 | 6 | } |
797 | 3 | if (_state->is_cancelled()) { |
798 | 0 | return Status::Cancelled("cancelled"); |
799 | 0 | } |
800 | | |
801 | 3 | if (filter_map_ptr == nullptr) { |
802 | 0 | DCHECK_EQ(pre_read_rows + _cached_filtered_rows, 0); |
803 | 0 | *read_rows = 0; |
804 | 0 | *batch_eof = true; |
805 | 0 | return Status::OK(); |
806 | 0 | } |
807 | | |
808 | 3 | FilterMap& filter_map = *filter_map_ptr; |
809 | 3 | DorisUniqueBufferPtr<uint8_t> rebuild_filter_map = nullptr; |
810 | 3 | if (_cached_filtered_rows != 0) { |
811 | 0 | RETURN_IF_ERROR(_rebuild_filter_map(filter_map, rebuild_filter_map, pre_read_rows)); |
812 | 0 | pre_read_rows += _cached_filtered_rows; |
813 | 0 | _cached_filtered_rows = 0; |
814 | 0 | } |
815 | | |
816 | | // lazy read columns |
817 | 3 | size_t lazy_read_rows; |
818 | 3 | bool lazy_eof; |
819 | 3 | RETURN_IF_ERROR(_read_column_data(block, _lazy_read_ctx.lazy_read_columns, pre_read_rows, |
820 | 3 | &lazy_read_rows, &lazy_eof, filter_map)); |
821 | | |
822 | 3 | if (pre_read_rows != lazy_read_rows) { |
823 | 0 | return Status::Corruption("Can't read the same number of rows when doing lazy read"); |
824 | 0 | } |
825 | | // pre_eof ^ lazy_eof |
826 | | // we set pre_read_rows as batch_size for lazy read columns, so pre_eof != lazy_eof |
827 | | |
828 | | // filter data in predicate columns, and remove filter column |
829 | 3 | { |
830 | 3 | SCOPED_RAW_TIMER(&_predicate_filter_time); |
831 | 3 | if (filter_map.has_filter()) { |
832 | 0 | std::vector<uint32_t> predicate_columns = _lazy_read_ctx.all_predicate_col_ids; |
833 | 0 | if (_iceberg_rowid_params.enabled) { |
834 | 0 | int row_id_idx = block->get_position_by_name(doris::BeConsts::ICEBERG_ROWID_COL); |
835 | 0 | if (row_id_idx >= 0 && |
836 | 0 | std::find(predicate_columns.begin(), predicate_columns.end(), |
837 | 0 | static_cast<uint32_t>(row_id_idx)) == predicate_columns.end()) { |
838 | 0 | predicate_columns.push_back(static_cast<uint32_t>(row_id_idx)); |
839 | 0 | } |
840 | 0 | } |
841 | 0 | RETURN_IF_CATCH_EXCEPTION( |
842 | 0 | Block::filter_block_internal(block, predicate_columns, result_filter)); |
843 | 0 | Block::erase_useless_column(block, origin_column_num); |
844 | |
|
845 | 3 | } else { |
846 | 3 | Block::erase_useless_column(block, origin_column_num); |
847 | 3 | } |
848 | 3 | } |
849 | | |
850 | 3 | RETURN_IF_ERROR(_convert_dict_cols_to_string_cols(block)); |
851 | | |
852 | 3 | size_t column_num = block->columns(); |
853 | 3 | size_t column_size = 0; |
854 | 12 | for (int i = 0; i < column_num; ++i) { |
855 | 9 | size_t cz = block->get_by_position(i).column->size(); |
856 | 9 | if (column_size != 0 && cz != 0) { |
857 | 6 | DCHECK_EQ(column_size, cz); |
858 | 6 | } |
859 | 9 | if (cz != 0) { |
860 | 9 | column_size = cz; |
861 | 9 | } |
862 | 9 | } |
863 | 3 | _lazy_read_filtered_rows += pre_read_rows - column_size; |
864 | 3 | *read_rows = column_size; |
865 | | |
866 | 3 | *batch_eof = pre_eof; |
867 | 3 | RETURN_IF_ERROR(_fill_partition_columns(block, column_size, _lazy_read_ctx.partition_columns)); |
868 | 3 | RETURN_IF_ERROR(_fill_missing_columns(block, column_size, _lazy_read_ctx.missing_columns)); |
869 | 3 | #ifndef NDEBUG |
870 | 9 | for (auto col : *block) { |
871 | 9 | col.column->sanity_check(); |
872 | 9 | DCHECK(block->rows() == col.column->size()) |
873 | 0 | << absl::Substitute("block rows = $0 , column rows = $1, col name = $2", |
874 | 0 | block->rows(), col.column->size(), col.name); |
875 | 9 | } |
876 | 3 | #endif |
877 | 3 | return Status::OK(); |
878 | 3 | } |
879 | | |
880 | | Status RowGroupReader::_rebuild_filter_map(FilterMap& filter_map, |
881 | | DorisUniqueBufferPtr<uint8_t>& filter_map_data, |
882 | 0 | size_t pre_read_rows) const { |
883 | 0 | if (_cached_filtered_rows == 0) { |
884 | 0 | return Status::OK(); |
885 | 0 | } |
886 | 0 | size_t total_rows = _cached_filtered_rows + pre_read_rows; |
887 | 0 | if (filter_map.filter_all()) { |
888 | 0 | RETURN_IF_ERROR(filter_map.init(nullptr, total_rows, true)); |
889 | 0 | return Status::OK(); |
890 | 0 | } |
891 | | |
892 | 0 | filter_map_data = make_unique_buffer<uint8_t>(total_rows); |
893 | 0 | auto* map = filter_map_data.get(); |
894 | 0 | for (size_t i = 0; i < _cached_filtered_rows; ++i) { |
895 | 0 | map[i] = 0; |
896 | 0 | } |
897 | 0 | const uint8_t* old_map = filter_map.filter_map_data(); |
898 | 0 | if (old_map == nullptr) { |
899 | | // select_vector.filter_all() == true is already built. |
900 | 0 | for (size_t i = _cached_filtered_rows; i < total_rows; ++i) { |
901 | 0 | map[i] = 1; |
902 | 0 | } |
903 | 0 | } else { |
904 | 0 | memcpy(map + _cached_filtered_rows, old_map, pre_read_rows); |
905 | 0 | } |
906 | 0 | RETURN_IF_ERROR(filter_map.init(map, total_rows, false)); |
907 | 0 | return Status::OK(); |
908 | 0 | } |
909 | | |
910 | | Status RowGroupReader::_fill_partition_columns( |
911 | | Block* block, size_t rows, |
912 | | const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>& |
913 | 52 | partition_columns) { |
914 | 52 | DataTypeSerDe::FormatOptions _text_formatOptions; |
915 | 52 | for (const auto& kv : partition_columns) { |
916 | 15 | auto doris_column = block->get_by_position((*_col_name_to_block_idx)[kv.first]).column; |
917 | | // obtained from block*, it is a mutable object. |
918 | 15 | auto* col_ptr = const_cast<IColumn*>(doris_column.get()); |
919 | 15 | const auto& [value, slot_desc] = kv.second; |
920 | 15 | auto _text_serde = slot_desc->get_data_type_ptr()->get_serde(); |
921 | 15 | Slice slice(value.data(), value.size()); |
922 | 15 | uint64_t num_deserialized = 0; |
923 | | // Be careful when reading empty rows from parquet row groups. |
924 | 15 | if (_text_serde->deserialize_column_from_fixed_json(*col_ptr, slice, rows, |
925 | 15 | &num_deserialized, |
926 | 15 | _text_formatOptions) != Status::OK()) { |
927 | 0 | return Status::InternalError("Failed to fill partition column: {}={}", |
928 | 0 | slot_desc->col_name(), value); |
929 | 0 | } |
930 | 15 | if (num_deserialized != rows) { |
931 | 0 | return Status::InternalError( |
932 | 0 | "Failed to fill partition column: {}={} ." |
933 | 0 | "Number of rows expected to be written : {}, number of rows actually written : " |
934 | 0 | "{}", |
935 | 0 | slot_desc->col_name(), value, num_deserialized, rows); |
936 | 0 | } |
937 | 15 | } |
938 | 52 | return Status::OK(); |
939 | 52 | } |
940 | | |
941 | | Status RowGroupReader::_fill_missing_columns( |
942 | | Block* block, size_t rows, |
943 | 52 | const std::unordered_map<std::string, VExprContextSPtr>& missing_columns) { |
944 | 52 | for (const auto& kv : missing_columns) { |
945 | 0 | if (!_col_name_to_block_idx->contains(kv.first)) { |
946 | 0 | return Status::InternalError("Missing column: {} not found in block {}", kv.first, |
947 | 0 | block->dump_structure()); |
948 | 0 | } |
949 | 0 | if (kv.second == nullptr) { |
950 | | // no default column, fill with null |
951 | 0 | auto mutable_column = block->get_by_position((*_col_name_to_block_idx)[kv.first]) |
952 | 0 | .column->assume_mutable(); |
953 | 0 | auto* nullable_column = assert_cast<ColumnNullable*>(mutable_column.get()); |
954 | 0 | nullable_column->insert_many_defaults(rows); |
955 | 0 | } else { |
956 | | // fill with default value |
957 | 0 | const auto& ctx = kv.second; |
958 | 0 | ColumnPtr result_column_ptr; |
959 | | // PT1 => dest primitive type |
960 | 0 | RETURN_IF_ERROR(ctx->execute(block, result_column_ptr)); |
961 | 0 | if (result_column_ptr->use_count() == 1) { |
962 | | // call resize because the first column of _src_block_ptr may not be filled by reader, |
963 | | // so _src_block_ptr->rows() may return wrong result, cause the column created by `ctx->execute()` |
964 | | // has only one row. |
965 | 0 | auto mutable_column = result_column_ptr->assume_mutable(); |
966 | 0 | mutable_column->resize(rows); |
967 | | // result_column_ptr maybe a ColumnConst, convert it to a normal column |
968 | 0 | result_column_ptr = result_column_ptr->convert_to_full_column_if_const(); |
969 | 0 | auto origin_column_type = |
970 | 0 | block->get_by_position((*_col_name_to_block_idx)[kv.first]).type; |
971 | 0 | bool is_nullable = origin_column_type->is_nullable(); |
972 | 0 | block->replace_by_position( |
973 | 0 | (*_col_name_to_block_idx)[kv.first], |
974 | 0 | is_nullable ? make_nullable(result_column_ptr) : result_column_ptr); |
975 | 0 | } |
976 | 0 | } |
977 | 0 | } |
978 | 52 | return Status::OK(); |
979 | 52 | } |
980 | | |
981 | | Status RowGroupReader::_read_empty_batch(size_t batch_size, size_t* read_rows, bool* batch_eof, |
982 | 3 | bool* modify_row_ids) { |
983 | 3 | *modify_row_ids = false; |
984 | 3 | if (_position_delete_ctx.has_filter) { |
985 | 0 | int64_t start_row_id = _position_delete_ctx.current_row_id; |
986 | 0 | int64_t end_row_id = std::min(_position_delete_ctx.current_row_id + (int64_t)batch_size, |
987 | 0 | _position_delete_ctx.last_row_id); |
988 | 0 | int64_t num_delete_rows = 0; |
989 | 0 | auto before_index = _position_delete_ctx.index; |
990 | 0 | while (_position_delete_ctx.index < _position_delete_ctx.end_index) { |
991 | 0 | const int64_t& delete_row_id = |
992 | 0 | _position_delete_ctx.delete_rows[_position_delete_ctx.index]; |
993 | 0 | if (delete_row_id < start_row_id) { |
994 | 0 | _position_delete_ctx.index++; |
995 | 0 | before_index = _position_delete_ctx.index; |
996 | 0 | } else if (delete_row_id < end_row_id) { |
997 | 0 | num_delete_rows++; |
998 | 0 | _position_delete_ctx.index++; |
999 | 0 | } else { // delete_row_id >= end_row_id |
1000 | 0 | break; |
1001 | 0 | } |
1002 | 0 | } |
1003 | 0 | *read_rows = end_row_id - start_row_id - num_delete_rows; |
1004 | 0 | _position_delete_ctx.current_row_id = end_row_id; |
1005 | 0 | *batch_eof = _position_delete_ctx.current_row_id == _position_delete_ctx.last_row_id; |
1006 | |
|
1007 | 0 | if (_row_id_column_iterator_pair.first != nullptr || _iceberg_rowid_params.enabled) { |
1008 | 0 | *modify_row_ids = true; |
1009 | 0 | _current_batch_row_ids.clear(); |
1010 | 0 | _current_batch_row_ids.resize(*read_rows); |
1011 | 0 | size_t idx = 0; |
1012 | 0 | for (auto id = start_row_id; id < end_row_id; id++) { |
1013 | 0 | if (before_index < _position_delete_ctx.index && |
1014 | 0 | id == _position_delete_ctx.delete_rows[before_index]) { |
1015 | 0 | before_index++; |
1016 | 0 | continue; |
1017 | 0 | } |
1018 | 0 | _current_batch_row_ids[idx++] = (rowid_t)id; |
1019 | 0 | } |
1020 | 0 | } |
1021 | 3 | } else { |
1022 | 3 | if (batch_size < _remaining_rows) { |
1023 | 2 | *read_rows = batch_size; |
1024 | 2 | _remaining_rows -= batch_size; |
1025 | 2 | *batch_eof = false; |
1026 | 2 | } else { |
1027 | 1 | *read_rows = _remaining_rows; |
1028 | 1 | _remaining_rows = 0; |
1029 | 1 | *batch_eof = true; |
1030 | 1 | } |
1031 | 3 | if (_iceberg_rowid_params.enabled) { |
1032 | 0 | *modify_row_ids = true; |
1033 | 0 | RETURN_IF_ERROR(_get_current_batch_row_id(*read_rows)); |
1034 | 0 | } |
1035 | 3 | } |
1036 | 3 | _total_read_rows += *read_rows; |
1037 | 3 | return Status::OK(); |
1038 | 3 | } |
1039 | | |
1040 | 5 | Status RowGroupReader::_get_current_batch_row_id(size_t read_rows) { |
1041 | 5 | _current_batch_row_ids.clear(); |
1042 | 5 | _current_batch_row_ids.resize(read_rows); |
1043 | | |
1044 | 5 | int64_t idx = 0; |
1045 | 5 | int64_t read_range_rows = 0; |
1046 | 19 | for (size_t range_idx = 0; range_idx < _read_ranges.range_size(); range_idx++) { |
1047 | 14 | auto range = _read_ranges.get_range(range_idx); |
1048 | 14 | if (read_rows == 0) { |
1049 | 0 | break; |
1050 | 0 | } |
1051 | 14 | if (read_range_rows + (range.to() - range.from()) > _total_read_rows) { |
1052 | 14 | int64_t fi = |
1053 | 14 | std::max(_total_read_rows, read_range_rows) - read_range_rows + range.from(); |
1054 | 14 | size_t len = std::min(read_rows, (size_t)(std::max(range.to(), fi) - fi)); |
1055 | | |
1056 | 14 | read_rows -= len; |
1057 | | |
1058 | 28 | for (auto i = 0; i < len; i++) { |
1059 | 14 | _current_batch_row_ids[idx++] = |
1060 | 14 | (rowid_t)(fi + i + _current_row_group_idx.first_row); |
1061 | 14 | } |
1062 | 14 | } |
1063 | 14 | read_range_rows += range.to() - range.from(); |
1064 | 14 | } |
1065 | 5 | return Status::OK(); |
1066 | 5 | } |
1067 | | |
1068 | | Status RowGroupReader::_fill_row_id_columns(Block* block, size_t read_rows, |
1069 | 49 | bool is_current_row_ids) { |
1070 | 49 | if (_row_id_column_iterator_pair.first != nullptr) { |
1071 | 5 | if (!is_current_row_ids) { |
1072 | 5 | RETURN_IF_ERROR(_get_current_batch_row_id(read_rows)); |
1073 | 5 | } |
1074 | 5 | auto col = block->get_by_position(_row_id_column_iterator_pair.second) |
1075 | 5 | .column->assume_mutable(); |
1076 | 5 | RETURN_IF_ERROR(_row_id_column_iterator_pair.first->read_by_rowids( |
1077 | 5 | _current_batch_row_ids.data(), _current_batch_row_ids.size(), col)); |
1078 | 5 | } |
1079 | | |
1080 | 49 | return Status::OK(); |
1081 | 49 | } |
1082 | | |
1083 | | Status RowGroupReader::_append_iceberg_rowid_column(Block* block, size_t read_rows, |
1084 | 49 | bool is_current_row_ids) { |
1085 | 49 | if (!_iceberg_rowid_params.enabled) { |
1086 | 49 | return Status::OK(); |
1087 | 49 | } |
1088 | 0 | if (!is_current_row_ids) { |
1089 | 0 | RETURN_IF_ERROR(_get_current_batch_row_id(read_rows)); |
1090 | 0 | } |
1091 | | |
1092 | 0 | int row_id_idx = block->get_position_by_name(doris::BeConsts::ICEBERG_ROWID_COL); |
1093 | 0 | if (row_id_idx >= 0) { |
1094 | 0 | auto& col_with_type = block->get_by_position(static_cast<size_t>(row_id_idx)); |
1095 | 0 | MutableColumnPtr row_id_column; |
1096 | 0 | RETURN_IF_ERROR(build_iceberg_rowid_column( |
1097 | 0 | col_with_type.type, _iceberg_rowid_params.file_path, _current_batch_row_ids, |
1098 | 0 | _iceberg_rowid_params.partition_spec_id, _iceberg_rowid_params.partition_data_json, |
1099 | 0 | &row_id_column)); |
1100 | 0 | col_with_type.column = std::move(row_id_column); |
1101 | 0 | } else { |
1102 | 0 | DataTypes field_types; |
1103 | 0 | field_types.push_back(std::make_shared<DataTypeString>()); |
1104 | 0 | field_types.push_back(std::make_shared<DataTypeInt64>()); |
1105 | 0 | field_types.push_back(std::make_shared<DataTypeInt32>()); |
1106 | 0 | field_types.push_back(std::make_shared<DataTypeString>()); |
1107 | |
|
1108 | 0 | std::vector<std::string> field_names = {"file_path", "row_position", "partition_spec_id", |
1109 | 0 | "partition_data"}; |
1110 | |
|
1111 | 0 | auto row_id_type = std::make_shared<DataTypeStruct>(field_types, field_names); |
1112 | 0 | MutableColumnPtr row_id_column; |
1113 | 0 | RETURN_IF_ERROR(build_iceberg_rowid_column( |
1114 | 0 | row_id_type, _iceberg_rowid_params.file_path, _current_batch_row_ids, |
1115 | 0 | _iceberg_rowid_params.partition_spec_id, _iceberg_rowid_params.partition_data_json, |
1116 | 0 | &row_id_column)); |
1117 | 0 | int insert_pos = _iceberg_rowid_params.row_id_column_pos; |
1118 | 0 | if (insert_pos < 0 || insert_pos > static_cast<int>(block->columns())) { |
1119 | 0 | insert_pos = static_cast<int>(block->columns()); |
1120 | 0 | } |
1121 | 0 | block->insert(static_cast<size_t>(insert_pos), |
1122 | 0 | ColumnWithTypeAndName(std::move(row_id_column), row_id_type, |
1123 | 0 | doris::BeConsts::ICEBERG_ROWID_COL)); |
1124 | 0 | } |
1125 | | |
1126 | 0 | if (_col_name_to_block_idx != nullptr) { |
1127 | 0 | *_col_name_to_block_idx = block->get_name_to_pos_map(); |
1128 | 0 | } |
1129 | |
|
1130 | 0 | return Status::OK(); |
1131 | 0 | } |
1132 | | |
1133 | 46 | Status RowGroupReader::_build_pos_delete_filter(size_t read_rows) { |
1134 | 46 | if (!_position_delete_ctx.has_filter) { |
1135 | 46 | _pos_delete_filter_ptr.reset(nullptr); |
1136 | 46 | _total_read_rows += read_rows; |
1137 | 46 | return Status::OK(); |
1138 | 46 | } |
1139 | 0 | _pos_delete_filter_ptr.reset(new IColumn::Filter(read_rows, 1)); |
1140 | 0 | auto* __restrict _pos_delete_filter_data = _pos_delete_filter_ptr->data(); |
1141 | 0 | while (_position_delete_ctx.index < _position_delete_ctx.end_index) { |
1142 | 0 | const int64_t delete_row_index_in_row_group = |
1143 | 0 | _position_delete_ctx.delete_rows[_position_delete_ctx.index] - |
1144 | 0 | _position_delete_ctx.first_row_id; |
1145 | 0 | int64_t read_range_rows = 0; |
1146 | 0 | size_t remaining_read_rows = _total_read_rows + read_rows; |
1147 | 0 | for (size_t range_idx = 0; range_idx < _read_ranges.range_size(); range_idx++) { |
1148 | 0 | auto range = _read_ranges.get_range(range_idx); |
1149 | 0 | if (delete_row_index_in_row_group < range.from()) { |
1150 | 0 | ++_position_delete_ctx.index; |
1151 | 0 | break; |
1152 | 0 | } else if (delete_row_index_in_row_group < range.to()) { |
1153 | 0 | int64_t index = (delete_row_index_in_row_group - range.from()) + read_range_rows - |
1154 | 0 | _total_read_rows; |
1155 | 0 | if (index > read_rows - 1) { |
1156 | 0 | _total_read_rows += read_rows; |
1157 | 0 | return Status::OK(); |
1158 | 0 | } |
1159 | 0 | _pos_delete_filter_data[index] = 0; |
1160 | 0 | ++_position_delete_ctx.index; |
1161 | 0 | break; |
1162 | 0 | } else { // delete_row >= range.last_row |
1163 | 0 | } |
1164 | | |
1165 | 0 | int64_t range_size = range.to() - range.from(); |
1166 | | // Don't search next range when there is no remaining_read_rows. |
1167 | 0 | if (remaining_read_rows <= range_size) { |
1168 | 0 | _total_read_rows += read_rows; |
1169 | 0 | return Status::OK(); |
1170 | 0 | } else { |
1171 | 0 | remaining_read_rows -= range_size; |
1172 | 0 | read_range_rows += range_size; |
1173 | 0 | } |
1174 | 0 | } |
1175 | 0 | } |
1176 | 0 | _total_read_rows += read_rows; |
1177 | 0 | return Status::OK(); |
1178 | 0 | } |
1179 | | |
1180 | | // need exception safety |
1181 | | Status RowGroupReader::_filter_block(Block* block, int column_to_keep, |
1182 | 34 | const std::vector<uint32_t>& columns_to_filter) { |
1183 | 34 | if (_pos_delete_filter_ptr) { |
1184 | 0 | RETURN_IF_CATCH_EXCEPTION( |
1185 | 0 | Block::filter_block_internal(block, columns_to_filter, (*_pos_delete_filter_ptr))); |
1186 | 0 | } |
1187 | 34 | Block::erase_useless_column(block, column_to_keep); |
1188 | | |
1189 | 34 | return Status::OK(); |
1190 | 34 | } |
1191 | | |
1192 | 6 | Status RowGroupReader::_rewrite_dict_predicates() { |
1193 | 6 | SCOPED_RAW_TIMER(&_dict_filter_rewrite_time); |
1194 | 6 | for (auto it = _dict_filter_cols.begin(); it != _dict_filter_cols.end();) { |
1195 | 2 | std::string& dict_filter_col_name = it->first; |
1196 | 2 | int slot_id = it->second; |
1197 | | // 1. Get dictionary values to a string column. |
1198 | 2 | MutableColumnPtr dict_value_column = ColumnString::create(); |
1199 | 2 | bool has_dict = false; |
1200 | 2 | RETURN_IF_ERROR(_column_readers[dict_filter_col_name]->read_dict_values_to_column( |
1201 | 2 | dict_value_column, &has_dict)); |
1202 | 2 | #ifndef NDEBUG |
1203 | 2 | dict_value_column->sanity_check(); |
1204 | 2 | #endif |
1205 | 2 | size_t dict_value_column_size = dict_value_column->size(); |
1206 | 2 | DCHECK(has_dict); |
1207 | | // 2. Build a temp block from the dict string column, then execute conjuncts and filter block. |
1208 | | // 2.1 Build a temp block from the dict string column to match the conjuncts executing. |
1209 | 2 | Block temp_block; |
1210 | 2 | int dict_pos = -1; |
1211 | 2 | int index = 0; |
1212 | 4 | for (const auto slot_desc : _tuple_descriptor->slots()) { |
1213 | 4 | if (slot_desc->id() == slot_id) { |
1214 | 2 | auto data_type = slot_desc->get_data_type_ptr(); |
1215 | 2 | if (data_type->is_nullable()) { |
1216 | 0 | temp_block.insert( |
1217 | 0 | {ColumnNullable::create( |
1218 | 0 | std::move( |
1219 | 0 | dict_value_column), // NOLINT(bugprone-use-after-move) |
1220 | 0 | ColumnUInt8::create(dict_value_column_size, 0)), |
1221 | 0 | std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>()), |
1222 | 0 | ""}); |
1223 | 2 | } else { |
1224 | 2 | temp_block.insert( |
1225 | 2 | {std::move(dict_value_column), std::make_shared<DataTypeString>(), ""}); |
1226 | 2 | } |
1227 | 2 | dict_pos = index; |
1228 | | |
1229 | 2 | } else { |
1230 | 2 | temp_block.insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
1231 | 2 | slot_desc->get_data_type_ptr(), |
1232 | 2 | slot_desc->col_name())); |
1233 | 2 | } |
1234 | 4 | ++index; |
1235 | 4 | } |
1236 | | |
1237 | | // 2.2 Execute conjuncts. |
1238 | 2 | VExprContextSPtrs ctxs; |
1239 | 2 | auto iter = _slot_id_to_filter_conjuncts->find(slot_id); |
1240 | 2 | if (iter != _slot_id_to_filter_conjuncts->end()) { |
1241 | 2 | for (auto& ctx : iter->second) { |
1242 | 2 | ctxs.push_back(ctx); |
1243 | 2 | } |
1244 | 2 | } else { |
1245 | 0 | std::stringstream msg; |
1246 | 0 | msg << "_slot_id_to_filter_conjuncts: slot_id [" << slot_id << "] not found"; |
1247 | 0 | return Status::NotFound(msg.str()); |
1248 | 0 | } |
1249 | | |
1250 | 2 | if (dict_pos != 0) { |
1251 | | // VExprContext.execute has an optimization, the filtering is executed when block->rows() > 0 |
1252 | | // The following process may be tricky and time-consuming, but we have no other way. |
1253 | 0 | temp_block.get_by_position(0).column->assume_mutable()->resize(dict_value_column_size); |
1254 | 0 | } |
1255 | 2 | IColumn::Filter result_filter(temp_block.rows(), 1); |
1256 | 2 | bool can_filter_all; |
1257 | 2 | { |
1258 | 2 | RETURN_IF_ERROR(VExprContext::execute_conjuncts(ctxs, nullptr, &temp_block, |
1259 | 2 | &result_filter, &can_filter_all)); |
1260 | 2 | } |
1261 | 2 | if (dict_pos != 0) { |
1262 | | // We have to clean the first column to insert right data. |
1263 | 0 | temp_block.get_by_position(0).column->assume_mutable()->clear(); |
1264 | 0 | } |
1265 | | |
1266 | | // If can_filter_all = true, can filter this row group. |
1267 | 2 | if (can_filter_all) { |
1268 | 2 | _is_row_group_filtered = true; |
1269 | 2 | return Status::OK(); |
1270 | 2 | } |
1271 | | |
1272 | | // 3. Get dict codes. |
1273 | 0 | std::vector<int32_t> dict_codes; |
1274 | 0 | for (size_t i = 0; i < result_filter.size(); ++i) { |
1275 | 0 | if (result_filter[i]) { |
1276 | 0 | dict_codes.emplace_back(i); |
1277 | 0 | } |
1278 | 0 | } |
1279 | | |
1280 | | // About Performance: if dict_column size is too large, it will generate a large IN filter. |
1281 | 0 | if (dict_codes.size() > MAX_DICT_CODE_PREDICATE_TO_REWRITE) { |
1282 | 0 | it = _dict_filter_cols.erase(it); |
1283 | 0 | for (auto& ctx : ctxs) { |
1284 | 0 | _filter_conjuncts.push_back(ctx); |
1285 | 0 | } |
1286 | 0 | continue; |
1287 | 0 | } |
1288 | | |
1289 | | // 4. Rewrite conjuncts. |
1290 | 0 | RETURN_IF_ERROR(_rewrite_dict_conjuncts( |
1291 | 0 | dict_codes, slot_id, temp_block.get_by_position(dict_pos).column->is_nullable())); |
1292 | 0 | ++it; |
1293 | 0 | } |
1294 | 4 | return Status::OK(); |
1295 | 6 | } |
1296 | | |
1297 | | Status RowGroupReader::_rewrite_dict_conjuncts(std::vector<int32_t>& dict_codes, int slot_id, |
1298 | 0 | bool is_nullable) { |
1299 | 0 | VExprSPtr root; |
1300 | 0 | if (dict_codes.size() == 1) { |
1301 | 0 | { |
1302 | 0 | TFunction fn; |
1303 | 0 | TFunctionName fn_name; |
1304 | 0 | fn_name.__set_db_name(""); |
1305 | 0 | fn_name.__set_function_name("eq"); |
1306 | 0 | fn.__set_name(fn_name); |
1307 | 0 | fn.__set_binary_type(TFunctionBinaryType::BUILTIN); |
1308 | 0 | std::vector<TTypeDesc> arg_types; |
1309 | 0 | arg_types.push_back(create_type_desc(PrimitiveType::TYPE_INT)); |
1310 | 0 | arg_types.push_back(create_type_desc(PrimitiveType::TYPE_INT)); |
1311 | 0 | fn.__set_arg_types(arg_types); |
1312 | 0 | fn.__set_ret_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
1313 | 0 | fn.__set_has_var_args(false); |
1314 | |
|
1315 | 0 | TExprNode texpr_node; |
1316 | 0 | texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
1317 | 0 | texpr_node.__set_node_type(TExprNodeType::BINARY_PRED); |
1318 | 0 | texpr_node.__set_opcode(TExprOpcode::EQ); |
1319 | 0 | texpr_node.__set_fn(fn); |
1320 | 0 | texpr_node.__set_num_children(2); |
1321 | 0 | texpr_node.__set_is_nullable(is_nullable); |
1322 | 0 | root = VectorizedFnCall::create_shared(texpr_node); |
1323 | 0 | } |
1324 | 0 | { |
1325 | 0 | SlotDescriptor* slot = nullptr; |
1326 | 0 | const std::vector<SlotDescriptor*>& slots = _tuple_descriptor->slots(); |
1327 | 0 | for (auto each : slots) { |
1328 | 0 | if (each->id() == slot_id) { |
1329 | 0 | slot = each; |
1330 | 0 | break; |
1331 | 0 | } |
1332 | 0 | } |
1333 | 0 | root->add_child(VSlotRef::create_shared(slot)); |
1334 | 0 | } |
1335 | 0 | { |
1336 | 0 | TExprNode texpr_node; |
1337 | 0 | texpr_node.__set_node_type(TExprNodeType::INT_LITERAL); |
1338 | 0 | texpr_node.__set_type(create_type_desc(TYPE_INT)); |
1339 | 0 | TIntLiteral int_literal; |
1340 | 0 | int_literal.__set_value(dict_codes[0]); |
1341 | 0 | texpr_node.__set_int_literal(int_literal); |
1342 | 0 | texpr_node.__set_is_nullable(is_nullable); |
1343 | 0 | root->add_child(VLiteral::create_shared(texpr_node)); |
1344 | 0 | } |
1345 | 0 | } else { |
1346 | 0 | { |
1347 | 0 | TTypeDesc type_desc = create_type_desc(PrimitiveType::TYPE_BOOLEAN); |
1348 | 0 | TExprNode node; |
1349 | 0 | node.__set_type(type_desc); |
1350 | 0 | node.__set_node_type(TExprNodeType::IN_PRED); |
1351 | 0 | node.in_predicate.__set_is_not_in(false); |
1352 | 0 | node.__set_opcode(TExprOpcode::FILTER_IN); |
1353 | | // VdirectInPredicate assume is_nullable = false. |
1354 | 0 | node.__set_is_nullable(false); |
1355 | |
|
1356 | 0 | std::shared_ptr<HybridSetBase> hybrid_set( |
1357 | 0 | create_set(PrimitiveType::TYPE_INT, dict_codes.size(), false)); |
1358 | 0 | for (int j = 0; j < dict_codes.size(); ++j) { |
1359 | 0 | hybrid_set->insert(&dict_codes[j]); |
1360 | 0 | } |
1361 | 0 | root = VDirectInPredicate::create_shared(node, hybrid_set); |
1362 | 0 | } |
1363 | 0 | { |
1364 | 0 | SlotDescriptor* slot = nullptr; |
1365 | 0 | const std::vector<SlotDescriptor*>& slots = _tuple_descriptor->slots(); |
1366 | 0 | for (auto each : slots) { |
1367 | 0 | if (each->id() == slot_id) { |
1368 | 0 | slot = each; |
1369 | 0 | break; |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | root->add_child(VSlotRef::create_shared(slot)); |
1373 | 0 | } |
1374 | 0 | } |
1375 | 0 | VExprContextSPtr rewritten_conjunct_ctx = VExprContext::create_shared(root); |
1376 | 0 | RETURN_IF_ERROR(rewritten_conjunct_ctx->prepare(_state, *_row_descriptor)); |
1377 | 0 | RETURN_IF_ERROR(rewritten_conjunct_ctx->open(_state)); |
1378 | 0 | _dict_filter_conjuncts.push_back(rewritten_conjunct_ctx); |
1379 | 0 | _filter_conjuncts.push_back(rewritten_conjunct_ctx); |
1380 | 0 | return Status::OK(); |
1381 | 0 | } |
1382 | | |
1383 | 44 | Status RowGroupReader::_convert_dict_cols_to_string_cols(Block* block) { |
1384 | 44 | for (auto& dict_filter_cols : _dict_filter_cols) { |
1385 | 0 | if (!_col_name_to_block_idx->contains(dict_filter_cols.first)) { |
1386 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, |
1387 | 0 | "Wrong read column '{}' in parquet file, block: {}", |
1388 | 0 | dict_filter_cols.first, block->dump_structure()); |
1389 | 0 | } |
1390 | 0 | ColumnWithTypeAndName& column_with_type_and_name = |
1391 | 0 | block->get_by_position((*_col_name_to_block_idx)[dict_filter_cols.first]); |
1392 | 0 | const ColumnPtr& column = column_with_type_and_name.column; |
1393 | 0 | if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*column)) { |
1394 | 0 | const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr(); |
1395 | 0 | const auto* dict_column = assert_cast<const ColumnInt32*>(nested_column.get()); |
1396 | 0 | DCHECK(dict_column); |
1397 | |
|
1398 | 0 | auto string_column = DORIS_TRY( |
1399 | 0 | _column_readers[dict_filter_cols.first]->convert_dict_column_to_string_column( |
1400 | 0 | dict_column)); |
1401 | |
|
1402 | 0 | column_with_type_and_name.type = |
1403 | 0 | std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>()); |
1404 | 0 | block->replace_by_position( |
1405 | 0 | (*_col_name_to_block_idx)[dict_filter_cols.first], |
1406 | 0 | ColumnNullable::create(std::move(string_column), |
1407 | 0 | nullable_column->get_null_map_column_ptr())); |
1408 | 0 | } else { |
1409 | 0 | const auto* dict_column = assert_cast<const ColumnInt32*>(column.get()); |
1410 | 0 | auto string_column = DORIS_TRY( |
1411 | 0 | _column_readers[dict_filter_cols.first]->convert_dict_column_to_string_column( |
1412 | 0 | dict_column)); |
1413 | |
|
1414 | 0 | column_with_type_and_name.type = std::make_shared<DataTypeString>(); |
1415 | 0 | block->replace_by_position((*_col_name_to_block_idx)[dict_filter_cols.first], |
1416 | 0 | std::move(string_column)); |
1417 | 0 | } |
1418 | 0 | } |
1419 | 44 | return Status::OK(); |
1420 | 44 | } |
1421 | | |
1422 | 37 | ParquetColumnReader::ColumnStatistics RowGroupReader::merged_column_statistics() { |
1423 | 37 | ParquetColumnReader::ColumnStatistics st; |
1424 | 106 | for (auto& reader : _column_readers) { |
1425 | 106 | auto ost = reader.second->column_statistics(); |
1426 | 106 | st.merge(ost); |
1427 | 106 | } |
1428 | 37 | return st; |
1429 | 37 | } |
1430 | | #include "common/compile_check_end.h" |
1431 | | |
1432 | | } // namespace doris |