Skip to main content

uqa_execution/query/subqueries/
analysis.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capture catalog and name resolution for SQL-owned correlation analysis.
8
9use super::SubqueryServices;
10use crate::query::CteScope;
11use uqa_sql::binding::correlation::{CorrelationContext, DecorrelatedExistsPlan};
12use uqa_sql::{plan::QueryPlan, SQLError};
13
14pub(super) fn decorrelate_exists<S: Clone + 'static>(
15    services: &SubqueryServices<'_, S>,
16    scope: &CteScope<S>,
17    plan: &QueryPlan,
18) -> Result<Option<DecorrelatedExistsPlan>, SQLError> {
19    let catalog = services.catalog.catalog_snapshot();
20    let resolution = services.session.relation_name_resolution();
21    uqa_sql::binding::correlation::decorrelate_exists(
22        CorrelationContext {
23            routines: services.routines,
24            catalog: &catalog,
25            resolution: &resolution,
26            scoped_relations: Some(scope),
27        },
28        plan,
29    )
30}
31
32pub(super) fn query_depends_on_outer_row<S: Clone + 'static>(
33    services: &SubqueryServices<'_, S>,
34    scope: &CteScope<S>,
35    plan: &QueryPlan,
36) -> Result<bool, SQLError> {
37    let catalog = services.catalog.catalog_snapshot();
38    let resolution = services.session.relation_name_resolution();
39    uqa_sql::binding::correlation::query_depends_on_outer_row(
40        CorrelationContext {
41            routines: services.routines,
42            catalog: &catalog,
43            resolution: &resolution,
44            scoped_relations: Some(scope),
45        },
46        plan,
47    )
48}
49
50impl<S: Clone> uqa_sql::binding::correlation::CorrelationRelations for CteScope<S> {
51    fn columns(&self, relation: &str) -> Option<&[String]> {
52        self.rows
53            .get(relation)
54            .map(|rows| rows.row_schema().columns())
55    }
56}