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


##########
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/data/GenericsHelpers.java:
##########
@@ -296,6 +320,134 @@ private static void assertEqualsUnsafe(Types.MapType map, 
Map<?, ?> expected, Ma
     }
   }
 
+  static void assertEquals(Variant expected, VariantVal actual) {
+    VariantMetadata actualMetadata =
+        
VariantMetadata.from(ByteBuffer.wrap(actual.getMetadata()).order(ByteOrder.LITTLE_ENDIAN));
+    VariantTestUtil.assertEqual(expected.metadata(), actualMetadata);
+
+    org.apache.spark.types.variant.Variant sparkVariant =
+        new org.apache.spark.types.variant.Variant(actual.getValue(), 
actual.getMetadata());
+    assertEquals(expected.value(), sparkVariant);
+  }
+
+  static void assertEquals(VariantValue expected, 
org.apache.spark.types.variant.Variant actual) {
+    assertThat(actual).isNotNull();
+    assertThat(expected).isNotNull();
+
+    if (expected.type() == PhysicalType.OBJECT) {
+      assertThat(actual.getType()).isEqualTo(VariantUtil.Type.OBJECT);
+      VariantObject expectedObject = expected.asObject();
+      assertThat(actual.objectSize())
+          .as("Variant object num fields should match")
+          .isEqualTo(expectedObject.numFields());
+
+      for (String fieldName : expectedObject.fieldNames()) {
+        assertEquals(expectedObject.get(fieldName), 
actual.getFieldByKey(fieldName));
+      }
+
+    } else if (expected.type() == PhysicalType.ARRAY) {
+      assertThat(actual.getType()).isEqualTo(VariantUtil.Type.ARRAY);
+      VariantArray expectedArray = expected.asArray();
+      assertThat(actual.arraySize())
+          .as("Variant array num element should match")
+          .isEqualTo(expectedArray.numElements());
+
+      for (int i = 0; i < expectedArray.numElements(); i += 1) {
+        assertEquals(expectedArray.get(i), actual.getElementAtIndex(i));
+      }
+
+    } else {
+      // Primitive type and value should match
+      VariantUtil.Type expectedType = null;
+      Object actualValue = null;
+      switch (expected.type()) {
+        case NULL:
+          expectedType = VariantUtil.Type.NULL;
+          actualValue = null;
+          break;
+        case BOOLEAN_TRUE:
+        case BOOLEAN_FALSE:
+          expectedType = VariantUtil.Type.BOOLEAN;
+          actualValue = actual.getBoolean();
+          break;
+        case INT8:
+          expectedType = VariantUtil.Type.LONG;
+          actualValue = (byte) actual.getLong();

Review Comment:
   This will discard bits from the long value, so casting the value and then 
comparing to the Iceberg value isn't sufficient to validate that the results 
match. To do this, you'd also need to check that `((long) actualValue) == 
actual.getLong()` after the cast to show that the cast didn't modify the value.



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