leaves12138 commented on code in PR #38:
URL: https://github.com/apache/paimon-cpp/pull/38#discussion_r3339168140


##########
src/paimon/common/utils/data_converter_utils.h:
##########
@@ -0,0 +1,280 @@
+/*
+ * 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 <charconv>
+#include <cstdint>
+#include <functional>
+#include <iomanip>
+#include <memory>
+#include <optional>
+#include <sstream>
+#include <string>
+#include <utility>
+
+#include "arrow/type.h"
+#include "fmt/core.h"
+#include "fmt/format.h"
+#include "fmt/ranges.h"
+#include "paimon/common/data/binary_row.h"
+#include "paimon/common/data/binary_row_writer.h"
+#include "paimon/common/data/binary_string.h"
+#include "paimon/common/utils/string_utils.h"
+#include "paimon/core/casting/date_to_string_cast_executor.h"
+#include "paimon/defs.h"
+#include "paimon/predicate/literal.h"
+#include "paimon/result.h"
+#include "paimon/status.h"
+
+namespace paimon {
+class MemoryPool;
+
+#define RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str, type)      
              \
+    if ((value) == std::nullopt) {                                             
              \
+        return Status::Invalid(                                                
              \
+            fmt::format("cannot convert field idx {}, field value {} to type 
{}", field_idx, \
+                        value_str, type));                                     
              \
+    }
+
+class DataConverterUtils {
+ public:
+    DataConverterUtils() = delete;
+    ~DataConverterUtils() = delete;
+
+    using StrToBinaryRowConverter =
+        std::function<Status(const std::string&, int32_t, BinaryRowWriter*)>;
+    using BinaryRowFieldToStrConverter =
+        std::function<Result<std::string>(const BinaryRow&, int32_t)>;
+
+    static Result<StrToBinaryRowConverter> 
CreateDataToBinaryRowConverter(arrow::Type::type type,
+                                                                          
MemoryPool* pool) {
+        StrToBinaryRowConverter converter;
+        switch (type) {
+            case arrow::Type::BOOL:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = StringUtils::StringToValue<bool>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::BOOL));
+                    writer->WriteBoolean(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::INT8:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = StringUtils::StringToValue<int8_t>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::INT8));
+                    writer->WriteByte(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::INT16:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = 
StringUtils::StringToValue<int16_t>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::INT16));
+                    writer->WriteShort(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::INT32:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = 
StringUtils::StringToValue<int32_t>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::INT32));
+                    writer->WriteInt(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::INT64:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = 
StringUtils::StringToValue<int64_t>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::INT64));
+                    writer->WriteLong(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::FLOAT:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = StringUtils::StringToValue<float>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::FLOAT));
+                    writer->WriteFloat(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::DOUBLE:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    auto value = StringUtils::StringToValue<double>(value_str);
+                    RETURN_INVALID_WITH_FIELD_INFO(value, field_idx, value_str,
+                                                   
arrow::internal::ToString(arrow::Type::DOUBLE));
+                    writer->WriteDouble(field_idx, value.value());
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::STRING:
+                converter = [pool](const std::string& value_str, int32_t 
field_idx,
+                                   BinaryRowWriter* writer) {
+                    BinaryString value = BinaryString::FromString(value_str, 
pool);
+                    writer->WriteString(field_idx, value);
+                    return Status::OK();
+                };
+                break;
+            case arrow::Type::DATE32:
+                converter = [](const std::string& value_str, int32_t field_idx,
+                               BinaryRowWriter* writer) {
+                    PAIMON_ASSIGN_OR_RAISE(int32_t date_value,
+                                           
StringUtils::StringToDate(value_str));
+                    writer->WriteInt(field_idx, date_value);
+                    return Status::OK();
+                };
+                break;
+            default:
+                return Status::NotImplemented(
+                    fmt::format("Do not support type {} in partition binary 
row",
+                                arrow::internal::ToString(type)));
+        }
+        return converter;
+    }
+
+    // support float and double
+    template <typename T>
+    static std::string FloatValueToString(const T& value, int32_t precision) {
+        std::stringstream oss;
+        if (value >= 1e-3 && value <= 1e7) {

Review Comment:
   This condition should use the magnitude rather than the signed value. As 
written, negative values such as `-233.0` or `-467.66472` take the scientific 
branch and are emitted as `-2.33E2` / `-4.6766472E2`, while positive values in 
the same range are emitted in fixed decimal form. That makes 
`CreateBinaryRowFieldToStringConverter` fail to round-trip negative 
FLOAT/DOUBLE partition values and can produce Java-incompatible partition path 
names. Please use `std::abs(value)` for the range check and add negative 
float/double coverage.



-- 
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]

Reply via email to