uqa_sql/prepared/
arguments.rs1use super::error;
10use crate::{ast::FunctionVolatility, plan::ExpressionPlan, ColumnType, SQLError, ScalarExpr};
11
12pub struct ArgumentValidationContext<'a> {
13 pub aggregates: &'a dyn crate::plan::AggregateClassifier,
14 pub volatility: &'a dyn crate::semantics::volatility::VolatilityCatalog,
15 pub cast_type: &'a dyn Fn(&str) -> Option<ColumnType>,
16 pub routines: &'a dyn crate::routines::RoutineResolution,
18 pub parameters: &'a [crate::SQLParam],
20}
21
22pub fn validate_assignment_type(
23 index: usize,
24 source: &ColumnType,
25 target: &ColumnType,
26) -> Result<(), SQLError> {
27 if !crate::assignment_type_compatible(source, target) {
28 return Err(error(
29 "42804",
30 format!(
31 "parameter ${} of type {} cannot be coerced to the expected type {}",
32 index + 1,
33 source.sql_name(),
34 target.sql_name()
35 ),
36 ));
37 }
38 Ok(())
39}
40
41pub fn parameter_base_type(mut ty: &ColumnType) -> &ColumnType {
42 while let ColumnType::Domain { base, .. } = ty {
43 ty = base;
44 }
45 ty
46}
47
48pub fn contains_domain(ty: &ColumnType) -> bool {
49 match ty {
50 ColumnType::Domain { .. } => true,
51 ColumnType::Array(element) => contains_domain(element),
52 _ => false,
53 }
54}
55
56pub fn immutable_argument(
57 context: &ArgumentValidationContext<'_>,
58 expression: &ScalarExpr,
59) -> bool {
60 let mut immutable = true;
61 expression.visit(&mut |expression| match expression {
62 ScalarExpr::Param(_)
63 | ScalarExpr::Column(_)
64 | ScalarExpr::QualifiedColumn { .. }
65 | ScalarExpr::InternalColumn(_)
66 | ScalarExpr::Position(_) => immutable = false,
67 ScalarExpr::Func {
68 name,
69 binding,
70 args,
71 ..
72 } => {
73 immutable &= crate::semantics::volatility::function_volatility_with_binding(
74 context.volatility,
75 name,
76 binding.as_ref(),
77 args.len(),
78 ) == FunctionVolatility::Immutable;
79 }
80 ScalarExpr::Cast { ty, .. } => {
81 immutable &= !(context.cast_type)(ty)
82 .as_ref()
83 .is_some_and(contains_domain);
84 }
85 _ => {}
86 });
87 immutable
88}
89
90pub fn validate_argument(
91 aggregates: &dyn crate::plan::AggregateClassifier,
92 argument: &ExpressionPlan,
93) -> Result<(), SQLError> {
94 if !argument.subqueries.is_empty() {
95 return Err(error("0A000", "cannot use subquery in EXECUTE parameter"));
96 }
97 if argument.scalar.contains_window() {
98 return Err(error(
99 "42P20",
100 "window functions are not allowed in EXECUTE parameters",
101 ));
102 }
103 if crate::semantics::aggregates::contains_aggregate(aggregates, &argument.scalar) {
104 return Err(error(
105 "42803",
106 "aggregate functions are not allowed in EXECUTE parameters",
107 ));
108 }
109 Ok(())
110}
111
112pub fn reject_set_argument(
114 context: &ArgumentValidationContext<'_>,
115 argument: &ExpressionPlan,
116) -> Result<(), SQLError> {
117 if crate::semantics::sets::validation::expression_may_return_set(
118 context.routines,
119 context.routines,
120 &argument.scalar,
121 &crate::RowSchema::default(),
122 context.parameters,
123 )? {
124 crate::semantics::sets::placement::SetPlacement::ExecuteParameter.admit(true)?;
125 }
126 Ok(())
127}