zddr commented on code in PR #31632:
URL: https://github.com/apache/doris/pull/31632#discussion_r1512390032


##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVUtil.java:
##########
@@ -66,4 +83,115 @@ public static boolean mtmvContainsExternalTable(MTMV mtmv) {
         }
         return false;
     }
+
+    /**
+     * Obtain the minimum second from `syncLimit` `timeUnit` ago
+     *
+     * @param timeUnit
+     * @param syncLimit
+     * @return
+     * @throws AnalysisException
+     */
+    public static long getNowTruncSubSec(MTMVPartitionSyncTimeUnit timeUnit, 
int syncLimit)
+            throws AnalysisException {
+        if (syncLimit < 1) {
+            throw new AnalysisException("Unexpected syncLimit, syncLimit: " + 
syncLimit);
+        }
+        // get current time
+        Expression now = DateTimeAcquire.now();
+        if (!(now instanceof DateTimeLiteral)) {
+            throw new AnalysisException("Obtaining current time does not meet 
expectations, now: " + now);
+        }
+        DateTimeLiteral nowLiteral = (DateTimeLiteral) now;
+        // date trunc
+        now = DateTimeExtractAndTransform
+                .dateTrunc(nowLiteral, new VarcharLiteral(timeUnit.name()));
+        if (!(now instanceof DateTimeLiteral)) {
+            throw new AnalysisException("date trunc not meet expectations, 
nowTrunc: " + now);
+        }
+        nowLiteral = (DateTimeLiteral) now;
+        // date sub
+        if (syncLimit > 1) {
+            nowLiteral = dateSub(nowLiteral, timeUnit, syncLimit - 1);
+        }
+        return ((IntegerLiteral) 
DateTimeExtractAndTransform.unixTimestamp(nowLiteral)).getValue();
+    }
+
+    private static DateTimeLiteral dateSub(
+            org.apache.doris.nereids.trees.expressions.literal.DateLiteral 
date, MTMVPartitionSyncTimeUnit timeUnit,
+            int num)
+            throws AnalysisException {
+        IntegerLiteral integerLiteral = new IntegerLiteral(num);
+        Expression result;
+        switch (timeUnit) {
+            case DAY:
+                result = DateTimeArithmetic.dateSub(date, integerLiteral);
+                break;
+            case YEAR:
+                result = DateTimeArithmetic.yearsSub(date, integerLiteral);
+                break;
+            case MONTH:
+                result = DateTimeArithmetic.monthsSub(date, integerLiteral);
+                break;
+            default:
+                throw new AnalysisException("not support timeUnit: " + 
timeUnit.name());
+        }
+        if (!(result instanceof DateTimeLiteral)) {
+            throw new AnalysisException("date sub not meet expectations, 
result: " + result);
+        }
+        return (DateTimeLiteral) result;
+    }
+
+    /**
+     * Convert LiteralExpr to second
+     *
+     * @param expr
+     * @param dateFormatOptional
+     * @return
+     * @throws AnalysisException
+     */
+    public static long getExprTimeSec(LiteralExpr expr, Optional<String> 
dateFormatOptional) throws AnalysisException {
+        if (expr instanceof MaxLiteral) {
+            return Long.MAX_VALUE;
+        }
+        if (expr instanceof NullLiteral) {
+            return Long.MIN_VALUE;
+        }
+        if (expr instanceof DateLiteral) {
+            return ((DateLiteral) expr).unixTimestamp(TimeUtils.getTimeZone()) 
/ 1000;
+        }
+        if (!dateFormatOptional.isPresent()) {
+            throw new AnalysisException("expr is not DateLiteral and 
DateFormat is not present.");
+        }
+        String dateFormat = dateFormatOptional.get();
+        Expression strToDate = DateTimeExtractAndTransform
+                .strToDate(new VarcharLiteral(expr.getStringValue()), new 
VarcharLiteral(dateFormat));

Review Comment:
   This is not a conversion between old and new optimizers, but rather 
encapsulating string values as VarcharLiteral



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to