PostgreSQL Source Code git master
Loading...
Searching...
No Matches
predtest.c File Reference
#include "postgres.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pathnodes.h"
#include "optimizer/optimizer.h"
#include "utils/array.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for predtest.c:

Go to the source code of this file.

Data Structures

struct  PredIterInfoData
 
struct  ArrayConstIterState
 
struct  ArrayExprIterState
 
struct  OprProofCacheKey
 
struct  OprProofCacheEntry
 

Macros

#define MAX_SAOP_ARRAY_SIZE   100
 
#define iterate_begin(item, clause, info)
 
#define iterate_end(info)
 
#define RCLT   COMPARE_LT
 
#define RCLE   COMPARE_LE
 
#define RCEQ   COMPARE_EQ
 
#define RCGE   COMPARE_GE
 
#define RCGT   COMPARE_GT
 
#define RCNE   COMPARE_NE
 
#define none   0
 

Typedefs

typedef struct PredIterInfoDataPredIterInfo
 
typedef struct PredIterInfoData PredIterInfoData
 
typedef struct OprProofCacheKey OprProofCacheKey
 
typedef struct OprProofCacheEntry OprProofCacheEntry
 

Enumerations

enum  PredClass { CLASS_ATOM , CLASS_AND , CLASS_OR }
 

Functions

static bool predicate_implied_by_recurse (Node *clause, Node *predicate, bool weak)
 
static bool predicate_refuted_by_recurse (Node *clause, Node *predicate, bool weak)
 
static PredClass predicate_classify (Node *clause, PredIterInfo info)
 
static void list_startup_fn (Node *clause, PredIterInfo info)
 
static Nodelist_next_fn (PredIterInfo info)
 
static void list_cleanup_fn (PredIterInfo info)
 
static void boolexpr_startup_fn (Node *clause, PredIterInfo info)
 
static void arrayconst_startup_fn (Node *clause, PredIterInfo info)
 
static Nodearrayconst_next_fn (PredIterInfo info)
 
static void arrayconst_cleanup_fn (PredIterInfo info)
 
static void arrayexpr_startup_fn (Node *clause, PredIterInfo info)
 
static Nodearrayexpr_next_fn (PredIterInfo info)
 
static void arrayexpr_cleanup_fn (PredIterInfo info)
 
static bool predicate_implied_by_simple_clause (Expr *predicate, Node *clause, bool weak)
 
static bool predicate_refuted_by_simple_clause (Expr *predicate, Node *clause, bool weak)
 
static Nodeextract_not_arg (Node *clause)
 
static Nodeextract_strong_not_arg (Node *clause)
 
static bool clause_is_strict_for (Node *clause, Node *subexpr, bool allow_false)
 
static bool operator_predicate_proof (Expr *predicate, Node *clause, bool refute_it, bool weak)
 
static bool operator_same_subexprs_proof (Oid pred_op, Oid clause_op, bool refute_it)
 
static bool operator_same_subexprs_lookup (Oid pred_op, Oid clause_op, bool refute_it)
 
static Oid get_btree_test_op (Oid pred_op, Oid clause_op, bool refute_it)
 
static void InvalidateOprProofCacheCallBack (Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
 
bool predicate_implied_by (List *predicate_list, List *clause_list, bool weak)
 
bool predicate_refuted_by (List *predicate_list, List *clause_list, bool weak)
 
static OprProofCacheEntrylookup_proof_cache (Oid pred_op, Oid clause_op, bool refute_it)
 

Variables

static const bool RC_implies_table [6][6]
 
static const bool RC_refutes_table [6][6]
 
static const CompareType RC_implic_table [6][6]
 
static const CompareType RC_refute_table [6][6]
 
static HTABOprProofCacheHash = NULL
 

Macro Definition Documentation

◆ iterate_begin

#define iterate_begin (   item,
  clause,
  info 
)
Value:
do { \
Node *item; \
(info).startup_fn((clause), &(info)); \
while ((item = (info).next_fn(&(info))) != NULL)
static int fb(int x)
Definition nodes.h:135

Definition at line 73 of file predtest.c.

74 { \
75 Node *item; \
76 (info).startup_fn((clause), &(info)); \
77 while ((item = (info).next_fn(&(info))) != NULL)

◆ iterate_end

#define iterate_end (   info)
Value:
(info).cleanup_fn(&(info)); \
} while (0)

Definition at line 79 of file predtest.c.

◆ MAX_SAOP_ARRAY_SIZE

#define MAX_SAOP_ARRAY_SIZE   100

Definition at line 41 of file predtest.c.

◆ none

#define none   0

Definition at line 1672 of file predtest.c.

◆ RCEQ

#define RCEQ   COMPARE_EQ

Definition at line 1666 of file predtest.c.

◆ RCGE

#define RCGE   COMPARE_GE

Definition at line 1667 of file predtest.c.

◆ RCGT

#define RCGT   COMPARE_GT

Definition at line 1668 of file predtest.c.

◆ RCLE

#define RCLE   COMPARE_LE

Definition at line 1665 of file predtest.c.

◆ RCLT

#define RCLT   COMPARE_LT

Definition at line 1664 of file predtest.c.

◆ RCNE

#define RCNE   COMPARE_NE

Definition at line 1669 of file predtest.c.

Typedef Documentation

◆ OprProofCacheEntry

◆ OprProofCacheKey

◆ PredIterInfo

Definition at line 58 of file predtest.c.

◆ PredIterInfoData

Enumeration Type Documentation

◆ PredClass

Enumerator
CLASS_ATOM 
CLASS_AND 
CLASS_OR 

Definition at line 51 of file predtest.c.

52{
53 CLASS_ATOM, /* expression that's not AND or OR */
54 CLASS_AND, /* expression with AND semantics */
55 CLASS_OR, /* expression with OR semantics */
56} PredClass;
PredClass
Definition predtest.c:52
@ CLASS_AND
Definition predtest.c:54
@ CLASS_OR
Definition predtest.c:55
@ CLASS_ATOM
Definition predtest.c:53

Function Documentation

◆ arrayconst_cleanup_fn()

static void arrayconst_cleanup_fn ( PredIterInfo  info)
static

Definition at line 1023 of file predtest.c.

1024{
1026
1027 pfree(state->elem_values);
1028 pfree(state->elem_nulls);
1029 list_free(state->opexpr.args);
1030 pfree(state);
1031}
void list_free(List *list)
Definition list.c:1546
void pfree(void *pointer)
Definition mcxt.c:1616

References list_free(), pfree(), and PredIterInfoData::state.

Referenced by predicate_classify().

◆ arrayconst_next_fn()

static Node * arrayconst_next_fn ( PredIterInfo  info)
static

Definition at line 1010 of file predtest.c.

1011{
1013
1014 if (state->next_elem >= state->num_elems)
1015 return NULL;
1016 state->const_expr.constvalue = state->elem_values[state->next_elem];
1017 state->const_expr.constisnull = state->elem_nulls[state->next_elem];
1018 state->next_elem++;
1019 return (Node *) &(state->opexpr);
1020}

References fb(), and PredIterInfoData::state.

Referenced by predicate_classify().

◆ arrayconst_startup_fn()

static void arrayconst_startup_fn ( Node clause,
PredIterInfo  info 
)
static

Definition at line 961 of file predtest.c.

962{
963 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
967 int16 elmlen;
968 bool elmbyval;
969 char elmalign;
970
971 /* Create working state struct */
973 info->state = state;
974
975 /* Deconstruct the array literal */
976 arrayconst = (Const *) lsecond(saop->args);
979 &elmlen, &elmbyval, &elmalign);
982 elmlen, elmbyval, elmalign,
983 &state->elem_values, &state->elem_nulls,
984 &state->num_elems);
985
986 /* Set up a dummy OpExpr to return as the per-item node */
987 state->opexpr.xpr.type = T_OpExpr;
988 state->opexpr.opno = saop->opno;
989 state->opexpr.opfuncid = saop->opfuncid;
990 state->opexpr.opresulttype = BOOLOID;
991 state->opexpr.opretset = false;
992 state->opexpr.opcollid = InvalidOid;
993 state->opexpr.inputcollid = saop->inputcollid;
994 state->opexpr.args = list_copy(saop->args);
995
996 /* Set up a dummy Const node to hold the per-element values */
997 state->const_expr.xpr.type = T_Const;
998 state->const_expr.consttype = ARR_ELEMTYPE(arrayval);
999 state->const_expr.consttypmod = -1;
1000 state->const_expr.constcollid = arrayconst->constcollid;
1001 state->const_expr.constlen = elmlen;
1002 state->const_expr.constbyval = elmbyval;
1003 lsecond(state->opexpr.args) = &state->const_expr;
1004
1005 /* Initialize iteration state */
1006 state->next_elem = 0;
1007}
#define DatumGetArrayTypeP(X)
Definition array.h:261
#define ARR_ELEMTYPE(a)
Definition array.h:292
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
int16_t int16
Definition c.h:613
#define palloc_object(type)
Definition fe_memutils.h:74
List * list_copy(const List *oldlist)
Definition list.c:1573
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2491
#define lsecond(l)
Definition pg_list.h:183
#define InvalidOid

References ScalarArrayOpExpr::args, ARR_ELEMTYPE, DatumGetArrayTypeP, deconstruct_array(), fb(), get_typlenbyvalalign(), InvalidOid, list_copy(), lsecond, ScalarArrayOpExpr::opno, palloc_object, and PredIterInfoData::state.

Referenced by predicate_classify().

◆ arrayexpr_cleanup_fn()

static void arrayexpr_cleanup_fn ( PredIterInfo  info)
static

Definition at line 1083 of file predtest.c.

1084{
1086
1087 list_free(state->opexpr.args);
1088 pfree(state);
1089}

References list_free(), pfree(), and PredIterInfoData::state.

Referenced by predicate_classify().

◆ arrayexpr_next_fn()

static Node * arrayexpr_next_fn ( PredIterInfo  info)
static

Definition at line 1071 of file predtest.c.

1072{
1074
1075 if (state->next == NULL)
1076 return NULL;
1077 lsecond(state->opexpr.args) = lfirst(state->next);
1078 state->next = lnext(info->state_list, state->next);
1079 return (Node *) &(state->opexpr);
1080}
#define lfirst(lc)
Definition pg_list.h:172
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
List * state_list
Definition predtest.c:64
struct state * next
Definition regguts.h:332

References fb(), lfirst, lnext(), lsecond, state::next, PredIterInfoData::state, and PredIterInfoData::state_list.

Referenced by predicate_classify().

◆ arrayexpr_startup_fn()

static void arrayexpr_startup_fn ( Node clause,
PredIterInfo  info 
)
static

Definition at line 1044 of file predtest.c.

1045{
1046 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1048 ArrayExpr *arrayexpr;
1049
1050 /* Create working state struct */
1052 info->state = state;
1053
1054 /* Set up a dummy OpExpr to return as the per-item node */
1055 state->opexpr.xpr.type = T_OpExpr;
1056 state->opexpr.opno = saop->opno;
1057 state->opexpr.opfuncid = saop->opfuncid;
1058 state->opexpr.opresulttype = BOOLOID;
1059 state->opexpr.opretset = false;
1060 state->opexpr.opcollid = InvalidOid;
1061 state->opexpr.inputcollid = saop->inputcollid;
1062 state->opexpr.args = list_copy(saop->args);
1063
1064 /* Initialize iteration variable to first member of ArrayExpr */
1065 arrayexpr = (ArrayExpr *) lsecond(saop->args);
1066 info->state_list = arrayexpr->elements;
1067 state->next = list_head(arrayexpr->elements);
1068}
static ListCell * list_head(const List *l)
Definition pg_list.h:128

References ScalarArrayOpExpr::args, fb(), InvalidOid, list_copy(), list_head(), lsecond, state::next, ScalarArrayOpExpr::opno, palloc_object, PredIterInfoData::state, and PredIterInfoData::state_list.

Referenced by predicate_classify().

◆ boolexpr_startup_fn()

static void boolexpr_startup_fn ( Node clause,
PredIterInfo  info 
)
static

Definition at line 940 of file predtest.c.

941{
942 info->state_list = ((BoolExpr *) clause)->args;
943 info->state = list_head(info->state_list);
944}

References list_head(), PredIterInfoData::state, and PredIterInfoData::state_list.

Referenced by predicate_classify().

◆ clause_is_strict_for()

static bool clause_is_strict_for ( Node clause,
Node subexpr,
bool  allow_false 
)
static

Definition at line 1462 of file predtest.c.

1463{
1464 ListCell *lc;
1465
1466 /* safety checks */
1467 if (clause == NULL || subexpr == NULL)
1468 return false;
1469
1470 /*
1471 * Look through any RelabelType nodes, so that we can match, say,
1472 * varcharcol with lower(varcharcol::text). (In general we could recurse
1473 * through any nullness-preserving, immutable operation.) We should not
1474 * see stacked RelabelTypes here.
1475 */
1476 if (IsA(clause, RelabelType))
1477 clause = (Node *) ((RelabelType *) clause)->arg;
1478 if (IsA(subexpr, RelabelType))
1479 subexpr = (Node *) ((RelabelType *) subexpr)->arg;
1480
1481 /* Base case */
1482 if (equal(clause, subexpr))
1483 return true;
1484
1485 /*
1486 * If we have a strict operator or function, a NULL result is guaranteed
1487 * if any input is forced NULL by subexpr. This is OK even if the op or
1488 * func isn't immutable, since it won't even be called on NULL input.
1489 */
1490 if (is_opclause(clause) &&
1491 op_strict(((OpExpr *) clause)->opno))
1492 {
1493 foreach(lc, ((OpExpr *) clause)->args)
1494 {
1495 if (clause_is_strict_for((Node *) lfirst(lc), subexpr, false))
1496 return true;
1497 }
1498 return false;
1499 }
1500 if (is_funcclause(clause) &&
1501 func_strict(((FuncExpr *) clause)->funcid))
1502 {
1503 foreach(lc, ((FuncExpr *) clause)->args)
1504 {
1505 if (clause_is_strict_for((Node *) lfirst(lc), subexpr, false))
1506 return true;
1507 }
1508 return false;
1509 }
1510
1511 /*
1512 * CoerceViaIO is strict (whether or not the I/O functions it calls are).
1513 * Likewise, ArrayCoerceExpr is strict for its array argument (regardless
1514 * of what the per-element expression is), ConvertRowtypeExpr is strict at
1515 * the row level, and CoerceToDomain is strict too. These are worth
1516 * checking mainly because it saves us having to explain to users why some
1517 * type coercions are known strict and others aren't.
1518 */
1519 if (IsA(clause, CoerceViaIO))
1520 return clause_is_strict_for((Node *) ((CoerceViaIO *) clause)->arg,
1521 subexpr, false);
1522 if (IsA(clause, ArrayCoerceExpr))
1523 return clause_is_strict_for((Node *) ((ArrayCoerceExpr *) clause)->arg,
1524 subexpr, false);
1525 if (IsA(clause, ConvertRowtypeExpr))
1526 return clause_is_strict_for((Node *) ((ConvertRowtypeExpr *) clause)->arg,
1527 subexpr, false);
1528 if (IsA(clause, CoerceToDomain))
1529 return clause_is_strict_for((Node *) ((CoerceToDomain *) clause)->arg,
1530 subexpr, false);
1531
1532 /*
1533 * ScalarArrayOpExpr is a special case. Note that we'd only reach here
1534 * with a ScalarArrayOpExpr clause if we failed to deconstruct it into an
1535 * AND or OR tree, as for example if it has too many array elements.
1536 */
1537 if (IsA(clause, ScalarArrayOpExpr))
1538 {
1539 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1540 Node *scalarnode = (Node *) linitial(saop->args);
1541 Node *arraynode = (Node *) lsecond(saop->args);
1542
1543 /*
1544 * If we can prove the scalar input to be null, and the operator is
1545 * strict, then the SAOP result has to be null --- unless the array is
1546 * empty. For an empty array, we'd get either false (for ANY) or true
1547 * (for ALL). So if allow_false = true then the proof succeeds anyway
1548 * for the ANY case; otherwise we can only make the proof if we can
1549 * prove the array non-empty.
1550 */
1551 if (clause_is_strict_for(scalarnode, subexpr, false) &&
1552 op_strict(saop->opno))
1553 {
1554 int nelems = 0;
1555
1556 if (allow_false && saop->useOr)
1557 return true; /* can succeed even if array is empty */
1558
1559 if (arraynode && IsA(arraynode, Const))
1560 {
1563
1564 /*
1565 * If array is constant NULL then we can succeed, as in the
1566 * case below.
1567 */
1568 if (arrayconst->constisnull)
1569 return true;
1570
1571 /* Otherwise, we can compute the number of elements. */
1572 arrval = DatumGetArrayTypeP(arrayconst->constvalue);
1574 }
1575 else if (arraynode && IsA(arraynode, ArrayExpr) &&
1576 !((ArrayExpr *) arraynode)->multidims)
1577 {
1578 /*
1579 * We can also reliably count the number of array elements if
1580 * the input is a non-multidim ARRAY[] expression.
1581 */
1582 nelems = list_length(((ArrayExpr *) arraynode)->elements);
1583 }
1584
1585 /* Proof succeeds if array is definitely non-empty */
1586 if (nelems > 0)
1587 return true;
1588 }
1589
1590 /*
1591 * If we can prove the array input to be null, the proof succeeds in
1592 * all cases, since ScalarArrayOpExpr will always return NULL for a
1593 * NULL array. Otherwise, we're done here.
1594 */
1595 return clause_is_strict_for(arraynode, subexpr, false);
1596 }
1597
1598 /*
1599 * When recursing into an expression, we might find a NULL constant.
1600 * That's certainly NULL, whether it matches subexpr or not.
1601 */
1602 if (IsA(clause, Const))
1603 return ((Const *) clause)->constisnull;
1604
1605 return false;
1606}
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DIMS(a)
Definition array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition arrayutils.c:57
Datum arg
Definition elog.c:1322
bool equal(const void *a, const void *b)
Definition equalfuncs.c:223
bool op_strict(Oid opno)
Definition lsyscache.c:1697
bool func_strict(Oid funcid)
Definition lsyscache.c:1981
static bool is_opclause(const void *clause)
Definition nodeFuncs.h:76
static bool is_funcclause(const void *clause)
Definition nodeFuncs.h:69
#define IsA(nodeptr, _type_)
Definition nodes.h:164
static int list_length(const List *l)
Definition pg_list.h:152
#define linitial(l)
Definition pg_list.h:178
static bool clause_is_strict_for(Node *clause, Node *subexpr, bool allow_false)
Definition predtest.c:1462

References arg, ScalarArrayOpExpr::args, ARR_DIMS, ARR_NDIM, ArrayGetNItems(), clause_is_strict_for(), DatumGetArrayTypeP, equal(), fb(), func_strict(), is_funcclause(), is_opclause(), IsA, lfirst, linitial, list_length(), lsecond, op_strict(), ScalarArrayOpExpr::opno, and ScalarArrayOpExpr::useOr.

Referenced by clause_is_strict_for(), predicate_implied_by_simple_clause(), and predicate_refuted_by_simple_clause().

◆ extract_not_arg()

static Node * extract_not_arg ( Node clause)
static

Definition at line 1388 of file predtest.c.

1389{
1390 if (clause == NULL)
1391 return NULL;
1392 if (IsA(clause, BoolExpr))
1393 {
1394 BoolExpr *bexpr = (BoolExpr *) clause;
1395
1396 if (bexpr->boolop == NOT_EXPR)
1397 return (Node *) linitial(bexpr->args);
1398 }
1399 else if (IsA(clause, BooleanTest))
1400 {
1401 BooleanTest *btest = (BooleanTest *) clause;
1402
1403 if (btest->booltesttype == IS_NOT_TRUE ||
1404 btest->booltesttype == IS_FALSE ||
1405 btest->booltesttype == IS_UNKNOWN)
1406 return (Node *) btest->arg;
1407 }
1408 return NULL;
1409}
@ IS_NOT_TRUE
Definition primnodes.h:2003
@ IS_UNKNOWN
Definition primnodes.h:2003
@ IS_FALSE
Definition primnodes.h:2003
@ NOT_EXPR
Definition primnodes.h:964

References fb(), IS_FALSE, IS_NOT_TRUE, IS_UNKNOWN, IsA, linitial, and NOT_EXPR.

Referenced by predicate_refuted_by_recurse().

◆ extract_strong_not_arg()

static Node * extract_strong_not_arg ( Node clause)
static

Definition at line 1416 of file predtest.c.

1417{
1418 if (clause == NULL)
1419 return NULL;
1420 if (IsA(clause, BoolExpr))
1421 {
1422 BoolExpr *bexpr = (BoolExpr *) clause;
1423
1424 if (bexpr->boolop == NOT_EXPR)
1425 return (Node *) linitial(bexpr->args);
1426 }
1427 else if (IsA(clause, BooleanTest))
1428 {
1429 BooleanTest *btest = (BooleanTest *) clause;
1430
1431 if (btest->booltesttype == IS_FALSE)
1432 return (Node *) btest->arg;
1433 }
1434 return NULL;
1435}

References fb(), IS_FALSE, IsA, linitial, and NOT_EXPR.

Referenced by predicate_refuted_by_recurse().

◆ get_btree_test_op()

static Oid get_btree_test_op ( Oid  pred_op,
Oid  clause_op,
bool  refute_it 
)
static

Definition at line 2332 of file predtest.c.

2333{
2335
2336 cache_entry = lookup_proof_cache(pred_op, clause_op, refute_it);
2337 if (refute_it)
2338 return cache_entry->refute_test_op;
2339 else
2340 return cache_entry->implic_test_op;
2341}
static OprProofCacheEntry * lookup_proof_cache(Oid pred_op, Oid clause_op, bool refute_it)
Definition predtest.c:2103

References fb(), and lookup_proof_cache().

Referenced by operator_predicate_proof().

◆ InvalidateOprProofCacheCallBack()

static void InvalidateOprProofCacheCallBack ( Datum  arg,
SysCacheIdentifier  cacheid,
uint32  hashvalue 
)
static

Definition at line 2348 of file predtest.c.

2350{
2351 HASH_SEQ_STATUS status;
2353
2355
2356 /* Currently we just reset all entries; hard to be smarter ... */
2358
2359 while ((hentry = (OprProofCacheEntry *) hash_seq_search(&status)) != NULL)
2360 {
2361 hentry->have_implic = false;
2362 hentry->have_refute = false;
2363 }
2364}
#define Assert(condition)
Definition c.h:945
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
static HTAB * OprProofCacheHash
Definition predtest.c:2095

References Assert, fb(), hash_seq_init(), hash_seq_search(), and OprProofCacheHash.

Referenced by lookup_proof_cache().

◆ list_cleanup_fn()

static void list_cleanup_fn ( PredIterInfo  info)
static

Definition at line 930 of file predtest.c.

931{
932 /* Nothing to clean up */
933}

Referenced by predicate_classify().

◆ list_next_fn()

static Node * list_next_fn ( PredIterInfo  info)
static

Definition at line 917 of file predtest.c.

918{
919 ListCell *l = (ListCell *) info->state;
920 Node *n;
921
922 if (l == NULL)
923 return NULL;
924 n = lfirst(l);
925 info->state = lnext(info->state_list, l);
926 return n;
927}

References fb(), lfirst, lnext(), PredIterInfoData::state, and PredIterInfoData::state_list.

Referenced by predicate_classify().

◆ list_startup_fn()

static void list_startup_fn ( Node clause,
PredIterInfo  info 
)
static

Definition at line 910 of file predtest.c.

911{
912 info->state_list = (List *) clause;
913 info->state = list_head(info->state_list);
914}
Definition pg_list.h:54

References list_head(), PredIterInfoData::state, and PredIterInfoData::state_list.

Referenced by predicate_classify().

◆ lookup_proof_cache()

static OprProofCacheEntry * lookup_proof_cache ( Oid  pred_op,
Oid  clause_op,
bool  refute_it 
)
static

Definition at line 2103 of file predtest.c.

2104{
2107 bool cfound;
2108 bool same_subexprs = false;
2110 bool found = false;
2113 ListCell *lcp,
2114 *lcc;
2115
2116 /*
2117 * Find or make a cache entry for this pair of operators.
2118 */
2119 if (OprProofCacheHash == NULL)
2120 {
2121 /* First time through: initialize the hash table */
2122 HASHCTL ctl;
2123
2124 ctl.keysize = sizeof(OprProofCacheKey);
2125 ctl.entrysize = sizeof(OprProofCacheEntry);
2126 OprProofCacheHash = hash_create("Btree proof lookup cache", 256,
2128
2129 /* Arrange to flush cache on pg_amop changes */
2132 (Datum) 0);
2133 }
2134
2135 key.pred_op = pred_op;
2136 key.clause_op = clause_op;
2138 &key,
2139 HASH_ENTER, &cfound);
2140 if (!cfound)
2141 {
2142 /* new cache entry, set it invalid */
2143 cache_entry->have_implic = false;
2144 cache_entry->have_refute = false;
2145 }
2146 else
2147 {
2148 /* pre-existing cache entry, see if we know the answer yet */
2149 if (refute_it ? cache_entry->have_refute : cache_entry->have_implic)
2151 }
2152
2153 /*
2154 * Try to find a btree opfamily containing the given operators.
2155 *
2156 * We must find a btree opfamily that contains both operators, else the
2157 * implication can't be determined. Also, the opfamily must contain a
2158 * suitable test operator taking the operators' righthand datatypes.
2159 *
2160 * If there are multiple matching opfamilies, assume we can use any one to
2161 * determine the logical relationship of the two operators and the correct
2162 * corresponding test operator. This should work for any logically
2163 * consistent opfamilies.
2164 *
2165 * Note that we can determine the operators' relationship for
2166 * same-subexprs cases even from an opfamily that lacks a usable test
2167 * operator. This can happen in cases with incomplete sets of cross-type
2168 * comparison operators.
2169 */
2171 if (clause_op_infos)
2173 else /* no point in looking */
2175
2176 foreach(lcp, pred_op_infos)
2177 {
2179 Oid opfamily_id = pred_op_info->opfamily_id;
2180
2181 foreach(lcc, clause_op_infos)
2182 {
2187
2188 /* Must find them in same opfamily */
2189 if (opfamily_id != clause_op_info->opfamily_id)
2190 continue;
2191 /* Lefttypes should match */
2192 Assert(clause_op_info->oplefttype == pred_op_info->oplefttype);
2193
2194 pred_cmptype = pred_op_info->cmptype;
2195 clause_cmptype = clause_op_info->cmptype;
2196
2197 /*
2198 * Check to see if we can make a proof for same-subexpressions
2199 * cases based on the operators' relationship in this opfamily.
2200 */
2201 if (refute_it)
2203 else
2205
2206 /*
2207 * Look up the "test" cmptype number in the implication table
2208 */
2209 if (refute_it)
2211 else
2213
2214 if (test_cmptype == 0)
2215 {
2216 /* Can't determine implication using this interpretation */
2217 continue;
2218 }
2219
2220 /*
2221 * See if opfamily has an operator for the test cmptype and the
2222 * datatypes.
2223 */
2224 if (test_cmptype == RCNE)
2225 {
2227 pred_op_info->oprighttype,
2228 clause_op_info->oprighttype,
2229 COMPARE_EQ);
2230 if (OidIsValid(test_op))
2232 }
2233 else
2234 {
2236 pred_op_info->oprighttype,
2237 clause_op_info->oprighttype,
2238 test_cmptype);
2239 }
2240
2241 if (!OidIsValid(test_op))
2242 continue;
2243
2244 /*
2245 * Last check: test_op must be immutable.
2246 *
2247 * Note that we require only the test_op to be immutable, not the
2248 * original clause_op. (pred_op is assumed to have been checked
2249 * immutable by the caller.) Essentially we are assuming that the
2250 * opfamily is consistent even if it contains operators that are
2251 * merely stable.
2252 */
2254 {
2255 found = true;
2256 break;
2257 }
2258 }
2259
2260 if (found)
2261 break;
2262 }
2263
2266
2267 if (!found)
2268 {
2269 /* couldn't find a suitable comparison operator */
2271 }
2272
2273 /*
2274 * If we think we were able to prove something about same-subexpressions
2275 * cases, check to make sure the clause_op is immutable before believing
2276 * it completely. (Usually, the clause_op would be immutable if the
2277 * pred_op is, but it's not entirely clear that this must be true in all
2278 * cases, so let's check.)
2279 */
2280 if (same_subexprs &&
2281 op_volatile(clause_op) != PROVOLATILE_IMMUTABLE)
2282 same_subexprs = false;
2283
2284 /* Cache the results, whether positive or negative */
2285 if (refute_it)
2286 {
2287 cache_entry->refute_test_op = test_op;
2288 cache_entry->same_subexprs_refutes = same_subexprs;
2289 cache_entry->have_refute = true;
2290 }
2291 else
2292 {
2293 cache_entry->implic_test_op = test_op;
2294 cache_entry->same_subexprs_implies = same_subexprs;
2295 cache_entry->have_implic = true;
2296 }
2297
2298 return cache_entry;
2299}
#define OidIsValid(objectId)
Definition c.h:860
CompareType
Definition cmptype.h:32
@ COMPARE_EQ
Definition cmptype.h:36
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
@ HASH_ENTER
Definition hsearch.h:114
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_BLOBS
Definition hsearch.h:97
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
void list_free_deep(List *list)
Definition list.c:1560
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
Definition lsyscache.c:199
List * get_op_index_interpretation(Oid opno)
Definition lsyscache.c:668
char op_volatile(Oid opno)
Definition lsyscache.c:1713
Oid get_negator(Oid opno)
Definition lsyscache.c:1753
#define NIL
Definition pg_list.h:68
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static const CompareType RC_implic_table[6][6]
Definition predtest.c:1700
static const bool RC_refutes_table[6][6]
Definition predtest.c:1687
static const bool RC_implies_table[6][6]
Definition predtest.c:1674
#define RCNE
Definition predtest.c:1669
static void InvalidateOprProofCacheCallBack(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition predtest.c:2348
static const CompareType RC_refute_table[6][6]
Definition predtest.c:1713
tree ctl
Definition radixtree.h:1838
Size keysize
Definition hsearch.h:75

References Assert, CacheRegisterSyscacheCallback(), COMPARE_EQ, ctl, fb(), get_negator(), get_op_index_interpretation(), get_opfamily_member_for_cmptype(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), InvalidateOprProofCacheCallBack(), InvalidOid, HASHCTL::keysize, lfirst, list_free_deep(), NIL, OidIsValid, op_volatile(), OprProofCacheHash, RC_implic_table, RC_implies_table, RC_refute_table, RC_refutes_table, and RCNE.

Referenced by get_btree_test_op(), and operator_same_subexprs_lookup().

◆ operator_predicate_proof()

static bool operator_predicate_proof ( Expr predicate,
Node clause,
bool  refute_it,
bool  weak 
)
static

Definition at line 1781 of file predtest.c.

1783{
1788 Oid pred_op,
1789 clause_op,
1790 test_op;
1792 *pred_rightop,
1796 *clause_const;
1797 Expr *test_expr;
1800 bool isNull;
1801 EState *estate;
1802 MemoryContext oldcontext;
1803
1804 /*
1805 * Both expressions must be binary opclauses, else we can't do anything.
1806 *
1807 * Note: in future we might extend this logic to other operator-based
1808 * constructs such as DistinctExpr. But the planner isn't very smart
1809 * about DistinctExpr in general, and this probably isn't the first place
1810 * to fix if you want to improve that.
1811 */
1812 if (!is_opclause(predicate))
1813 return false;
1815 if (list_length(pred_opexpr->args) != 2)
1816 return false;
1817 if (!is_opclause(clause))
1818 return false;
1819 clause_opexpr = (OpExpr *) clause;
1820 if (list_length(clause_opexpr->args) != 2)
1821 return false;
1822
1823 /*
1824 * If they're marked with different collations then we can't do anything.
1825 * This is a cheap test so let's get it out of the way early.
1826 */
1827 pred_collation = pred_opexpr->inputcollid;
1828 clause_collation = clause_opexpr->inputcollid;
1830 return false;
1831
1832 /* Grab the operator OIDs now too. We may commute these below. */
1833 pred_op = pred_opexpr->opno;
1834 clause_op = clause_opexpr->opno;
1835
1836 /*
1837 * We have to match up at least one pair of input expressions.
1838 */
1839 pred_leftop = (Node *) linitial(pred_opexpr->args);
1840 pred_rightop = (Node *) lsecond(pred_opexpr->args);
1843
1845 {
1847 {
1848 /* We have x op1 y and x op2 y */
1849 return operator_same_subexprs_proof(pred_op, clause_op, refute_it);
1850 }
1851 else
1852 {
1853 /* Fail unless rightops are both Consts */
1854 if (pred_rightop == NULL || !IsA(pred_rightop, Const))
1855 return false;
1858 return false;
1860 }
1861 }
1863 {
1864 /* Fail unless leftops are both Consts */
1865 if (pred_leftop == NULL || !IsA(pred_leftop, Const))
1866 return false;
1869 return false;
1871 /* Commute both operators so we can assume Consts are on the right */
1872 pred_op = get_commutator(pred_op);
1873 if (!OidIsValid(pred_op))
1874 return false;
1875 clause_op = get_commutator(clause_op);
1876 if (!OidIsValid(clause_op))
1877 return false;
1878 }
1879 else if (equal(pred_leftop, clause_rightop))
1880 {
1882 {
1883 /* We have x op1 y and y op2 x */
1884 /* Commute pred_op that we can treat this like a straight match */
1885 pred_op = get_commutator(pred_op);
1886 if (!OidIsValid(pred_op))
1887 return false;
1888 return operator_same_subexprs_proof(pred_op, clause_op, refute_it);
1889 }
1890 else
1891 {
1892 /* Fail unless pred_rightop/clause_leftop are both Consts */
1893 if (pred_rightop == NULL || !IsA(pred_rightop, Const))
1894 return false;
1897 return false;
1899 /* Commute clause_op so we can assume Consts are on the right */
1900 clause_op = get_commutator(clause_op);
1901 if (!OidIsValid(clause_op))
1902 return false;
1903 }
1904 }
1905 else if (equal(pred_rightop, clause_leftop))
1906 {
1907 /* Fail unless pred_leftop/clause_rightop are both Consts */
1908 if (pred_leftop == NULL || !IsA(pred_leftop, Const))
1909 return false;
1912 return false;
1914 /* Commute pred_op so we can assume Consts are on the right */
1915 pred_op = get_commutator(pred_op);
1916 if (!OidIsValid(pred_op))
1917 return false;
1918 }
1919 else
1920 {
1921 /* Failed to match up any of the subexpressions, so we lose */
1922 return false;
1923 }
1924
1925 /*
1926 * We have two identical subexpressions, and two other subexpressions that
1927 * are not identical but are both Consts; and we have commuted the
1928 * operators if necessary so that the Consts are on the right. We'll need
1929 * to compare the Consts' values. If either is NULL, we can't do that, so
1930 * usually the proof fails ... but in some cases we can claim success.
1931 */
1932 if (clause_const->constisnull)
1933 {
1934 /* If clause_op isn't strict, we can't prove anything */
1935 if (!op_strict(clause_op))
1936 return false;
1937
1938 /*
1939 * At this point we know that the clause returns NULL. For proof
1940 * types that assume truth of the clause, this means the proof is
1941 * vacuously true (a/k/a "false implies anything"). That's all proof
1942 * types except weak implication.
1943 */
1944 if (!(weak && !refute_it))
1945 return true;
1946
1947 /*
1948 * For weak implication, it's still possible for the proof to succeed,
1949 * if the predicate can also be proven NULL. In that case we've got
1950 * NULL => NULL which is valid for this proof type.
1951 */
1952 if (pred_const->constisnull && op_strict(pred_op))
1953 return true;
1954 /* Else the proof fails */
1955 return false;
1956 }
1957 if (pred_const->constisnull)
1958 {
1959 /*
1960 * If the pred_op is strict, we know the predicate yields NULL, which
1961 * means the proof succeeds for either weak implication or weak
1962 * refutation.
1963 */
1964 if (weak && op_strict(pred_op))
1965 return true;
1966 /* Else the proof fails */
1967 return false;
1968 }
1969
1970 /*
1971 * Lookup the constant-comparison operator using the system catalogs and
1972 * the operator implication tables.
1973 */
1974 test_op = get_btree_test_op(pred_op, clause_op, refute_it);
1975
1976 if (!OidIsValid(test_op))
1977 {
1978 /* couldn't find a suitable comparison operator */
1979 return false;
1980 }
1981
1982 /*
1983 * Evaluate the test. For this we need an EState.
1984 */
1985 estate = CreateExecutorState();
1986
1987 /* We can use the estate's working context to avoid memory leaks. */
1988 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1989
1990 /* Build expression tree */
1992 BOOLOID,
1993 false,
1994 (Expr *) pred_const,
1995 (Expr *) clause_const,
1996 InvalidOid,
1998
1999 /* Fill in opfuncids */
2001
2002 /* Prepare it for execution */
2004
2005 /* And execute it. */
2007 GetPerTupleExprContext(estate),
2008 &isNull);
2009
2010 /* Get back to outer memory context */
2011 MemoryContextSwitchTo(oldcontext);
2012
2013 /* Release all the junk we just created */
2014 FreeExecutorState(estate);
2015
2016 if (isNull)
2017 {
2018 /* Treat a null result as non-proof ... but it's a tad fishy ... */
2019 elog(DEBUG2, "null predicate test result");
2020 return false;
2021 }
2022 return DatumGetBool(test_result);
2023}
#define DEBUG2
Definition elog.h:29
#define elog(elevel,...)
Definition elog.h:226
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition execExpr.c:143
void FreeExecutorState(EState *estate)
Definition execUtils.c:197
EState * CreateExecutorState(void)
Definition execUtils.c:90
#define GetPerTupleExprContext(estate)
Definition executor.h:660
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition executor.h:439
Oid get_commutator(Oid opno)
Definition lsyscache.c:1729
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition makefuncs.c:701
void fix_opfuncids(Node *node)
Definition nodeFuncs.c:1848
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static bool operator_same_subexprs_proof(Oid pred_op, Oid clause_op, bool refute_it)
Definition predtest.c:2034
static Oid get_btree_test_op(Oid pred_op, Oid clause_op, bool refute_it)
Definition predtest.c:2332
MemoryContext es_query_cxt
Definition execnodes.h:722

References CreateExecutorState(), DatumGetBool(), DEBUG2, elog, equal(), EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fb(), fix_opfuncids(), FreeExecutorState(), get_btree_test_op(), get_commutator(), GetPerTupleExprContext, InvalidOid, is_opclause(), IsA, linitial, list_length(), lsecond, make_opclause(), MemoryContextSwitchTo(), OidIsValid, op_strict(), and operator_same_subexprs_proof().

Referenced by predicate_implied_by_simple_clause(), and predicate_refuted_by_simple_clause().

◆ operator_same_subexprs_lookup()

static bool operator_same_subexprs_lookup ( Oid  pred_op,
Oid  clause_op,
bool  refute_it 
)
static

Definition at line 2307 of file predtest.c.

2308{
2310
2311 cache_entry = lookup_proof_cache(pred_op, clause_op, refute_it);
2312 if (refute_it)
2313 return cache_entry->same_subexprs_refutes;
2314 else
2315 return cache_entry->same_subexprs_implies;
2316}

References fb(), and lookup_proof_cache().

Referenced by operator_same_subexprs_proof().

◆ operator_same_subexprs_proof()

static bool operator_same_subexprs_proof ( Oid  pred_op,
Oid  clause_op,
bool  refute_it 
)
static

Definition at line 2034 of file predtest.c.

2035{
2036 /*
2037 * A simple and general rule is that the predicate is proven if clause_op
2038 * and pred_op are the same, or refuted if they are each other's negators.
2039 * We need not check immutability since the pred_op is already known
2040 * immutable. (Actually, by this point we may have the commutator of a
2041 * known-immutable pred_op, but that should certainly be immutable too.
2042 * Likewise we don't worry whether the pred_op's negator is immutable.)
2043 *
2044 * Note: the "same" case won't get here if we actually had EXPR1 clause_op
2045 * EXPR2 and EXPR1 pred_op EXPR2, because the overall-expression-equality
2046 * test in predicate_implied_by_simple_clause would have caught it. But
2047 * we can see the same operator after having commuted the pred_op.
2048 */
2049 if (refute_it)
2050 {
2051 if (get_negator(pred_op) == clause_op)
2052 return true;
2053 }
2054 else
2055 {
2056 if (pred_op == clause_op)
2057 return true;
2058 }
2059
2060 /*
2061 * Otherwise, see if we can determine the implication by finding the
2062 * operators' relationship via some btree opfamily.
2063 */
2064 return operator_same_subexprs_lookup(pred_op, clause_op, refute_it);
2065}
static bool operator_same_subexprs_lookup(Oid pred_op, Oid clause_op, bool refute_it)
Definition predtest.c:2307

References fb(), get_negator(), and operator_same_subexprs_lookup().

Referenced by operator_predicate_proof().

◆ predicate_classify()

static PredClass predicate_classify ( Node clause,
PredIterInfo  info 
)
static

Definition at line 828 of file predtest.c.

829{
830 /* Caller should not pass us NULL, nor a RestrictInfo clause */
831 Assert(clause != NULL);
832 Assert(!IsA(clause, RestrictInfo));
833
834 /*
835 * If we see a List, assume it's an implicit-AND list; this is the correct
836 * semantics for lists of RestrictInfo nodes.
837 */
838 if (IsA(clause, List))
839 {
841 info->next_fn = list_next_fn;
843 return CLASS_AND;
844 }
845
846 /* Handle normal AND and OR boolean clauses */
847 if (is_andclause(clause))
848 {
850 info->next_fn = list_next_fn;
852 return CLASS_AND;
853 }
854 if (is_orclause(clause))
855 {
857 info->next_fn = list_next_fn;
859 return CLASS_OR;
860 }
861
862 /* Handle ScalarArrayOpExpr */
863 if (IsA(clause, ScalarArrayOpExpr))
864 {
865 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
866 Node *arraynode = (Node *) lsecond(saop->args);
867
868 /*
869 * We can break this down into an AND or OR structure, but only if we
870 * know how to iterate through expressions for the array's elements.
871 * We can do that if the array operand is a non-null constant or a
872 * simple ArrayExpr.
873 */
874 if (arraynode && IsA(arraynode, Const) &&
875 !((Const *) arraynode)->constisnull)
876 {
878 int nelems;
879
882 if (nelems <= MAX_SAOP_ARRAY_SIZE)
883 {
887 return saop->useOr ? CLASS_OR : CLASS_AND;
888 }
889 }
890 else if (arraynode && IsA(arraynode, ArrayExpr) &&
891 !((ArrayExpr *) arraynode)->multidims &&
893 {
897 return saop->useOr ? CLASS_OR : CLASS_AND;
898 }
899 }
900
901 /* None of the above, so it's an atom */
902 return CLASS_ATOM;
903}
static bool is_andclause(const void *clause)
Definition nodeFuncs.h:107
static bool is_orclause(const void *clause)
Definition nodeFuncs.h:116
static Node * arrayconst_next_fn(PredIterInfo info)
Definition predtest.c:1010
static Node * arrayexpr_next_fn(PredIterInfo info)
Definition predtest.c:1071
static void arrayexpr_startup_fn(Node *clause, PredIterInfo info)
Definition predtest.c:1044
static void boolexpr_startup_fn(Node *clause, PredIterInfo info)
Definition predtest.c:940
static void arrayconst_startup_fn(Node *clause, PredIterInfo info)
Definition predtest.c:961
#define MAX_SAOP_ARRAY_SIZE
Definition predtest.c:41
static void list_cleanup_fn(PredIterInfo info)
Definition predtest.c:930
static Node * list_next_fn(PredIterInfo info)
Definition predtest.c:917
static void arrayconst_cleanup_fn(PredIterInfo info)
Definition predtest.c:1023
static void list_startup_fn(Node *clause, PredIterInfo info)
Definition predtest.c:910
static void arrayexpr_cleanup_fn(PredIterInfo info)
Definition predtest.c:1083
void(* startup_fn)(Node *clause, PredIterInfo info)
Definition predtest.c:66
void(* cleanup_fn)(PredIterInfo info)
Definition predtest.c:70
Node *(* next_fn)(PredIterInfo info)
Definition predtest.c:68

References ScalarArrayOpExpr::args, ARR_DIMS, ARR_NDIM, arrayconst_cleanup_fn(), arrayconst_next_fn(), arrayconst_startup_fn(), arrayexpr_cleanup_fn(), arrayexpr_next_fn(), arrayexpr_startup_fn(), ArrayGetNItems(), Assert, boolexpr_startup_fn(), CLASS_AND, CLASS_ATOM, CLASS_OR, PredIterInfoData::cleanup_fn, DatumGetArrayTypeP, fb(), is_andclause(), is_orclause(), IsA, list_cleanup_fn(), list_length(), list_next_fn(), list_startup_fn(), lsecond, MAX_SAOP_ARRAY_SIZE, PredIterInfoData::next_fn, PredIterInfoData::startup_fn, and ScalarArrayOpExpr::useOr.

Referenced by predicate_implied_by_recurse(), and predicate_refuted_by_recurse().

◆ predicate_implied_by()

bool predicate_implied_by ( List predicate_list,
List clause_list,
bool  weak 
)

Definition at line 154 of file predtest.c.

156{
157 Node *p,
158 *c;
159
160 if (predicate_list == NIL)
161 return true; /* no predicate: implication is vacuous */
162 if (clause_list == NIL)
163 return false; /* no restriction: implication must fail */
164
165 /*
166 * If either input is a single-element list, replace it with its lone
167 * member; this avoids one useless level of AND-recursion. We only need
168 * to worry about this at top level, since eval_const_expressions should
169 * have gotten rid of any trivial ANDs or ORs below that.
170 */
171 if (list_length(predicate_list) == 1)
173 else
174 p = (Node *) predicate_list;
175 if (list_length(clause_list) == 1)
176 c = (Node *) linitial(clause_list);
177 else
178 c = (Node *) clause_list;
179
180 /* And away we go ... */
182}
static bool predicate_implied_by_recurse(Node *clause, Node *predicate, bool weak)
Definition predtest.c:292
char * c

References fb(), linitial, list_length(), NIL, and predicate_implied_by_recurse().

Referenced by add_predicate_to_index_quals(), build_paths_for_OR(), check_index_predicates(), choose_bitmap_and(), ConstraintImpliedByRelConstraint(), create_bitmap_scan_plan(), create_bitmap_subplan(), create_indexscan_plan(), infer_arbiter_indexes(), and test_predtest().

◆ predicate_implied_by_recurse()

static bool predicate_implied_by_recurse ( Node clause,
Node predicate,
bool  weak 
)
static

Definition at line 292 of file predtest.c.

294{
298 bool result;
299
300 /* skip through RestrictInfo */
301 Assert(clause != NULL);
302 if (IsA(clause, RestrictInfo))
303 clause = (Node *) ((RestrictInfo *) clause)->clause;
304
306
307 switch (predicate_classify(clause, &clause_info))
308 {
309 case CLASS_AND:
310 switch (pclass)
311 {
312 case CLASS_AND:
313
314 /*
315 * AND-clause => AND-clause if A implies each of B's items
316 */
317 result = true;
319 {
321 weak))
322 {
323 result = false;
324 break;
325 }
326 }
328 return result;
329
330 case CLASS_OR:
331
332 /*
333 * AND-clause => OR-clause if A implies any of B's items
334 *
335 * Needed to handle (x AND y) => ((x AND y) OR z)
336 */
337 result = false;
339 {
341 weak))
342 {
343 result = true;
344 break;
345 }
346 }
348 if (result)
349 return result;
350
351 /*
352 * Also check if any of A's items implies B
353 *
354 * Needed to handle ((x OR y) AND z) => (x OR y)
355 */
357 {
359 weak))
360 {
361 result = true;
362 break;
363 }
364 }
366 return result;
367
368 case CLASS_ATOM:
369
370 /*
371 * AND-clause => atom if any of A's items implies B
372 */
373 result = false;
375 {
377 weak))
378 {
379 result = true;
380 break;
381 }
382 }
384 return result;
385 }
386 break;
387
388 case CLASS_OR:
389 switch (pclass)
390 {
391 case CLASS_OR:
392
393 /*
394 * OR-clause => OR-clause if each of A's items implies any
395 * of B's items. Messy but can't do it any more simply.
396 */
397 result = true;
399 {
400 bool presult = false;
401
403 {
405 weak))
406 {
407 presult = true;
408 break;
409 }
410 }
412 if (!presult)
413 {
414 result = false; /* doesn't imply any of B's */
415 break;
416 }
417 }
419 return result;
420
421 case CLASS_AND:
422 case CLASS_ATOM:
423
424 /*
425 * OR-clause => AND-clause if each of A's items implies B
426 *
427 * OR-clause => atom if each of A's items implies B
428 */
429 result = true;
431 {
433 weak))
434 {
435 result = false;
436 break;
437 }
438 }
440 return result;
441 }
442 break;
443
444 case CLASS_ATOM:
445 switch (pclass)
446 {
447 case CLASS_AND:
448
449 /*
450 * atom => AND-clause if A implies each of B's items
451 */
452 result = true;
454 {
456 weak))
457 {
458 result = false;
459 break;
460 }
461 }
463 return result;
464
465 case CLASS_OR:
466
467 /*
468 * atom => OR-clause if A implies any of B's items
469 */
470 result = false;
472 {
474 weak))
475 {
476 result = true;
477 break;
478 }
479 }
481 return result;
482
483 case CLASS_ATOM:
484
485 /*
486 * atom => atom is the base case
487 */
488 return
490 clause,
491 weak);
492 }
493 break;
494 }
495
496 /* can't get here */
497 elog(ERROR, "predicate_classify returned a bogus value");
498 return false;
499}
#define ERROR
Definition elog.h:39
#define iterate_end(info)
Definition predtest.c:79
static bool predicate_implied_by_simple_clause(Expr *predicate, Node *clause, bool weak)
Definition predtest.c:1100
static PredClass predicate_classify(Node *clause, PredIterInfo info)
Definition predtest.c:828
#define iterate_begin(item, clause, info)
Definition predtest.c:73

References Assert, CLASS_AND, CLASS_ATOM, CLASS_OR, elog, ERROR, fb(), IsA, iterate_begin, iterate_end, predicate_classify(), predicate_implied_by_recurse(), and predicate_implied_by_simple_clause().

Referenced by predicate_implied_by(), predicate_implied_by_recurse(), and predicate_refuted_by_recurse().

◆ predicate_implied_by_simple_clause()

static bool predicate_implied_by_simple_clause ( Expr predicate,
Node clause,
bool  weak 
)
static

Definition at line 1100 of file predtest.c.

1102{
1103 /* Allow interrupting long proof attempts */
1105
1106 /*
1107 * A simple and general rule is that a clause implies itself, hence we
1108 * check if they are equal(); this works for any kind of expression, and
1109 * for either implication definition. (Actually, there is an implied
1110 * assumption that the functions in the expression are immutable --- but
1111 * this was checked for the predicate by the caller.)
1112 */
1113 if (equal((Node *) predicate, clause))
1114 return true;
1115
1116 /* Next we have some clause-type-specific strategies */
1117 switch (nodeTag(clause))
1118 {
1119 case T_OpExpr:
1120 {
1121 OpExpr *op = (OpExpr *) clause;
1122
1123 /*----------
1124 * For boolean x, "x = TRUE" is equivalent to "x", likewise
1125 * "x = FALSE" is equivalent to "NOT x". These can be worth
1126 * checking because, while we preferentially simplify boolean
1127 * comparisons down to "x" and "NOT x", the other form has to
1128 * be dealt with anyway in the context of index conditions.
1129 *
1130 * We could likewise check whether the predicate is boolean
1131 * equality to a constant; but there are no known use-cases
1132 * for that at the moment, assuming that the predicate has
1133 * been through constant-folding.
1134 *----------
1135 */
1136 if (op->opno == BooleanEqualOperator)
1137 {
1138 Node *rightop;
1139
1140 Assert(list_length(op->args) == 2);
1141 rightop = lsecond(op->args);
1142 /* We might never see null Consts here, but better check */
1143 if (rightop && IsA(rightop, Const) &&
1144 !((Const *) rightop)->constisnull)
1145 {
1146 Node *leftop = linitial(op->args);
1147
1149 {
1150 /* X = true implies X */
1151 if (equal(predicate, leftop))
1152 return true;
1153 }
1154 else
1155 {
1156 /* X = false implies NOT X */
1157 if (is_notclause(predicate) &&
1159 return true;
1160 }
1161 }
1162 }
1163 }
1164 break;
1165 default:
1166 break;
1167 }
1168
1169 /* ... and some predicate-type-specific ones */
1170 switch (nodeTag(predicate))
1171 {
1172 case T_NullTest:
1173 {
1175
1176 switch (predntest->nulltesttype)
1177 {
1178 case IS_NOT_NULL:
1179
1180 /*
1181 * If the predicate is of the form "foo IS NOT NULL",
1182 * and we are considering strong implication, we can
1183 * conclude that the predicate is implied if the
1184 * clause is strict for "foo", i.e., it must yield
1185 * false or NULL when "foo" is NULL. In that case
1186 * truth of the clause ensures that "foo" isn't NULL.
1187 * (Again, this is a safe conclusion because "foo"
1188 * must be immutable.) This doesn't work for weak
1189 * implication, though. Also, "row IS NOT NULL" does
1190 * not act in the simple way we have in mind.
1191 */
1192 if (!weak &&
1193 !predntest->argisrow &&
1194 clause_is_strict_for(clause,
1195 (Node *) predntest->arg,
1196 true))
1197 return true;
1198 break;
1199 case IS_NULL:
1200 break;
1201 }
1202 }
1203 break;
1204 default:
1205 break;
1206 }
1207
1208 /*
1209 * Finally, if both clauses are binary operator expressions, we may be
1210 * able to prove something using the system's knowledge about operators;
1211 * those proof rules are encapsulated in operator_predicate_proof().
1212 */
1213 return operator_predicate_proof(predicate, clause, false, weak);
1214}
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static bool is_notclause(const void *clause)
Definition nodeFuncs.h:125
static Expr * get_notclausearg(const void *notclause)
Definition nodeFuncs.h:134
#define nodeTag(nodeptr)
Definition nodes.h:139
static bool operator_predicate_proof(Expr *predicate, Node *clause, bool refute_it, bool weak)
Definition predtest.c:1781
@ IS_NULL
Definition primnodes.h:1979
@ IS_NOT_NULL
Definition primnodes.h:1979
Oid opno
Definition primnodes.h:851
List * args
Definition primnodes.h:869

References OpExpr::args, Assert, CHECK_FOR_INTERRUPTS, clause_is_strict_for(), DatumGetBool(), equal(), fb(), get_notclausearg(), IS_NOT_NULL, is_notclause(), IS_NULL, IsA, linitial, list_length(), lsecond, nodeTag, operator_predicate_proof(), and OpExpr::opno.

Referenced by predicate_implied_by_recurse().

◆ predicate_refuted_by()

bool predicate_refuted_by ( List predicate_list,
List clause_list,
bool  weak 
)

Definition at line 224 of file predtest.c.

226{
227 Node *p,
228 *c;
229
230 if (predicate_list == NIL)
231 return false; /* no predicate: no refutation is possible */
232 if (clause_list == NIL)
233 return false; /* no restriction: refutation must fail */
234
235 /*
236 * If either input is a single-element list, replace it with its lone
237 * member; this avoids one useless level of AND-recursion. We only need
238 * to worry about this at top level, since eval_const_expressions should
239 * have gotten rid of any trivial ANDs or ORs below that.
240 */
241 if (list_length(predicate_list) == 1)
243 else
244 p = (Node *) predicate_list;
245 if (list_length(clause_list) == 1)
246 c = (Node *) linitial(clause_list);
247 else
248 c = (Node *) clause_list;
249
250 /* And away we go ... */
252}
static bool predicate_refuted_by_recurse(Node *clause, Node *predicate, bool weak)
Definition predtest.c:533

References fb(), linitial, list_length(), NIL, and predicate_refuted_by_recurse().

Referenced by gen_partprune_steps_internal(), relation_excluded_by_constraints(), and test_predtest().

◆ predicate_refuted_by_recurse()

static bool predicate_refuted_by_recurse ( Node clause,
Node predicate,
bool  weak 
)
static

Definition at line 533 of file predtest.c.

535{
539 Node *not_arg;
540 bool result;
541
542 /* skip through RestrictInfo */
543 Assert(clause != NULL);
544 if (IsA(clause, RestrictInfo))
545 clause = (Node *) ((RestrictInfo *) clause)->clause;
546
548
549 switch (predicate_classify(clause, &clause_info))
550 {
551 case CLASS_AND:
552 switch (pclass)
553 {
554 case CLASS_AND:
555
556 /*
557 * AND-clause R=> AND-clause if A refutes any of B's items
558 *
559 * Needed to handle (x AND y) R=> ((!x OR !y) AND z)
560 */
561 result = false;
563 {
565 weak))
566 {
567 result = true;
568 break;
569 }
570 }
572 if (result)
573 return result;
574
575 /*
576 * Also check if any of A's items refutes B
577 *
578 * Needed to handle ((x OR y) AND z) R=> (!x AND !y)
579 */
581 {
583 weak))
584 {
585 result = true;
586 break;
587 }
588 }
590 return result;
591
592 case CLASS_OR:
593
594 /*
595 * AND-clause R=> OR-clause if A refutes each of B's items
596 */
597 result = true;
599 {
601 weak))
602 {
603 result = false;
604 break;
605 }
606 }
608 return result;
609
610 case CLASS_ATOM:
611
612 /*
613 * If B is a NOT-type clause, A R=> B if A => B's arg
614 *
615 * Since, for either type of refutation, we are starting
616 * with the premise that A is true, we can use a strong
617 * implication test in all cases. That proves B's arg is
618 * true, which is more than we need for weak refutation if
619 * B is a simple NOT, but it allows not worrying about
620 * exactly which kind of negation clause we have.
621 */
623 if (not_arg &&
625 false))
626 return true;
627
628 /*
629 * AND-clause R=> atom if any of A's items refutes B
630 */
631 result = false;
633 {
635 weak))
636 {
637 result = true;
638 break;
639 }
640 }
642 return result;
643 }
644 break;
645
646 case CLASS_OR:
647 switch (pclass)
648 {
649 case CLASS_OR:
650
651 /*
652 * OR-clause R=> OR-clause if A refutes each of B's items
653 */
654 result = true;
656 {
658 weak))
659 {
660 result = false;
661 break;
662 }
663 }
665 return result;
666
667 case CLASS_AND:
668
669 /*
670 * OR-clause R=> AND-clause if each of A's items refutes
671 * any of B's items.
672 */
673 result = true;
675 {
676 bool presult = false;
677
679 {
681 weak))
682 {
683 presult = true;
684 break;
685 }
686 }
688 if (!presult)
689 {
690 result = false; /* citem refutes nothing */
691 break;
692 }
693 }
695 return result;
696
697 case CLASS_ATOM:
698
699 /*
700 * If B is a NOT-type clause, A R=> B if A => B's arg
701 *
702 * Same logic as for the AND-clause case above.
703 */
705 if (not_arg &&
707 false))
708 return true;
709
710 /*
711 * OR-clause R=> atom if each of A's items refutes B
712 */
713 result = true;
715 {
717 weak))
718 {
719 result = false;
720 break;
721 }
722 }
724 return result;
725 }
726 break;
727
728 case CLASS_ATOM:
729
730 /*
731 * If A is a strong NOT-clause, A R=> B if B => A's arg
732 *
733 * Since A is strong, we may assume A's arg is false (not just
734 * not-true). If B weakly implies A's arg, then B can be neither
735 * true nor null, so that strong refutation is proven. If B
736 * strongly implies A's arg, then B cannot be true, so that weak
737 * refutation is proven.
738 */
740 if (not_arg &&
742 !weak))
743 return true;
744
745 switch (pclass)
746 {
747 case CLASS_AND:
748
749 /*
750 * atom R=> AND-clause if A refutes any of B's items
751 */
752 result = false;
754 {
756 weak))
757 {
758 result = true;
759 break;
760 }
761 }
763 return result;
764
765 case CLASS_OR:
766
767 /*
768 * atom R=> OR-clause if A refutes each of B's items
769 */
770 result = true;
772 {
774 weak))
775 {
776 result = false;
777 break;
778 }
779 }
781 return result;
782
783 case CLASS_ATOM:
784
785 /*
786 * If B is a NOT-type clause, A R=> B if A => B's arg
787 *
788 * Same logic as for the AND-clause case above.
789 */
791 if (not_arg &&
793 false))
794 return true;
795
796 /*
797 * atom R=> atom is the base case
798 */
799 return
801 clause,
802 weak);
803 }
804 break;
805 }
806
807 /* can't get here */
808 elog(ERROR, "predicate_classify returned a bogus value");
809 return false;
810}
static bool predicate_refuted_by_simple_clause(Expr *predicate, Node *clause, bool weak)
Definition predtest.c:1227
static Node * extract_strong_not_arg(Node *clause)
Definition predtest.c:1416
static Node * extract_not_arg(Node *clause)
Definition predtest.c:1388

References Assert, CLASS_AND, CLASS_ATOM, CLASS_OR, elog, ERROR, extract_not_arg(), extract_strong_not_arg(), fb(), IsA, iterate_begin, iterate_end, predicate_classify(), predicate_implied_by_recurse(), predicate_refuted_by_recurse(), and predicate_refuted_by_simple_clause().

Referenced by predicate_refuted_by(), and predicate_refuted_by_recurse().

◆ predicate_refuted_by_simple_clause()

static bool predicate_refuted_by_simple_clause ( Expr predicate,
Node clause,
bool  weak 
)
static

Definition at line 1227 of file predtest.c.

1229{
1230 /* Allow interrupting long proof attempts */
1232
1233 /*
1234 * A simple clause can't refute itself, so unlike the implication case,
1235 * checking for equal() clauses isn't helpful.
1236 *
1237 * But relation_excluded_by_constraints() checks for self-contradictions
1238 * in a list of clauses, so that we may get here with predicate and clause
1239 * being actually pointer-equal, and that is worth eliminating quickly.
1240 */
1241 if ((Node *) predicate == clause)
1242 return false;
1243
1244 /* Next we have some clause-type-specific strategies */
1245 switch (nodeTag(clause))
1246 {
1247 case T_NullTest:
1248 {
1249 NullTest *clausentest = (NullTest *) clause;
1250
1251 /* row IS NULL does not act in the simple way we have in mind */
1252 if (clausentest->argisrow)
1253 return false;
1254
1255 switch (clausentest->nulltesttype)
1256 {
1257 case IS_NULL:
1258 {
1259 switch (nodeTag(predicate))
1260 {
1261 case T_NullTest:
1262 {
1264
1265 /*
1266 * row IS NULL does not act in the
1267 * simple way we have in mind
1268 */
1269 if (predntest->argisrow)
1270 return false;
1271
1272 /*
1273 * foo IS NULL refutes foo IS NOT
1274 * NULL, at least in the non-row case,
1275 * for both strong and weak refutation
1276 */
1277 if (predntest->nulltesttype == IS_NOT_NULL &&
1278 equal(predntest->arg, clausentest->arg))
1279 return true;
1280 }
1281 break;
1282 default:
1283 break;
1284 }
1285
1286 /*
1287 * foo IS NULL weakly refutes any predicate that
1288 * is strict for foo, since then the predicate
1289 * must yield false or NULL (and since foo appears
1290 * in the predicate, it's known immutable).
1291 */
1292 if (weak &&
1294 (Node *) clausentest->arg,
1295 true))
1296 return true;
1297
1298 return false; /* we can't succeed below... */
1299 }
1300 break;
1301 case IS_NOT_NULL:
1302 break;
1303 }
1304 }
1305 break;
1306 default:
1307 break;
1308 }
1309
1310 /* ... and some predicate-type-specific ones */
1311 switch (nodeTag(predicate))
1312 {
1313 case T_NullTest:
1314 {
1316
1317 /* row IS NULL does not act in the simple way we have in mind */
1318 if (predntest->argisrow)
1319 return false;
1320
1321 switch (predntest->nulltesttype)
1322 {
1323 case IS_NULL:
1324 {
1325 switch (nodeTag(clause))
1326 {
1327 case T_NullTest:
1328 {
1329 NullTest *clausentest = (NullTest *) clause;
1330
1331 /*
1332 * row IS NULL does not act in the
1333 * simple way we have in mind
1334 */
1335 if (clausentest->argisrow)
1336 return false;
1337
1338 /*
1339 * foo IS NOT NULL refutes foo IS NULL
1340 * for both strong and weak refutation
1341 */
1342 if (clausentest->nulltesttype == IS_NOT_NULL &&
1343 equal(clausentest->arg, predntest->arg))
1344 return true;
1345 }
1346 break;
1347 default:
1348 break;
1349 }
1350
1351 /*
1352 * When the predicate is of the form "foo IS
1353 * NULL", we can conclude that the predicate is
1354 * refuted if the clause is strict for "foo" (see
1355 * notes for implication case). That works for
1356 * either strong or weak refutation.
1357 */
1358 if (clause_is_strict_for(clause,
1359 (Node *) predntest->arg,
1360 true))
1361 return true;
1362 }
1363 break;
1364 case IS_NOT_NULL:
1365 break;
1366 }
1367
1368 return false; /* we can't succeed below... */
1369 }
1370 break;
1371 default:
1372 break;
1373 }
1374
1375 /*
1376 * Finally, if both clauses are binary operator expressions, we may be
1377 * able to prove something using the system's knowledge about operators.
1378 */
1379 return operator_predicate_proof(predicate, clause, true, weak);
1380}

References CHECK_FOR_INTERRUPTS, clause_is_strict_for(), equal(), fb(), IS_NOT_NULL, IS_NULL, nodeTag, and operator_predicate_proof().

Referenced by predicate_refuted_by_recurse().

Variable Documentation

◆ OprProofCacheHash

HTAB* OprProofCacheHash = NULL
static

Definition at line 2095 of file predtest.c.

Referenced by InvalidateOprProofCacheCallBack(), and lookup_proof_cache().

◆ RC_implic_table

const CompareType RC_implic_table[6][6]
static
Initial value:
= {
}
#define RCGE
Definition predtest.c:1667
#define RCLE
Definition predtest.c:1665
#define none
Definition predtest.c:1672
#define RCGT
Definition predtest.c:1668
#define RCLT
Definition predtest.c:1664
#define RCEQ
Definition predtest.c:1666

Definition at line 1700 of file predtest.c.

1700 {
1701/*
1702 * The predicate operator:
1703 * LT LE EQ GE GT NE
1704 */
1705 {RCGE, RCGE, none, none, none, RCGE}, /* LT */
1706 {RCGT, RCGE, none, none, none, RCGT}, /* LE */
1707 {RCGT, RCGE, RCEQ, RCLE, RCLT, RCNE}, /* EQ */
1708 {none, none, none, RCLE, RCLT, RCLT}, /* GE */
1709 {none, none, none, RCLE, RCLE, RCLE}, /* GT */
1710 {none, none, none, none, none, RCEQ} /* NE */
1711};

Referenced by lookup_proof_cache().

◆ RC_implies_table

const bool RC_implies_table[6][6]
static
Initial value:
= {
{true, true, none, none, none, true},
{none, true, none, none, none, none},
{none, true, true, true, none, none},
{none, none, none, true, none, none},
{none, none, none, true, true, true},
{none, none, none, none, none, true}
}

Definition at line 1674 of file predtest.c.

1674 {
1675/*
1676 * The predicate operator:
1677 * LT LE EQ GE GT NE
1678 */
1679 {true, true, none, none, none, true}, /* LT */
1680 {none, true, none, none, none, none}, /* LE */
1681 {none, true, true, true, none, none}, /* EQ */
1682 {none, none, none, true, none, none}, /* GE */
1683 {none, none, none, true, true, true}, /* GT */
1684 {none, none, none, none, none, true} /* NE */
1685};

Referenced by lookup_proof_cache().

◆ RC_refute_table

const CompareType RC_refute_table[6][6]
static
Initial value:

Definition at line 1713 of file predtest.c.

1713 {
1714/*
1715 * The predicate operator:
1716 * LT LE EQ GE GT NE
1717 */
1718 {none, none, RCGE, RCGE, RCGE, none}, /* LT */
1719 {none, none, RCGT, RCGT, RCGE, none}, /* LE */
1720 {RCLE, RCLT, RCNE, RCGT, RCGE, RCEQ}, /* EQ */
1721 {RCLE, RCLT, RCLT, none, none, none}, /* GE */
1722 {RCLE, RCLE, RCLE, none, none, none}, /* GT */
1723 {none, none, RCEQ, none, none, none} /* NE */
1724};

Referenced by lookup_proof_cache().

◆ RC_refutes_table

const bool RC_refutes_table[6][6]
static
Initial value:
= {
{none, none, true, true, true, none},
{none, none, none, none, true, none},
{true, none, none, none, true, true},
{true, none, none, none, none, none},
{true, true, true, none, none, none},
{none, none, true, none, none, none}
}

Definition at line 1687 of file predtest.c.

1687 {
1688/*
1689 * The predicate operator:
1690 * LT LE EQ GE GT NE
1691 */
1692 {none, none, true, true, true, none}, /* LT */
1693 {none, none, none, none, true, none}, /* LE */
1694 {true, none, none, none, true, true}, /* EQ */
1695 {true, none, none, none, none, none}, /* GE */
1696 {true, true, true, none, none, none}, /* GT */
1697 {none, none, true, none, none, none} /* NE */
1698};

Referenced by lookup_proof_cache().