Skip to main content

uqa_sql/routines/resolution/production/
routine.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Procedure and function calls share admitted candidate ranking while retaining their selected invocation independently.
8
9use super::{diagnostics, matching, selection};
10use crate::{
11    ast::{FunctionBinding, RoutineInvocationBinding, RoutineVariadicMode},
12    routines::{
13        resolution::{RoutineCallKind, RoutineOverloadContext},
14        SQLUserFunction,
15    },
16    type_resolution::RoutineCallDescriptor,
17    ColumnType, SQLError,
18};
19use std::sync::Arc;
20use uqa_core::memory::{Produced, ProductionControl};
21
22pub struct OwnedStaticFunctionMatch {
23    pub function: Arc<SQLUserFunction>,
24    pub invocation: Produced<RoutineInvocationBinding>,
25}
26
27impl RoutineOverloadContext<'_> {
28    #[expect(
29        clippy::too_many_arguments,
30        reason = "selected call syntax, routine kind and admission share one existing resolution boundary"
31    )]
32    pub fn resolve_static_sql_routine_match_with_control(
33        &self,
34        name: &str,
35        binding: Option<&FunctionBinding>,
36        argument_names: &[Option<String>],
37        argument_types: &[Option<ColumnType>],
38        explicit_variadic: bool,
39        kind: RoutineCallKind,
40        control: &ProductionControl<'_>,
41    ) -> Result<Option<OwnedStaticFunctionMatch>, SQLError> {
42        control.check()?;
43        let call = RoutineCallDescriptor {
44            argument_names,
45            argument_types,
46            explicit_variadic,
47        };
48        let matched = if let Some(binding) = binding {
49            if binding.builtin {
50                return Ok(None);
51            }
52            let function = self
53                .catalog
54                .select_bound_routine_with_control(binding, kind, control)?
55                .ok_or_else(|| {
56                    diagnostics::bound(kind, binding, control)
57                        .unwrap_or_else(std::convert::identity)
58                })?;
59            if let Some(invocation) = &binding.invocation {
60                if invocation.argument_positions.len() != argument_types.len()
61                    || matches!(
62                        invocation.variadic_mode,
63                        RoutineVariadicMode::Explicit { .. }
64                    ) != explicit_variadic
65                {
66                    return Err(diagnostics::bound(kind, binding, control)?);
67                }
68                diagnostics::kind(name, call, kind, &function.def, control)?;
69                return Ok(Some(OwnedStaticFunctionMatch {
70                    function,
71                    invocation: crate::plan::copy::copy_syntax_with_control(
72                        invocation.as_ref(),
73                        control,
74                    )?,
75                }));
76            }
77            matching::match_candidate(
78                &self.catalog.routine_type_snapshot(),
79                function,
80                call,
81                kind,
82                control,
83            )
84            .map_err(|error| selection::signature_error(error, control))?
85            .ok_or_else(|| {
86                diagnostics::bound(kind, binding, control).unwrap_or_else(std::convert::identity)
87            })?
88        } else {
89            let Some(matched) = selection::select_unbound_kind(self, name, call, kind, control)?
90            else {
91                return Ok(None);
92            };
93            matched
94        };
95        diagnostics::kind(name, call, kind, &matched.function.def, control)?;
96        if let Some(error) = &matched.signature.default_error {
97            return Err(diagnostics::default_type(self.catalog, error, control)?);
98        }
99        Ok(Some(OwnedStaticFunctionMatch {
100            function: matched.function,
101            invocation: matched.invocation,
102        }))
103    }
104}