github-actions[bot] commented on code in PR #62535: URL: https://github.com/apache/doris/pull/62535#discussion_r3097674915
########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExprDateTruncDateAddSub.java: ########## @@ -0,0 +1,312 @@ +// 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.CastExpr; +import org.apache.doris.analysis.Expr; +import org.apache.doris.analysis.ExprToSqlVisitor; +import org.apache.doris.analysis.FunctionCallExpr; +import org.apache.doris.analysis.LiteralExpr; +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); + while (dateTruncArg instanceof CastExpr) { + dateTruncArg = dateTruncArg.getChild(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)); Review Comment: This class only remembers `timeUnit` and `offsetHours`; nothing from `timestampArithmeticExpr.getChild(0)` is preserved. That means the roll-up code below behaves as if every supported form were just `date_add/sub(partition_col, N HOUR)`. For the new cast-preserving forms that is incorrect: `date_trunc(date_add(cast(k2 as date), INTERVAL 3 HOUR), 'day')` does not map `[2025-07-23 21:00:00, 2025-07-24 21:00:00)` to a single MV day, but `dateTruncByOffset()` will currently accept it and map the whole partition to `2025-07-24`. So the PR persists cast semantics but computes partition lineage as if the cast were absent. We either need to model the inner expression here (and in `equals()/hashCode()`) or reject casted hour-offset expressions up front. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/UpdateMvByPartitionCommand.java: ########## @@ -224,6 +254,71 @@ private static Expression convertRangePartitionToCompare(PartitionItem item, Slo return predicate; } Review Comment: `getInverseHourOffsetForBaseFilter()` makes the same pure-shift assumption. Once the persisted expr can be `date_trunc(date_add(cast(k2 as date), INTERVAL 3 HOUR), 'day')`, translating an MV day partition with only `-offsetHours` produces `[day-3h, nextDay-3h)` on `k2`, which includes previous-day evening rows and misses late rows from the target day. Partial refresh will therefore scan the wrong base-table rows for the new casted forms unless this path also models the inner cast (or those expressions are rejected earlier). ########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/MTMVPartitionDefinition.java: ########## @@ -178,18 +494,78 @@ private static List<Pair<Integer, Expr>> convertDateTruncToLegacyArguments(List< } private static Pair<Integer, Expr> convertToLegacyRecursion(Expression expression) { - if (expression instanceof Slot) { + if (expression instanceof UnboundSlot) { + return Pair.of(1, new SlotRef(null, ((UnboundSlot) expression).getName())); + } else if (expression instanceof Slot) { return Pair.of(1, new SlotRef(null, ((Slot) expression).getName())); + } else if (expression instanceof Cast) { + Pair<Integer, Expr> child = convertToLegacyRecursion(((Cast) expression).child()); + Type castTargetType = ((Cast) expression).getDataType().toCatalogDataType(); + return Pair.of(child.key(), new CastExpr(castTargetType, child.value(), expression.nullable())); + } else if (expression instanceof HoursAdd) { + return Pair.of(1, convertDateAddSubToLegacy((HoursAdd) expression, "date_add")); + } else if (expression instanceof HoursSub) { + return Pair.of(1, convertDateAddSubToLegacy((HoursSub) expression, "date_sub")); + } else if (expression instanceof UnboundFunction) { + String name = ((UnboundFunction) expression).getName().toLowerCase(); + if ("hours_add".equals(name) || "date_add".equals(name)) { + return Pair.of(1, convertDateAddSubToLegacy(expression, "date_add")); + } else if ("hours_sub".equals(name) || "date_sub".equals(name)) { + return Pair.of(1, convertDateAddSubToLegacy(expression, "date_sub")); + } else { + throw new AnalysisException("unsupported argument " + expression.toString()); + } } else if (expression instanceof Literal) { return Pair.of(2, new StringLiteral(((Literal) expression).getStringValue())); - } else if (expression instanceof Cast) { - // mv partition roll up only need the slot in cast - return convertToLegacyRecursion(((Cast) expression).child()); } else { throw new AnalysisException("unsupported argument " + expression.toString()); } } + private static TimestampArithmeticExpr convertDateAddSubToLegacy(Expression expression, String funcName) { + Pair<Integer, Expr> timeExprPair = convertToLegacyRecursion(expression.child(0)); + if (timeExprPair.key() != 1) { + throw new AnalysisException("unsupported date arithmetic argument " + expression.toString()); + } + Expr amountExpr; + Expression offsetExpression = expression.child(1); + if (offsetExpression instanceof Literal) { + amountExpr = ((Literal) offsetExpression).toLegacyLiteral(); + } else if (offsetExpression instanceof Interval) { + Interval interval = (Interval) offsetExpression; + if (interval.timeUnit() != Interval.TimeUnit.HOUR) { + throw new AnalysisException( + "only HOUR unit is supported in date_add/date_sub for mtmv partition: " + expression); + } + if (!(interval.value() instanceof Literal)) { + throw new AnalysisException("date arithmetic offset should be literal " + expression.toString()); + } + amountExpr = ((Literal) interval.value()).toLegacyLiteral(); + } else { Review Comment: `extractSlotName()` loses source identity here. On the raw side `UnboundSlot.getName()` preserves qualifiers (`t1.k2`), but after lineage rewrite `SlotReference.getName()` is usually just `k2`. As a result, `PARTITION BY (date_trunc(date_add(t1.k2, INTERVAL 3 HOUR), 'day'))` will fail to match its SELECT output, and joins with two `k2` columns become falsely ambiguous even though the raw expr is qualified. This matcher needs table-qualified/original-column identity rather than a bare name string. -- 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]
