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
24 changes: 24 additions & 0 deletions cpp/src/arrow/acero/plan_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,30 @@ TEST(ExecPlanExecution, DeclarationToSchema) {
AssertSchemaEqual(expected_out_schema, actual_out_schema);
}

TEST(ExecPlanExecution, ProjectPreservesDirectFieldNullability) {
auto input_schema = schema({field("r", int64(), false), field("n", int64(), true)});
Declaration source(
"exec_batch_source",
ExecBatchSourceNodeOptions(input_schema, std::vector<compute::ExecBatch>{}));

auto direct = Declaration::Sequence(
{source,
{"project", ProjectNodeOptions({field_ref("r"), field_ref("n")}, {"r", "n"})}});
ASSERT_OK_AND_ASSIGN(auto direct_schema, DeclarationToSchema(direct));
AssertSchemaEqual(input_schema, direct_schema);

auto reordered = Declaration::Sequence(
{source,
{"project",
ProjectNodeOptions({field_ref("n"), field_ref("r"),
call("add", {field_ref("r"), literal(int64_t{1})})},
{"n", "renamed", "computed"})}});
ASSERT_OK_AND_ASSIGN(auto reordered_schema, DeclarationToSchema(reordered));
AssertSchemaEqual(schema({field("n", int64(), true), field("renamed", int64(), false),
field("computed", int64(), true)}),
reordered_schema);
}

TEST(ExecPlanExecution, DeclarationToReader) {
auto basic_data = MakeBasicBatches();
auto plan = Declaration::Sequence(
Expand Down
16 changes: 13 additions & 3 deletions cpp/src/arrow/acero/project_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,24 @@ class ProjectNode : public MapNode {
" doesn't match size of expressions " +
std::to_string(exprs.size())));
}
const auto& input_schema = *inputs[0]->output_schema();
FieldVector fields(exprs.size());
int i = 0;
for (auto& expr : exprs) {
if (!expr.IsBound()) {
ARROW_ASSIGN_OR_RAISE(expr, expr.Bind(*inputs[0]->output_schema(),
plan->query_context()->exec_context()));
ARROW_ASSIGN_OR_RAISE(
expr, expr.Bind(input_schema, plan->query_context()->exec_context()));
}
fields[i] = field(std::move(names[i]), expr.type()->GetSharedPtr());
bool nullable = true;
if (const auto* parameter = expr.parameter()) {
if (parameter->indices.size() == 1) {
int index = parameter->indices[0];
if (index >= 0 && index < input_schema.num_fields()) {
nullable = input_schema.field(index)->nullable();
}
}
}
fields[i] = field(std::move(names[i]), expr.type()->GetSharedPtr(), nullable);
++i;
}
return plan->EmplaceNode<ProjectNode>(plan, std::move(inputs),
Expand Down
Loading