uqa_sql/plan/query/
computation.rs1use super::{AggregateClassifier, ComputePlan, QueryBlockPlan, ScalarExpr};
10use crate::SQLError;
11use uqa_core::memory::ProductionControl;
12
13impl QueryBlockPlan {
14 pub fn compute_kind(&self, aggregates: &dyn AggregateClassifier) -> ComputePlan {
16 self.compute_observing(&mut |expression| {
17 Ok::<_, std::convert::Infallible>(crate::semantics::aggregates::contains_aggregate(
18 aggregates, expression,
19 ))
20 })
21 .unwrap_or_else(|never| match never {})
22 }
23
24 pub fn compute_kind_with_control(
25 &self,
26 aggregates: &dyn AggregateClassifier,
27 control: &ProductionControl<'_>,
28 ) -> Result<ComputePlan, SQLError> {
29 control.check()?;
30 self.compute_observing(&mut |expression| {
31 crate::semantics::aggregates::contains_aggregate_with_control(
32 aggregates, expression, control,
33 )
34 })
35 }
36
37 fn compute_observing<E>(
38 &self,
39 observe: &mut impl FnMut(&ScalarExpr) -> Result<bool, E>,
40 ) -> Result<ComputePlan, E> {
41 let mut aggregate =
42 !self.group_by.is_empty() || !self.grouping_sets.is_empty() || self.having.is_some();
43 if !aggregate {
44 for expression in self
45 .projections
46 .iter()
47 .map(|projection| &projection.expr)
48 .chain(
49 self.windows
50 .iter()
51 .flat_map(|window| window.spec.expressions()),
52 )
53 {
54 if observe(expression)? {
55 aggregate = true;
56 break;
57 }
58 }
59 }
60 let window = self
61 .projections
62 .iter()
63 .any(|projection| projection.expr.contains_window());
64 Ok(if aggregate {
65 ComputePlan::Aggregate
66 } else if window {
67 ComputePlan::Window
68 } else {
69 ComputePlan::Project
70 })
71 }
72}