|
| 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 | +#pragma once |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | + |
| 22 | +#include "olap/column_predicate.h" |
| 23 | +#include "olap/rowset/segment_v2/bloom_filter.h" |
| 24 | +#include "olap/rowset/segment_v2/inverted_index_reader.h" |
| 25 | +#include "olap/wrapper_field.h" |
| 26 | +#include "vec/columns/column_dictionary.h" |
| 27 | + |
| 28 | +namespace doris { |
| 29 | + |
| 30 | +/** |
| 31 | + * A wrapper predicate that delegate to nested predicate |
| 32 | + * but pass (set/return true) for NULL value rows. |
| 33 | + * |
| 34 | + * At parent, it's used for topn runtime predicate. |
| 35 | +*/ |
| 36 | +class AcceptNullPredicate : public ColumnPredicate { |
| 37 | +public: |
| 38 | + AcceptNullPredicate(ColumnPredicate* nested) |
| 39 | + : ColumnPredicate(nested->column_id(), nested->opposite()), _nested {nested} {} |
| 40 | + |
| 41 | + PredicateType type() const override { return _nested->type(); } |
| 42 | + |
| 43 | + Status evaluate(BitmapIndexIterator* iterator, uint32_t num_rows, |
| 44 | + roaring::Roaring* roaring) const override { |
| 45 | + return _nested->evaluate(iterator, num_rows, roaring); |
| 46 | + } |
| 47 | + |
| 48 | + Status evaluate(const Schema& schema, InvertedIndexIterator* iterator, uint32_t num_rows, |
| 49 | + roaring::Roaring* bitmap) const override { |
| 50 | + return _nested->evaluate(schema, iterator, num_rows, bitmap); |
| 51 | + } |
| 52 | + |
| 53 | + uint16_t evaluate(const vectorized::IColumn& column, uint16_t* sel, |
| 54 | + uint16_t size) const override { |
| 55 | + LOG(FATAL) << "evaluate without flags not supported"; |
| 56 | + // return _nested->evaluate(column, sel, size); |
| 57 | + } |
| 58 | + |
| 59 | + void evaluate_and(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
| 60 | + bool* flags) const override { |
| 61 | + if (column.has_null()) { |
| 62 | + // copy original flags |
| 63 | + bool* original_flags = new bool[size]; |
| 64 | + memcpy(original_flags, flags, size * sizeof(bool)); |
| 65 | + |
| 66 | + // call evaluate_and and restore true for NULL rows |
| 67 | + _nested->evaluate_and(column, sel, size, flags); |
| 68 | + for (uint16_t i = 0; i < size; ++i) { |
| 69 | + uint16_t idx = sel[i]; |
| 70 | + if (original_flags[idx] && !flags[idx] && column.is_null_at(idx)) { |
| 71 | + flags[i] = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + _nested->evaluate_and(column, sel, size, flags); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void evaluate_or(const vectorized::IColumn& column, const uint16_t* sel, uint16_t size, |
| 80 | + bool* flags) const override { |
| 81 | + if (column.has_null()) { |
| 82 | + // call evaluate_or and set true for NULL rows |
| 83 | + _nested->evaluate_or(column, sel, size, flags); |
| 84 | + for (uint16_t i = 0; i < size; ++i) { |
| 85 | + uint16_t idx = sel[i]; |
| 86 | + if (!flags[idx] && column.is_null_at(idx)) { |
| 87 | + flags[i] = true; |
| 88 | + } |
| 89 | + } |
| 90 | + } else { |
| 91 | + _nested->evaluate_or(column, sel, size, flags); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + bool evaluate_and(const std::pair<WrapperField*, WrapperField*>& statistic) const override { |
| 96 | + // there is null in range, accept it |
| 97 | + if (statistic.first->is_null() || statistic.second->is_null()) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + return _nested->evaluate_and(statistic); |
| 101 | + } |
| 102 | + |
| 103 | + bool evaluate_del(const std::pair<WrapperField*, WrapperField*>& statistic) const override { |
| 104 | + return _nested->evaluate_del(statistic); |
| 105 | + } |
| 106 | + |
| 107 | + bool evaluate_and(const BloomFilter* bf) const override { return _nested->evaluate_and(bf); } |
| 108 | + |
| 109 | + bool can_do_bloom_filter() const override { return _nested->can_do_bloom_filter(); } |
| 110 | + |
| 111 | + void evaluate_vec(const vectorized::IColumn& column, uint16_t size, |
| 112 | + bool* flags) const override { |
| 113 | + _nested->evaluate_vec(column, size, flags); |
| 114 | + if (column.has_null()) { |
| 115 | + for (uint16_t i = 0; i < size; ++i) { |
| 116 | + if (!flags[i] && column.is_null_at(i)) { |
| 117 | + // set true for NULL rows |
| 118 | + flags[i] = true; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + void evaluate_and_vec(const vectorized::IColumn& column, uint16_t size, |
| 125 | + bool* flags) const override { |
| 126 | + if (column.has_null()) { |
| 127 | + // copy original flags |
| 128 | + bool* original_flags = new bool[size]; |
| 129 | + memcpy(original_flags, flags, size * sizeof(bool)); |
| 130 | + |
| 131 | + // call evaluate_and_vec and restore true for NULL rows |
| 132 | + _nested->evaluate_and_vec(column, size, flags); |
| 133 | + for (uint16_t i = 0; i < size; ++i) { |
| 134 | + if (original_flags[i] && !flags[i] && column.is_null_at(i)) { |
| 135 | + flags[i] = true; |
| 136 | + } |
| 137 | + } |
| 138 | + } else { |
| 139 | + _nested->evaluate_and_vec(column, size, flags); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + std::string get_search_str() const override { return _nested->get_search_str(); } |
| 144 | + |
| 145 | + std::string debug_string() const override { |
| 146 | + return "passnull predicate for " + _nested->debug_string(); |
| 147 | + } |
| 148 | + |
| 149 | + /// Some predicates need to be cloned for each segment. |
| 150 | + bool need_to_clone() const override { return _nested->need_to_clone(); } |
| 151 | + |
| 152 | + void clone(ColumnPredicate** to) const override { |
| 153 | + if (need_to_clone()) { |
| 154 | + ColumnPredicate* clone_nested; |
| 155 | + _nested->clone(&clone_nested); |
| 156 | + *to = new AcceptNullPredicate(clone_nested); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +private: |
| 161 | + std::string _debug_string() const override { |
| 162 | + return "passnull predicate for " + _nested->debug_string(); |
| 163 | + } |
| 164 | + |
| 165 | + ColumnPredicate* _nested; |
| 166 | +}; |
| 167 | + |
| 168 | +} //namespace doris |
0 commit comments