hello-stephen commented on code in PR #71158:
URL: https://github.com/apache/doris/pull/71158#discussion_r4737664599


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/PartitionExprUtil.java:
##########
@@ -202,50 +215,147 @@ public static Map<String, AddPartitionOp> 
getAddPartitionClauseFromPartitionValu
                             + "_" + 
Integer.toHexString(partitionName.hashCode());
                     }
                 }
+                if (partitionColumn.stream().anyMatch(column -> 
column.getType().isTimeStampTz())) {
+                    partitionName = 
existingTimestampTzListPartitionName(olapTable, partitionInfo,
+                            PartitionKey.createListPartitionKey(inValues, 
partitionColumn), partitionName);
+                }
             } else {
                 throw new AnalysisException("now only support range and list 
partition");
             }
 
-            Map<String, String> partitionProperties = Maps.newHashMap();
-            DistributionInfo defaultDistributionInfo = 
olapTable.getDefaultDistributionInfo();
-            DistributionDesc distributionDesc = 
defaultDistributionInfo.toDistributionDesc();
-            if (olapTable.isAutoBucket() && partitionType == 
PartitionType.RANGE) {
-                // Use unified auto bucket calculator
-                AutoBucketCalculator.AutoBucketContext context = new 
AutoBucketCalculator.AutoBucketContext(
-                        olapTable, partitionName, partitionName, false,
-                        defaultDistributionInfo.getBucketNum());
-
-                int bucketsNum = 
AutoBucketCalculator.calculateAutoBucketsWithBoundsCheck(context);
-
-                // Only update distribution if calculation was successful 
(bucketsNum != default)
-                if (bucketsNum != defaultDistributionInfo.getBucketNum()) {
-                    if (defaultDistributionInfo.getType() == 
DistributionInfo.DistributionInfoType.HASH) {
-                        HashDistributionInfo hashDistributionInfo = 
(HashDistributionInfo) defaultDistributionInfo;
-                        List<String> distColumnNames = new ArrayList<>();
-                        for (Column distributionColumn : 
hashDistributionInfo.getDistributionColumns()) {
-                            distColumnNames.add(distributionColumn.getName());
-                        }
-                        distributionDesc = new 
HashDistributionDesc(bucketsNum, distColumnNames);
-                    } else {
-                        distributionDesc = new 
RandomDistributionDesc(bucketsNum);
+            Map<String, PartitionKeyDesc> descriptions = Maps.newHashMap();
+            if (partitionType == PartitionType.RANGE && 
partitionColumn.get(0).getType().isTimeStampTz()
+                    && "date_trunc".equalsIgnoreCase(intervalInfo.fnName)) {
+                descriptions = timestampTzRangePartitions(olapTable, 
partitionInfo, partitionName, partitionKeyDesc);
+            } else {
+                descriptions.put(partitionName, partitionKeyDesc);
+            }
+            for (Map.Entry<String, PartitionKeyDesc> entry : 
descriptions.entrySet()) {
+                result.put(entry.getKey(), buildAddPartitionOp(
+                        olapTable, partitionType, entry.getKey(), 
entry.getValue()));
+            }
+        }
+        return result;
+    }
+
+    private static AddPartitionOp buildAddPartitionOp(OlapTable olapTable, 
PartitionType partitionType,
+            String partitionName, PartitionKeyDesc partitionKeyDesc) throws 
AnalysisException {
+        Map<String, String> partitionProperties = Maps.newHashMap();
+        DistributionInfo defaultDistributionInfo = 
olapTable.getDefaultDistributionInfo();
+        DistributionDesc distributionDesc = 
defaultDistributionInfo.toDistributionDesc();
+        if (olapTable.isAutoBucket() && partitionType == PartitionType.RANGE) {
+            // Use unified auto bucket calculator
+            AutoBucketCalculator.AutoBucketContext context = new 
AutoBucketCalculator.AutoBucketContext(
+                    olapTable, partitionName, partitionName, false,
+                    defaultDistributionInfo.getBucketNum());
+
+            int bucketsNum = 
AutoBucketCalculator.calculateAutoBucketsWithBoundsCheck(context);
+
+            // Only update distribution if calculation was successful 
(bucketsNum != default)
+            if (bucketsNum != defaultDistributionInfo.getBucketNum()) {
+                if (defaultDistributionInfo.getType() == 
DistributionInfo.DistributionInfoType.HASH) {
+                    HashDistributionInfo hashDistributionInfo = 
(HashDistributionInfo) defaultDistributionInfo;
+                    List<String> distColumnNames = new ArrayList<>();
+                    for (Column distributionColumn : 
hashDistributionInfo.getDistributionColumns()) {
+                        distColumnNames.add(distributionColumn.getName());
                     }
+                    distributionDesc = new HashDistributionDesc(bucketsNum, 
distColumnNames);
+                } else {
+                    distributionDesc = new RandomDistributionDesc(bucketsNum);
+                }
+            }
+        }
+
+        SinglePartitionDesc singleRangePartitionDesc = new 
SinglePartitionDesc(true, partitionName,
+                partitionKeyDesc, partitionProperties);
+        // iff table's storage medium is not equal default storage medium,
+        // should add storage medium in partition properties
+        if 
(!DataProperty.DEFAULT_STORAGE_MEDIUM.equals(olapTable.getStorageMedium())) {
+            partitionProperties.put(PropertyAnalyzer.PROPERTIES_STORAGE_MEDIUM,
+                    olapTable.getStorageMedium().name());
+        }
+        AddPartitionOp addPartitionClause = new AddPartitionOp(
+                singleRangePartitionDesc.translateToPartitionDefinition(),
+                distributionDesc.toDistributionDescriptor(), 
partitionProperties, false);
+        return addPartitionClause;
+    }
+
+    private static String existingTimestampTzListPartitionName(OlapTable 
table, PartitionInfo partitionInfo,
+            PartitionKey key, String canonicalName) {
+        table.readLock();
+        try {
+            if (table.checkPartitionNameExist(canonicalName)) {
+                return canonicalName;
+            }
+            // Older writers named partitions with their local offset. Reuse 
the same key even
+            // when that persisted partition's name differs from today's UTC 
spelling.
+            for (Map.Entry<Long, PartitionItem> entry : 
partitionInfo.getIdToItem(false).entrySet()) {
+                if (((ListPartitionItem) 
entry.getValue()).getItems().contains(key)) {
+                    return table.getPartition(entry.getKey()).getName();
                 }
             }
+            return canonicalName;
+        } finally {
+            table.readUnlock();
+        }
+    }
 
-            SinglePartitionDesc singleRangePartitionDesc = new 
SinglePartitionDesc(true, partitionName,
-                    partitionKeyDesc, partitionProperties);
-            // iff table's storage medium is not equal default storage medium,
-            // should add storage medium in partition properties
-            if 
(!DataProperty.DEFAULT_STORAGE_MEDIUM.equals(olapTable.getStorageMedium())) {
-                
partitionProperties.put(PropertyAnalyzer.PROPERTIES_STORAGE_MEDIUM,
-                        olapTable.getStorageMedium().name());
+    private static Map<String, PartitionKeyDesc> 
timestampTzRangePartitions(OlapTable table,
+            PartitionInfo partitionInfo, String canonicalName, 
PartitionKeyDesc requestedDesc)
+            throws AnalysisException {
+        List<Column> columns = partitionInfo.getPartitionColumns();
+        Range<PartitionKey> requested = Range.closedOpen(
+                
PartitionKey.createPartitionKey(requestedDesc.getLowerValues(), columns),
+                
PartitionKey.createPartitionKey(requestedDesc.getUpperValues(), columns));
+        RangeSet<PartitionKey> missing = TreeRangeSet.create();
+        missing.add(requested);
+        Map<String, PartitionKeyDesc> descriptions = Maps.newHashMap();
+        table.readLock();
+        try {
+            for (Map.Entry<Long, PartitionItem> entry : 
partitionInfo.getIdToItem(false).entrySet()) {
+                Range<PartitionKey> existing = ((RangePartitionItem) 
entry.getValue()).getItems();
+                if (requested.isConnected(existing) && 
!requested.intersection(existing).isEmpty()) {
+                    missing.remove(existing);

Review Comment:
   [P1] Guard split ranges against older partition-routing writers
   
   During a rolling upgrade, suppose the UTC partition `[2024-01-04 00:00Z, 
2024-01-05 00:00Z)` exists and an older Asia/Shanghai BE inserts `2024-01-04 
01:00Z`. Its `date_trunc` projects this to `2024-01-03 16:00Z`, and its 
unchanged transformed-slot routing requests the missing shifted day. This 
subtraction now creates `[2024-01-03 16:00Z, 2024-01-04 00:00Z)` and returns 
success. The old BE retries using the projected floor, selects that gap, and 
writes the original `01:00Z` row outside its persisted range. A query 
restricted to January 4 prunes the gap and misses the committed row. The 
baseline rejected this overlapping request instead (with the FE default 
timezone set to UTC). Please gate the new range/routing semantics on writer 
compatibility, or reject incompatible writers, and cover this mixed-version 
case before enabling gap creation.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to