Skip to main content

uqa_sql/expr/
observations.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Inspect deferred and catalog-dependent value carriers at output boundaries.
8
9use uqa_core::Value;
10
11/// SQL row NULL tests inspect the current live fields while preserving deferred scalar reads.
12pub fn sql_null_test_with_control(
13    value: &Value,
14    negated: bool,
15    engine: Option<&dyn super::EngineHook>,
16    control: &uqa_core::memory::ProductionControl<'_>,
17) -> Result<bool, crate::SQLError> {
18    control.check()?;
19    if let Value::Datum(datum) = value {
20        let catalog = engine.map(super::EngineValueCatalog);
21        if let Some(result) = super::datums::record_null_test_with_control(
22            datum,
23            negated,
24            catalog
25                .as_ref()
26                .map(|catalog| catalog as &dyn super::SQLValueCatalog),
27            control,
28        )? {
29            return Ok(result);
30        }
31    }
32    Ok(uqa_core::sql_null_test(Some(value), negated))
33}
34
35#[derive(Clone, Copy, Default)]
36pub(crate) struct OutputCarriers {
37    pub(crate) datum: bool,
38    pub(crate) enumeration: bool,
39}
40
41/// Inspect both carrier kinds without decoding values or querying the catalog.
42pub(crate) fn output_carriers(value: &Value) -> OutputCarriers {
43    let mut found = OutputCarriers::default();
44    any_nested_value(value, &mut |value| {
45        found.datum |= matches!(value, Value::Datum(_));
46        found.enumeration |= matches!(value, Value::Enum(_));
47        // Decode the first Datum before inspecting any later value, as the
48        // original Datum-presence check did. Decoding may itself fail.
49        found.datum
50    });
51    found
52}
53
54/// Walk native carrier children in their stored order, stopping at the first match.
55pub(crate) fn any_nested_value(value: &Value, predicate: &mut impl FnMut(&Value) -> bool) -> bool {
56    #[cfg(test)]
57    record_visit();
58    if predicate(value) {
59        return true;
60    }
61    match value {
62        Value::Array(array) => array
63            .elements()
64            .iter()
65            .any(|value| any_nested_value(value, predicate)),
66        Value::List(values) => values
67            .iter()
68            .any(|value| any_nested_value(value, predicate)),
69        Value::Row(values) => values
70            .iter()
71            .any(|value| any_nested_value(value, predicate)),
72        Value::Record(fields) => fields
73            .values()
74            .iter()
75            .any(|value| any_nested_value(value, predicate)),
76        Value::Map(values) => values
77            .values()
78            .any(|value| any_nested_value(value, predicate)),
79        _ => false,
80    }
81}
82
83#[cfg(test)]
84thread_local! {
85    static VALUE_VISITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
86}
87
88#[cfg(test)]
89pub(crate) fn record_visit() {
90    VALUE_VISITS.with(|visits| visits.set(visits.get() + 1));
91}
92
93#[cfg(test)]
94pub(crate) fn visits() -> usize {
95    VALUE_VISITS.with(std::cell::Cell::get)
96}