Skip to content

Commit 1871546

Browse files
authored
[improvement](be) Optimize projected fixed-width Parquet predicate filtering (apache#65934)
### What problem does this PR solve? Issue Number: N/A Related PR: apache#65921 Problem Summary: When a fixed-width Parquet predicate column was also projected, Doris materialized all selected predicate values and compacted the complete column after filtering. The native decoder had already produced the values needed by the raw predicate, so this added an avoidable materialize-and-compact pass. The overhead is visible in TPC-DS Q88. ### What is changed? Evaluate eligible predicates on decoded fixed-width values and append only matching values to the projected column in the same decoder pass. - Generalize the PLAIN-specific consumer and reader APIs into a fixed-width raw filter-and-project path. - Support PLAIN and BYTE_STREAM_SPLIT for identity-width INT32, INT64, FLOAT, and DOUBLE values. - Support DELTA_BINARY_PACKED for INT32 and INT64 values. - Prevalidate advertised chunk encodings before consuming definition levels. - Reject an unexpected unsupported late-page encoding instead of attempting a fallback after cursor progress. - Keep the existing dictionary-id filter and dictionary materialization path unchanged. The raw decoder cannot rewind after predicate evaluation. Therefore, when the predicate column is projected, survivors must be appended before the encoded values and definition-level cursor are consumed. Predicate-only columns retain their placeholder behavior. Nested columns, converted logical types, residual/delete predicates, unsupported expressions, and unsupported encodings continue to use the existing materializing fallback. ### Microbenchmark The reader microbenchmark from apache#65921 was run with a Release build, warm fixture cache, CPU 8, a one-second minimum time, and 10 repetitions. The matrix adds BYTE_STREAM_SPLIT and DELTA_BINARY_PACKED coverage with: - 10% alternating NULLs; - predicate-only and predicate-projected modes; - 1%, 10%, 50%, and 90% selectivity. All 16 combinations completed with the expected raw and selected row counts. Projected, 10% selectivity: | Encoding | CPU ns/raw row, master | CPU ns/raw row, PR | Improvement | CPU CV master / PR | |---|---:|---:|---:|---:| | BYTE_STREAM_SPLIT | 36.41 | 33.43 | 8.2% | 1.25% / 1.47% | | DELTA_BINARY_PACKED | 38.75 | 36.84 | 4.9% | 5.40% / 2.94% | The Delta baseline ran under sustained host load, so its result should be treated as directional. The original projected PLAIN results remain: | Selectivity | CPU ns/raw row, master | CPU ns/raw row, PR | Improvement | |---:|---:|---:|---:| | 1% | 32.80 | 30.80 | 6.1% | | 10% | 33.95 | 31.82 | 6.3% | | 50% | 37.76 | 35.05 | 7.2% | | 90% | 42.06 | 37.66 | 10.5% | ### Release note None ### Check List (For Author) - Test: Unit Test and Manual test - 17 targeted ASAN unit tests covering raw expression evaluation, nullable mapping, projected PLAIN/BSS/Delta scans, unsupported-type fallback, mixed-page safety, residual predicates, and benchmark matrix constraints - Release benchmark target linked successfully - 16-case BSS/Delta reader microbenchmark matrix - Before/after projected-filter microbenchmarks - clang-format 16, check-format, and `git diff --check` - clang-tidy was attempted but blocked by existing master/toolchain errors: unmatched `NOLINTEND` in `be/src/core/types.h` and missing `stddef.h` from the configured toolchain - Behavior changed: No. This extends the existing raw predicate optimization to equivalent fixed-width decoder output. - Does this need documentation: No
1 parent 58e8734 commit 1871546

15 files changed

Lines changed: 355 additions & 138 deletions

‎be/benchmark/parquet/parquet_benchmark_scenarios.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ inline std::vector<ReaderScenario> reader_scenarios() {
126126
scenario.operation = ReaderOperation::PREDICATE_SCAN;
127127
add(scenario);
128128
}
129+
for (const auto encoding : {Encoding::BYTE_STREAM_SPLIT, Encoding::DELTA_BINARY_PACKED}) {
130+
for (const int selectivity : {1, 10, 50, 90}) {
131+
for (const auto projection :
132+
{Projection::PREDICATE_ONLY, Projection::PREDICATE_PROJECTED}) {
133+
auto scenario = baseline;
134+
scenario.operation = ReaderOperation::PREDICATE_SCAN;
135+
scenario.encoding = encoding;
136+
scenario.selectivity_percent = selectivity;
137+
scenario.projection = projection;
138+
add(scenario);
139+
}
140+
}
141+
}
129142
for (const int width : {4, 32, 128, 512}) {
130143
for (const int predicate_position : {0, width - 1}) {
131144
auto scenario = baseline;

‎be/src/format_v2/parquet/parquet_profile.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ void ParquetProfile::init(RuntimeProfile* profile) {
175175
TUnit::BYTES, parquet_profile, 1);
176176
predicate_compaction_count = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PredicateCompactionCount",
177177
TUnit::UNIT, parquet_profile, 1);
178-
plain_predicate_direct_batches = ADD_CHILD_COUNTER_WITH_LEVEL(
179-
profile, "PlainPredicateDirectBatches", TUnit::UNIT, parquet_profile, 1);
180-
plain_predicate_direct_rows = ADD_CHILD_COUNTER_WITH_LEVEL(profile, "PlainPredicateDirectRows",
181-
TUnit::UNIT, parquet_profile, 1);
178+
fixed_width_predicate_direct_batches = ADD_CHILD_COUNTER_WITH_LEVEL(
179+
profile, "FixedWidthPredicateDirectBatches", TUnit::UNIT, parquet_profile, 1);
180+
fixed_width_predicate_direct_rows = ADD_CHILD_COUNTER_WITH_LEVEL(
181+
profile, "FixedWidthPredicateDirectRows", TUnit::UNIT, parquet_profile, 1);
182182
dict_filter_rewrite_time =
183183
ADD_CHILD_TIMER_WITH_LEVEL(profile, "DictFilterRewriteTime", parquet_profile, 1);
184184
dict_filter_expr_rewrite_time =
@@ -316,8 +316,8 @@ ParquetScanProfile ParquetProfile::scan_profile() const {
316316
.predicate_compaction_time = predicate_compaction_time,
317317
.predicate_compaction_bytes = predicate_compaction_bytes,
318318
.predicate_compaction_count = predicate_compaction_count,
319-
.plain_predicate_direct_batches = plain_predicate_direct_batches,
320-
.plain_predicate_direct_rows = plain_predicate_direct_rows,
319+
.fixed_width_predicate_direct_batches = fixed_width_predicate_direct_batches,
320+
.fixed_width_predicate_direct_rows = fixed_width_predicate_direct_rows,
321321
.dict_filter_rewrite_time = dict_filter_rewrite_time,
322322
.dict_filter_expr_rewrite_time = dict_filter_expr_rewrite_time,
323323
.dict_filter_read_dict_time = dict_filter_read_dict_time,

‎be/src/format_v2/parquet/parquet_profile.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ struct ParquetScanProfile {
8888
RuntimeProfile::Counter* predicate_compaction_time = nullptr;
8989
RuntimeProfile::Counter* predicate_compaction_bytes = nullptr;
9090
RuntimeProfile::Counter* predicate_compaction_count = nullptr;
91-
RuntimeProfile::Counter* plain_predicate_direct_batches = nullptr;
92-
RuntimeProfile::Counter* plain_predicate_direct_rows = nullptr;
91+
RuntimeProfile::Counter* fixed_width_predicate_direct_batches = nullptr;
92+
RuntimeProfile::Counter* fixed_width_predicate_direct_rows = nullptr;
9393
RuntimeProfile::Counter* dict_filter_rewrite_time = nullptr; // dictionary rewrite time (ns)
9494
RuntimeProfile::Counter* dict_filter_expr_rewrite_time =
9595
nullptr; // expression/residual rewrite time (ns)
@@ -203,8 +203,8 @@ struct ParquetProfile {
203203
RuntimeProfile::Counter* predicate_compaction_time = nullptr;
204204
RuntimeProfile::Counter* predicate_compaction_bytes = nullptr;
205205
RuntimeProfile::Counter* predicate_compaction_count = nullptr;
206-
RuntimeProfile::Counter* plain_predicate_direct_batches = nullptr;
207-
RuntimeProfile::Counter* plain_predicate_direct_rows = nullptr;
206+
RuntimeProfile::Counter* fixed_width_predicate_direct_batches = nullptr;
207+
RuntimeProfile::Counter* fixed_width_predicate_direct_rows = nullptr;
208208
RuntimeProfile::Counter* dict_filter_rewrite_time = nullptr;
209209
RuntimeProfile::Counter* dict_filter_expr_rewrite_time = nullptr;
210210
RuntimeProfile::Counter* dict_filter_read_dict_time = nullptr;

‎be/src/format_v2/parquet/parquet_scan.cpp‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,11 +1551,11 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
15511551
auto read_predicate_column =
15521552
[&](ParquetColumnReader* column_reader, size_t block_position,
15531553
format::LocalColumnId local_id, const VExprContextSPtrs* single_column_conjuncts,
1554-
bool* used_dictionary_filter, bool* used_plain_filter) -> Status {
1554+
bool* used_dictionary_filter, bool* used_fixed_width_filter) -> Status {
15551555
DORIS_CHECK(used_dictionary_filter != nullptr);
1556-
DORIS_CHECK(used_plain_filter != nullptr);
1556+
DORIS_CHECK(used_fixed_width_filter != nullptr);
15571557
*used_dictionary_filter = false;
1558-
*used_plain_filter = false;
1558+
*used_fixed_width_filter = false;
15591559
DCHECK(remove_nullable(column_reader->type())
15601560
->equals(*remove_nullable(file_block->get_by_position(block_position).type)))
15611561
<< column_reader->type()->get_name() << " "
@@ -1597,8 +1597,7 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
15971597
}
15981598

15991599
if (single_column_conjuncts != nullptr &&
1600-
!residual_predicate_positions.contains(block_position) &&
1601-
request.is_predicate_only(local_id)) {
1600+
!residual_predicate_positions.contains(block_position)) {
16021601
VExprSPtrs direct_conjuncts;
16031602
direct_conjuncts.reserve(single_column_conjuncts->size());
16041603
std::ranges::transform(*single_column_conjuncts, std::back_inserter(direct_conjuncts),
@@ -1607,13 +1606,19 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
16071606
const uint16_t selected_rows_before = *selected_rows;
16081607
IColumn::Filter compact_filter;
16091608
bool used_filter = false;
1610-
RETURN_IF_ERROR(column_reader->select_with_plain_filter(
1609+
const bool predicate_only = request.is_predicate_only(local_id);
1610+
// The raw decoder cannot rewind after evaluating encoded fixed-width values.
1611+
// Project survivors in that pass when output still needs the predicate column.
1612+
IColumn* projected_column = predicate_only ? nullptr : column.get();
1613+
RETURN_IF_ERROR(column_reader->select_with_fixed_width_filter(
16111614
*selection, *selected_rows, batch_rows, direct_conjuncts,
1612-
cast_set<int>(block_position), &compact_filter, &used_filter));
1615+
cast_set<int>(block_position), projected_column, &compact_filter,
1616+
&used_filter));
16131617
if (used_filter) {
16141618
DORIS_CHECK_EQ(compact_filter.size(), selected_rows_before);
1615-
update_counter_if_not_null(_scan_profile.plain_predicate_direct_batches, 1);
1616-
update_counter_if_not_null(_scan_profile.plain_predicate_direct_rows,
1619+
update_counter_if_not_null(_scan_profile.fixed_width_predicate_direct_batches,
1620+
1);
1621+
update_counter_if_not_null(_scan_profile.fixed_width_predicate_direct_rows,
16171622
selected_rows_before);
16181623
const uint16_t new_selected_rows = count_selected_rows(compact_filter);
16191624
const auto filtered_rows = static_cast<int64_t>(selected_rows_before) -
@@ -1625,15 +1630,19 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
16251630
*selected_rows = apply_compact_filter_to_selection(
16261631
compact_filter, selection, selected_rows_before);
16271632
}
1628-
// This slot is absent from every residual/delete conjunct, so no later
1629-
// expression can observe its payload. Keep only the block row-shape contract.
1630-
auto placeholder = column->clone_empty();
1631-
placeholder->insert_many_defaults(*selected_rows);
1632-
file_block->replace_by_position(block_position, std::move(placeholder));
1633+
if (predicate_only) {
1634+
// This slot is absent from every residual/delete conjunct, so no later
1635+
// expression can observe its payload. Keep only the block row-shape contract.
1636+
auto placeholder = column->clone_empty();
1637+
placeholder->insert_many_defaults(*selected_rows);
1638+
file_block->replace_by_position(block_position, std::move(placeholder));
1639+
} else {
1640+
file_block->replace_by_position(block_position, std::move(column));
1641+
}
16331642
read_column_positions.push_back(cast_set<uint32_t>(block_position));
16341643
remember_column_selection(cast_set<uint32_t>(block_position));
16351644
*predicate_columns_filtered = true;
1636-
*used_plain_filter = true;
1645+
*used_fixed_width_filter = true;
16371646
return Status::OK();
16381647
}
16391648
}
@@ -1761,10 +1770,10 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
17611770
auto position_it = request.local_positions.find(fid);
17621771
DORIS_CHECK(position_it != request.local_positions.end());
17631772
bool used_dictionary_filter = false;
1764-
bool used_plain_filter = false;
1773+
bool used_fixed_width_filter = false;
17651774
RETURN_IF_ERROR(read_predicate_column(column_reader.get(), position_it->second.value(),
17661775
fid, nullptr, &used_dictionary_filter,
1767-
&used_plain_filter));
1776+
&used_fixed_width_filter));
17681777
}
17691778
return Status::OK();
17701779
};
@@ -1803,21 +1812,21 @@ Status ParquetScanScheduler::read_filter_columns(int64_t batch_rows,
18031812
predicate_batch_sequence);
18041813
const int64_t start_ns = sample ? MonotonicNanos() : 0;
18051814
bool used_dictionary_filter = false;
1806-
bool used_plain_filter = false;
1815+
bool used_fixed_width_filter = false;
18071816
const auto conjunct_it = schedule.single_column_conjuncts.find(block_position);
18081817
const VExprContextSPtrs* column_conjuncts =
18091818
conjunct_it == schedule.single_column_conjuncts.end() ? nullptr
18101819
: &conjunct_it->second;
18111820
RETURN_IF_ERROR(read_predicate_column(reader_it->second.get(), block_position, fid,
18121821
column_conjuncts, &used_dictionary_filter,
1813-
&used_plain_filter));
1822+
&used_fixed_width_filter));
18141823
if (*selected_rows != 0 && conjunct_it != schedule.single_column_conjuncts.end()) {
18151824
if (used_dictionary_filter) {
18161825
const auto residual_it = _current_dictionary_residual_conjuncts.find(fid);
18171826
DORIS_CHECK(residual_it != _current_dictionary_residual_conjuncts.end());
18181827
RETURN_IF_ERROR(execute_scheduled_dictionary_residual_conjuncts_with_profile(
18191828
residual_it->second));
1820-
} else if (!used_plain_filter) {
1829+
} else if (!used_fixed_width_filter) {
18211830
RETURN_IF_ERROR(execute_scheduled_conjuncts_with_profile(conjunct_it->second));
18221831
}
18231832
}

‎be/src/format_v2/parquet/reader/column_reader.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ Status ParquetColumnReader::select_with_dictionary_filter(const SelectionVector&
8181
name());
8282
}
8383

84-
Status ParquetColumnReader::select_with_plain_filter(const SelectionVector&, uint16_t, int64_t,
85-
const VExprSPtrs&, int,
86-
IColumn::Filter* row_filter,
87-
bool* used_filter) {
84+
Status ParquetColumnReader::select_with_fixed_width_filter(const SelectionVector&, uint16_t,
85+
int64_t, const VExprSPtrs&, int,
86+
IColumn*, IColumn::Filter* row_filter,
87+
bool* used_filter) {
8888
DORIS_CHECK(row_filter != nullptr);
8989
DORIS_CHECK(used_filter != nullptr);
9090
row_filter->clear();

‎be/src/format_v2/parquet/reader/column_reader.h‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ class ParquetColumnReader {
6161
MutableColumnPtr& column,
6262
IColumn::Filter* row_filter, bool* used_filter);
6363

64-
// Consume batch_rows and evaluate eligible fixed-width PLAIN values without constructing a
65-
// predicate column. Implementations must leave the cursor untouched when used_filter=false.
66-
virtual Status select_with_plain_filter(const SelectionVector& selection,
67-
uint16_t selected_rows, int64_t batch_rows,
68-
const VExprSPtrs& conjuncts, int column_id,
69-
IColumn::Filter* row_filter, bool* used_filter);
64+
// Consume batch_rows and evaluate eligible fixed-width values without first constructing a
65+
// complete predicate column. Append survivors when projected_column is non-null. Implementations
66+
// must leave the cursor untouched when used_filter=false.
67+
virtual Status select_with_fixed_width_filter(const SelectionVector& selection,
68+
uint16_t selected_rows, int64_t batch_rows,
69+
const VExprSPtrs& conjuncts, int column_id,
70+
IColumn* projected_column,
71+
IColumn::Filter* row_filter, bool* used_filter);
7072

7173
// Native statistics are cumulative and can be recursively aggregated for complex columns.
7274
// Flush once at the scheduler batch boundary instead of snapshotting after each operation.

‎be/src/format_v2/parquet/reader/native/column_chunk_reader.cpp‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,15 @@ Status decode_selected_nullable_values(IColumn& column, const DataTypeSerDe& ser
653653
return Status::OK();
654654
}
655655

656-
class PlainPredicateConsumer final : public ParquetFixedValueConsumer {
656+
class FixedWidthPredicateConsumer final : public ParquetFixedValueConsumer {
657657
public:
658-
PlainPredicateConsumer(const VExprSPtrs& conjuncts, DataTypePtr data_type, int column_id,
659-
IColumn::Filter* matches)
658+
FixedWidthPredicateConsumer(const VExprSPtrs& conjuncts, DataTypePtr data_type, int column_id,
659+
IColumn::Filter* matches, IColumn* projected_column)
660660
: _conjuncts(conjuncts),
661661
_data_type(std::move(data_type)),
662662
_column_id(column_id),
663-
_matches(matches) {
663+
_matches(matches),
664+
_projected_column(projected_column) {
664665
DORIS_CHECK(_matches != nullptr);
665666
}
666667

@@ -672,6 +673,23 @@ class PlainPredicateConsumer final : public ParquetFixedValueConsumer {
672673
_data_type, _column_id,
673674
_matches->data() + old_size));
674675
}
676+
if (_projected_column != nullptr) {
677+
size_t row = 0;
678+
while (row < num_values) {
679+
while (row < num_values && (*_matches)[old_size + row] == 0) {
680+
++row;
681+
}
682+
const size_t run_begin = row;
683+
while (row < num_values && (*_matches)[old_size + row] != 0) {
684+
++row;
685+
}
686+
if (row != run_begin) {
687+
_projected_column->insert_many_raw_data(
688+
reinterpret_cast<const char*>(values + run_begin * value_width),
689+
row - run_begin);
690+
}
691+
}
692+
}
675693
return Status::OK();
676694
}
677695

@@ -680,6 +698,7 @@ class PlainPredicateConsumer final : public ParquetFixedValueConsumer {
680698
DataTypePtr _data_type;
681699
int _column_id;
682700
IColumn::Filter* _matches;
701+
IColumn* _projected_column;
683702
};
684703

685704
} // namespace
@@ -1428,11 +1447,10 @@ Status ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::materialize_values(
14281447
}
14291448

14301449
template <bool IN_COLLECTION, bool OFFSET_INDEX>
1431-
bool ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::can_filter_plain_values(
1450+
bool ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::can_filter_fixed_width_values(
14321451
const VExprSPtrs& conjuncts, int column_id) const {
1433-
if (conjuncts.empty() || _current_encoding != tparquet::Encoding::PLAIN ||
1434-
(_metadata.type != tparquet::Type::INT32 && _metadata.type != tparquet::Type::INT64 &&
1435-
_metadata.type != tparquet::Type::FLOAT && _metadata.type != tparquet::Type::DOUBLE)) {
1452+
if (conjuncts.empty() ||
1453+
!supports_raw_fixed_filter_encoding(_current_encoding, _metadata.type)) {
14361454
return false;
14371455
}
14381456
const auto primitive_type = remove_nullable(_field_schema->data_type)->get_primitive_type();
@@ -1453,17 +1471,17 @@ bool ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::can_filter_plain_values(
14531471
}
14541472

14551473
template <bool IN_COLLECTION, bool OFFSET_INDEX>
1456-
Status ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::filter_plain_values(
1474+
Status ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::filter_fixed_width_values(
14571475
const VExprSPtrs& conjuncts, int column_id, ColumnSelectVector& select_vector,
1458-
NullMap* selected_nulls, IColumn::Filter* physical_matches, IColumn::Filter* row_filter,
1459-
bool* used_filter) {
1476+
NullMap* selected_nulls, IColumn::Filter* physical_matches, IColumn* projected_column,
1477+
IColumn::Filter* row_filter, bool* used_filter) {
14601478
DORIS_CHECK(selected_nulls != nullptr);
14611479
DORIS_CHECK(physical_matches != nullptr);
14621480
DORIS_CHECK(row_filter != nullptr);
14631481
DORIS_CHECK(used_filter != nullptr);
14641482
*used_filter = false;
14651483
row_filter->clear();
1466-
if (!can_filter_plain_values(conjuncts, column_id)) {
1484+
if (!can_filter_fixed_width_values(conjuncts, column_id)) {
14671485
return Status::OK();
14681486
}
14691487
if (UNLIKELY(_remaining_num_values < select_vector.num_values())) {
@@ -1520,8 +1538,8 @@ Status ColumnChunkReader<IN_COLLECTION, OFFSET_INDEX>::filter_plain_values(
15201538
if (selection.selected_values == 0) {
15211539
RETURN_IF_ERROR(_page_decoder->skip_values(selection.total_values));
15221540
} else {
1523-
PlainPredicateConsumer consumer(conjuncts, _field_schema->data_type, column_id,
1524-
physical_matches);
1541+
FixedWidthPredicateConsumer consumer(conjuncts, _field_schema->data_type, column_id,
1542+
physical_matches, projected_column);
15251543
RETURN_IF_ERROR(_page_decoder->decode_selected_fixed_values(selection, consumer));
15261544
DORIS_CHECK_EQ(physical_matches->size(), selection.selected_values);
15271545
}

0 commit comments

Comments
 (0)