uqa_sql/semantics/sets/
placement.rs1use crate::SQLError;
10
11const LATERAL_HINT: &str =
12 "You might be able to move the set-returning function into a LATERAL FROM item.";
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum SetPlacement {
17 Expanding,
19 FromFunction,
21 JoinCondition,
22 Where,
23 Having,
24 Filter,
25 WindowFrame,
27 Update,
29 Limit,
30 Offset,
31 Returning,
32 Values,
34 MergeWhen,
35 IndexExpression,
37 IndexPredicate,
39 ExecuteParameter,
40 TriggerWhen,
41 CallArgument,
42}
43
44impl SetPlacement {
45 pub fn admit(self, nested: bool) -> Result<(), SQLError> {
47 let context = match self {
48 Self::Expanding => return Ok(()),
49 Self::FromFunction if nested => return Err(top_level_from()),
50 Self::FromFunction => return Ok(()),
51 Self::JoinCondition => "JOIN conditions",
52 Self::Where => "WHERE",
53 Self::Having => "HAVING",
54 Self::Filter => "FILTER",
55 Self::WindowFrame => "window definitions",
56 Self::Update => "UPDATE",
57 Self::Limit => "LIMIT",
58 Self::Offset => "OFFSET",
59 Self::Returning => "RETURNING",
60 Self::Values => "VALUES",
61 Self::MergeWhen => "MERGE WHEN conditions",
62 Self::IndexExpression => "index expressions",
63 Self::IndexPredicate => "index predicates",
64 Self::ExecuteParameter => "EXECUTE parameters",
65 Self::TriggerWhen => "trigger WHEN conditions",
66 Self::CallArgument => "CALL arguments",
67 };
68 Err(feature_not_supported(format!(
69 "set-returning functions are not allowed in {context}"
70 )))
71 }
72}
73
74pub fn top_level_from() -> SQLError {
76 feature_not_supported("set-returning functions must appear at top level of FROM".into())
77}
78
79pub fn conditional_set_error(message: &str) -> SQLError {
81 SQLError::Diagnostic {
82 sqlstate: "0A000".into(),
83 message: message.into(),
84 detail: None,
85 hint: Some(LATERAL_HINT.into()),
86 }
87}
88
89pub fn set_argument_error(construct: &str) -> SQLError {
91 datatype_mismatch(format!("argument of {construct} must not return a set"))
92}
93
94pub fn row_comparison_set_error() -> SQLError {
96 datatype_mismatch("row comparison operator must not return a set".into())
97}
98
99pub fn set_valued_call_error() -> SQLError {
101 feature_not_supported("set-valued function called in context that cannot accept a set".into())
102}
103
104fn feature_not_supported(message: String) -> SQLError {
105 SQLError::Routine {
106 sqlstate: "0A000".into(),
107 message,
108 }
109}
110
111fn datatype_mismatch(message: String) -> SQLError {
112 SQLError::Routine {
113 sqlstate: "42804".into(),
114 message,
115 }
116}