uqa_sql/prepared/entry/
reuse.rs1use super::PreparedStatementPlan;
10use crate::prepared::composites::CompositeInputs;
11use crate::{
12 ast::FunctionBinding,
13 plan::{QueryPlan, RelationalPlan, SourcePlan, UnifiedPlan},
14 ColumnType, ScalarExpr,
15};
16use uqa_core::Value;
17
18impl PreparedStatementPlan {
19 #[must_use]
21 pub fn has_tracked_executable_dependencies(&self) -> bool {
22 let composites = &self.composite_inputs;
23 let tracked_type = |ty: &ColumnType| independent_type(ty) || composites.tracks_type(ty);
24 if self.needs_analysis
25 || !self.dependencies.routines.is_empty()
26 || !self
27 .parameter_types
28 .iter()
29 .all(|ty| ty.as_ref().is_some_and(tracked_type))
30 {
31 return false;
32 }
33 let Some(schema) = &self.result_schema else {
34 return false;
35 };
36 if !schema
37 .column_types()
38 .iter()
39 .all(|ty| ty.as_ref().is_some_and(tracked_type))
40 || (0..schema.len()).any(|index| {
41 schema.record_fields(index).is_some()
42 && schema.column_types()[index]
43 .as_ref()
44 .is_none_or(|ty| !composites.tracks_type(ty))
45 })
46 {
47 return false;
48 }
49 let tracked_relations = !self.dependencies.relations.is_empty()
50 && self.dependency_snapshot.as_ref().is_some_and(|snapshot| {
51 snapshot.global_catalog.is_some()
52 && self
53 .dependencies
54 .relations
55 .iter()
56 .all(|oid| snapshot.relations.get(oid).is_some_and(Option::is_some))
57 });
58 if !self.dependencies.relations.is_empty() && !tracked_relations {
59 return false;
60 }
61 self.plan
62 .as_ref()
63 .is_some_and(|plan| independent_plan(plan, true, tracked_relations, composites))
64 && (composites.has_inputs()
66 || independent_plan(&self.source_plan, false, tracked_relations, composites))
67 && independent_plan(&self.logical_plan, false, tracked_relations, composites)
68 }
69}
70
71fn independent_plan(
72 plan: &UnifiedPlan,
73 executable: bool,
74 tracked_relations: bool,
75 composites: &CompositeInputs,
76) -> bool {
77 match plan {
78 UnifiedPlan::Query(query) => {
79 independent_query(query, executable, tracked_relations, composites)
80 }
81 UnifiedPlan::Command(_) => false,
82 }
83}
84
85fn independent_query(
86 query: &QueryPlan,
87 executable: bool,
88 tracked_relations: bool,
89 composites: &CompositeInputs,
90) -> bool {
91 if !query.ctes.is_empty() {
92 return false;
93 }
94 let admitted =
95 |expression: &ScalarExpr| independent_expression(expression, executable, composites);
96 match &query.root {
97 RelationalPlan::QueryBlock(block) => {
98 block.from.as_ref().is_none_or(|source| {
99 tracked_relations && matches!(source, SourcePlan::Table { .. })
100 }) && block.subqueries.is_empty()
101 && block.windows.is_empty()
102 && block.locking.is_empty()
103 && block.query_expressions().all(admitted)
104 }
105 RelationalPlan::SetOp {
106 left,
107 right,
108 order_by,
109 limit,
110 offset,
111 subqueries,
112 ..
113 } => {
114 subqueries.is_empty()
115 && independent_query(left, executable, tracked_relations, composites)
116 && independent_query(right, executable, tracked_relations, composites)
117 && order_by
118 .iter()
119 .map(|order| &order.expr)
120 .chain(limit.as_deref())
121 .chain(offset.as_deref())
122 .all(admitted)
123 }
124 RelationalPlan::Values { rows, subqueries } => {
125 subqueries.is_empty() && rows.iter().flatten().all(admitted)
126 }
127 }
128}
129
130fn independent_expression(
131 expression: &ScalarExpr,
132 executable: bool,
133 composites: &CompositeInputs,
134) -> bool {
135 let mut independent = true;
136 expression.visit(&mut |part| {
137 independent &= match part {
138 ScalarExpr::Literal(value) => independent_value(value),
139 ScalarExpr::Collated { ty, .. } => independent_type(ty) || composites.tracks_type(ty),
140 ScalarExpr::Cast { ty, .. } => independent_type_name(ty) || composites.tracks_name(ty),
141 ScalarExpr::TypedLiteral {
142 value,
143 ty,
144 bound_type,
145 parameter_index,
146 ..
147 } => {
148 parameter_index.is_none()
149 && ((bound_type
150 .as_ref()
151 .is_some_and(|ty| composites.tracks_type(ty))
152 || composites.tracks_name(ty))
153 || (independent_value(value)
154 && bound_type
155 .as_ref()
156 .map_or_else(|| independent_type_name(ty), independent_type)))
157 }
158 ScalarExpr::CompositeRow {
159 bound_type,
160 binding,
161 ..
162 } => {
163 bound_type
164 .as_ref()
165 .is_some_and(|ty| composites.tracks_type(ty))
166 || composites.tracks_name(&binding.ty)
167 }
168 ScalarExpr::Func {
169 binding,
170 args,
171 distinct,
172 order_by,
173 filter,
174 ..
175 } => {
176 !*distinct
177 && order_by.is_empty()
178 && filter.is_none()
179 && binding.as_ref().map_or(!executable, |binding| {
180 tracked_field(binding, composites)
181 || (independent_binding(binding)
182 && (!executable
183 || (args.len() == binding.argument_types.len()
184 && crate::fixed_builtin_return_type(binding)
185 .is_some_and(|ty| independent_type(&ty)))))
186 })
187 }
188 ScalarExpr::Column(_)
189 | ScalarExpr::QualifiedColumn { .. }
190 | ScalarExpr::Param(_)
191 | ScalarExpr::Array(_)
192 | ScalarExpr::Binary { .. }
193 | ScalarExpr::UnaryMinus(_)
194 | ScalarExpr::Not(_)
195 | ScalarExpr::And(_)
196 | ScalarExpr::Or(_)
197 | ScalarExpr::IsNull { .. }
198 | ScalarExpr::Between { .. }
199 | ScalarExpr::InList { .. }
200 | ScalarExpr::Case { .. } => true,
201 _ => false,
202 };
203 });
204 independent
205}
206
207fn tracked_field(binding: &FunctionBinding, composites: &CompositeInputs) -> bool {
208 binding.builtin
209 && binding.object_id.is_none()
210 && binding.dispatch == Some(crate::ast::FunctionDispatch::FieldSelect)
211 && binding.invocation.is_none()
212 && binding.resolution_error.is_none()
213 && binding.composite_field().is_some_and(|field| {
214 composites.tracks_oid(field.type_oid)
215 && (independent_type(&field.result_type)
216 || composites.tracks_type(&field.result_type))
217 })
218}
219
220fn independent_binding(binding: &FunctionBinding) -> bool {
221 binding.builtin
222 && binding.object_id.is_none()
223 && binding.dispatch.is_none()
224 && binding.invocation.is_none()
225 && binding.resolution_error.is_none()
226 && binding
227 .argument_types
228 .iter()
229 .all(|ty| independent_type_name(ty))
230}
231
232fn independent_value(value: &Value) -> bool {
233 match value {
234 Value::Null
235 | Value::Bool(_)
236 | Value::Int(_)
237 | Value::Float(_)
238 | Value::Str(_)
239 | Value::FixedChar(_)
240 | Value::Bytes(_)
241 | Value::BitString(_)
242 | Value::Decimal(_)
243 | Value::Json(_)
244 | Value::JsonB(_) => true,
245 Value::Datum(datum) => crate::catalog::type_metadata::builtin_scalar_type(datum.type_oid())
246 .is_some_and(independent_type),
247 Value::Array(array) => array.elements().iter().all(independent_value),
248 _ => false,
249 }
250}
251
252fn independent_type_name(name: &str) -> bool {
253 ColumnType::from_sql_name(name).is_ok_and(|ty| independent_type(&ty))
254}
255
256fn independent_type(ty: &ColumnType) -> bool {
258 match ty {
259 ColumnType::Array(element) => independent_type(element),
260 ColumnType::SmallInteger
261 | ColumnType::Integer
262 | ColumnType::BigInteger
263 | ColumnType::Boolean
264 | ColumnType::Text
265 | ColumnType::Name
266 | ColumnType::Uuid
267 | ColumnType::Varchar(_)
268 | ColumnType::Bpchar
269 | ColumnType::Character(_)
270 | ColumnType::Real
271 | ColumnType::DoublePrecision
272 | ColumnType::Numeric { .. }
273 | ColumnType::Json
274 | ColumnType::JsonB
275 | ColumnType::Bytea
276 | ColumnType::InternalChar => true,
277 _ => false,
278 }
279}
280
281#[cfg(test)]
282mod tests;