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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//! CREATE TABLE parsing
use vibesql_types::DataType;
use super::super::*;
impl Parser {
/// Parse CREATE TABLE statement
/// Supports CREATE [TEMP|TEMPORARY] TABLE syntax (SQLite compatibility)
pub(in crate::parser) fn parse_create_table_statement(
&mut self,
) -> Result<vibesql_ast::CreateTableStmt, ParseError> {
self.expect_keyword(Keyword::Create)?;
// Check for TEMP or TEMPORARY keyword (SQLite compatibility)
// Temporary tables are stored in a separate "temp" schema
let temporary = if self.peek_keyword(Keyword::Temp) || self.peek_keyword(Keyword::Temporary)
{
self.advance();
true
} else {
false
};
self.expect_keyword(Keyword::Table)?;
// Check for optional IF NOT EXISTS clause
let if_not_exists = if self.peek_keyword(Keyword::If) {
self.advance(); // consume IF
self.expect_keyword(Keyword::Not)?;
self.expect_keyword(Keyword::Exists)?;
true
} else {
false
};
// Parse table name with quoted flag (supports schema.table).
// Capture the verbatim source spelling of the table-name token before
// `parse_table_ref` normalizes (de-quotes) it. SQLite echoes the table
// name exactly as written — including its quoting form and casing — in
// the "table ... already exists" error, whereas `table_name` below holds
// the normalized identifier used for catalog lookups. For a
// `schema.name` qualified reference the table-name token is the one
// following the `.` separator, so we record the cursor position of the
// *final* identifier token consumed by the table ref. (Mirrors the
// CREATE TRIGGER mechanism from #5538.)
let name_start_pos = self.current_position();
let table = self.parse_table_ref()?;
let name_token_pos = self.current_position().saturating_sub(1).max(name_start_pos);
let name_source = self.token_source_at(name_token_pos).map(str::to_string);
// Check for CREATE TABLE ... AS SELECT syntax
if self.peek_keyword(Keyword::As) {
self.advance(); // consume AS
// Parse the SELECT statement
let select_stmt = self.parse_select_statement()?;
// Expect semicolon or EOF
if matches!(self.peek(), Token::Semicolon) {
self.advance();
}
return Ok(vibesql_ast::CreateTableStmt {
temporary,
if_not_exists,
table_name: table.full_name(),
columns: Vec::new(),
table_constraints: Vec::new(),
table_options: Vec::new(),
quoted: table.is_any_quoted(),
name_source,
as_query: Some(Box::new(select_stmt)),
without_rowid: false, // CREATE TABLE AS SELECT cannot be WITHOUT ROWID
});
}
// Parse column definitions and table constraints
self.expect_token(Token::LParen)?;
let mut columns = Vec::new();
let mut table_constraints = Vec::new();
loop {
// Check if this is a table-level constraint (including CONSTRAINT keyword)
if self.peek_keyword(Keyword::Constraint)
|| self.peek_keyword(Keyword::Primary)
|| self.peek_keyword(Keyword::Foreign)
|| self.peek_keyword(Keyword::Unique)
|| self.peek_keyword(Keyword::Check)
|| self.peek_keyword(Keyword::Fulltext)
{
table_constraints.push(self.parse_table_constraint()?);
if matches!(self.peek(), Token::Comma) {
self.advance();
continue;
} else {
break;
}
}
// Parse column name
let name = match self.peek() {
Token::Identifier(col) | Token::DelimitedIdentifier(col) => {
let c = col.clone();
self.advance();
c
}
// Allow unreserved keywords (like TIMESTAMP, DATE, TIME, INTERVAL) as column names
// SQL:1999: normalize to lowercase when used as unquoted identifier
Token::Keyword { keyword: kw, .. } if kw.can_be_identifier() => {
let col_name = format!("{}", kw).to_lowercase();
self.advance();
col_name
}
_ => return Err(ParseError { message: "Expected column name".to_string() }),
};
// Parse data type (optional for SQLite compatibility)
// SQLite allows typeless columns with BLOB affinity
// Also check for generated column syntax: AS(expression) or GENERATED ALWAYS AS(expression)
let (data_type, is_exact_integer_type, mut generated_expr) =
if self.peek_keyword(Keyword::As) {
// Generated column (short form): AS(expression)
self.advance(); // consume AS
self.expect_token(Token::LParen)?;
let expr = self.parse_expression()?;
self.expect_token(Token::RParen)?;
// Skip optional STORED or VIRTUAL keyword
if self.peek_keyword(Keyword::Stored) || self.peek_keyword(Keyword::Virtual) {
self.advance();
}
// Generated columns have the type inferred from the expression
// For now, use BLOB affinity (will be refined by type inference)
(DataType::BinaryLargeObject, false, Some(Box::new(expr)))
} else if self.is_column_constraint_or_separator() {
// No data type specified - use BLOB affinity (SQLite default)
(DataType::BinaryLargeObject, false, None)
} else {
let (dt, is_int) = self.parse_data_type_with_integer_flag()?;
(dt, is_int, None)
};
// Check for GENERATED ALWAYS AS (expression) [STORED|VIRTUAL] after data type
if generated_expr.is_none() && self.peek_keyword(Keyword::Generated) {
self.advance(); // consume GENERATED
self.expect_keyword(Keyword::Always)?;
self.expect_keyword(Keyword::As)?;
self.expect_token(Token::LParen)?;
let expr = self.parse_expression()?;
self.expect_token(Token::RParen)?;
// Skip optional STORED or VIRTUAL keyword
if self.peek_keyword(Keyword::Stored) || self.peek_keyword(Keyword::Virtual) {
self.advance();
}
generated_expr = Some(Box::new(expr));
}
// Also check for short form AS (expression) after data type
if generated_expr.is_none() && self.peek_keyword(Keyword::As) {
self.advance(); // consume AS
self.expect_token(Token::LParen)?;
let expr = self.parse_expression()?;
self.expect_token(Token::RParen)?;
// Skip optional STORED or VIRTUAL keyword
if self.peek_keyword(Keyword::Stored) || self.peek_keyword(Keyword::Virtual) {
self.advance();
}
generated_expr = Some(Box::new(expr));
}
// Parse optional DEFAULT clause (before COMMENT, per MySQL standard).
// SQLite also allows DEFAULT to appear interleaved with column constraints
// (e.g. `idx UNIQUE DEFAULT NULL`), so `parse_column_constraints` below may
// also consume a DEFAULT clause and return it via its second tuple element.
let mut default_value = if self.peek_keyword(Keyword::Default) {
self.advance(); // consume DEFAULT
Some(Box::new(self.parse_expression()?))
} else {
None
};
// Parse optional COMMENT clause (after DEFAULT, per MySQL standard)
let comment = if self.peek_keyword(Keyword::Comment) {
self.advance(); // consume COMMENT
match self.peek() {
Token::String(s) => {
let c = s.clone();
self.advance();
Some(c)
}
_ => {
return Err(ParseError {
message: "Expected string literal after COMMENT".to_string(),
})
}
}
} else {
None
};
// Parse column constraints (which may include NOT NULL and an interleaved DEFAULT)
let (constraints, mid_default) = self.parse_column_constraints()?;
if let Some(d) = mid_default {
if default_value.is_some() {
return Err(ParseError {
message: "Multiple DEFAULT clauses for the same column".to_string(),
});
}
default_value = Some(d);
}
// Determine nullability based on constraints
let nullable = !constraints
.iter()
.any(|c| matches!(&c.kind, vibesql_ast::ColumnConstraintKind::NotNull));
columns.push(vibesql_ast::ColumnDef {
name,
data_type,
nullable,
constraints,
default_value,
comment,
generated_expr,
is_exact_integer_type,
});
if matches!(self.peek(), Token::Comma) {
self.advance();
} else {
break;
}
}
self.expect_token(Token::RParen)?;
// Parse optional table options (MySQL extensions)
let table_options = self.parse_table_options()?;
// Parse optional WITH OIDS / WITHOUT OIDS clause (PostgreSQL)
// or WITHOUT ROWID clause (SQLite)
let mut without_rowid = false;
if self.peek_keyword(Keyword::With) {
self.advance(); // consume WITH
self.expect_keyword(Keyword::Oids)?;
} else if self.peek_keyword(Keyword::Without) {
self.advance(); // consume WITHOUT
if self.peek_keyword(Keyword::Oids) {
self.advance(); // consume OIDS
} else if self.peek_keyword(Keyword::Rowid) {
self.advance(); // consume ROWID (SQLite WITHOUT ROWID tables)
without_rowid = true;
} else {
return Err(ParseError {
message: "Expected OIDS or ROWID after WITHOUT".to_string(),
});
}
}
// Expect semicolon or EOF
if matches!(self.peek(), Token::Semicolon) {
self.advance();
}
Ok(vibesql_ast::CreateTableStmt {
temporary,
if_not_exists,
table_name: table.full_name(),
columns,
table_constraints,
table_options,
quoted: table.is_any_quoted(),
name_source,
as_query: None,
without_rowid,
})
}
/// Check if the current token indicates a column constraint or column separator
/// rather than a data type. This enables SQLite-style typeless column definitions.
///
/// Returns true if the next token is:
/// - A column separator: `,` or `)`
/// - A column constraint keyword: `NULL`, `NOT`, `PRIMARY`, `UNIQUE`, `CHECK`, `REFERENCES`,
/// `AUTO_INCREMENT`, `AUTOINCREMENT`, `KEY`, `CONSTRAINT`
/// - A column modifier: `DEFAULT`, `COMMENT`
fn is_column_constraint_or_separator(&self) -> bool {
match self.peek() {
// Column separators
Token::Comma | Token::RParen => true,
// Column constraint keywords
Token::Keyword { keyword: Keyword::Null, .. } => true,
Token::Keyword { keyword: Keyword::Not, .. } => true,
Token::Keyword { keyword: Keyword::Primary, .. } => true,
Token::Keyword { keyword: Keyword::Unique, .. } => true,
Token::Keyword { keyword: Keyword::Check, .. } => true,
Token::Keyword { keyword: Keyword::References, .. } => true,
Token::Keyword { keyword: Keyword::AutoIncrement, .. } => true,
Token::Keyword { keyword: Keyword::Key, .. } => true,
Token::Keyword { keyword: Keyword::Constraint, .. } => true,
// Column modifiers (appear after data type normally)
Token::Keyword { keyword: Keyword::Default, .. } => true,
Token::Keyword { keyword: Keyword::Comment, .. } => true,
Token::Keyword { keyword: Keyword::Collate, .. } => true,
// Generated column keywords (SQLite)
Token::Keyword { keyword: Keyword::Generated, .. } => true,
Token::Keyword { keyword: Keyword::As, .. } => true,
// Not a constraint/separator - likely a data type
_ => false,
}
}
}