-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[nereids] consider numNulls in filter estimation #29184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ae1bb9
[nereids] consider numNulls in filter estimation
d550200
[nereids] consider numNulls in filter estimation
d722b23
[nereids] consider numNulls in filter estimation
3c5d5ac
[nereids] consider numNulls in filter estimation
0f692eb
[nereids] consider numNulls in filter estimation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,8 @@ public Statistics visitCompoundPredicate(CompoundPredicate predicate, Estimation | |
| colBuilder.setMinValue(union.getLow()).setMinExpr(union.getLowExpr()) | ||
| .setMaxValue(union.getHigh()).setMaxExpr(union.getHighExpr()) | ||
| .setNdv(union.getDistinctValues()); | ||
| double maxNumNulls = Math.max(leftColStats.numNulls, rightColStats.numNulls); | ||
| colBuilder.setNumNulls(Math.min(colBuilder.getCount(), maxNumNulls)); | ||
| orStats.addColumnStats(slot, colBuilder.build()); | ||
| } | ||
| } | ||
|
|
@@ -175,7 +177,7 @@ public Statistics visitComparisonPredicate(ComparisonPredicate cp, EstimationCon | |
| } | ||
|
|
||
| private Statistics updateLessThanLiteral(Expression leftExpr, ColumnStatistic statsForLeft, | ||
| ColumnStatistic statsForRight, EstimationContext context, boolean contains) { | ||
| ColumnStatistic statsForRight, EstimationContext context) { | ||
| StatisticRange rightRange = new StatisticRange(statsForLeft.minValue, statsForLeft.minExpr, | ||
| statsForRight.maxValue, statsForRight.maxExpr, | ||
| statsForLeft.ndv, leftExpr.getDataType()); | ||
|
|
@@ -185,7 +187,7 @@ private Statistics updateLessThanLiteral(Expression leftExpr, ColumnStatistic st | |
| } | ||
|
|
||
| private Statistics updateGreaterThanLiteral(Expression leftExpr, ColumnStatistic statsForLeft, | ||
| ColumnStatistic statsForRight, EstimationContext context, boolean contains) { | ||
| ColumnStatistic statsForRight, EstimationContext context) { | ||
| StatisticRange rightRange = new StatisticRange(statsForRight.minValue, statsForRight.minExpr, | ||
| statsForLeft.maxValue, statsForLeft.maxExpr, | ||
| statsForLeft.ndv, leftExpr.getDataType()); | ||
|
|
@@ -202,12 +204,9 @@ private Statistics calculateWhenLiteralRight(ComparisonPredicate cp, | |
| return estimateEqualTo(cp, statsForLeft, statsForRight, context); | ||
| } else { | ||
| if (cp instanceof LessThan || cp instanceof LessThanEqual) { | ||
| return updateLessThanLiteral(cp.left(), statsForLeft, statsForRight, | ||
| context, cp instanceof LessThanEqual); | ||
| return updateLessThanLiteral(cp.left(), statsForLeft, statsForRight, context); | ||
| } else if (cp instanceof GreaterThan || cp instanceof GreaterThanEqual) { | ||
|
|
||
| return updateGreaterThanLiteral(cp.left(), statsForLeft, statsForRight, context, | ||
| cp instanceof GreaterThanEqual); | ||
| return updateGreaterThanLiteral(cp.left(), statsForLeft, statsForRight, context); | ||
| } else { | ||
| throw new RuntimeException(String.format("Unexpected expression : %s", cp.toSql())); | ||
| } | ||
|
|
@@ -225,6 +224,7 @@ private Statistics estimateEqualTo(ComparisonPredicate cp, ColumnStatistic stats | |
| } else { | ||
| selectivity = StatsMathUtil.minNonNaN(1.0, 1.0 / ndv); | ||
| } | ||
| selectivity = getNotNullSelectivity(statsForLeft, selectivity); | ||
| Statistics equalStats = context.statistics.withSel(selectivity); | ||
| Expression left = cp.left(); | ||
| equalStats.addColumnStats(left, statsForRight); | ||
|
|
@@ -331,10 +331,12 @@ A not in (1, 2, 3, 100): | |
| selectivity = 1.0; | ||
| } | ||
| } | ||
| compareExprStatsBuilder.setNumNulls(0); | ||
| Statistics estimated = new Statistics(context.statistics); | ||
| ColumnStatistic stats = compareExprStatsBuilder.build(); | ||
| selectivity = getNotNullSelectivity(stats, selectivity); | ||
| estimated = estimated.withSel(selectivity); | ||
| estimated.addColumnStats(compareExpr, | ||
| compareExprStatsBuilder.build()); | ||
| estimated.addColumnStats(compareExpr, stats); | ||
| context.addKeyIfSlot(compareExpr); | ||
| return estimated; | ||
| } | ||
|
|
@@ -394,6 +396,11 @@ public Statistics visitNot(Not not, EstimationContext context) { | |
| .setMaxValue(originColStats.maxValue) | ||
| .setMaxExpr(originColStats.maxExpr); | ||
| } | ||
| if (not.child().getInputSlots().size() == 1 && !(child instanceof IsNull)) { | ||
| // only consider the single column numNull, otherwise, ignore | ||
| rowCount = Math.max(rowCount - originColStats.numNulls, 1); | ||
| statisticsBuilder.setRowCount(rowCount); | ||
| } | ||
| statisticsBuilder.putColumnStatistics(slot, colBuilder.build()); | ||
| } | ||
| } | ||
|
|
@@ -460,15 +467,18 @@ private Statistics estimateBinaryComparisonFilter(Expression leftExpr, ColumnSta | |
| .setMaxValue(Double.POSITIVE_INFINITY) | ||
| .setMaxExpr(null) | ||
| .setNdv(0) | ||
| .setCount(0); | ||
| .setCount(0) | ||
| .setNumNulls(0); | ||
| } else { | ||
| leftColumnStatisticBuilder = new ColumnStatisticBuilder(leftStats) | ||
| .setMinValue(intersectRange.getLow()) | ||
| .setMinExpr(intersectRange.getLowExpr()) | ||
| .setMaxValue(intersectRange.getHigh()) | ||
| .setMaxExpr(intersectRange.getHighExpr()) | ||
| .setNdv(intersectRange.getDistinctValues()); | ||
| .setNdv(intersectRange.getDistinctValues()) | ||
| .setNumNulls(0); | ||
| double sel = leftRange.overlapPercentWith(rightRange); | ||
| sel = getNotNullSelectivity(leftStats, sel); | ||
| updatedStatistics = context.statistics.withSel(sel); | ||
| leftColumnStatisticBuilder.setCount(updatedStatistics.getRowCount()); | ||
| } | ||
|
|
@@ -488,6 +498,7 @@ private Statistics estimateColumnEqualToColumn(Expression leftExpr, ColumnStatis | |
| intersectBuilder.setNdv(intersect.getDistinctValues()); | ||
| intersectBuilder.setMinValue(intersect.getLow()); | ||
| intersectBuilder.setMaxValue(intersect.getHigh()); | ||
| intersectBuilder.setNumNulls(0); | ||
| double sel = 1 / StatsMathUtil.nonZeroDivisor(Math.max(leftStats.ndv, rightStats.ndv)); | ||
| Statistics updatedStatistics = context.statistics.withSel(sel); | ||
| updatedStatistics.addColumnStats(leftExpr, intersectBuilder.build()); | ||
|
|
@@ -568,10 +579,34 @@ public Statistics visitLike(Like like, EstimationContext context) { | |
| "col stats not found. slot=%s in %s", | ||
| like.left().toSql(), like.toSql()); | ||
| ColumnStatisticBuilder colBuilder = new ColumnStatisticBuilder(origin); | ||
| colBuilder.setNdv(origin.ndv * DEFAULT_LIKE_COMPARISON_SELECTIVITY).setNumNulls(0); | ||
| double selectivity = StatsMathUtil.divide(DEFAULT_LIKE_COMPARISON_SELECTIVITY, origin.ndv); | ||
| double notNullSel = getNotNullSelectivity(origin, selectivity); | ||
| colBuilder.setNdv(origin.ndv * DEFAULT_LIKE_COMPARISON_SELECTIVITY) | ||
| .setCount(notNullSel * context.statistics.getRowCount()).setNumNulls(0); | ||
| statsBuilder.putColumnStatistics(like.left(), colBuilder.build()); | ||
| context.addKeyIfSlot(like.left()); | ||
| } | ||
| return statsBuilder.build(); | ||
| } | ||
|
|
||
| private double getNotNullSelectivity(ColumnStatistic stats, double origSel) { | ||
| double rowCount = stats.count; | ||
| double numNulls = stats.numNulls; | ||
|
|
||
| // comment following check since current rowCount and ndv may be inconsistant | ||
| // e.g, rowCount has been reduced by one filter but another filter column's | ||
| // ndv and numNull remains originally, which will unexpectedly go into the following | ||
| // normalization. | ||
|
|
||
| //if (numNulls > rowCount - ndv) { | ||
| // numNulls = rowCount - ndv > 0 ? rowCount - ndv : 0; | ||
| //} | ||
| double notNullSel = rowCount <= 1.0 ? 1.0 : 1 - getValidSelectivity(numNulls / rowCount); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if rowCount=0, notNullSel is NaN. And this NaN pollute following derivation. |
||
| double validSel = origSel * notNullSel; | ||
| return getValidSelectivity(validSel); | ||
| } | ||
|
|
||
| private static double getValidSelectivity(double nullSel) { | ||
| return nullSel < 0 ? 0 : (nullSel > 1 ? 1 : nullSel); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could cause heavy errors when the child is unknown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the precondition has been checked before.