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

Function Unflatten

cpp/src/parquet/schema.cc:555–619  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

553// Schema converters
554
555std::unique_ptr<Node> Unflatten(std::span<const format::SchemaElement> elements,
556 int max_depth) {
557 if (elements.empty()) {
558 throw ParquetException("Empty Parquet schema (no root)");
559 }
560 if (elements[0].num_children == 0) {
561 if (elements.size() == 1) {
562 // Degenerate case of Parquet file with no columns
563 return GroupNode::FromParquet(&elements[0], {});
564 } else {
565 throw ParquetException(
566 "Parquet schema had multiple nodes but root had no children");
567 }
568 }
569
570 // We don't check that the root node is repeated since this is not
571 // consistently set by implementations
572
573 size_t pos = 0;
574 size_t num_reserved = 0;
575
576 std::function<std::unique_ptr<Node>(int depth)> NextNode = [&](int depth) {
577 if (pos == elements.size()) {
578 throw ParquetException("Malformed Parquet schema: not enough elements");
579 }
580 const SchemaElement& element = elements[pos++];
581 const void* opaque_element = static_cast<const void*>(&element);
582
583 if (element.num_children == 0 && element.__isset.type) {
584 // Leaf (primitive) node: always has a type
585 return PrimitiveNode::FromParquet(opaque_element);
586 } else {
587 // Group node (may have 0 children, but cannot have a type)
588 // Protect against denial-of-service through stack exhaustion when parsing
589 // deeply nested schemas.
590 if (depth >= max_depth) {
591 std::stringstream ss;
592 ss << "Parquet schema too deeply nested, consider increasing schema depth limit "
593 "(current limit is "
594 << max_depth << ")";
595 throw ParquetException(ss.str());
596 }
597 if (element.num_children < 0) {
598 throw ParquetException("Malformed Parquet schema: negative number of children");
599 }
600 // Guard against excessive pre-reservation by an invalid schema.
601 // For example, a sequence of group nodes advertising N, N-1, etc. children
602 // could lead to quadratic preallocation.
603 num_reserved += static_cast<size_t>(element.num_children);
604 if (num_reserved > elements.size()) {
605 throw ParquetException("Malformed Parquet schema: not enough elements");
606 }
607 NodeVector fields(element.num_children);
608 for (int i = 0; i < element.num_children; ++i) {
609 fields[i] = NextNode(depth + 1);
610 }
611 return GroupNode::FromParquet(opaque_element, std::move(fields));
612 }

Callers 3

InitSchemaMethod · 0.85
SchemaFromThriftFunction · 0.85
ConvertMethod · 0.85

Calls 4

ParquetExceptionFunction · 0.85
strMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45

Tested by 1

ConvertMethod · 0.68