Commit 0f0a3309 authored by Tom Lane's avatar Tom Lane

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.
parent afab814a
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -420,7 +420,7 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
* to the result selectivity. Also add up the total fraction represented * to the result selectivity. Also add up the total fraction represented
* by MCV entries. * by MCV entries.
*/ */
mcv_selec = mcv_selectivity(vardata, &opproc, constval, mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
&sumcommon); &sumcommon);
/* /*
...@@ -460,16 +460,17 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt, ...@@ -460,16 +460,17 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
* mcv_selectivity - Examine the MCV list for scalarineqsel * mcv_selectivity - Examine the MCV list for scalarineqsel
* *
* Determine the fraction of the variable's MCV population that satisfies * 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 * the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
* population represented by the MCV list. This code will work for any * compute the fraction of the total column population represented by the MCV
* boolean-returning predicate operator. * list. This code will work for any boolean-returning predicate operator.
* *
* The function result is the MCV selectivity, and the fraction of the * The function result is the MCV selectivity, and the fraction of the
* total population is returned into *sumcommonp. Zeroes are returned * total population is returned into *sumcommonp. Zeroes are returned
* if there is no MCV list. * if there is no MCV list.
*/ */
double double
mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
Datum constval, bool varonleft,
double *sumcommonp) double *sumcommonp)
{ {
double mcv_selec, double mcv_selec,
...@@ -483,11 +484,6 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, ...@@ -483,11 +484,6 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
mcv_selec = 0.0; mcv_selec = 0.0;
sumcommon = 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) && if (HeapTupleIsValid(vardata->statsTuple) &&
get_attstatsslot(vardata->statsTuple, get_attstatsslot(vardata->statsTuple,
vardata->atttype, vardata->atttypmod, vardata->atttype, vardata->atttypmod,
...@@ -497,9 +493,13 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, ...@@ -497,9 +493,13 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
{ {
for (i = 0; i < nvalues; i++) for (i = 0; i < nvalues; i++)
{ {
if (DatumGetBool(FunctionCall2(opproc, if (varonleft ?
DatumGetBool(FunctionCall2(opproc,
values[i], values[i],
constval))) constval)) :
DatumGetBool(FunctionCall2(opproc,
constval,
values[i])))
mcv_selec += numbers[i]; mcv_selec += numbers[i];
sumcommon += numbers[i]; sumcommon += numbers[i];
} }
...@@ -1018,7 +1018,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype) ...@@ -1018,7 +1018,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
* represented by MCV entries. * represented by MCV entries.
*/ */
fmgr_info(get_opcode(operator), &opproc); fmgr_info(get_opcode(operator), &opproc);
mcv_selec = mcv_selectivity(&vardata, &opproc, constval, mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
&sumcommon); &sumcommon);
/* /*
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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, ...@@ -108,7 +108,8 @@ extern void get_join_variables(PlannerInfo *root, List *args,
VariableStatData *vardata2); VariableStatData *vardata2);
extern double get_variable_numdistinct(VariableStatData *vardata); extern double get_variable_numdistinct(VariableStatData *vardata);
extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, 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, extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
Pattern_Type ptype, Pattern_Type ptype,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment