You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "WZhuo (via GitHub)" <gi...@apache.org> on 2026/01/08 09:30:05 UTC
[PR] feat: utilities for transfrom partiton values to human strings [iceberg-cpp]
WZhuo opened a new pull request, #501:
URL: https://github.com/apache/iceberg-cpp/pull/501
(no comment)
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add utility to print transformed value as human string [iceberg-cpp]
Posted by "zhjwpku (via GitHub)" <gi...@apache.org>.
zhjwpku commented on code in PR #501:
URL: https://github.com/apache/iceberg-cpp/pull/501#discussion_r2680847302
##########
src/iceberg/util/transform_util.cc:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/util/transform_util.h"
+
+#include <array>
+#include <chrono>
+
+namespace iceberg {
+
+namespace {
+constexpr auto kEpochDate = std::chrono::year{1970} / std::chrono::January / 1;
+constexpr int64_t kMicrosPerMillis = 1'000;
+constexpr int64_t kMicrosPerSecond = 1'000'000;
+} // namespace
+
+std::string TransformUtil::HumanYear(int32_t year_ordinal) {
+ auto y = kEpochDate + std::chrono::years{year_ordinal};
+ return std::format("{:%Y}", y);
+}
+
+std::string TransformUtil::HumanMonth(int32_t month_ordinal) {
+ auto ym = kEpochDate + std::chrono::months(month_ordinal);
+ return std::format("{:%Y-%m}", ym);
+}
+
+std::string TransformUtil::HumanDay(int32_t day_ordinal) {
+ auto ymd = std::chrono::sys_days(kEpochDate) + std::chrono::days{day_ordinal};
+ return std::format("{:%F}", ymd);
+}
+
+std::string TransformUtil::HumanHour(int32_t hour_ordinal) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>{
+ std::chrono::hours{hour_ordinal}};
+ return std::format("{:%F-%H}", tp);
+}
+
+std::string TransformUtil::HumanTime(int64_t micros_from_midnight) {
+ std::chrono::hh_mm_ss<std::chrono::seconds> hms{
+ std::chrono::seconds{micros_from_midnight / kMicrosPerSecond}};
+ auto micros = micros_from_midnight % kMicrosPerSecond;
+ if (micros == 0 && hms.seconds().count() == 0) {
+ return std::format("{:%R}", hms);
+ } else if (micros == 0) {
+ return std::format("{:%T}", hms);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%T}.{:03d}", hms, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%T}.{:06d}", hms, micros);
+ }
+}
+
+std::string TransformUtil::HumanTimestamp(int64_t timestamp_micros) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{
+ std::chrono::seconds(timestamp_micros / kMicrosPerSecond)};
+ auto micros = timestamp_micros % kMicrosPerSecond;
+ if (micros == 0) {
+ return std::format("{:%FT%T}", tp);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%FT%T}.{:03d}", tp, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%FT%T}.{:06d}", tp, micros);
+ }
+}
+
+std::string TransformUtil::HumanTimestampWithZone(int64_t timestamp_micros) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{
+ std::chrono::seconds(timestamp_micros / kMicrosPerSecond)};
+ auto micros = timestamp_micros % kMicrosPerSecond;
+ if (micros == 0) {
+ return std::format("{:%FT%T}+00:00", tp);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%FT%T}.{:03d}+00:00", tp, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%FT%T}.{:06d}+00:00", tp, micros);
+ }
+}
+
+std::string TransformUtil::Base64Encode(std::string_view str_to_encode) {
+ static constexpr std::string_view kBase64Chars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ int32_t i = 0;
+ int32_t j = 0;
+ std::array<unsigned char, 3> char_array_3;
Review Comment:
nit: should we use uint8_t as elsewhere? Not a strong opinion.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add utility to print transformed value as human string [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #501:
URL: https://github.com/apache/iceberg-cpp/pull/501#discussion_r2680887641
##########
src/iceberg/util/transform_util.cc:
##########
@@ -0,0 +1,144 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/util/transform_util.h"
+
+#include <array>
+#include <chrono>
+
+namespace iceberg {
+
+namespace {
+constexpr auto kEpochDate = std::chrono::year{1970} / std::chrono::January / 1;
+constexpr int64_t kMicrosPerMillis = 1'000;
+constexpr int64_t kMicrosPerSecond = 1'000'000;
+} // namespace
+
+std::string TransformUtil::HumanYear(int32_t year_ordinal) {
+ auto y = kEpochDate + std::chrono::years{year_ordinal};
+ return std::format("{:%Y}", y);
+}
+
+std::string TransformUtil::HumanMonth(int32_t month_ordinal) {
+ auto ym = kEpochDate + std::chrono::months(month_ordinal);
+ return std::format("{:%Y-%m}", ym);
+}
+
+std::string TransformUtil::HumanDay(int32_t day_ordinal) {
+ auto ymd = std::chrono::sys_days(kEpochDate) + std::chrono::days{day_ordinal};
+ return std::format("{:%F}", ymd);
+}
+
+std::string TransformUtil::HumanHour(int32_t hour_ordinal) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::hours>{
+ std::chrono::hours{hour_ordinal}};
+ return std::format("{:%F-%H}", tp);
+}
+
+std::string TransformUtil::HumanTime(int64_t micros_from_midnight) {
+ std::chrono::hh_mm_ss<std::chrono::seconds> hms{
+ std::chrono::seconds{micros_from_midnight / kMicrosPerSecond}};
+ auto micros = micros_from_midnight % kMicrosPerSecond;
+ if (micros == 0 && hms.seconds().count() == 0) {
+ return std::format("{:%R}", hms);
+ } else if (micros == 0) {
+ return std::format("{:%T}", hms);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%T}.{:03d}", hms, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%T}.{:06d}", hms, micros);
+ }
+}
+
+std::string TransformUtil::HumanTimestamp(int64_t timestamp_micros) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{
+ std::chrono::seconds(timestamp_micros / kMicrosPerSecond)};
+ auto micros = timestamp_micros % kMicrosPerSecond;
+ if (micros == 0) {
+ return std::format("{:%FT%T}", tp);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%FT%T}.{:03d}", tp, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%FT%T}.{:06d}", tp, micros);
+ }
+}
+
+std::string TransformUtil::HumanTimestampWithZone(int64_t timestamp_micros) {
+ auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>{
+ std::chrono::seconds(timestamp_micros / kMicrosPerSecond)};
+ auto micros = timestamp_micros % kMicrosPerSecond;
+ if (micros == 0) {
+ return std::format("{:%FT%T}+00:00", tp);
+ } else if (micros % kMicrosPerMillis == 0) {
+ return std::format("{:%FT%T}.{:03d}+00:00", tp, micros / kMicrosPerMillis);
+ } else {
+ return std::format("{:%FT%T}.{:06d}+00:00", tp, micros);
+ }
+}
+
+std::string TransformUtil::Base64Encode(std::string_view str_to_encode) {
+ static constexpr std::string_view kBase64Chars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ int32_t i = 0;
+ int32_t j = 0;
+ std::array<unsigned char, 3> char_array_3;
Review Comment:
That may require extra cast when appending to the 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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: utilities for transfrom partiton values to human strings [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #501:
URL: https://github.com/apache/iceberg-cpp/pull/501#discussion_r2672806420
##########
src/iceberg/util/transform_util.h:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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 <string>
+
+#include "iceberg/iceberg_export.h"
+
+namespace iceberg {
+
+class ICEBERG_EXPORT TransformUtil {
+ public:
+ /// \brief Returns a human-readable string for a year.
+ ///
+ /// The string is formatted as "yyyy".
+ ///
+ /// \param year The year to format.
+ /// \return A human-readable string for the year.
+ static std::string HumanYear(int32_t year);
+
+ /// \brief Returns a human-readable string for a month.
+ ///
+ /// The string is formatted as "yyyy-MM".
+ ///
+ /// \param month The month to format.
+ /// \return A human-readable string for the month.
+ static std::string HumanMonth(int32_t month);
+
+ /// \brief Returns a human-readable string for the given day ordinal.
+ ///
+ /// The string is formatted as: `yyyy-MM-dd`.
+ ///
+ /// \param day_ordinal The day ordinal.
+ /// \return A human-readable string for the given day ordinal.
+ static std::string HumanDay(int32_t day_ordinal);
+
+ /// \brief Returns a human-readable string for the given hour ordinal.
+ ///
+ /// The string is formatted as: `yyyy-MM-dd-HH`.
+ ///
+ /// \param hour_ordinal The hour ordinal.
+ /// \return A human-readable string for the given hour ordinal.
+ static std::string HumanHour(int32_t hour_ordinal);
+
+ /// \brief Outputs this time as a String, such as 10:15.
+ ///
+ /// The output will be one of the following ISO-8601 formats:
+ /// HH:mm
+ /// HH:mm:ss
+ /// HH:mm:ss.SSS
+ /// HH:mm:ss.SSSSSS
+ /// The format used will be the shortest that outputs the full value of the time where
+ /// the omitted parts are implied to be zero.
+ ///
+ /// \param microseconds_from_midnight the time in microseconds from midnight
+ /// \return a string representation of this time
+ static std::string HumanTime(int64_t micros_from_midnight);
+
+ /// \brief Returns a string representation of a timestamp in microseconds.
+ ///
+ /// The output will be one of the following forms, according to the precision of the
+ /// timestamp:
+ /// - yyyy-MM-dd HH:mm:ss
+ /// - yyyy-MM-dd HH:mm:ss.SSS
+ /// - yyyy-MM-dd HH:mm:ss.SSSSSS
Review Comment:
```suggestion
/// - yyyy-MM-ddTHH:mm:ss
/// - yyyy-MM-ddTHH:mm:ss.SSS
/// - yyyy-MM-ddTHH:mm:ss.SSSSSS
```
The java formatter is as below:
```java
ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.append(ISO_LOCAL_TIME)
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
```
##########
src/iceberg/util/transform_util.h:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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 <string>
+
+#include "iceberg/iceberg_export.h"
+
+namespace iceberg {
+
+class ICEBERG_EXPORT TransformUtil {
+ public:
+ /// \brief Returns a human-readable string for a year.
+ ///
+ /// The string is formatted as "yyyy".
+ ///
+ /// \param year The year to format.
+ /// \return A human-readable string for the year.
+ static std::string HumanYear(int32_t year);
+
+ /// \brief Returns a human-readable string for a month.
+ ///
+ /// The string is formatted as "yyyy-MM".
+ ///
+ /// \param month The month to format.
+ /// \return A human-readable string for the month.
+ static std::string HumanMonth(int32_t month);
+
+ /// \brief Returns a human-readable string for the given day ordinal.
+ ///
+ /// The string is formatted as: `yyyy-MM-dd`.
+ ///
+ /// \param day_ordinal The day ordinal.
+ /// \return A human-readable string for the given day ordinal.
+ static std::string HumanDay(int32_t day_ordinal);
+
+ /// \brief Returns a human-readable string for the given hour ordinal.
+ ///
+ /// The string is formatted as: `yyyy-MM-dd-HH`.
+ ///
+ /// \param hour_ordinal The hour ordinal.
+ /// \return A human-readable string for the given hour ordinal.
+ static std::string HumanHour(int32_t hour_ordinal);
+
+ /// \brief Outputs this time as a String, such as 10:15.
+ ///
+ /// The output will be one of the following ISO-8601 formats:
+ /// HH:mm
+ /// HH:mm:ss
+ /// HH:mm:ss.SSS
+ /// HH:mm:ss.SSSSSS
+ /// The format used will be the shortest that outputs the full value of the time where
+ /// the omitted parts are implied to be zero.
+ ///
+ /// \param microseconds_from_midnight the time in microseconds from midnight
+ /// \return a string representation of this time
+ static std::string HumanTime(int64_t micros_from_midnight);
+
+ /// \brief Returns a string representation of a timestamp in microseconds.
+ ///
+ /// The output will be one of the following forms, according to the precision of the
+ /// timestamp:
+ /// - yyyy-MM-dd HH:mm:ss
+ /// - yyyy-MM-dd HH:mm:ss.SSS
+ /// - yyyy-MM-dd HH:mm:ss.SSSSSS
+ ///
+ /// \param timestamp_micros the timestamp in microseconds.
+ /// \return a string representation of this timestamp.
+ static std::string HumanTimestamp(int64_t timestamp_micros);
+
+ /// \brief Returns a human-readable string representation of a timestamp with a time
+ /// zone.
+ ///
+ /// The output will be one of the following forms, according to the precision of the
+ /// timestamp:
+ /// - yyyy-MM-dd HH:mm:ss+00:00
Review Comment:
ditto
##########
src/iceberg/test/transform_util_test.cc:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/util/transform_util.h"
+
+#include <gtest/gtest.h>
+
+namespace iceberg {
+
+TEST(TransformUtilTest, HumanYear) {
+ EXPECT_EQ("1970", TransformUtil::HumanYear(0));
Review Comment:
Let's add large negative number to test year smaller than 1000. Same for other cases.
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add utility to print transformed value as human string [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac merged PR #501:
URL: https://github.com/apache/iceberg-cpp/pull/501
--
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-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org