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            owned_children: None,
30        };
31        let binding = copying.binding(Source::Borrowed(self))?;
32        Ok(copying.finish(binding)?.into())
33    }
34    pub(crate) fn with_composite_projection_with_control(
35        &self,
36        dropped: bool,
37        changed_type: Option<&crate::ColumnType>,
38        control: &uqa_core::memory::ProductionControl<'_>,
39    ) -> Result<uqa_core::memory::Produced<Self>> {
40        control.check()?;
41        assert!(
42            self.composite_field().is_some(),
43            "composite projection requires selected field metadata"
44        );
45        let mut copying = Lowering {
46            control: control
47                .budget()
48                .map(|_| super::resources::Control::from_production(*control)),
49            owned_children: None,
50        };
51        let binding = copying.binding_fields(self, Some((dropped, changed_type)))?;
52        Ok(control.finish(binding, copying.control.map(|owner| owner.memory))?)
53    }
54}
55
56impl Lowering<'_> {
57    pub(super) fn binding(
58        &mut self,
59        source: Source<'_, FunctionBinding>,
60    ) -> Result<FunctionBinding> {
61        self.check()?;
62        let binding = match source {
63            Source::Owned(binding) => return Ok(binding),
64            Source::Borrowed(binding) => binding,
65        };
66        self.binding_fields(binding, None)
67    }
68
69    fn binding_fields(
70        &mut self,
71        binding: &FunctionBinding,
72        field_projection: Option<(bool, Option<&crate::ColumnType>)>,
73    ) -> Result<FunctionBinding> {
74        let FunctionBinding {
75            object_id,
76            name,
77            argument_types,
78            builtin,
79            dispatch,
80            invocation,
81            value_binding,
82            resolution_error,
83            input_collation,
84        } = binding;
85        Ok(FunctionBinding {
86            object_id: *object_id,
87            input_collation: *input_collation,
88            name: self.copy_text(name)?,
89            argument_types: self.map(argument_types.iter(), |this, text| this.copy_text(text))?,
90            builtin: *builtin,
91            dispatch: *dispatch,
92            invocation: invocation
93                .as_ref()
94                .map(|invocation| self.boxed(|this| this.invocation(invocation)))
95                .transpose()?,
96            value_binding: value_binding
97                .as_ref()
98                .map(|binding| {
99                    self.boxed(|this| {
100                        use crate::ast::FunctionValueBinding;
101                        Ok(match binding.as_ref() {
102                            FunctionValueBinding::Assignment(assignment) => {
103                                FunctionValueBinding::Assignment(crate::ast::AssignmentBinding {
104                                    path: this.map(assignment.path.iter(), |_, step| Ok(*step))?,
105                                    value_argument: assignment.value_argument,
106                                })
107                            }
108                            FunctionValueBinding::CompositeField(field) => {
109                                FunctionValueBinding::CompositeField(
110                                    crate::ast::CompositeFieldBinding {
111                                        collation_oid: field.collation_oid,
112                                        type_oid: field.type_oid,
113                                        number: field.number,
114                                        result_type: this
115                                            .scalar_type_copy(Some(&field.result_type))?
116                                            .expect("field result type"),
117                                        dropped: field_projection
118                                            .map_or(field.dropped, |(dropped, _)| dropped),
119                                        changed_type: this.scalar_type_copy(
120                                            field_projection
121                                                .map_or(field.changed_type.as_ref(), |(_, ty)| ty),
122                                        )?,
123                                    },
124                                )
125                            }
126                        })
127                    })
128                })
129                .transpose()?,
130            resolution_error: resolution_error
131                .as_ref()
132                .map(|error| self.boxed(|this| this.resolution_error(error)))
133                .transpose()?,
134        })
135    }
136
137    fn invocation(
138        &mut self,
139        invocation: &RoutineInvocationBinding,
140    ) -> Result<RoutineInvocationBinding> {
141        let RoutineInvocationBinding {
142            argument_positions,
143            argument_targets,
144            argument_sources,
145            parameter_types,
146            return_type,
147            variadic_mode,
148        } = invocation;
149        Ok(RoutineInvocationBinding {
150            argument_positions: self.map(argument_positions.iter(), |_, position| Ok(*position))?,
151            argument_targets: self
152                .map(argument_targets.iter(), |this, text| this.copy_text(text))?,
153            argument_sources: self.map(argument_sources.iter(), |this, text| {
154                text.as_ref().map(|text| this.copy_text(text)).transpose()
155            })?,
156            parameter_types: self.map(parameter_types.iter(), |this, text| this.copy_text(text))?,
157            return_type: return_type
158                .as_ref()
159                .map(|text| self.copy_text(text))
160                .transpose()?,
161            variadic_mode: *variadic_mode,
162        })
163    }
164
165    fn resolution_error(
166        &mut self,
167        error: &FunctionResolutionError,
168    ) -> Result<FunctionResolutionError> {
169        Ok(match error {
170            FunctionResolutionError::UndefinedFunction { signature } => {
171                FunctionResolutionError::UndefinedFunction {
172                    signature: self.copy_text(signature)?,
173                }
174            }
175            FunctionResolutionError::Operator(error) => {
176                FunctionResolutionError::Operator(self.boxed(|this| {
177                    Ok(OperatorResolutionError {
178                        sqlstate: this.copy_text(&error.sqlstate)?,
179                        message: this.copy_text(&error.message)?,
180                    })
181                })?)
182            }
183        })
184    }
185}