reifydb-rql 0.4.12

ReifyDB Query Language (RQL) parser and AST
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use crate::{
	Result,
	ast::{
		identifier::{MaybeQualifiedColumnIdentifier, UnqualifiedIdentifier},
		parse::Parser,
	},
	bump::BumpFragment,
	diagnostic::AstError,
	token::{
		operator::Operator,
		token::{Literal, Token, TokenKind},
	},
};

impl<'bump> Parser<'bump> {
	pub(crate) fn parse_identifier(&mut self) -> Result<UnqualifiedIdentifier<'bump>> {
		let token = self.consume(TokenKind::Identifier)?;
		Ok(UnqualifiedIdentifier::new(token))
	}

	/// Parse an identifier or keyword as an identifier (simple, no hyphen handling)
	/// Used in expression contexts where hyphens should remain as operators
	pub(crate) fn parse_as_identifier(&mut self) -> Result<UnqualifiedIdentifier<'bump>> {
		let token = self.consume_name()?;
		Ok(UnqualifiedIdentifier::new(token))
	}

	/// Parse an identifier that may contain hyphens, allowing keywords as the first token
	/// Used in DDL contexts where hyphenated identifiers are supported
	/// Consumes: (identifier|keyword) [-(identifier|keyword)]*
	/// Returns: UnqualifiedIdentifier with combined text
	pub(crate) fn parse_identifier_with_hyphens(&mut self) -> Result<UnqualifiedIdentifier<'bump>> {
		let first_token = self.advance()?;

		// Helper to check if a token can be used as an identifier part
		let is_identifier_like = |token: &Token| {
			matches!(
				token.kind,
				TokenKind::Identifier | TokenKind::Keyword(_) | TokenKind::Literal(Literal::Number)
			)
		};

		// Reject bare numbers as identifiers (e.g., `42`), but allow if followed by hyphen+identifier (e.g.,
		// `10-min`)
		if matches!(first_token.kind, TokenKind::Literal(Literal::Number)) {
			let has_hyphen_continuation = !self.is_eof()
				&& self.current_expect_operator(Operator::Minus).is_ok()
				&& self.position + 1 < self.tokens.len()
				&& is_identifier_like(&self.tokens[self.position + 1]);
			if !has_hyphen_continuation {
				return Err(AstError::UnexpectedToken {
					expected: "identifier (identifiers cannot start with digits)".to_string(),
					fragment: first_token.fragment.to_owned(),
				}
				.into());
			}
		}
		let start_line = first_token.fragment.line();
		let start_column = first_token.fragment.column();
		let first_fragment = first_token.fragment;

		// Check if next token is hyphen followed by identifier or keyword
		// If not, return what we have so far (no allocations beyond bump)
		if self.is_eof()
			|| self.current_expect_operator(Operator::Minus).is_err()
			|| self.position + 1 >= self.tokens.len()
			|| !is_identifier_like(&self.tokens[self.position + 1])
		{
			let text = self.bump().alloc_str(first_token.fragment.text());
			let fragment = BumpFragment::Statement {
				text,
				offset: 0,
				source_end: 0,
				line: start_line,
				column: start_column,
			};
			return Ok(UnqualifiedIdentifier::from_fragment(fragment));
		}

		// Build combined string in place for hyphenated identifiers
		let mut combined = String::from(first_token.fragment.text());

		// Look for pattern: - (identifier | keyword | number)
		while !self.is_eof()
			&& self.current_expect_operator(Operator::Minus).is_ok()
			&& self.position + 1 < self.tokens.len()
			&& is_identifier_like(&self.tokens[self.position + 1])
		{
			self.consume_operator(Operator::Minus)?; // consume hyphen
			let next_token = self.advance()?; // consume identifier or keyword or number
			combined.push('-');
			combined.push_str(next_token.fragment.text());
		}

		// Validate: no consecutive hyphens
		if combined.contains("--") {
			return Err(AstError::UnexpectedToken {
				expected: "identifier without consecutive hyphens".to_string(),
				fragment: first_fragment.to_owned(),
			}
			.into());
		}

		// Create Fragment with combined text
		let text = self.bump().alloc_str(&combined);
		let fragment = BumpFragment::Statement {
			text,
			offset: 0,
			source_end: 0,
			line: start_line,
			column: start_column,
		};

		Ok(UnqualifiedIdentifier::from_fragment(fragment))
	}

	/// Parse a double-colon-separated chain of identifiers: `a::b::c::d`
	/// Returns all segments. Caller splits namespace vs. name.
	pub(crate) fn parse_double_colon_separated_identifiers(&mut self) -> Result<Vec<UnqualifiedIdentifier<'bump>>> {
		let mut segments = vec![self.parse_identifier_with_hyphens()?];
		while !self.is_eof() && self.current_expect_operator(Operator::DoubleColon).is_ok() {
			self.consume_operator(Operator::DoubleColon)?;
			segments.push(self.parse_identifier_with_hyphens()?);
		}
		Ok(segments)
	}

	/// Parse a potentially qualified column identifier
	/// Handles patterns like: column, table.column, ns::table.column, ns1::ns2::table.column
	/// Supports hyphenated identifiers like: my-column, my-table.my-column
	pub(crate) fn parse_column_identifier(&mut self) -> Result<MaybeQualifiedColumnIdentifier<'bump>> {
		// Parse :: separated identifiers for namespace::table qualification
		let mut ns_table_segments = self.parse_double_colon_separated_identifiers()?;

		// Check for .column (dot-separated column access)
		if !self.is_eof() && self.current_expect_operator(Operator::Dot).is_ok() {
			self.consume_operator(Operator::Dot)?;
			let col = self.parse_identifier_with_hyphens()?;

			// ns_table_segments = [namespace..., table], col = column
			let table = ns_table_segments.pop().unwrap();
			let namespace: Vec<_> = ns_table_segments.into_iter().map(|s| s.into_fragment()).collect();

			Ok(MaybeQualifiedColumnIdentifier::with_shape(
				namespace,
				table.into_fragment(),
				col.into_fragment(),
			))
		} else {
			// No dot found - all segments are just identifiers
			Self::segments_to_column_identifier(ns_table_segments)
		}
	}

	fn segments_to_column_identifier(
		mut segments: Vec<UnqualifiedIdentifier<'bump>>,
	) -> Result<MaybeQualifiedColumnIdentifier<'bump>> {
		match segments.len() {
			1 => {
				let col = segments.remove(0);
				Ok(MaybeQualifiedColumnIdentifier::unqualified(col.into_fragment()))
			}
			2 => {
				let table = segments.remove(0);
				let col = segments.remove(0);
				Ok(MaybeQualifiedColumnIdentifier::with_shape(
					vec![],
					table.into_fragment(),
					col.into_fragment(),
				))
			}
			_ => {
				let col = segments.pop().unwrap();
				let table = segments.pop().unwrap();
				let namespace: Vec<_> = segments.into_iter().map(|s| s.into_fragment()).collect();
				Ok(MaybeQualifiedColumnIdentifier::with_shape(
					namespace,
					table.into_fragment(),
					col.into_fragment(),
				))
			}
		}
	}
}

#[cfg(test)]
pub mod tests {
	use crate::{
		ast::{
			ast::{
				Ast::{Create, Identifier},
				AstCreate::Namespace,
			},
			parse::parse,
		},
		bump::Bump,
		token::tokenize,
	};

	#[test]
	fn identifier() {
		let bump = Bump::new();
		let source = "x";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Identifier(identifier) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};
		assert_eq!(identifier.text(), "x");
	}

	#[test]
	fn identifier_with_underscore() {
		let bump = Bump::new();
		let source = "some_identifier";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Identifier(identifier) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};
		assert_eq!(identifier.text(), "some_identifier");
	}

	#[test]
	fn identifier_with_hyphen_context_aware() {
		let bump = Bump::new();
		// Test hyphenated identifier in CREATE NAMESPACE context
		let source = "CREATE NAMESPACE my-identifier";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "my-identifier");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_with_multiple_hyphens() {
		let bump = Bump::new();
		// Test identifier with multiple hyphens in CREATE NAMESPACE context
		let source = "CREATE NAMESPACE user-profile-data";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "user-profile-data");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_with_double_hyphens_should_fail() {
		let bump = Bump::new();
		// When using unquoted identifiers, double hyphens are tokenized as two minus operators
		// Input: "CREATE NAMESPACE name--space"
		// Tokens: [CREATE, NAMESPACE, name, -, -, space]
		//
		// The parser should:
		// 1. Parse "CREATE NAMESPACE name" successfully
		// 2. See trailing tokens "- - space"
		// 3. REJECT the trailing tokens as invalid after a CREATE statement
		//
		// Rationale: CREATE statements (and all DDL) should stand alone. Trailing tokens
		// are almost certainly a user error. If consecutive hyphens are intended, use backticks.

		let source = "CREATE NAMESPACE name--space";
		let tokens: Vec<_> = tokenize(&bump, source).unwrap().into_iter().collect();
		assert_eq!(tokens.len(), 6); // CREATE, NAMESPACE, name, -, -, space

		let result = parse(&bump, source, tokens);

		// Parser should reject this with an error about unexpected trailing tokens
		assert!(result.is_err(), "Parser should reject trailing tokens after CREATE statement");

		// Verify error message is helpful
		if let Err(e) = result {
			let error_msg = format!("{:?}", e);
			assert!(
				error_msg.contains("unexpected") || error_msg.contains("DDL"),
				"Error should mention unexpected tokens or DDL: {}",
				error_msg
			);
		}
	}

	#[test]
	fn identifier_backtick_with_hyphen() {
		let bump = Bump::new();
		let source = "`my-identifier`";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Identifier(identifier) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};
		assert_eq!(identifier.text(), "my-identifier");
	}

	#[test]
	fn identifier_backtick_without_hyphen() {
		let bump = Bump::new();
		// Test that backticks work for simple identifiers without special characters
		let source = "`myidentifier`";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Identifier(identifier) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};
		assert_eq!(identifier.text(), "myidentifier");
	}

	#[test]
	fn identifier_backtick_with_underscore() {
		let bump = Bump::new();
		// Test that backticks work for identifiers with underscores
		let source = "`my_identifier`";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Identifier(identifier) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};
		assert_eq!(identifier.text(), "my_identifier");
	}

	#[test]
	fn identifier_with_hyphen_and_number_suffix() {
		let bump = Bump::new();
		// Number suffix is valid: twap-10min
		let source = "CREATE NAMESPACE twap-10min";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "twap-10min");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_with_hyphen_and_number_middle() {
		let bump = Bump::new();
		// Number in middle is valid: avg-10min-window
		let source = "CREATE NAMESPACE avg-10min-window";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "avg-10min-window");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_with_keyword_and_number() {
		let bump = Bump::new();
		let source = "CREATE NAMESPACE create-2024-table";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "create-2024-table");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_digit_starting() {
		let bump = Bump::new();
		let source = "CREATE NAMESPACE 10min";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "10min");
		} else {
			panic!("Expected namespace creation");
		}
	}

	#[test]
	fn identifier_digit_starting_with_hyphen() {
		let bump = Bump::new();
		let source = "CREATE NAMESPACE 10min-window";
		let tokens = tokenize(&bump, source).unwrap().into_iter().collect();
		let mut result = parse(&bump, source, tokens).unwrap();
		assert_eq!(result.len(), 1);

		let Create(create) = result.pop().unwrap().nodes.pop().unwrap() else {
			panic!()
		};

		if let Namespace(ns) = create {
			assert_eq!(ns.namespace.segments[0].text(), "10min-window");
		} else {
			panic!("Expected namespace creation");
		}
	}
}