Skip to content
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
11 changes: 9 additions & 2 deletions cpp/src/arrow/dataset/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,15 @@ Result<ProjectionDescr> ProjectionDescr::FromExpressions(
compute::MakeStructOptions project_options{std::move(names)};

for (size_t i = 0; i < exprs.size(); ++i) {
if (auto ref = exprs[i].field_ref()) {
// set metadata and nullability for plain field references
compute::Expression expr = exprs[i];
while (const auto* call = expr.call()) {
if (call->function_name != "cast" || call->arguments.size() != 1) {
break;
}
expr = call->arguments[0];
}
if (auto ref = expr.field_ref()) {
// Preserve metadata and nullability through casts rooted at a field.
ARROW_ASSIGN_OR_RAISE(auto field, ref->GetOne(dataset_schema));
project_options.field_nullability[i] = field->nullable();
project_options.field_metadata[i] = field->metadata();
Expand Down
27 changes: 27 additions & 0 deletions cpp/src/arrow/dataset/scanner_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "arrow/testing/matchers.h"
#include "arrow/testing/util.h"
#include "arrow/util/byte_size.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/range.h"
#include "arrow/util/thread_pool.h"
#include "arrow/util/vector.h"
Expand Down Expand Up @@ -2146,6 +2147,32 @@ TEST_F(TestScannerBuilder, TestProject) {
ASSERT_RAISES(Invalid, builder.Project({literal(2), field_ref("a")}, {"a"}));
}

TEST_F(TestScannerBuilder, TestProjectPreservesNullabilityThroughCast) {
auto metadata = key_value_metadata({{"key", "value"}});
auto dataset_schema = schema({
field("required", int32(), /*nullable=*/false, metadata),
field("optional", int16(), /*nullable=*/true),
});
auto cast_to_i64 =
compute::call("cast", {field_ref("required")}, compute::CastOptions::Safe(int64()));
auto cast_to_f64 = compute::call("cast", {field_ref("optional")},
compute::CastOptions::Safe(float64()));

ASSERT_OK_AND_ASSIGN(auto projection, ProjectionDescr::FromExpressions(
{cast_to_i64, cast_to_f64},
{"required", "optional"}, *dataset_schema));

const auto& required = projection.schema->field(0);
EXPECT_EQ(*required->type(), *int64());
EXPECT_FALSE(required->nullable());
ASSERT_NE(required->metadata(), nullptr);
EXPECT_TRUE(required->metadata()->Equals(*metadata));

const auto& optional = projection.schema->field(1);
EXPECT_EQ(*optional->type(), *float64());
EXPECT_TRUE(optional->nullable());
}

TEST_F(TestScannerBuilder, TestFilter) {
ScannerBuilder builder(dataset_, options_);

Expand Down