1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Expressions written back as SQL for the mapping queries, and the functions a level calls.
use super::{
Oid, Resolved, Spi, Sql, Walker, collect_functions, cstr, elements, pg_sys, quote_ident,
quote_literal, tag, term_sql,
};
impl Walker<'_> {
/// Write `expr` as SQL that resolves no name through `search_path`: relations,
/// operators, functions and types are schema-qualified. `None` for a node it
/// does not write, or an `unnest` element outside a comparison. `term_of` gives
/// what each Var / Param stands for (Vars in walk order); `next_var` is called
/// after each Var.
///
/// SAFETY: `expr` is a valid expression.
pub(super) unsafe fn deparse(
&mut self,
expr: *mut pg_sys::Node,
term_of: &dyn Fn(*mut pg_sys::Node) -> Option<Resolved>,
next_var: &mut dyn FnMut(),
) -> Option<Sql> {
// SAFETY: each node is checked by tag before it is cast.
unsafe {
match tag(expr)? {
pg_sys::NodeTag::T_Var => {
let term = term_of(expr)?;
next_var();
term_sql(&term)
}
pg_sys::NodeTag::T_Param => term_sql(&term_of(expr)?),
pg_sys::NodeTag::T_Const => {
let c = expr.cast::<pg_sys::Const>();
let ty = self.type_name((*c).consttype);
if (*c).constisnull {
return Some(Sql::text(format!("NULL::{ty}")));
}
let mut output: Oid = Oid::INVALID;
let mut varlena = false;
pg_sys::getTypeOutputInfo((*c).consttype, &raw mut output, &raw mut varlena);
let text = cstr(pg_sys::OidOutputFunctionCall(output, (*c).constvalue));
Some(Sql::text(format!("{}::{ty}", quote_literal(&text)?)))
}
pg_sys::NodeTag::T_OpExpr => {
let op = expr.cast::<pg_sys::OpExpr>();
let name = self.operator_name((*op).opno);
let args = elements::<pg_sys::Node>((*op).args);
let mut sql = Sql::text("(");
match args[..] {
[arg] => {
sql.push_text(&format!("OPERATOR({name}) "));
sql.push_sql(self.deparse(arg, term_of, next_var)?);
}
[l, r] => {
sql.push_sql(self.deparse(l, term_of, next_var)?);
sql.push_text(&format!(" OPERATOR({name}) "));
sql.push_sql(self.deparse(r, term_of, next_var)?);
}
_ => return None,
}
sql.push_text(")");
Some(sql)
}
pg_sys::NodeTag::T_ScalarArrayOpExpr => {
let op = expr.cast::<pg_sys::ScalarArrayOpExpr>();
let name = self.operator_name((*op).opno);
let [l, r] = elements::<pg_sys::Node>((*op).args)[..] else {
return None;
};
let mut sql = Sql::text("(");
sql.push_sql(self.deparse(l, term_of, next_var)?);
sql.push_text(&format!(
" OPERATOR({name}) {} (",
if (*op).useOr { "ANY" } else { "ALL" }
));
sql.push_sql(self.deparse(r, term_of, next_var)?);
sql.push_text("))");
Some(sql)
}
pg_sys::NodeTag::T_FuncExpr => {
let f = expr.cast::<pg_sys::FuncExpr>();
let args = elements::<pg_sys::Node>((*f).args);
let mut sql;
if (*f).funcformat == pg_sys::CoercionForm::COERCE_EXPLICIT_CALL
|| (*f).funcformat == pg_sys::CoercionForm::COERCE_SQL_SYNTAX
{
let (name, _) = self.function((*f).funcid);
sql = Sql::text(format!("{name}("));
for (i, arg) in args.into_iter().enumerate() {
if i > 0 {
sql.push_text(", ");
}
sql.push_sql(self.deparse(arg, term_of, next_var)?);
}
sql.push_text(")");
} else {
let [arg, ..] = args[..] else { return None };
sql = Sql::text("(");
sql.push_sql(self.deparse(arg, term_of, next_var)?);
sql.push_text(&format!(")::{}", self.type_name((*f).funcresulttype)));
}
Some(sql)
}
pg_sys::NodeTag::T_RelabelType => {
let r = expr.cast::<pg_sys::RelabelType>();
let mut sql = Sql::text("(");
sql.push_sql(self.deparse((*r).arg.cast(), term_of, next_var)?);
sql.push_text(&format!(")::{}", self.type_name((*r).resulttype)));
Some(sql)
}
pg_sys::NodeTag::T_ArrayCoerceExpr => {
let r = expr.cast::<pg_sys::ArrayCoerceExpr>();
let mut sql = Sql::text("(");
sql.push_sql(self.deparse((*r).arg.cast(), term_of, next_var)?);
sql.push_text(&format!(")::{}", self.type_name((*r).resulttype)));
Some(sql)
}
pg_sys::NodeTag::T_CoerceViaIO => {
let r = expr.cast::<pg_sys::CoerceViaIO>();
let mut sql = Sql::text("(");
sql.push_sql(self.deparse((*r).arg.cast(), term_of, next_var)?);
sql.push_text(&format!(")::{}", self.type_name((*r).resulttype)));
Some(sql)
}
pg_sys::NodeTag::T_BoolExpr => {
let b = expr.cast::<pg_sys::BoolExpr>();
let args = elements::<pg_sys::Node>((*b).args);
let mut sql = Sql::text("(");
match (*b).boolop {
pg_sys::BoolExprType::NOT_EXPR => {
sql.push_text("NOT ");
sql.push_sql(self.deparse(*args.first()?, term_of, next_var)?);
}
op => {
let joiner = if op == pg_sys::BoolExprType::AND_EXPR {
" AND "
} else {
" OR "
};
for (i, arg) in args.into_iter().enumerate() {
if i > 0 {
sql.push_text(joiner);
}
sql.push_sql(self.deparse(arg, term_of, next_var)?);
}
}
}
sql.push_text(")");
Some(sql)
}
_ => None,
}
}
}
pub(super) fn type_name(&mut self, ty: Oid) -> String {
self.catalog
.types
.entry(ty.to_u32())
// SAFETY: a catalog lookup by OID.
.or_insert_with(|| cstr(unsafe { pg_sys::format_type_be_qualified(ty) }))
.clone()
}
pub(super) fn operator_name(&mut self, opno: Oid) -> String {
self.catalog
.operators
.entry(opno.to_u32())
.or_insert_with(|| {
Spi::get_one_with_args::<String>(
"SELECT pg_catalog.quote_ident(n.nspname) || '.' || o.oprname \
FROM pg_catalog.pg_operator o \
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace \
WHERE o.oid = $1",
&[crate::utils::spi::oid(opno)],
)
.ok()
.flatten()
.unwrap_or_default()
})
.clone()
}
/// The qualified name of a function, and whether it is immutable.
pub(super) fn function(&mut self, funcid: Oid) -> (String, bool) {
self.catalog
.functions
.entry(funcid.to_u32())
.or_insert_with(|| {
// SAFETY: catalog lookups by OID.
unsafe {
let name = cstr(pg_sys::get_func_name(funcid));
let nsp = cstr(pg_sys::get_namespace_name(pg_sys::get_func_namespace(
funcid,
)));
let immutable = pg_sys::func_volatile(funcid)
== pg_sys::PROVOLATILE_IMMUTABLE.cast_signed();
(
format!("{}.{}", quote_ident(&nsp), quote_ident(&name)),
immutable,
)
}
})
.clone()
}
// ── functions that may read tables ──────────────────────────────────────
/// Record the non-immutable functions outside `pg_catalog` that this level
/// calls: tables they read are invisible to `pg_tviews`.
///
/// SAFETY: `query` is the valid Query of the innermost level.
pub(super) unsafe fn note_functions(&mut self, query: *mut pg_sys::Node) {
let mut funcids = Vec::new();
// SAFETY: a read-only walk of a valid Query.
unsafe { collect_functions(query, &mut funcids) };
for funcid in funcids {
let (name, immutable) = self.function(funcid);
if !immutable
&& !name.starts_with("pg_catalog.")
&& !self.graph.untracked_functions.contains(&funcid.to_u32())
{
self.graph.untracked_functions.push(funcid.to_u32());
}
}
}
}