morningman commented on a change in pull request #1644: Refactor DateLiteral 
class in FE
URL: https://github.com/apache/incubator-doris/pull/1644#discussion_r315624772
 
 

 ##########
 File path: fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
 ##########
 @@ -187,18 +222,266 @@ protected Expr uncheckedCastTo(Type targetType) throws 
AnalysisException {
     @Override
     public void write(DataOutput out) throws IOException {
         super.write(out);
-        out.writeLong(date.getTime());
+        long ymd = ((year * 13 + month) << 5) | day;
+        long hms = (hour << 12) | (minute << 6) | second;
+        long packed_datetime = ((ymd << 17) | hms) << 24 + microsecond;
+        out.writeLong(packed_datetime);
     }
 
     @Override
     public void readFields(DataInput in) throws IOException {
         super.readFields(in);
-        date = new Date(in.readLong());
+        if (Catalog.getCurrentCatalogJournalVersion() >= 
FeMetaVersion.VERSION_59) {
+            long packed_time = in.readLong();
+            microsecond = (packed_time % (1L << 24));
+            long ymdhms = (packed_time >> 24);
+            long ymd = ymdhms >> 17;
+            long hms = ymdhms % (1 << 17);
+
+            day = ymd % (1 << 5);
+            long ym = ymd >> 5;
+            month = ym % 13;
+            year = ym / 13;
+            year %= 10000;
+            second = hms % (1 << 6);
+            minute = (hms >> 6) % (1 << 6);
+            hour = (hms >> 12);
+            this.type = Type.DATETIME;
+        } else {
+            Date date = new Date(in.readLong());
+            String date_str = TimeUtils.format(date, Type.DATETIME);
+            try {
+                init(date_str, Type.DATETIME);
+            } catch (AnalysisException ex) {
+                throw new IOException(ex.getMessage());
+            }
+        }
     }
 
     public static DateLiteral read(DataInput in) throws IOException {
         DateLiteral literal = new DateLiteral();
         literal.readFields(in);
         return literal;
     }
+
+    public long unixTime(TimeZone timeZone) throws ParseException {
+        SimpleDateFormat dateFormat;
+        if (type == Type.DATE) {
+            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+        } else {
+            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        }
+        dateFormat.setTimeZone(timeZone);
+        return dateFormat.parse(getStringValue()).getTime();
+    }
+
+    public static DateLiteral dateParser(String date, String pattern) throws 
AnalysisException{
+        DateLiteral dateLiteral = new DateLiteral();
+        if(HAS_TIME_PART.matcher(pattern).matches()) {
+            dateLiteral.setType(Type.DATETIME);
+        } else {
+            dateLiteral.setType(Type.DATE);
+        }
+
+        LocalDateTime dateTime = 
FormatBuilder(pattern).toFormatter().parseLocalDateTime(date);
+        dateLiteral.setYear(dateTime.getYear());
+        dateLiteral.setMonth(dateTime.getMonthOfYear());
+        dateLiteral.setDay(dateTime.getDayOfMonth());
+        dateLiteral.setHour(dateTime.getHourOfDay());
+        dateLiteral.setMinute(dateTime.getMinuteOfHour());
+        dateLiteral.setSecond(dateTime.getSecondOfMinute());
+        return dateLiteral;
+    }
+
+    public String dateFormat(String pattern) throws AnalysisException{
+        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
+        if (type == Type.DATE) {
+            builder.appendYear(4, 4).appendLiteral("-").
+                    
appendMonthOfYear(2).appendLiteral("-").appendDayOfMonth(2);
+        } else {
+            builder.appendYear(4, 4).appendLiteral("-").
+                    appendMonthOfYear(2).appendLiteral("-")
+                    .appendDayOfMonth(2).appendLiteral(" ")
+                    .appendHourOfDay(2).appendLiteral(":")
+                    .appendMinuteOfHour(2).appendLiteral(":")
+                    .appendSecondOfMinute(2);
+        }
+
+        return builder.toFormatter().parseLocalDateTime(getStringValue())
+                .toString(FormatBuilder(pattern).toFormatter());
+    }
+
+    private static DateTimeFormatterBuilder FormatBuilder(String pattern) 
throws AnalysisException{
 
 Review comment:
   ```suggestion
       private static DateTimeFormatterBuilder formatBuilder(String pattern) 
throws AnalysisException{
   ```

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