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;
314 if (opfamily == TEXT_PATTERN_BTREE_FAM_OID)
316 eqopr = TextEqualOperator;
317 ltopr = TextPatternLessOperator;
318 geopr = TextPatternGreaterEqualOperator;
319 collation_aware =
false;
321 else if (opfamily == TEXT_SPGIST_FAM_OID)
323 eqopr = TextEqualOperator;
324 ltopr = TextPatternLessOperator;
325 geopr = TextPatternGreaterEqualOperator;
327 preopr = TextPrefixOperator;
328 collation_aware =
false;
332 eqopr = TextEqualOperator;
333 ltopr = TextLessOperator;
334 geopr = TextGreaterEqualOperator;
335 collation_aware =
true;
345 eqopr = NameEqualTextOperator;
346 ltopr = NameLessTextOperator;
347 geopr = NameGreaterEqualTextOperator;
348 collation_aware =
true;
352 if (opfamily == BPCHAR_PATTERN_BTREE_FAM_OID)
354 eqopr = BpcharEqualOperator;
355 ltopr = BpcharPatternLessOperator;
356 geopr = BpcharPatternGreaterEqualOperator;
357 collation_aware =
false;
361 eqopr = BpcharEqualOperator;
362 ltopr = BpcharLessOperator;
363 geopr = BpcharGreaterEqualOperator;
364 collation_aware =
true;
366 rdatatype = BPCHAROID;
369 eqopr = ByteaEqualOperator;
370 ltopr = ByteaLessOperator;
371 geopr = ByteaGreaterEqualOperator;
372 collation_aware =
false;
373 rdatatype = BYTEAOID;
390 rdatatype == BPCHAROID);
435 if (collation_aware &&
464 (
Expr *) leftop, (
Expr *) greaterstr,
466 result =
lappend(result, expr);
507 Const *prefix = NULL;
509 double nullfrac = 0.0;
526 &vardata, &other, &varonleft))
528 if (!varonleft || !
IsA(other,
Const))
538 if (((
Const *) other)->constisnull)
543 constval = ((
Const *) other)->constvalue;
544 consttype = ((
Const *) other)->consttype;
552 if (consttype != TEXTOID && consttype != BYTEAOID)
570 eqopr = TextEqualOperator;
571 ltopr = TextLessOperator;
572 geopr = TextGreaterEqualOperator;
581 eqopr = NameEqualTextOperator;
582 ltopr = NameLessTextOperator;
583 geopr = NameGreaterEqualTextOperator;
587 eqopr = BpcharEqualOperator;
588 ltopr = BpcharLessOperator;
589 geopr = BpcharGreaterEqualOperator;
590 rdatatype = BPCHAROID;
593 eqopr = ByteaEqualOperator;
594 ltopr = ByteaLessOperator;
595 geopr = ByteaGreaterEqualOperator;
596 rdatatype = BYTEAOID;
612 nullfrac = stats->stanullfrac;
624 patt = (
Const *) other;
626 &prefix, &rest_selec);
634 if (prefix && prefix->
consttype != rdatatype)
637 rdatatype == BPCHAROID);
646 result =
var_eq_const(&vardata, eqopr, collation, prefix->constvalue,
706 double hist_weight = hist_size / 100.0;
708 selec = selec * hist_weight + heursel * (1.0 - hist_weight);
715 else if (selec > 0.9999)
733 selec *= 1.0 - nullfrac - sumcommon;
740 result = 1.0 - result - nullfrac;
776 elog(
ERROR,
"patternsel called for operator without a negator");
1003 bool locale_is_c =
false;
1006 Assert(
typeid == BYTEAOID ||
typeid == TEXTOID);
1008 if (case_insensitive)
1010 if (
typeid == BYTEAOID)
1012 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1013 errmsg(
"case insensitive matching not supported on type bytea")));
1022 (
errcode(ERRCODE_INDETERMINATE_COLLATION),
1023 errmsg(
"could not determine which collation to use for ILIKE"),
1024 errhint(
"Use the COLLATE clause to set the collation explicitly.")));
1034 if (
typeid != BYTEAOID)
1037 pattlen = strlen(patt);
1044 patt = (
char *)
palloc(pattlen);
1049 match =
palloc(pattlen + 1);
1051 for (pos = 0; pos < pattlen; pos++)
1054 if (patt[pos] ==
'%' ||
1059 if (patt[pos] ==
'\\')
1067 if (case_insensitive &&
1071 match[match_pos++] = patt[pos];
1074 match[match_pos] =
'\0';
1076 if (
typeid != BYTEAOID)
1081 if (rest_selec != NULL)
1111 if (
typeid == BYTEAOID)
1113 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1114 errmsg(
"regular-expression matching not supported on type bytea")));
1118 case_insensitive, collation,
1123 *prefix_const = NULL;
1125 if (rest_selec != NULL)
1140 if (rest_selec != NULL)
1176 prefix, rest_selec);
1180 prefix, rest_selec);
1184 prefix, rest_selec);
1188 prefix, rest_selec);
1202 if (rest_selec != NULL)
1206 elog(
ERROR,
"unrecognized ptype: %d", (
int) ptype);
1239 Const *greaterstrcon;
1246 geopr, &opproc,
true,
true,
1248 prefixcon->constvalue,
1267 ltopr, &opproc,
false,
false,
1269 greaterstrcon->constvalue,
1297 eq_sel =
var_eq_const(vardata, eqopr, collation, prefixcon->constvalue,
1298 false,
true,
false);
1316 #define FIXED_CHAR_SEL 0.20
1317 #define CHAR_RANGE_SEL 0.25
1318 #define ANY_CHAR_SEL 0.9
1319 #define FULL_WILDCARD_SEL 5.0
1320 #define PARTIAL_WILDCARD_SEL 2.0
1329 for (pos = 0; pos < pattlen; pos++)
1331 if (patt[pos] !=
'%' && patt[pos] !=
'_')
1335 for (; pos < pattlen; pos++)
1338 if (patt[pos] ==
'%')
1340 else if (patt[pos] ==
'_')
1342 else if (patt[pos] ==
'\\')
1363 int paren_depth = 0;
1370 for (pos = 0; pos < pattlen; pos++)
1372 if (patt[pos] ==
'(')
1374 if (paren_depth == 0)
1378 else if (patt[pos] ==
')' && paren_depth > 0)
1381 if (paren_depth == 0)
1383 pos - (paren_pos + 1),
1386 else if (patt[pos] ==
'|' && paren_depth == 0)
1393 pattlen - (pos + 1),
1397 else if (patt[pos] ==
'[')
1399 bool negclass =
false;
1401 if (patt[++pos] ==
'^')
1406 if (patt[pos] ==
']')
1408 while (pos < pattlen && patt[pos] !=
']')
1410 if (paren_depth == 0)
1413 else if (patt[pos] ==
'.')
1415 if (paren_depth == 0)
1418 else if (patt[pos] ==
'*' ||
1423 if (paren_depth == 0)
1426 else if (patt[pos] ==
'{')
1428 while (pos < pattlen && patt[pos] !=
'}')
1430 if (paren_depth == 0)
1433 else if (patt[pos] ==
'\\')
1439 if (paren_depth == 0)
1444 if (paren_depth == 0)
1456 int fixed_prefix_len)
1461 if (pattlen > 0 && patt[pattlen - 1] ==
'$' &&
1462 (pattlen == 1 || patt[pattlen - 2] !=
'\\'))
1479 if (fixed_prefix_len > 0)
1505 return (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z');
1508 else if (
locale &&
locale->provider == COLLPROVIDER_ICU)
1510 (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z');
1511 else if (
locale &&
locale->provider == COLLPROVIDER_LIBC)
1514 return isalpha((
unsigned char)
c);
1579 char *cmptxt = NULL;
1588 if (datatype == BYTEAOID)
1596 cmpstr = str_const->constvalue;
1600 if (datatype == NAMEOID)
1602 str_const->constvalue));
1605 len = strlen(workstr);
1607 cmpstr = str_const->constvalue;
1611 static char suffixchar = 0;
1612 static Oid suffixcollation = 0;
1614 if (!suffixchar || suffixcollation != collation)
1619 if (
varstr_cmp(best, 1,
"z", 1, collation) < 0)
1621 if (
varstr_cmp(best, 1,
"y", 1, collation) < 0)
1623 if (
varstr_cmp(best, 1,
"9", 1, collation) < 0)
1626 suffixcollation = collation;
1630 if (datatype == NAMEOID)
1633 memcpy(cmptxt, workstr,
len);
1634 cmptxt[
len] = suffixchar;
1635 cmptxt[
len + 1] =
'\0';
1650 if (datatype == BYTEAOID)
1659 unsigned char *lastchar;
1662 if (datatype == BYTEAOID)
1666 lastchar = (
unsigned char *) (workstr +
len - charlen);
1676 while (charinc(lastchar, charlen))
1678 Const *workstr_const;
1680 if (datatype == BYTEAOID)
1688 workstr_const->constvalue)))
1694 return workstr_const;
1699 pfree(workstr_const);
1707 workstr[
len] =
'\0';
1732 if (datatype == NAMEOID)
1734 else if (datatype == BYTEAOID)
1759 collation = DEFAULT_COLLATION_OID;
1764 collation = C_COLLATION_OID;
1774 elog(
ERROR,
"unexpected datatype in string_to_const: %u",
1779 return makeConst(datatype, -1, collation, constlen,
1780 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 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)
static int pattern_char_isalpha(char c, bool is_multibyte, pg_locale_t locale, bool locale_is_c)
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)
bool lc_collate_is_c(Oid collation)
pg_locale_t pg_newlocale_from_collation(Oid collid)
bool lc_ctype_is_c(Oid collation)
FormData_pg_statistic * Form_pg_statistic
bool(* mbcharacter_incrementer)(unsigned char *mbstr, int len)
void check_stack_depth(void)
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
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)