Uploaded image for project: 'Hive'
  1. Hive
  2. HIVE-22952

Use LinkedHashMap in TestStandardObjectInspectors.java

    XMLWordPrintableJSON

Details

    Description

      The test in org.apache.hadoop.hive.serde2.objectinspector.TestStandardObjectInspectors#testStandardUnionObjectInspector can fail due to a different iteration order of HashMap. The failure is presented as follows.

      org.junit.ComparisonFailure:
      expected:<{4:{6:"six",7:"seven",8:"eight"}}>
      but was:<{4:{6:"six",8:"eight",7:"seven"}}>

      The reason is that the assertion assertEquals("{4:{6:\"six\",7:\"seven\",8:\"eight\"}}", SerDeUtils.getJSONString(union, uoi1)); compares a hard-coded string against the string representation of a JSON object, which is implemented by a HashMap. To get the string, the HashMap is iterated here at serde/src/java/org/apache/hadoop/hive/serde2/SerDeUtils.java:343
      for (Object entry : omap.entrySet())

      The specification about HashMap says that "this class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time". The documentation is here for your reference: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

       

      The fix is to use LinkedHashMap instead of HashMap. In this way, the non-deterministic behaviour is eliminated and the test will become more stable.

       

      Attachments

        Issue Links

          Activity

            People

              cpugputpu cpugputpu
              cpugputpu cpugputpu
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m