|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#include <cstddef> |
| 22 | +#include <cstdint> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | +#include <utility> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "arrow/array/array_base.h" |
| 29 | +#include "fmt/format.h" |
| 30 | +#include "paimon/common/predicate/compound_function.h" |
| 31 | +#include "paimon/common/predicate/predicate_filter.h" |
| 32 | +#include "paimon/predicate/predicate.h" |
| 33 | +#include "paimon/result.h" |
| 34 | +#include "paimon/status.h" |
| 35 | + |
| 36 | +namespace arrow { |
| 37 | +class Schema; |
| 38 | +} // namespace arrow |
| 39 | + |
| 40 | +namespace paimon { |
| 41 | +class InternalArray; |
| 42 | +class InternalRow; |
| 43 | + |
| 44 | +/// A `CompoundFunction` to eval and. |
| 45 | +class And : public CompoundFunction { |
| 46 | + public: |
| 47 | + static const And& Instance() { |
| 48 | + static const And instance = And(); |
| 49 | + return instance; |
| 50 | + } |
| 51 | + |
| 52 | + Result<std::vector<char>> Test( |
| 53 | + const arrow::Array& array, |
| 54 | + const std::vector<std::shared_ptr<Predicate>>& children) const override { |
| 55 | + std::vector<char> is_valid(array.length(), true); |
| 56 | + for (const auto& child : children) { |
| 57 | + auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child); |
| 58 | + if (!child_filter) { |
| 59 | + return Status::Invalid( |
| 60 | + fmt::format("child filter {} does not support Test", child->ToString())); |
| 61 | + } |
| 62 | + PAIMON_ASSIGN_OR_RAISE(std::vector<char> child_valid, child_filter->Test(array)); |
| 63 | + for (size_t i = 0; i < is_valid.size(); i++) { |
| 64 | + is_valid[i] = (is_valid[i] & child_valid[i]); |
| 65 | + } |
| 66 | + } |
| 67 | + return is_valid; |
| 68 | + } |
| 69 | + |
| 70 | + Result<bool> Test(const std::shared_ptr<arrow::Schema>& schema, const InternalRow& row, |
| 71 | + const std::vector<std::shared_ptr<Predicate>>& children) const override { |
| 72 | + for (const auto& child : children) { |
| 73 | + auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child); |
| 74 | + if (!child_filter) { |
| 75 | + return Status::Invalid( |
| 76 | + fmt::format("child filter {} does not support Test", child->ToString())); |
| 77 | + } |
| 78 | + PAIMON_ASSIGN_OR_RAISE(bool is_valid, child_filter->Test(schema, row)); |
| 79 | + if (!is_valid) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + Result<bool> Test(const std::shared_ptr<arrow::Schema>& schema, int64_t row_count, |
| 87 | + const InternalRow& min_values, const InternalRow& max_values, |
| 88 | + const InternalArray& null_counts, |
| 89 | + const std::vector<std::shared_ptr<Predicate>>& children) const override { |
| 90 | + for (const auto& child : children) { |
| 91 | + auto child_filter = std::dynamic_pointer_cast<PredicateFilter>(child); |
| 92 | + if (!child_filter) { |
| 93 | + return Status::Invalid( |
| 94 | + fmt::format("child filter {} does not support Test", child->ToString())); |
| 95 | + } |
| 96 | + PAIMON_ASSIGN_OR_RAISE(bool is_valid, child_filter->Test(schema, row_count, min_values, |
| 97 | + max_values, null_counts)); |
| 98 | + if (!is_valid) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + Type GetType() const override { |
| 106 | + return Type::AND; |
| 107 | + } |
| 108 | + const CompoundFunction& Negate() const override; |
| 109 | + std::string ToString() const override { |
| 110 | + return "And"; |
| 111 | + } |
| 112 | + |
| 113 | + private: |
| 114 | + And() = default; |
| 115 | +}; |
| 116 | +} // namespace paimon |
0 commit comments