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 */
471471double
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 /*
0 commit comments