Skip to content

Commit ca70043

Browse files
NickCrewspitrou
authored andcommitted
[C++]: support casting nullable fields to non-nullable if there are no null values
Fixes apache#33592
1 parent 251758d commit ca70043

2 files changed

Lines changed: 34 additions & 24 deletions

File tree

‎cpp/src/arrow/compute/kernels/scalar_cast_nested.cc‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,6 @@ struct CastStruct {
363363
const auto& in_field = in_type.field(in_field_index);
364364
// If there are more in_fields check if they match the out_field.
365365
if (in_field->name() == out_field->name()) {
366-
if (in_field->nullable() && !out_field->nullable()) {
367-
return Status::TypeError("cannot cast nullable field to non-nullable field: ",
368-
in_type.ToString(), " ", out_type.ToString());
369-
}
370366
// Found matching in_field and out_field.
371367
fields_to_select[out_field_index++] = in_field_index;
372368
// Using the same in_field for multiple out_fields is not allowed.
@@ -403,17 +399,26 @@ struct CastStruct {
403399
}
404400

405401
int out_field_index = 0;
406-
for (int field_index : fields_to_select) {
407-
const auto& target_type = out->type()->field(out_field_index++)->type();
408-
if (field_index == kFillNullSentinel) {
409-
ARROW_ASSIGN_OR_RAISE(auto nulls,
410-
MakeArrayOfNull(target_type->GetSharedPtr(), batch.length));
402+
for (int in_field_index : fields_to_select) {
403+
const auto& out_field = out_type.field(out_field_index++);
404+
const auto& out_field_type = out_field->type();
405+
if (in_field_index == kFillNullSentinel) {
406+
ARROW_ASSIGN_OR_RAISE(
407+
auto nulls, MakeArrayOfNull(out_field_type->GetSharedPtr(), batch.length));
411408
out_array->child_data.push_back(nulls->data());
412409
} else {
413-
const auto& values = (in_array.child_data[field_index].ToArrayData()->Slice(
410+
const auto& in_field = in_type.field(in_field_index);
411+
const auto& in_values = (in_array.child_data[in_field_index].ToArrayData()->Slice(
414412
in_array.offset, in_array.length));
415-
ARROW_ASSIGN_OR_RAISE(Datum cast_values,
416-
Cast(values, target_type, options, ctx->exec_context()));
413+
if (in_field->nullable() && !out_field->nullable() &&
414+
in_values->GetNullCount() > 0) {
415+
return Status::Invalid(
416+
"field '", in_field->name(), "' of type ", in_field->type()->ToString(),
417+
" has nulls. Can't cast to non-nullable field '", out_field->name(),
418+
"' of type ", out_field_type->ToString());
419+
}
420+
ARROW_ASSIGN_OR_RAISE(Datum cast_values, Cast(in_values, out_field_type, options,
421+
ctx->exec_context()));
417422
DCHECK(cast_values.is_array());
418423
out_array->child_data.push_back(cast_values.array());
419424
}

‎cpp/src/arrow/compute/kernels/scalar_cast_test.cc‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4119,7 +4119,7 @@ TEST(Cast, StructToDifferentNullabilityStruct) {
41194119
CheckCast(src_non_nullable, dest3_nullable);
41204120
}
41214121
{
4122-
// But NOT OK to go from nullable to non-nullable...
4122+
// But when going from nullable to non-nullable, all data must be non-null...
41234123
std::vector<std::shared_ptr<Field>> fields_src_nullable = {
41244124
std::make_shared<Field>("a", int8(), true),
41254125
std::make_shared<Field>("b", int8(), true),
@@ -4140,8 +4140,10 @@ TEST(Cast, StructToDifferentNullabilityStruct) {
41404140
const auto dest1_non_nullable = arrow::struct_(fields_dest1_non_nullable);
41414141
const auto options1_non_nullable = CastOptions::Safe(dest1_non_nullable);
41424142
EXPECT_RAISES_WITH_MESSAGE_THAT(
4143-
TypeError,
4144-
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
4143+
Invalid,
4144+
::testing::HasSubstr(
4145+
"field 'a' of type int8 has nulls. Can't cast to non-nullable field 'a' "
4146+
"of type int64"),
41454147
Cast(src_nullable, options1_non_nullable));
41464148

41474149
std::vector<std::shared_ptr<Field>> fields_dest2_non_nullable = {
@@ -4150,18 +4152,21 @@ TEST(Cast, StructToDifferentNullabilityStruct) {
41504152
const auto dest2_non_nullable = arrow::struct_(fields_dest2_non_nullable);
41514153
const auto options2_non_nullable = CastOptions::Safe(dest2_non_nullable);
41524154
EXPECT_RAISES_WITH_MESSAGE_THAT(
4153-
TypeError,
4154-
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
4155+
Invalid,
4156+
::testing::HasSubstr(
4157+
"field 'a' of type int8 has nulls. Can't cast to non-nullable field 'a' "
4158+
"of type int64"),
41554159
Cast(src_nullable, options2_non_nullable));
41564160

4157-
std::vector<std::shared_ptr<Field>> fields_dest3_non_nullable = {
4161+
std::shared_ptr<Array> c_dest_no_nulls;
4162+
c_dest_no_nulls = ArrayFromJSON(int64(), "[9, 11, 44]");
4163+
std::vector<std::shared_ptr<Field>> fields_dest_no_nulls = {
41584164
std::make_shared<Field>("c", int64(), false)};
4159-
const auto dest3_non_nullable = arrow::struct_(fields_dest3_non_nullable);
4160-
const auto options3_non_nullable = CastOptions::Safe(dest3_non_nullable);
4161-
EXPECT_RAISES_WITH_MESSAGE_THAT(
4162-
TypeError,
4163-
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
4164-
Cast(src_nullable, options3_non_nullable));
4165+
ASSERT_OK_AND_ASSIGN(auto dest_no_nulls,
4166+
StructArray::Make({c_dest_no_nulls}, fields_dest_no_nulls));
4167+
const auto options3_non_nullable =
4168+
CastOptions::Safe(arrow::struct_(fields_dest_no_nulls));
4169+
CheckCast(src_nullable, dest_no_nulls, options3_non_nullable);
41654170
}
41664171
}
41674172

0 commit comments

Comments
 (0)