ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
qual_classify.h
Go to the documentation of this file.
1/**
2 * @file qual_classify.h
3 * @brief Predicate-tree classification helpers shared by the query
4 * rewriters (the safe-query rewrite and the joint-width UCQ
5 * recogniser).
6 *
7 * These are pure, side-effect-free analyses of a @c WHERE / @c ON qual
8 * tree of base-relation @c Var nodes: flatten an @c AND tree, recognise
9 * @c Var=Var equijoins and @c Var=Const selections, collect the base Vars
10 * / varnos a sub-tree touches, and split a conjunction into the
11 * single-relation selections (pushable into a relation scan) and the
12 * cross-relation residual (joins). They encode the same notion of
13 * "structure vs. pre-filter" both rewriters rely on: an equijoin
14 * identifies variables (structure); a single-relation predicate is a
15 * selection (a pre-filter); everything else is residual.
16 *
17 * They carry no safety-specific logic, so both the safe-query and
18 * joint-width rewriters share them.
19 */
20#ifndef QUAL_CLASSIFY_H
21#define QUAL_CLASSIFY_H
22
23#include "postgres.h"
24#include "nodes/parsenodes.h"
25#include "nodes/pg_list.h"
26#include "nodes/bitmapset.h"
27
28/** @brief Walker context for @c qc_collect_vars_walker. */
29typedef struct qc_vars_ctx {
30 List *vars; ///< Deduplicated list of distinct base-level Var nodes
32
33/** @brief Walker context for @c qc_collect_varnos_walker. */
34typedef struct qc_varnos_ctx {
35 Bitmapset *varnos; ///< Set of @c varno values seen in base-level Vars
37
38/**
39 * @brief Tree walker that collects every distinct base-level Var node
40 * (@c varlevelsup == 0), deduplicated by @c (varno, varattno).
41 */
42extern bool qc_collect_vars_walker(Node *node, qc_vars_ctx *ctx);
43
44/**
45 * @brief Position of a Var inside @p vars (matched on @c (varno,
46 * varattno)); -1 if absent.
47 */
48extern int qc_var_index(List *vars, Index varno, AttrNumber varattno);
49
50/**
51 * @brief Recognise a conjunct equating two base @c Vars (the canonical
52 * equality for the operand types, through @c RelabelType casts).
53 * Fills @p *l, @p *r on match.
54 */
55extern bool qc_is_var_eq(Expr *qual, Var **l, Var **r);
56
57/**
58 * @brief Recognise a conjunct of shape @c Var=Const (either order, through
59 * @c RelabelType casts; non-NULL literal, canonical equality).
60 * Fills @p *var, @p *konst on match.
61 */
62extern bool qc_is_var_const_eq(Expr *qual, Var **var, Const **konst);
63
64/**
65 * @brief Walk @p quals as an @c AND tree, appending each @c Var=Var
66 * equijoin's two Vars (left, right) to @p *out. OR / NOT are not
67 * traversed (they are not equijoins).
68 */
69extern void qc_collect_equalities(Node *quals, List **out);
70
71/**
72 * @brief Collect the distinct base-level @c varno values referenced by a
73 * sub-tree (used to tell a single-relation selection from a
74 * cross-relation predicate).
75 */
76extern bool qc_collect_varnos_walker(Node *node, qc_varnos_ctx *ctx);
77
78/**
79 * @brief Flatten the top-level @c AND tree of a qual into a flat list of
80 * leaf conjuncts (a bare @c List is an implicit AND). The result
81 * shares pointers with the input; @c copyObject before mutating.
82 */
83extern void qc_flatten_and(Node *n, List **out);
84
85/**
86 * @brief Partition top-level conjuncts into atom-local selections and the
87 * cross-atom residual.
88 *
89 * @p per_atom_out is a caller-allocated, zero-initialised array of length
90 * @p natoms; a conjunct whose base Vars all reference a single @c varno
91 * (1..natoms) and that is non-volatile lands in that atom's list, the
92 * rest in @p *out_residual (rebuilt as @c NULL / the lone conjunct / a
93 * fresh @c AND). Volatile predicates stay in the residual (the inner
94 * @c DISTINCT must not change their evaluation count).
95 */
96extern void qc_split_quals(Node *quals, int natoms,
97 List **per_atom_out, Node **out_residual);
98
99#endif /* QUAL_CLASSIFY_H */
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,...
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.