Skip to content

Commit

Permalink
GH-43183: [C++] Add date{32,64} to date{32,64} cast (#43192)
Browse files Browse the repository at this point in the history
### Rationale for this change

This one seems to be missing, see #43183

### What changes are included in this PR?

### Are these changes tested?

I'm not sure what the best place is to test this, please advise

### Are there any user-facing changes?

* GitHub Issue: #43183

Lead-authored-by: Fokko <[email protected]>
Co-authored-by: Fokko Driesprong <[email protected]>
Co-authored-by: Hyunseok Seo <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
Fokko and llama90 authored Jul 10, 2024
1 parent dffff1c commit 5e451d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cpp/src/arrow/compute/kernels/scalar_cast_temporal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,12 @@ void AddCrossUnitCastNoPreallocate(CastFunction* func) {

std::shared_ptr<CastFunction> GetDate32Cast() {
auto func = std::make_shared<CastFunction>("cast_date32", Type::DATE32);
auto out_ty = date32();
const auto& out_ty = date32();
AddCommonCasts(Type::DATE32, out_ty, func.get());

// date32 -> date32
AddZeroCopyCast(Type::DATE32, date32(), date32(), func.get());

// int32 -> date32
AddZeroCopyCast(Type::INT32, int32(), date32(), func.get());

Expand All @@ -532,9 +535,12 @@ std::shared_ptr<CastFunction> GetDate32Cast() {

std::shared_ptr<CastFunction> GetDate64Cast() {
auto func = std::make_shared<CastFunction>("cast_date64", Type::DATE64);
auto out_ty = date64();
const auto& out_ty = date64();
AddCommonCasts(Type::DATE64, out_ty, func.get());

// date64 -> date64
AddZeroCopyCast(Type::DATE64, date64(), date64(), func.get());

// int64 -> date64
AddZeroCopyCast(Type::INT64, int64(), date64(), func.get());

Expand Down
4 changes: 4 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_cast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,10 @@ TEST(Cast, DateToDate) {
86400000,
864000000])");

// Zero copy
CheckCast(day_32, day_32);
CheckCast(day_64, day_64);

// Multiply promotion
CheckCast(day_32, day_64);

Expand Down
8 changes: 8 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,14 @@ def test_cast():
assert pc.cast(arr, expected.type) == expected


@pytest.mark.parametrize('value_type', [pa.date32(), pa.date64()])
def test_identity_cast_dates(value_type):
dt = datetime.date(1990, 3, 1)

arr = pa.array([dt], type=value_type)
assert pc.cast(arr, value_type) == arr


@pytest.mark.parametrize('value_type', numerical_arrow_types)
def test_fsl_to_fsl_cast(value_type):
# Different field name and different type.
Expand Down

0 comments on commit 5e451d8

Please sign in to comment.