|
| 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 <format> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#include <nlohmann/json.hpp> |
| 26 | + |
| 27 | +#include "iceberg/expression/expressions.h" |
| 28 | +#include "iceberg/expression/json_serde_internal.h" |
| 29 | +#include "iceberg/expression/literal.h" |
| 30 | +#include "iceberg/util/checked_cast.h" |
| 31 | +#include "iceberg/util/json_util_internal.h" |
| 32 | +#include "iceberg/util/macros.h" |
| 33 | + |
| 34 | +namespace iceberg { |
| 35 | +namespace { |
| 36 | +// Expression type strings |
| 37 | +constexpr std::string_view kTypeTrue = "true"; |
| 38 | +constexpr std::string_view kTypeFalse = "false"; |
| 39 | +constexpr std::string_view kTypeEq = "eq"; |
| 40 | +constexpr std::string_view kTypeAnd = "and"; |
| 41 | +constexpr std::string_view kTypeOr = "or"; |
| 42 | +constexpr std::string_view kTypeNot = "not"; |
| 43 | +constexpr std::string_view kTypeIn = "in"; |
| 44 | +constexpr std::string_view kTypeNotIn = "not-in"; |
| 45 | +constexpr std::string_view kTypeLt = "lt"; |
| 46 | +constexpr std::string_view kTypeLtEq = "lt-eq"; |
| 47 | +constexpr std::string_view kTypeGt = "gt"; |
| 48 | +constexpr std::string_view kTypeGtEq = "gt-eq"; |
| 49 | +constexpr std::string_view kTypeNotEq = "not-eq"; |
| 50 | +constexpr std::string_view kTypeStartsWith = "starts-with"; |
| 51 | +constexpr std::string_view kTypeNotStartsWith = "not-starts-with"; |
| 52 | +constexpr std::string_view kTypeIsNull = "is-null"; |
| 53 | +constexpr std::string_view kTypeNotNull = "not-null"; |
| 54 | +constexpr std::string_view kTypeIsNan = "is-nan"; |
| 55 | +constexpr std::string_view kTypeNotNan = "not-nan"; |
| 56 | +} // namespace |
| 57 | + |
| 58 | +bool IsUnaryOperation(Expression::Operation op) { |
| 59 | + switch (op) { |
| 60 | + case Expression::Operation::kIsNull: |
| 61 | + case Expression::Operation::kNotNull: |
| 62 | + case Expression::Operation::kIsNan: |
| 63 | + case Expression::Operation::kNotNan: |
| 64 | + return true; |
| 65 | + default: |
| 66 | + return false; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +bool IsSetOperation(Expression::Operation op) { |
| 71 | + switch (op) { |
| 72 | + case Expression::Operation::kIn: |
| 73 | + case Expression::Operation::kNotIn: |
| 74 | + return true; |
| 75 | + default: |
| 76 | + return false; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +Result<Expression::Operation> OperationTypeFromString(const std::string_view typeStr) { |
| 81 | + if (typeStr == kTypeTrue) return Expression::Operation::kTrue; |
| 82 | + if (typeStr == kTypeFalse) return Expression::Operation::kFalse; |
| 83 | + if (typeStr == kTypeAnd) return Expression::Operation::kAnd; |
| 84 | + if (typeStr == kTypeOr) return Expression::Operation::kOr; |
| 85 | + if (typeStr == kTypeNot) return Expression::Operation::kNot; |
| 86 | + if (typeStr == kTypeEq) return Expression::Operation::kEq; |
| 87 | + if (typeStr == kTypeNotEq) return Expression::Operation::kNotEq; |
| 88 | + if (typeStr == kTypeLt) return Expression::Operation::kLt; |
| 89 | + if (typeStr == kTypeLtEq) return Expression::Operation::kLtEq; |
| 90 | + if (typeStr == kTypeGt) return Expression::Operation::kGt; |
| 91 | + if (typeStr == kTypeGtEq) return Expression::Operation::kGtEq; |
| 92 | + if (typeStr == kTypeIn) return Expression::Operation::kIn; |
| 93 | + if (typeStr == kTypeNotIn) return Expression::Operation::kNotIn; |
| 94 | + if (typeStr == kTypeIsNull) return Expression::Operation::kIsNull; |
| 95 | + if (typeStr == kTypeNotNull) return Expression::Operation::kNotNull; |
| 96 | + if (typeStr == kTypeIsNan) return Expression::Operation::kIsNan; |
| 97 | + if (typeStr == kTypeNotNan) return Expression::Operation::kNotNan; |
| 98 | + if (typeStr == kTypeStartsWith) return Expression::Operation::kStartsWith; |
| 99 | + if (typeStr == kTypeNotStartsWith) return Expression::Operation::kNotStartsWith; |
| 100 | + |
| 101 | + return JsonParseError("Unknown expression type: {}", typeStr); |
| 102 | +} |
| 103 | + |
| 104 | +std::string_view ToStringOperationType(Expression::Operation op) { |
| 105 | + switch (op) { |
| 106 | + case Expression::Operation::kTrue: |
| 107 | + return kTypeTrue; |
| 108 | + case Expression::Operation::kFalse: |
| 109 | + return kTypeFalse; |
| 110 | + case Expression::Operation::kAnd: |
| 111 | + return kTypeAnd; |
| 112 | + case Expression::Operation::kOr: |
| 113 | + return kTypeOr; |
| 114 | + case Expression::Operation::kNot: |
| 115 | + return kTypeNot; |
| 116 | + case Expression::Operation::kEq: |
| 117 | + return kTypeEq; |
| 118 | + case Expression::Operation::kNotEq: |
| 119 | + return kTypeNotEq; |
| 120 | + case Expression::Operation::kLt: |
| 121 | + return kTypeLt; |
| 122 | + case Expression::Operation::kLtEq: |
| 123 | + return kTypeLtEq; |
| 124 | + case Expression::Operation::kGt: |
| 125 | + return kTypeGt; |
| 126 | + case Expression::Operation::kGtEq: |
| 127 | + return kTypeGtEq; |
| 128 | + case Expression::Operation::kIn: |
| 129 | + return kTypeIn; |
| 130 | + case Expression::Operation::kNotIn: |
| 131 | + return kTypeNotIn; |
| 132 | + case Expression::Operation::kIsNull: |
| 133 | + return kTypeIsNull; |
| 134 | + case Expression::Operation::kNotNull: |
| 135 | + return kTypeNotNull; |
| 136 | + case Expression::Operation::kIsNan: |
| 137 | + return kTypeIsNan; |
| 138 | + case Expression::Operation::kNotNan: |
| 139 | + return kTypeNotNan; |
| 140 | + case Expression::Operation::kStartsWith: |
| 141 | + return kTypeStartsWith; |
| 142 | + case Expression::Operation::kNotStartsWith: |
| 143 | + return kTypeNotStartsWith; |
| 144 | + default: |
| 145 | + ICEBERG_CHECK_OR_DIE(false, "Unknown expression operation."); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) { |
| 150 | + // Handle boolean |
| 151 | + if (json.is_boolean()) { |
| 152 | + return json.get<bool>() |
| 153 | + ? internal::checked_pointer_cast<Expression>(True::Instance()) |
| 154 | + : internal::checked_pointer_cast<Expression>(False::Instance()); |
| 155 | + } |
| 156 | + return JsonParseError("Only booleans are currently supported."); |
| 157 | +} |
| 158 | + |
| 159 | +nlohmann::json ExpressionToJson(const Expression& expr) { |
| 160 | + switch (expr.op()) { |
| 161 | + case Expression::Operation::kTrue: |
| 162 | + return true; |
| 163 | + |
| 164 | + case Expression::Operation::kFalse: |
| 165 | + return false; |
| 166 | + default: |
| 167 | + ICEBERG_CHECK_OR_DIE(false, "Only booleans are currently supported."); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace iceberg |
0 commit comments