1515 *
1616 *
1717 * IDENTIFICATION
18- * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.202 2006/04/2700:46:58 tgl Exp $
18+ * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.203 2006/04/2717: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 theMCV 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 thefraction 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 */
471471double
472- mcv_selectivity (VariableStatData * vardata ,FmgrInfo * opproc ,Datum constval ,
472+ mcv_selectivity (VariableStatData * vardata ,FmgrInfo * opproc ,
473+ Datum constval ,bool varonleft ,
473474double * sumcommonp )
474475{
475476double mcv_selec ,
@@ -483,11 +484,6 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
483484mcv_selec = 0.0 ;
484485sumcommon = 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- */
491487if (HeapTupleIsValid (vardata -> statsTuple )&&
492488get_attstatsslot (vardata -> statsTuple ,
493489vardata -> atttype ,vardata -> atttypmod ,
@@ -497,9 +493,13 @@ mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval,
497493{
498494for (i = 0 ;i < nvalues ;i ++ )
499495{
500- if (DatumGetBool (FunctionCall2 (opproc ,
496+ if (varonleft ?
497+ DatumGetBool (FunctionCall2 (opproc ,
501498values [i ],
502- constval )))
499+ constval )) :
500+ DatumGetBool (FunctionCall2 (opproc ,
501+ constval ,
502+ values [i ])))
503503mcv_selec += numbers [i ];
504504sumcommon += numbers [i ];
505505}
@@ -1018,7 +1018,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
10181018 * represented by MCV entries.
10191019 */
10201020fmgr_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/*