Skip to main content

uqa_sql/semantics/
mutation_inputs.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Row-independent mutation input shapes.
8use crate::ScalarExpr;
9
10/// A whole-column assignment whose expression needs no input row. Selecting it never evaluates the expression or its target coercion.
11pub struct RowIndependentAssignment<'a> {
12    pub column: &'a str,
13    pub value: &'a ScalarExpr,
14}
15
16pub fn row_independent_assignments(
17    assignments: &[crate::plan::AssignmentPlan],
18) -> Option<Vec<RowIndependentAssignment<'_>>> {
19    assignments
20        .iter()
21        .map(|assignment| {
22            let crate::ast::AssignmentTargets::Single(target) = &assignment.target else {
23                return None;
24            };
25            (target.is_whole_column() && expr_is_row_independent(&assignment.value)).then_some(
26                RowIndependentAssignment {
27                    column: &target.column,
28                    value: &assignment.value,
29                },
30            )
31        })
32        .collect()
33}
34
35/// Borrow an equality lookup without evaluating its value before the caller selects the complete physical path.
36pub fn point_lookup_input(filter: Option<&ScalarExpr>) -> Option<(&str, &ScalarExpr)> {
37    let ScalarExpr::Binary {
38        op: crate::ast::BinaryOp::Equal,
39        lhs,
40        rhs,
41    } = filter?
42    else {
43        return None;
44    };
45    for (column, value) in [(lhs, rhs), (rhs, lhs)] {
46        if let Some(column) = top_level_column(column) {
47            if expr_is_row_independent(value) {
48                return Some((column, value));
49            }
50        }
51    }
52    None
53}
54
55pub fn top_level_column(expr: &ScalarExpr) -> Option<&str> {
56    match expr {
57        ScalarExpr::Column(name) => Some(name),
58        ScalarExpr::QualifiedColumn { column, .. } => Some(column),
59        _ => None,
60    }
61}
62
63pub fn expr_is_row_independent(expr: &ScalarExpr) -> bool {
64    match expr {
65        ScalarExpr::Literal(_)
66        | ScalarExpr::TypedLiteral { .. }
67        | ScalarExpr::RetainedLiteral(_)
68        | ScalarExpr::Param(_) => true,
69        ScalarExpr::Array(items)
70        | ScalarExpr::Row(items)
71        | ScalarExpr::CompositeRow { items, .. }
72        | ScalarExpr::And(items)
73        | ScalarExpr::Or(items) => items.iter().all(expr_is_row_independent),
74        ScalarExpr::Binary { lhs, rhs, .. } => {
75            expr_is_row_independent(lhs) && expr_is_row_independent(rhs)
76        }
77        ScalarExpr::Not(inner) | ScalarExpr::UnaryMinus(inner) => expr_is_row_independent(inner),
78        ScalarExpr::IsNull { expr, .. } => expr_is_row_independent(expr),
79        ScalarExpr::Between { expr, low, high } => {
80            expr_is_row_independent(expr)
81                && expr_is_row_independent(low)
82                && expr_is_row_independent(high)
83        }
84        ScalarExpr::InList { expr, list, .. } => {
85            expr_is_row_independent(expr) && list.iter().all(expr_is_row_independent)
86        }
87        ScalarExpr::Case {
88            base,
89            when,
90            else_branch,
91        } => {
92            base.as_deref().map_or(true, expr_is_row_independent)
93                && when.iter().all(|(condition, result)| {
94                    expr_is_row_independent(condition) && expr_is_row_independent(result)
95                })
96                && else_branch.as_deref().map_or(true, expr_is_row_independent)
97        }
98        ScalarExpr::Cast { expr, .. } | ScalarExpr::Collated { expr, .. } => {
99            expr_is_row_independent(expr)
100        }
101        ScalarExpr::Default
102        | ScalarExpr::Star
103        | ScalarExpr::QualifiedStar(_)
104        | ScalarExpr::Column(_)
105        | ScalarExpr::Position(_)
106        | ScalarExpr::InternalColumn(_)
107        | ScalarExpr::QualifiedColumn { .. }
108        | ScalarExpr::Func { .. }
109        | ScalarExpr::WindowCall { .. }
110        | ScalarExpr::ScalarSubquery(_)
111        | ScalarExpr::Exists { .. }
112        | ScalarExpr::InSubquery { .. } => false,
113    }
114}