From 1ee869e395fb75d6e042e79fa94967156311fd6b Mon Sep 17 00:00:00 2001 From: stephen Date: Wed, 6 Nov 2024 22:35:37 +0800 Subject: [PATCH] [BugFix] fix concat nullsFractionc Signed-off-by: stephen --- .../ExpressionStatisticCalculator.java | 4 ++-- .../PredicateStatisticsCalculatorTest.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/ExpressionStatisticCalculator.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/ExpressionStatisticCalculator.java index 767e1e878228c..130831fecaef7 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/ExpressionStatisticCalculator.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/statistics/ExpressionStatisticCalculator.java @@ -643,8 +643,8 @@ private ColumnStatistic multiaryExpressionCalculate(CallOperator callOperator, distinctValues = Math.min(rowCount, childColumnStatisticList.stream().mapToDouble(ColumnStatistic::getDistinctValuesCount).sum()); averageRowSize = childColumnStatisticList.stream().mapToDouble(ColumnStatistic::getAverageRowSize).sum(); - nullsFraction = 1 - childColumnStatisticList.stream().mapToDouble(ColumnStatistic::getAverageRowSize) - .reduce(1, (a, b) -> (1 - a) * (1 - b)); + nullsFraction = 1 - childColumnStatisticList.stream().mapToDouble(ColumnStatistic::getNullsFraction) + .reduce(1.0, (accumulator, nullFraction) -> accumulator * (1 - nullFraction)); return new ColumnStatistic(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, nullsFraction, averageRowSize, distinctValues); default: diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/statistics/PredicateStatisticsCalculatorTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/statistics/PredicateStatisticsCalculatorTest.java index 67ec599c2068c..e249ef57ca852 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/statistics/PredicateStatisticsCalculatorTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/statistics/PredicateStatisticsCalculatorTest.java @@ -15,10 +15,12 @@ package com.starrocks.sql.optimizer.statistics; +import com.google.common.collect.Lists; import com.starrocks.analysis.BinaryType; import com.starrocks.catalog.Type; import com.starrocks.sql.optimizer.Utils; import com.starrocks.sql.optimizer.operator.scalar.BinaryPredicateOperator; +import com.starrocks.sql.optimizer.operator.scalar.CallOperator; import com.starrocks.sql.optimizer.operator.scalar.ColumnRefOperator; import com.starrocks.sql.optimizer.operator.scalar.CompoundPredicateOperator; import com.starrocks.sql.optimizer.operator.scalar.ConstantOperator; @@ -111,4 +113,18 @@ public void testNullEqStatistic() throws Exception { Assert.assertEquals(5000, estimatedStatistics.getOutputRowCount(), 0.001); Assert.assertEquals(1, estimatedStatistics.getColumnStatistic(c1).getNullsFraction(), 0.001); } + + @Test + public void testConcatExpressionCalculate() { + ColumnRefOperator c1 = new ColumnRefOperator(0, Type.VARCHAR, "c1", true); + ConstantOperator c2 = new ConstantOperator("-", Type.VARCHAR); + ColumnRefOperator c3 = new ColumnRefOperator(1, Type.VARCHAR, "c3", true); + CallOperator concat = new CallOperator("concat", Type.VARCHAR, Lists.newArrayList(c1, c2, c3)); + Statistics statistics = Statistics.builder() + .addColumnStatistic(c1, ColumnStatistic.builder().setNullsFraction(0.2).setDistinctValuesCount(10).build()) + .addColumnStatistic(c3, ColumnStatistic.builder().setNullsFraction(0.4).setDistinctValuesCount(10).build()) + .setOutputRowCount(10000).build(); + ColumnStatistic estimatedStatistics = ExpressionStatisticCalculator.calculate(concat, statistics); + Assert.assertEquals(0.52, estimatedStatistics.getNullsFraction(), 0.001); + } }