leaves12138 commented on code in PR #1041:
URL: https://github.com/apache/paimon-rust/pull/1041#discussion_r4184339269


##########
crates/paimon/src/arrow/format/variant_projection.rs:
##########
@@ -0,0 +1,236 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Read-type-driven Parquet clipping, mirroring VariantShreddingTypePruner.
+
+use crate::spec::{parse_variant_metadata, RowType};
+use crate::variant::{parse_path, PathSegment};
+use parquet::basic::LogicalType;
+use parquet::schema::types::Type;
+use std::collections::{HashMap, HashSet};
+
+#[derive(Default)]
+struct PathNode {
+    children: HashMap<String, PathNode>,
+    element: Option<Box<PathNode>>,
+    keep_all: bool,
+}
+
+/// The requested physical leaves, relative to the Variant column. Invalid
+/// paths retain the full input so strict/try extraction handles the error at
+/// the same point as an unshredded read.
+pub(super) fn projected_paths(row: &RowType, physical: &Type) -> 
HashSet<Vec<String>> {
+    let mut root = PathNode::default();
+    for field in row.fields() {
+        let segments = field.description().and_then(|description| {
+            parse_variant_metadata(description)
+                .ok()
+                .and_then(|metadata| parse_path(metadata.path()).ok())
+        });
+        let Some(segments) = segments else {
+            root.keep_all = true;
+            break;
+        };
+        let mut node = &mut root;
+        for segment in segments {
+            node = match segment {
+                PathSegment::Key(key) => node.children.entry(key).or_default(),
+                PathSegment::Index(_) => 
node.element.get_or_insert_with(Default::default),
+            };
+        }
+        node.keep_all = true;
+    }
+    let mut result = HashSet::new();
+    clip(physical, &root, &mut Vec::new(), &mut result);
+    result
+}
+
+pub(super) fn all_leaves(
+    physical: &Type,
+    path: &mut Vec<String>,
+    output: &mut HashSet<Vec<String>>,
+) {
+    if physical.is_primitive() {
+        output.insert(path.clone());
+    } else {
+        for child in physical.get_fields() {
+            path.push(child.name().to_string());
+            all_leaves(child, path, output);
+            path.pop();
+        }
+    }
+}
+
+fn keep_child(child: &Type, path: &mut Vec<String>, output: &mut 
HashSet<Vec<String>>) {
+    path.push(child.name().to_string());
+    all_leaves(child, path, output);
+    path.pop();
+}
+
+fn clip(
+    physical: &Type,
+    node: &PathNode,
+    path: &mut Vec<String>,
+    output: &mut HashSet<Vec<String>>,
+) {
+    if physical.is_primitive() || node.keep_all {
+        all_leaves(physical, path, output);
+        return;
+    }
+    let fields = physical.get_fields();
+    let Some(typed) = fields.iter().find(|child| child.name() == 
"typed_value") else {
+        all_leaves(physical, path, output);
+        return;
+    };
+    let logical = typed.get_basic_info().logical_type_ref();
+    let object = !typed.is_primitive() && logical.is_none();
+    let list = !typed.is_primitive()
+        && matches!(logical, Some(LogicalType::List))

Review Comment:
   The list check here is weaker than Java's `VariantShreddingTypePruner`: Java 
calls `ParquetListLayoutResolver.isCanonicalList(typedValue)` before 
descending, while this only checks the LIST logical annotation. Legacy LIST 
layouts (for example a repeated group named `array` or `<list>_tuple`) are 
intentionally not canonical in Java and must be kept whole; this code instead 
assumes `typed_value / list / element` and clips through 
`repeated.get_fields().first()`. On such files the projected leaf paths can be 
wrong and required Variant data may be pruned. Please mirror the canonical-list 
check (or conservatively keep non-canonical LISTs whole).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to