MCPcopy Create free account
hub / github.com/apache/arrow / NestedSelector

Class NestedSelector

cpp/src/arrow/type.cc:1636–1781  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1634// of its children.
1635template <typename T, bool IsFlattening = false>
1636class NestedSelector {
1637 public:
1638 using ArrowType = T;
1639 using Util = NestedSelectorUtil;
1640
1641 explicit NestedSelector(const std::vector<std::shared_ptr<T>>& children)
1642 : parent_or_children_(&children) {}
1643 explicit NestedSelector(const T& parent) : parent_or_children_(&parent) {}
1644 explicit NestedSelector(std::shared_ptr<T> parent)
1645 : owned_parent_(std::move(parent)), parent_or_children_(owned_parent_.get()) {}
1646 template <typename Arg>
1647 NestedSelector(Arg&& arg, MemoryPool* pool) : NestedSelector(std::forward<Arg>(arg)) {
1648 if (pool) {
1649 pool_ = pool;
1650 }
1651 }
1652
1653 // If the index is out of bounds, this returns an invalid selector rather than an
1654 // error.
1655 Result<NestedSelector> GetChild(int i) const {
1656 std::shared_ptr<T> child;
1657 if (auto parent = get_parent()) {
1658 const DataType* type = Util::GetType(*parent);
1659 // We avoid this check for schema fields since it's inconsequential (plus there are
1660 // tests elsewhere that rely on it not happening)
1661 if constexpr (!std::is_same_v<T, Field>) {
1662 if (ARROW_PREDICT_FALSE(type->id() != Type::STRUCT)) {
1663 return Util::NonStructError();
1664 }
1665 }
1666 // Bounds-check the index *once* using the parent's type
1667 if (ARROW_PREDICT_TRUE(i >= 0 && i < type->num_fields())) {
1668 ARROW_ASSIGN_OR_RAISE(child, GetChild(*parent, i, pool_));
1669 }
1670 } else if (auto children = get_children()) {
1671 if (ARROW_PREDICT_TRUE(i >= 0 && static_cast<size_t>(i) < children->size())) {
1672 child = (*children)[i];
1673 }
1674 }
1675 return NestedSelector(std::move(child), pool_);
1676 }
1677
1678 Result<std::shared_ptr<T>> Finish() const {
1679 DCHECK(get_parent() && owned_parent_);
1680 return owned_parent_;
1681 }
1682
1683 template <typename OStream, typename U = T>
1684 std::enable_if_t<std::is_same_v<U, Field>> Summarize(OStream* os) const {
1685 const FieldVector* fields = get_children();
1686 if (!fields && get_parent()) {
1687 fields = &get_parent()->type()->fields();
1688 }
1689 *os << "fields: { ";
1690 if (fields) {
1691 for (const auto& field : *fields) {
1692 *os << field->ToString() << ", ";
1693 }

Callers 1

GetChildMethod · 0.85

Calls 2

default_memory_poolFunction · 0.85
is_validFunction · 0.50

Tested by

no test coverage detected