1use crate::{
10 capabilities::{query_scope::CteScope, ScopedEngineHook},
11 session::StatementReadSnapshot,
12 Engine,
13};
14use uqa_core::Value;
15use uqa_execution::{
16 query::{
17 sources::SourceContext,
18 statement::context::QueryContext,
19 subqueries::{
20 context::{ScopedSubqueryHooks, SubqueryProbe, SubqueryQueryContexts},
21 SubqueryServices,
22 },
23 },
24 scalar::plan::{PhysicalOuterRow, PhysicalSubqueryRunner},
25 SubqueryResult,
26};
27use uqa_sql::{plan::QueryPlan, SQLError, SQLParam};
28
29impl Engine {
30 pub(crate) fn subquery_services(&self) -> SubqueryServices<'_, StatementReadSnapshot> {
31 SubqueryServices {
32 routines: self,
33 catalog: self,
34 session: self,
35 volatility: self,
36 queries: self,
37 hooks: self,
38 }
39 }
40}
41
42impl SubqueryQueryContexts<StatementReadSnapshot> for Engine {
43 fn query_context(&self) -> QueryContext<'_, StatementReadSnapshot> {
44 self.query_execution_context()
45 }
46 fn source_context(&self) -> SourceContext<'_, StatementReadSnapshot> {
47 self.source_execution_context()
48 }
49}
50
51impl ScopedSubqueryHooks<StatementReadSnapshot> for Engine {
52 fn with_hooks(&self, scope: &CteScope, probe: SubqueryProbe<'_>) -> Result<bool, SQLError> {
53 let hook = ScopedEngineHook::new(self, scope);
54 probe(&hook, &hook)
55 }
56}
57
58impl PhysicalSubqueryRunner for ScopedEngineHook<'_> {
59 fn execute_subquery(
60 &self,
61 subquery: usize,
62 plan: &QueryPlan,
63 outer_row: PhysicalOuterRow<'_>,
64 params: &[SQLParam],
65 ) -> Result<SubqueryResult, SQLError> {
66 self.subquery_context()
67 .execute_subquery(subquery, plan, outer_row, params)
68 }
69 fn row_subquery_value(
70 &self,
71 subquery: usize,
72 plan: &QueryPlan,
73 outer_row: PhysicalOuterRow<'_>,
74 params: &[SQLParam],
75 ) -> Result<Option<uqa_execution::OwnedPhysicalRow>, SQLError> {
76 self.subquery_context()
77 .row_subquery_value(subquery, plan, outer_row, params)
78 }
79 fn scalar_subquery_value(
80 &self,
81 subquery: usize,
82 plan: &QueryPlan,
83 outer_row: PhysicalOuterRow<'_>,
84 params: &[SQLParam],
85 ) -> Result<Value, SQLError> {
86 self.subquery_context()
87 .scalar_subquery_value(subquery, plan, outer_row, params)
88 }
89 fn subquery_exists(
90 &self,
91 subquery: usize,
92 plan: &QueryPlan,
93 outer_row: PhysicalOuterRow<'_>,
94 params: &[SQLParam],
95 ) -> Result<bool, SQLError> {
96 self.subquery_context()
97 .subquery_exists(subquery, plan, outer_row, params)
98 }
99 fn subquery_contains_selected(
100 &self,
101 subquery: usize,
102 plan: &QueryPlan,
103 needle: uqa_execution::scalar::SubqueryNeedle<'_>,
104 outer_row: PhysicalOuterRow<'_>,
105 params: &[SQLParam],
106 ) -> Result<Option<bool>, SQLError> {
107 self.subquery_context()
108 .subquery_contains_selected(subquery, plan, needle, outer_row, params)
109 }
110
111 fn subquery_contains(
112 &self,
113 subquery: usize,
114 plan: &QueryPlan,
115 needle: &Value,
116 outer_row: PhysicalOuterRow<'_>,
117 params: &[SQLParam],
118 ) -> Result<Option<bool>, SQLError> {
119 self.subquery_context()
120 .subquery_contains(subquery, plan, needle, outer_row, params)
121 }
122}