Skip to content

[C++] Add Compute Kernel for Casting between Different Timestamp Types #39050

@llama90

Description

@llama90

Describe the enhancement requested

This is a sub-issue of the issue mentioned below.

As we can see in the parent issue, we can find tables organizing the related functions.

file name code snippet
scalar_test.cc TimestampScalar(value, timestamp(in)).CastTo(timestamp(out)).ValueOrDie();
scalar_test.cc TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(utf8()));
scalar_test.cc TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(int64()));
scalar_test.cc .CastTo(date64()));

The functions require handling "ValueOrDie called on an error: Invalid: Casting from timestamp[ns] to timestamp[us] would lose data: 1234" error.

We need to implement casting between different Timestamp types.

scalar_test.cc

Snippet to reproduce

TEST(TestTimestampScalars, Cast) {
  auto convert = [](TimeUnit::type in, TimeUnit::type out, int64_t value) -> int64_t {
-   auto scalar =
-       TimestampScalar(value, timestamp(in)).CastTo(timestamp(out)).ValueOrDie();
-   return internal::checked_pointer_cast<TimestampScalar>(scalar)->value;
+   EXPECT_OK_AND_ASSIGN(auto casted, Cast(TimestampScalar(value, timestamp(in)), timestamp(out)));
+   return internal::checked_pointer_cast<TimestampScalar>(casted.scalar())->value;  
  };

  EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::MILLI, 1), 1000);
  EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::NANO, 1), 1000000000);

  EXPECT_EQ(convert(TimeUnit::NANO, TimeUnit::MICRO, 1234), 1);
  EXPECT_EQ(convert(TimeUnit::MICRO, TimeUnit::MILLI, 4567), 4);

  ASSERT_OK_AND_ASSIGN(auto str,
                       TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(utf8()));
  EXPECT_EQ(*str, StringScalar("1970-01-01 00:00:01.024"));
  ASSERT_OK_AND_ASSIGN(auto i64,
                       TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(int64()));
  EXPECT_EQ(*i64, Int64Scalar(1024));

  constexpr int64_t kMillisecondsInDay = 86400000;
  ASSERT_OK_AND_ASSIGN(
      auto d64, TimestampScalar(1024 * kMillisecondsInDay + 3, timestamp(TimeUnit::MILLI))
                    .CastTo(date64()));
  EXPECT_EQ(*d64, Date64Scalar(1024 * kMillisecondsInDay));
}

Component(s)

C++

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions