Skip to main content

uqa_sql/semantics/
windows.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Window expression classification.
8
9use crate::ScalarExpr;
10
11pub fn expr_has_window(expr: &ScalarExpr) -> bool {
12    match expr {
13        ScalarExpr::WindowCall { .. } => true,
14        ScalarExpr::Func {
15            args,
16            order_by,
17            filter,
18            ..
19        } => {
20            args.iter().any(expr_has_window)
21                || order_by.iter().any(|order| expr_has_window(&order.expr))
22                || filter.as_ref().is_some_and(|expr| expr_has_window(expr))
23        }
24        ScalarExpr::Array(items)
25        | ScalarExpr::Row(items)
26        | ScalarExpr::CompositeRow { items, .. }
27        | ScalarExpr::And(items)
28        | ScalarExpr::Or(items) => items.iter().any(expr_has_window),
29        ScalarExpr::Binary { lhs, rhs, .. } => expr_has_window(lhs) || expr_has_window(rhs),
30        ScalarExpr::Not(inner)
31        | ScalarExpr::UnaryMinus(inner)
32        | ScalarExpr::IsNull { expr: inner, .. }
33        | ScalarExpr::Cast { expr: inner, .. } => expr_has_window(inner),
34        ScalarExpr::Between { expr, low, high } => {
35            expr_has_window(expr) || expr_has_window(low) || expr_has_window(high)
36        }
37        ScalarExpr::InList { expr, list, .. } => {
38            expr_has_window(expr) || list.iter().any(expr_has_window)
39        }
40        ScalarExpr::Case {
41            base,
42            when,
43            else_branch,
44        } => {
45            base.as_ref().is_some_and(|expr| expr_has_window(expr))
46                || when
47                    .iter()
48                    .any(|(cond, result)| expr_has_window(cond) || expr_has_window(result))
49                || else_branch
50                    .as_ref()
51                    .is_some_and(|expr| expr_has_window(expr))
52        }
53        ScalarExpr::Default
54        | ScalarExpr::Star
55        | ScalarExpr::QualifiedStar(_)
56        | ScalarExpr::Column(_)
57        | ScalarExpr::Position(_)
58        | ScalarExpr::InternalColumn(_)
59        | ScalarExpr::QualifiedColumn { .. }
60        | ScalarExpr::Literal(_)
61        | ScalarExpr::TypedLiteral { .. }
62        | ScalarExpr::Param(_)
63        | ScalarExpr::ScalarSubquery(_)
64        | ScalarExpr::Exists { .. }
65        | ScalarExpr::InSubquery { .. } => false,
66    }
67}