hakanuzum commented on code in PR #62599: URL: https://github.com/apache/doris/pull/62599#discussion_r3130691362
########## fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPartitionExprDateTruncDateAddSub.java: ########## @@ -0,0 +1,338 @@ +// 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.ArrayList; +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)); + 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 { + DateTimeV2Literal first = 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"); + } + DateTimeV2Literal identity = dateTruncByOffset(partitionValue.getStringValue(), dateFormat); + if (i == 0) { + first = identity; + } else if (!isSameTime(first, identity)) { + throw new AnalysisException( + String.format("partition values not equal, first: %s, identity: %s", first, identity)); + } + } + Preconditions.checkState(first != null, "partition values is empty"); + return first.toString(); + } + + @Override + public PartitionKeyDesc generateRollUpPartitionKeyDesc(PartitionKeyDesc partitionKeyDesc, + MTMVPartitionInfo mvPartitionInfo, MTMVRelatedTableIf pctTable) throws AnalysisException { + List<PartitionKeyDesc> descs = generateRollUpPartitionKeyDescs(partitionKeyDesc, mvPartitionInfo, pctTable); + Preconditions.checkState(descs.size() == 1, + "expected single roll-up partition desc, but got: %s", descs); + return descs.get(0); + } + + @Override + public List<PartitionKeyDesc> generateRollUpPartitionKeyDescs(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 beginBucket = dateTruncByOffset( + partitionKeyDesc.getLowerValues().get(0).getStringValue(), Optional.empty()); + + // The upper bound of a range partition is exclusive. + // Compute the offset-applied upper value BEFORE truncation so we can detect + // whether it falls exactly on a time-unit boundary. + // + // Case A – offset-aligned boundary (e.g. upper = 21:00:00, offset +3h): + // upperWithOffset = 00:00:00 of next day == endBucket → exact boundary hit + // → the last actual data value (epsilon below upper) maps to the PREVIOUS bucket, + // so endBucket must NOT be included. + // + // Case B – UTC-midnight boundary (e.g. upper = 00:00:00, offset +3h): + // upperWithOffset = 03:00:00 != endBucket (00:00:00) → mid-bucket hit + // → actual data near the upper bound still maps to endBucket, + // so endBucket MUST be included. + DateTimeV2Literal upperRaw = strToDate( Review Comment: Fixed in latest commit -- 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]
