7#include "nodes/parsenodes.h"
8#include "nodes/pg_list.h"
9#include "nodes/bitmapset.h"
10#include "nodes/makefuncs.h"
11#include "nodes/nodeFuncs.h"
12#if PG_VERSION_NUM >= 120000
13#include "optimizer/optimizer.h"
15#include "optimizer/clauses.h"
27 Var *v = (Var *) node;
28 if (v->varlevelsup != 0)
30 foreach (lc, ctx->
vars) {
31 Var *existing = (Var *) lfirst(lc);
32 if (existing->varno == v->varno && existing->varattno == v->varattno)
46 Var *v = (Var *) lfirst(lc);
47 if (v->varno == varno && v->varattno == varattno)
61 if (!IsA(qual, OpExpr))
64 if (list_length(op->args) != 2)
66 ln = (Node *) linitial(op->args);
67 rn = (Node *) lsecond(op->args);
68 while (IsA(ln, RelabelType))
69 ln = (Node *) ((RelabelType *) ln)->arg;
70 while (IsA(rn, RelabelType))
71 rn = (Node *) ((RelabelType *) rn)->arg;
72 if (!IsA(ln, Var) || !IsA(rn, Var))
76 if (lv->varlevelsup != 0 || rv->varlevelsup != 0)
79 if (expected == InvalidOid || op->opno != expected)
94 if (!IsA(qual, OpExpr))
97 if (list_length(op->args) != 2)
99 ln = (Node *) linitial(op->args);
100 rn = (Node *) lsecond(op->args);
101 while (IsA(ln, RelabelType))
102 ln = (Node *) ((RelabelType *) ln)->arg;
103 while (IsA(rn, RelabelType))
104 rn = (Node *) ((RelabelType *) rn)->arg;
105 if (IsA(ln, Var) && IsA(rn, Const)) {
108 }
else if (IsA(ln, Const) && IsA(rn, Var)) {
114 if (v->varlevelsup != 0)
119 if (expected == InvalidOid || op->opno != expected)
130 if (IsA(quals, List)) {
132 foreach (lc, (List *) quals)
136 if (IsA(quals, BoolExpr)) {
137 BoolExpr *be = (BoolExpr *) quals;
138 if (be->boolop == AND_EXPR) {
140 foreach (lc, be->args)
148 *out = lappend(*out, l);
149 *out = lappend(*out, r);
158 if (IsA(node, Var)) {
159 Var *v = (Var *) node;
160 if (v->varlevelsup == 0 && (
int) v->varno >= 1)
161 ctx->
varnos = bms_add_member(ctx->
varnos, (
int) v->varno);
173 foreach (lc, (List *) n)
177 if (IsA(n, BoolExpr) && ((BoolExpr *) n)->boolop == AND_EXPR) {
179 foreach (lc, ((BoolExpr *) n)->args)
183 *out = lappend(*out, n);
187 List **per_atom_out, Node **out_residual)
189 List *conjuncts = NIL;
190 List *residual = NIL;
194 for (i = 0; i < natoms; i++)
195 per_atom_out[i] = NIL;
199 foreach (lc, conjuncts) {
200 Node *qual = (Node *) lfirst(lc);
204 if (contain_volatile_functions(qual)) {
205 residual = lappend(residual, qual);
210 nmembers = bms_num_members(vctx.
varnos);
212 int v = bms_singleton_member(vctx.
varnos);
213 if (v >= 1 && v <= natoms) {
214 per_atom_out[v - 1] = lappend(per_atom_out[v - 1], qual);
220 residual = lappend(residual, qual);
224 *out_residual = NULL;
225 else if (list_length(residual) == 1)
226 *out_residual = (Node *) linitial(residual);
228 *out_residual = (Node *) makeBoolExpr(AND_EXPR, residual, -1);
Oid find_equality_operator(Oid ltypeId, Oid rtypeId)
Find the equality operator OID for two given types.
Core types, constants, and utilities shared across ProvSQL.
void qc_flatten_and(Node *n, List **out)
Flatten the top-level AND tree of a qual into a flat list of leaf conjuncts (a bare List is an implic...
void qc_split_quals(Node *quals, int natoms, List **per_atom_out, Node **out_residual)
Partition top-level conjuncts into atom-local selections and the cross-atom residual.
int qc_var_index(List *vars, Index varno, AttrNumber varattno)
Position of a Var inside vars (matched on (varno, varattno)); -1 if absent.
void qc_collect_equalities(Node *quals, List **out)
Walk quals as an AND tree, appending each Var=Var equijoin's two Vars (left, right) to *out.
bool qc_collect_vars_walker(Node *node, qc_vars_ctx *ctx)
Tree walker that collects every distinct base-level Var node (varlevelsup == 0), deduplicated by (var...
bool qc_collect_varnos_walker(Node *node, qc_varnos_ctx *ctx)
Collect the distinct base-level varno values referenced by a sub-tree (used to tell a single-relation...
bool qc_is_var_eq(Expr *qual, Var **l, Var **r)
Recognise a conjunct equating two base Vars (the canonical equality for the operand types,...
bool qc_is_var_const_eq(Expr *qual, Var **var, Const **konst)
Recognise a conjunct of shape Var=Const (either order, through RelabelType casts; non-NULL literal,...
Predicate-tree classification helpers shared by the query rewriters (the safe-query rewrite and the j...
Walker context for qc_collect_varnos_walker.
Bitmapset * varnos
Set of varno values seen in base-level Vars.
Walker context for qc_collect_vars_walker.
List * vars
Deduplicated list of distinct base-level Var nodes.