Skip to content

Commit 0fa2967

Browse files
Gautam Paraigparai
authored andcommitted
DRILL-7245: Cap NDV at row count after applying filters
closes #1786
1 parent b774eec commit 0fa2967

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

exec/java-exec/src/main/java/org/apache/drill/exec/planner/cost/DrillRelMdDistinctRowCount.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Double getDistinctRowCount(RelNode rel, RelMetadataQuery mq, ImmutableBit
136136
*/
137137
private Double getDistinctRowCountInternal(TableScan scan, RelMetadataQuery mq, DrillTable table,
138138
ImmutableBitSet groupKey, RelDataType type, RexNode predicate) {
139-
double selectivity, rowCount;
139+
double selectivity, gbyColPredSel, rowCount;
140140
/* If predicate is present, determine its selectivity to estimate filtered rows.
141141
* Thereafter, compute the number of distinct rows.
142142
*/
@@ -172,16 +172,17 @@ private Double getDistinctRowCountInternal(TableScan scan, RelMetadataQuery mq,
172172
break;
173173
}
174174
estRowCnt *= ndv;
175-
selectivity = getPredSelectivityContainingInputRef(predicate, i, mq, scan);
175+
gbyColPredSel = getPredSelectivityContainingInputRef(predicate, i, mq, scan);
176176
/* If predicate is on group-by column, scale down the NDV by selectivity. Consider the query
177177
* select a, b from t where a = 10 group by a, b. Here, NDV(a) will be scaled down by SEL(a)
178178
* whereas NDV(b) will not.
179179
*/
180-
if (selectivity > 0) {
181-
estRowCnt *= selectivity;
180+
if (gbyColPredSel > 0) {
181+
estRowCnt *= gbyColPredSel;
182182
}
183183
}
184-
estRowCnt = Math.min(estRowCnt, rowCount);
184+
// Estimated NDV should not exceed number of rows after applying the filters
185+
estRowCnt = Math.min(estRowCnt, selectivity*rowCount);
185186
if (!allColsHaveNDV) {
186187
if (logger.isDebugEnabled()) {
187188
logger.debug(String.format("NDV not available for %s(%s). Using default rowcount for group-by %s",

0 commit comments

Comments
 (0)