blob: dc016488b83d926c52834158a2940da933c3d9c4 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package edu.uci.ics.asterix.aql.parser;
12
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17
18import java.util.Map;
19import java.util.HashMap;
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +000020import java.util.LinkedHashMap;
vinayakb38b7ca42012-03-05 05:44:15 +000021import edu.uci.ics.asterix.aql.literal.FloatLiteral;
22import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
23import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000024import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000025import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000026import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000027import edu.uci.ics.asterix.aql.literal.NullLiteral;
28import edu.uci.ics.asterix.aql.literal.StringLiteral;
29import edu.uci.ics.asterix.aql.literal.TrueLiteral;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000030import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
vinayakb38b7ca42012-03-05 05:44:15 +000031
32import edu.uci.ics.asterix.aql.base.*;
33import edu.uci.ics.asterix.aql.expression.*;
34import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
35import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
36import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
37import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
38import edu.uci.ics.asterix.aql.base.Statement.Kind;
39import edu.uci.ics.asterix.aql.context.Scope;
40import edu.uci.ics.asterix.aql.context.RootScopeFactory;
41import edu.uci.ics.asterix.common.annotations.*;
42import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000043import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000044import edu.uci.ics.asterix.common.functions.FunctionSignature;
alexander.behm07617fd2012-07-25 10:13:50 +000045import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
46import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000047import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
48import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
49import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
50
51
vinayakb38b7ca42012-03-05 05:44:15 +000052
53
54public class AQLParser extends ScopeChecker {
55
vinayakb38b7ca42012-03-05 05:44:15 +000056 // optimizer hints
57 private static final String HASH_GROUP_BY_HINT = "hash";
58 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000059 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000060 private static final String INMEMORY_HINT = "inmem";
61 private static final String VAL_FILE_HINT = "val-files";
62 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
63 private static final String INTERVAL_HINT = "interval";
64 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
65 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
66 private static final String LIST_VAL_FILE_HINT = "list-val-file";
67 private static final String LIST_HINT = "list";
68 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
69 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
70 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
71 private static final String AUTO_HINT = "auto";
72
73 private static final String GEN_FIELDS_HINT = "gen-fields";
74
75 // data generator hints
76 private static final String DGEN_HINT = "dgen";
Till Westmann31c21f92013-05-08 09:21:53 -070077
78 private static class IndexParams {
79 public IndexType type;
80 public int gramLength;
81
82 public IndexParams(IndexType type, int gramLength) {
83 this.type = type;
84 this.gramLength = gramLength;
85 }
86 };
vinayakb38b7ca42012-03-05 05:44:15 +000087
88 private static String getHint(Token t) {
Till Westmann31c21f92013-05-08 09:21:53 -070089 if (t.specialToken == null) {
90 return null;
91 }
92 String s = t.specialToken.image;
93 int n = s.length();
94 if (n < 2) {
95 return null;
96 }
97 return s.substring(1).trim();
vinayakb38b7ca42012-03-05 05:44:15 +000098 }
99
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000100 public AQLParser(String s){
Till Westmann31c21f92013-05-08 09:21:53 -0700101 this(new StringReader(s));
102 super.setInput(s);
103 }
vinayakb38b7ca42012-03-05 05:44:15 +0000104
Till Westmann31c21f92013-05-08 09:21:53 -0700105 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
106 File file = new File(args[0]);
107 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
108 AQLParser parser = new AQLParser(fis);
109 List<Statement> st = parser.Statement();
110 //st.accept(new AQLPrintVisitor(), 0);
111 }
vinayakb38b7ca42012-03-05 05:44:15 +0000112}
113
114PARSER_END(AQLParser)
115
116
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000117List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000118{
vinayakb38b7ca42012-03-05 05:44:15 +0000119 scopeStack.push(RootScopeFactory.createRootScope(this));
120 List<Statement> decls = new ArrayList<Statement>();
Till Westmann31c21f92013-05-08 09:21:53 -0700121 Statement stmt = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000122}
123{
Till Westmann31c21f92013-05-08 09:21:53 -0700124 ( stmt = SingleStatement() (";") ?
vinayakb38b7ca42012-03-05 05:44:15 +0000125 {
Till Westmann31c21f92013-05-08 09:21:53 -0700126 decls.add(stmt);
127 }
128 )*
129 <EOF>
130 {
131 return decls;
132 }
133}
134
135Statement SingleStatement() throws ParseException:
136{
137 Statement stmt = null;
138}
139{
140 (
141 stmt = DataverseDeclaration()
142 | stmt = FunctionDeclaration()
143 | stmt = CreateStatement()
144 | stmt = LoadStatement()
145 | stmt = DropStatement()
146 | stmt = WriteStatement()
147 | stmt = SetStatement()
148 | stmt = InsertStatement()
149 | stmt = DeleteStatement()
150 | stmt = UpdateStatement()
151 | stmt = FeedStatement()
152 | stmt = Query()
153 )
154 {
155 return stmt;
156 }
157}
158
159DataverseDecl DataverseDeclaration() throws ParseException:
160{
Till Westmann14a20a72013-05-09 00:06:24 -0700161 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700162}
163{
Till Westmanna4242bc2013-05-08 17:49:55 -0700164 "use" "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700165 {
Till Westmann14a20a72013-05-09 00:06:24 -0700166 defaultDataverse = dvName;
167 return new DataverseDecl(new Identifier(dvName));
Till Westmann31c21f92013-05-08 09:21:53 -0700168 }
169}
170
171Statement CreateStatement() throws ParseException:
172{
173 String hint = null;
174 boolean dgen = false;
175 Statement stmt = null;
176}
177{
178 "create"
179 (
180 {
181 hint = getHint(token);
182 if (hint != null && hint.startsWith(DGEN_HINT)) {
183 dgen = true;
184 }
185 }
186 stmt = TypeSpecification(hint, dgen)
187 | stmt = NodegroupSpecification()
188 | stmt = DatasetSpecification()
189 | stmt = IndexSpecification()
190 | stmt = DataverseSpecification()
191 | stmt = FunctionSpecification()
192 )
193 {
194 return stmt;
195 }
196}
197
198TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
199{
200 Pair<Identifier,Identifier> nameComponents = null;
201 boolean ifNotExists = false;
202 TypeExpression typeExpr = null;
203}
204{
205 "type" nameComponents = FunctionOrTypeName() ifNotExists = IfNotExists()
206 "as" typeExpr = TypeExpr()
207 {
208 long numValues = -1;
209 String filename = null;
210 if (dgen) {
211 String splits[] = hint.split(" +");
212 if (splits.length != 3) {
213 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
214 }
215 filename = splits[1];
216 numValues = Long.parseLong(splits[2]);
217 }
218 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
219 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
220 }
221}
222
223
224NodegroupDecl NodegroupSpecification() throws ParseException:
225{
Till Westmann14a20a72013-05-09 00:06:24 -0700226 String name = null;
227 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700228 boolean ifNotExists = false;
229 List<Identifier>ncNames = null;
230}
231{
Till Westmanna4242bc2013-05-08 17:49:55 -0700232 "nodegroup" name = Identifier()
233 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700234 {
235 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700236 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700237 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700238 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700239 {
Till Westmann14a20a72013-05-09 00:06:24 -0700240 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700241 }
242 )*
243 {
Till Westmann14a20a72013-05-09 00:06:24 -0700244 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700245 }
246}
247
248DatasetDecl DatasetSpecification() throws ParseException:
249{
250 Pair<Identifier,Identifier> nameComponents = null;
251 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700252 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700253 String adapterName = null;
254 Map<String,String> properties = null;
255 FunctionSignature appliedFunction = null;
256 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700257 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700258 Map<String,String> hints = new HashMap<String,String>();
259 DatasetDecl dsetDecl = null;
260}
261{
262 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700263 "external" <DATASET> nameComponents = QualifiedName()
264 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
265 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700266 "using" adapterName = AdapterName() properties = Configuration()
267 ( "hints" hints = Properties() )?
268 {
269 ExternalDetailsDecl edd = new ExternalDetailsDecl();
270 edd.setAdapter(adapterName);
271 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700272 dsetDecl = new DatasetDecl(nameComponents.first,
273 nameComponents.second,
274 new Identifier(typeName),
275 hints,
276 DatasetType.EXTERNAL,
277 edd,
278 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700279 }
280
Till Westmanna4242bc2013-05-08 17:49:55 -0700281 | "feed" <DATASET> nameComponents = QualifiedName()
282 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
283 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700284 "using" adapterName = AdapterName() properties = Configuration()
285 (appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
Till Westmanna4242bc2013-05-08 17:49:55 -0700286 ( "on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700287 ( "hints" hints = Properties() )?
288 {
Till Westmann14a20a72013-05-09 00:06:24 -0700289 FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
290 properties,
291 appliedFunction,
292 nodeGroupName != null
293 ? new Identifier(nodeGroupName)
294 : null,
295 primaryKeyFields);
296 dsetDecl = new DatasetDecl(nameComponents.first,
297 nameComponents.second,
298 new Identifier(typeName),
299 hints,
300 DatasetType.FEED,
301 fdd,
302 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700303 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700304 | <DATASET> nameComponents = QualifiedName()
305 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
306 ifNotExists = IfNotExists()
307 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700308 ( "hints" hints = Properties() )?
309 {
Till Westmann14a20a72013-05-09 00:06:24 -0700310 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
311 ? new Identifier(nodeGroupName)
312 : null,
313 primaryKeyFields);
314 dsetDecl = new DatasetDecl(nameComponents.first,
315 nameComponents.second,
316 new Identifier(typeName),
317 hints,
318 DatasetType.INTERNAL,
319 idd,
320 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700321 }
322 )
323 {
324 return dsetDecl;
325 }
326}
327
328CreateIndexStatement IndexSpecification() throws ParseException:
329{
330 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700331 String indexName = null;
332 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700333 boolean ifNotExists = false;
334 Pair<Identifier,Identifier> nameComponents = null;
335 IndexParams indexType = null;
336}
337{
Till Westmanna4242bc2013-05-08 17:49:55 -0700338 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700339 ifNotExists = IfNotExists()
340 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700341 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700342 {
Till Westmann14a20a72013-05-09 00:06:24 -0700343 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700344 }
Till Westmann14a20a72013-05-09 00:06:24 -0700345 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700346 {
Till Westmann14a20a72013-05-09 00:06:24 -0700347 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700348 }
349 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
350 {
Till Westmann14a20a72013-05-09 00:06:24 -0700351 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700352 cis.setIfNotExists(ifNotExists);
353 cis.setDataverseName(nameComponents.first);
354 cis.setDatasetName(nameComponents.second);
355 if (indexType != null) {
356 cis.setIndexType(indexType.type);
357 cis.setGramLength(indexType.gramLength);
358 }
359 return cis;
360 }
361}
362
363IndexParams IndexType() throws ParseException:
364{
365 IndexType type = null;
366 int gramLength = 0;
367}
368{
369 ("btree"
370 {
371 type = IndexType.BTREE;
372 }
373 | "rtree"
374 {
375 type = IndexType.RTREE;
376 }
377 | "keyword"
378 {
379 type = IndexType.WORD_INVIX;
380 }
381 | "fuzzy keyword"
382 {
383 type = IndexType.FUZZY_WORD_INVIX;
384 }
385 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
386 {
387 type = IndexType.NGRAM_INVIX;
388 gramLength = Integer.valueOf(token.image);
389 }
390 <RIGHTPAREN>
391 | "fuzzy ngram" <LEFTPAREN> <INTEGER_LITERAL>
392 {
393 type = IndexType.FUZZY_NGRAM_INVIX;
394 gramLength = Integer.valueOf(token.image);
395 }
396 <RIGHTPAREN>)
397 {
398 return new IndexParams(type, gramLength);
399 }
400}
401
402CreateDataverseStatement DataverseSpecification() throws ParseException :
403{
Till Westmann14a20a72013-05-09 00:06:24 -0700404 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700405 boolean ifNotExists = false;
406 String format = null;
407}
408{
Till Westmanna4242bc2013-05-08 17:49:55 -0700409 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700410 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700411 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700412 {
Till Westmann14a20a72013-05-09 00:06:24 -0700413 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700414 }
415}
416
417CreateFunctionStatement FunctionSpecification() throws ParseException:
418{
419 FunctionSignature signature;
420 boolean ifNotExists = false;
421 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
422 String functionBody;
423 VarIdentifier var = null;
424 Expression functionBodyExpr;
425 Token beginPos;
426 Token endPos;
427 Pair<Identifier,Identifier> nameComponents=null;
428
429 createNewScope();
430}
431{
432 "function" nameComponents = FunctionOrTypeName()
433 ifNotExists = IfNotExists()
434 <LEFTPAREN> (<VARIABLE>
435 {
436 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700437 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700438 paramList.add(var);
439 getCurrentScope().addNewVarSymbolToScope(var);
440 }
441 ("," <VARIABLE>
442 {
443 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700444 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700445 paramList.add(var);
446 getCurrentScope().addNewVarSymbolToScope(var);
447 }
448 )*)? <RIGHTPAREN> "{"
449 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700450 beginPos = token;
Till Westmann31c21f92013-05-08 09:21:53 -0700451 }
452 functionBodyExpr = Expression() "}"
453 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700454 endPos = token;
Till Westmann31c21f92013-05-08 09:21:53 -0700455 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
456 String dataverse = nameComponents.first.getValue();
457 String functionName = nameComponents.second.getValue();
458 signature = new FunctionSignature(dataverse, functionName, paramList.size());
459 getCurrentScope().addFunctionDescriptor(signature, false);
460 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
461 }
462}
463
464boolean IfNotExists() throws ParseException:
465{
466}
467{
468 ( "if not exists"
469 {
470 return true;
471 }
472 )?
473 {
474 return false;
475 }
476}
477
478FunctionSignature ApplyFunction() throws ParseException:
479{
480 FunctionSignature funcSig = null;
481}
482{
483 "apply" "function" funcSig = FunctionSignature()
484 {
485 return funcSig;
486 }
487}
488
489FunctionSignature FunctionSignature() throws ParseException:
490{
491 Pair<Identifier,Identifier> pairId = null;
492 int arity = 0;
493}
494{
495 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
496 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700497 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700498 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
499 throw new ParseException(" invalid arity:" + arity);
500 }
501
502 String dataverse = pairId.first.getValue();
503 String functionName = pairId.second.getValue();
504 return new FunctionSignature(dataverse, functionName, arity);
505 }
506}
507
508List<String> PrimaryKey() throws ParseException:
509{
Till Westmann14a20a72013-05-09 00:06:24 -0700510 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700511 List<String> primaryKeyFields = new ArrayList<String>();
512}
513{
Till Westmann14a20a72013-05-09 00:06:24 -0700514 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700515 {
Till Westmann14a20a72013-05-09 00:06:24 -0700516 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700517 }
Till Westmann14a20a72013-05-09 00:06:24 -0700518 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700519 {
Till Westmann14a20a72013-05-09 00:06:24 -0700520 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700521 }
522 )*
523 {
524 return primaryKeyFields;
525 }
526}
527
528Statement DropStatement() throws ParseException:
529{
Till Westmann14a20a72013-05-09 00:06:24 -0700530 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700531 Pair<Identifier,Identifier> pairId = null;
532 Triple<Identifier,Identifier,Identifier> tripleId = null;
533 FunctionSignature funcSig = null;
534 boolean ifExists = false;
535 Statement stmt = null;
536}
537{
538 "drop"
539 (
540 <DATASET> pairId = QualifiedName() ifExists = IfExists()
541 {
542 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
543 }
544 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
545 {
546 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
547 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700548 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700549 {
Till Westmann14a20a72013-05-09 00:06:24 -0700550 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700551 }
552 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
553 {
554 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
555 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700556 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700557 {
Till Westmann14a20a72013-05-09 00:06:24 -0700558 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700559 }
560 | "function" funcSig = FunctionSignature() ifExists = IfExists()
561 {
562 stmt = new FunctionDropStatement(funcSig, ifExists);
563 }
564 )
565 {
566 return stmt;
567 }
568}
569
570boolean IfExists() throws ParseException :
571{
572}
573{
574 ( "if" "exists"
575 {
576 return true;
577 }
578 )?
579 {
580 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000581 }
582}
583
584InsertStatement InsertStatement() throws ParseException:
585{
Till Westmann31c21f92013-05-08 09:21:53 -0700586 Pair<Identifier,Identifier> nameComponents = null;
587 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000588}
589{
Till Westmann31c21f92013-05-08 09:21:53 -0700590 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
591 {
592 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
593 }
vinayakb38b7ca42012-03-05 05:44:15 +0000594}
595
596DeleteStatement DeleteStatement() throws ParseException:
597{
Till Westmann31c21f92013-05-08 09:21:53 -0700598 VariableExpr var = null;
599 Expression condition = null;
600 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000601}
602{
Till Westmann31c21f92013-05-08 09:21:53 -0700603 "delete" var = Variable()
604 {
605 getCurrentScope().addNewVarSymbolToScope(var.getVar());
606 }
607 "from" <DATASET> nameComponents = QualifiedName()
608 ("where" condition = Expression())?
609 {
610 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
611 }
vinayakb38b7ca42012-03-05 05:44:15 +0000612}
613
614UpdateStatement UpdateStatement() throws ParseException:
615{
Till Westmann31c21f92013-05-08 09:21:53 -0700616 VariableExpr vars;
617 Expression target;
618 Expression condition;
619 UpdateClause uc;
620 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000621}
622{
Till Westmann31c21f92013-05-08 09:21:53 -0700623 "update" vars = Variable() "in" target = Expression()
624 "where" condition = Expression()
625 <LEFTPAREN> (uc = UpdateClause()
626 {
627 ucs.add(uc);
628 }
629 ("," uc = UpdateClause()
630 {
631 ucs.add(uc);
632 }
633 )*) <RIGHTPAREN>
634 {
635 return new UpdateStatement(vars, target, condition, ucs);
636 }
vinayakb38b7ca42012-03-05 05:44:15 +0000637}
638
vinayakb38b7ca42012-03-05 05:44:15 +0000639UpdateClause UpdateClause() throws ParseException:
640{
Till Westmann31c21f92013-05-08 09:21:53 -0700641 Expression target = null;
642 Expression value = null ;
643 InsertStatement is = null;
644 DeleteStatement ds = null;
645 UpdateStatement us = null;
646 Expression condition = null;
647 UpdateClause ifbranch = null;
648 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000649}
650{
651 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700652 | is = InsertStatement()
653 | ds = DeleteStatement()
654 | us = UpdateStatement()
655 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
656 "then" ifbranch = UpdateClause()
657 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
658 {
659 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
660 }
vinayakb38b7ca42012-03-05 05:44:15 +0000661}
662
vinayakb38b7ca42012-03-05 05:44:15 +0000663Statement SetStatement() throws ParseException:
664{
665 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700666 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000667}
668{
Till Westmann7d535322013-05-09 00:40:02 -0700669 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700670 {
Till Westmann31c21f92013-05-08 09:21:53 -0700671 return new SetStatement(pn, pv);
672 }
vinayakb38b7ca42012-03-05 05:44:15 +0000673}
674
675Statement WriteStatement() throws ParseException:
676{
Till Westmann14a20a72013-05-09 00:06:24 -0700677 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000678 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000679 Statement stmt = null;
680 Query query;
681 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000682 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000683}
684{
Till Westmann31c21f92013-05-08 09:21:53 -0700685 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700686 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
687 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700688 {
Till Westmann14a20a72013-05-09 00:06:24 -0700689 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
Till Westmann31c21f92013-05-08 09:21:53 -0700690 }
691 ) | (
692 "into" <DATASET>
693 {
694 nameComponents = QualifiedName();
695 }
696 <LEFTPAREN> query = Query() <RIGHTPAREN>
697 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000698 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700699 }
700 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000701 {
702 return stmt;
703 }
704}
705
vinayakb38b7ca42012-03-05 05:44:15 +0000706LoadFromFileStatement LoadStatement() throws ParseException:
707{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000708 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000709 Identifier datasetName = null;
710 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000711 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000712 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000713 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000714}
715{
Till Westmann31c21f92013-05-08 09:21:53 -0700716 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000717 {
Till Westmann31c21f92013-05-08 09:21:53 -0700718 dataverseName = nameComponents.first;
719 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000720 }
Till Westmann31c21f92013-05-08 09:21:53 -0700721 "using" adapterName = AdapterName() properties = Configuration()
722 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000723 {
Till Westmann31c21f92013-05-08 09:21:53 -0700724 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000725 }
726 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700727 {
728 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
729 }
vinayakb38b7ca42012-03-05 05:44:15 +0000730}
731
vinayakb38b7ca42012-03-05 05:44:15 +0000732
Till Westmann31c21f92013-05-08 09:21:53 -0700733String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000734{
ramangrover29669d8f62013-02-11 06:03:32 +0000735 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000736}
737{
Till Westmann68d99652013-05-09 11:15:21 -0700738 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000739 {
Till Westmann7d535322013-05-09 00:40:02 -0700740 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000741 }
vinayakb38b7ca42012-03-05 05:44:15 +0000742}
743
Till Westmann31c21f92013-05-08 09:21:53 -0700744Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000745{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000746 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700747 Map<String,String> configuration = null;
748 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000749}
750{
Till Westmann31c21f92013-05-08 09:21:53 -0700751 (
752 "begin" "feed" nameComponents = QualifiedName()
753 {
754 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
755 }
756 | "suspend" "feed" nameComponents = QualifiedName()
757 {
758 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
759 }
760 | "resume" "feed" nameComponents = QualifiedName()
761 {
762 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
763 }
764 | "end" "feed" nameComponents = QualifiedName()
765 {
766 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
767 }
768 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
769 {
770 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
771 }
772 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000773 {
Till Westmann31c21f92013-05-08 09:21:53 -0700774 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000775 }
776}
777
Till Westmann31c21f92013-05-08 09:21:53 -0700778Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000779{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000780 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700781 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000782}
783{
Till Westmann31c21f92013-05-08 09:21:53 -0700784 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000785 {
Till Westmann31c21f92013-05-08 09:21:53 -0700786 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000787 }
Till Westmann31c21f92013-05-08 09:21:53 -0700788 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000789 {
Till Westmann31c21f92013-05-08 09:21:53 -0700790 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000791 }
Till Westmann31c21f92013-05-08 09:21:53 -0700792 )* )? <RIGHTPAREN>
793 {
794 return configuration;
795 }
796}
797
798Pair<String, String> KeyValuePair() throws ParseException:
799{
800 String key;
801 String value;
802}
803{
Till Westmann7d535322013-05-09 00:40:02 -0700804 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700805 {
806 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000807 }
Till Westmann31c21f92013-05-08 09:21:53 -0700808}
809
810Map<String,String> Properties() throws ParseException:
811{
812 Map<String,String> properties = new HashMap<String,String>();
813 Pair<String, String> property;
814}
815{
816 ( <LEFTPAREN> property = Property()
817 {
818 properties.put(property.first, property.second);
819 }
820 ( "," property = Property()
821 {
822 properties.put(property.first, property.second);
823 }
824 )* <RIGHTPAREN> )?
825 {
826 return properties;
827 }
828}
829
830Pair<String, String> Property() throws ParseException:
831{
832 String key;
833 String value;
834}
835{
Till Westmann7d535322013-05-09 00:40:02 -0700836 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700837 {
838 try {
839 value = "" + Long.valueOf(token.image);
840 } catch (NumberFormatException nfe) {
841 throw new ParseException("inapproriate value: " + token.image);
842 }
843 }
844 )
845 {
846 return new Pair<String, String>(key.toUpperCase(), value);
847 }
vinayakb38b7ca42012-03-05 05:44:15 +0000848}
849
850TypeExpression TypeExpr() throws ParseException:
851{
852 TypeExpression typeExpr = null;
853}
854{
855 (
856 typeExpr = RecordTypeDef()
857 | typeExpr = TypeReference()
858 | typeExpr = OrderedListTypeDef()
859 | typeExpr = UnorderedListTypeDef()
860 )
861 {
862 return typeExpr;
863 }
864}
865
866RecordTypeDefinition RecordTypeDef() throws ParseException:
867{
868 RecordTypeDefinition recType = new RecordTypeDefinition();
869 RecordTypeDefinition.RecordKind recordKind = null;
870}
871{
872 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
873 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
874 "{"
875 {
876 String hint = getHint(token);
877 if (hint != null) {
878 String splits[] = hint.split(" +");
879 if (splits[0].equals(GEN_FIELDS_HINT)) {
880 if (splits.length != 5) {
881 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
882 }
883 if (!splits[1].equals("int")) {
884 throw new ParseException("The only supported type for gen-fields is int.");
885 }
886 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
887 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
888 recType.setUndeclaredFieldsDataGen(ufdg);
889 }
890 }
891
892 }
893 (
894 RecordField(recType)
895 ( "," RecordField(recType) )*
896 )?
897 "}"
898 {
899 if (recordKind == null) {
900 recordKind = RecordTypeDefinition.RecordKind.OPEN;
901 }
902 recType.setRecordKind(recordKind);
903 return recType;
904 }
905}
906
907void RecordField(RecordTypeDefinition recType) throws ParseException:
908{
909 String fieldName;
910 TypeExpression type = null;
911 boolean nullable = false;
912}
913{
Till Westmann14a20a72013-05-09 00:06:24 -0700914 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000915 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700916 fieldName = token.image;
917 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000918 IRecordFieldDataGen rfdg = null;
919 if (hint != null) {
920 String splits[] = hint.split(" +");
921 if (splits[0].equals(VAL_FILE_HINT)) {
922 File[] valFiles = new File[splits.length - 1];
923 for (int k=1; k<splits.length; k++) {
924 valFiles[k-1] = new File(splits[k]);
925 }
926 rfdg = new FieldValFileDataGen(valFiles);
927 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
928 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
929 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
930 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
931 } else if (splits[0].equals(LIST_HINT)) {
932 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
933 } else if (splits[0].equals(INTERVAL_HINT)) {
934 FieldIntervalDataGen.ValueType vt;
935 if (splits[1].equals("int")) {
936 vt = FieldIntervalDataGen.ValueType.INT;
937 } else if (splits[1].equals("long")) {
938 vt = FieldIntervalDataGen.ValueType.LONG;
939 } else if (splits[1].equals("float")) {
940 vt = FieldIntervalDataGen.ValueType.FLOAT;
941 } else if (splits[1].equals("double")) {
942 vt = FieldIntervalDataGen.ValueType.DOUBLE;
943 } else {
944 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
945 }
946 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
947 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
948 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
949 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
950 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
951 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
952 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
953 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
954 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
955 } else if (splits[0].equals(AUTO_HINT)) {
956 rfdg = new AutoDataGen(splits[1]);
957 }
958 }
959 }
960 ":"
961 ( type = TypeExpr() )
962 ("?" { nullable = true; } )?
963 {
964 recType.addField(fieldName, type, nullable, rfdg);
965 }
966}
967
968TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000969{
Till Westmann14a20a72013-05-09 00:06:24 -0700970 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700971}
972{
973 id = Identifier()
974 {
Till Westmann14a20a72013-05-09 00:06:24 -0700975 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700976 }
vinayakb38b7ca42012-03-05 05:44:15 +0000977}
978
979OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
980{
981 TypeExpression type = null;
982}
983{
984 "["
985 ( type = TypeExpr() )
986 "]"
987 {
988 return new OrderedListTypeDefinition(type);
989 }
990}
991
992
993UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
994{
995 TypeExpression type = null;
996}
997{
998 "{{"
999 ( type = TypeExpr() )
1000 "}}"
1001 {
1002 return new UnorderedListTypeDefinition(type);
1003 }
1004}
1005
Till Westmann31c21f92013-05-08 09:21:53 -07001006
1007Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001008{
Till Westmann31c21f92013-05-08 09:21:53 -07001009 Pair<Identifier,Identifier> name = null;
1010}
1011{
1012 name = QualifiedName()
1013 {
1014 if (name.first == null) {
1015 name.first = new Identifier(defaultDataverse);
1016 }
1017 return name;
1018 }
1019}
1020
Till Westmann14a20a72013-05-09 00:06:24 -07001021String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001022{
Till Westmann68d99652013-05-09 11:15:21 -07001023 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001024}
1025{
1026 <IDENTIFIER>
1027 {
Till Westmann14a20a72013-05-09 00:06:24 -07001028 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001029 }
Till Westmann68d99652013-05-09 11:15:21 -07001030 | lit = StringLiteral()
1031 {
1032 return lit;
1033 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001034}
1035
Till Westmann7d535322013-05-09 00:40:02 -07001036String StringLiteral() throws ParseException:
1037{
1038}
1039{
1040 <STRING_LITERAL>
1041 {
1042 return removeQuotesAndEscapes(token.image);
1043 }
1044}
1045
Till Westmann31c21f92013-05-08 09:21:53 -07001046Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1047{
Till Westmann14a20a72013-05-09 00:06:24 -07001048 String first = null;
1049 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001050}
1051{
Till Westmanna4242bc2013-05-08 17:49:55 -07001052 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001053 {
Till Westmann14a20a72013-05-09 00:06:24 -07001054 Identifier id1 = null;
1055 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001056 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001057 id2 = new Identifier(first);
1058 } else
1059 {
1060 id1 = new Identifier(first);
1061 id2 = new Identifier(second);
1062 }
1063 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001064 }
1065}
1066
Till Westmann31c21f92013-05-08 09:21:53 -07001067Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001068{
Till Westmann14a20a72013-05-09 00:06:24 -07001069 String first = null;
1070 String second = null;
1071 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001072}
1073{
Till Westmanna4242bc2013-05-08 17:49:55 -07001074 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001075 {
Till Westmann14a20a72013-05-09 00:06:24 -07001076 Identifier id1 = null;
1077 Identifier id2 = null;
1078 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001079 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001080 id2 = new Identifier(first);
1081 id3 = new Identifier(second);
1082 } else {
1083 id1 = new Identifier(first);
1084 id2 = new Identifier(second);
1085 id3 = new Identifier(third);
1086 }
1087 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001088 }
1089}
1090
vinayakb38b7ca42012-03-05 05:44:15 +00001091FunctionDecl FunctionDeclaration() throws ParseException:
1092{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001093 FunctionDecl funcDecl;
1094 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001095 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001096 int arity = 0;
1097 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1098 Expression funcBody;
1099 VarIdentifier var = null;
1100 createNewScope();
1101}
1102{
Till Westmann14a20a72013-05-09 00:06:24 -07001103 "declare" "function" functionName = Identifier() <LEFTPAREN> (<VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001104 {
1105 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -07001106 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001107 paramList.add(var);
1108 getCurrentScope().addNewVarSymbolToScope(var);
1109 arity++;
1110 }
Till Westmann31c21f92013-05-08 09:21:53 -07001111 ("," <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001112 {
1113 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -07001114 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001115 paramList.add(var);
1116 getCurrentScope().addNewVarSymbolToScope(var);
1117 arity++;
Till Westmann31c21f92013-05-08 09:21:53 -07001118 }
1119 )*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001120 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001121 signature = new FunctionSignature(defaultDataverse, functionName, arity);
1122 getCurrentScope().addFunctionDescriptor(signature, false);
1123 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1124 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001125 }
1126}
1127
vinayakb38b7ca42012-03-05 05:44:15 +00001128
Till Westmann31c21f92013-05-08 09:21:53 -07001129Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001130{
1131 Query query = new Query();
1132 Expression expr;
1133}
1134{
Till Westmann31c21f92013-05-08 09:21:53 -07001135 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001136 {
1137 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001138 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001139 return query;
1140 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001141
vinayakb38b7ca42012-03-05 05:44:15 +00001142}
1143
1144
1145
1146Expression Expression():
1147{
1148 Expression expr = null;
1149 Expression exprP = null;
1150}
1151{
1152(
1153
1154//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1155 expr = OperatorExpr()
1156 | expr = IfThenElse()
1157 | expr = FLWOGR()
1158 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001159
vinayakb38b7ca42012-03-05 05:44:15 +00001160
1161)
1162 {
1163 return (exprP==null) ? expr : exprP;
1164 }
1165}
1166
1167
1168
1169Expression OperatorExpr()throws ParseException:
1170{
1171 OperatorExpr op = null;
1172 Expression operand = null;
1173}
1174{
1175 operand = AndExpr()
1176 (
1177
1178 "or"
1179 {
1180 if (op == null) {
1181 op = new OperatorExpr();
1182 op.addOperand(operand);
1183 op.setCurrentop(true);
1184 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001185 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001186 }
1187
1188 operand = AndExpr()
1189 {
1190 op.addOperand(operand);
1191 }
1192
1193 )*
1194
1195 {
1196 return op==null? operand: op;
1197 }
1198}
1199
1200Expression AndExpr()throws ParseException:
1201{
1202 OperatorExpr op = null;
1203 Expression operand = null;
1204}
1205{
1206 operand = RelExpr()
1207 (
1208
1209 "and"
1210 {
1211 if (op == null) {
1212 op = new OperatorExpr();
1213 op.addOperand(operand);
1214 op.setCurrentop(true);
1215 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001216 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001217 }
1218
1219 operand = RelExpr()
1220 {
1221 op.addOperand(operand);
1222 }
1223
1224 )*
1225
1226 {
1227 return op==null? operand: op;
1228 }
1229}
1230
1231
1232
1233Expression RelExpr()throws ParseException:
1234{
1235 OperatorExpr op = null;
1236 Expression operand = null;
1237 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001238 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001239}
1240{
1241 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001242 {
1243 if (operand instanceof VariableExpr) {
1244 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001245 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001246 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001247 }
1248 }
1249 }
1250
1251 (
1252 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1253 {
alexander.behm07617fd2012-07-25 10:13:50 +00001254 String mhint = getHint(token);
1255 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1256 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1257 }
vinayakb38b7ca42012-03-05 05:44:15 +00001258 if (op == null) {
1259 op = new OperatorExpr();
1260 op.addOperand(operand, broadcast);
1261 op.setCurrentop(true);
1262 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001263 }
1264 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001265 }
1266
1267 operand = AddExpr()
1268 {
alexander.behm07617fd2012-07-25 10:13:50 +00001269 broadcast = false;
1270 if (operand instanceof VariableExpr) {
1271 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001272 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1273 broadcast = true;
1274 }
alexander.behm07617fd2012-07-25 10:13:50 +00001275 }
vinayakb38b7ca42012-03-05 05:44:15 +00001276 op.addOperand(operand, broadcast);
1277 }
1278 )?
1279
1280 {
alexander.behm07617fd2012-07-25 10:13:50 +00001281 if (annotation != null) {
1282 op.addHint(annotation);
1283 }
vinayakb38b7ca42012-03-05 05:44:15 +00001284 return op==null? operand: op;
1285 }
1286}
1287
1288Expression AddExpr()throws ParseException:
1289{
1290 OperatorExpr op = null;
1291 Expression operand = null;
1292}
1293{
1294 operand = MultExpr()
1295
1296 ( ("+" | "-")
1297 {
1298 if (op == null) {
1299 op = new OperatorExpr();
1300 op.addOperand(operand);
1301 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001302 }
1303 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001304 }
1305
1306 operand = MultExpr()
1307 {
1308 op.addOperand(operand);
1309 }
1310 )*
1311
1312 {
1313 return op==null? operand: op;
1314 }
1315}
1316
1317Expression MultExpr()throws ParseException:
1318{
1319 OperatorExpr op = null;
1320 Expression operand = null;
1321}
1322{
1323 operand = UnionExpr()
1324
1325 (( "*" | "/" | "%" | <CARET> | "idiv")
1326 {
1327 if (op == null) {
1328 op = new OperatorExpr();
1329 op.addOperand(operand);
1330 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001331 }
1332 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001333 }
1334 operand = UnionExpr()
1335 {
1336 op.addOperand(operand);
1337 }
1338 )*
1339
1340 {
1341 return op==null?operand:op;
1342 }
1343}
1344
1345Expression UnionExpr() throws ParseException:
1346{
1347 UnionExpr union = null;
1348 Expression operand1 = null;
1349 Expression operand2 = null;
1350}
1351{
1352 operand1 = UnaryExpr()
1353 ("union"
1354 (operand2 = UnaryExpr()) {
1355 if (union == null) {
1356 union = new UnionExpr();
1357 union.addExpr(operand1);
1358 }
1359 union.addExpr(operand2);
1360 } )*
1361 {
1362 return (union == null)? operand1: union;
1363 }
1364}
1365
1366Expression UnaryExpr() throws ParseException:
1367{
1368 Expression uexpr = null;
1369 Expression expr = null;
1370}
1371{
1372 (( "+"|"-")
1373 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001374 uexpr = new UnaryExpr();
1375 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001376 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001377 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001378 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1379 else
1380 throw new ParseException();
1381 }
1382 )?
1383
1384 expr = ValueExpr()
1385 {
1386 if(uexpr!=null){
1387 ((UnaryExpr)uexpr).setExpr(expr);
1388 return uexpr;
1389 }
1390 else{
1391 return expr;
1392 }
1393 }
1394}
1395
Till Westmann04478e72013-05-13 23:01:34 -07001396Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001397{
1398 Expression expr = null;
1399 Identifier ident = null;
1400 AbstractAccessor fa = null;
1401 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001402}
1403{
Till Westmann04478e72013-05-13 23:01:34 -07001404 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001405 {
Till Westmann04478e72013-05-13 23:01:34 -07001406 fa = (fa == null ? new FieldAccessor(expr, ident)
1407 : new FieldAccessor(fa, ident));
1408 }
1409 | index = Index()
1410 {
1411 fa = (fa == null ? new IndexAccessor(expr, index)
1412 : new IndexAccessor(fa, index));
1413 }
1414 )*
1415 {
1416 return fa == null ? expr : fa;
1417 }
vinayakb38b7ca42012-03-05 05:44:15 +00001418}
1419
1420Identifier Field() throws ParseException:
1421{
Till Westmann14a20a72013-05-09 00:06:24 -07001422 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001423}
1424{
Till Westmanna4242bc2013-05-08 17:49:55 -07001425 "." ident = Identifier()
1426 {
Till Westmann14a20a72013-05-09 00:06:24 -07001427 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001428 }
vinayakb38b7ca42012-03-05 05:44:15 +00001429}
1430
1431int Index() throws ParseException:
1432{
1433 Expression expr = null;
1434 int idx = -2;
1435}
1436{
1437 "[" ( expr = Expression()
1438 {
1439 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1440 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001441 Literal lit = ((LiteralExpr)expr).getValue();
1442 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1443 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001444 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001445 }
vinayakb38b7ca42012-03-05 05:44:15 +00001446 else {
1447 throw new ParseException("Index should be an INTEGER");
1448 }
1449 }
1450
1451 }
1452
1453 | "?"
1454 {
1455 idx = IndexAccessor.ANY;
1456 // ANY
1457 }
1458
1459 )
1460
1461 "]"
1462 {
1463 return idx;
1464 }
1465}
1466
1467
1468Expression PrimaryExpr()throws ParseException:
1469{
1470 Expression expr = null;
1471}
1472{
Till Westmann68d99652013-05-09 11:15:21 -07001473 ( LOOKAHEAD(2)
1474 expr = FunctionCallExpr()
1475 | expr = Literal()
1476 | expr = DatasetAccessExpression()
1477 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001478 {
1479 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001480 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001481 }
Till Westmann68d99652013-05-09 11:15:21 -07001482 | expr = ListConstructor()
1483 | expr = RecordConstructor()
1484 | expr = ParenthesizedExpression()
1485 )
1486 {
1487 return expr;
1488 }
vinayakb38b7ca42012-03-05 05:44:15 +00001489}
1490
1491Expression Literal() throws ParseException:
1492{
vinayakb38b7ca42012-03-05 05:44:15 +00001493 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001494 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001495}
1496{
Till Westmann7d535322013-05-09 00:40:02 -07001497 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001498 {
Till Westmann7d535322013-05-09 00:40:02 -07001499 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001500 }
1501 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001502 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001503 try {
1504 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1505 } catch(NumberFormatException ex) {
1506 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1507 }
1508 }
1509 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001510 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001511 lit.setValue(new FloatLiteral(new Float(token.image)));
1512 }
1513 | <DOUBLE_LITERAL>
1514 {
1515 lit.setValue(new DoubleLiteral(new Double(token.image)));
1516 }
1517 | <NULL>
1518 {
1519 lit.setValue(NullLiteral.INSTANCE);
1520 }
1521 | <TRUE>
1522 {
1523 lit.setValue(TrueLiteral.INSTANCE);
1524 }
1525 | <FALSE>
1526 {
1527 lit.setValue(FalseLiteral.INSTANCE);
1528 }
1529 )
vinayakb38b7ca42012-03-05 05:44:15 +00001530 {
1531 return lit;
1532 }
1533}
1534
1535
1536VariableExpr VariableRef() throws ParseException:
1537{
1538 VariableExpr varExp = new VariableExpr();
1539 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001540}
1541{
1542 <VARIABLE>
1543 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001544 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001545 Identifier ident = lookupSymbol(varName);
1546 if (isInForbiddenScopes(varName)) {
1547 throw new ParseException("Inside limit clauses, it is disallowed to reference a variable having the same name as any variable bound in the same scope as the limit clause.");
1548 }
1549 if(ident != null) { // exist such ident
1550 varExp.setIsNewVar(false);
1551 varExp.setVar((VarIdentifier)ident);
1552 } else {
1553 varExp.setVar(var);
1554 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001555 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001556 return varExp;
1557 }
1558}
1559
1560
1561VariableExpr Variable() throws ParseException:
1562{
1563 VariableExpr varExp = new VariableExpr();
1564 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001565}
1566{
1567 <VARIABLE>
1568 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001569 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001570 if(ident != null) { // exist such ident
1571 varExp.setIsNewVar(false);
1572 }
1573 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001574 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001575 return varExp;
1576 }
1577}
1578
1579Expression ListConstructor() throws ParseException:
1580{
1581 Expression expr = null;
1582}
1583{
1584 (
1585 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1586 )
1587
1588 {
1589 return expr;
1590 }
1591}
1592
1593
1594ListConstructor OrderedListConstructor() throws ParseException:
1595{
1596 ListConstructor expr = new ListConstructor();
1597 Expression tmp = null;
1598 List<Expression> exprList = new ArrayList<Expression>();
1599 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1600}
1601{
1602
1603 "["
1604 ( tmp = Expression()
1605 {
1606 exprList.add(tmp);
1607 }
1608
1609 ("," tmp = Expression() { exprList.add(tmp); })*
1610 )?
1611
1612 "]"
1613
1614 {
1615 expr.setExprList(exprList);
1616 return expr;
1617 }
1618}
1619
1620ListConstructor UnorderedListConstructor() throws ParseException:
1621{
1622 ListConstructor expr = new ListConstructor();
1623 Expression tmp = null;
1624 List<Expression> exprList = new ArrayList<Expression>();
1625 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1626}
1627{
1628
1629 "{{" ( tmp = Expression()
1630 {
1631 exprList.add(tmp);
1632 }
1633 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1634 {
1635 expr.setExprList(exprList);
1636 return expr;
1637 }
1638}
1639
1640RecordConstructor RecordConstructor() throws ParseException:
1641{
1642 RecordConstructor expr = new RecordConstructor();
1643 FieldBinding tmp = null;
1644 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1645}
1646{
1647 "{" (tmp = FieldBinding()
1648 {
1649 fbList.add(tmp);
1650 }
1651 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1652 {
1653 expr.setFbList(fbList);
1654 return expr;
1655 }
1656}
1657
1658FieldBinding FieldBinding() throws ParseException:
1659{
1660 FieldBinding fb = new FieldBinding();
1661 Expression left, right;
1662}
1663{
1664 left = Expression() ":" right = Expression()
1665 {
1666 fb.setLeftExpr(left);
1667 fb.setRightExpr(right);
1668 return fb;
1669 }
1670}
1671
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001672
vinayakb38b7ca42012-03-05 05:44:15 +00001673Expression FunctionCallExpr() throws ParseException:
1674{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001675 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001676 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001677 Expression tmp;
1678 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001679 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001680 String funcName;
1681 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001682 String hint = null;
1683 String id1 = null;
1684 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001685}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001686{
Till Westmann31c21f92013-05-08 09:21:53 -07001687 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001688 {
Till Westmann31c21f92013-05-08 09:21:53 -07001689 dataverse = funcId.first.getValue();
1690 funcName = funcId.second.getValue();
1691 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001692 }
Till Westmann31c21f92013-05-08 09:21:53 -07001693 <LEFTPAREN> (tmp = Expression()
1694 {
1695 argList.add(tmp);
1696 arity ++;
1697 }
1698 ("," tmp = Expression()
1699 {
1700 argList.add(tmp);
1701 arity++;
1702 }
1703 )*)? <RIGHTPAREN>
1704 {
1705 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1706 if (signature == null) {
1707 signature = new FunctionSignature(dataverse, funcName, arity);
1708 }
1709 callExpr = new CallExpr(signature,argList);
1710 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1711 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1712 }
1713 return callExpr;
1714 }
vinayakb38b7ca42012-03-05 05:44:15 +00001715}
1716
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001717Expression DatasetAccessExpression() throws ParseException:
1718{
1719 CallExpr callExpr;
1720 List<Expression> argList = new ArrayList<Expression>();
1721 String funcName;
1722 String dataverse;
Till Westmann14a20a72013-05-09 00:06:24 -07001723 String arg1 = null;
1724 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001725 LiteralExpr ds;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001726 Expression nameArg;
1727 int arity = 0;
1728}
1729{
Till Westmann14a20a72013-05-09 00:06:24 -07001730 <DATASET>
1731 {
1732 dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1733 funcName = token.image;
1734 }
1735 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1736 {
1737 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
1738 ds = new LiteralExpr();
1739 ds.setValue( new StringLiteral(name) );
1740 argList.add(ds);
1741 arity ++;
1742 }
1743 | ( <LEFTPAREN> nameArg = Expression()
1744 {
1745 argList.add(nameArg);
1746 arity ++;
1747 }
1748 ( "," nameArg = Expression()
1749 {
1750 argList.add(nameArg);
1751 arity++;
1752 }
1753 )* <RIGHTPAREN> ) )
1754 {
1755 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1756 if (signature == null) {
1757 signature = new FunctionSignature(dataverse, funcName, arity);
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001758 }
Till Westmann14a20a72013-05-09 00:06:24 -07001759 callExpr = new CallExpr(signature,argList);
1760 return callExpr;
1761 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001762}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001763
vinayakb38b7ca42012-03-05 05:44:15 +00001764Expression ParenthesizedExpression() throws ParseException:
1765{
1766 Expression expr;
1767}
1768{
1769 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1770 {
1771 return expr;
1772 }
1773}
1774
1775Expression IfThenElse() throws ParseException:
1776{
1777 Expression condExpr;
1778 Expression thenExpr;
1779 Expression elseExpr;
1780 IfExpr ifExpr = new IfExpr();
1781}
1782{
1783 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1784
1785 {
1786 ifExpr.setCondExpr(condExpr);
1787 ifExpr.setThenExpr(thenExpr);
1788 ifExpr.setElseExpr(elseExpr);
1789 return ifExpr;
1790 }
1791}
1792
1793Expression FLWOGR() throws ParseException:
1794{
1795 FLWOGRExpression flworg = new FLWOGRExpression();
1796 List<Clause> clauseList = new ArrayList<Clause>();
1797 Expression returnExpr;
1798 Clause tmp;
1799 createNewScope();
1800}
1801{
1802 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1803 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1804
1805 {
1806 flworg.setClauseList(clauseList);
1807 flworg.setReturnExpr(returnExpr);
1808 removeCurrentScope();
1809 return flworg;
1810 }
1811}
1812
1813Clause Clause()throws ParseException :
1814{
1815 Clause clause;
1816}
1817{
1818 (
1819 clause = ForClause()
1820 | clause = LetClause()
1821 | clause = WhereClause()
1822 | clause = OrderbyClause()
1823 | clause = GroupClause()
1824 | clause = LimitClause()
1825 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001826 )
1827 {
1828 return clause;
1829 }
1830}
1831
1832Clause ForClause()throws ParseException :
1833{
1834 ForClause fc = new ForClause();
1835 VariableExpr varExp;
1836 VariableExpr varPos = null;
1837 Expression inExp;
1838 extendCurrentScope();
1839}
1840{
1841 "for" varExp = Variable()
1842 {
1843 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1844 }
1845 ("at" varPos = Variable()
1846 {
1847 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1848 }
1849 )?
1850 "in" ( inExp = Expression() )
1851 {
1852 fc.setVarExpr(varExp);
1853 fc.setInExpr(inExp);
1854 if (varPos != null) {
1855 fc.setPosExpr(varPos);
1856 }
1857 return fc;
1858 }
1859}
1860
1861Clause LetClause() throws ParseException:
1862{
1863 LetClause lc = new LetClause();
1864 VariableExpr varExp;
1865 Expression beExp;
1866 extendCurrentScope();
1867}
1868{
ilovesoupb2527c12012-07-12 03:21:13 +00001869 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001870 {
1871 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001872 lc.setVarExpr(varExp);
1873 lc.setBeExpr(beExp);
1874 return lc;
1875 }
1876}
1877
1878Clause WhereClause()throws ParseException :
1879{
1880 WhereClause wc = new WhereClause();
1881 Expression whereExpr;
1882}
1883{
1884 "where" whereExpr = Expression()
1885 {
1886 wc.setWhereExpr(whereExpr);
1887 return wc;
1888 }
1889}
1890
1891Clause OrderbyClause()throws ParseException :
1892{
1893 OrderbyClause oc = new OrderbyClause();
1894 Expression orderbyExpr;
1895 List<Expression> orderbyList = new ArrayList<Expression>();
1896 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1897 int numOfOrderby = 0;
1898}
1899{
1900 (
1901 "order"
1902 {
1903 String hint = getHint(token);
1904 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1905 String splits[] = hint.split(" +");
1906 int numFrames = Integer.parseInt(splits[1]);
1907 int numTuples = Integer.parseInt(splits[2]);
1908 oc.setNumFrames(numFrames);
1909 oc.setNumTuples(numTuples);
1910 }
1911 }
1912 "by" orderbyExpr = Expression()
1913 {
1914 orderbyList.add(orderbyExpr);
1915 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1916 }
1917 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1918 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1919 {
1920 modifierList.add(modif);
1921 }
1922
1923 ("," orderbyExpr = Expression()
1924 {
1925 orderbyList.add(orderbyExpr);
1926 modif = OrderbyClause.OrderModifier.ASC;
1927 }
1928 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1929 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1930 {
1931 modifierList.add(modif);
1932 }
1933 )*
1934)
1935 {
1936 oc.setModifierList(modifierList);
1937 oc.setOrderbyList(orderbyList);
1938 return oc;
1939 }
1940}
1941Clause GroupClause()throws ParseException :
1942{
1943 GroupbyClause gbc = new GroupbyClause();
1944 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1945 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1946 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1947 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1948 VariableExpr var = null;
1949 VariableExpr withVar = null;
1950 Expression expr = null;
1951 VariableExpr decorVar = null;
1952 Expression decorExpr = null;
1953}
1954{
1955 {
1956 Scope newScope = extendCurrentScopeNoPush(true);
1957 // extendCurrentScope(true);
1958 }
1959 "group"
1960 {
1961 String hint = getHint(token);
1962 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1963 gbc.setHashGroupByHint(true);
1964 }
1965 }
1966 "by" (LOOKAHEAD(2) var = Variable()
1967 {
1968 newScope.addNewVarSymbolToScope(var.getVar());
1969 } ":=")?
1970 expr = Expression()
1971 {
1972 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1973 vePairList.add(pair1);
1974 }
1975 ("," ( LOOKAHEAD(2) var = Variable()
1976 {
1977 newScope.addNewVarSymbolToScope(var.getVar());
1978 } ":=")?
1979 expr = Expression()
1980 {
1981 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1982 vePairList.add(pair2);
1983 }
1984 )*
1985 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1986 {
1987 newScope.addNewVarSymbolToScope(decorVar.getVar());
1988 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1989 decorPairList.add(pair3);
1990 }
1991 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1992 {
1993 newScope.addNewVarSymbolToScope(decorVar.getVar());
1994 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1995 decorPairList.add(pair4);
1996 }
1997 )*
1998 )?
1999 "with" withVar = VariableRef()
2000 {
2001 if(withVar.getIsNewVar()==true)
2002 throw new ParseException("can't find variable " + withVar.getVar());
2003 withVarList.add(withVar);
2004 newScope.addNewVarSymbolToScope(withVar.getVar());
2005 }
2006 ("," withVar = VariableRef()
2007 {
2008 if(withVar.getIsNewVar()==true)
2009 throw new ParseException("can't find variable " + withVar.getVar());
2010 withVarList.add(withVar);
2011 newScope.addNewVarSymbolToScope(withVar.getVar());
2012 })*
2013 {
2014 gbc.setGbyPairList(vePairList);
2015 gbc.setDecorPairList(decorPairList);
2016 gbc.setWithVarList(withVarList);
2017 replaceCurrentScope(newScope);
2018 return gbc;
2019 }
2020}
2021
2022
2023LimitClause LimitClause() throws ParseException:
2024{
2025 LimitClause lc = new LimitClause();
2026 Expression expr;
2027 pushForbiddenScope(getCurrentScope());
2028}
2029{
2030 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2031 ("offset" expr = Expression() { lc.setOffset(expr); })?
2032
2033 {
2034 popForbiddenScope();
2035 return lc;
2036 }
2037}
2038
2039DistinctClause DistinctClause() throws ParseException:
2040{
2041 List<Expression> exprs = new ArrayList<Expression>();
2042 Expression expr;
2043}
2044{
2045 "distinct" "by" expr = Expression()
2046 {
2047 exprs.add(expr);
2048 }
2049 ("," expr = Expression()
2050 {
2051 exprs.add(expr);
2052 }
2053 )*
2054 {
2055 return new DistinctClause(exprs);
2056 }
2057}
2058
vinayakb38b7ca42012-03-05 05:44:15 +00002059QuantifiedExpression QuantifiedExpression()throws ParseException:
2060{
2061 QuantifiedExpression qc = new QuantifiedExpression();
2062 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2063 Expression satisfiesExpr;
2064 VariableExpr var;
2065 Expression inExpr;
2066 QuantifiedPair pair;
2067}
2068{
2069 {
2070 createNewScope();
2071 }
2072
2073 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2074 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2075 var = Variable() "in" inExpr = Expression()
2076 {
2077 pair = new QuantifiedPair(var, inExpr);
2078 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2079 quantifiedList.add(pair);
2080 }
2081 (
2082 "," var = Variable() "in" inExpr = Expression()
2083 {
2084 pair = new QuantifiedPair(var, inExpr);
2085 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2086 quantifiedList.add(pair);
2087 }
2088 )*
2089 "satisfies" satisfiesExpr = Expression()
2090 {
2091 qc.setSatisfiesExpr(satisfiesExpr);
2092 qc.setQuantifiedList(quantifiedList);
2093 removeCurrentScope();
2094 return qc;
2095 }
2096}
2097
2098TOKEN_MGR_DECLS:
2099{
2100 public int commentDepth = 0;
2101}
2102
2103<DEFAULT>
2104TOKEN :
2105{
2106 <CARET : "^" >
2107}
2108
2109<DEFAULT>
2110TOKEN :
2111{
2112 <DATASET : "dataset" >
2113}
2114
2115<DEFAULT>
2116TOKEN :
2117{
2118 <LEFTPAREN : "(" >
2119}
2120
2121<DEFAULT>
2122TOKEN :
2123{
2124 <RIGHTPAREN : ")" >
2125}
2126
2127
2128<DEFAULT>
2129TOKEN :
2130{
2131 <INTEGER_LITERAL : (<DIGIT>)+ >
2132}
2133
2134
2135<DEFAULT>
2136TOKEN :
2137{
2138 <NULL : "null">
2139}
2140
2141<DEFAULT>
2142TOKEN :
2143{
2144 <TRUE : "true">
2145}
2146
2147<DEFAULT>
2148TOKEN :
2149{
2150 <FALSE : "false">
2151}
2152
2153<DEFAULT>
2154TOKEN :
2155{
2156 <#DIGIT : ["0" - "9"]>
2157}
2158
2159
2160TOKEN:
2161{
2162 < DOUBLE_LITERAL: <INTEGER>
2163 | <INTEGER> ( "." <INTEGER> )?
2164 | "." <INTEGER>
2165 >
2166 |
2167 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2168 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2169 | "." <INTEGER> ( "f" | "F" )
2170 >
2171 |
2172 <INTEGER : (<DIGIT>)+ >
2173}
2174
2175<DEFAULT>
2176TOKEN :
2177{
2178 <#LETTER : ["A" - "Z", "a" - "z"]>
2179}
2180
2181<DEFAULT>
2182TOKEN :
2183{
2184 <SPECIALCHARS : ["$", "_", "-"] >
2185}
2186
2187<DEFAULT>
2188TOKEN :
2189{
2190 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2191 |
2192 < #EscapeQuot: "\\\"" >
2193 |
2194 < #EscapeApos: "\\\'" >
2195}
2196
2197<DEFAULT>
2198TOKEN :
2199{
2200 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2201}
2202
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002203
vinayakb38b7ca42012-03-05 05:44:15 +00002204<DEFAULT>
2205TOKEN :
2206{
2207 <VARIABLE : "$" <IDENTIFIER> >
2208}
2209
2210SKIP:
2211{
2212 " "
2213| "\t"
2214| "\r"
2215| "\n"
2216}
2217
2218SKIP:
2219{
2220 <"//" (~["\n"])* "\n">
2221}
2222
2223SKIP:
2224{
2225 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2226}
2227
2228
2229SKIP:
2230{
2231 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2232}
2233
2234<INSIDE_COMMENT>
2235SPECIAL_TOKEN:
2236{
2237 <"+"(" ")*(~["*"])*>
2238}
2239
2240<INSIDE_COMMENT>
2241SKIP:
2242{
2243 <"/*"> {commentDepth++;}
2244}
2245
2246<INSIDE_COMMENT>
2247SKIP:
2248{
2249 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2250| <~[]>
2251}