rdblue commented on code in PR #13219:
URL: https://github.com/apache/iceberg/pull/13219#discussion_r2229583249


##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/data/TestSparkVariants.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.
+ */
+package org.apache.iceberg.spark.data;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.UUID;
+import org.apache.iceberg.spark.TestBase;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.variants.ShreddedObject;
+import org.apache.iceberg.variants.ValueArray;
+import org.apache.iceberg.variants.Variant;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+import org.apache.iceberg.variants.Variants;
+import org.apache.spark.sql.types.VariantType$;
+import org.apache.spark.unsafe.types.VariantVal;
+import org.junit.jupiter.api.Test;
+
+public class TestSparkVariants extends TestBase {
+
+  @Test
+  public void testIcebergVariantTypeToSparkVariantType() {
+    // Test that Iceberg's VariantType converts to Spark's VariantType
+    Types.VariantType icebergVariantType = Types.VariantType.get();
+
+    // This would be done by TypeToSparkType converter
+    // For now, we just verify the types exist and can be referenced
+    assertThat(icebergVariantType).isNotNull();
+    assertThat(VariantType$.MODULE$).isNotNull();
+  }
+
+  @Test
+  public void testSparkVariantTypeToIcebergVariantType() {
+    // Test that Spark's VariantType converts to Iceberg's VariantType
+    org.apache.spark.sql.types.DataType sparkVariantType = 
VariantType$.MODULE$;
+
+    // This would be done by SparkTypeToType converter
+    // For now, we just verify the types exist and can be referenced
+    assertThat(sparkVariantType).isNotNull();
+    assertThat(Types.VariantType.get()).isNotNull();
+  }
+
+  @Test
+  public void testVariantValueSerialization() {
+    // Test that Iceberg Variant can be serialized to bytes that are 
compatible with Spark's
+    // VariantVal
+    VariantMetadata metadata = Variants.emptyMetadata();
+    VariantValue stringValue = Variants.of("test");
+    Variant variant = Variant.of(metadata, stringValue);
+
+    // Serialize the variant
+    ByteBuffer metadataBuffer =
+        
ByteBuffer.allocate(metadata.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
+    metadata.writeTo(metadataBuffer, 0);
+
+    ByteBuffer valueBuffer =
+        
ByteBuffer.allocate(stringValue.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
+    stringValue.writeTo(valueBuffer, 0);
+
+    // Verify serialization worked - the writeTo method doesn't change buffer 
position
+    // Instead, verify that the data was written correctly by checking the 
buffer contents
+    
assertThat(metadataBuffer.array().length).isEqualTo(metadata.sizeInBytes());
+    
assertThat(valueBuffer.array().length).isEqualTo(stringValue.sizeInBytes());
+
+    // Test that we can create a VariantVal from the serialized bytes
+    byte[] metadataBytes = metadataBuffer.array();
+    byte[] valueBytes = valueBuffer.array();
+
+    // This tests the integration point - Spark VariantVal should be able to 
use these bytes
+    VariantVal sparkVariant = new VariantVal(valueBytes, metadataBytes);
+    assertThat(sparkVariant).isNotNull();

Review Comment:
   I don't think this test is really validating against Spark. There are other 
areas, like Avro variant support, where we will produce buffers using `writeTo` 
and `testPrimitiveValueSerialization` in `TestPrimitiveWrapper`. What I am 
interested in is the stated purpose of the test from the comment at the top: 
whether the bytes produces are compatible with Spark's encoding/decoding.
   
   This creates a `VariantVal` that wraps two byte buffers and checks that it 
is non-null, but that isn't exercising any Spark code paths that check that the 
variant is valid or work with the encoded bytes.



##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/data/TestSparkVariants.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.
+ */
+package org.apache.iceberg.spark.data;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.UUID;
+import org.apache.iceberg.spark.TestBase;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.variants.ShreddedObject;
+import org.apache.iceberg.variants.ValueArray;
+import org.apache.iceberg.variants.Variant;
+import org.apache.iceberg.variants.VariantMetadata;
+import org.apache.iceberg.variants.VariantValue;
+import org.apache.iceberg.variants.Variants;
+import org.apache.spark.sql.types.VariantType$;
+import org.apache.spark.unsafe.types.VariantVal;
+import org.junit.jupiter.api.Test;
+
+public class TestSparkVariants extends TestBase {
+
+  @Test
+  public void testIcebergVariantTypeToSparkVariantType() {
+    // Test that Iceberg's VariantType converts to Spark's VariantType
+    Types.VariantType icebergVariantType = Types.VariantType.get();
+
+    // This would be done by TypeToSparkType converter
+    // For now, we just verify the types exist and can be referenced
+    assertThat(icebergVariantType).isNotNull();
+    assertThat(VariantType$.MODULE$).isNotNull();
+  }
+
+  @Test
+  public void testSparkVariantTypeToIcebergVariantType() {
+    // Test that Spark's VariantType converts to Iceberg's VariantType
+    org.apache.spark.sql.types.DataType sparkVariantType = 
VariantType$.MODULE$;
+
+    // This would be done by SparkTypeToType converter
+    // For now, we just verify the types exist and can be referenced
+    assertThat(sparkVariantType).isNotNull();
+    assertThat(Types.VariantType.get()).isNotNull();
+  }
+
+  @Test
+  public void testVariantValueSerialization() {
+    // Test that Iceberg Variant can be serialized to bytes that are 
compatible with Spark's
+    // VariantVal
+    VariantMetadata metadata = Variants.emptyMetadata();
+    VariantValue stringValue = Variants.of("test");
+    Variant variant = Variant.of(metadata, stringValue);
+
+    // Serialize the variant
+    ByteBuffer metadataBuffer =
+        
ByteBuffer.allocate(metadata.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
+    metadata.writeTo(metadataBuffer, 0);
+
+    ByteBuffer valueBuffer =
+        
ByteBuffer.allocate(stringValue.sizeInBytes()).order(ByteOrder.LITTLE_ENDIAN);
+    stringValue.writeTo(valueBuffer, 0);
+
+    // Verify serialization worked - the writeTo method doesn't change buffer 
position
+    // Instead, verify that the data was written correctly by checking the 
buffer contents
+    
assertThat(metadataBuffer.array().length).isEqualTo(metadata.sizeInBytes());
+    
assertThat(valueBuffer.array().length).isEqualTo(stringValue.sizeInBytes());
+
+    // Test that we can create a VariantVal from the serialized bytes
+    byte[] metadataBytes = metadataBuffer.array();
+    byte[] valueBytes = valueBuffer.array();
+
+    // This tests the integration point - Spark VariantVal should be able to 
use these bytes
+    VariantVal sparkVariant = new VariantVal(valueBytes, metadataBytes);
+    assertThat(sparkVariant).isNotNull();
+  }
+
+  @Test
+  public void testVariantPrimitiveRoundTrip() {
+    // Test various primitive types

Review Comment:
   There's a good list of Variants in `TestPrimitiveWrapper` that I'd recommend 
using here as a parameterized test:
   
   ```java
     private static final VariantPrimitive<?>[] PRIMITIVES =
         new VariantPrimitive[] {
           Variants.ofNull(),
           Variants.of(true),
           Variants.of(false),
           Variants.of((byte) 34),
           Variants.of((byte) -34),
           Variants.of((short) 1234),
           Variants.of((short) -1234),
           Variants.of(12345),
           Variants.of(-12345),
           Variants.of(9876543210L),
           Variants.of(-9876543210L),
           Variants.of(10.11F),
           Variants.of(-10.11F),
           Variants.of(14.3D),
           Variants.of(-14.3D),
           Variants.ofIsoDate("2024-11-07"),
           Variants.ofIsoDate("1957-11-07"),
           Variants.ofIsoTimestamptz("2024-11-07T12:33:54.123456+00:00"),
           Variants.ofIsoTimestamptz("1957-11-07T12:33:54.123456+00:00"),
           Variants.ofIsoTimestampntz("2024-11-07T12:33:54.123456"),
           Variants.ofIsoTimestampntz("1957-11-07T12:33:54.123456"),
           Variants.of(new BigDecimal("123456.7890")), // decimal4
           Variants.of(new BigDecimal("-123456.7890")), // decimal4
           Variants.of(new BigDecimal("1234567890.987654321")), // decimal8
           Variants.of(new BigDecimal("-1234567890.987654321")), // decimal8
           Variants.of(new BigDecimal("9876543210.123456789")), // decimal16
           Variants.of(new BigDecimal("-9876543210.123456789")), // decimal16
           Variants.of(ByteBuffer.wrap(new byte[] {0x0a, 0x0b, 0x0c, 0x0d})),
           Variants.of("iceberg"),
         };
   
     @ParameterizedTest
     @FieldSource("PRIMITIVES")
     public void testVariantPrimitiveRoundTrip(VariantPrimitive<?> primitive) {
       testPrimitiveVariant(primitive, primitive.get());
     }
   ```



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to