This is an automated email from the ASF dual-hosted git repository.

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 900b7349e5 cleanup: equals, hashCode and copy defects in the core row 
helpers,fi… (#9293)
900b7349e5 is described below

commit 900b7349e52f038524a448bf770dd4b0f211d47c
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 6 20:11:23 2026 +0100

    cleanup: equals, hashCode and copy defects in the core row helpers,fi… 
(#9293)
    
    * cleanup: equals, hashCode and copy defects in the core row helpers,fixes  
#9292
    
    * fix remark
---
 .../java/org/apache/hop/core/RowMetaAndData.java   |   6 +
 .../java/org/apache/hop/core/row/RowBuffer.java    |   4 +-
 .../java/org/apache/hop/core/row/RowDataUtil.java  |  10 +-
 .../main/java/org/apache/hop/core/row/RowMeta.java |  10 ++
 .../org/apache/hop/core/row/RowMetaBuilder.java    |   4 +-
 .../apache/hop/core/row/value/ValueMetaBase.java   |  58 ++++-----
 .../hop/core/row/RowHelpersContractTest.java       | 143 +++++++++++++++++++++
 .../java/org/apache/hop/core/row/RowMetaTest.java  |   3 +
 8 files changed, 199 insertions(+), 39 deletions(-)

diff --git a/core/src/main/java/org/apache/hop/core/RowMetaAndData.java 
b/core/src/main/java/org/apache/hop/core/RowMetaAndData.java
index f39b8c89a4..fb0226f3f5 100644
--- a/core/src/main/java/org/apache/hop/core/RowMetaAndData.java
+++ b/core/src/main/java/org/apache/hop/core/RowMetaAndData.java
@@ -112,6 +112,12 @@ public class RowMetaAndData implements Cloneable {
 
   @Override
   public boolean equals(Object obj) {
+    if (this == obj) {
+      return true;
+    }
+    if (!(obj instanceof RowMetaAndData)) {
+      return false;
+    }
     try {
       return rowMeta.compare(data, ((RowMetaAndData) obj).getData()) == 0;
     } catch (HopValueException e) {
diff --git a/core/src/main/java/org/apache/hop/core/row/RowBuffer.java 
b/core/src/main/java/org/apache/hop/core/row/RowBuffer.java
index 9f63324767..5b71050413 100644
--- a/core/src/main/java/org/apache/hop/core/row/RowBuffer.java
+++ b/core/src/main/java/org/apache/hop/core/row/RowBuffer.java
@@ -96,7 +96,9 @@ public class RowBuffer {
 
   @Override
   public int hashCode() {
-    return Objects.hash(rowMeta, buffer);
+    // equals() compares the rows value by value, so the arrays themselves 
(hashed by identity)
+    // must not take part here.
+    return Objects.hash(rowMeta, buffer.size());
   }
 
   @Override
diff --git a/core/src/main/java/org/apache/hop/core/row/RowDataUtil.java 
b/core/src/main/java/org/apache/hop/core/row/RowDataUtil.java
index e129c61943..a988831a28 100644
--- a/core/src/main/java/org/apache/hop/core/row/RowDataUtil.java
+++ b/core/src/main/java/org/apache/hop/core/row/RowDataUtil.java
@@ -34,7 +34,7 @@ package org.apache.hop.core.row;
  * desired number of fields.
  */
 public class RowDataUtil {
-  public static int OVER_ALLOCATE_SIZE = 10;
+  public static final int OVER_ALLOCATE_SIZE = 10;
 
   /**
    * Allocate a new Object array. However, over allocate by a constant factor 
to make adding values
@@ -77,6 +77,9 @@ public class RowDataUtil {
    * @return A new object array, resized.
    */
   public static Object[] createResizedCopy(Object[] objects, int newSize) {
+    if (objects == null) {
+      return allocateRowData(newSize);
+    }
 
     Object[] newObjects;
     if (objects.length < newSize) {
@@ -84,10 +87,7 @@ public class RowDataUtil {
     } else {
       newObjects = new Object[objects.length];
     }
-
-    if (objects != null) {
-      System.arraycopy(objects, 0, newObjects, 0, objects.length);
-    }
+    System.arraycopy(objects, 0, newObjects, 0, objects.length);
     return newObjects;
   }
 
diff --git a/core/src/main/java/org/apache/hop/core/row/RowMeta.java 
b/core/src/main/java/org/apache/hop/core/row/RowMeta.java
index b970586378..fcd737acbb 100644
--- a/core/src/main/java/org/apache/hop/core/row/RowMeta.java
+++ b/core/src/main/java/org/apache/hop/core/row/RowMeta.java
@@ -116,6 +116,9 @@ public class RowMeta implements IRowMeta {
     if (this == o) return true;
     if (o == null || getClass() != o.getClass()) return false;
     RowMeta rowMeta = (RowMeta) o;
+    if (valueMetaList.size() != rowMeta.size()) {
+      return false;
+    }
     for (int i = 0; i < valueMetaList.size(); i++) {
       IValueMeta thisValue = valueMetaList.get(i);
       IValueMeta thatValue = rowMeta.getValueMeta(i);
@@ -1224,6 +1227,13 @@ public class RowMeta implements IRowMeta {
    */
   @Override
   public int hashCode(Object[] rowData) throws HopValueException {
+    // Only the values this row metadata describes: a row is usually longer 
than its metadata
+    // (RowDataUtil.OVER_ALLOCATE_SIZE), and that padding must not change the 
hash. For a row of
+    // exactly size() values the result is the same as 
Arrays.deepHashCode(rowData).
+    int size = size();
+    if (rowData != null && rowData.length > size) {
+      return Arrays.deepHashCode(Arrays.copyOf(rowData, size));
+    }
     return Arrays.deepHashCode(rowData);
   }
 
diff --git a/core/src/main/java/org/apache/hop/core/row/RowMetaBuilder.java 
b/core/src/main/java/org/apache/hop/core/row/RowMetaBuilder.java
index 14d91eea37..7b115e46b7 100644
--- a/core/src/main/java/org/apache/hop/core/row/RowMetaBuilder.java
+++ b/core/src/main/java/org/apache/hop/core/row/RowMetaBuilder.java
@@ -55,12 +55,12 @@ public class RowMetaBuilder {
   }
 
   public RowMetaBuilder addNumber(String name, int length, int precision) {
-    rowMeta.addValueMeta(new ValueMetaNumber(name, length, -1));
+    rowMeta.addValueMeta(new ValueMetaNumber(name, length, precision));
     return this;
   }
 
   public RowMetaBuilder addBigNumber(String name, int length, int precision) {
-    rowMeta.addValueMeta(new ValueMetaBigNumber(name, length, -1));
+    rowMeta.addValueMeta(new ValueMetaBigNumber(name, length, precision));
     return this;
   }
 
diff --git 
a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java 
b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
index 546a53c6dc..d1ef71b518 100644
--- a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
+++ b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
@@ -514,37 +514,33 @@ public class ValueMetaBase implements IValueMeta {
 
   @Override
   public int hashCode() {
-    int result =
-        Objects.hash(
-            name,
-            length,
-            precision,
-            type,
-            trimType,
-            roundingType,
-            storageType,
-            origin,
-            comments,
-            conversionMask,
-            stringEncoding,
-            decimalSymbol,
-            groupingSymbol,
-            currencySymbol,
-            collatorStrength,
-            caseInsensitive,
-            collatorDisabled,
-            sortedDescending,
-            outputPaddingEnabled,
-            largeTextField,
-            collatorLocale,
-            collator,
-            dateFormatLocale,
-            dateFormatTimeZone,
-            dateFormatLenient,
-            lenientStringToNumber,
-            emptyStringAndNullAreDifferent);
-    result = 31 * result + Arrays.hashCode(index);
-    return result;
+    // Only the values equals() compares, or equal value metadata would hash 
differently.
+    // Use the getters: getPrecision() is fixed for Integer, Binary, String 
and Boolean.
+    return Objects.hash(
+        name,
+        getLength(),
+        getPrecision(),
+        type,
+        trimType,
+        roundingType,
+        storageType,
+        conversionMask,
+        stringEncoding,
+        decimalSymbol,
+        groupingSymbol,
+        currencySymbol,
+        collatorStrength,
+        caseInsensitive,
+        collatorDisabled,
+        sortedDescending,
+        outputPaddingEnabled,
+        largeTextField,
+        collatorLocale,
+        dateFormatLocale,
+        dateFormatTimeZone,
+        dateFormatLenient,
+        lenientStringToNumber,
+        emptyStringAndNullAreDifferent);
   }
 
   public static final String[] SINGLE_BYTE_ENCODINGS =
diff --git 
a/core/src/test/java/org/apache/hop/core/row/RowHelpersContractTest.java 
b/core/src/test/java/org/apache/hop/core/row/RowHelpersContractTest.java
new file mode 100644
index 0000000000..2205e603a8
--- /dev/null
+++ b/core/src/test/java/org/apache/hop/core/row/RowHelpersContractTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.hop.core.row;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.hop.core.HopClientEnvironment;
+import org.apache.hop.core.RowMetaAndData;
+import org.apache.hop.core.row.value.ValueMetaInteger;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.junit.rules.RestoreHopEnvironmentExtension;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/** The equals/hashCode contracts and the copy semantics of the row helpers. */
+@ExtendWith(RestoreHopEnvironmentExtension.class)
+class RowHelpersContractTest {
+
+  @BeforeAll
+  static void init() throws Exception {
+    HopClientEnvironment.init();
+  }
+
+  private static IRowMeta rowMeta(String... names) {
+    IRowMeta rowMeta = new RowMeta();
+    for (String name : names) {
+      rowMeta.addValueMeta(new ValueMetaString(name));
+    }
+    return rowMeta;
+  }
+
+  @Test
+  void rowMetaEqualsIsSymmetricForAPrefix() {
+    IRowMeta shorter = rowMeta("a");
+    IRowMeta longer = rowMeta("a", "b");
+
+    assertNotEquals(shorter, longer);
+    assertNotEquals(longer, shorter);
+    assertEquals(rowMeta("a", "b"), longer);
+  }
+
+  @Test
+  void rowHashIgnoresPadding() throws Exception {
+    IRowMeta rowMeta = rowMeta("a", "b");
+    Object[] exact = new Object[] {"x", "y"};
+    Object[] padded = RowDataUtil.allocateRowData(rowMeta.size());
+    padded[0] = "x";
+    padded[1] = "y";
+
+    assertEquals(rowMeta.hashCode(exact), rowMeta.hashCode(padded));
+    // Exact-size rows keep the value they always had: Checksum and 
Combination lookup store it.
+    assertEquals(Arrays.deepHashCode(exact), rowMeta.hashCode(exact));
+  }
+
+  @Test
+  void rowMetaBuilderKeepsPrecision() {
+    IRowMeta rowMeta = new RowMetaBuilder().addNumber("n", 9, 
2).addBigNumber("b", 20, 4).build();
+
+    assertEquals(2, rowMeta.getValueMeta(0).getPrecision());
+    assertEquals(4, rowMeta.getValueMeta(1).getPrecision());
+  }
+
+  @Test
+  void createResizedCopyOfNullIsAnEmptyRow() {
+    Object[] row = RowDataUtil.createResizedCopy((Object[]) null, 3);
+
+    assertTrue(row.length >= 3);
+    assertTrue(Arrays.stream(row).allMatch(v -> v == null));
+  }
+
+  @Test
+  void equalRowBuffersHaveEqualHashCodes() {
+    IRowMeta rowMeta = new RowMeta();
+    rowMeta.addValueMeta(new ValueMetaInteger("id"));
+    List<Object[]> rows1 = new ArrayList<>();
+    rows1.add(new Object[] {1L});
+    List<Object[]> rows2 = new ArrayList<>();
+    rows2.add(new Object[] {1L});
+
+    RowBuffer buffer1 = new RowBuffer(rowMeta, rows1);
+    RowBuffer buffer2 = new RowBuffer(rowMeta, rows2);
+
+    assertEquals(buffer1, buffer2);
+    assertEquals(buffer1.hashCode(), buffer2.hashCode());
+  }
+
+  @Test
+  void rowMetaAndDataIsNotEqualToNullOrAnotherType() {
+    RowMetaAndData row = new RowMetaAndData(rowMeta("a"), "x");
+
+    assertFalse(row.equals(null));
+    assertFalse(row.equals("x"));
+    assertTrue(row.equals(row));
+  }
+
+  @Test
+  void equalValueMetasHaveEqualHashCodes() {
+    ValueMetaString one = new ValueMetaString("name");
+    one.setOrigin("Transform 1");
+    one.setComments("first");
+    ValueMetaString two = new ValueMetaString("name");
+    two.setOrigin("Transform 2");
+    two.setComments("second");
+
+    assertEquals(one, two);
+    assertEquals(one.hashCode(), two.hashCode());
+
+    // getPrecision() is fixed for these types, so the stored precision must 
not change the hash.
+    ValueMetaInteger plain = new ValueMetaInteger("id");
+    ValueMetaInteger withPrecision = new ValueMetaInteger("id");
+    withPrecision.setPrecision(0);
+    assertEquals(plain, withPrecision);
+    assertEquals(plain.hashCode(), withPrecision.hashCode());
+
+    ValueMetaString text = new ValueMetaString("text");
+    ValueMetaString textWithPrecision = new ValueMetaString("text");
+    textWithPrecision.setPrecision(3);
+    assertEquals(text, textWithPrecision);
+    assertEquals(text.hashCode(), textWithPrecision.hashCode());
+  }
+}
diff --git a/core/src/test/java/org/apache/hop/core/row/RowMetaTest.java 
b/core/src/test/java/org/apache/hop/core/row/RowMetaTest.java
index fa7376e3c7..30a4ed6126 100644
--- a/core/src/test/java/org/apache/hop/core/row/RowMetaTest.java
+++ b/core/src/test/java/org/apache/hop/core/row/RowMetaTest.java
@@ -39,6 +39,7 @@ import org.apache.hop.core.HopClientEnvironment;
 import org.apache.hop.core.exception.HopPluginException;
 import org.apache.hop.core.exception.HopValueException;
 import org.apache.hop.core.plugins.PluginRegistry;
+import org.apache.hop.core.row.value.ValueMetaBinary;
 import org.apache.hop.core.row.value.ValueMetaDate;
 import org.apache.hop.core.row.value.ValueMetaFactory;
 import org.apache.hop.core.row.value.ValueMetaInteger;
@@ -500,6 +501,8 @@ class RowMetaTest {
   @Test
   void testHashCode() {
     rowMeta.clear();
+    // the hash covers the values the row metadata describes
+    rowMeta.addValueMeta(new ValueMetaBinary("bin"));
     byte[] byteArray = new byte[] {49, 50, 51};
     Object[] objArray = new Object[] {byteArray};
     try {

Reply via email to