Skip to content

Commit 0f0a330

Browse files
committed
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.
1 parent afab814 commit 0f0a330

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/backend/utils/adt/selfuncs.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.202 2006/04/27 00:46:58 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.203 2006/04/27 17:52:40 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -420,7 +420,7 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
420420
* to the result selectivity. Also add up the total fraction represented
421421
* by MCV entries.
422422
*/
423-
mcv_selec = mcv_selectivity(vardata, &opproc, constval,
423+
mcv_selec = mcv_selectivity(vardata, &opproc, constval, true,
424424
&sumcommon);
425425

426426
/*
@@ -460,16 +460,17 @@ scalarineqsel(PlannerInfo *root, Oid operator, bool isgt,
460460
* mcv_selectivity - Examine the MCV list for scalarineqsel
461461
*
462462
* Determine the fraction of the variable's MCV population that satisfies
463-
* the predicate (VAR OP CONST), as well as the fraction of the total column
464-
* population represented by the MCV list. This code will work for any
465-
* boolean-returning predicate operator.
463+
* the predicate (VAR OP CONST), or (CONST OP VAR) if !varonleft. Also
464+
* compute the fraction of the total column population represented by the MCV
465+
* list. This code will work for any boolean-returning predicate operator.
466466
*
467467
* The function result is the MCV selectivity, and the fraction of the
468468
* total population is returned into *sumcommonp. Zeroes are returned
469469
* if there is no MCV list.
470470
*/
471471
double
472-
mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
472+
mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
473+
Datum constval, bool varonleft,
473474
double *sumcommonp)
474475
{
475476
double mcv_selec,
@@ -483,11 +484,6 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
483484
mcv_selec = 0.0;
484485
sumcommon = 0.0;
485486

486-
/*
487-
* If we have most-common-values info, add up the fractions of the MCV
488-
* entries that satisfy MCV OP CONST. Also add up the total fraction
489-
* represented by MCV entries.
490-
*/
491487
if (HeapTupleIsValid(vardata->statsTuple) &&
492488
get_attstatsslot(vardata->statsTuple,
493489
vardata->atttype, vardata->atttypmod,
@@ -497,9 +493,13 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
497493
{
498494
for (i = 0; i < nvalues; i++)
499495
{
500-
if (DatumGetBool(FunctionCall2(opproc,
496+
if (varonleft ?
497+
DatumGetBool(FunctionCall2(opproc,
501498
values[i],
502-
constval)))
499+
constval)) :
500+
DatumGetBool(FunctionCall2(opproc,
501+
constval,
502+
values[i])))
503503
mcv_selec += numbers[i];
504504
sumcommon += numbers[i];
505505
}
@@ -1018,7 +1018,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
10181018
* represented by MCV entries.
10191019
*/
10201020
fmgr_info(get_opcode(operator), &opproc);
1021-
mcv_selec = mcv_selectivity(&vardata, &opproc, constval,
1021+
mcv_selec = mcv_selectivity(&vardata, &opproc, constval, true,
10221022
&sumcommon);
10231023

10241024
/*

src/include/utils/selfuncs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.31 2006/04/27 00:46:59 tgl Exp $
11+
* $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.32 2006/04/27 17:52:40 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -108,7 +108,8 @@ extern void get_join_variables(PlannerInfo *root, List *args,
108108
VariableStatData *vardata2);
109109
extern double get_variable_numdistinct(VariableStatData *vardata);
110110
extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
111-
Datum constval, double *sumcommonp);
111+
Datum constval, bool varonleft,
112+
double *sumcommonp);
112113

113114
extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
114115
Pattern_Type ptype,

0 commit comments

Comments
 (0)