github-actions[bot] commented on code in PR #62535: URL: https://github.com/apache/doris/pull/62535#discussion_r3093742591
########## regression-test/suites/mtmv_p0/test_union_compensation_mtmv_date_add_hour_offset.groovy: ########## @@ -0,0 +1,123 @@ +// 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. + +suite("test_union_compensation_mtmv_date_add_hour_offset", "mtmv") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + sql "SET enable_materialized_view_rewrite=true" + sql "SET enable_materialized_view_nest_rewrite=true" + sql "SET enable_materialized_view_union_rewrite=true" + sql "SET enable_nereids_timeout=false" + + sql """drop materialized view if exists mv_test_union_compensation_mtmv_date_add_hour_offset""" + sql """drop table if exists t_test_union_compensation_mtmv_date_add_hour_offset""" + + sql """ + CREATE TABLE t_test_union_compensation_mtmv_date_add_hour_offset ( + id BIGINT NOT NULL, + k2 DATETIME NOT NULL + ) ENGINE=OLAP + DUPLICATE KEY(id) + PARTITION BY range(k2) + ( + PARTITION p_20250724 VALUES [("2025-07-24 00:00:00"),("2025-07-25 00:00:00")), Review Comment: This regression case is built on raw midnight source partitions, but the implementation above still rejects any source partition that spans two shifted day buckets. With these ranges and `+3 HOUR`, `p_20250725` overlaps both local days `2025-07-25` and `2025-07-26`, so MV partition generation should fail before union compensation is even reached. To make this test meaningful, either split the source partitions at the shifted boundary (`21:00`) or first fix the rollup mapping logic so one source partition can contribute to adjacent MV partitions. ########## 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)) { Review Comment: This still assumes that every source range partition maps to exactly one shifted `date_trunc(...)` bucket. For a normal UTC partition like `[2025-07-25 00:00:00, 2025-07-26 00:00:00)` and `date_trunc(date_add(k2, INTERVAL 3 HOUR), 'day')`, `beginTruncTime` is `2025-07-25 00:00:00` but `endTruncTime` becomes `2025-07-26 00:00:00`, so this throws `partition values not equal`. That means `CreateMTMVInfo.generatePartitionDesc()` / `MTMVPartitionUtil.alignMvPartition()` still cannot support the advertised `UTC table -> local business day` case unless the base partitions are already pre-shifted to the local-day boundary (for `+3h`, `21:00`). ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java: ########## @@ -57,6 +57,7 @@ import org.apache.doris.nereids.trees.plans.TableId; import org.apache.doris.nereids.trees.plans.algebra.CatalogRelation; import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.commands.UpdateMvByPartitionCommand; Review Comment: `UpdateMvByPartitionCommand` is not referenced anywhere in this file. FE checkstyle enables `UnusedImports`, so this import should be removed or `mvn validate` will fail on style checks. -- 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]
