|
32 | 32 | #include "iceberg/util/macros.h" |
33 | 33 |
|
34 | 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 | +/// \brief Converts a JSON type string to an Expression::Operation. |
| 59 | +/// |
| 60 | +/// \param type_str The JSON type string |
| 61 | +/// \return The corresponding Operation or an error if unknown |
| 62 | +Result<Expression::Operation> OperationTypeFromString(const std::string_view type_str) { |
| 63 | + if (type_str == kTypeTrue) return Expression::Operation::kTrue; |
| 64 | + if (type_str == kTypeFalse) return Expression::Operation::kFalse; |
| 65 | + if (type_str == kTypeAnd) return Expression::Operation::kAnd; |
| 66 | + if (type_str == kTypeOr) return Expression::Operation::kOr; |
| 67 | + if (type_str == kTypeNot) return Expression::Operation::kNot; |
| 68 | + if (type_str == kTypeEq) return Expression::Operation::kEq; |
| 69 | + if (type_str == kTypeNotEq) return Expression::Operation::kNotEq; |
| 70 | + if (type_str == kTypeLt) return Expression::Operation::kLt; |
| 71 | + if (type_str == kTypeLtEq) return Expression::Operation::kLtEq; |
| 72 | + if (type_str == kTypeGt) return Expression::Operation::kGt; |
| 73 | + if (type_str == kTypeGtEq) return Expression::Operation::kGtEq; |
| 74 | + if (type_str == kTypeIn) return Expression::Operation::kIn; |
| 75 | + if (type_str == kTypeNotIn) return Expression::Operation::kNotIn; |
| 76 | + if (type_str == kTypeIsNull) return Expression::Operation::kIsNull; |
| 77 | + if (type_str == kTypeNotNull) return Expression::Operation::kNotNull; |
| 78 | + if (type_str == kTypeIsNan) return Expression::Operation::kIsNan; |
| 79 | + if (type_str == kTypeNotNan) return Expression::Operation::kNotNan; |
| 80 | + if (type_str == kTypeStartsWith) return Expression::Operation::kStartsWith; |
| 81 | + if (type_str == kTypeNotStartsWith) return Expression::Operation::kNotStartsWith; |
| 82 | + |
| 83 | + return JsonParseError("Unknown expression type: {}", type_str); |
| 84 | +} |
| 85 | + |
| 86 | +/// \brief Converts an Expression::Operation to its JSON string representation. |
| 87 | +/// |
| 88 | +/// \param op The operation to convert |
| 89 | +/// \return The JSON type string (e.g., "eq", "lt-eq", "is-null") |
| 90 | +std::string_view ToStringOperationType(Expression::Operation op) { |
| 91 | + switch (op) { |
| 92 | + case Expression::Operation::kTrue: |
| 93 | + return kTypeTrue; |
| 94 | + case Expression::Operation::kFalse: |
| 95 | + return kTypeFalse; |
| 96 | + case Expression::Operation::kAnd: |
| 97 | + return kTypeAnd; |
| 98 | + case Expression::Operation::kOr: |
| 99 | + return kTypeOr; |
| 100 | + case Expression::Operation::kNot: |
| 101 | + return kTypeNot; |
| 102 | + case Expression::Operation::kEq: |
| 103 | + return kTypeEq; |
| 104 | + case Expression::Operation::kNotEq: |
| 105 | + return kTypeNotEq; |
| 106 | + case Expression::Operation::kLt: |
| 107 | + return kTypeLt; |
| 108 | + case Expression::Operation::kLtEq: |
| 109 | + return kTypeLtEq; |
| 110 | + case Expression::Operation::kGt: |
| 111 | + return kTypeGt; |
| 112 | + case Expression::Operation::kGtEq: |
| 113 | + return kTypeGtEq; |
| 114 | + case Expression::Operation::kIn: |
| 115 | + return kTypeIn; |
| 116 | + case Expression::Operation::kNotIn: |
| 117 | + return kTypeNotIn; |
| 118 | + case Expression::Operation::kIsNull: |
| 119 | + return kTypeIsNull; |
| 120 | + case Expression::Operation::kNotNull: |
| 121 | + return kTypeNotNull; |
| 122 | + case Expression::Operation::kIsNan: |
| 123 | + return kTypeIsNan; |
| 124 | + case Expression::Operation::kNotNan: |
| 125 | + return kTypeNotNan; |
| 126 | + case Expression::Operation::kStartsWith: |
| 127 | + return kTypeStartsWith; |
| 128 | + case Expression::Operation::kNotStartsWith: |
| 129 | + return kTypeNotStartsWith; |
| 130 | + default: |
| 131 | + return "unknown"; |
| 132 | + } |
| 133 | +} |
35 | 134 |
|
36 | 135 | Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) { |
37 | 136 | // Handle boolean |
38 | 137 | if (json.is_boolean()) { |
39 | 138 | return json.get<bool>() ? std::static_pointer_cast<Expression>(True::Instance()) |
40 | 139 | : std::static_pointer_cast<Expression>(False::Instance()); |
41 | 140 | } |
42 | | - return JsonParseError("Only boolean are currently supported"); |
| 141 | + return JsonParseError("Only booleans are currently supported"); |
43 | 142 | } |
44 | 143 |
|
45 | 144 | nlohmann::json ToJson(const Expression& expr) { |
|
0 commit comments