1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Special SQL:1999 string function syntax parsing
//!
//! This module handles special SQL:1999 function syntax that differs from
//! standard function call syntax:
//! - POSITION(substring IN string [USING unit])
//! - TRIM([position] [removal_char FROM] string)
//! - SUBSTRING(string FROM start [FOR length] [USING unit])
//! - EXTRACT(field FROM expr)
use super::super::*;
impl Parser {
/// Parse POSITION(substring IN string [USING unit])
/// SQL:1999 standard syntax
pub(super) fn parse_position_function(
&mut self,
) -> Result<vibesql_ast::Expression, ParseError> {
// Parse substring at primary level (literals, identifiers, function calls)
// to avoid IN operator consumption at comparison level
let substring = self.parse_primary_expression()?;
// Expect IN keyword
self.expect_keyword(Keyword::In)?;
// Parse string expression at primary level
let string = self.parse_primary_expression()?;
// Parse optional USING clause
let character_unit =
if matches!(self.peek(), Token::Keyword { keyword: Keyword::Using, .. }) {
self.advance(); // consume USING
Some(self.parse_character_unit()?)
} else {
None
};
// Expect closing parenthesis
self.expect_token(Token::RParen)?;
Ok(vibesql_ast::Expression::Position {
substring: Box::new(substring),
string: Box::new(string),
character_unit,
})
}
/// Parse EXTRACT(field FROM expr)
/// SQL:1999 standard syntax for extracting date/time components
///
/// Forms:
/// - EXTRACT(YEAR FROM date_column)
/// - EXTRACT(MONTH FROM timestamp_column)
/// - EXTRACT(DAY FROM '2024-01-15')
/// - EXTRACT(HOUR FROM current_timestamp)
pub(super) fn parse_extract_function(&mut self) -> Result<vibesql_ast::Expression, ParseError> {
// Parse the field (date/time unit) - reuse the interval unit parser
let field = self.parse_interval_unit()?;
// Expect FROM keyword
self.expect_keyword(Keyword::From)?;
// Parse the expression at primary level to avoid operator conflicts
let expr = self.parse_primary_expression()?;
// Expect closing parenthesis
self.expect_token(Token::RParen)?;
Ok(vibesql_ast::Expression::Extract { field, expr: Box::new(expr) })
}
/// Parse TRIM([position] [removal_char FROM] string)
/// SQL:1999 standard syntax
///
/// Forms:
/// - TRIM(string) -- remove spaces from both sides
/// - TRIM('x' FROM string) -- remove 'x' from both sides
/// - TRIM(BOTH 'x' FROM string) -- remove 'x' from both sides
/// - TRIM(LEADING 'x' FROM string) -- remove 'x' from start
/// - TRIM(TRAILING 'x' FROM string) -- remove 'x' from end
/// - TRIM(FROM string) -- remove spaces from both sides (explicit FROM)
/// - TRIM(BOTH FROM string) -- remove spaces from both sides (explicit BOTH)
/// - TRIM(LEADING FROM string) -- remove spaces from start (explicit LEADING)
/// - TRIM(TRAILING FROM string) -- remove spaces from end (explicit TRAILING)
pub(super) fn parse_trim_function(&mut self) -> Result<vibesql_ast::Expression, ParseError> {
let mut position: Option<vibesql_ast::TrimPosition> = None;
let removal_char: Option<Box<vibesql_ast::Expression>>;
// Check for BOTH/LEADING/TRAILING keywords
match self.peek() {
Token::Keyword { keyword: Keyword::Both, .. } => {
self.advance();
position = Some(vibesql_ast::TrimPosition::Both);
}
Token::Keyword { keyword: Keyword::Leading, .. } => {
self.advance();
position = Some(vibesql_ast::TrimPosition::Leading);
}
Token::Keyword { keyword: Keyword::Trailing, .. } => {
self.advance();
position = Some(vibesql_ast::TrimPosition::Trailing);
}
_ => {}
}
// Check if FROM comes immediately (no removal char specified)
// This handles: TRIM(FROM 'foo'), TRIM(BOTH FROM 'foo'), etc.
if matches!(self.peek(), Token::Keyword { keyword: Keyword::From, .. }) {
self.advance(); // consume FROM
removal_char = None; // Default to space
let string = self.parse_primary_expression()?;
self.expect_token(Token::RParen)?;
return Ok(vibesql_ast::Expression::Trim {
position,
removal_char,
string: Box::new(string),
});
}
// Try to parse the first expression (could be removal_char or string)
let first_expr = self.parse_primary_expression()?;
// Check if this is followed by FROM keyword
if matches!(self.peek(), Token::Keyword { keyword: Keyword::From, .. }) {
self.advance(); // consume FROM
// first_expr is the removal_char, now parse the string
removal_char = Some(Box::new(first_expr));
let string = self.parse_primary_expression()?;
self.expect_token(Token::RParen)?;
Ok(vibesql_ast::Expression::Trim { position, removal_char, string: Box::new(string) })
} else {
// No FROM keyword, so first_expr is the string
// removal_char defaults to None (which means space)
self.expect_token(Token::RParen)?;
Ok(vibesql_ast::Expression::Trim {
position,
removal_char: None,
string: Box::new(first_expr),
})
}
}
/// Parse SUBSTRING(string FROM start [FOR length] [USING unit])
/// SQL:1999 standard syntax - alternative to comma syntax
pub(super) fn parse_substring_function(
&mut self,
function_name: String,
) -> Result<vibesql_ast::Expression, ParseError> {
// Parse the string expression
let string_expr = self.parse_expression()?;
// Check which syntax: comma or FROM keyword
let (start_expr, length_expr) = if self.try_consume_keyword(Keyword::From) {
// FROM/FOR syntax: SUBSTRING(string FROM start [FOR length])
let start = self.parse_expression()?;
let length = if self.try_consume_keyword(Keyword::For) {
Some(self.parse_expression()?)
} else {
None
};
(start, length)
} else {
// Comma syntax: SUBSTRING(string, start [, length])
self.expect_token(Token::Comma)?;
let start = self.parse_expression()?;
let length = if matches!(self.peek(), Token::Comma) {
self.advance(); // consume comma
Some(self.parse_expression()?)
} else {
None
};
(start, length)
};
// Parse optional USING clause
let character_unit =
if matches!(self.peek(), Token::Keyword { keyword: Keyword::Using, .. }) {
self.advance(); // consume USING
Some(self.parse_character_unit()?)
} else {
None
};
self.expect_token(Token::RParen)?;
// For SUBSTRING, we represent it as a function call with 2 or 3 arguments
// The executor will handle the semantics
let mut args = vec![string_expr, start_expr];
if let Some(length) = length_expr {
args.push(length);
}
Ok(vibesql_ast::Expression::Function {
name: vibesql_ast::FunctionIdentifier::new(&function_name),
args,
character_unit,
})
}
/// Parse CHARACTERS or OCTETS keyword for USING clause
/// SQL:1999 Section 6.29: String value functions
pub(super) fn parse_character_unit(
&mut self,
) -> Result<vibesql_ast::CharacterUnit, ParseError> {
match self.peek() {
Token::Keyword { keyword: Keyword::Characters, .. } => {
self.advance();
Ok(vibesql_ast::CharacterUnit::Characters)
}
Token::Keyword { keyword: Keyword::Octets, .. } => {
self.advance();
Ok(vibesql_ast::CharacterUnit::Octets)
}
_ => Err(ParseError {
message: format!(
"Expected CHARACTERS or OCTETS after USING, found {:?}",
self.peek()
),
}),
}
}
}