Skip to content

Commit

Permalink
opt: fix internal error "estimated row count must be non-zero"
Browse files Browse the repository at this point in the history
Release justification: low risk, high benefit changes to existing
functionality

This commit fixes a rare error that could occur when a query had
many highly selective filter predicates. This error occured when the
estimated selectivity of a Select operator was 0. Prior to this commit,
we prevented the selectivity of a single filter from ever going below
1e-10, but to get the overall selectivity we multiplied the individual
selectivities together. We only needed 32 filter conditions in which
the selectivity was 1e-10 for the overall selectivity to underflow the
floating point representation and result in selectivity 0.

This commit fixes the error by setting the selectivity to 1e-10 *after*
multiplying the individual selectivities together if it is less than
1e-10.

Fixes cockroachdb#53311

Release note (bug fix): Fixed a rare internal error that could occur
during planning of queries with many highly selective predicates.
  • Loading branch information
rytaft committed Sep 2, 2020
1 parent e9efaa3 commit ee664a8
Show file tree
Hide file tree
Showing 6 changed files with 649 additions and 172 deletions.
29 changes: 23 additions & 6 deletions pkg/sql/opt/memo/statistics_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,9 @@ func (sb *statisticsBuilder) selectivityFromDistinctCounts(
)
}

return selectivity
// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

// selectivityFromHistograms is similar to selectivityFromDistinctCounts, in
Expand Down Expand Up @@ -3138,7 +3140,10 @@ func (sb *statisticsBuilder) selectivityFromHistograms(
nonNullSelectivity, nullSelectivity, inputColStat.NullCount, inputStats.RowCount,
)
}
return selectivity

// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

// selectivityFromNullsRemoved calculates the selectivity from null-rejecting
Expand All @@ -3161,7 +3166,9 @@ func (sb *statisticsBuilder) selectivityFromNullsRemoved(
}
})

return selectivity
// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

// predicateSelectivity calculates the selectivity of a predicate, using the
Expand Down Expand Up @@ -3196,7 +3203,10 @@ func (sb *statisticsBuilder) selectivityFromEquivalencies(
equivGroup := filterFD.ComputeEquivGroup(i)
selectivity *= sb.selectivityFromEquivalency(equivGroup, e, s)
})
return selectivity

// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

func (sb *statisticsBuilder) selectivityFromEquivalency(
Expand Down Expand Up @@ -3241,7 +3251,10 @@ func (sb *statisticsBuilder) selectivityFromEquivalenciesSemiJoin(
equivGroup, leftOutputCols, rightOutputCols, e, s,
)
})
return selectivity

// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

func (sb *statisticsBuilder) selectivityFromEquivalencySemiJoin(
Expand Down Expand Up @@ -3279,7 +3292,11 @@ func (sb *statisticsBuilder) selectivityFromEquivalencySemiJoin(
func (sb *statisticsBuilder) selectivityFromUnappliedConjuncts(
numUnappliedConjuncts float64,
) (selectivity float64) {
return math.Pow(unknownFilterSelectivity, numUnappliedConjuncts)
selectivity = math.Pow(unknownFilterSelectivity, numUnappliedConjuncts)

// Avoid setting selectivity to 0. The stats may be stale, and we
// can end up with weird and inefficient plans if we estimate 0 rows.
return max(selectivity, epsilon)
}

// tryReduceCols is used to determine which columns to use for selectivity
Expand Down
Loading

0 comments on commit ee664a8

Please sign in to comment.