Skip to content

Commit f79e8a6

Browse files
GH-55315: [C++][Python] Support integer casts to date32
1 parent d9a0ab4 commit f79e8a6

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎cpp/src/arrow/compute/kernels/scalar_cast_temporal.cc‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#include "arrow/compute/kernels/scalar_cast_internal.h"
2525
#include "arrow/compute/kernels/temporal_internal.h"
2626
#include "arrow/util/bitmap_reader.h"
27+
#include "arrow/util/int_util.h"
2728
#include "arrow/util/logging_internal.h"
2829
#include "arrow/util/time.h"
2930
#include "arrow/util/value_parsing.h"
3031

3132
namespace arrow {
3233

34+
using internal::IntegersCanFit;
3335
using internal::ParseTimestampISO8601;
3436
using internal::ParseYYYY_MM_DD;
3537

@@ -495,6 +497,22 @@ struct CastFunctor<O, I,
495497
}
496498
};
497499

500+
// Integer values represent days when cast to date32. Keep the same
501+
// overflow-checking semantics as integer-to-integer casts while writing the
502+
// result into the date32 array's int32 storage.
503+
template <typename I>
504+
struct CastFunctor<Date32Type, I, enable_if_t<is_integer_type<I>::value>> {
505+
static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
506+
const auto& options = checked_cast<const CastState&>(*ctx->state()).options;
507+
if (!options.allow_int_overflow) {
508+
RETURN_NOT_OK(IntegersCanFit(batch[0].array, *int32()));
509+
}
510+
CastNumberToNumberUnsafe(batch[0].type()->id(), Type::INT32, batch[0].array,
511+
out->array_span_mutable());
512+
return Status::OK();
513+
}
514+
};
515+
498516
namespace {
499517

500518
template <typename Type>
@@ -523,9 +541,18 @@ std::shared_ptr<CastFunction> GetDate32Cast() {
523541
// date32 -> date32
524542
AddZeroCopyCast(Type::DATE32, date32(), date32(), func.get());
525543

526-
// int32 -> date32
544+
// int32 -> date32 (zero copy)
527545
AddZeroCopyCast(Type::INT32, int32(), date32(), func.get());
528546

547+
// Other integer types -> date32
548+
for (const auto& in_ty : IntTypes()) {
549+
if (in_ty->id() == Type::INT32) {
550+
continue;
551+
}
552+
auto exec = GenerateInteger<CastFunctor, Date32Type>(in_ty->id());
553+
DCHECK_OK(func->AddKernel(in_ty->id(), {in_ty}, date32(), std::move(exec)));
554+
}
555+
529556
// date64 -> date32
530557
AddSimpleCast<Date64Type, Date32Type>(date64(), date32(), func.get());
531558

‎cpp/src/arrow/compute/kernels/scalar_cast_test.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ TEST(Cast, CanCast) {
219219
ExpectCanCast(null(), {smallint()});
220220
ExpectCanCast(tinyint(), {smallint()}); // cast between compatible storage types
221221

222+
for (const auto& integer_type : kIntegerTypes) {
223+
ExpectCanCast(integer_type, {date32()});
224+
}
222225
ExpectCanCast(date32(), {utf8(), large_utf8()});
223226
ExpectCanCast(date64(), {utf8(), large_utf8()});
224227
ExpectCanCast(timestamp(TimeUnit::NANO), {utf8(), large_utf8()});
@@ -2831,6 +2834,18 @@ TEST(Cast, DateToDate) {
28312834
CheckCast(day_64_will_be_truncated, day_32, options);
28322835
}
28332836

2837+
TEST(Cast, IntegerToDate32) {
2838+
auto expected = ArrayFromJSON(date32(), "[0, null, 1, 2]");
2839+
for (const auto& integer_type : kIntegerTypes) {
2840+
CheckCast(ArrayFromJSON(integer_type, "[0, null, 1, 2]"), expected);
2841+
}
2842+
2843+
CheckCastFails(ArrayFromJSON(int64(), "[0, 2147483648]"),
2844+
CastOptions::Safe(date32()));
2845+
CheckCastFails(ArrayFromJSON(uint64(), "[0, 4294967296]"),
2846+
CastOptions::Safe(date32()));
2847+
}
2848+
28342849
TEST(Cast, DateZeroCopy) {
28352850
for (auto zero_copy_to_type : {
28362851
date32(),

‎python/pyarrow/tests/test_array.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,17 @@ def test_cast_timestamp_to_int():
22922292
assert result.equals(expected)
22932293

22942294

2295+
@pytest.mark.numpy
2296+
def test_cast_date32_from_numpy_integers():
2297+
expected = pa.array([datetime.date(1970, 1, 1),
2298+
datetime.date(1970, 1, 2),
2299+
datetime.date(1970, 1, 3)], type=pa.date32())
2300+
for dtype in ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
2301+
'int64', 'uint64']:
2302+
result = pa.array(np.array([0, 1, 2], dtype=dtype), type=pa.date32())
2303+
assert result.equals(expected)
2304+
2305+
22952306
def test_cast_date32_to_int():
22962307
arr = pa.array([0, 1, 2], type='i4')
22972308

0 commit comments

Comments
 (0)