98 bool case_insensitive);
100 bool case_insensitive,
101 int fixed_prefix_len);
258 bool collation_aware;
271 ((
Const *) rightop)->constisnull)
273 patt = (
Const *) rightop;
298 if (opfamily == TEXT_PATTERN_BTREE_FAM_OID)
300 eqopr = TextEqualOperator;
301 ltopr = TextPatternLessOperator;
302 geopr = TextPatternGreaterEqualOperator;
303 collation_aware =
false;
305 else if (opfamily == TEXT_SPGIST_FAM_OID)
307 eqopr = TextEqualOperator;
308 ltopr = TextPatternLessOperator;
309 geopr = TextPatternGreaterEqualOperator;
311 preopr = TextPrefixOperator;
312 collation_aware =
false;
316 eqopr = TextEqualOperator;
317 ltopr = TextLessOperator;
318 geopr = TextGreaterEqualOperator;
319 collation_aware =
true;
329 eqopr = NameEqualTextOperator;
330 ltopr = NameLessTextOperator;
331 geopr = NameGreaterEqualTextOperator;
332 collation_aware =
true;
336 if (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID)
338 eqopr = BpcharEqualOperator;
339 ltopr = BpcharPatternLessOperator;
340 geopr = BpcharPatternGreaterEqualOperator;
341 collation_aware =
false;
345 eqopr = BpcharEqualOperator;
346 ltopr = BpcharLessOperator;
347 geopr = BpcharGreaterEqualOperator;
348 collation_aware =
true;
350 rdatatype = BPCHAROID;
353 eqopr = ByteaEqualOperator;
354 ltopr = ByteaLessOperator;
355 geopr = ByteaGreaterEqualOperator;
356 collation_aware =
false;
357 rdatatype = BYTEAOID;
374 rdatatype == BPCHAROID);
391 if (indexcollation != expr_coll)
432 if (collation_aware &&
461 (
Expr *) leftop, (
Expr *) greaterstr,
463 result =
lappend(result, expr);
504 Const *prefix = NULL;
506 double nullfrac = 0.0;
523 &vardata, &other, &varonleft))
525 if (!varonleft || !
IsA(other,
Const))
535 if (((
Const *) other)->constisnull)
540 constval = ((
Const *) other)->constvalue;
541 consttype = ((
Const *) other)->consttype;
549 if (consttype != TEXTOID && consttype != BYTEAOID)
567 eqopr = TextEqualOperator;
568 ltopr = TextLessOperator;
569 geopr = TextGreaterEqualOperator;
578 eqopr = NameEqualTextOperator;
579 ltopr = NameLessTextOperator;
580 geopr = NameGreaterEqualTextOperator;
584 eqopr = BpcharEqualOperator;
585 ltopr = BpcharLessOperator;
586 geopr = BpcharGreaterEqualOperator;
587 rdatatype = BPCHAROID;
590 eqopr = ByteaEqualOperator;
591 ltopr = ByteaLessOperator;
592 geopr = ByteaGreaterEqualOperator;
593 rdatatype = BYTEAOID;
609 nullfrac = stats->stanullfrac;
621 patt = (
Const *) other;
623 &prefix, &rest_selec);
631 if (prefix && prefix->
consttype != rdatatype)
634 rdatatype == BPCHAROID);
643 result =
var_eq_const(&vardata, eqopr, collation, prefix->constvalue,
703 double hist_weight = hist_size / 100.0;
705 selec = selec * hist_weight + heursel * (1.0 - hist_weight);
712 else if (selec > 0.9999)
730 selec *= 1.0 - nullfrac - sumcommon;
737 result = 1.0 - result - nullfrac;
773 elog(
ERROR,
"patternsel called for operator without a negator");
1002 Assert(
typeid == BYTEAOID ||
typeid == TEXTOID);
1004 if (case_insensitive)
1006 if (
typeid == BYTEAOID)
1008 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1009 errmsg(
"case insensitive matching not supported on type bytea")));
1018 (
errcode(ERRCODE_INDETERMINATE_COLLATION),
1019 errmsg(
"could not determine which collation to use for ILIKE"),
1020 errhint(
"Use the COLLATE clause to set the collation explicitly.")));
1026 if (
typeid != BYTEAOID)
1029 pattlen = strlen(patt);
1036 patt = (
char *)
palloc(pattlen);
1041 match =
palloc(pattlen + 1);
1043 for (pos = 0; pos < pattlen; pos++)
1046 if (patt[pos] ==
'%' ||
1051 if (patt[pos] ==
'\\')
1059 if (case_insensitive &&
1063 match[match_pos++] = patt[pos];
1066 match[match_pos] =
'\0';
1068 if (
typeid != BYTEAOID)
1073 if (rest_selec != NULL)
1103 if (
typeid == BYTEAOID)
1105 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1106 errmsg(
"regular-expression matching not supported on type bytea")));
1110 case_insensitive, collation,
1115 *prefix_const = NULL;
1117 if (rest_selec != NULL)
1132 if (rest_selec != NULL)
1168 prefix, rest_selec);
1172 prefix, rest_selec);
1176 prefix, rest_selec);
1180 prefix, rest_selec);
1194 if (rest_selec != NULL)
1198 elog(
ERROR,
"unrecognized ptype: %d", (
int) ptype);
1231 Const *greaterstrcon;
1238 geopr, &opproc,
true,
true,
1240 prefixcon->constvalue,
1259 ltopr, &opproc,
false,
false,
1261 greaterstrcon->constvalue,
1289 eq_sel =
var_eq_const(vardata, eqopr, collation, prefixcon->constvalue,
1290 false,
true,
false);
1308#define FIXED_CHAR_SEL 0.20
1309#define CHAR_RANGE_SEL 0.25
1310#define ANY_CHAR_SEL 0.9
1311#define FULL_WILDCARD_SEL 5.0
1312#define PARTIAL_WILDCARD_SEL 2.0
1321 for (pos = 0; pos < pattlen; pos++)
1323 if (patt[pos] !=
'%' && patt[pos] !=
'_')
1327 for (; pos < pattlen; pos++)
1330 if (patt[pos] ==
'%')
1332 else if (patt[pos] ==
'_')
1334 else if (patt[pos] ==
'\\')
1355 int paren_depth = 0;
1362 for (pos = 0; pos < pattlen; pos++)
1364 if (patt[pos] ==
'(')
1366 if (paren_depth == 0)
1370 else if (patt[pos] ==
')' && paren_depth > 0)
1373 if (paren_depth == 0)
1375 pos - (paren_pos + 1),
1378 else if (patt[pos] ==
'|' && paren_depth == 0)
1385 pattlen - (pos + 1),
1389 else if (patt[pos] ==
'[')
1391 bool negclass =
false;
1393 if (patt[++pos] ==
'^')
1398 if (patt[pos] ==
']')
1400 while (pos < pattlen && patt[pos] !=
']')
1402 if (paren_depth == 0)
1405 else if (patt[pos] ==
'.')
1407 if (paren_depth == 0)
1410 else if (patt[pos] ==
'*' ||
1415 if (paren_depth == 0)
1418 else if (patt[pos] ==
'{')
1420 while (pos < pattlen && patt[pos] !=
'}')
1422 if (paren_depth == 0)
1425 else if (patt[pos] ==
'\\')
1431 if (paren_depth == 0)
1436 if (paren_depth == 0)
1448 int fixed_prefix_len)
1453 if (pattlen > 0 && patt[pattlen - 1] ==
'$' &&
1454 (pattlen == 1 || patt[pattlen - 2] !=
'\\'))
1471 if (fixed_prefix_len > 0)
1497 return (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z');
1500 else if (
locale->provider != COLLPROVIDER_LIBC)
1502 (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z');
1569 char *cmptxt = NULL;
1578 if (datatype == BYTEAOID)
1586 cmpstr = str_const->constvalue;
1590 if (datatype == NAMEOID)
1592 str_const->constvalue));
1595 len = strlen(workstr);
1597 cmpstr = str_const->constvalue;
1601 static char suffixchar = 0;
1602 static Oid suffixcollation = 0;
1604 if (!suffixchar || suffixcollation != collation)
1609 if (
varstr_cmp(best, 1,
"z", 1, collation) < 0)
1611 if (
varstr_cmp(best, 1,
"y", 1, collation) < 0)
1613 if (
varstr_cmp(best, 1,
"9", 1, collation) < 0)
1616 suffixcollation = collation;
1620 if (datatype == NAMEOID)
1623 memcpy(cmptxt, workstr,
len);
1624 cmptxt[
len] = suffixchar;
1625 cmptxt[
len + 1] =
'\0';
1640 if (datatype == BYTEAOID)
1649 unsigned char *lastchar;
1652 if (datatype == BYTEAOID)
1656 lastchar = (
unsigned char *) (workstr +
len - charlen);
1666 while (charinc(lastchar, charlen))
1668 Const *workstr_const;
1670 if (datatype == BYTEAOID)
1678 workstr_const->constvalue)))
1684 return workstr_const;
1689 pfree(workstr_const);
1697 workstr[
len] =
'\0';
1722 if (datatype == NAMEOID)
1724 else if (datatype == BYTEAOID)
1749 collation = DEFAULT_COLLATION_OID;
1754 collation = C_COLLATION_OID;
1764 elog(
ERROR,
"unexpected datatype in string_to_const: %u",
1769 return makeConst(datatype, -1, collation, constlen,
1770 conval,
false,
false);
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
#define OidIsValid(objectId)
Datum datumCopy(Datum value, bool typByVal, int typLen)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
#define DatumGetByteaPP(X)
#define PG_RETURN_FLOAT8(x)
#define DatumGetTextPP(X)
#define PG_GETARG_POINTER(n)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_INT32(n)
#define PG_RETURN_POINTER(x)
#define PG_GET_COLLATION()
#define HeapTupleIsValid(tuple)
Datum icregexnesel(PG_FUNCTION_ARGS)
Datum regexnesel(PG_FUNCTION_ARGS)
static Node * like_regex_support(Node *rawreq, Pattern_Type ptype)
Datum iclikesel(PG_FUNCTION_ARGS)
Datum texticregexeq_support(PG_FUNCTION_ARGS)
static Selectivity prefix_selectivity(PlannerInfo *root, VariableStatData *vardata, Oid eqopr, Oid ltopr, Oid geopr, Oid collation, Const *prefixcon)
#define FULL_WILDCARD_SEL
Datum iclikejoinsel(PG_FUNCTION_ARGS)
Datum prefixjoinsel(PG_FUNCTION_ARGS)
static double patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
Datum regexeqsel(PG_FUNCTION_ARGS)
static Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Datum likejoinsel(PG_FUNCTION_ARGS)
static Selectivity like_selectivity(const char *patt, int pattlen, bool case_insensitive)
Datum icregexnejoinsel(PG_FUNCTION_ARGS)
static Pattern_Prefix_Status like_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
static List * match_pattern_prefix(Node *leftop, Node *rightop, Pattern_Type ptype, Oid expr_coll, Oid opfamily, Oid indexcollation)
Datum nlikejoinsel(PG_FUNCTION_ARGS)
static Datum string_to_datum(const char *str, Oid datatype)
Datum icnlikejoinsel(PG_FUNCTION_ARGS)
static Selectivity regex_selectivity_sub(const char *patt, int pattlen, bool case_insensitive)
Datum texticlike_support(PG_FUNCTION_ARGS)
Datum nlikesel(PG_FUNCTION_ARGS)
static int pattern_char_isalpha(char c, bool is_multibyte, pg_locale_t locale)
static Const * string_to_const(const char *str, Oid datatype)
#define PARTIAL_WILDCARD_SEL
Datum text_starts_with_support(PG_FUNCTION_ARGS)
static Const * string_to_bytea_const(const char *str, size_t str_len)
static Pattern_Prefix_Status regex_fixed_prefix(Const *patt_const, bool case_insensitive, Oid collation, Const **prefix_const, Selectivity *rest_selec)
static Const * make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
Datum icregexeqsel(PG_FUNCTION_ARGS)
Datum textlike_support(PG_FUNCTION_ARGS)
Datum regexnejoinsel(PG_FUNCTION_ARGS)
static bool byte_increment(unsigned char *ptr, int len)
static double patternsel_common(PlannerInfo *root, Oid oprid, Oid opfuncid, List *args, int varRelid, Oid collation, Pattern_Type ptype, bool negate)
static Selectivity regex_selectivity(const char *patt, int pattlen, bool case_insensitive, int fixed_prefix_len)
Datum icnlikesel(PG_FUNCTION_ARGS)
Datum textregexeq_support(PG_FUNCTION_ARGS)
Datum prefixsel(PG_FUNCTION_ARGS)
static double patternjoinsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
Datum likesel(PG_FUNCTION_ARGS)
Datum regexeqjoinsel(PG_FUNCTION_ARGS)
Datum icregexeqjoinsel(PG_FUNCTION_ARGS)
List * lappend(List *list, void *datum)
RegProcedure get_opcode(Oid opno)
bool get_collation_isdeterministic(Oid colloid)
bool op_in_opfamily(Oid opno, Oid opfamily)
Oid get_negator(Oid opno)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
mbcharacter_incrementer pg_database_encoding_character_incrementer(void)
int pg_mbcliplen(const char *mbstr, int len, int limit)
int pg_database_encoding_max_length(void)
void pfree(void *pointer)
Datum nameout(PG_FUNCTION_ARGS)
Datum namein(PG_FUNCTION_ARGS)
Oid exprType(const Node *expr)
static bool is_opclause(const void *clause)
static bool is_funcclause(const void *clause)
#define IsA(nodeptr, _type_)
static int list_length(const List *l)
pg_locale_t pg_newlocale_from_collation(Oid collid)
FormData_pg_statistic * Form_pg_statistic
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static char * DatumGetCString(Datum X)
static Pointer DatumGetPointer(Datum X)
static Datum CStringGetDatum(const char *X)
char * regexp_fixed_prefix(text *text_re, bool case_insensitive, Oid collation, bool *exact)
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
double var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq, Oid collation, Datum constval, Oid consttype)
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
#define ReleaseVariableStats(vardata)
#define CLAMP_PROBABILITY(p)
#define DEFAULT_MATCH_SEL
void check_stack_depth(void)
struct PlannerInfo * root
#define SET_VARSIZE(PTR, len)
#define VARSIZE_ANY_EXHDR(PTR)
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Datum byteain(PG_FUNCTION_ARGS)