Skip to main content

uqa_sql/catalog/stored_view/
dependencies.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Select stored view dependencies and rewrite exact routine identities in their bound plans.
8
9use super::StoredView;
10use crate::{
11    ast::FunctionBinding,
12    binding::view_dependencies::{
13        query_plan_references_function, query_plan_references_relation,
14        query_plan_references_sequence, rewrite_query_plan_routine_identity,
15    },
16};
17use std::collections::{BTreeMap, BTreeSet};
18use uqa_core::RelationIdentity;
19
20/// Canonical bound sources make dependency checks exact; malformed in-memory plans remain conservative to prevent dangling DDL.
21pub fn views_depending_on_relation(
22    views: &BTreeMap<RelationIdentity, StoredView>,
23    target: &RelationIdentity,
24) -> Vec<String> {
25    let empty_ctes = BTreeSet::new();
26    let mut dependents = views
27        .iter()
28        .filter(|(relation, view)| {
29            *relation != target && query_plan_references_relation(&view.query, target, &empty_ctes)
30        })
31        .map(|(relation, _)| relation.qualified_name())
32        .collect::<Vec<_>>();
33    dependents.sort_unstable();
34    dependents
35}
36
37pub fn views_depending_on_sequence(
38    views: &BTreeMap<RelationIdentity, StoredView>,
39    target: &RelationIdentity,
40) -> Vec<String> {
41    let mut dependents = views
42        .iter()
43        .filter(|(_, view)| query_plan_references_sequence(&view.query, target))
44        .map(|(relation, _)| relation.qualified_name())
45        .collect::<Vec<_>>();
46    dependents.sort_unstable();
47    dependents
48}
49
50/// Return type is excluded from the exact non-builtin routine identity.
51pub fn views_depending_on_function(
52    views: &BTreeMap<RelationIdentity, StoredView>,
53    target: &FunctionBinding,
54) -> Vec<String> {
55    let mut dependents = views
56        .iter()
57        .filter(|(_, view)| query_plan_references_function(&view.query, target))
58        .map(|(relation, _)| relation.qualified_name())
59        .collect::<Vec<_>>();
60    dependents.sort_unstable();
61    dependents
62}
63
64pub fn rewrite_view_routine_identity(
65    views: &mut BTreeMap<RelationIdentity, StoredView>,
66    target: &FunctionBinding,
67    new_name: &str,
68) -> Vec<RelationIdentity> {
69    let mut changed = Vec::new();
70    for (relation, view) in views {
71        if rewrite_query_plan_routine_identity(&mut view.query, target, new_name) {
72            changed.push(relation.clone());
73        }
74    }
75    changed
76}