imay commented on a change in pull request #1989: Modify fixed partition feature
URL: https://github.com/apache/incubator-doris/pull/1989#discussion_r335264404
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/catalog/RangePartitionInfo.java
 ##########
 @@ -95,84 +92,81 @@ public void addPartition(long partitionId, 
Range<PartitionKey> range, DataProper
 
     public Range<PartitionKey> checkAndCreateRange(SingleRangePartitionDesc 
desc) throws DdlException {
         Range<PartitionKey> newRange = null;
+        PartitionKeyDesc partitionKeyDesc = desc.getPartitionKeyDesc();
         // check range
         try {
-            // create single value partition key
-            PartitionKeyDesc partKeyDesc = desc.getPartitionKeyDesc();
-            PartitionKey singlePartitionKey  = null;
-            if (partKeyDesc.isMax()) {
-                singlePartitionKey = 
PartitionKey.createInfinityPartitionKey(partitionColumns, true);
-            } else {
-                singlePartitionKey = 
PartitionKey.createPartitionKey(partKeyDesc.getUpperValues(), partitionColumns);
-            }
+            newRange = createAndCheckNewRange(partitionKeyDesc);
+        } catch (AnalysisException e) {
+            throw new DdlException("Invalid range value format: " + 
e.getMessage());
+        }
 
-            if (singlePartitionKey.isMinValue()) {
-                throw new DdlException("Partition value should not be MIN 
VALUE: " + singlePartitionKey.toSql());
-            }
+        Preconditions.checkNotNull(newRange);
+        return newRange;
+    }
 
-            List<Map.Entry<Long, Range<PartitionKey>>> entries =
-                    new ArrayList<Map.Entry<Long, 
Range<PartitionKey>>>(this.idToRange.entrySet());
-            Collections.sort(entries, RANGE_MAP_ENTRY_COMPARATOR);
-
-            Range<PartitionKey> lastRange = null;
-            Range<PartitionKey> nextRange = null;
-            for (Map.Entry<Long, Range<PartitionKey>> entry : entries) {
-                nextRange = entry.getValue();
-
-                // check if equals to upper bound
-                PartitionKey upperKey = nextRange.upperEndpoint();
-                if (upperKey.compareTo(singlePartitionKey) >= 0) {
-                    PartitionKey lowKey = null;
-                    if (partKeyDesc.hasLowerValues()) {
-                        lowKey = 
PartitionKey.createPartitionKey(partKeyDesc.getLowerValues(), partitionColumns);
-                    } else {
-                        if (lastRange == null) {
-                            lowKey = 
PartitionKey.createInfinityPartitionKey(partitionColumns, false);
-                        } else {
-                            lowKey = lastRange.upperEndpoint();
-                        }
-                    }
-                    // check: [left, right), error if left equal right
-                    if (lowKey.compareTo(singlePartitionKey) >= 0) {
-                        throw new IllegalArgumentException("The right value 
must be more than the left value");
-                    }
-                    newRange = Range.closedOpen(lowKey, singlePartitionKey);
-
-                    // check if range intersected
-                    checkRangeIntersect(newRange, nextRange);
-                    break;
-                }
+    // create a new range and check it.
+    private Range<PartitionKey> createAndCheckNewRange(PartitionKeyDesc 
partKeyDesc)
+            throws AnalysisException, DdlException {
+        Range<PartitionKey> newRange = null;
+        // generate and sort the existing ranges
+        List<Map.Entry<Long, Range<PartitionKey>>> sortedRanges = new 
ArrayList<Map.Entry<Long, Range<PartitionKey>>>(
+                this.idToRange.entrySet());
+        Collections.sort(sortedRanges, RANGE_MAP_ENTRY_COMPARATOR);
+
+        // create upper values for new range
+        PartitionKey newRangeUpper = null;
+        if (partKeyDesc.isMax()) {
+            newRangeUpper = 
PartitionKey.createInfinityPartitionKey(partitionColumns, true);
+        } else {
+            newRangeUpper = 
PartitionKey.createPartitionKey(partKeyDesc.getUpperValues(), partitionColumns);
+        }
+        if (newRangeUpper.isMinValue()) {
+            throw new DdlException("Partition's upper value should not be MIN 
VALUE: " + partKeyDesc.toSql());
+        }
+
+        Range<PartitionKey> lastRange = null;
+        Range<PartitionKey> nextRange = null;
+        for (Map.Entry<Long, Range<PartitionKey>> entry : sortedRanges) {
+            nextRange = entry.getValue();
+            // check if equals to upper bound
+            PartitionKey upperKey = nextRange.upperEndpoint();
+            if (upperKey.compareTo(newRangeUpper) >= 0) {
+                newRange = checkNewRange(partKeyDesc, newRangeUpper, 
lastRange, nextRange);
+                break;
+            } else {
                 lastRange = nextRange;
-            } // end for ranges
-
-            if (newRange == null) {
-                PartitionKey lowKey = null;
-                if (partKeyDesc.hasLowerValues()) {
-                    lowKey = 
PartitionKey.createPartitionKey(partKeyDesc.getLowerValues(), partitionColumns);
-                } else {
-                    if (lastRange == null) {
-                        // add first partition to this table. so the lower key 
is MIN
-                        lowKey = 
PartitionKey.createInfinityPartitionKey(partitionColumns, false);
-                    } else {
-                        lowKey = lastRange.upperEndpoint();
-                    }
-                }
-                // check: [left, right), error if left equal right
-                if (lowKey.compareTo(singlePartitionKey) >= 0) {
-                    throw new IllegalArgumentException("The right value must 
be more than the left value");
-                }
-                newRange = Range.closedOpen(lowKey, singlePartitionKey);
+            }
+        } // end for ranges
 
-                // check if range intersected. The first Partition if 
lastRange == null
-                if (lastRange != null) {
-                    checkRangeIntersect(newRange, lastRange);
-                }
+        if (newRange == null) /* the new range's upper value is larger than 
any existing ranges */ {
+            newRange = checkNewRange(partKeyDesc, newRangeUpper, lastRange, 
nextRange);
+        }
+        return newRange;
+    }
+
+    private Range<PartitionKey> checkNewRange(PartitionKeyDesc partKeyDesc, 
PartitionKey newRangeUpper,
+            Range<PartitionKey> lastRange, Range<PartitionKey> nextRange) 
throws AnalysisException, DdlException {
+        Range<PartitionKey> newRange;
+        PartitionKey lowKey = null;
+        if (partKeyDesc.hasLowerValues()) {
+            lowKey = 
PartitionKey.createPartitionKey(partKeyDesc.getLowerValues(), partitionColumns);
+        } else {
+            if (lastRange == null) {
+                lowKey = 
PartitionKey.createInfinityPartitionKey(partitionColumns, false);
+            } else {
+                lowKey = lastRange.upperEndpoint();
             }
-        } catch (AnalysisException e) {
-            throw new DdlException("Invalid range value format: " + 
e.getMessage());
         }
+        // check: [left, right), error if left equal right
+        if (lowKey.compareTo(newRangeUpper) >= 0) {
 
 Review comment:
   why not check lowKey with lastRange's upper key?

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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to