atognolag commented on code in PR #38406:
URL: https://github.com/apache/beam/pull/38406#discussion_r3203011770


##########
sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/IcebergRowSorterTest.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.beam.sdk.io.iceberg;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.Row;
+import org.apache.iceberg.NullOrder;
+import org.apache.iceberg.SortOrder;
+import org.apache.iceberg.types.Types;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class IcebergRowSorterTest {
+
+  private static final Schema BEAM_SCHEMA =
+      Schema.builder()
+          .addInt32Field("id")
+          .addNullableField("name", Schema.FieldType.STRING)
+          .addNullableField("value", Schema.FieldType.DOUBLE)
+          .addNullableField("active", Schema.FieldType.BOOLEAN)
+          .build();
+
+  private static final org.apache.iceberg.Schema ICEBERG_SCHEMA =
+      new org.apache.iceberg.Schema(
+          Types.NestedField.required(1, "id", Types.IntegerType.get()),
+          Types.NestedField.optional(2, "name", Types.StringType.get()),
+          Types.NestedField.optional(3, "value", Types.DoubleType.get()),
+          Types.NestedField.optional(4, "active", Types.BooleanType.get()));
+
+  private static final Comparator<byte[]> BYTE_ARR_COMPARATOR =
+      
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.primitives.UnsignedBytes
+          .lexicographicalComparator();
+
+  @Test
+  public void testStringKeyEncodingOrder() throws Exception {
+    SortOrder sortOrder = 
SortOrder.builderFor(ICEBERG_SCHEMA).asc("name").build();
+
+    Row r1 = Row.withSchema(BEAM_SCHEMA).addValues(1, "apple", 1.5, 
true).build();
+    Row r2 = Row.withSchema(BEAM_SCHEMA).addValues(2, "banana", 2.0, 
true).build();
+    Row r3 = Row.withSchema(BEAM_SCHEMA).addValues(3, "apricot", 3.0, 
false).build();
+
+    byte[] k1 = IcebergRowSorter.encodeSortKey(r1, sortOrder, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] k2 = IcebergRowSorter.encodeSortKey(r2, sortOrder, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] k3 = IcebergRowSorter.encodeSortKey(r3, sortOrder, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k1, k2) < 0); // apple < banana
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k1, k3) < 0); // apple < apricot
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k3, k2) < 0); // apricot < banana
+  }
+
+  @Test
+  public void testStringCollisionProofing() throws Exception {
+    // Tests that secondary columns don't bleed into primary columns.
+    // Row 1: Primary="abc", Secondary="def"
+    // Row 2: Primary="abcdef", Secondary=null
+    // In raw byte concatenation, both could equal "abcdef\0" if delimiters or 
escaping fail.
+    SortOrder sortOrder = 
SortOrder.builderFor(ICEBERG_SCHEMA).asc("name").asc("value").build();
+
+    Row r1 = Row.withSchema(BEAM_SCHEMA).addValues(1, "abc", 1.0, 
true).build();
+    Row r2 = Row.withSchema(BEAM_SCHEMA).addValues(2, "abcdef", null, 
true).build();
+
+    byte[] k1 = IcebergRowSorter.encodeSortKey(r1, sortOrder, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] k2 = IcebergRowSorter.encodeSortKey(r2, sortOrder, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+
+    // "abc" must sort lexicographically before "abcdef"
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k1, k2) < 0);
+  }
+
+  @Test
+  public void testDescInversion() throws Exception {
+    SortOrder sortOrderAsc = 
SortOrder.builderFor(ICEBERG_SCHEMA).asc("id").build();
+    SortOrder sortOrderDesc = 
SortOrder.builderFor(ICEBERG_SCHEMA).desc("id").build();
+
+    Row r1 = Row.withSchema(BEAM_SCHEMA).addValues(10, "test", 1.5, 
true).build();
+    Row r2 = Row.withSchema(BEAM_SCHEMA).addValues(20, "test", 2.0, 
true).build();
+
+    byte[] k1Asc = IcebergRowSorter.encodeSortKey(r1, sortOrderAsc, 
ICEBERG_SCHEMA, BEAM_SCHEMA);
+    byte[] k2Asc = IcebergRowSorter.encodeSortKey(r2, sortOrderAsc, 
ICEBERG_SCHEMA, BEAM_SCHEMA);
+
+    byte[] k1Desc = IcebergRowSorter.encodeSortKey(r1, sortOrderDesc, 
ICEBERG_SCHEMA, BEAM_SCHEMA);
+    byte[] k2Desc = IcebergRowSorter.encodeSortKey(r2, sortOrderDesc, 
ICEBERG_SCHEMA, BEAM_SCHEMA);
+
+    // Ascending: 10 < 20
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k1Asc, k2Asc) < 0);
+
+    // Descending: 10 > 20 (inverted bytes)
+    assertTrue(BYTE_ARR_COMPARATOR.compare(k1Desc, k2Desc) > 0);
+  }
+
+  @Test
+  public void testNullOrderingMatrix() throws Exception {
+    Row rNonNull = Row.withSchema(BEAM_SCHEMA).addValues(1, "apple", 1.5, 
true).build();
+    Row rNull = Row.withSchema(BEAM_SCHEMA).addValues(2, null, 2.0, 
true).build();
+
+    // 1. ASC, NULLS_FIRST
+    SortOrder ascFirst =
+        SortOrder.builderFor(ICEBERG_SCHEMA).asc("name", 
NullOrder.NULLS_FIRST).build();
+    byte[] kNonNullAscFirst =
+        IcebergRowSorter.encodeSortKey(rNonNull, ascFirst, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] kNullAscFirst =
+        IcebergRowSorter.encodeSortKey(rNull, ascFirst, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    assertTrue(
+        "ASC NULLS_FIRST failed: null should sort before non-null",
+        BYTE_ARR_COMPARATOR.compare(kNullAscFirst, kNonNullAscFirst) < 0);
+
+    // 2. ASC, NULLS_LAST
+    SortOrder ascLast =
+        SortOrder.builderFor(ICEBERG_SCHEMA).asc("name", 
NullOrder.NULLS_LAST).build();
+    byte[] kNonNullAscLast =
+        IcebergRowSorter.encodeSortKey(rNonNull, ascLast, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] kNullAscLast =
+        IcebergRowSorter.encodeSortKey(rNull, ascLast, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    assertTrue(
+        "ASC NULLS_LAST failed: null should sort after non-null",
+        BYTE_ARR_COMPARATOR.compare(kNullAscLast, kNonNullAscLast) > 0);
+
+    // 3. DESC, NULLS_FIRST
+    SortOrder descFirst =
+        SortOrder.builderFor(ICEBERG_SCHEMA).desc("name", 
NullOrder.NULLS_FIRST).build();
+    byte[] kNonNullDescFirst =
+        IcebergRowSorter.encodeSortKey(rNonNull, descFirst, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] kNullDescFirst =
+        IcebergRowSorter.encodeSortKey(rNull, descFirst, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    assertTrue(
+        "DESC NULLS_FIRST failed: null should sort before non-null",
+        BYTE_ARR_COMPARATOR.compare(kNullDescFirst, kNonNullDescFirst) > 0);
+
+    // 4. DESC, NULLS_LAST
+    SortOrder descLast =
+        SortOrder.builderFor(ICEBERG_SCHEMA).desc("name", 
NullOrder.NULLS_LAST).build();
+    byte[] kNonNullDescLast =
+        IcebergRowSorter.encodeSortKey(rNonNull, descLast, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    byte[] kNullDescLast =
+        IcebergRowSorter.encodeSortKey(rNull, descLast, ICEBERG_SCHEMA, 
BEAM_SCHEMA);
+    assertTrue(
+        "DESC NULLS_LAST failed: null should sort after non-null",
+        BYTE_ARR_COMPARATOR.compare(kNullDescLast, kNonNullDescLast) < 0);
+  }

Review Comment:
   Fix adopted



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

Reply via email to