From 5570a5a4f07df370ff3292aff1652b19b6387e32 Mon Sep 17 00:00:00 2001 From: Junwang Zhao Date: Wed, 20 Aug 2025 19:43:15 +0800 Subject: [PATCH] chore: use more from_chars --- src/iceberg/avro/avro_schema_util.cc | 11 +++++++---- src/iceberg/schema_internal.cc | 7 ++++++- test/config_test.cc | 3 ++- test/partition_field_test.cc | 2 +- test/partition_spec_test.cc | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/iceberg/avro/avro_schema_util.cc b/src/iceberg/avro/avro_schema_util.cc index 28e9e7bfc..1e278b21f 100644 --- a/src/iceberg/avro/avro_schema_util.cc +++ b/src/iceberg/avro/avro_schema_util.cc @@ -17,6 +17,7 @@ * under the License. */ +#include #include #include #include @@ -413,11 +414,13 @@ Result GetId(const ::avro::NodePtr& node, const std::string& attr_name, return InvalidSchema("Missing avro attribute: {}", attr_name); } - try { - return std::stoi(id_str.value()); - } catch (const std::exception& e) { - return InvalidSchema("Invalid {}: {}", attr_name, id_str.value()); + int32_t id; + const auto& id_value = id_str.value(); + auto [_, ec] = std::from_chars(id_value.data(), id_value.data() + id_value.size(), id); + if (ec != std::errc()) { + return InvalidSchema("Invalid {}: {}", attr_name, id_value); } + return id; } Result GetElementId(const ::avro::NodePtr& node) { diff --git a/src/iceberg/schema_internal.cc b/src/iceberg/schema_internal.cc index fca6f01e3..124c595d5 100644 --- a/src/iceberg/schema_internal.cc +++ b/src/iceberg/schema_internal.cc @@ -19,6 +19,7 @@ #include "iceberg/schema_internal.h" +#include #include #include #include @@ -192,7 +193,11 @@ int32_t GetFieldId(const ArrowSchema& schema) { return kUnknownFieldId; } - return std::stoi(std::string(field_id_value.data, field_id_value.size_bytes)); + int32_t field_id = kUnknownFieldId; + std::from_chars(field_id_value.data, field_id_value.data + field_id_value.size_bytes, + field_id); + + return field_id; } Result> FromArrowSchema(const ArrowSchema& schema) { diff --git a/test/config_test.cc b/test/config_test.cc index 1ee04224e..712a0890d 100644 --- a/test/config_test.cc +++ b/test/config_test.cc @@ -17,10 +17,11 @@ * under the License. */ +#include "iceberg/util/config.h" + #include #include -#include namespace iceberg { diff --git a/test/partition_field_test.cc b/test/partition_field_test.cc index 31400164b..04ee9b26d 100644 --- a/test/partition_field_test.cc +++ b/test/partition_field_test.cc @@ -22,9 +22,9 @@ #include #include -#include #include "iceberg/transform.h" +#include "iceberg/type.h" #include "iceberg/util/formatter.h" // IWYU pragma: keep namespace iceberg { diff --git a/test/partition_spec_test.cc b/test/partition_spec_test.cc index d4a3f46d5..871bb0813 100644 --- a/test/partition_spec_test.cc +++ b/test/partition_spec_test.cc @@ -24,10 +24,10 @@ #include #include -#include #include "iceberg/partition_field.h" #include "iceberg/schema.h" +#include "iceberg/schema_field.h" #include "iceberg/transform.h" #include "iceberg/util/formatter.h" // IWYU pragma: keep