Skip to main content

uqa_sql/ast/
retained_literal.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Frozen runtime input payloads preserve ordinary AST serialization and scalar lowering.
8
9use super::Expr;
10use crate::{ir::RetainedScalarLiteral, SQLError};
11use serde::{Serialize, Serializer};
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct RetainedInputLiteral(pub RetainedScalarLiteral);
15
16impl RetainedInputLiteral {
17    pub(crate) fn retain(
18        expression: Expr,
19        storage: &dyn crate::ir::ConstantValueStorage,
20    ) -> Result<Expr, SQLError> {
21        let (value, declaration) = match &expression {
22            Expr::TypedLiteral {
23                value,
24                ty,
25                composite_source: None,
26                ..
27            } => (value, Some(ty)),
28            Expr::Literal(value) => (value, None),
29            _ => return Ok(expression),
30        };
31        let cancellation = crate::expr::statement_cancellation().unwrap_or_default();
32        let bytes = value.retained_payload_bytes(
33            &uqa_core::memory::MemoryBudget::new(usize::MAX),
34            &cancellation,
35        )?;
36        if bytes <= size_of::<crate::ScalarExpr>()
37            || crate::expr::composites::literal::contains_records(value)
38            || crate::expr::datums::contains_datum(value)
39        {
40            return Ok(expression);
41        }
42        let ty = if let Some(name) = declaration {
43            crate::type_resolution::pg_catalog_builtin_type(name)
44        } else {
45            crate::type_resolution::scalar_type(
46                &crate::ScalarExpr::Literal(value.clone()),
47                &crate::RowSchema::default(),
48                &[],
49            )?
50        };
51        let Some(ty) = ty else {
52            return Ok(expression);
53        };
54        if crate::type_resolution::pg_catalog_builtin_type(&ty.catalog_name()).as_ref() != Some(&ty)
55        {
56            return Ok(expression);
57        }
58        let (value, declaration, collation_oid) = match expression {
59            Expr::TypedLiteral {
60                value,
61                ty,
62                collation_oid,
63                ..
64            } => (value, Some(ty), collation_oid),
65            Expr::Literal(value) => (value, None, None),
66            _ => unreachable!("selected evaluated input"),
67        };
68        Ok(Expr::RetainedLiteral(Box::new(Self(
69            RetainedScalarLiteral {
70                value: storage.retain(value)?,
71                ty,
72                declaration,
73                collation_oid,
74            },
75        ))))
76    }
77
78    pub fn materialize(&self) -> Result<Expr, SQLError> {
79        let value = self.0.value.read()?;
80        Ok(match &self.0.declaration {
81            Some(ty) => Expr::TypedLiteral {
82                value,
83                ty: ty.clone(),
84                collation_oid: self.0.collation_oid,
85                composite_source: None,
86            },
87            None => Expr::Literal(value),
88        })
89    }
90}
91
92impl Serialize for RetainedInputLiteral {
93    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
94        self.materialize()
95            .map_err(serde::ser::Error::custom)?
96            .serialize(serializer)
97    }
98}
99
100#[cfg(test)]
101mod tests;