Generalize mcv_selectivity() to support both VAR OP CONST and CONST OP VAR

cases.  This was not needed in the existing uses within selfuncs.c, but if
we're gonna export it for general use, the extra generality seems helpful.
Motivated by looking at ltree example.
This commit is contained in:
Tom Lane 2006-04-27 17:52:40 +00:00
parent afab814a18
commit 0f0a33099c
2 changed files with 17 additions and 16 deletions

View File

@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.202 2006/04/27 00:46:58 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.203 2006/04/27 17:52:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -420,7 +420,7 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
* to the result selectivity. Also add up the total fraction represented
* by MCV entries.
*/
mcv_selec = mcv_selectivity(vardata, &opproc, constval,
mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
&sumcommon);
/*
@ -460,16 +460,17 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
* mcv_selectivity - Examine the MCV list for scalarineqsel
*
* Determine the fraction of the variable's MCV population that satisfies
* the predicate (VAR OP CONST), as well as the fraction of the total column
* population represented by the MCV list. This code will work for any
* boolean-returning predicate operator.
* the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
* compute the fraction of the total column population represented by the MCV
* list. This code will work for any boolean-returning predicate operator.
*
* The function result is the MCV selectivity, and the fraction of the
* total population is returned into *sumcommonp. Zeroes are returned
* if there is no MCV list.
*/
double
mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
Datum constval, bool varonleft,
double *sumcommonp)
{
double mcv_selec,
@ -483,11 +484,6 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
mcv_selec = 0.0;
sumcommon = 0.0;
/*
* If we have most-common-values info, add up the fractions of the MCV
* entries that satisfy MCV OP CONST. Also add up the total fraction
* represented by MCV entries.
*/
if (HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple,
vardata->atttype, vardata->atttypmod,
@ -497,9 +493,13 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
{
for (i = 0; i < nvalues; i++)
{
if (DatumGetBool(FunctionCall2(opproc,
if (varonleft ?
DatumGetBool(FunctionCall2(opproc,
values[i],
constval)))
constval)) :
DatumGetBool(FunctionCall2(opproc,
constval,
values[i])))
mcv_selec += numbers[i];
sumcommon += numbers[i];
}
@ -1018,7 +1018,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
* represented by MCV entries.
*/
fmgr_info(get_opcode(operator), &opproc);
mcv_selec = mcv_selectivity(&vardata, &opproc, constval,
mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
&sumcommon);
/*

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.31 2006/04/27 00:46:59 tgl Exp $
* $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.32 2006/04/27 17:52:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -108,7 +108,8 @@ extern void get_join_variables(PlannerInfo *root, List *args,
VariableStatData *vardata2);
extern double get_variable_numdistinct(VariableStatData *vardata);
extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
Datum constval, double *sumcommonp);
Datum constval, bool varonleft,
double *sumcommonp);
extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
Pattern_Type ptype,