1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-09 13:09:39 +03:00

Tweak selectivity and related routines to cope with domains. Per report

from Andreas Pflug.
This commit is contained in:
Tom Lane
2003-03-23 01:49:13 +00:00
parent e8b9aeea41
commit 2d0cf42cd4
2 changed files with 71 additions and 25 deletions

View File

@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.124 2002/11/01 19:33:09 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.124.2.1 2003/03/23 01:49:13 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1848,7 +1848,7 @@ expand_indexqual_conditions(List *indexquals)
* Given a fixed prefix that all the "leftop" values must have,
* generate suitable indexqual condition(s). expr_op is the original
* LIKE or regex operator; we use it to deduce the appropriate comparison
* operators.
* operators and operand datatypes.
*/
static List *
prefix_quals(Var *leftop, Oid expr_op,
@@ -1904,10 +1904,13 @@ prefix_quals(Var *leftop, Oid expr_op,
return NIL;
}
if (prefix_const->consttype != BYTEAOID)
prefix = DatumGetCString(DirectFunctionCall1(textout, prefix_const->constvalue));
/* Prefix constant is text for all except BYTEA_LIKE */
if (datatype != BYTEAOID)
prefix = DatumGetCString(DirectFunctionCall1(textout,
prefix_const->constvalue));
else
prefix = DatumGetCString(DirectFunctionCall1(byteaout, prefix_const->constvalue));
prefix = DatumGetCString(DirectFunctionCall1(byteaout,
prefix_const->constvalue));
/*
* If we found an exact-match pattern, generate an "=" indexqual.

View File

@@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.119.2.1 2003/01/22 20:17:07 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v 1.119.2.2 2003/03/23 01:49:13 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -87,6 +87,7 @@
#include "optimizer/pathnode.h"
#include "optimizer/plancat.h"
#include "optimizer/prep.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parsetree.h"
@@ -169,7 +170,8 @@ static bool get_restriction_var(List *args, int varRelid,
Var **var, Node **other,
bool *varonleft);
static void get_join_vars(List *args, Var **var1, Var **var2);
static Selectivity prefix_selectivity(Query *root, Var *var, Const *prefix);
static Selectivity prefix_selectivity(Query *root, Var *var, Oid vartype,
Const *prefix);
static Selectivity pattern_selectivity(Const *patt, Pattern_Type ptype);
static bool string_lessthan(const char *str1, const char *str2,
Oid datatype);
@@ -220,7 +222,8 @@ eqsel(PG_FUNCTION_ARGS)
* If the something is a NULL constant, assume operator is strict and
* return zero, ie, operator will never return TRUE.
*/
if (IsA(other, Const) &&((Const *) other)->constisnull)
if (IsA(other, Const) &&
((Const *) other)->constisnull)
PG_RETURN_FLOAT8(0.0);
/* get stats for the attribute, if available */
@@ -827,6 +830,8 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
bool varonleft;
Oid relid;
Datum constval;
Oid consttype;
Oid vartype;
Pattern_Prefix_Status pstatus;
Const *patt = NULL;
Const *prefix = NULL;
@@ -854,13 +859,25 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
if (((Const *) other)->constisnull)
return 0.0;
constval = ((Const *) other)->constvalue;
consttype = ((Const *) other)->consttype;
/*
* the right-hand const is type text or bytea for all supported
* operators
* The right-hand const is type text or bytea for all supported
* operators. We do not expect to see binary-compatible types here,
* since const-folding should have relabeled the const to exactly match
* the operator's declared type.
*/
Assert(((Const *) other)->consttype == TEXTOID ||
((Const *) other)->consttype == BYTEAOID);
if (consttype != TEXTOID && consttype != BYTEAOID)
return DEFAULT_MATCH_SEL;
/*
* The var, on the other hand, might be a binary-compatible type;
* particularly a domain. Try to fold it if it's not recognized
* immediately.
*/
vartype = var->vartype;
if (vartype != consttype)
vartype = getBaseType(vartype);
/* divide pattern into fixed prefix and remainder */
patt = (Const *) other;
@@ -871,12 +888,12 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
/*
* Pattern specifies an exact match, so pretend operator is '='
*/
Oid eqopr = find_operator("=", var->vartype);
Oid eqopr = find_operator("=", vartype);
List *eqargs;
if (eqopr == InvalidOid)
elog(ERROR, "patternsel: no = operator for type %u",
var->vartype);
vartype);
eqargs = makeList2(var, prefix);
result = DatumGetFloat8(DirectFunctionCall4(eqsel,
PointerGetDatum(root),
@@ -896,7 +913,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype)
Selectivity selec;
if (pstatus == Pattern_Prefix_Partial)
prefixsel = prefix_selectivity(root, var, prefix);
prefixsel = prefix_selectivity(root, var, vartype, prefix);
else
prefixsel = 1.0;
restsel = pattern_selectivity(rest, ptype);
@@ -1025,7 +1042,8 @@ booltestsel(Query *root, BoolTestType booltesttype, Node *arg, int varRelid)
if (IsA(arg, RelabelType))
arg = ((RelabelType *) arg)->arg;
if (IsA(arg, Var) &&(varRelid == 0 || varRelid == ((Var *) arg)->varno))
if (IsA(arg, Var) &&
(varRelid == 0 || varRelid == ((Var *) arg)->varno))
var = (Var *) arg;
else
{
@@ -1735,6 +1753,8 @@ mergejoinscansel(Query *root, Node *clause,
{
Var *left,
*right;
Oid lefttype,
righttype;
Oid opno,
lsortop,
rsortop,
@@ -1759,8 +1779,13 @@ mergejoinscansel(Query *root, Node *clause,
if (!right)
return; /* shouldn't happen */
/* Save the direct input types of the operator */
lefttype = exprType((Node *) left);
righttype = exprType((Node *) right);
/* Can't do anything if inputs are not Vars */
if (!IsA(left, Var) ||!IsA(right, Var))
if (!IsA(left, Var) ||
!IsA(right, Var))
return;
/* Verify mergejoinability and get left and right "<" operators */
@@ -1802,13 +1827,13 @@ mergejoinscansel(Query *root, Node *clause,
* non-default estimates, else stick with our 1.0.
*/
selec = scalarineqsel(root, leop, false, left,
rightmax, right->vartype);
rightmax, righttype);
if (selec != DEFAULT_INEQ_SEL)
*leftscan = selec;
/* And similarly for the right variable. */
selec = scalarineqsel(root, revleop, false, right,
leftmax, left->vartype);
leftmax, lefttype);
if (selec != DEFAULT_INEQ_SEL)
*rightscan = selec;
@@ -1989,6 +2014,19 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
Datum lobound, Datum hibound, Oid boundstypid,
double *scaledlobound, double *scaledhibound)
{
/*
* In present usage, we can assume that the valuetypid exactly matches
* the declared input type of the operator we are invoked for (because
* constant-folding will ensure that any Const passed to the operator
* has been reduced to the correct type). However, the boundstypid is
* the type of some variable that might be only binary-compatible with
* the declared type; in particular it might be a domain type. Must
* fold the variable type down to base type so we can recognize it.
* (But we can skip that lookup if the variable type matches the const.)
*/
if (boundstypid != valuetypid)
boundstypid = getBaseType(boundstypid);
switch (valuetypid)
{
/*
@@ -2960,13 +2998,18 @@ pattern_fixed_prefix(Const *patt, Pattern_Type ptype,
* A fixed prefix "foo" is estimated as the selectivity of the expression
* "var >= 'foo' AND var < 'fop'" (see also indxqual.c).
*
* Because of constant-folding, we can assume that the prefixcon constant's
* type exactly matches the operator's declared input type; but it's not
* safe to make the same assumption for the Var, so the type to use for the
* Var must be passed in separately.
*
* XXX Note: we make use of the upper bound to estimate operator selectivity
* even if the locale is such that we cannot rely on the upper-bound string.
* The selectivity only needs to be approximately right anyway, so it seems
* more useful to use the upper-bound code than not.
*/
static Selectivity
prefix_selectivity(Query *root, Var *var, Const *prefixcon)
prefix_selectivity(Query *root, Var *var, Oid vartype, Const *prefixcon)
{
Selectivity prefixsel;
Oid cmpopr;
@@ -2974,17 +3017,17 @@ prefix_selectivity(Query *root, Var *var, Const *prefixcon)
List *cmpargs;
Const *greaterstrcon;
cmpopr = find_operator(">=", var->vartype);
cmpopr = find_operator(">=", vartype);
if (cmpopr == InvalidOid)
elog(ERROR, "prefix_selectivity: no >= operator for type %u",
var->vartype);
vartype);
if (prefixcon->consttype != BYTEAOID)
prefix = DatumGetCString(DirectFunctionCall1(textout, prefixcon->constvalue));
else
prefix = DatumGetCString(DirectFunctionCall1(byteaout, prefixcon->constvalue));
/* If var is type NAME, must adjust type of comparison constant */
if (var->vartype == NAMEOID)
if (vartype == NAMEOID)
prefixcon = string_to_const(prefix, NAMEOID);
cmpargs = makeList2(var, prefixcon);
@@ -3005,10 +3048,10 @@ prefix_selectivity(Query *root, Var *var, Const *prefixcon)
{
Selectivity topsel;
cmpopr = find_operator("<", var->vartype);
cmpopr = find_operator("<", vartype);
if (cmpopr == InvalidOid)
elog(ERROR, "prefix_selectivity: no < operator for type %u",
var->vartype);
vartype);
cmpargs = makeList2(var, greaterstrcon);
/* Assume scalarltsel is appropriate for all supported types */
topsel = DatumGetFloat8(DirectFunctionCall4(scalarltsel,