Skip to content

Commit

Permalink
Add testing and also fix the list type castTo
Browse files Browse the repository at this point in the history
  • Loading branch information
mapleFU committed Feb 5, 2025
1 parent cbe3627 commit 6495e3e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cpp/src/arrow/scalar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1290,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
40 changes: 40 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,45 @@ TEST(TestBooleanScalar, Cast) {
}
}

TEST(TestScalar, IndentityCast) {
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::map(arrow::binary(), arrow::int32()),
// struct_({field("float", arrow::float32())}),
}) {
test_identity_cast_for_type(type);
}
/*
for (auto& type: {
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

0 comments on commit 6495e3e

Please sign in to comment.