Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
llama90 opened this issue Dec 3, 2023 · 0 comments · Fixed by #39060
Closed

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

llama90 opened this issue Dec 3, 2023 · 0 comments · Fixed by #39060

Comments

@llama90
Copy link
Contributor

llama90 commented Dec 3, 2023

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++

@kou kou closed this as completed in #39060 Dec 5, 2023
kou pushed a commit that referenced this issue Dec 5, 2023
…n test (#39060)

### Rationale for this change

Remove legacy code

### What changes are included in this PR?

* Replace the legacy scalar CastTo implementation for Timestamp Scalar in test. The cast function already supports casting between different timestamp types. We just need to allow for the possibility of data loss by using `CastOptions::Unsafe()`. 

### Are these changes tested?

Yes. It is passed by existing test cases.

### Are there any user-facing changes?

No.

* Closes: #39050

Authored-by: Hyunseok Seo <hsseo0501@gmail.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
@kou kou added this to the 15.0.0 milestone Dec 5, 2023
@kou kou changed the title [C++] Add Compute Kernel for Casting between Different Timestamp Types [C++] Add Compute Kernel for Casting between Different Timestamp Types Dec 5, 2023
dgreiss pushed a commit to dgreiss/arrow that referenced this issue Feb 19, 2024
…alar in test (apache#39060)

### Rationale for this change

Remove legacy code

### What changes are included in this PR?

* Replace the legacy scalar CastTo implementation for Timestamp Scalar in test. The cast function already supports casting between different timestamp types. We just need to allow for the possibility of data loss by using `CastOptions::Unsafe()`. 

### Are these changes tested?

Yes. It is passed by existing test cases.

### Are there any user-facing changes?

No.

* Closes: apache#39050

Authored-by: Hyunseok Seo <hsseo0501@gmail.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants