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

GH-45362: [C++] Fix identity cast for time and list scalar #45370

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cpp/src/arrow/scalar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1177,14 +1177,16 @@ enable_if_duration<To, Result<std::shared_ptr<Scalar>>> CastImpl(
}

// time to time
template <typename To, typename From, typename T = typename To::TypeClass>
template <typename To, typename From,
typename T = typename TypeTraits<To>::ScalarType::TypeClass>
enable_if_time<To, Result<std::shared_ptr<Scalar>>> CastImpl(
const TimeScalar<From>& from, std::shared_ptr<DataType> to_type) {
using ToScalar = typename TypeTraits<To>::ScalarType;
ARROW_ASSIGN_OR_RAISE(
auto value, util::ConvertTimestampValue(AsTimestampType<From>(from.type),
AsTimestampType<To>(to_type), from.value));
return std::make_shared<ToScalar>(value, std::move(to_type));
return std::make_shared<ToScalar>(static_cast<typename To::c_type>(value),
std::move(to_type));
}

constexpr int64_t kMillisecondsInDay = 86400000;
Expand Down Expand Up @@ -1288,10 +1290,11 @@ CastImpl(const StructScalar& from, std::shared_ptr<DataType> to_type) {
}

// casts between variable-length and fixed-length list types
template <typename To, typename From>
template <typename To, typename FromScalar,
typename From = typename FromScalar::TypeClass>
std::enable_if_t<is_list_type<To>::value && is_list_type<From>::value,
Result<std::shared_ptr<Scalar>>>
CastImpl(const From& from, std::shared_ptr<DataType> to_type) {
CastImpl(const FromScalar& from, std::shared_ptr<DataType> to_type) {
if constexpr (sizeof(typename To::offset_type) < sizeof(int64_t)) {
if (from.value->length() > std::numeric_limits<typename To::offset_type>::max()) {
return Status::Invalid(from.type->ToString(), " too large to cast to ",
Expand Down
48 changes: 48 additions & 0 deletions cpp/src/arrow/scalar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "arrow/status.h"
#include "arrow/testing/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/random.h"
#include "arrow/testing/util.h"
#include "arrow/type_traits.h"

Expand Down Expand Up @@ -156,6 +157,50 @@ TEST(TestBooleanScalar, Cast) {
}
}

TEST(TestScalar, IdentityCast) {
random::RandomArrayGenerator gen(/*seed=*/42);
auto test_identity_cast_for_type =
[&gen](const std::shared_ptr<arrow::DataType>& data_type) {
auto tmp_array = gen.ArrayOf(data_type, /*size=*/1, /*null_probability=*/0.0);
ARROW_SCOPED_TRACE("data type = ", data_type->ToString());
ASSERT_OK_AND_ASSIGN(auto scalar, tmp_array->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto casted_scalar, scalar->CastTo(data_type));
ASSERT_TRUE(casted_scalar->Equals(*scalar));
ASSERT_TRUE(scalar->Equals(*casted_scalar));
};
for (auto& type : PrimitiveTypes()) {
test_identity_cast_for_type(type);
}
for (auto& type : DurationTypes()) {
test_identity_cast_for_type(type);
}
for (auto& type : IntervalTypes()) {
test_identity_cast_for_type(type);
}
for (auto& type : {
arrow::fixed_size_list(arrow::int32(), 20), arrow::list(arrow::int32()),
arrow::large_list(arrow::int32()),
// TODO(GH-45430): CastTo for ListView is not implemented yet.
// arrow::list_view(arrow::int32()), arrow::large_list_view(arrow::int32())
// TODO(GH-45431): CastTo for ComplexType is not implemented yet.
// arrow::map(arrow::binary(), arrow::int32()),
// struct_({field("float", arrow::float32())}),
}) {
test_identity_cast_for_type(type);
}
// TODO(GH-45429): CastTo for Decimal is not implemented yet.
/*
for (auto& type: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decimal seems not have castTo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we open issues for the missing casts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added, but seems previous issue wants to deprecate them, I think these issues may also be mark as not planned, lol

arrow::decimal32(2, 2),
arrow::decimal64(4, 4),
arrow::decimal128(10, 10),
arrow::decimal128(20, 20),
}) {
test_identity_cast_for_type(type);
}
*/
}

template <typename T>
class TestNumericScalar : public ::testing::Test {
public:
Expand Down Expand Up @@ -866,6 +911,9 @@ TEST(TestTimeScalars, Basics) {
ASSERT_TRUE(first->Equals(*MakeScalar(ty, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(*MakeScalar(ty, 42).ValueOrDie()));
ASSERT_FALSE(last->Equals(*MakeScalar("string")));

ASSERT_OK_AND_ASSIGN(auto casted, first->CastTo(ty));
ASSERT_TRUE(casted->Equals(*first));
pitrou marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading