/*
* Extract rowcount and width estimates for possible use in grouping
* decisions. Beware here of the possibility that
- * cheapest_path->parent is NULL (ie, there is no FROM clause).
+ * cheapest_path->parent is NULL (ie, there is no FROM clause). Also,
+ * if the final rel has been proven dummy, its rows estimate will be
+ * zero; clamp it to one to avoid zero-divide in subsequent
+ * calculations.
*/
if (cheapest_path->parent)
{
- path_rows = cheapest_path->parent->rows;
+ path_rows = clamp_row_est(cheapest_path->parent->rows);
path_width = cheapest_path->parent->width;
}
else
* *isdefault: set to TRUE if the result is a default rather than based on
* anything meaningful.
*
- * NB: be careful to produce an integral result, since callers may compare
- * the result to exact integer counts.
+ * NB: be careful to produce a positive integral result, since callers may
+ * compare the result to exact integer counts, or might divide by it.
*/
double
get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
* If we had an absolute estimate, use that.
*/
if (stadistinct > 0.0)
- return stadistinct;
+ return clamp_row_est(stadistinct);
/*
* Otherwise we need to get the relation size; punt if not available.
* If we had a relative estimate, use that.
*/
if (stadistinct < 0.0)
- return floor((-stadistinct * ntuples) + 0.5);
+ return clamp_row_est(-stadistinct * ntuples);
/*
* With no data, estimate ndistinct = ntuples if the table is small, else
* that the behavior isn't discontinuous.
*/
if (ntuples < DEFAULT_NUM_DISTINCT)
- return ntuples;
+ return clamp_row_est(ntuples);
*isdefault = true;
return DEFAULT_NUM_DISTINCT;