|
| 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 | +#pragma once |
| 21 | + |
| 22 | +/// \file iceberg/expression.h |
| 23 | +/// Expression interface for Iceberg table operations. |
| 24 | + |
| 25 | +#include <memory> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +#include "iceberg/expected.h" |
| 29 | +#include "iceberg/iceberg_export.h" |
| 30 | +#include "iceberg/result.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +/// \brief Represents a boolean expression tree. |
| 35 | +class ICEBERG_EXPORT Expression { |
| 36 | + public: |
| 37 | + /// Operation types for expressions |
| 38 | + enum class Operation { |
| 39 | + kTrue, |
| 40 | + kFalse, |
| 41 | + kIsNull, |
| 42 | + kNotNull, |
| 43 | + kIsNan, |
| 44 | + kNotNan, |
| 45 | + kLt, |
| 46 | + kLtEq, |
| 47 | + kGt, |
| 48 | + kGtEq, |
| 49 | + kEq, |
| 50 | + kNotEq, |
| 51 | + kIn, |
| 52 | + kNotIn, |
| 53 | + kNot, |
| 54 | + kAnd, |
| 55 | + kOr, |
| 56 | + kStartsWith, |
| 57 | + kNotStartsWith, |
| 58 | + kCount, |
| 59 | + kCountStar, |
| 60 | + kMax, |
| 61 | + kMin |
| 62 | + }; |
| 63 | + |
| 64 | + virtual ~Expression() = default; |
| 65 | + |
| 66 | + /// \brief Returns the operation for an expression node. |
| 67 | + virtual Operation op() const = 0; |
| 68 | + |
| 69 | + /// \brief Returns the negation of this expression, equivalent to not(this). |
| 70 | + virtual Result<std::shared_ptr<Expression>> Negate() const { |
| 71 | + return unexpected( |
| 72 | + Error(ErrorKind::kInvalidExpression, "Expression cannot be negated")); |
| 73 | + } |
| 74 | + |
| 75 | + /// \brief Returns whether this expression will accept the same values as another. |
| 76 | + /// \param other another expression |
| 77 | + /// \return true if the expressions are equivalent |
| 78 | + virtual bool Equals(const Expression& other) const { |
| 79 | + // only bound predicates can be equivalent |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + virtual std::string ToString() const { return "Expression"; } |
| 84 | +}; |
| 85 | + |
| 86 | +/// \brief An Expression that is always true. |
| 87 | +/// |
| 88 | +/// Represents a boolean predicate that always evaluates to true. |
| 89 | +class ICEBERG_EXPORT True : public Expression { |
| 90 | + public: |
| 91 | + /// \brief Returns the singleton instance |
| 92 | + static const std::shared_ptr<True>& Instance(); |
| 93 | + |
| 94 | + Operation op() const override { return Operation::kTrue; } |
| 95 | + |
| 96 | + std::string ToString() const override { return "true"; } |
| 97 | + |
| 98 | + Result<std::shared_ptr<Expression>> Negate() const override; |
| 99 | + |
| 100 | + bool Equals(const Expression& other) const override { |
| 101 | + return other.op() == Operation::kTrue; |
| 102 | + } |
| 103 | + |
| 104 | + private: |
| 105 | + constexpr True() = default; |
| 106 | +}; |
| 107 | + |
| 108 | +/// \brief An expression that is always false. |
| 109 | +class ICEBERG_EXPORT False : public Expression { |
| 110 | + public: |
| 111 | + /// \brief Returns the singleton instance |
| 112 | + static const std::shared_ptr<False>& Instance(); |
| 113 | + |
| 114 | + Operation op() const override { return Operation::kFalse; } |
| 115 | + |
| 116 | + std::string ToString() const override { return "false"; } |
| 117 | + |
| 118 | + Result<std::shared_ptr<Expression>> Negate() const override; |
| 119 | + |
| 120 | + bool Equals(const Expression& other) const override { |
| 121 | + return other.op() == Operation::kFalse; |
| 122 | + } |
| 123 | + |
| 124 | + private: |
| 125 | + constexpr False() = default; |
| 126 | +}; |
| 127 | + |
| 128 | +/// \brief An Expression that represents a logical AND operation between two expressions. |
| 129 | +/// |
| 130 | +/// This expression evaluates to true if and only if both of its child expressions |
| 131 | +/// evaluate to true. |
| 132 | +class ICEBERG_EXPORT And : public Expression { |
| 133 | + public: |
| 134 | + /// \brief Constructs an And expression from two sub-expressions. |
| 135 | + /// |
| 136 | + /// \param left The left operand of the AND expression |
| 137 | + /// \param right The right operand of the AND expression |
| 138 | + And(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right); |
| 139 | + |
| 140 | + /// \brief Returns the left operand of the AND expression. |
| 141 | + /// |
| 142 | + /// \return The left operand of the AND expression |
| 143 | + const std::shared_ptr<Expression>& left() const { return left_; } |
| 144 | + |
| 145 | + /// \brief Returns the right operand of the AND expression. |
| 146 | + /// |
| 147 | + /// \return The right operand of the AND expression |
| 148 | + const std::shared_ptr<Expression>& right() const { return right_; } |
| 149 | + |
| 150 | + Operation op() const override { return Operation::kAnd; } |
| 151 | + |
| 152 | + std::string ToString() const override; |
| 153 | + |
| 154 | + Result<std::shared_ptr<Expression>> Negate() const override; |
| 155 | + |
| 156 | + bool Equals(const Expression& other) const override; |
| 157 | + |
| 158 | + private: |
| 159 | + std::shared_ptr<Expression> left_; |
| 160 | + std::shared_ptr<Expression> right_; |
| 161 | +}; |
| 162 | + |
| 163 | +} // namespace iceberg |
0 commit comments