diff -Naur pgsql.virg/src/backend/optimizer/path/clausesel.c pgsql.dev/src/backend/optimizer/path/clausesel.c --- pgsql.virg/src/backend/optimizer/path/clausesel.c Thu Jun 21 04:18:11 2001 +++ pgsql.dev/src/backend/optimizer/path/clausesel.c Sat Jun 23 18:32:17 2001 @@ -22,6 +22,7 @@ #include "optimizer/plancat.h" #include "optimizer/restrictinfo.h" #include "parser/parsetree.h" +#include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/lsyscache.h" @@ -408,6 +409,14 @@ varRelid); } } + } + else if (IsA(clause, BooleanTest)) + { + s1 = (Selectivity) DatumGetFloat8(DirectFunctionCall4(booltestsel, + PointerGetDatum(root), + (Datum)0, + PointerGetDatum(clause), + Int32GetDatum(varRelid))); } else if (IsA(clause, Param)) { diff -Naur pgsql.virg/src/backend/utils/adt/selfuncs.c pgsql.dev/src/backend/utils/adt/selfuncs.c --- pgsql.virg/src/backend/utils/adt/selfuncs.c Thu Jun 21 04:18:12 2001 +++ pgsql.dev/src/backend/utils/adt/selfuncs.c Sat Jun 23 19:01:23 2001 @@ -934,6 +934,181 @@ } /* + * booltestsel - Selectivity of BooleanTest Node. + */ +Datum +booltestsel(PG_FUNCTION_ARGS) +{ + Query *root = (Query *) PG_GETARG_POINTER(0); + Oid operator = PG_GETARG_OID(1); + List *args = (List *) PG_GETARG_POINTER(2); + int varRelid = PG_GETARG_INT32(3); + Var *var; + Node *clause; + BooleanTest *btest; + Node *arg; + Oid relid; + HeapTuple statsTuple; + Datum *values; + int nvalues; + float4 *numbers; + int nnumbers; + double selec = DEFAULT_EQ_SEL; + + operator = operator; /* keep compiler quiet */ + + clause = (Node *) args; + /* + * If expression is not BooleanTest + * then punt and return a default estimate. + */ + if (!IsA(clause, BooleanTest)) + PG_RETURN_FLOAT8(DEFAULT_EQ_SEL); + + btest = (BooleanTest *) clause; + arg = (Node *) btest->arg; + + /* + * Ignore any binary-compatible relabeling + */ + if (IsA(arg, RelabelType)) + arg = ((RelabelType *) arg)->arg; + + if (IsA(arg, Var) && (varRelid == 0 || varRelid == ((Var *) arg)->varno)) + var = (Var *) arg; + else /* punt */ + PG_RETURN_FLOAT8(DEFAULT_EQ_SEL); + + relid = getrelid(var->varno, root->rtable); + if (relid == InvalidOid) + PG_RETURN_FLOAT8(DEFAULT_EQ_SEL); + + /* get stats for the attribute, if available */ + statsTuple = SearchSysCache(STATRELATT, + ObjectIdGetDatum(relid), + Int16GetDatum(var->varattno), + 0, 0); + if (HeapTupleIsValid(statsTuple)) + { + Form_pg_statistic stats; + + stats = (Form_pg_statistic) GETSTRUCT(statsTuple); + + /* + * Var is being compared to a known non-null constant, + * i.e. true, false, or unknown + */ + if (get_attstatsslot(statsTuple, var->vartype, var->vartypmod, + STATISTIC_KIND_MCV, InvalidOid, + &values, &nvalues, + &numbers, &nnumbers)) + { + bool match = false; + int i; + + if (btest->booltesttype == IS_UNKNOWN || btest->booltesttype == IS_NOT_UNKNOWN) + { + switch (btest->booltesttype) + { + case IS_UNKNOWN: + /* + * Only select unknown (null) values. + * Use null fraction directly. + */ + selec = stats->stanullfrac; + break; + case IS_NOT_UNKNOWN: + /* + * Only select not unknown (not null) values. + * Calculate from null fraction directly. + */ + selec = 1.0 - stats->stanullfrac; + break; + default: + elog(ERROR, "booltestsel: unexpected booltesttype %d", + (int) btest->booltesttype); + return (Datum) 0; /* keep compiler quiet */ + } + } + else + { + for (i = 0; i < nvalues; i++) + { + switch (btest->booltesttype) + { + case IS_TRUE: + match = (values[i] == true); + break; + case IS_NOT_TRUE: + match = (values[i] == false); + break; + case IS_FALSE: + match = (values[i] == false); + break; + case IS_NOT_FALSE: + match = (values[i] == true); + break; + default: + elog(ERROR, "booltestsel: unexpected booltesttype %d", + (int) btest->booltesttype); + return (Datum) 0; /* keep compiler quiet */ + } + if (match == true) + break; + } + if (match == true) + { + /* + * Constant is "=" to this common value. We know + * selectivity exactly (or as exactly as VACUUM + * could calculate it, anyway). + */ + selec = numbers[i]; + } + else + { + /* + * Constant is not null and not "=" to this common + * value. Since only true, false, and null are + * possible, calculate it. + */ + selec = 1.0 - numbers[i] - stats->stanullfrac; + } + } + } + else + { + /* + * no most-common-value info available + */ + values = NULL; + numbers = NULL; + nvalues = nnumbers = 0; + } + + ReleaseSysCache(statsTuple); + } + else + { + /* + * No VACUUM ANALYZE stats available, so make a guess using + * the number of distinct values (2) and assuming they are + * equally common. (The guess is unlikely to be very good, + * but we do know a few special cases.) + */ + selec = 0.5; + } + + /* result should be in range, but make sure... */ + if (selec < 0.0) + selec = 0.0; + else if (selec > 1.0) + selec = 1.0; + + PG_RETURN_FLOAT8((float8) selec); +} + +/* * eqjoinsel - Join selectivity of "=" */ Datum diff -Naur pgsql.virg/src/include/utils/builtins.h pgsql.dev/src/include/utils/builtins.h --- pgsql.virg/src/include/utils/builtins.h Thu Jun 21 04:18:21 2001 +++ pgsql.dev/src/include/utils/builtins.h Sat Jun 23 15:35:02 2001 @@ -355,6 +355,7 @@ extern Datum icregexnesel(PG_FUNCTION_ARGS); extern Datum nlikesel(PG_FUNCTION_ARGS); extern Datum icnlikesel(PG_FUNCTION_ARGS); +extern Datum booltestsel(PG_FUNCTION_ARGS); extern Datum eqjoinsel(PG_FUNCTION_ARGS); extern Datum neqjoinsel(PG_FUNCTION_ARGS);