Skip to content

Commit e3f194b

Browse files
mbasmanovameta-codesync[bot]
authored andcommitted
feat: Add valueToString to TimestampWithTimeZoneType (facebookincubator#16840)
Summary: Pull Request resolved: facebookincubator#16840 Add valueToString(int64_t) to TimestampWithTimeZoneType that formats a packed timestamp-with-timezone value as "YYYY-MM-DD HH:MM:SS.mmm <timezone>" to match Presto output. Uses the same Joda formatter ("yyyy-MM-dd HH:mm:ss.SSS ZZZ") as CAST(timestamp_with_timezone AS VARCHAR). Previously, there was no way to get a human-readable string from a packed TimestampWithTimezone value without going through the full CAST execution machinery. Reviewed By: srsuryadev Differential Revision: D97293095 fbshipit-source-id: 126f61eabd43ca11cbac2c4773308e61b48a8d92
1 parent 59585ff commit e3f194b

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

velox/functions/prestosql/types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ velox_add_library(
3131
SfmSketchRegistration.cpp
3232
TDigestRegistration.cpp
3333
TimestampWithTimeZoneRegistration.cpp
34+
TimestampWithTimeZoneType.cpp
3435
TimeWithTimezoneRegistration.cpp
3536
TimeWithTimezoneType.cpp
3637
SetDigestRegistration.cpp
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
18+
#include "velox/functions/lib/DateTimeFormatter.h"
19+
#include "velox/type/tz/TimeZoneMap.h"
20+
21+
namespace facebook::velox {
22+
23+
std::string TimestampWithTimeZoneType::valueToString(int64_t value) const {
24+
static auto kFormatter =
25+
functions::buildJodaDateTimeFormatter("yyyy-MM-dd HH:mm:ss.SSS ZZZ")
26+
.value();
27+
28+
auto timestamp = unpackTimestampUtc(value);
29+
auto* timeZone = tz::locateZone(tz::getTimeZoneName(unpackZoneKeyId(value)));
30+
31+
auto maxSize = kFormatter->maxResultSize(timeZone);
32+
std::string result(maxSize, '\0');
33+
auto size = kFormatter->format(timestamp, timeZone, maxSize, result.data());
34+
result.resize(size);
35+
return result;
36+
}
37+
38+
} // namespace facebook::velox

velox/functions/prestosql/types/TimestampWithTimeZoneType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class TimestampWithTimeZoneType final : public BigintType {
9292
return name();
9393
}
9494

95+
/// Formats a packed value as "YYYY-MM-DD HH:MM:SS.mmm <timezone>".
96+
std::string valueToString(int64_t value) const;
97+
9598
folly::dynamic serialize() const override {
9699
folly::dynamic obj = folly::dynamic::object;
97100
obj["name"] = "Type";

velox/functions/prestosql/types/tests/TimestampWithTimeZoneTypeTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,22 @@ TEST_F(TimestampWithTimeZoneTypeTest, hash) {
133133
expectHashesNeq(-1639426440000, "+03:00", 1639426440000, "-14:00");
134134
}
135135

136+
TEST_F(TimestampWithTimeZoneTypeTest, valueToString) {
137+
// 1639426440000 ms = 2021-12-13 20:14:00.000 UTC.
138+
auto packed = pack(1639426440000, tz::getTimeZoneID("America/Los_Angeles"));
139+
ASSERT_EQ(
140+
TIMESTAMP_WITH_TIME_ZONE()->valueToString(packed),
141+
"2021-12-13 12:14:00.000 America/Los_Angeles");
142+
143+
packed = pack(1639426440000, tz::getTimeZoneID("+03:00"));
144+
ASSERT_EQ(
145+
TIMESTAMP_WITH_TIME_ZONE()->valueToString(packed),
146+
"2021-12-13 23:14:00.000 +03:00");
147+
148+
packed = pack(0, tz::getTimeZoneID("UTC"));
149+
ASSERT_EQ(
150+
TIMESTAMP_WITH_TIME_ZONE()->valueToString(packed),
151+
"1970-01-01 00:00:00.000 UTC");
152+
}
153+
136154
} // namespace facebook::velox::test

0 commit comments

Comments
 (0)