raghuvanshraj commented on code in PR #3699:
URL: https://github.com/apache/iceberg-rust/pull/3699#discussion_r4628674040


##########
crates/iceberg/src/writer/file_writer/parquet_writer.rs:
##########
@@ -1061,18 +1089,246 @@ mod tests {
     }
 
     #[test]
-    fn test_index_by_parquet_path_variant_is_unsupported() {
-        // Writing variant columns to Parquet is not supported yet; indexing a 
schema that
-        // contains one must error rather than silently miss-map columns.
+    fn test_index_by_parquet_path_variant() {
+        // Both leaves of a variant group map to the id of the variant field, 
at the top level
+        // and inside a struct, a list and a map.
         let schema = Schema::builder()
             .with_fields(vec![
                 NestedField::optional(1, "v", 
Type::Variant(VariantType)).into(),
+                NestedField::optional(
+                    2,
+                    "s",
+                    Type::Struct(StructType::new(vec![
+                        NestedField::required(3, "sv", 
Type::Variant(VariantType)).into(),
+                    ])),
+                )
+                .into(),
+                NestedField::optional(
+                    4,
+                    "l",
+                    Type::List(ListType::new(
+                        NestedField::list_element(5, 
Type::Variant(VariantType), true).into(),
+                    )),
+                )
+                .into(),
+                NestedField::optional(
+                    6,
+                    "m",
+                    Type::Map(MapType::new(
+                        NestedField::map_key_element(7, 
Type::Primitive(PrimitiveType::String))
+                            .into(),
+                        NestedField::map_value_element(8, 
Type::Variant(VariantType), true).into(),
+                    )),
+                )
+                .into(),
             ])
             .build()
             .unwrap();
         let mut visitor = IndexByParquetPathName::new();
-        let err = visit_schema(&schema, &mut visitor).unwrap_err();
-        assert_eq!(err.kind(), ErrorKind::FeatureUnsupported, "{err}");
+        visit_schema(&schema, &mut visitor).unwrap();
+        let expect = HashMap::from([
+            ("v.metadata".to_string(), 1),
+            ("v.value".to_string(), 1),
+            ("s.sv.metadata".to_string(), 3),
+            ("s.sv.value".to_string(), 3),
+            ("l.list.element.metadata".to_string(), 5),
+            ("l.list.element.value".to_string(), 5),
+            ("m.key_value.key".to_string(), 7),
+            ("m.key_value.value.metadata".to_string(), 8),
+            ("m.key_value.value.value".to_string(), 8),
+        ]);
+        assert_eq!(visitor.name_to_id, expect);
+        assert!(visitor.is_variant_value("v.value"));
+        assert!(visitor.is_variant_value("m.key_value.value.value"));
+        assert!(!visitor.is_variant_value("v.metadata"));
+        assert!(!visitor.is_variant_value("m.key_value.key"));
+    }
+
+    /// Makes one variant from each JSON row in the Arrow layout of the 
Iceberg table type.
+    /// `None` makes a null row.
+    fn variants_from_json(rows: &[Option<&str>]) -> ArrayRef {
+        let json = Arc::new(arrow_array::StringArray::from(rows.to_vec())) as 
ArrayRef;
+        let variants = 
ArrayRef::from(parquet::variant::json_to_variant(&json).unwrap());
+        let table_type = 
crate::arrow::type_to_arrow_type(&Type::Variant(VariantType)).unwrap();
+        arrow_cast::cast(&variants, &table_type).unwrap()
+    }
+
+    /// Renders every row of a variant column as JSON. A null row renders as 
`None`.
+    fn variants_to_json(column: &ArrayRef) -> Vec<Option<String>> {
+        let variants = 
parquet::variant::VariantArray::try_new(column.as_ref()).unwrap();
+        let json = 
parquet::variant::variant_to_json(&ArrayRef::from(variants)).unwrap();
+        (0..json.len())
+            .map(|i| (!json.is_null(i)).then(|| json.value(i).to_string()))
+            .collect()
+    }
+
+    /// Writes an unshredded variant column, checks the Parquet schema and the 
data file
+    /// metrics, and reads the rows back through the Iceberg reader.
+    #[tokio::test]
+    async fn test_parquet_writer_variant_roundtrip() -> Result<()> {
+        let temp_dir = TempDir::new().unwrap();
+        let file_io = FileIO::new_with_fs();
+        let schema = Arc::new(
+            Schema::builder()
+                .with_fields(vec![
+                    NestedField::required(1, "id", 
Type::Primitive(PrimitiveType::Int)).into(),
+                    NestedField::optional(2, "v", 
Type::Variant(VariantType)).into(),

Review Comment:
   Added l: list<variant> and m: map<string, variant> with nulls and empty 
containers, and asserted value counts, null counts and column sizes for them. 
The counts come out as for any repeated leaf (an empty or null container adds 
one value and one null), same as the string keys next to them.
   
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to