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


##########
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 updated the test to validate the values in Iceberg variant and spark 
variant now.



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