lidavidm commented on a change in pull request #12724: URL: https://github.com/apache/arrow/pull/12724#discussion_r837596344
########## File path: cpp/src/arrow/compute/kernels/scalar_cast_test.cc ########## @@ -2245,8 +2245,182 @@ static void CheckStructToStruct( } } -TEST(Cast, StructToSameSizedAndNamedStruct) { - CheckStructToStruct({int32(), float32(), int64()}); +static void CheckStructToStructSubset( + const std::vector<std::shared_ptr<DataType>>& value_types) { + for (const auto& src_value_type : value_types) { + ARROW_SCOPED_TRACE("From type: ", src_value_type->ToString()); + for (const auto& dest_value_type : value_types) { + ARROW_SCOPED_TRACE("To type: ", dest_value_type->ToString()); + + std::vector<std::string> field_names = {"a", "b", "c", "d", "e"}; + + std::shared_ptr<Array> a1, b1, c1, d1, e1; + a1 = ArrayFromJSON(src_value_type, "[1, 2, 5]"); + b1 = ArrayFromJSON(src_value_type, "[3, 4, 7]"); + c1 = ArrayFromJSON(src_value_type, "[9, 11, 44]"); + d1 = ArrayFromJSON(src_value_type, "[6, 51, 49]"); + e1 = ArrayFromJSON(src_value_type, "[19, 17, 74]"); + + std::shared_ptr<Array> a2, b2, c2, d2, e2; + a2 = ArrayFromJSON(dest_value_type, "[1, 2, 5]"); + b2 = ArrayFromJSON(dest_value_type, "[3, 4, 7]"); + c2 = ArrayFromJSON(dest_value_type, "[9, 11, 44]"); + d2 = ArrayFromJSON(dest_value_type, "[6, 51, 49]"); + e2 = ArrayFromJSON(dest_value_type, "[19, 17, 74]"); + + ASSERT_OK_AND_ASSIGN(auto src, + StructArray::Make({a1, b1, c1, d1, e1}, field_names)); + ASSERT_OK_AND_ASSIGN(auto dest1, + StructArray::Make({a2}, std::vector<std::string>{"a"})); + CheckCast(src, dest1); + + ASSERT_OK_AND_ASSIGN( + auto dest2, StructArray::Make({b2, c2}, std::vector<std::string>{"b", "c"})); + CheckCast(src, dest2); + + ASSERT_OK_AND_ASSIGN( + auto dest3, + StructArray::Make({c2, d2, e2}, std::vector<std::string>{"c", "d", "e"})); + CheckCast(src, dest3); + + ASSERT_OK_AND_ASSIGN( + auto dest4, StructArray::Make({a2, b2, c2, e2}, + std::vector<std::string>{"a", "b", "c", "e"})); + CheckCast(src, dest4); + + ASSERT_OK_AND_ASSIGN( + auto dest5, StructArray::Make({a2, b2, c2, d2, e2}, {"a", "b", "c", "d", "e"})); + CheckCast(src, dest5); + + // field does not exist + const auto dest6 = arrow::struct_({std::make_shared<Field>("a", int8()), + std::make_shared<Field>("d", int16()), + std::make_shared<Field>("f", int64())}); + const auto options6 = CastOptions::Safe(dest6); + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, + ::testing::HasSubstr("struct fields don't match or are in the wrong order"), + Cast(src, options6)); + + // fields in wrong order + const auto dest7 = arrow::struct_({std::make_shared<Field>("a", int8()), + std::make_shared<Field>("c", int16()), + std::make_shared<Field>("b", int64())}); + 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)); + + // duplicate field names + const auto dest8 = arrow::struct_( + {std::make_shared<Field>("a", int8()), std::make_shared<Field>("c", int16()), + std::make_shared<Field>("d", int32()), std::make_shared<Field>("a", int64())}); + const auto options8 = CastOptions::Safe(dest8); + EXPECT_RAISES_WITH_MESSAGE_THAT( + TypeError, + ::testing::HasSubstr("struct fields don't match or are in the wrong order"), + Cast(src, options8)); + } + } +} + +static void CheckStructToStructSubsetWithNulls( Review comment: struct arrays are arrays, they have their own null bitmap, but their child arrays are still arrays, and have their own null bitmap, these are distinct -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org