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-33592: [C++] support casting nullable fields to non-nullable if there are no null values #43782

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions cpp/src/arrow/compute/kernels/scalar_cast_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,6 @@ struct CastStruct {
const auto& in_field = in_type.field(in_field_index);
const auto& out_field = out_type.field(out_field_index);
if (in_field->name() == out_field->name()) {
if (in_field->nullable() && !out_field->nullable()) {
return Status::TypeError("cannot cast nullable field to non-nullable field: ",
in_type.ToString(), " ", out_type.ToString());
}
fields_to_select[out_field_index++] = in_field_index;
}
}
Expand All @@ -382,10 +378,16 @@ struct CastStruct {
for (int field_index : fields_to_select) {
const auto& values = (in_array.child_data[field_index].ToArrayData()->Slice(
in_array.offset, in_array.length));
const auto& target_type = out->type()->field(out_field_index++)->type();
const auto& target_field = out->type()->field(out_field_index++);

if (!target_field->nullable() && values->null_count > 0) {
return Status::TypeError("field '", target_field->name(),
"' has nulls. Can't cast to non-nullable type ",
target_field->type()->ToString());
}

ARROW_ASSIGN_OR_RAISE(Datum cast_values,
Cast(values, target_type, options, ctx->exec_context()));
ARROW_ASSIGN_OR_RAISE(Datum cast_values, Cast(values, target_field->type(), options,
ctx->exec_context()));

DCHECK(cast_values.is_array());
out_array->child_data.push_back(cast_values.array());
Expand Down
25 changes: 14 additions & 11 deletions cpp/src/arrow/compute/kernels/scalar_cast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2898,7 +2898,7 @@ TEST(Cast, StructToDifferentNullabilityStruct) {
CheckCast(src_non_nullable, dest3_nullable);
}
{
// But NOT OK to go from nullable to non-nullable...
// But when going from nullable to non-nullable, all data must be non-null...
std::vector<std::shared_ptr<Field>> fields_src_nullable = {
std::make_shared<Field>("a", int8(), true),
std::make_shared<Field>("b", int8(), true),
Expand All @@ -2920,27 +2920,30 @@ TEST(Cast, StructToDifferentNullabilityStruct) {
const auto options1_non_nullable = CastOptions::Safe(dest1_non_nullable);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
::testing::HasSubstr(
"field 'a' has nulls. Can't cast to non-nullable type int64"),
Cast(src_nullable, options1_non_nullable));

std::vector<std::shared_ptr<Field>> fields_dest2_non_nullable = {
std::make_shared<Field>("a", int64(), false),
std::make_shared<Field>("c", int64(), false)};
const auto dest2_non_nullable = arrow::struct_(fields_dest2_non_nullable);
const auto options2_non_nullable = CastOptions::Safe(dest2_non_nullable);
const auto options2_non_nullable = CastOptions::Unsafe(dest2_non_nullable);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
::testing::HasSubstr(
"field 'a' has nulls. Can't cast to non-nullable type int64"),
Cast(src_nullable, options2_non_nullable));

std::vector<std::shared_ptr<Field>> fields_dest3_non_nullable = {
std::shared_ptr<Array> c_dest_no_nulls;
c_dest_no_nulls = ArrayFromJSON(int64(), "[9, 11, 44]");
std::vector<std::shared_ptr<Field>> fields_dest_no_nulls = {
std::make_shared<Field>("c", int64(), false)};
const auto dest3_non_nullable = arrow::struct_(fields_dest3_non_nullable);
const auto options3_non_nullable = CastOptions::Safe(dest3_non_nullable);
EXPECT_RAISES_WITH_MESSAGE_THAT(
TypeError,
::testing::HasSubstr("cannot cast nullable field to non-nullable field"),
Cast(src_nullable, options3_non_nullable));
ASSERT_OK_AND_ASSIGN(auto dest_no_nulls,
StructArray::Make({c_dest_no_nulls}, fields_dest_no_nulls));
const auto options3_non_nullable =
CastOptions::Unsafe(arrow::struct_(fields_dest_no_nulls));
CheckCast(src_nullable, dest_no_nulls, options3_non_nullable);
}
}

Expand Down
Loading