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            catalog: &catalog,
24            resolution: &resolution,
25            scoped_relations: Some(scope),
26        },
27        plan,
28    )
29}
30
31pub(super) fn query_depends_on_outer_row<S: Clone + 'static>(
32    services: &SubqueryServices<'_, S>,
33    scope: &CteScope<S>,
34    plan: &QueryPlan,
35) -> Result<bool, SQLError> {
36    let catalog = services.catalog.catalog_snapshot();
37    let resolution = services.session.relation_name_resolution();
38    uqa_sql::binding::correlation::query_depends_on_outer_row(
39        CorrelationContext {
40            catalog: &catalog,
41            resolution: &resolution,
42            scoped_relations: Some(scope),
43        },
44        plan,
45    )
46}
47
48impl<S: Clone> uqa_sql::binding::correlation::CorrelationRelations for CteScope<S> {
49    fn columns(&self, relation: &str) -> Option<&[String]> {
50        self.rows
51            .get(relation)
52            .map(|rows| rows.row_schema().columns())
53    }
54}