imay commented on a change in pull request #2725: Add 
timestampadd/timestampdiff function
URL: https://github.com/apache/incubator-doris/pull/2725#discussion_r365807854
 
 

 ##########
 File path: 
fe/src/main/java/org/apache/doris/analysis/TimestampArithmeticExpr.java
 ##########
 @@ -120,69 +120,141 @@ private Type fixType() {
     @Override
     public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
         // Check if name of function call is date_sub or date_add.
+        String funcOpName;
         if (funcName != null) {
-            if (funcName.toUpperCase().equals("DATE_ADD")
-                    || funcName.toUpperCase().equals("DAYS_ADD")
-                    || funcName.toUpperCase().equals("ADDDATE")) {
-                op = ArithmeticExpr.Operator.ADD;
-            } else if (funcName.toUpperCase().equals("DATE_SUB")
-                    || funcName.toUpperCase().equals("DAYS_SUB")
-                    || funcName.toUpperCase().equals("SUBDATE")) {
-                op = ArithmeticExpr.Operator.SUBTRACT;
+            if (funcName.equalsIgnoreCase("TIMESTAMPDIFF")) {
+                timeUnit = TIME_UNITS_MAP.get(timeUnitIdent.toUpperCase());
+                if (timeUnit == null) {
+                    throw new AnalysisException("Invalid time unit '" + 
timeUnitIdent
+                            + "' in timestamp arithmetic expression '" + 
toSql() + "'.");
+                }
+                Type dateType = fixType();
+                if (dateType.isDate() && timeUnit.isDateTime()) {
+                    dateType = Type.DATETIME;
+                }
+                // The first child must return a timestamp or null.
+                if (!getChild(0).getType().isDateType() && 
!getChild(0).getType().isNull()) {
+                    if (!dateType.isValid()) {
+                        throw new AnalysisException("Operand '" + 
getChild(0).toSql()
+                                + "' of timestamp arithmetic expression '" + 
toSql() + "' returns type '"
+                                + getChild(0).getType() + "'. Expected type 
'TIMESTAMP/DATE/DATETIME'.");
+                    }
+                    castChild(dateType, 0);
+                }
 
 Review comment:
   seems that these codes are the same with others.
   Could you try to reduce these same code?

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