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

 ##########
 File path: fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
 ##########
 @@ -75,9 +75,27 @@ public static IntLiteral dateDiff(LiteralExpr first, 
LiteralExpr second) throws
 
     @FEFunction(name = "date_add", argTypes = { "DATETIME", "INT" }, 
returnType = "DATETIME")
     public static DateLiteral dateAdd(LiteralExpr date, LiteralExpr day) 
throws AnalysisException {
-        Date d = new Date(getTime(date));
-        d = DateUtils.addDays(d, (int) day.getLongValue());
-        return new DateLiteral(DateFormatUtils.format(d, "yyyy-MM-dd 
HH:mm:ss"), Type.DATETIME);
+        DateLiteral dateLiteral = (DateLiteral) date;
+        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
+        if (dateLiteral.getType() == 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);
+        }
+        LocalDateTime dateTime = 
+            
builder.toFormatter().parseLocalDateTime(dateLiteral.getStringValue()).plusDays((int)
 day.getLongValue());
+        if (dateLiteral.getType() == Type.DATE) {
+            return new DateLiteral(dateTime.getYear(), 
dateTime.getMonthOfYear(), dateTime.getDayOfMonth());
+        } else {
+            return new DateLiteral(dateTime.getYear(), 
dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
+                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), 
dateTime.getSecondOfMinute());
 
 Review comment:
   Why not move this logic to DateLiteral. And I think we can make formatter 
static which is thread-safe.

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