Skip to main content

uqa_sql/plan/scalar/
binding.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Function bindings are copied only for borrowed column ASTs.
8
9use super::{
10    resources::{Lowering, Result},
11    source::Source,
12};
13use crate::ast::{
14    FunctionBinding, FunctionResolutionError, OperatorResolutionError, RoutineInvocationBinding,
15};
16
17impl FunctionBinding {
18    /// Copy the complete bound identity with the scalar lowerer's existing allocation admission.
19    pub fn clone_with_control(
20        &self,
21        control: &uqa_core::memory::ProductionControl<'_>,
22    ) -> Result<uqa_core::memory::Produced<Self>> {
23        control.check()?;
24        if control.budget().is_none() {
25            return Ok(control.finish(self.clone(), None)?);
26        }
27        let mut copying = Lowering {
28            control: Some(super::resources::Control::from_production(*control)),
29        };
30        let binding = copying.binding(Source::Borrowed(self))?;
31        Ok(copying.finish(binding)?.into())
32    }
33}
34
35impl Lowering<'_> {
36    pub(super) fn binding(
37        &mut self,
38        source: Source<'_, FunctionBinding>,
39    ) -> Result<FunctionBinding> {
40        self.check()?;
41        let binding = match source {
42            Source::Owned(binding) => return Ok(binding),
43            Source::Borrowed(binding) => binding,
44        };
45        let FunctionBinding {
46            object_id,
47            name,
48            argument_types,
49            builtin,
50            dispatch,
51            invocation,
52            value_binding,
53            resolution_error,
54            input_collation,
55        } = binding;
56        Ok(FunctionBinding {
57            object_id: *object_id,
58            input_collation: *input_collation,
59            name: self.copy_text(name)?,
60            argument_types: self.map(argument_types.iter(), |this, text| this.copy_text(text))?,
61            builtin: *builtin,
62            dispatch: *dispatch,
63            invocation: invocation
64                .as_ref()
65                .map(|invocation| self.boxed(|this| this.invocation(invocation)))
66                .transpose()?,
67            value_binding: value_binding
68                .as_ref()
69                .map(|binding| {
70                    self.boxed(|this| {
71                        use crate::ast::FunctionValueBinding;
72                        Ok(match binding.as_ref() {
73                            FunctionValueBinding::Assignment(assignment) => {
74                                FunctionValueBinding::Assignment(crate::ast::AssignmentBinding {
75                                    path: this.map(assignment.path.iter(), |_, step| Ok(*step))?,
76                                    value_argument: assignment.value_argument,
77                                })
78                            }
79                            FunctionValueBinding::CompositeField(field) => {
80                                FunctionValueBinding::CompositeField(
81                                    crate::ast::CompositeFieldBinding {
82                                        collation_oid: field.collation_oid,
83                                        type_oid: field.type_oid,
84                                        number: field.number,
85                                        result_type: this
86                                            .scalar_type_copy(Some(&field.result_type))?
87                                            .expect("field result type"),
88                                        dropped: field.dropped,
89                                        changed_type: this
90                                            .scalar_type_copy(field.changed_type.as_ref())?,
91                                    },
92                                )
93                            }
94                        })
95                    })
96                })
97                .transpose()?,
98            resolution_error: resolution_error
99                .as_ref()
100                .map(|error| self.boxed(|this| this.resolution_error(error)))
101                .transpose()?,
102        })
103    }
104
105    fn invocation(
106        &mut self,
107        invocation: &RoutineInvocationBinding,
108    ) -> Result<RoutineInvocationBinding> {
109        let RoutineInvocationBinding {
110            argument_positions,
111            argument_targets,
112            argument_sources,
113            parameter_types,
114            return_type,
115            variadic_mode,
116        } = invocation;
117        Ok(RoutineInvocationBinding {
118            argument_positions: self.map(argument_positions.iter(), |_, position| Ok(*position))?,
119            argument_targets: self
120                .map(argument_targets.iter(), |this, text| this.copy_text(text))?,
121            argument_sources: self.map(argument_sources.iter(), |this, text| {
122                text.as_ref().map(|text| this.copy_text(text)).transpose()
123            })?,
124            parameter_types: self.map(parameter_types.iter(), |this, text| this.copy_text(text))?,
125            return_type: return_type
126                .as_ref()
127                .map(|text| self.copy_text(text))
128                .transpose()?,
129            variadic_mode: *variadic_mode,
130        })
131    }
132
133    fn resolution_error(
134        &mut self,
135        error: &FunctionResolutionError,
136    ) -> Result<FunctionResolutionError> {
137        Ok(match error {
138            FunctionResolutionError::UndefinedFunction { signature } => {
139                FunctionResolutionError::UndefinedFunction {
140                    signature: self.copy_text(signature)?,
141                }
142            }
143            FunctionResolutionError::Operator(error) => {
144                FunctionResolutionError::Operator(self.boxed(|this| {
145                    Ok(OperatorResolutionError {
146                        sqlstate: this.copy_text(&error.sqlstate)?,
147                        message: this.copy_text(&error.message)?,
148                    })
149                })?)
150            }
151        })
152    }
153}