Skip to main content

uqa_sql/semantics/sets/
static_setness.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Catalog-only set-returning routine classification before physical traversal.
8use super::validation::builtin_returns_set;
9use crate::ast::FunctionBinding;
10
11pub fn function_may_return_set_statically(
12    catalog: &dyn crate::routines::RoutineResolution,
13    name: &str,
14    binding: Option<&FunctionBinding>,
15) -> bool {
16    if binding.and_then(|binding| binding.dispatch).is_some()
17        || binding.is_some_and(FunctionBinding::is_polymorphic_builtin_syntax)
18    {
19        return false;
20    }
21    // A call bound to a SQL routine returns a set exactly when that routine does, whatever built-in shares its name.
22    if let Some(binding) = binding.filter(|binding| !binding.builtin) {
23        return catalog
24            .lookup_bound_sql_functions_by_binding(binding)
25            .is_some_and(|overloads| {
26                overloads.iter().any(|function| {
27                    !function.def.is_procedure
28                        && crate::routines::routine_signature_types(&function.def)
29                            == binding.argument_types
30                        && function.def.returns_set()
31                })
32            });
33    }
34    let identity = name.to_ascii_lowercase();
35    let builtin = crate::semantics::builtin_function_dispatch_name(&identity);
36    if builtin_returns_set(&builtin) || catalog.has_registered_table_function(&identity) {
37        return true;
38    }
39    if binding.is_some() {
40        return false;
41    }
42    catalog
43        .lookup_visible_sql_functions_for_analysis(name)
44        .ok()
45        .flatten()
46        .is_some_and(|overloads| {
47            overloads
48                .iter()
49                .any(|function| !function.def.is_procedure && function.def.returns_set())
50        })
51}
52
53/// Whether a call of a built-in routine returns a set: a call bound to a built-in, or an unbound call that no SQL routine resolved. Scalar evaluation reports such a call with `PostgreSQL`'s executor diagnostic instead of evaluating it.
54pub fn builtin_call_returns_set(name: &str, binding: Option<&FunctionBinding>) -> bool {
55    if binding.is_some_and(|binding| {
56        !binding.builtin || binding.dispatch.is_some() || binding.is_polymorphic_builtin_syntax()
57    }) {
58        return false;
59    }
60    let name = binding
61        .map_or(name, |binding| binding.name.as_str())
62        .to_ascii_lowercase();
63    builtin_returns_set(name.strip_prefix("pg_catalog.").unwrap_or(&name))
64}
65
66/// Whether `expression` contains a call that returns a set, outside its subqueries, classified from the catalog alone: a bound call by its routine, an unbound call by every visible routine of its name.
67pub fn expression_may_return_set_statically(
68    catalog: &dyn crate::routines::RoutineResolution,
69    expression: &crate::ScalarExpr,
70) -> bool {
71    let mut returns_set = false;
72    expression.visit(&mut |node| {
73        if let crate::ScalarExpr::Func { name, binding, .. } = node {
74            if !returns_set {
75                returns_set = function_may_return_set_statically(catalog, name, binding.as_ref());
76            }
77        }
78    });
79    returns_set
80}