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
use super::*;
impl DmlParser {
/// Analyzing the UPDATE statement (in its complete form, including the UPDATE token)
pub fn parse_update_statement(&mut self, ctx: &mut ParseContext) -> Result<Stmt, ParseError> {
let start_span = ctx.current_span();
ctx.expect_token(TokenKind::Update)?;
self.parse_update_after_token(ctx, start_span)
}
/// Analyzing the UPSERT statement (in its complete form, including the UPSERT token)
pub fn parse_upsert_statement(&mut self, ctx: &mut ParseContext) -> Result<Stmt, ParseError> {
let start_span = ctx.current_span();
ctx.expect_token(TokenKind::Upsert)?;
ctx.set_upsert_mode(true);
let result = self.parse_update_after_token(ctx, start_span);
ctx.set_upsert_mode(false);
result
}
/// Parse the UPDATE statement after the UPDATE token has been consumed.
pub fn parse_update_after_token(
&mut self,
ctx: &mut ParseContext,
start_span: crate::parser::ast::types::Span,
) -> Result<Stmt, ParseError> {
use crate::parser::ast::stmt::{SetClause, UpdateStmt, UpdateTarget};
use crate::parser::parsing::clause_parser::ClauseParser;
// Check whether it is UPSERT syntax.
let is_upsert = ctx.is_upsert_mode();
let target = if ctx.match_token(TokenKind::Vertex) {
if is_upsert && ctx.check_token(TokenKind::On) {
// UPSERT VERTEX ON <tag> SET ... WHERE id(vid) == <n>
ctx.match_token(TokenKind::On);
let tag_name = ctx.expect_identifier()?;
// Parse SET clause
let set_clause = if ctx.match_token(TokenKind::Set) {
ClauseParser::new().parse_set_clause(ctx)?
} else {
SetClause {
span: ctx.current_span(),
assignments: Vec::new(),
}
};
// Parse WHERE clause (used to specify vertex ID)
let where_clause = if ctx.match_token(TokenKind::Where) {
ctx.recover_clause(|_| Ok(None), |c| self.parse_expression(c).map(Some))?
} else {
None
};
// Parse YIELD clause
let yield_clause = if ctx.match_token(TokenKind::Yield) {
Some(ClauseParser::new().parse_yield_clause(ctx)?)
} else {
None
};
// Parse and discard an optional WHEN condition: upsert
// semantics do not gate the insert-or-update decision on it.
if ctx.match_token(TokenKind::When) {
ctx.recover_clause(|_| Ok(None), |c| self.parse_expression(c).map(Some))?;
}
// Extract vid from WHERE clause condition. Only the
// `id(vid) == <literal>` conjunct is consumed; any remaining
// terms stay as a regular filter predicate.
let (vid_value_opt, residual) =
Self::extract_id_with_residual(ctx, where_clause.as_ref(), &["vid"]);
let vid_value = vid_value_opt.ok_or_else(|| {
ParseError::new(
crate::parser::core::error::ParseErrorKind::SyntaxError,
"UPSERT VERTEX ON form requires WHERE id(vid) == <literal>".to_string(),
ctx.current_position(),
)
})?;
let vid_expr = CoreExpression::Literal(vid_value);
let vid_expr_meta = linkrs_core::types::expr::ExpressionMeta::with_span(
vid_expr,
ctx.current_span(),
);
let vid_id = ctx
.expression_context_clone()
.register_expression(vid_expr_meta);
let vid = ContextualExpression::new(vid_id, ctx.expression_context_clone());
let span = ctx.span_since(start_span.start);
return Ok(Stmt::Update(UpdateStmt {
span,
target: UpdateTarget::TagOnVertex {
vid: Box::new(vid),
tag_name,
},
set_clause,
where_clause: residual,
yield_clause,
is_upsert,
}));
}
match self.parse_update_vertex(ctx)? {
UpdateTarget::Vertex(vid) if ctx.match_token(TokenKind::On) => {
let tag_name = ctx.expect_identifier()?;
UpdateTarget::TagOnVertex {
vid: Box::new(vid),
tag_name,
}
}
other => other,
}
} else if ctx.match_token(TokenKind::Tag) {
// UPDATE TAG <tag> SET ... WHERE ... (bulk update scoped to one tag).
let tag_name = ctx.expect_identifier()?;
UpdateTarget::Tag(tag_name)
} else if ctx.match_token(TokenKind::Edge) {
if is_upsert && ctx.check_token(TokenKind::On) {
// UPSERT EDGE ON <edge_type> SET ... WHERE id(src) == <n> AND id(dst) == <m>
ctx.match_token(TokenKind::On);
let edge_type = ctx.expect_identifier()?;
// Parse SET clause
let set_clause = if ctx.match_token(TokenKind::Set) {
ClauseParser::new().parse_set_clause(ctx)?
} else {
SetClause {
span: ctx.current_span(),
assignments: Vec::new(),
}
};
// Parse WHERE clause
let where_clause = if ctx.match_token(TokenKind::Where) {
ctx.recover_clause(|_| Ok(None), |c| self.parse_expression(c).map(Some))?
} else {
None
};
// Optional edge rank (@rank) trailing the WHERE clause.
let rank = if ctx.match_token(TokenKind::At) {
Some(self.parse_expression(ctx)?)
} else {
None
};
// Parse YIELD clause
let yield_clause = if ctx.match_token(TokenKind::Yield) {
Some(ClauseParser::new().parse_yield_clause(ctx)?)
} else {
None
};
// Parse and discard an optional WHEN condition: upsert
// semantics do not gate the insert-or-update decision on it.
if ctx.match_token(TokenKind::When) {
ctx.recover_clause(|_| Ok(None), |c| self.parse_expression(c).map(Some))?;
}
// Extract src and dst from WHERE clause. Only the
// `id(src)/id(dst) == <literal>` conjuncts are consumed; any
// remaining terms stay as a regular filter predicate.
let (src_value_opt, after_src) =
Self::extract_id_with_residual(ctx, where_clause.as_ref(), &["src", "source"]);
let src_value = src_value_opt.ok_or_else(|| {
ParseError::new(
crate::parser::core::error::ParseErrorKind::SyntaxError,
"UPSERT EDGE ON form requires WHERE id(src) == <literal> AND id(dst) == <literal>"
.to_string(),
ctx.current_position(),
)
})?;
let (dst_value_opt, residual) = Self::extract_id_with_residual(
ctx,
after_src.as_ref(),
&["dst", "dest", "destination"],
);
let dst_value = dst_value_opt.ok_or_else(|| {
ParseError::new(
crate::parser::core::error::ParseErrorKind::SyntaxError,
"UPSERT EDGE ON form requires WHERE id(src) == <literal> AND id(dst) == <literal>"
.to_string(),
ctx.current_position(),
)
})?;
let make_literal_expr =
|ctx: &mut ParseContext, value: linkrs_core::Value| -> ContextualExpression {
let expr = CoreExpression::Literal(value);
let meta = linkrs_core::types::expr::ExpressionMeta::with_span(
expr,
ctx.current_span(),
);
let id = ctx.expression_context_clone().register_expression(meta);
ContextualExpression::new(id, ctx.expression_context_clone())
};
let src = make_literal_expr(ctx, src_value);
let dst = make_literal_expr(ctx, dst_value);
let span = ctx.span_since(start_span.start);
return Ok(Stmt::Update(UpdateStmt {
span,
target: UpdateTarget::Edge {
edge_type: Some(edge_type),
src,
dst,
rank,
},
set_clause,
where_clause: residual,
is_upsert,
yield_clause,
}));
}
self.parse_update_edge(ctx)?
} else {
// Check whether the syntax is correct for the UPSERT VERTEX vid ON tag_name command.
if is_upsert {
let vid = self.parse_expression(ctx)?;
if ctx.match_token(TokenKind::On) {
let tag_name = ctx.expect_identifier()?;
UpdateTarget::TagOnVertex {
vid: Box::new(vid),
tag_name,
}
} else {
UpdateTarget::Vertex(vid)
}
} else {
// A reserved keyword cannot start the update target. Consume
// the keyword so error recovery cannot reinterpret the rest
// of the input as a different statement.
if matches!(
ctx.current_token().kind,
TokenKind::Set
| TokenKind::Where
| TokenKind::When
| TokenKind::Yield
| TokenKind::On
| TokenKind::Of
) {
ctx.next_token();
return Err(ParseError::new(
crate::parser::core::error::ParseErrorKind::UnexpectedToken,
"Expected vertex id or edge target after UPDATE".to_string(),
ctx.current_position(),
));
}
// Check for old-style edge update syntax: src -> dst OF edge_type
// Try to parse as expression first, then check if followed by ->
let expr = self.parse_expression(ctx)?;
if ctx.check_token(TokenKind::Arrow) {
// This is edge update syntax: src -> dst [@rank] OF edge_type
self.parse_update_edge_short(ctx, expr)?
} else if ctx.match_token(TokenKind::On) {
let tag_name = ctx.expect_identifier()?;
UpdateTarget::TagOnVertex {
vid: Box::new(expr),
tag_name,
}
} else {
// Regular vertex update
UpdateTarget::Vertex(expr)
}
}
};
let set_clause = if ctx.match_token(TokenKind::Set) {
ClauseParser::new().parse_set_clause(ctx)?
} else {
SetClause {
span: ctx.current_span(),
assignments: Vec::new(),
}
};
let where_clause = if ctx.match_token(TokenKind::Where) || ctx.match_token(TokenKind::When)
{
ctx.recover_clause(|_| Ok(None), |c| self.parse_expression(c).map(Some))?
} else {
None
};
// Parse YIELD clause
let yield_clause = if ctx.match_token(TokenKind::Yield) {
Some(ClauseParser::new().parse_yield_clause(ctx)?)
} else {
None
};
let span = ctx.span_since(start_span.start);
Ok(Stmt::Update(UpdateStmt {
span,
target,
set_clause,
where_clause,
is_upsert,
yield_clause,
}))
}
fn parse_update_vertex(&mut self, ctx: &mut ParseContext) -> Result<UpdateTarget, ParseError> {
let vid = self.parse_expression(ctx)?;
Ok(UpdateTarget::Vertex(vid))
}
fn parse_update_edge(&mut self, ctx: &mut ParseContext) -> Result<UpdateTarget, ParseError> {
// `->` is an edge arrow inside this statement, not the JSON access
// operator, so expression parsing must not treat `-> 'key'` as a
// postfix JSON get.
ctx.with_edge_syntax_mode(|ctx| {
// Check whether it is UPSERT EDGE syntax: src -> dst @rank OF edge_type
// or UPDATE EDGE Syntax: OF edge_type FROM src TO dst [@rank]
// or UPDATE EDGE Syntax (short): src -> dst [@rank] OF edge_type
let is_upsert = ctx.is_upsert_mode();
// UPSERT EDGE always uses the short shape.
// For UPDATE, distinguish by syntax, not by whether the endpoint is a literal.
if is_upsert || ctx.check_token(TokenKind::Of) == false {
// UPDATE/UPSERT EDGE short shape: src -> dst [@rank] OF edge_type.
// We already hold `src` if the caller parsed nothing; parse it here.
let src = if ctx.check_token(TokenKind::Arrow) {
// Defensive: caller unexpectedly consumed nothing; handled below.
return Err(ParseError::new(
ParseErrorKind::UnexpectedToken,
"Expected edge source expression before ->".to_string(),
ctx.current_position(),
));
} else {
self.parse_expression(ctx)?
};
ctx.expect_token(TokenKind::Arrow)?;
let dst = self.parse_expression(ctx)?;
let rank = if ctx.match_token(TokenKind::At) {
Some(self.parse_expression(ctx)?)
} else {
None
};
ctx.expect_token(TokenKind::Of)?;
let edge_type = Some(ctx.expect_identifier()?);
Ok(UpdateTarget::Edge {
edge_type,
src,
dst,
rank,
})
} else {
// UPDATE EDGE long shape: OF edge_type FROM src TO dst [@rank].
ctx.expect_token(TokenKind::Of)?;
// Analyzing edge types
let edge_type = ctx.expect_identifier()?;
// Analyzing src and dst
ctx.expect_token(TokenKind::From)?;
let src = self.parse_expression(ctx)?;
ctx.expect_token(TokenKind::To)?;
let dst = self.parse_expression(ctx)?;
// Analysis of @rank (optional)
let rank = if ctx.match_token(TokenKind::At) {
Some(self.parse_expression(ctx)?)
} else {
None
};
Ok(UpdateTarget::Edge {
edge_type: Some(edge_type),
src,
dst,
rank,
})
}
})
}
/// Parse short edge update syntax: src -> dst [@rank] OF edge_type
/// This is called after parsing the src expression and seeing the -> token
fn parse_update_edge_short(
&mut self,
ctx: &mut ParseContext,
src: ContextualExpression,
) -> Result<UpdateTarget, ParseError> {
use crate::parser::ast::stmt::UpdateTarget;
ctx.with_edge_syntax_mode(|ctx| {
// The -> token
ctx.expect_token(TokenKind::Arrow)?;
// Parse dst
let dst = self.parse_expression(ctx)?;
// Optional @rank
let rank = if ctx.match_token(TokenKind::At) {
Some(self.parse_expression(ctx)?)
} else {
None
};
// OF edge_type
ctx.expect_token(TokenKind::Of)?;
let edge_type = Some(ctx.expect_identifier()?);
Ok(UpdateTarget::Edge {
edge_type,
src,
dst,
rank,
})
})
}
}