Skip to content

Commit 549b9ae

Browse files
author
Innocent
committed
Added helper for operation enum conversion
1 parent 52e2501 commit 549b9ae

3 files changed

Lines changed: 118 additions & 1 deletion

File tree

src/iceberg/expression/json_internal.cc

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,113 @@
3232
#include "iceberg/util/macros.h"
3333

3434
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+
}
35134

36135
Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) {
37136
// Handle boolean
38137
if (json.is_boolean()) {
39138
return json.get<bool>() ? std::static_pointer_cast<Expression>(True::Instance())
40139
: std::static_pointer_cast<Expression>(False::Instance());
41140
}
42-
return JsonParseError("Only boolean are currently supported");
141+
return JsonParseError("Only booleans are currently supported");
43142
}
44143

45144
nlohmann::json ToJson(const Expression& expr) {

src/iceberg/expression/json_internal.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,17 @@ ICEBERG_DECLARE_JSON_SERDE(Expression)
4848

4949
#undef ICEBERG_DECLARE_JSON_SERDE
5050

51+
/// \brief Converts an operation type string to an Expression::Operation.
52+
///
53+
/// \param type_str The operation type string
54+
/// \return The corresponding Operation or an error if unknown
55+
ICEBERG_EXPORT Result<Expression::Operation> OperationTypeFromString(
56+
const std::string_view type_str);
57+
58+
/// \brief Converts an Expression::Operation to its string representation.
59+
///
60+
/// \param op The operation to convert
61+
/// \return The operation type string (e.g., "eq", "lt-eq", "is-null")
62+
ICEBERG_EXPORT std::string_view ToStringOperationType(Expression::Operation op);
63+
5164
} // namespace iceberg

src/iceberg/test/expression_json_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@ TEST_F(ExpressionJsonTest, FalseExpression) {
7777
EXPECT_EQ(result.value()->op(), Expression::Operation::kFalse);
7878
}
7979

80+
TEST_F(ExpressionJsonTest, OpToString) {
81+
EXPECT_EQ(OperationTypeFromString("true"), Expression::Operation::kTrue);
82+
EXPECT_EQ("true", ToStringOperationType(Expression::Operation::kTrue));
83+
}
84+
8085
} // namespace iceberg

0 commit comments

Comments
 (0)