Skip to content

Commit be2d7e6

Browse files
committed
fix: timestamps should be in microseconds
also fix some lint warnings
1 parent 3c06e2a commit be2d7e6

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

src/iceberg/transform_function.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Result<std::optional<Literal>> YearTransform::Transform(const Literal& literal)
232232
literal.ToString(), source_type()->ToString());
233233
}
234234

235-
using namespace std::chrono;
235+
using namespace std::chrono; // NOLINT
236236
switch (source_type()->type_id()) {
237237
case TypeId::kDate: {
238238
auto value = std::get<int32_t>(literal.value());
@@ -243,8 +243,8 @@ Result<std::optional<Literal>> YearTransform::Transform(const Literal& literal)
243243
case TypeId::kTimestamp:
244244
case TypeId::kTimestampTz: {
245245
auto value = std::get<int64_t>(literal.value());
246-
// Convert milliseconds-since-epoch into a `year_month_day` object
247-
auto ymd = year_month_day(floor<days>(sys_time<milliseconds>(milliseconds{value})));
246+
// Convert microseconds-since-epoch into a `year_month_day` object
247+
auto ymd = year_month_day(floor<days>(sys_time<microseconds>(microseconds{value})));
248248
return Literal::Int(static_cast<int32_t>(ymd.year()));
249249
}
250250
default:
@@ -288,7 +288,7 @@ Result<std::optional<Literal>> MonthTransform::Transform(const Literal& literal)
288288
literal.ToString(), source_type()->ToString());
289289
}
290290

291-
using namespace std::chrono;
291+
using namespace std::chrono; // NOLINT
292292
switch (source_type()->type_id()) {
293293
case TypeId::kDate: {
294294
auto value = std::get<int32_t>(literal.value());
@@ -305,8 +305,8 @@ Result<std::optional<Literal>> MonthTransform::Transform(const Literal& literal)
305305
case TypeId::kTimestamp:
306306
case TypeId::kTimestampTz: {
307307
auto value = std::get<int64_t>(literal.value());
308-
// Convert milliseconds-since-epoch into a `year_month_day` object
309-
auto ymd = year_month_day(floor<days>(sys_time<milliseconds>(milliseconds{value})));
308+
// Convert microseconds-since-epoch into a `year_month_day` object
309+
auto ymd = year_month_day(floor<days>(sys_time<microseconds>(microseconds{value})));
310310
auto epoch_ymd = year_month_day(year{1970} / January / 1);
311311
auto delta = ymd.year() - epoch_ymd.year();
312312
// Calculate the month as months from 1970-01
@@ -356,7 +356,7 @@ Result<std::optional<Literal>> DayTransform::Transform(const Literal& literal) {
356356
literal.ToString(), source_type()->ToString());
357357
}
358358

359-
using namespace std::chrono;
359+
using namespace std::chrono; // NOLINT
360360
switch (source_type()->type_id()) {
361361
case TypeId::kDate: {
362362
// Day is the same as the date value
@@ -365,8 +365,8 @@ Result<std::optional<Literal>> DayTransform::Transform(const Literal& literal) {
365365
case TypeId::kTimestamp:
366366
case TypeId::kTimestampTz: {
367367
auto value = std::get<int64_t>(literal.value());
368-
// Convert milliseconds to `sys_days` (chronological days since epoch)
369-
auto timestamp = sys_time<milliseconds>(milliseconds{value});
368+
// Convert microseconds to `sys_days` (chronological days since epoch)
369+
auto timestamp = sys_time<microseconds>(microseconds{value});
370370
auto days_since_epoch = floor<days>(timestamp);
371371

372372
return Literal::Date(
@@ -411,13 +411,13 @@ Result<std::optional<Literal>> HourTransform::Transform(const Literal& literal)
411411
literal.ToString(), source_type()->ToString());
412412
}
413413

414-
using namespace std::chrono;
414+
using namespace std::chrono; // NOLINT
415415
switch (source_type()->type_id()) {
416416
case TypeId::kTimestamp:
417417
case TypeId::kTimestampTz: {
418418
auto value = std::get<int64_t>(literal.value());
419-
// Create a `sys_time` object from the milliseconds value
420-
auto timestamp = sys_time<milliseconds>(milliseconds{value});
419+
// Create a `sys_time` object from the microseconds value
420+
auto timestamp = sys_time<microseconds>(microseconds{value});
421421

422422
// Convert the time since epoch directly into hours
423423
auto hours_since_epoch = duration_cast<hours>(timestamp.time_since_epoch()).count();

test/transform_test.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,17 @@ TEST(TransformFunctionTransformTest, IdentityTransform) {
214214
.source = Literal::Long(1234567890),
215215
.expected = Literal::Long(1234567890)},
216216
{.source_type = iceberg::timestamp(),
217-
.source = Literal::Timestamp(1622547800000),
218-
.expected = Literal::Timestamp(1622547800000)},
217+
.source = Literal::Timestamp(1622547800000000),
218+
.expected = Literal::Timestamp(1622547800000000)},
219219
{.source_type = iceberg::timestamp_tz(),
220-
.source = Literal::TimestampTz(1622547800000),
221-
.expected = Literal::TimestampTz(1622547800000)},
220+
.source = Literal::TimestampTz(1622547800000000),
221+
.expected = Literal::TimestampTz(1622547800000000)},
222222
{.source_type = iceberg::float32(),
223223
.source = Literal::Float(3.14),
224224
.expected = Literal::Float(3.14)},
225225
{.source_type = iceberg::float64(),
226-
.source = Literal::Double(2.71828),
227-
.expected = Literal::Double(2.71828)},
226+
.source = Literal::Double(1.23e-5),
227+
.expected = Literal::Double(1.23e-5)},
228228
{.source_type = iceberg::string(),
229229
.source = Literal::String("Hello, World!"),
230230
.expected = Literal::String("Hello, World!")},
@@ -268,10 +268,10 @@ TEST(TransformFunctionTransformTest, BucketTransform) {
268268
.source = Literal::Long(1234567890),
269269
.expected = Literal::Int(3)},
270270
{.source_type = iceberg::timestamp(),
271-
.source = Literal::Timestamp(1622547800000),
271+
.source = Literal::Timestamp(1622547800000000),
272272
.expected = Literal::Int(1)},
273273
{.source_type = iceberg::timestamp_tz(),
274-
.source = Literal::TimestampTz(1622547800000),
274+
.source = Literal::TimestampTz(1622547800000000),
275275
.expected = Literal::Int(1)},
276276
{.source_type = iceberg::string(),
277277
.source = Literal::String("test"),
@@ -335,10 +335,10 @@ TEST(TransformFunctionTransformTest, YearTransform) {
335335

336336
const std::vector<Case> cases = {
337337
{.source_type = iceberg::timestamp(),
338-
.source = Literal::Timestamp(1622547800000),
338+
.source = Literal::Timestamp(1622547800000000),
339339
.expected = Literal::Int(2021)},
340340
{.source_type = iceberg::timestamp_tz(),
341-
.source = Literal::TimestampTz(1622547800000),
341+
.source = Literal::TimestampTz(1622547800000000),
342342
.expected = Literal::Int(2021)},
343343
{.source_type = iceberg::date(),
344344
.source = Literal::Date(30000),
@@ -368,10 +368,10 @@ TEST(TransformFunctionTransformTest, MonthTransform) {
368368

369369
const std::vector<Case> cases = {
370370
{.source_type = iceberg::timestamp(),
371-
.source = Literal::Timestamp(1622547800000),
371+
.source = Literal::Timestamp(1622547800000000),
372372
.expected = Literal::Int(617)},
373373
{.source_type = iceberg::timestamp_tz(),
374-
.source = Literal::TimestampTz(1622547800000),
374+
.source = Literal::TimestampTz(1622547800000000),
375375
.expected = Literal::Int(617)},
376376
{.source_type = iceberg::date(),
377377
.source = Literal::Date(30000),
@@ -401,10 +401,10 @@ TEST(TransformFunctionTransformTest, DayTransform) {
401401

402402
const std::vector<Case> cases = {
403403
{.source_type = iceberg::timestamp(),
404-
.source = Literal::Timestamp(1622547800000),
404+
.source = Literal::Timestamp(1622547800000000),
405405
.expected = Literal::Date(18779)},
406406
{.source_type = iceberg::timestamp_tz(),
407-
.source = Literal::TimestampTz(1622547800000),
407+
.source = Literal::TimestampTz(1622547800000000),
408408
.expected = Literal::Date(18779)},
409409
{.source_type = iceberg::date(),
410410
.source = Literal::Date(30000),
@@ -434,10 +434,10 @@ TEST(TransformFunctionTransformTest, HourTransform) {
434434

435435
const std::vector<Case> cases = {
436436
{.source_type = iceberg::timestamp(),
437-
.source = Literal::Timestamp(1622547800000),
437+
.source = Literal::Timestamp(1622547800000000),
438438
.expected = Literal::Int(450707)},
439439
{.source_type = iceberg::timestamp_tz(),
440-
.source = Literal::TimestampTz(1622547800000),
440+
.source = Literal::TimestampTz(1622547800000000),
441441
.expected = Literal::Int(450707)},
442442
};
443443

@@ -466,11 +466,12 @@ TEST(TransformFunctionTransformTest, VoidTransform) {
466466
{.source_type = iceberg::int32(), .source = Literal::Int(42)},
467467
{.source_type = iceberg::int32(), .source = Literal::Date(30000)},
468468
{.source_type = iceberg::int64(), .source = Literal::Long(1234567890)},
469-
{.source_type = iceberg::timestamp(), .source = Literal::Timestamp(1622547800000)},
469+
{.source_type = iceberg::timestamp(),
470+
.source = Literal::Timestamp(1622547800000000)},
470471
{.source_type = iceberg::timestamp_tz(),
471-
.source = Literal::TimestampTz(1622547800000)},
472+
.source = Literal::TimestampTz(1622547800000000)},
472473
{.source_type = iceberg::float32(), .source = Literal::Float(3.14)},
473-
{.source_type = iceberg::float64(), .source = Literal::Double(2.71828)},
474+
{.source_type = iceberg::float64(), .source = Literal::Double(1.23e-5)},
474475
{.source_type = iceberg::string(), .source = Literal::String("Hello, World!")},
475476
{.source_type = iceberg::binary(), .source = Literal::Binary({0x01, 0x02, 0x03})},
476477
};
@@ -479,7 +480,7 @@ TEST(TransformFunctionTransformTest, VoidTransform) {
479480
auto transformPtr = transform->Bind(c.source_type);
480481
ASSERT_TRUE(transformPtr.has_value()) << "Failed to bind void transform";
481482
auto result = transformPtr.value()->Transform(c.source);
482-
ASSERT_TRUE(result == std::nullopt)
483+
ASSERT_EQ(std::nullopt, result)
483484
<< "Expected void transform to return no result for source: "
484485
<< c.source.ToString();
485486
}

0 commit comments

Comments
 (0)