|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/expression/evaluator.h" |
| 21 | + |
| 22 | +#include "iceberg/expression/binder.h" |
| 23 | +#include "iceberg/expression/expression_visitor.h" |
| 24 | +#include "iceberg/schema.h" |
| 25 | +#include "iceberg/util/macros.h" |
| 26 | + |
| 27 | +namespace iceberg { |
| 28 | + |
| 29 | +class Evaluator::EvalVisitor : public BoundVisitor<bool> { |
| 30 | + public: |
| 31 | + void UpdateRow(const StructLike* row) { row_ = row; } |
| 32 | + |
| 33 | + Result<bool> AlwaysTrue() override { return true; } |
| 34 | + |
| 35 | + Result<bool> AlwaysFalse() override { return false; } |
| 36 | + |
| 37 | + Result<bool> Not(bool child_result) override { return !child_result; } |
| 38 | + |
| 39 | + Result<bool> And(bool left_result, bool right_result) override { |
| 40 | + return left_result && right_result; |
| 41 | + } |
| 42 | + |
| 43 | + Result<bool> Or(bool left_result, bool right_result) override { |
| 44 | + return left_result || right_result; |
| 45 | + } |
| 46 | + |
| 47 | + Result<bool> IsNull(const std::shared_ptr<BoundTerm>& term) override { |
| 48 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 49 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 50 | + return value.IsNull(); |
| 51 | + } |
| 52 | + |
| 53 | + Result<bool> NotNull(const std::shared_ptr<BoundTerm>& term) override { |
| 54 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 55 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 56 | + return !value.IsNull(); |
| 57 | + } |
| 58 | + |
| 59 | + Result<bool> IsNaN(const std::shared_ptr<BoundTerm>& term) override { |
| 60 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 61 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 62 | + return value.IsNaN(); |
| 63 | + } |
| 64 | + |
| 65 | + Result<bool> NotNaN(const std::shared_ptr<BoundTerm>& term) override { |
| 66 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 67 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 68 | + return !value.IsNaN(); |
| 69 | + } |
| 70 | + |
| 71 | + Result<bool> Lt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override { |
| 72 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 73 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 74 | + return value < lit; |
| 75 | + } |
| 76 | + |
| 77 | + Result<bool> LtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override { |
| 78 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 79 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 80 | + return value <= lit; |
| 81 | + } |
| 82 | + |
| 83 | + Result<bool> Gt(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override { |
| 84 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 85 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 86 | + return value > lit; |
| 87 | + } |
| 88 | + |
| 89 | + Result<bool> GtEq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override { |
| 90 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 91 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 92 | + return value >= lit; |
| 93 | + } |
| 94 | + |
| 95 | + Result<bool> Eq(const std::shared_ptr<BoundTerm>& term, const Literal& lit) override { |
| 96 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 97 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 98 | + return value == lit; |
| 99 | + } |
| 100 | + |
| 101 | + Result<bool> NotEq(const std::shared_ptr<BoundTerm>& term, |
| 102 | + const Literal& lit) override { |
| 103 | + ICEBERG_ASSIGN_OR_RAISE(auto eq_result, Eq(term, lit)); |
| 104 | + return !eq_result; |
| 105 | + } |
| 106 | + |
| 107 | + Result<bool> In(const std::shared_ptr<BoundTerm>& term, |
| 108 | + const BoundSetPredicate::LiteralSet& literal_set) override { |
| 109 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 110 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 111 | + return literal_set.contains(value); |
| 112 | + } |
| 113 | + |
| 114 | + Result<bool> NotIn(const std::shared_ptr<BoundTerm>& term, |
| 115 | + const BoundSetPredicate::LiteralSet& literal_set) override { |
| 116 | + ICEBERG_ASSIGN_OR_RAISE(auto in_result, In(term, literal_set)); |
| 117 | + return !in_result; |
| 118 | + } |
| 119 | + |
| 120 | + Result<bool> StartsWith(const std::shared_ptr<BoundTerm>& term, |
| 121 | + const Literal& lit) override { |
| 122 | + ICEBERG_DCHECK(row_, "Row is not set"); |
| 123 | + ICEBERG_ASSIGN_OR_RAISE(auto value, term->Evaluate(*row_)); |
| 124 | + |
| 125 | + // Both value and literal should be strings |
| 126 | + if (!std::holds_alternative<std::string>(value.value()) || |
| 127 | + !std::holds_alternative<std::string>(lit.value())) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + const auto& str_value = std::get<std::string>(value.value()); |
| 132 | + const auto& str_prefix = std::get<std::string>(lit.value()); |
| 133 | + return str_value.starts_with(str_prefix); |
| 134 | + } |
| 135 | + |
| 136 | + Result<bool> NotStartsWith(const std::shared_ptr<BoundTerm>& term, |
| 137 | + const Literal& lit) override { |
| 138 | + ICEBERG_ASSIGN_OR_RAISE(auto starts_result, StartsWith(term, lit)); |
| 139 | + return !starts_result; |
| 140 | + } |
| 141 | + |
| 142 | + private: |
| 143 | + const StructLike* row_{nullptr}; |
| 144 | +}; |
| 145 | + |
| 146 | +Evaluator::Evaluator(std::shared_ptr<Expression> bound_expr) |
| 147 | + : bound_expr_(std::move(bound_expr)), visitor_(std::make_unique<EvalVisitor>()) {} |
| 148 | + |
| 149 | +Evaluator::~Evaluator() = default; |
| 150 | + |
| 151 | +Result<std::unique_ptr<Evaluator>> Evaluator::Make(const Schema& schema, |
| 152 | + std::shared_ptr<Expression> unbound, |
| 153 | + bool case_sensitive) { |
| 154 | + ICEBERG_ASSIGN_OR_RAISE(auto bound_expr, Binder::Bind(schema, unbound, case_sensitive)); |
| 155 | + return std::unique_ptr<Evaluator>(new Evaluator(std::move(bound_expr))); |
| 156 | +} |
| 157 | + |
| 158 | +Result<bool> Evaluator::Eval(const StructLike& row) const { |
| 159 | + visitor_->UpdateRow(&row); |
| 160 | + return Visit<bool, EvalVisitor>(bound_expr_, *visitor_); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace iceberg |
0 commit comments