RussellSpitzer commented on code in PR #18209:
URL: https://github.com/apache/iceberg/pull/18209#discussion_r4208519877


##########
spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/TestChangelogBinaryValues.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.IntFunction;
+import java.util.stream.Stream;
+import org.apache.iceberg.ChangelogOperation;
+import org.apache.iceberg.MetadataColumns;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.RowFactory;
+import org.apache.spark.sql.catalyst.CatalystTypeConverters;
+import org.apache.spark.sql.types.DataType;
+import org.apache.spark.sql.types.DataTypes;
+import org.apache.spark.sql.types.StructType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class TestChangelogBinaryValues {
+  private static final String DELETE = ChangelogOperation.DELETE.name();
+  private static final String INSERT = ChangelogOperation.INSERT.name();
+
+  static Stream<Arguments> values() {
+    DataType binary = DataTypes.BinaryType;
+    DataType array = DataTypes.createArrayType(binary);
+    StructType struct = new StructType().add("patch", binary).add("number", 
DataTypes.IntegerType);
+    return Stream.of(
+        Arguments.of(binary, (IntFunction<Object>) value -> new byte[] {(byte) 
value}),
+        Arguments.of(
+            array, (IntFunction<Object>) value -> Arrays.asList(new byte[] 
{(byte) value}, null)),
+        Arguments.of(
+            DataTypes.createArrayType(array),
+            (IntFunction<Object>) value -> List.of(List.of(new byte[] {(byte) 
value}))),
+        Arguments.of(
+            struct, (IntFunction<Object>) value -> RowFactory.create(new 
byte[] {(byte) value}, 7)),
+        Arguments.of(
+            DataTypes.createArrayType(struct),
+            (IntFunction<Object>)
+                value -> List.of(RowFactory.create(new byte[] {(byte) value}, 
7))),
+        // the only struct shape Row.equals gets wrong: array fields use 
Scala's Seq equality,
+        // which compares byte[] elements by reference
+        Arguments.of(
+            new StructType().add("patches", array),
+            (IntFunction<Object>) value -> RowFactory.create(List.of(new 
byte[] {(byte) value}))),
+        Arguments.of(
+            DataTypes.createArrayType(DataTypes.IntegerType),
+            (IntFunction<Object>) value -> Arrays.asList(value, null)));
+  }
+
+  @ParameterizedTest(name = "{0}")
+  @MethodSource("values")
+  public void testRemoveNetChangesWithEqualValues(DataType type, 
IntFunction<Object> values) {
+    Row insert = row(type, values.apply(1), INSERT, 0);
+    Row delete = row(type, values.apply(1), DELETE, 1);
+    Row latest = row(type, values.apply(2), INSERT, 1);
+    Iterator<Row> result =
+        ChangelogIterator.removeNetCarryovers(
+            List.of(insert, delete, latest).iterator(), schema(type));
+    assertThat(result).toIterable().containsExactly(latest);
+  }
+
+  @ParameterizedTest(name = "{0}")
+  @MethodSource("values")
+  public void testRemoveCarryoversWithEqualValues(DataType type, 
IntFunction<Object> values) {
+    assertRemoved(
+        type,
+        List.of(row(type, values.apply(1), DELETE, 0), row(type, 
values.apply(1), INSERT, 0)));
+  }
+
+  @ParameterizedTest(name = "{0}")
+  @MethodSource("values")
+  public void testRetainChangesWithDifferentValues(DataType type, 
IntFunction<Object> values) {
+    assertRetained(
+        type,
+        List.of(row(type, values.apply(1), DELETE, 0), row(type, 
values.apply(2), INSERT, 0)));
+  }
+
+  @ParameterizedTest(name = "{0}")
+  @MethodSource("values")
+  public void testDistinguishNullFromNonNull(DataType type, 
IntFunction<Object> values) {
+    assertRetained(
+        type, List.of(row(type, null, DELETE, 0), row(type, values.apply(1), 
INSERT, 0)));
+    assertRetained(
+        type, List.of(row(type, values.apply(1), DELETE, 0), row(type, null, 
INSERT, 0)));
+    assertRemoved(type, List.of(row(type, null, DELETE, 0), row(type, null, 
INSERT, 0)));
+  }
+
+  @Test
+  public void testRetainChangesWithDifferentArrayLengths() {
+    DataType type = DataTypes.createArrayType(DataTypes.BinaryType);

Review Comment:
   nit: I'd probably extract this to class final constant B
   
   ```java
   private static final DataType BINARY_ARRAY = 
DataTypes.createArrayType(DataTypes.BinaryType);
   
   ```



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