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

difin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new 41925d79d67 HIVE-28603 Fixing Multiple Flaky tests in module serde 
which are due to non determinism (#5527)
41925d79d67 is described below

commit 41925d79d672d1f373d85f067d915ac6d32b7a22
Author: Nikunj Agarwal <[email protected]>
AuthorDate: Tue Nov 12 09:17:59 2024 -0600

    HIVE-28603 Fixing Multiple Flaky tests in module serde which are due to non 
determinism (#5527)
    
    * HIVE-28603 Added sorting of fields in 
ObjectInspectorUtils#getDeclaredNonStaticFields to make the function 
deterministic and fix flaky tests (Nikunj Agarwal, reviewed by Dmitriy 
Fingerman)
---
 .../objectinspector/ObjectInspectorUtils.java      | 25 ++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git 
a/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
 
b/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
index d25282a6855..89ea5399fde 100644
--- 
a/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
+++ 
b/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
@@ -24,6 +24,7 @@ import java.lang.reflect.Type;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -558,6 +559,7 @@ public final class ObjectInspectorUtils {
   public static Field[] getDeclaredNonStaticFields(Class<?> c) {
     Field[] f = c.getDeclaredFields();
     ArrayList<Field> af = new ArrayList<Field>();
+    Arrays.sort(f, 
Comparator.comparingInt(ObjectInspectorUtils::getSlotValue));
     for (int i = 0; i < f.length; ++i) {
       if (!Modifier.isStatic(f[i].getModifiers())) {
         af.add(f[i]);
@@ -1652,4 +1654,27 @@ public final class ObjectInspectorUtils {
   private ObjectInspectorUtils() {
     // prevent instantiation
   }
+
+  /**
+   * Returns slot value used for ordering the fields to make it deterministic
+   * @param field : field of a given class
+   * @return
+   */
+  private static int getSlotValue(Field field) {
+    Field slotField = null;
+    boolean originalAccessible = false;
+    try {
+      slotField = Field.class.getDeclaredField("slot");
+      originalAccessible = slotField.isAccessible();
+      slotField.setAccessible(true);
+      return slotField.getInt(field);
+    } catch (NoSuchFieldException | IllegalAccessException | 
IllegalArgumentException e) {
+      LOG.error("Error getting a slot value:", e);
+      throw new RuntimeException("Error getting a slot value");
+    } finally {
+      if (slotField != null) {
+        slotField.setAccessible(originalAccessible);
+      }
+    }
+  }
 }

Reply via email to