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-39051: [C++] Use Cast() instead of CastTo() for List Scalar in test #39353

Merged
merged 16 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
10 changes: 9 additions & 1 deletion cpp/src/arrow/compute/kernels/scalar_cast_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ void AddTypeToTypeCast(CastFunction* func) {
kernel.exec = CastFunctor::Exec;
kernel.signature = KernelSignature::Make({InputType(SrcT::type_id)}, kOutputTargetType);
kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
DCHECK_OK(func->AddKernel(StructType::type_id, std::move(kernel)));
DCHECK_OK(func->AddKernel(SrcT::type_id, std::move(kernel)));
}

template <typename DestType>
Expand Down Expand Up @@ -480,14 +480,18 @@ std::vector<std::shared_ptr<CastFunction>> GetNestedCasts() {
auto cast_list = std::make_shared<CastFunction>("cast_list", Type::LIST);
AddCommonCasts(Type::LIST, kOutputTargetType, cast_list.get());
AddListCast<ListType, ListType>(cast_list.get());
AddListCast<ListViewType, ListType>(cast_list.get());
AddListCast<LargeListType, ListType>(cast_list.get());
AddListCast<LargeListViewType, ListType>(cast_list.get());
AddTypeToTypeCast<CastFixedToVarList<ListType>, FixedSizeListType>(cast_list.get());

auto cast_large_list =
std::make_shared<CastFunction>("cast_large_list", Type::LARGE_LIST);
AddCommonCasts(Type::LARGE_LIST, kOutputTargetType, cast_large_list.get());
AddListCast<ListType, LargeListType>(cast_large_list.get());
AddListCast<ListViewType, LargeListType>(cast_large_list.get());
AddListCast<LargeListType, LargeListType>(cast_large_list.get());
AddListCast<LargeListViewType, LargeListType>(cast_large_list.get());
AddTypeToTypeCast<CastFixedToVarList<LargeListType>, FixedSizeListType>(
cast_large_list.get());

Expand All @@ -503,7 +507,11 @@ std::vector<std::shared_ptr<CastFunction>> GetNestedCasts() {
AddCommonCasts(Type::FIXED_SIZE_LIST, kOutputTargetType, cast_fsl.get());
AddTypeToTypeCast<CastFixedList, FixedSizeListType>(cast_fsl.get());
AddTypeToTypeCast<CastVarToFixedList<ListType>, ListType>(cast_fsl.get());
AddTypeToTypeCast<CastVarToFixedList<ListViewType>, ListViewType>(cast_fsl.get());
AddTypeToTypeCast<CastVarToFixedList<LargeListType>, LargeListType>(cast_fsl.get());
AddTypeToTypeCast<CastVarToFixedList<LargeListViewType>, LargeListViewType>(
cast_fsl.get());
AddTypeToTypeCast<CastVarToFixedList<ListType>, MapType>(cast_fsl.get());

// So is struct
auto cast_struct = std::make_shared<CastFunction>("cast_struct", Type::STRUCT);
Expand Down
51 changes: 34 additions & 17 deletions cpp/src/arrow/scalar_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1077,21 +1077,38 @@ std::shared_ptr<DataType> MakeListType<FixedSizeListType>(

template <typename ScalarType>
void CheckListCast(const ScalarType& scalar, const std::shared_ptr<DataType>& to_type) {
EXPECT_OK_AND_ASSIGN(auto cast_scalar, scalar.CastTo(to_type));
ASSERT_OK(cast_scalar->ValidateFull());
ASSERT_EQ(*cast_scalar->type, *to_type);
EXPECT_OK_AND_ASSIGN(auto cast_scalar, Cast(scalar, to_type));
ASSERT_OK(cast_scalar.scalar()->ValidateFull());
ASSERT_EQ(*cast_scalar.scalar()->type, *to_type);

ASSERT_EQ(scalar.is_valid, cast_scalar->is_valid);
ASSERT_EQ(scalar.is_valid, cast_scalar.scalar()->is_valid);
ASSERT_TRUE(scalar.is_valid);
ASSERT_ARRAYS_EQUAL(*scalar.value,
*checked_cast<const BaseListScalar&>(*cast_scalar).value);
*checked_cast<const BaseListScalar&>(*cast_scalar.scalar()).value);
}

void CheckInvalidListCast(const Scalar& scalar, const std::shared_ptr<DataType>& to_type,
std::tuple<StatusCode, std::string> GetExpectedError(
const std::shared_ptr<DataType>& type,
const std::shared_ptr<DataType>& invalidCastType) {
if (type->id() == Type::FIXED_SIZE_LIST) {
return std::make_tuple(
StatusCode::TypeError,
"Size of FixedSizeList is not the same. input list: " + type->ToString() +
" output list: " + invalidCastType->ToString());
} else {
return std::make_tuple(
StatusCode::Invalid,
"ListType can only be casted to FixedSizeListType if the lists are all the "
"expected size.");
}
}

template <typename ScalarType>
void CheckInvalidListCast(const ScalarType& scalar,
const std::shared_ptr<DataType>& to_type, const StatusCode code,
const std::string& expected_message) {
EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(StatusCode::Invalid,
::testing::HasSubstr(expected_message),
scalar.CastTo(to_type));
EXPECT_RAISES_WITH_CODE_AND_MESSAGE_THAT(code, ::testing::HasSubstr(expected_message),
Copy link
Member

Choose a reason for hiding this comment

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

It's strange that CheckInvalidListCast() may not expect StatusCode::Invalid.
Can we use better function name?

Copy link
Contributor Author

@llama90 llama90 Dec 26, 2023

Choose a reason for hiding this comment

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

Yes. You are correct. In the current situation, as you mentioned, both Invalid and TypeError can occur. I think CheckListCastError or VerifyListCastFailure would be more appropriate. What do you think? (First, I chose CheckListCastError.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kou Hello. When you have free time, could you please review it? Thank you :)

Copy link
Member

Choose a reason for hiding this comment

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

Sorry! I reviewed this!

Cast(scalar, to_type));
}

template <typename T>
Expand Down Expand Up @@ -1178,10 +1195,10 @@ class TestListLikeScalar : public ::testing::Test {
CheckListCast(
scalar, fixed_size_list(value_->type(), static_cast<int32_t>(value_->length())));

CheckInvalidListCast(scalar, fixed_size_list(value_->type(), 5),
"Cannot cast " + scalar.type->ToString() + " of length " +
std::to_string(value_->length()) +
" to fixed size list of length 5");
auto invalidCastType = fixed_size_list(value_->type(), 5);
auto [expectedCode, expectedMessage] = GetExpectedError(type_, invalidCastType);

CheckInvalidListCast(scalar, invalidCastType, expectedCode, expectedMessage);
}

protected:
Expand Down Expand Up @@ -1238,10 +1255,10 @@ TEST(TestMapScalar, Cast) {
CheckListCast(scalar, large_list(key_value_type));
CheckListCast(scalar, fixed_size_list(key_value_type, 2));

CheckInvalidListCast(scalar, fixed_size_list(key_value_type, 5),
"Cannot cast " + scalar.type->ToString() + " of length " +
std::to_string(value->length()) +
" to fixed size list of length 5");
auto invalidCastType = fixed_size_list(key_value_type, 5);
auto [expectedCode, expectedMessage] = GetExpectedError(scalar.type, invalidCastType);

CheckInvalidListCast(scalar, invalidCastType, expectedCode, expectedMessage);
}

TEST(TestStructScalar, FieldAccess) {
Expand Down
Loading