WingsGo commented on a change in pull request #3679:
URL: https://github.com/apache/incubator-doris/pull/3679#discussion_r429885387



##########
File path: 
fe/src/main/java/org/apache/doris/common/util/DynamicPartitionUtil.java
##########
@@ -273,34 +321,110 @@ public static String getPartitionFormat(Column column) 
throws DdlException {
         }
     }
 
-    public static String getFormattedPartitionName(String name, String 
timeUnit) {
-        name = name.replace("-", "").replace(":", "").replace(" ", "");
+    public static String getFormattedPartitionName(TimeZone tz, String 
formattedDateStr, String timeUnit) {
+        formattedDateStr = formattedDateStr.replace("-", "").replace(":", 
"").replace(" ", "");
         if (timeUnit.equalsIgnoreCase(TimeUnit.DAY.toString())) {
-            return name.substring(0, 8);
+            return formattedDateStr.substring(0, 8);
         } else if (timeUnit.equalsIgnoreCase(TimeUnit.MONTH.toString())) {
-            return name.substring(0, 6);
+            return formattedDateStr.substring(0, 6);
         } else {
-            name = name.substring(0, 8);
-            Calendar calendar = Calendar.getInstance();
+            formattedDateStr = formattedDateStr.substring(0, 8);
+            Calendar calendar = Calendar.getInstance(tz);
             try {
-                calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse(name));
+                calendar.setTime(new 
SimpleDateFormat("yyyyMMdd").parse(formattedDateStr));
             } catch (ParseException e) {
                 LOG.warn("Format dynamic partition name error. Error={}", 
e.getMessage());
-                return name;
+                return formattedDateStr;
+            }
+            int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
+            if (weekOfYear <= 1 && calendar.get(Calendar.MONTH) >= 11) {
+                // eg: JDK think 2019-12-30 as the first week of year 2020, we 
need to handle this.
+                // to make it as the 53rd week of year 2019.
+                weekOfYear += 52;
             }
-            return String.format("%s_%02d", calendar.get(Calendar.YEAR), 
calendar.get(Calendar.WEEK_OF_YEAR));
+            return String.format("%s_%02d", calendar.get(Calendar.YEAR), 
weekOfYear);
         }
     }
 
-    public static String getPartitionRange(String timeUnit, int offset, 
Calendar calendar, String format) {
+    // return the partition range date string formatted as yyyy-MM-dd[ 
HH:mm::ss]
+    // TODO: support HOUR and YEAR
+    public static String getPartitionRangeString(DynamicPartitionProperty 
property, Calendar current,
+            int offset, String format) {
+        String timeUnit = property.getTimeUnit();
+        TimeZone tz = property.getTimeZone();
         if (timeUnit.equalsIgnoreCase(TimeUnit.DAY.toString())) {
-            calendar.add(Calendar.DAY_OF_MONTH, offset);
+            return getPartitionRangeOfDay(current, offset, tz, format);
         } else if (timeUnit.equalsIgnoreCase(TimeUnit.WEEK.toString())) {
-            calendar.add(Calendar.WEEK_OF_MONTH, offset);
-        } else {
-            calendar.add(Calendar.MONTH, offset);
+            return getPartitionRangeOfWeek(current, offset, 
property.getStartOfWeek(), tz, format);
+        } else { // MONTH
+            return getPartitionRangeOfMonth(current, offset, 
property.getStartOfMonth(), tz, format);
+        }
+    }
+    
+    /*
+     * return formatted string of partition range in DAY granularity.
+     * offset: The offset from the current day. 0 means current day, 1 means 
tomorrow, -1 means yesterday.
+     * format: the format of the return date string.
+     * 
+     * Eg:
+     *  Today is 2020-05-24, offset = -1
+     *  It will return 2020-05-23
+     */
+    private static String getPartitionRangeOfDay(Calendar current, int offset, 
TimeZone tz, String format) {
+        current.add(Calendar.DATE, offset);
+        return getFormattedTimeWithoutHourMinuteSecond(current, format);
+    }
+
+    /*
+     * return formatted string of partition range in WEEK granularity.
+     * offset: The offset from the current week. 0 means current week, 1 means 
next week, -1 means last week.
+     * startOf: Define the start day of each week. 1 means MONDAY, 7 means 
SUNDAY.
+     * format: the format of the return date string.
+     * 
+     * Eg:
+     *  Today is 2020-05-24, offset = -1, startOf.dayOfWeek = 3
+     *  It will return 2020-05-20  (Wednesday of last week)
+     */
+    private static String getPartitionRangeOfWeek(Calendar current, int 
offset, StartOfDate startOf, TimeZone tz,
+            String format) {
+        Preconditions.checkArgument(startOf.isStartOfWeek());
+        // 1. get the offset week
+        current.add(Calendar.WEEK_OF_YEAR, offset);
+        // 2. get the date of `startOf` week
+        int day = current.get(Calendar.DAY_OF_WEEK);
+        // SUNDAY will return 1, we will set it to 7, and make MONDAY to 1, 
and so on
+        day = day == 1 ? 7 : day - 1;
+        current.add(Calendar.DATE, (startOf.dayOfWeek - day));
+        return getFormattedTimeWithoutHourMinuteSecond(current, format);
+    }
+
+    /*
+     * return formatted string of partition range in MONTH granularity.
+     * offset: The offset from the current month. 0 means current month, 1 
means next month, -1 means last month.
+     * startOf: Define the start date of each month. 1 means start on the 1st 
of every month.
+     * format: the format of the return date string.
+     * 
+     * Eg:
+     *  Today is 2020-05-24, offset = 1, startOf.month = 3
+     *  It will return 2020-06-03 
+     */
+    private static String getPartitionRangeOfMonth(Calendar current, int 
offset, StartOfDate startOf, TimeZone tz,

Review comment:
       OK, we can change it in next PR~




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



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

Reply via email to