wgtmac commented on code in PR #117:
URL: https://github.com/apache/iceberg-cpp/pull/117#discussion_r2131227449


##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is owned literal of a primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// dest type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// dest type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string

Review Comment:
   Do we need a `string_view` variant?



##########
src/iceberg/CMakeLists.txt:
##########
@@ -24,6 +24,7 @@ set(ICEBERG_SOURCES
     expression/expression.cc
     file_reader.cc
     json_internal.cc
+    literal.cc

Review Comment:
   Should it be put into the `expression` subdir?



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {

Review Comment:
   Should we simply use `Literal` instead of `PrimitiveLiteral` which looks 
lengthy?



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);
+
+  /// Create iceberg literal from bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);
+
+  /// Serialize iceberg literal to bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  Result<std::vector<uint8_t>> Serialize() const;
+
+  /// Get the Iceberg Type of the literal.
+  const std::shared_ptr<PrimitiveType>& type() const;
+
+  /// Converts this literal to a literal of the given type.
+  ///
+  /// When a predicate is bound to a concrete data column, literals are 
converted to match
+  /// the bound column's type. This conversion process is more narrow than a 
cast and is
+  /// only intended for cases where substituting one type is a common mistake 
(e.g. 34
+  /// instead of 34L) or where this API avoids requiring a concrete class 
(e.g., dates).
+  ///
+  /// If conversion to a target type is not supported, this method returns an 
error.
+  ///
+  /// This method may return BelowMin or AboveMax when the target type is not 
as wide as
+  /// the original type. These values indicate that the containing predicate 
can be
+  /// simplified. For example, std::numeric_limits<int>::max()+1 converted to 
an int will
+  /// result in AboveMax and can simplify a < 
std::numeric_limits<int>::max()+1 to always
+  /// true.
+  ///
+  /// \param target_type A primitive PrimitiveType
+  /// \return A Result containing a literal of the given type or an error if 
conversion
+  /// was not valid
+  Result<PrimitiveLiteral> CastTo(
+      const std::shared_ptr<PrimitiveType>& target_type) const;

Review Comment:
   ```suggestion
     Result<PrimitiveLiteral> CastTo(
         const PrimitiveType& target_type) const;
   ```
   
   std::shared_ptr has a narrower use case



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);
+
+  /// Create iceberg literal from bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);
+
+  /// Serialize iceberg literal to bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  Result<std::vector<uint8_t>> Serialize() const;
+
+  /// Get the Iceberg Type of the literal.
+  const std::shared_ptr<PrimitiveType>& type() const;
+
+  /// Converts this literal to a literal of the given type.
+  ///
+  /// When a predicate is bound to a concrete data column, literals are 
converted to match
+  /// the bound column's type. This conversion process is more narrow than a 
cast and is
+  /// only intended for cases where substituting one type is a common mistake 
(e.g. 34
+  /// instead of 34L) or where this API avoids requiring a concrete class 
(e.g., dates).
+  ///
+  /// If conversion to a target type is not supported, this method returns an 
error.
+  ///
+  /// This method may return BelowMin or AboveMax when the target type is not 
as wide as
+  /// the original type. These values indicate that the containing predicate 
can be
+  /// simplified. For example, std::numeric_limits<int>::max()+1 converted to 
an int will
+  /// result in AboveMax and can simplify a < 
std::numeric_limits<int>::max()+1 to always
+  /// true.
+  ///
+  /// \param target_type A primitive PrimitiveType
+  /// \return A Result containing a literal of the given type or an error if 
conversion
+  /// was not valid
+  Result<PrimitiveLiteral> CastTo(
+      const std::shared_ptr<PrimitiveType>& target_type) const;
+
+  /// Compare two PrimitiveLiterals. Both literals must have the same type
+  /// and should not be AboveMax or BelowMin.
+  std::partial_ordering operator<=>(const PrimitiveLiteral& other) const;
+
+  bool isAboveMax() const;
+  bool isBelowMin() const;
+
+  std::string ToString() const;
+
+ private:
+  PrimitiveLiteral(PrimitiveLiteralValue value, std::shared_ptr<PrimitiveType> 
type);
+
+  static PrimitiveLiteral BelowMinLiteral(std::shared_ptr<PrimitiveType> type);

Review Comment:
   I don't think we need a type here, right? If your intention is to make them 
comparable, I think we can add `ToAboveMax()` and `ToBelowMin()` to replace 
these two static functions to keep the original type.



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);
+
+  /// Create iceberg literal from bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);

Review Comment:
   Don't we need the type as an input argument?



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);

Review Comment:
   I think we need. Otherwise we don't know the exact type of the literal.



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);
+
+  /// Create iceberg literal from bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);
+
+  /// Serialize iceberg literal to bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  Result<std::vector<uint8_t>> Serialize() const;
+
+  /// Get the Iceberg Type of the literal.
+  const std::shared_ptr<PrimitiveType>& type() const;
+
+  /// Converts this literal to a literal of the given type.
+  ///
+  /// When a predicate is bound to a concrete data column, literals are 
converted to match
+  /// the bound column's type. This conversion process is more narrow than a 
cast and is
+  /// only intended for cases where substituting one type is a common mistake 
(e.g. 34
+  /// instead of 34L) or where this API avoids requiring a concrete class 
(e.g., dates).
+  ///
+  /// If conversion to a target type is not supported, this method returns an 
error.
+  ///
+  /// This method may return BelowMin or AboveMax when the target type is not 
as wide as
+  /// the original type. These values indicate that the containing predicate 
can be
+  /// simplified. For example, std::numeric_limits<int>::max()+1 converted to 
an int will
+  /// result in AboveMax and can simplify a < 
std::numeric_limits<int>::max()+1 to always
+  /// true.
+  ///
+  /// \param target_type A primitive PrimitiveType
+  /// \return A Result containing a literal of the given type or an error if 
conversion
+  /// was not valid
+  Result<PrimitiveLiteral> CastTo(
+      const std::shared_ptr<PrimitiveType>& target_type) const;
+
+  /// Compare two PrimitiveLiterals. Both literals must have the same type
+  /// and should not be AboveMax or BelowMin.
+  std::partial_ordering operator<=>(const PrimitiveLiteral& other) const;
+
+  bool isAboveMax() const;
+  bool isBelowMin() const;
+
+  std::string ToString() const;
+
+ private:
+  PrimitiveLiteral(PrimitiveLiteralValue value, std::shared_ptr<PrimitiveType> 
type);
+
+  static PrimitiveLiteral BelowMinLiteral(std::shared_ptr<PrimitiveType> type);
+  static PrimitiveLiteral AboveMaxLiteral(std::shared_ptr<PrimitiveType> type);
+
+  // Helper methods for type casting
+  Result<PrimitiveLiteral> CastFromInt(TypeId target_type_id) const;

Review Comment:
   What about moving these functions to anonymous namespace in the source file? 
Or make them as public non-member functions if you want to add test cases for 
them. Besides, why choosing `TypeId` instead of `Type`? Current functions look 
weird to me.



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed
+                   std::array<uint8_t, 16>,  // for uuid and decimal
+                   BelowMin, AboveMax>;
+
+ public:
+  /// Factory methods for primitive types
+  static PrimitiveLiteral Boolean(bool value);
+  static PrimitiveLiteral Int(int32_t value);
+  static PrimitiveLiteral Long(int64_t value);
+  static PrimitiveLiteral Float(float value);
+  static PrimitiveLiteral Double(double value);
+  static PrimitiveLiteral String(std::string value);
+  static PrimitiveLiteral Binary(std::vector<uint8_t> value);
+
+  /// Create iceberg literal from bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);
+
+  /// Serialize iceberg literal to bytes.
+  ///
+  /// See [this 
spec](https://iceberg.apache.org/spec/#binary-single-value-serialization)
+  /// for reference.
+  Result<std::vector<uint8_t>> Serialize() const;
+
+  /// Get the Iceberg Type of the literal.
+  const std::shared_ptr<PrimitiveType>& type() const;
+
+  /// Converts this literal to a literal of the given type.
+  ///
+  /// When a predicate is bound to a concrete data column, literals are 
converted to match
+  /// the bound column's type. This conversion process is more narrow than a 
cast and is
+  /// only intended for cases where substituting one type is a common mistake 
(e.g. 34
+  /// instead of 34L) or where this API avoids requiring a concrete class 
(e.g., dates).
+  ///
+  /// If conversion to a target type is not supported, this method returns an 
error.
+  ///
+  /// This method may return BelowMin or AboveMax when the target type is not 
as wide as
+  /// the original type. These values indicate that the containing predicate 
can be
+  /// simplified. For example, std::numeric_limits<int>::max()+1 converted to 
an int will
+  /// result in AboveMax and can simplify a < 
std::numeric_limits<int>::max()+1 to always
+  /// true.
+  ///
+  /// \param target_type A primitive PrimitiveType
+  /// \return A Result containing a literal of the given type or an error if 
conversion
+  /// was not valid
+  Result<PrimitiveLiteral> CastTo(
+      const std::shared_ptr<PrimitiveType>& target_type) const;
+
+  /// Compare two PrimitiveLiterals. Both literals must have the same type
+  /// and should not be AboveMax or BelowMin.
+  std::partial_ordering operator<=>(const PrimitiveLiteral& other) const;
+
+  bool isAboveMax() const;
+  bool isBelowMin() const;

Review Comment:
   ```suggestion
     bool IsAboveMax() const;
     bool IsBelowMin() const;
   ```



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =

Review Comment:
   ```suggestion
     using Value =
   ```
   
   This is equivalent to `Literal::Value` so we don't need to duplicate them.



##########
src/iceberg/literal.h:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <compare>
+#include <memory>
+#include <string>
+#include <variant>
+#include <vector>
+
+#include "iceberg/result.h"
+#include "iceberg/type.h"
+
+namespace iceberg {
+
+/// \brief PrimitiveLiteral is a literal value that is associated with a 
primitive type.
+class ICEBERG_EXPORT PrimitiveLiteral {
+ private:
+  /// \brief Exception type for values that are below the minimum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct BelowMin {
+    bool operator==(const BelowMin&) const = default;
+    std::strong_ordering operator<=>(const BelowMin&) const = default;
+  };
+
+  /// \brief Exception type for values that are above the maximum allowed 
value for a
+  /// primitive type.
+  ///
+  /// When casting a value to a narrow primitive type, if the value exceeds 
the maximum of
+  /// target type, it might be above the maximum allowed value for that type.
+  struct AboveMax {
+    bool operator==(const AboveMax&) const = default;
+    std::strong_ordering operator<=>(const AboveMax&) const = default;
+  };
+
+  using PrimitiveLiteralValue =
+      std::variant<bool,                     // for boolean
+                   int32_t,                  // for int, date
+                   int64_t,                  // for long, timestamp, 
timestamp_tz, time
+                   float,                    // for float
+                   double,                   // for double
+                   std::string,              // for string
+                   std::vector<uint8_t>,     // for binary, fixed

Review Comment:
   Why not reusing `std::string`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org


Reply via email to