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


##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 <algorithm>
+#include <bit>
+#include <cstring>
+#include <span>
+#include <vector>
+
+#include "iceberg/result.h"
+
+/// \file iceberg/util/endian.h
+/// \brief Endianness conversion utilities
+
+namespace iceberg::util {
+
+template <typename T>
+concept LittleEndianWritable =
+    std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
+    std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, 
uint8_t>;

Review Comment:
   ```suggestion
   concept LittleEndianWritable = std::is_arithmetic_v<T>;
   ```
   
   Is this enough?



##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 <algorithm>
+#include <bit>
+#include <cstring>
+#include <span>
+#include <vector>
+
+#include "iceberg/result.h"
+
+/// \file iceberg/util/endian.h
+/// \brief Endianness conversion utilities
+
+namespace iceberg::util {
+
+template <typename T>
+concept LittleEndianWritable =
+    std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
+    std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, 
uint8_t>;
+
+/// \brief Convert a value to little-endian format.
+template <LittleEndianWritable T>
+T ToLittleEndian(T value) {
+  if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
+    return std::byteswap(value);
+  }
+  return value;
+}
+
+/// \brief Convert a value from little-endian format.
+template <LittleEndianWritable T>
+T FromLittleEndian(T value) {
+  return ToLittleEndian(value);

Review Comment:
   This looks weird. I'd rather copy the above function.



##########
src/iceberg/util/conversions.h:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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 <vector>
+
+#include "iceberg/expression/literal.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type.h"

Review Comment:
   ```suggestion
   #include "iceberg/type_fwd.h"
   ```



##########
src/iceberg/expression/literal.h:
##########
@@ -144,8 +144,9 @@ class ICEBERG_EXPORT Literal {
   Literal(Value value, std::shared_ptr<PrimitiveType> type);
 
   friend class LiteralCaster;
+  friend class LiteralSerializer;

Review Comment:
   ```suggestion
   ```



##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 <algorithm>
+#include <bit>
+#include <cstring>
+#include <span>
+#include <vector>
+
+#include "iceberg/result.h"
+
+/// \file iceberg/util/endian.h
+/// \brief Endianness conversion utilities
+
+namespace iceberg::util {

Review Comment:
   ```suggestion
   namespace iceberg {
   ```



##########
src/iceberg/expression/literal.cc:
##########
@@ -151,11 +155,11 @@ Literal Literal::Binary(std::vector<uint8_t> value) {
 
 Result<Literal> Literal::Deserialize(std::span<const uint8_t> data,
                                      std::shared_ptr<PrimitiveType> type) {
-  return NotImplemented("Deserialization of Literal is not implemented yet");
+  return Conversions::FromBytes(type, data);

Review Comment:
   ```suggestion
     return Conversions::FromBytes(std::move(type), data);
   ```



##########
src/iceberg/expression/literal.cc:
##########
@@ -21,8 +21,12 @@
 
 #include <cmath>
 #include <concepts>
+#include <cstring>
+#include <utility>

Review Comment:
   ```suggestion
   ```



##########
src/iceberg/util/endian.h:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 <algorithm>
+#include <bit>
+#include <cstring>
+#include <span>
+#include <vector>
+
+#include "iceberg/result.h"
+
+/// \file iceberg/util/endian.h
+/// \brief Endianness conversion utilities
+
+namespace iceberg::util {
+
+template <typename T>
+concept LittleEndianWritable =
+    std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
+    std::is_same_v<T, float> || std::is_same_v<T, double> || std::is_same_v<T, 
uint8_t>;
+
+/// \brief Convert a value to little-endian format.
+template <LittleEndianWritable T>
+T ToLittleEndian(T value) {
+  if constexpr (std::endian::native != std::endian::little && sizeof(T) > 1) {
+    return std::byteswap(value);
+  }
+  return value;
+}
+
+/// \brief Convert a value from little-endian format.
+template <LittleEndianWritable T>
+T FromLittleEndian(T value) {
+  return ToLittleEndian(value);
+}
+
+/// \brief Write a value in little-endian format to the buffer.
+template <LittleEndianWritable T>
+void WriteLittleEndian(std::vector<uint8_t>& buffer, T value) {
+  T le_value = ToLittleEndian(value);
+  const auto* bytes = reinterpret_cast<const uint8_t*>(&le_value);

Review Comment:
   ```suggestion
     value = ToLittleEndian(value);
     const auto* bytes = reinterpret_cast<const uint8_t*>(&value);
   ```



##########
src/iceberg/util/conversions.h:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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 <vector>
+
+#include "iceberg/expression/literal.h"
+#include "iceberg/iceberg_export.h"

Review Comment:
   ```suggestion
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to