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
use super::*;
impl Parser {
/// Parse identifier-based expressions (column references, qualified names)
pub(super) fn parse_identifier_expression(
&mut self,
) -> Result<Option<vibesql_ast::Expression>, ParseError> {
// Extract identifier string from token (handles regular identifiers and
// keywords-as-identifiers)
// Track whether identifier is quoted for proper SQL:1999 case handling
let (first, first_quoted) = match self.peek() {
Token::Identifier(id) => {
let name = id.clone();
self.advance();
(name, false)
}
Token::DelimitedIdentifier(id) => {
// Delimited identifiers (backtick/double-quote) preserve case
let name = id.clone();
self.advance();
(name, true)
}
Token::Keyword { keyword: kw, .. } if kw.can_be_identifier() => {
// Allow certain keywords (MONTH, YEAR, DAY, etc.) to be used as column names
// SQL:1999 normalizes unquoted identifiers to lowercase
let name = format!("{}", kw).to_lowercase();
self.advance();
(name, false)
}
_ => return Ok(None),
};
// Check for hex/binary literal (x'...' or b'...')
let first_upper = first.to_uppercase();
if (first_upper == "X" || first_upper == "B") && matches!(self.peek(), Token::String(_)) {
if let Token::String(s) = self.peek() {
let string_val = s.clone();
self.advance();
if first_upper == "X" {
// Parse hex literal: x'303132' -> "\x30\x31\x32"
// Validate and decode hex string
if string_val.len() % 2 != 0 {
return Err(ParseError {
message: format!(
"Hex literal must have even number of digits: x'{}'",
string_val
),
});
}
let mut bytes = Vec::new();
for i in (0..string_val.len()).step_by(2) {
let hex_byte = &string_val[i..i + 2];
match u8::from_str_radix(hex_byte, 16) {
Ok(byte) => bytes.push(byte),
Err(_) => {
return Err(ParseError {
message: format!(
"Invalid hex digit in literal: x'{}'",
string_val
),
});
}
}
}
// Convert bytes to string (UTF-8 lossy conversion for compatibility)
let result = arcstr::ArcStr::from(String::from_utf8_lossy(&bytes).as_ref());
return Ok(Some(vibesql_ast::Expression::Literal(
vibesql_types::SqlValue::Varchar(result),
)));
} else {
// Parse binary literal: b'01010101' -> "U"
if !string_val.chars().all(|c| c == '0' || c == '1') {
return Err(ParseError {
message: format!(
"Binary literal must contain only 0 and 1: b'{}'",
string_val
),
});
}
if string_val.len() % 8 != 0 {
return Err(ParseError {
message: format!(
"Binary literal must have length divisible by 8: b'{}'",
string_val
),
});
}
let mut bytes = Vec::new();
for i in (0..string_val.len()).step_by(8) {
let binary_byte = &string_val[i..i + 8];
match u8::from_str_radix(binary_byte, 2) {
Ok(byte) => bytes.push(byte),
Err(_) => {
return Err(ParseError {
message: format!("Invalid binary literal: b'{}'", string_val),
});
}
}
}
// Convert bytes to string
let result = arcstr::ArcStr::from(String::from_utf8_lossy(&bytes).as_ref());
return Ok(Some(vibesql_ast::Expression::Literal(
vibesql_types::SqlValue::Varchar(result),
)));
}
}
}
// Check for function call (identifier followed by '(')
if matches!(self.peek(), Token::LParen) {
// This is handled by parse_function_call, return None
// We need to rewind
self.position -= 1;
return Ok(None);
}
// Check for qualified column reference (table.column or schema.table.column)
// Keywords are allowed as column names when qualified (e.g., ss.year)
if matches!(self.peek(), Token::Symbol('.')) {
self.advance(); // consume first '.'
let (second, second_quoted) = match self.peek() {
Token::Identifier(id) => {
let name = id.clone();
self.advance();
(name, false)
}
Token::DelimitedIdentifier(id) => {
let name = id.clone();
self.advance();
(name, true)
}
Token::Keyword { keyword: kw, .. } => {
// Allow keywords as column names when qualified (e.g., table.year)
// SQL:1999 normalizes unquoted identifiers to lowercase
let name = kw.to_string().to_lowercase();
self.advance();
(name, false)
}
_ => {
return Err(ParseError {
message: "Expected identifier after '.'".to_string(),
});
}
};
// Check for pseudo-variable (OLD.column or NEW.column)
let first_upper = first.to_uppercase();
if first_upper == "OLD" || first_upper == "NEW" {
let pseudo_table = if first_upper == "OLD" {
vibesql_ast::PseudoTable::Old
} else {
vibesql_ast::PseudoTable::New
};
return Ok(Some(vibesql_ast::Expression::PseudoVariable {
pseudo_table,
column: second,
}));
}
// Check for three-part name (schema.table.column)
if matches!(self.peek(), Token::Symbol('.')) {
self.advance(); // consume second '.'
let (third, third_quoted) = match self.peek() {
Token::Identifier(id) => {
let name = id.clone();
self.advance();
(name, false)
}
Token::DelimitedIdentifier(id) => {
let name = id.clone();
self.advance();
(name, true)
}
Token::Keyword { keyword: kw, .. } => {
let name = kw.to_string().to_lowercase();
self.advance();
(name, false)
}
_ => {
return Err(ParseError {
message: "Expected column name after '.'".to_string(),
});
}
};
// schema.table.column
Ok(Some(vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::fully_qualified(
&first,
first_quoted,
&second,
second_quoted,
&third,
third_quoted,
),
)))
} else {
// table.column (two-part name)
Ok(Some(vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::qualified(
&first,
first_quoted,
&second,
second_quoted,
),
)))
}
} else {
// Simple column reference
Ok(Some(vibesql_ast::Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
&first,
first_quoted,
))))
}
}
}