github-actions[bot] commented on code in PR #62535:
URL: https://github.com/apache/doris/pull/62535#discussion_r3093081038
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateMvByPartitionCommand.java:
##########
@@ -224,6 +253,64 @@ private static Expression
convertRangePartitionToCompare(PartitionItem item, Slo
return predicate;
}
+ /**
+ * For partition expression like date_trunc(date_add/sub(base_col,
INTERVAL N HOUR), unit),
+ * the base-table filter should use inverse offset (target - N / + N).
+ */
+ public static Optional<Long> getInverseHourOffsetForBaseFilter(MTMV mv) {
+ Expr partitionExpr = mv.getMvPartitionInfo().getExpr();
+ if (!(partitionExpr instanceof FunctionCallExpr)) {
+ return Optional.empty();
+ }
+ FunctionCallExpr functionCallExpr = (FunctionCallExpr) partitionExpr;
+ if
(!"date_trunc".equalsIgnoreCase(functionCallExpr.getFnName().getFunction())) {
+ return Optional.empty();
+ }
+ List<Expr> params = functionCallExpr.getParams().exprs();
+ if (params.size() != 2 || !(params.get(0) instanceof
TimestampArithmeticExpr)) {
+ return Optional.empty();
+ }
+ TimestampArithmeticExpr timestampArithmeticExpr =
(TimestampArithmeticExpr) params.get(0);
+ if (timestampArithmeticExpr.getTimeUnit() !=
TimestampArithmeticExpr.TimeUnit.HOUR
+ || !(timestampArithmeticExpr.getChild(1) instanceof
LiteralExpr)) {
+ return Optional.empty();
+ }
+ long offsetHours;
+ try {
+ offsetHours = Long.parseLong(((LiteralExpr)
timestampArithmeticExpr.getChild(1)).getStringValue());
+ } catch (NumberFormatException e) {
+ return Optional.empty();
+ }
+ String funcName = timestampArithmeticExpr.getFuncName().toLowerCase();
+ if ("date_sub".equals(funcName)) {
+ offsetHours = -offsetHours;
+ } else if (!"date_add".equals(funcName)) {
+ return Optional.empty();
+ }
+ return Optional.of(-offsetHours);
+ }
+
+ private static Expression shiftDateLikeLiteralByHours(Expression
expression, long shiftHours) {
+ if (shiftHours == 0) {
+ return expression;
+ }
+ if (expression instanceof DateTimeV2Literal) {
+ return ((DateTimeV2Literal) expression).plusHours(shiftHours);
+ }
+ if (expression instanceof DateTimeLiteral) {
+ return ((DateTimeLiteral) expression).plusHours(shiftHours);
+ }
+ if (expression instanceof DateV2Literal) {
+ return ((DateV2Literal)
expression).toBeginOfTheDay().plusHours(shiftHours);
+ }
Review Comment:
Blocking: MTMVPartitionExprDateTruncDateAddSub.analyze() now allows
TIMESTAMPTZ base partition columns, but this helper only shifts Date and
DateTime literals and falls through for TimestampTzLiteral. PartitionKey to
Literal.fromLegacyLiteral() produces TimestampTzLiteral for TIMESTAMPTZ
partitions, so refresh and union-compensation still use the unshifted MV bounds
even though getInverseHourOffsetForBaseFilter() computed an inverse offset. For
an MV partitioned by date_trunc(date_add(ts_tz, INTERVAL 3 HOUR), day),
refreshing one MV partition will filter the base table on the wrong time window
and can miss rows or scan the wrong partitions. Please either shift
TimestampTzLiteral here too or reject TIMESTAMPTZ in analysis, and add a
regression for refresh or union on a TIMESTAMPTZ partition column.
##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExprDateTruncDateAddSub.java:
##########
@@ -0,0 +1,301 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.mtmv;
+
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.ExprToSqlVisitor;
+import org.apache.doris.analysis.PartitionExprUtil;
+import org.apache.doris.analysis.PartitionKeyDesc;
+import org.apache.doris.analysis.PartitionValue;
+import org.apache.doris.analysis.StringLiteral;
+import org.apache.doris.analysis.TimestampArithmeticExpr;
+import org.apache.doris.analysis.ToSqlParams;
+import org.apache.doris.catalog.PartitionType;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.util.PropertyAnalyzer;
+import org.apache.doris.datasource.mvcc.MvccUtil;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import
org.apache.doris.nereids.trees.expressions.functions.executable.DateTimeArithmetic;
+import
org.apache.doris.nereids.trees.expressions.functions.executable.DateTimeExtractAndTransform;
+import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal;
+import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal;
+import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Rollup service for expressions:
+ * date_trunc(date_add/date_sub(partition_col, interval N hour), 'unit')
+ */
+public class MTMVPartitionExprDateTruncDateAddSub implements
MTMVPartitionExprService {
+ private static final Set<String> TIME_UNITS = ImmutableSet.of(
+ "year", "quarter", "week", "month", "day", "hour");
+ private final String timeUnit;
+ private final long offsetHours;
+
+ public MTMVPartitionExprDateTruncDateAddSub(FunctionCallExpr
functionCallExpr) throws AnalysisException {
+ List<Expr> paramsExprs = functionCallExpr.getParams().exprs();
+ if (paramsExprs.size() != 2) {
+ throw new AnalysisException("date_trunc params exprs size should
be 2.");
+ }
+ Expr dateTruncUnit = paramsExprs.get(1);
+ if (!(dateTruncUnit instanceof StringLiteral)) {
+ throw new AnalysisException("date_trunc param of time unit is not
string literal.");
+ }
+ this.timeUnit = dateTruncUnit.getStringValue().toLowerCase();
+
+ Expr dateTruncArg = paramsExprs.get(0);
+ if (!(dateTruncArg instanceof TimestampArithmeticExpr)) {
+ throw new AnalysisException("date_trunc first argument should be
date_add/date_sub in mtmv partition");
+ }
+ TimestampArithmeticExpr timestampArithmeticExpr =
(TimestampArithmeticExpr) dateTruncArg;
+ if (timestampArithmeticExpr.getTimeUnit() !=
TimestampArithmeticExpr.TimeUnit.HOUR) {
+ throw new AnalysisException("only HOUR unit is supported in
date_add/date_sub for mtmv partition");
+ }
+ if (!(timestampArithmeticExpr.getChild(1) instanceof LiteralExpr)) {
+ throw new AnalysisException("date_add/date_sub offset should be a
literal");
+ }
+ long offset = parseOffsetHours((LiteralExpr)
timestampArithmeticExpr.getChild(1));
+ String functionName =
timestampArithmeticExpr.getFuncName().toLowerCase();
+ if ("date_sub".equals(functionName)) {
+ this.offsetHours = -offset;
+ } else if ("date_add".equals(functionName)) {
+ this.offsetHours = offset;
+ } else {
+ throw new AnalysisException("only date_add/date_sub is supported
in mtmv partition");
+ }
+ }
+
+ @Override
+ public String getRollUpIdentity(PartitionKeyDesc partitionKeyDesc,
Map<String, String> mvProperties)
+ throws AnalysisException {
+ String res = null;
+ Optional<String> dateFormat = getDateFormat(mvProperties);
+ List<List<PartitionValue>> inValues = partitionKeyDesc.getInValues();
+ for (int i = 0; i < inValues.size(); i++) {
+ PartitionValue partitionValue = inValues.get(i).get(0);
+ if (partitionValue.isNullPartition()) {
+ throw new AnalysisException("date_trunc + date_add/date_sub
not support null partition value");
+ }
+ String identity =
dateTruncByOffset(partitionValue.getStringValue(), dateFormat,
false).toString();
+ if (i == 0) {
+ res = identity;
+ } else if (!Objects.equals(res, identity)) {
+ throw new AnalysisException(
+ String.format("partition values not equal, res: %s,
identity: %s", res, identity));
+ }
+ }
+ return res;
+ }
+
+ @Override
+ public PartitionKeyDesc generateRollUpPartitionKeyDesc(PartitionKeyDesc
partitionKeyDesc,
+ MTMVPartitionInfo mvPartitionInfo, MTMVRelatedTableIf pctTable)
throws AnalysisException {
+ Type partitionColumnType = MTMVPartitionUtil
+ .getPartitionColumnType(pctTable,
mvPartitionInfo.getPartitionColByPctTable(pctTable));
+ Preconditions.checkState(partitionKeyDesc.getLowerValues().size() == 1,
+ "only support one partition column");
+ DateTimeV2Literal beginTime = dateTruncByOffset(
+ partitionKeyDesc.getLowerValues().get(0).getStringValue(),
Optional.empty(), false);
+ PartitionValue lowerValue = new
PartitionValue(dateTimeToStr(beginTime, partitionColumnType));
+ PartitionValue upperValue =
getUpperValue(partitionKeyDesc.getUpperValues().get(0), beginTime,
+ partitionColumnType);
+ return PartitionKeyDesc.createFixed(
+ Collections.singletonList(lowerValue),
+ Collections.singletonList(upperValue));
+ }
+
+ @Override
+ public void analyze(MTMVPartitionInfo mvPartitionInfo) throws
AnalysisException {
+ if (!TIME_UNITS.contains(this.timeUnit)) {
+ throw new AnalysisException(
+ String.format("timeUnit not support: %s, only support:
%s", this.timeUnit, TIME_UNITS));
+ }
+ List<BaseColInfo> pctInfos = mvPartitionInfo.getPctInfos();
+ for (BaseColInfo pctInfo : pctInfos) {
+ MTMVRelatedTableIf pctTable =
MTMVUtil.getRelatedTable(pctInfo.getTableInfo());
+ PartitionType partitionType =
pctTable.getPartitionType(MvccUtil.getSnapshotFromContext(pctTable));
+ if (partitionType == PartitionType.RANGE) {
+ Type partitionColumnType =
MTMVPartitionUtil.getPartitionColumnType(pctTable, pctInfo.getColName());
+ if (!(partitionColumnType.isDatetime() ||
partitionColumnType.isDatetimeV2()
+ || partitionColumnType.isTimeStampTz())) {
+ throw new AnalysisException(
+ "partitionColumnType should be datetime/datetimev2
"
+ + "when PartitionType is range and expr is
date_trunc + date_add/date_sub");
+ }
+ } else {
+ throw new AnalysisException("date_trunc + date_add/date_sub
only support range partition");
+ }
+ }
+ }
+
+ @Override
+ public String toSql(MTMVPartitionInfo mvPartitionInfo) {
+ Preconditions.checkState(mvPartitionInfo.getExpr() != null, "mtmv
partition expr is null");
+ return mvPartitionInfo.getExpr().accept(ExprToSqlVisitor.INSTANCE,
ToSqlParams.WITHOUT_TABLE);
+ }
+
+ private PartitionValue getUpperValue(PartitionValue upperValue,
DateTimeV2Literal beginTruncTime,
+ Type partitionColumnType) throws AnalysisException {
+ if (upperValue.isMax()) {
+ throw new AnalysisException("date_trunc + date_add/date_sub not
support MAXVALUE partition");
+ }
+ DateTimeV2Literal endTruncTime =
dateTruncByOffset(upperValue.getStringValue(), Optional.empty(), true);
+ if (!Objects.equals(beginTruncTime, endTruncTime)) {
+ throw new AnalysisException(
+ String.format("partition values not equal, beginTruncTime:
%s, endTruncTime: %s",
+ beginTruncTime, endTruncTime));
+ }
+ DateTimeV2Literal endTime = dateIncrement(beginTruncTime);
+ return new PartitionValue(dateTimeToStr(endTime, partitionColumnType));
+ }
+
+ private DateTimeV2Literal dateTruncByOffset(String value, Optional<String>
dateFormat, boolean isUpper)
+ throws AnalysisException {
+ DateTimeV2Literal dateTimeLiteral = strToDate(value, dateFormat);
+ dateTimeLiteral = dateOffset(dateTimeLiteral);
+ if (isUpper) {
+ dateTimeLiteral = (DateTimeV2Literal)
DateTimeArithmetic.secondsSub(dateTimeLiteral, new BigIntLiteral(1));
Review Comment:
Subtracting a full second here is too coarse for the DATETIMEV2(6) support
added by this PR. If a base partition starts and ends within the first second
of the rolled-up unit, the exclusive upper bound is still in the same logical
day or week or month, but backing it off by one second pushes it into the
previous unit and makes beginTruncTime != endTruncTime. Example: with plus 3
hour rollup, a base range [2025-07-24 21:00:00.100000, 2025-07-24
21:00:00.900000) should still map to 2025-07-25, but this code turns the upper
bound into 2025-07-24 23:59:59.900000 before truncation and rejects it. Please
use the smallest supported epsilon for the literal scale instead of a
hard-coded one-second step, and add a regression with fractional partition
boundaries.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]