Skip to main content

uqa_sql/expr/
array_access.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Evaluate array access once, retaining `PostgreSQL`'s ordered bound effects.
8
9use std::ops::Deref;
10use uqa_core::{
11    memory::{Produced, ProductionControl},
12    Value,
13};
14
15use crate::{
16    ast::{ArrayAccess, MAX_ARRAY_SUBSCRIPT_DIMENSIONS},
17    SQLError,
18};
19
20/// The caller owns expression scheduling and input guards; SQL owns NULL reachability, bound conversion and selected array output.
21pub fn eval_array_access_with_control(
22    access: ArrayAccess,
23    control: &ProductionControl<'_>,
24    evaluate: impl FnMut(usize) -> Result<Produced<Value>, SQLError>,
25) -> Result<Produced<Value>, SQLError> {
26    eval_array_access_with_catalog_and_control(access, None, control, evaluate)
27}
28
29/// The value catalog interprets an admitted physical array after every reached bound has evaluated. It never repeats array or element input.
30pub fn eval_array_access_with_catalog_and_control(
31    access: ArrayAccess,
32    catalog: Option<&dyn super::SQLValueCatalog>,
33    control: &ProductionControl<'_>,
34    mut evaluate: impl FnMut(usize) -> Result<Produced<Value>, SQLError>,
35) -> Result<Produced<Value>, SQLError> {
36    control.check()?;
37    access.validate_dimensions()?;
38    let array = evaluate(0)?;
39    eval_array_access_from_input(access, &array, catalog, control, evaluate)
40}
41
42/// Already evaluated inputs stay borrowed; produced inputs retain their original guards until each conversion finishes.
43pub(in crate::expr) fn eval_array_access_from_input<B: Deref<Target = Value>>(
44    access: ArrayAccess,
45    array: &Value,
46    catalog: Option<&dyn super::SQLValueCatalog>,
47    control: &ProductionControl<'_>,
48    mut evaluate: impl FnMut(usize) -> Result<B, SQLError>,
49) -> Result<Produced<Value>, SQLError> {
50    control.check()?;
51    access.validate_dimensions()?;
52    if matches!(array, Value::Null) {
53        return Ok(control.finish(Value::Null, control.empty_reservation())?);
54    }
55    let mut bounds: [Value; MAX_ARRAY_SUBSCRIPT_DIMENSIONS * 2] =
56        std::array::from_fn(|_| Value::Null);
57    for index in access.evaluation_order() {
58        control.check()?;
59        let value = evaluate(index)?;
60        let value = super::cast_value_from_with_control(&value, "integer", None, control)?;
61        bounds[index - 1] = match *value {
62            Value::Null => Value::Null,
63            Value::Int(value) => Value::Int(value),
64            _ => {
65                return Err(SQLError::Internal(
66                    "array bound coercion did not produce an integer".into(),
67                ))
68            }
69        };
70    }
71    if (1..=access.bound_count())
72        .any(|index| !access.is_omitted(index) && matches!(bounds[index - 1], Value::Null))
73    {
74        return Ok(control.finish(Value::Null, control.empty_reservation())?);
75    }
76    let decoded;
77    let array = if let Value::Datum(datum) = array {
78        decoded =
79            super::datums::read_array_input_with_catalog_and_control(datum, catalog, control)?;
80        &decoded
81    } else {
82        array
83    };
84    super::scalar_postgres::subscripts::eval_selected_array_access(
85        array,
86        &bounds[..access.bound_count()],
87        access.slice_bounds(),
88        control,
89    )
90}
91
92#[cfg(test)]
93mod tests;