englefly commented on code in PR #12987: URL: https://github.com/apache/doris/pull/12987#discussion_r990582926
########## fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ExpressionEstimation.java: ########## @@ -0,0 +1,210 @@ +// 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.nereids.stats; + +import org.apache.doris.nereids.trees.expressions.Add; +import org.apache.doris.nereids.trees.expressions.BinaryArithmetic; +import org.apache.doris.nereids.trees.expressions.Divide; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Multiply; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.Subtract; +import org.apache.doris.nereids.trees.expressions.functions.agg.Avg; +import org.apache.doris.nereids.trees.expressions.functions.agg.Count; +import org.apache.doris.nereids.trees.expressions.functions.agg.Max; +import org.apache.doris.nereids.trees.expressions.functions.agg.Min; +import org.apache.doris.nereids.trees.expressions.functions.agg.Sum; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring; +import org.apache.doris.nereids.trees.expressions.functions.scalar.WeekOfYear; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Year; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.util.Utils; +import org.apache.doris.statistics.ColumnStat; +import org.apache.doris.statistics.StatsDeriveResult; + +import com.google.common.base.Preconditions; + +/** + * Used to estimate for expressions that not producing boolean value. + */ +public class ExpressionEstimation extends ExpressionVisitor<ColumnStat, StatsDeriveResult> { + + private static ExpressionEstimation INSTANCE = new ExpressionEstimation(); + + public static ColumnStat estimate(Expression expression, StatsDeriveResult stats) { + return INSTANCE.visit(expression, stats); + } + + @Override + public ColumnStat visit(Expression expr, StatsDeriveResult context) { + return expr.accept(this, context); + } + + @Override + public ColumnStat visitLiteral(Literal literal, StatsDeriveResult context) { + if (literal.isStringLiteral()) { + return ColumnStat.UNKNOWN; + } + double literalVal = Double.parseDouble(literal.getValue().toString()); + ColumnStat columnStat = new ColumnStat(); + columnStat.setMaxValue(literalVal); + columnStat.setMinValue(literalVal); + columnStat.setNdv(1); + columnStat.setNumNulls(1); + columnStat.setAvgSizeByte(1); + return columnStat; + } + + @Override + public ColumnStat visitSlotReference(SlotReference slotReference, StatsDeriveResult context) { + ColumnStat columnStat = context.getColumnStatsBySlot(slotReference); + Preconditions.checkState(columnStat != null); + return columnStat; + } + + @Override + public ColumnStat visitBinaryArithmetic(BinaryArithmetic binaryArithmetic, StatsDeriveResult context) { + ColumnStat leftColStats = binaryArithmetic.left().accept(this, context); + ColumnStat rightColStats = binaryArithmetic.right().accept(this, context); + double leftNdv = leftColStats.getNdv(); + double rightNdv = rightColStats.getNdv(); + double ndv = Math.max(leftNdv, rightNdv); + double leftNullCount = leftColStats.getNumNulls(); + double rightNullCount = rightColStats.getNumNulls(); + double rowCount = context.getRowCount(); Review Comment: this rowCount is for left, right or binary result? in L92, `leftNullCount / rowCount ` makes sense only if rowCount is the left row count. -- 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