BinShi-SecularBird commented on a change in pull request #471: PHOENIX-5176 
KeyRange.compareUpperRange(KeyRang 1, KeyRang 2) returns wrong result when two 
key ranges have the same upper bound values but one is inclusive and another is 
exclusive
URL: https://github.com/apache/phoenix/pull/471#discussion_r267901019
 
 

 ##########
 File path: 
phoenix-core/src/test/java/org/apache/phoenix/query/KeyRangeMoreTest.java
 ##########
 @@ -192,64 +193,78 @@ public void testListIntersectForBoundary() throws 
Exception {
 
         
listIntersectAndAssert(Arrays.asList(KeyRange.EMPTY_RANGE),Arrays.asList(KeyRange.EVERYTHING_RANGE),Arrays.asList(KeyRange.EMPTY_RANGE));
 
-        rowKeyRanges1=Arrays.asList(
-                PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(2),
-                            true,
-                            PInteger.INSTANCE.toBytes(5),
-                            true),
-                PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(8),
-                            true,
-                            KeyRange.UNBOUND,
-                            false));
-        rowKeyRanges2=Arrays.asList(
-                PInteger.INSTANCE.getKeyRange(
-                        KeyRange.UNBOUND,
-                        false,
-                        PInteger.INSTANCE.toBytes(4),
-                        true),
-                PInteger.INSTANCE.getKeyRange(
-                        PInteger.INSTANCE.toBytes(7),
-                        true,
-                        PInteger.INSTANCE.toBytes(10),
-                        true),
-                PInteger.INSTANCE.getKeyRange(
-                    PInteger.INSTANCE.toBytes(13),
-                    true,
-                    PInteger.INSTANCE.toBytes(14),
-                    true),
-                PInteger.INSTANCE.getKeyRange(
-                    PInteger.INSTANCE.toBytes(19),
-                    true,
-                    KeyRange.UNBOUND,
-                    false)
-                );
-        expected=Arrays.asList(
-                PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(2),
-                            true,
-                            PInteger.INSTANCE.toBytes(4),
-                            true),
-                    PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(8),
-                            true,
-                            PInteger.INSTANCE.toBytes(10),
-                            true),
-                    PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(13),
-                            true,
-                            PInteger.INSTANCE.toBytes(14),
-                            true),
-                    PInteger.INSTANCE.getKeyRange(
-                            PInteger.INSTANCE.toBytes(19),
-                            true,
-                            KeyRange.UNBOUND,
-                            false)
-                );
+        rowKeyRanges1 = CreateKeyRangeList(
+                Arrays.asList(2, 5, 8, Integer.MAX_VALUE),
+                Arrays.asList(true, true, true, false));
+        rowKeyRanges2 = CreateKeyRangeList(
+                Arrays.asList(Integer.MIN_VALUE, 4, 7, 10, 13, 14, 19, 
Integer.MAX_VALUE),
+                Arrays.asList(false, true, true, true, true, true, true, 
false));
+        expected = CreateKeyRangeList(
+                Arrays.asList(2, 4, 8, 10, 13, 14, 19, Integer.MAX_VALUE),
+                Arrays.asList(true, true, true, true, true, true, true, 
false));
+        listIntersectAndAssert(rowKeyRanges1, rowKeyRanges2, expected);
+
+        rowKeyRanges1 = CreateKeyRangeList(
+                Arrays.asList(3, 5, 5, 6),
+                Arrays.asList(true, false, true, false));
+        rowKeyRanges2 = CreateKeyRangeList(
+                Arrays.asList(3, 5, 6, 7),
+                Arrays.asList(true, true, true, true));
+        expected = CreateKeyRangeList(
+                Arrays.asList(3, 5),
+                Arrays.asList(true, true));
         listIntersectAndAssert(rowKeyRanges1, rowKeyRanges2, expected);
     }
 
+    @Test
+    public void testKeyRangeCompareUpperRange() throws Exception {
+        List<KeyRange> rowKeyRanges1 = CreateKeyRangeListWithFixedLowerRange(
+                Arrays.asList(Integer.MAX_VALUE, Integer.MAX_VALUE, 10000, 
1001, 1000, 1000, 1000, 1000),
+                Arrays.asList(false, false, true, true, true, true, false, 
true));
+        List<KeyRange> rowKeyRanges2 = CreateKeyRangeListWithFixedLowerRange(
+                Arrays.asList(Integer.MAX_VALUE, 10000, Integer.MAX_VALUE, 
1000, 1001, 1000, 1000, 1000),
+                Arrays.asList(false, false, false, true, true, true, false, 
false));
+        List<Integer> expectedResults = Arrays.asList(0, 1, -1, 1, -1, 0, 0, 
1);
+        assertEquals(rowKeyRanges1.size(), rowKeyRanges2.size());
+        assertEquals(rowKeyRanges1.size(), expectedResults.size());
+
+        for (int i = 0; i < expectedResults.size(); i++) {
+            int compareResult = 
KeyRange.compareUpperRange(rowKeyRanges1.get(i), rowKeyRanges2.get(i));
+            assertEquals(expectedResults.get(i).intValue(), compareResult);
+        }
+    }
+
+    private static List<KeyRange> 
CreateKeyRangeListWithFixedLowerRange(List<Integer> keys, List<Boolean> 
boundaryConditions) {
+        assertEquals(keys.size(), boundaryConditions.size());
+        List<Integer> newKeys = Lists.newArrayListWithCapacity(keys.size() * 
2);
+        List<Boolean> newBoundaryConditions = 
Lists.newArrayListWithCapacity(boundaryConditions.size() * 2);
+
+        for (int i = 0; i < keys.size(); i++) {
+            newKeys.add(0);
+            newBoundaryConditions.add(true);
+            newKeys.add(keys.get(i));
+            newBoundaryConditions.add(boundaryConditions.get(i));
+        }
+
+        return CreateKeyRangeList(newKeys, newBoundaryConditions);
+    }
+
+    private static List<KeyRange> CreateKeyRangeList(List<Integer> keys, 
List<Boolean> boundaryConditions) {
+        assertEquals(keys.size(), boundaryConditions.size());
+        assertTrue(keys.size() % 2 == 0);
+
+        int size = keys.size() / 2;
+        List<KeyRange> keyRangeList = Lists.newArrayListWithCapacity(size);
+
+        for (int i = 0; i < size; i++) {
+            byte[] startKey = keys.get(2*i).equals(Integer.MIN_VALUE) ? 
KeyRange.UNBOUND : PInteger.INSTANCE.toBytes(keys.get(2*i));
+            byte[] endKey = keys.get(2*i + 1).equals(Integer.MAX_VALUE) ? 
KeyRange.UNBOUND : PInteger.INSTANCE.toBytes(keys.get(2*i + 1));
+            keyRangeList.add(PInteger.INSTANCE.getKeyRange(startKey, 
boundaryConditions.get(2*i), endKey, boundaryConditions.get(2*i+1)));
+        }
+
+        return keyRangeList;
+    }
+
     private static void listIntersectAndAssert(List<KeyRange> 
rowKeyRanges1,List<KeyRange> rowKeyRanges2,List<KeyRange> expected) {
         for(int i=0;i<200;i++) {
 
 Review comment:
   The purpose of the original test author just want to randomly shuffle the 
ranges in list for certain times and make sure the function can correct sort 
the given tow ranges and calculate the intersection. I don't like this test 
code because once there is failure we don't know what's the case which caused 
the failure, but I think improving this is out of scope of my change due to the 
following two reasons:
   1. Using the ranges in ascending order is efficient for testing my fix to 
the original problem.
   2. Reshuffle the ranges should be the unit test of KeyRange.coalesce(...) 
instead of KeyRange.intersect().

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to