|
24 | 24 | #include "arrow/compute/kernels/scalar_cast_internal.h" |
25 | 25 | #include "arrow/compute/kernels/temporal_internal.h" |
26 | 26 | #include "arrow/util/bitmap_reader.h" |
| 27 | +#include "arrow/util/int_util.h" |
27 | 28 | #include "arrow/util/logging_internal.h" |
28 | 29 | #include "arrow/util/time.h" |
29 | 30 | #include "arrow/util/value_parsing.h" |
30 | 31 |
|
31 | 32 | namespace arrow { |
32 | 33 |
|
| 34 | +using internal::IntegersCanFit; |
33 | 35 | using internal::ParseTimestampISO8601; |
34 | 36 | using internal::ParseYYYY_MM_DD; |
35 | 37 |
|
@@ -495,6 +497,22 @@ struct CastFunctor<O, I, |
495 | 497 | } |
496 | 498 | }; |
497 | 499 |
|
| 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 | + |
498 | 516 | namespace { |
499 | 517 |
|
500 | 518 | template <typename Type> |
@@ -523,9 +541,18 @@ std::shared_ptr<CastFunction> GetDate32Cast() { |
523 | 541 | // date32 -> date32 |
524 | 542 | AddZeroCopyCast(Type::DATE32, date32(), date32(), func.get()); |
525 | 543 |
|
526 | | - // int32 -> date32 |
| 544 | + // int32 -> date32 (zero copy) |
527 | 545 | AddZeroCopyCast(Type::INT32, int32(), date32(), func.get()); |
528 | 546 |
|
| 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 | + |
529 | 556 | // date64 -> date32 |
530 | 557 | AddSimpleCast<Date64Type, Date32Type>(date64(), date32(), func.get()); |
531 | 558 |
|
|
0 commit comments