GH-50596: [C++] AppendScalar implementation uses polymorphism - #50652
GH-50596: [C++] AppendScalar implementation uses polymorphism#50652KHARSHAVARDHAN-eng wants to merge 2 commits into
Conversation
|
|
c896c6d to
fd0653f
Compare
fd0653f to
4841ad1
Compare
There was a problem hiding this comment.
Pull request overview
Refactors C++ ArrayBuilder::AppendScalar / AppendScalars away from the centralized visitor-based implementation and into per-builder virtual overrides, aligning scalar appends with the existing builder polymorphism.
Changes:
- Removes the centralized
AppendScalarImplvisitor fromArrayBuilderand switches the base implementation to a NotImplemented fallback. - Adds
AppendScalar/AppendScalarsoverrides across primitive, binary, decimal, nested, and union builders. - Introduces shared helper implementations for decimal builder scalar appends.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| cpp/src/arrow/array/builder_base.cc | Removes visitor-based append implementation; base AppendScalar(s) now delegates to overrides / NotImplemented. |
| cpp/src/arrow/array/builder_primitive.h | Adds polymorphic scalar append support for NullBuilder, NumericBuilder, and BooleanBuilder. |
| cpp/src/arrow/array/builder_binary.h | Adds polymorphic scalar append support for binary-like builders (templated base + declarations for view/fixed-size). |
| cpp/src/arrow/array/builder_binary.cc | Implements scalar append logic for BinaryViewBuilder and FixedSizeBinaryBuilder. |
| cpp/src/arrow/array/builder_decimal.h | Declares scalar append overrides for decimal builders. |
| cpp/src/arrow/array/builder_decimal.cc | Implements decimal scalar append via shared helpers. |
| cpp/src/arrow/array/builder_nested.h | Declares scalar append overrides for list-like, map, fixed-size list, and struct builders. |
| cpp/src/arrow/array/builder_nested.cc | Implements scalar append for nested builders and relocates template instantiations. |
| cpp/src/arrow/array/builder_union.h | Declares scalar append overrides for dense/sparse union builders. |
| cpp/src/arrow/array/builder_union.cc | Implements scalar append logic for dense/sparse union builders. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if (scalar.type->id() == Type::NA) { | ||
| return AppendNulls(n_repeats); | ||
| } | ||
| if (scalar.type->id() != type()->id()) { | ||
| return Status::Invalid("Cannot append scalar of type ", scalar.type->ToString(), |
| if (scalars.empty()) return Status::OK(); | ||
| const auto ty = type(); | ||
| for (const auto& scalar : scalars) { | ||
| if (!scalar->type->Equals(ty)) { | ||
| return Status::Invalid("Cannot append scalar of type ", scalar->type->ToString(), | ||
| " to builder for type ", type()->ToString()); | ||
| } | ||
| RETURN_NOT_OK(AppendScalar(*scalar, 1)); | ||
| } | ||
|
|
||
| using Iterator = DerefConstIterator<ScalarVector::const_iterator>; | ||
| return AppendScalarImpl<Iterator>{Iterator{scalars.begin()}, Iterator{scalars.end()}, | ||
| /*n_repeats=*/1, this} | ||
| .Convert(); | ||
| return Status::OK(); |
|
Thanks for the review! I’ve addressed both points by restoring strict type validation and adding pre-validation for AppendScalars to prevent partial mutations. I also added regression tests and verified the full arrow-array-test suite (1061/1061 passing). |
|
@raulcd , |
Summary
ArrayBuilder::AppendScalarandAppendScalarspreviously relied on thecentralized
AppendScalarImplvisitor to dispatch scalar appends based onthe scalar type.
This refactors scalar appending to use virtual method polymorphism within
the builder hierarchy, moving the implementation into the appropriate
builder classes while preserving the existing behavior.
Implementation
AppendScalarImplvisitor.AppendScalarandAppendScalarson the relevant builderclasses.
dispatch with virtual method polymorphism.
Testing
arrow-array-test.arrow-array-testsuite.