Skip to content

Commit

Permalink
Better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Newton committed Jan 14, 2025
1 parent c9a10fd commit 31a3e21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
6 changes: 2 additions & 4 deletions cpp/src/arrow/compute/kernels/scalar_cast_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,8 @@ struct CastStruct {
} else if (out_field->nullable()) {
fields_to_select[out_field_index] = kFillNullSentinel;
} else {
return Status::TypeError("struct fields don't match or are in the wrong order");
// return Status::TypeError("cannot cast non-nullable field to nullable field:
// ",
// in_type.ToString(), " ", out_type.ToString());
return Status::TypeError("struct fields don't match: non-nullable out field ",
out_field->name(), " not found in ", in_type.ToString());
}
}

Expand Down
47 changes: 18 additions & 29 deletions cpp/src/arrow/compute/kernels/scalar_cast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3832,10 +3832,10 @@ static void CheckStructToStructSubset(
{std::make_shared<Field>("a", int8()), std::make_shared<Field>("d", int16()),
std::make_shared<Field>("f", int64(), /*nullable=*/false)});
const auto options7 = CastOptions::Safe(dest7);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src, options7));
ASSERT_OK(Cast(src, options7));
// EXPECT_RAISES_WITH_MESSAGE_THAT(TypeError,
// ::testing::HasSubstr("struct fields don't
// match"), Cast(src, options7));

// fields in wrong order
ASSERT_OK_AND_ASSIGN(auto dest8, StructArray::Make({a2, c2, b2}, {"a", "c", "b"}));
Expand All @@ -3851,10 +3851,9 @@ static void CheckStructToStructSubset(
std::make_shared<Field>("d", int64()),
std::make_shared<Field>("a", int8(), /*nullable=*/false)});
const auto options10 = CastOptions::Safe(dest10);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src, options10));
EXPECT_RAISES_WITH_MESSAGE_THAT(TypeError,
::testing::HasSubstr("struct fields don't match"),
Cast(src, options10));

// duplicate present field names
ASSERT_OK_AND_ASSIGN(
Expand Down Expand Up @@ -3887,8 +3886,7 @@ static void CheckStructToStructSubset(
const auto options5_duplicate_field_names =
CastOptions::Safe(dest5_duplicate_field_names);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
TypeError, ::testing::HasSubstr("struct fields don't match"),
Cast(src_duplicate_field_names, options5_duplicate_field_names));
}
}
Expand Down Expand Up @@ -3963,10 +3961,9 @@ static void CheckStructToStructSubsetWithNulls(
{std::make_shared<Field>("a", int8()), std::make_shared<Field>("d", int16()),
std::make_shared<Field>("f", int64(), /*nullable=*/false)});
const auto options7_null = CastOptions::Safe(dest7_null);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src_null, options7_null));
EXPECT_RAISES_WITH_MESSAGE_THAT(TypeError,
::testing::HasSubstr("struct fields don't match"),
Cast(src_null, options7_null));

// fields in wrong order
ASSERT_OK_AND_ASSIGN(auto dest8_null,
Expand All @@ -3984,10 +3981,9 @@ static void CheckStructToStructSubsetWithNulls(
std::make_shared<Field>("d", int64()),
std::make_shared<Field>("a", int8(), /*nullable=*/false)});
const auto options10_null = CastOptions::Safe(dest10_null);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src_null, options10_null));
EXPECT_RAISES_WITH_MESSAGE_THAT(TypeError,
::testing::HasSubstr("struct fields don't match"),
Cast(src_null, options10_null));

// duplicate present field values
ASSERT_OK_AND_ASSIGN(
Expand Down Expand Up @@ -4024,8 +4020,7 @@ static void CheckStructToStructSubsetWithNulls(
const auto options5_duplicate_field_names_null =
CastOptions::Safe(dest5_duplicate_field_names_null);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
TypeError, ::testing::HasSubstr("struct fields don't match"),
Cast(src_duplicate_field_names_null, options5_duplicate_field_names_null));
}
}
Expand Down Expand Up @@ -4057,9 +4052,7 @@ TEST(Cast, StructToSameSizedButDifferentNamedStruct) {
const auto options2 = CastOptions::Safe(dest2);

EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src, options2));
TypeError, ::testing::HasSubstr("struct fields don't match"), Cast(src, options2));
}

TEST(Cast, StructToBiggerStruct) {
Expand All @@ -4075,9 +4068,7 @@ TEST(Cast, StructToBiggerStruct) {
const auto options1 = CastOptions::Safe(dest1);

EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src, options1));
TypeError, ::testing::HasSubstr("struct fields don't match"), Cast(src, options1));

const auto dest2 =
arrow::struct_({std::make_shared<Field>("a", int8()),
Expand All @@ -4086,9 +4077,7 @@ TEST(Cast, StructToBiggerStruct) {
const auto options2 = CastOptions::Safe(dest2);

EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("struct fields don't match or are in the wrong order"),
Cast(src, options2));
TypeError, ::testing::HasSubstr("struct fields don't match"), Cast(src, options2));
}

TEST(Cast, StructToBiggerNullableStruct) {
Expand Down

0 comments on commit 31a3e21

Please sign in to comment.