Skip to content

Commit

Permalink
[BugFix] fix concat expr with multiple args nullsFraction (StarRocks#…
Browse files Browse the repository at this point in the history
…52683)

Signed-off-by: stephen <[email protected]>
Signed-off-by: zhiminr.ren <[email protected]>
  • Loading branch information
stephen-shelby authored and renzhimin7 committed Nov 7, 2024
1 parent 6e1f8dd commit 5bde6f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 5bde6f1

Please sign in to comment.