Coverage Report

Created: 2026-07-14 21:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/materialized_reader_util.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_v2/materialized_reader_util.h"
19
20
#include <utility>
21
22
#include "core/block/block.h"
23
#include "core/column/column_vector.h"
24
#include "core/data_type/data_type_nullable.h"
25
#include "core/types.h"
26
#include "exprs/vexpr_context.h"
27
#include "format_v2/file_reader.h"
28
#include "io/io_common.h"
29
30
namespace doris::format {
31
namespace {
32
33
4
void update_counter(RuntimeProfile::Counter* counter, int64_t value) {
34
4
    if (counter != nullptr) {
35
4
        COUNTER_UPDATE(counter, value);
36
4
    }
37
4
}
38
39
} // namespace
40
41
4
ColumnPtr make_column_nullable_if_needed(ColumnPtr column, const DataTypePtr& target_type) {
42
4
    if (target_type != nullptr && target_type->is_nullable() && column.get() != nullptr &&
43
4
        !column->is_nullable()) {
44
4
        return make_nullable(std::move(column));
45
4
    }
46
0
    return column;
47
4
}
48
49
Status apply_materialized_reader_filters(const FileScanRequest* request, io::IOContext* io_ctx,
50
                                         Block* file_block, size_t* rows,
51
57
                                         const MaterializedReaderFilterProfile* profile) {
52
57
    DORIS_CHECK(file_block != nullptr);
53
57
    DORIS_CHECK(rows != nullptr);
54
57
    const size_t rows_before_filter = *rows;
55
57
    size_t rows_after_delete_filter = rows_before_filter;
56
57
    if (request != nullptr && rows_before_filter > 0 && !request->delete_conjuncts.empty()) {
57
1
        {
58
1
            SCOPED_TIMER(profile == nullptr ? nullptr : profile->delete_conjunct_filter_time);
59
            // Delete conjuncts use the opposite polarity from ordinary predicates: TRUE marks a
60
            // row for deletion. Multiple delete files are combined as a union, so a row remains
61
            // visible only when every delete conjunct returns FALSE.
62
1
            IColumn::Filter keep_filter(rows_before_filter, 1);
63
1
            for (const auto& delete_conjunct : request->delete_conjuncts) {
64
1
                DORIS_CHECK(delete_conjunct != nullptr);
65
1
                int result_column_id = -1;
66
1
                RETURN_IF_ERROR(delete_conjunct->root()->execute(delete_conjunct.get(), file_block,
67
1
                                                                 &result_column_id));
68
1
                DORIS_CHECK(result_column_id >= 0 &&
69
1
                            result_column_id < static_cast<int>(file_block->columns()));
70
1
                const auto& delete_filter =
71
1
                        assert_cast<const ColumnUInt8&>(
72
1
                                *file_block->get_by_position(result_column_id).column)
73
1
                                .get_data();
74
1
                DORIS_CHECK(delete_filter.size() == rows_before_filter);
75
4
                for (size_t row = 0; row < rows_before_filter; ++row) {
76
3
                    keep_filter[row] &= !delete_filter[row];
77
3
                }
78
1
                file_block->erase(result_column_id);
79
1
            }
80
1
            RETURN_IF_CATCH_EXCEPTION(Block::filter_block_internal(file_block, keep_filter));
81
1
        }
82
1
        rows_after_delete_filter =
83
1
                file_block->columns() == 0 ? rows_before_filter : file_block->rows();
84
1
        if (profile != nullptr) {
85
0
            update_counter(profile->rows_filtered_by_delete_conjunct,
86
0
                           rows_before_filter - rows_after_delete_filter);
87
0
        }
88
1
    }
89
90
57
    size_t rows_after_filter = rows_after_delete_filter;
91
57
    if (request != nullptr && rows_after_delete_filter > 0 && !request->conjuncts.empty()) {
92
7
        {
93
7
            SCOPED_TIMER(profile == nullptr ? nullptr : profile->conjunct_filter_time);
94
7
            RETURN_IF_ERROR(VExprContext::filter_block(request->conjuncts, file_block,
95
7
                                                       file_block->columns()));
96
7
        }
97
7
        rows_after_filter =
98
7
                file_block->columns() == 0 ? rows_after_delete_filter : file_block->rows();
99
7
        const auto rows_filtered_by_conjunct = rows_after_delete_filter - rows_after_filter;
100
7
        if (profile != nullptr) {
101
4
            update_counter(profile->rows_filtered_by_conjunct, rows_filtered_by_conjunct);
102
4
        }
103
7
        if (io_ctx != nullptr) {
104
5
            io_ctx->predicate_filtered_rows += rows_filtered_by_conjunct;
105
5
        }
106
7
    }
107
57
    *rows = rows_after_filter;
108
57
    return Status::OK();
109
57
}
110
111
} // namespace doris::format