Formal Grammar for Query Language
This is the definition for the GSQL Query Language syntax. It is defined as a set of rules expressed in EBNF notation.

Notation Used to Define Syntax

This defines the EBNF notation used to describe the syntax. Rules contains terminal and non-terminal symbols. A terminal symbol is a base-level symbol which expresses literal output. All symbols in single or double quotes (e.g., '+', "=", ")", "10") are terminal symbols. A non-terminal symbol is defined as some combination of terminal and non-terminal symbols. The left-hand side of a rule is always a non-terminal; this rule defines the non-terminal. The example rule below defines assignmentStmt (that is, an Assignment Statement) to be a name followed by an equal sign followed by an expression, operator, and expression with a terminating semi-colon. AssignmentStmt, name, and expr are all non-terminals. Additionally, all KEYWORDS are in all-capitals and are terminal symbols. The ":=" is part of EBNF and states the left hand side can be expanded to the right hand side.
EBNF Syntax example: A rule
1
assignmentStmt := name "=" expr op expr ";"
Copied!
A vertical bar | in EBNF indicates choice. Choose either the symbol on the left or on the right. A sequence of vertical bars means choose any one of the symbols in the sequence.
EBNF Syntax: vertical bar
1
op := "+" | "-" | "*" | "/"
Copied!
Square brackets [ ] indicate an optional part or group of symbols. Parentheses ( ) group symbols together. The rule below defines a constant to be one, two, or three digits preceded by an optional plus or minus sign.
EBNF Syntax: Square brackets and parentheses
1
constant := ["+" | "-"] (digit | (digit digit) | (digit digit digit))
Copied!
Star * and plus + are symbols in EBNF for closure. Star means zero or more occurrences, and plus means one or more occurrences. The following defines intConstant to be an optional plus or minus followed by one or more digits. It also defines floatConstant to be an optional plus or minus followed by zero or more digits followed by a decimal followed by one or more digits. The star and plus also can be applied to groups of symbols as in the definition of list. The non-terminal list is defined as a parenthesized list of comma-separated expressions (expr). The list has at least one expression which can be followed by zero or more comma-expression pairs.
EBNF Syntax: square brackets and parentheses
1
intConstant := ["+" | "-"] digit+
2
floatConstant := ["+" | "-"] digit* "." digit+
3
list := "(" expr ["," expr]* ")"
Copied!

GSQL Query Language EBNF

1
#########################################################
2
## EBNF for GSQL Query Language
3
4
createQuery := CREATE [OR REPLACE] [DISTRIBUTED] QUERY queryName
5
"(" [parameterList] ")"
6
[FOR GRAPH graphName]
7
[RETURNS "(" baseType | accumType ")"]
8
[API "(" stringLiteral ")"]
9
[SYNTAX syntaxName]
10
"{" queryBody "}"
11
12
interpretQuery := INTERPRET QUERY "(" ")"
13
[FOR GRAPH graphName]
14
[SYNTAX syntaxName]
15
"{" queryBody "}"
16
17
parameterValueList := parameterValue ["," parameterValue]*
18
parameterValue := parameterConstant
19
| "[" parameterValue ["," parameterValue]* "]" // BAG or SET
20
| "(" stringLiteral "," stringLiteral ")" // a generic VERTEX value
21
parameterConstant := numeric | stringLiteral | TRUE | FALSE
22
parameterList := parameterType paramName ["=" constant]
23
["," parameterType paramName ["=" constant]]*
24
25
syntaxName := name
26
27
queryBody := [typedefs] [declExceptStmts] queryBodyStmts
28
typedefs := (typedef ";")+
29
declStmts := (declStmt ";")+
30
declStmt := baseDeclStmt | accumDeclStmt | fileDeclStmt
31
declExceptStmts := (declExceptStmt ";")+
32
queryBodyStmts := (queryBodyStmt ";")+
33
queryBodyStmt := assignStmt // Assignment
34
| vSetVarDeclStmt // Declaration
35
| declStmts // Declaration
36
| lAccumAssignStmt // Assignment
37
| gAccumAssignStmt // Assignment
38
| gAccumAccumStmt // Assignment
39
| funcCallStmt // Function Call
40
| selectStmt // Select
41
| queryBodyCaseStmt // Control Flow
42
| queryBodyIfStmt // Control Flow
43
| queryBodyWhileStmt // Control Flow
44
| queryBodyForEachStmt // Control Flow
45
| BREAK // Control Flow
46
| CONTINUE // Control Flow
47
| updateStmt // Data Modification
48
| insertStmt // Data Modification
49
| queryBodyDeleteStmt // Data Modification
50
| printStmt // Output
51
| printlnStmt // Output
52
| logStmt // Output
53
| returnStmt // Output
54
| raiseStmt // Exception
55
| tryStmt // Exception
56
57
installQuery := INSTALL QUERY [installOptions] ( "*" | ALL |queryName ["," queryName]* )
58
runQuery := (RUN | INTERPRET) QUERY [runOptions] queryName "(" parameterValueList ")"
59
60
showQuery := SHOW QUERY queryName
61
dropQuery := DROP QUERY ( "*" | ALL | queryName ["," queryName]* )
62
63
#########################################################
64
## Types and names
65
66
lowercase := [a-z]
67
uppercase := [A-Z]
68
letter := lowercase | uppercase
69
digit := [0-9]
70
integer := ["-"]digit+
71
real := ["-"]("."digit+) | ["-"](digit+"."digit*)
72
73
numeric := integer | real
74
stringLiteral := '"' [~["] | '\\' ('"' | '\\')]* '"'
75
76
name := (letter | "_") [letter | digit | "_"]* // can be a single "_" or start with "_"
77
graphName := name
78
queryName := name
79
paramName := name
80
vertexType := name
81
edgeType := name
82
accumName := name
83
vertexSetName := name
84
attrName := name
85
varName := name
86
tupleType := name
87
fieldName := name
88
funcName := name
89
90
type := baseType | tupleType | accumType | STRING COMPRESS
91
92
baseType := INT
93
| UINT
94
| FLOAT
95
| DOUBLE
96
| STRING
97
| BOOL
98
| VERTEX ["<" vertexType ">"]
99
| EDGE
100
| JSONOBJECT
101
| JSONARRAY
102
| DATETIME
103
104
filePath := paramName | stringLiteral
105
106
typedef := TYPEDEF TUPLE "<" tupleFields ">" tupleType
107
108
tupleFields := (baseType fieldName) | (fieldName baseType)
109
["," (baseType fieldName) | (fieldName baseType)]*
110
111
parameterType := baseType
112
| [ SET | BAG ] "<" baseType ">"
113
| FILE
114
115
#########################################################
116
## Accumulators
117
118
accumDeclStmt := accumType localAccumName ["=" constant]
119
["," localAccumName ["=" constant]]*
120
| [STATIC] accumType globalAccumName ["=" constant]
121
["," globalAccumName ["=" constant]]*
122
localAccumName := "@"accumName;
123
globalAccumName := "@@"accumName;
124
125
126
accumType := "SumAccum" "<" ( INT | FLOAT | DOUBLE | STRING | STRING COMPRESS) ">"
127
| "MaxAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
128
| "MinAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
129
| "AvgAccum"
130
| "OrAccum"
131
| "AndAccum"
132
| "BitwiseOrAccum"
133
| "BitwiseAndAccum"
134
| "ListAccum" "<" type ">"
135
| "SetAccum" "<" elementType ">"
136
| "BagAccum" "<" elementType ">"
137
| "MapAccum" "<" elementType "," (baseType | accumType | tupleType) ">"
138
| "HeapAccum" "<" tupleType ">" "(" simpleSize "," fieleName [ASC | DESC]
139
["," fieldName [ASC | DESC]]* ")"
140
| "GroupByAccum" "<" elementType fieldName ["," elementType fieldName]* ,
141
accumType fieldName ["," accumType fieldName]* ">"
142
| "ArrayAccum" "<" accumName ">"
143
144
elementType := baseType | tupleType | STRING COMPRESS
145
146
gAccumAccumStmt := globalAccumName "+=" expr
147
148
###############################################################################
149
## Operators, Functions, and Expressions
150
151
constant := numeric | stringLiteral | TRUE | FALSE | GSQL_UINT_MAX
152
| GSQL_INT_MAX | GSQL_INT_MIN | TO_DATETIME "(" stringLiteral ")"
153
154
mathOperator := "*" | "/" | "%" | "+" | "-" | "<<" | ">>" | "&" | "|"
155
156
condition := expr
157
| expr comparisonOperator expr
158
| expr [ NOT ] IN setBagExpr
159
| expr IS [ NOT ] NULL
160
| expr BETWEEN expr AND expr
161
| "(" condition ")"
162
| NOT condition
163
| condition (AND | OR) condition
164
| (TRUE | FALSE)
165
| expr [NOT] LIKE expr [ESCAPE escape_char]
166
167
comparisonOperator := "<" | "<=" | ">" | ">=" | "==" | "!="
168
169
aggregator := COUNT | MAX | MIN | AVG | SUM
170
171
expr := name
172
| globalAccumName
173
| name "." name
174
| name "." localAccumName ["\'"]
175
| name "." name "." name "(" [argList] ")"
176
| name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ]
177
| name ["<" type ["," type]* ">"] "(" [argList] ")"
178
| name "." localAccumName ("." name "(" [argList] ")")+ ["." name]
179
| globalAccumName ("." name "(" [argList] ")")+ ["." name]
180
| COALESCE "(" [argList] ")"
181
| aggregator "(" [DISTINCT] setBagExpr ")"
182
| ISEMPTY "(" setBagExpr ")"
183
| expr mathOperator expr
184
| "-" expr
185
| "(" expr ")"
186
| "(" argList "->" argList ")" // key value pair for MapAccum
187
| "[" argList "]" // a list
188
| constant
189
| setBagExpr
190
| name "(" argList ")" // function call or a tuple object
191
192
setBagExpr := name
193
| globalAccumName
194
| name "." name
195
| name "." localAccumName
196
| name "." localAccumName ("." name "(" [argList] ")")+
197
| name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ]
198
| globalAccumName ("." name "(" [argList] ")")+
199
| setBagExpr (UNION | INTERSECT | MINUS) setBagExpr
200
| "(" argList ")"
201
| "(" setBagExpr ")"
202
203
#########################################################
204
## Declarations and Assignments ##
205
206
## Declarations ##
207
baseDeclStmt := baseType name ["=" expr] ["," name ["=" expr]]*
208
fileDeclStmt := FILE fileVar "(" filePath ")"
209
fileVar := name
210
211
localVarDeclStmt := baseType varName "=" expr
212
213
vSetVarDeclStmt := vertexSetName ["(" vertexType ")"]
214
"=" (seedSet | simpleSet | selectBlock)
215
216
simpleSet := vertexSetName | "(" simpleSet ")"
217
| simpleSet (UNION | INTERSECT | MINUS) simpleSet
218
219
seedSet := "{" [seed ["," seed ]*] "}"
220
seed := '_'
221
| ANY
222
| vertexSetName
223
| globalAccumName
224
| vertexType ".*"
225
| paramName
226
| "SelectVertex" selectVertParams
227
228
selectVertParams := "(" filePath "," columnId "," (columnId | name) ","
229
stringLiteral "," (TRUE | FALSE) ")" ["." FILTER "(" condition ")"]
230
231
columnId := "quot;(integer | stringLiteral)
232
233
## Assignment Statements ##
234
assignStmt := name "=" expr
235
| name "." attrName "=" expr
236
237
attrAccumStmt := name "." attrName "+=" expr
238
239
lAccumAssignStmt := vertexAlias "." localAccumName ("+="| "=") expr
240
241
gAccumAssignStmt := globalAccumName ("+=" | "=") expr
242
243
loadAccumStmt := globalAccumName "=" "{" LOADACCUM loadAccumParams
244
["," LOADACCUM loadAccumParams]* "}"
245
246
loadAccumParams := "(" filePath "," columnId ["," [columnId]* ","
247
stringLiteral "," (TRUE | FALSE) ")" ["." FILTER "(" condition ")"]
248
249
## Function Call Statement ##
250
funcCallStmt := name ["<" type ["," type"]* ">"] "(" [argList] ")"
251
| globalAccumName ("." funcName "(" [argList] ")")+
252
253
argList := expr ["," expr]*
254
255
256
#########################################################
257
## Select Statement
258
259
selectStmt := gsqlSelectBlock
260
| sqlSelectBlock
261
262
gsqlSelectBlock := gsqlSelectClause
263
fromClause
264
[sampleClause]
265
[whereClause]
266
[accumClause]
267
[postAccumClause]*
268
[havingClause]
269
[orderClause]
270
[limitClause]
271
272
sqlSelectBlock := sqlSelectClause
273
fromClause
274
[whereClause]
275
[groupByClause]
276
[havingClause]
277
[orderClause]
278
[limitClause]
279
280
gsqlSelectClause := vertexSetName "=" SELECT vertexAlias
281
sqlSelectClause := SELECT [DISTINCT] columnExpr ("," columnExpr)*
282
INTO tableName
283
columnExpr := expr [AS columnName]
284
| aggregator "("[DISTINCT] expr ")" [AS columnName]
285
columnName := name
286
tableName := name
287
288
fromClause := FROM (step | stepV2 | pathPattern ["," pathPattern]*)
289
290
step := stepSourceSet ["-" "(" stepEdgeSet ")" ("-"|"->") stepVertexSet]
291
stepV2 := stepVertexSet ["-" "(" stepEdgeSet ")" "-" stepVertexSet]
292
293
stepSourceSet := vertexSetName [":" vertexAlias]
294
stepEdgeSet := [stepEdgeTypes] [":" edgeAlias]
295
stepVertexSet := [stepVertexTypes] [":" vertexAlias]
296
alias := (vertexAlias | edgeAlias)
297
vertexAlias := name
298
edgeAlias := name
299
300
stepEdgeTypes := atomicEdgeType | "(" edgeSetType ["|" edgeSetType]* ")"
301
atomicEdgeType := "_" | ANY | edgeSetType
302
edgeSetType := edgeType | paramName | globalAccumName
303
304
stepVertexTypes := atomicVertexType | "(" vertexSetType ["|" vertexSetType]* ")"
305
atomicVertexType := "_" | ANY | vertexSetType
306
vertexSetType := vertexType | paramName | globalAccumName
307
308
#----------# Pattern Matching #----------#
309
pathPattern := stepVertexSet ["-" "(" pathEdgePattern ")" "-" stepVertexSet]*
310
311
pathEdgePattern := atomicEdgePattern
312
| "(" pathEdgePattern ")"
313
| pathEdgePattern "." pathEdgePattern
314
| disjPattern
315
| starPattern
316
317
atomicEdgePattern := atomicEdgeType
318
| atomicEdgeType ">"
319
| "<" atomicEdgeType
320
321
disjPattern := atomicEdgePattern ("|" atomicEdgePattern)*
322
323
starPattern := ([atomicEdgePattern] | "(" disjPattern ")") "*" [starBounds]
324
325
starBounds := CONST_INT ".." CONST_INT
326
| CONST_INT ".."
327
| ".." CONST_INT
328
| CONST_INT
329
#--------------------------------------#
330
331
sampleClause := SAMPLE ( expr | expr "%" ) EDGE WHEN condition
332
| SAMPLE expr TARGET WHEN condition
333
| SAMPLE expr "%" TARGET PINNED WHEN condition
334
335
whereClause := WHERE condition
336
337
accumClause := [perClauseV2] ACCUM dmlSubStmtList
338
339
perClauseV2 := PER "(" alias ["," alias] ")"
340
341
postAccumClause := "POST-ACCUM" dmlSubStmtList
342
343
dmlSubStmtList := dmlSubStmt ["," dmlSubStmt]*
344
345
dmlSubStmt := assignStmt // Assignment
346
| funcCallStmt // Function Call
347
| gAccumAccumStmt // Assignment
348
| lAccumAccumStmt // Assignment
349
| attrAccumStmt // Assignment
350
| vAccumFuncCall // Function Call
351
| localVarDeclStmt // Declaration
352
| dmlSubCaseStmt // Control Flow
353
| dmlSubIfStmt // Control Flow
354
| dmlSubWhileStmt // Control Flow
355
| dmlSubForEachStmt // Control Flow
356
| BREAK // Control Flow
357
| CONTINUE // Control Flow
358
| insertStmt // Data Modification
359
| dmlSubDeleteStmt // Data Modification
360
| printlnStmt // Output
361
| logStmt // Output
362
363
364
vAccumFuncCall := vertexAlias "." localAccumName ("." funcName "(" [argList] ")")+
365
366
groupByClause := GROUP BY groupExpr ("," groupExpr)*
367
groupExpr := expr
368
369
havingClause := HAVING condition
370
371
orderClause := ORDER BY expr [ASC | DESC] ["," expr [ASC | DESC]]*
372
373
limitClause := LIMIT ( expr | expr "," expr | expr OFFSET expr )
374
375
#########################################################
376
## Control Flow Statements ##
377
378
queryBodyIfStmt := IF condition THEN queryBodyStmts
379
[ELSE IF condition THEN queryBodyStmts ]*
380
[ELSE queryBodyStmts ] END
381
dmlSubIfStmt := IF condition THEN dmlSubStmtList
382
[ELSE IF condition THEN dmlSubStmtList ]*
383
[ELSE dmlSubStmtList ] END
384
385
queryBodyCaseStmt := CASE (WHEN condition THEN queryBodyStmts)+ [ELSE queryBodyStmts] END
386
| CASE expr (WHEN constant THEN queryBodyStmts)+ [ELSE queryBodyStmts] END
387
dmlSubCaseStmt := CASE (WHEN condition THEN dmlSubStmtList)+ [ELSE dmlSubStmtList] END
388
| CASE expr (WHEN constant THEN dmlSubStmtList)+ [ELSE dmlSubStmtList] END
389
390
queryBodyWhileStmt := WHILE condition [LIMIT simpleSize] DO queryBodyStmts END
391
dmlSubWhileStmt := WHILE condition [LIMIT simpleSize] DO dmlSubStmtList END
392
simpleSize := integer | varName | paramName
393
394
queryBodyForEachStmt := FOREACH forEachControl DO queryBodyStmts END
395
dmlSubForEachStmt := FOREACH forEachControl DO dmlSubStmtList END
396
397
forEachControl := ( iterationVar | "(" keyVar ("," valueVar)+ ")") (IN | ":") setBagExpr
398
| iterationVar IN RANGE "[" expr "," expr"]" ["." STEP "(" expr ")"]
399
iterationVar := name
400
keyVar := name
401
valueVar := name
402
403
#########################################################
404
## Other Data Modifications Statements ##
405
406
queryBodyDeleteStmt := DELETE alias FROM pattern [whereClause]
407
dmlSubDeleteStmt := DELETE "(" alias ")"
408
409
updateStmt := UPDATE alias FROM pattern SET dmlSubStmtList [whereClause]
410
411
insertStmt := insertVertexStmt | insertEdgeStmt
412
insertVertexStmt := INSERT INTO (vertexType | name)
413
["(" PRIMARY_ID ["," attrName]* ")"]
414
VALUES "(" ( "_" | expr ) ["," ("_" | expr)]*] ")"
415
416
insertEdgeStmt := INSERT INTO (edgeType | EDGE name)
417
["(" FROM "," TO ["," attrName]* ")"]
418
VALUES "(" ( "_" | expr ) [vertexType]
419
["," ( "_" | expr ) [vertexType] ["," ("_" | expr)]*] ")"
420
421
#########################################################
422
## Output Statements ##
423
424
printStmt := PRINT printExpr ("," printExpr)* [WHERE condition] [TO_CSV (filePath | fileVar)]
425
printExpr := (expr | vExprSet) [ AS jsonKey]
426
| tableName
427
vExprSet := expr "[" vSetProj ("," vSetProj)* "]"
428
vSetProj := expr [ AS jsonKey]
429
jsonKey := name
430
431
printlnStmt := fileVar ".println" "(" expr ("," expr)* ")"
432
433
logStmt := LOG "(" condition "," argList ")"
434
435
returnStmt := RETURN expr
436
437
#########################################################
438
## Exception Statements ##
439
440
declExceptStmt := EXCEPTION exceptVarName "(" errorInt ")"
441
exceptVarName := name
442
errorInt := integer
443
444
445
raiseStmt := RAISE exceptVarName [errorMsg]
446
errorMsg := "(" expr ")"
447
448
449
tryStmt := TRY queryBodyStmts EXCEPTION caseExceptBlock+ [elseExceptBlock] END ";"
450
caseExceptBlock := WHEN exceptVarName THEN queryBodyStmts
451
elseExceptBlock := ELSE queryBodyStmts
Copied!
Last modified 1mo ago