blob: aa2da9e47fb9460afe92c8eab0eb989295e73a19 [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()
411 ( "with format" <STRING_LITERAL>
412 {
413 format = removeQuotesAndEscapes(token.image);
414 }
415 )?
416 {
Till Westmann14a20a72013-05-09 00:06:24 -0700417 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700418 }
419}
420
421CreateFunctionStatement FunctionSpecification() throws ParseException:
422{
423 FunctionSignature signature;
424 boolean ifNotExists = false;
425 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
426 String functionBody;
427 VarIdentifier var = null;
428 Expression functionBodyExpr;
429 Token beginPos;
430 Token endPos;
431 Pair<Identifier,Identifier> nameComponents=null;
432
433 createNewScope();
434}
435{
436 "function" nameComponents = FunctionOrTypeName()
437 ifNotExists = IfNotExists()
438 <LEFTPAREN> (<VARIABLE>
439 {
440 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700441 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700442 paramList.add(var);
443 getCurrentScope().addNewVarSymbolToScope(var);
444 }
445 ("," <VARIABLE>
446 {
447 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700448 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700449 paramList.add(var);
450 getCurrentScope().addNewVarSymbolToScope(var);
451 }
452 )*)? <RIGHTPAREN> "{"
453 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700454 beginPos = token;
Till Westmann31c21f92013-05-08 09:21:53 -0700455 }
456 functionBodyExpr = Expression() "}"
457 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700458 endPos = token;
Till Westmann31c21f92013-05-08 09:21:53 -0700459 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
460 String dataverse = nameComponents.first.getValue();
461 String functionName = nameComponents.second.getValue();
462 signature = new FunctionSignature(dataverse, functionName, paramList.size());
463 getCurrentScope().addFunctionDescriptor(signature, false);
464 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
465 }
466}
467
468boolean IfNotExists() throws ParseException:
469{
470}
471{
472 ( "if not exists"
473 {
474 return true;
475 }
476 )?
477 {
478 return false;
479 }
480}
481
482FunctionSignature ApplyFunction() throws ParseException:
483{
484 FunctionSignature funcSig = null;
485}
486{
487 "apply" "function" funcSig = FunctionSignature()
488 {
489 return funcSig;
490 }
491}
492
493FunctionSignature FunctionSignature() throws ParseException:
494{
495 Pair<Identifier,Identifier> pairId = null;
496 int arity = 0;
497}
498{
499 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
500 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700501 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700502 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
503 throw new ParseException(" invalid arity:" + arity);
504 }
505
506 String dataverse = pairId.first.getValue();
507 String functionName = pairId.second.getValue();
508 return new FunctionSignature(dataverse, functionName, arity);
509 }
510}
511
512List<String> PrimaryKey() throws ParseException:
513{
Till Westmann14a20a72013-05-09 00:06:24 -0700514 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700515 List<String> primaryKeyFields = new ArrayList<String>();
516}
517{
Till Westmann14a20a72013-05-09 00:06:24 -0700518 "primary" "key" 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 }
Till Westmann14a20a72013-05-09 00:06:24 -0700522 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700523 {
Till Westmann14a20a72013-05-09 00:06:24 -0700524 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700525 }
526 )*
527 {
528 return primaryKeyFields;
529 }
530}
531
532Statement DropStatement() throws ParseException:
533{
Till Westmann14a20a72013-05-09 00:06:24 -0700534 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700535 Pair<Identifier,Identifier> pairId = null;
536 Triple<Identifier,Identifier,Identifier> tripleId = null;
537 FunctionSignature funcSig = null;
538 boolean ifExists = false;
539 Statement stmt = null;
540}
541{
542 "drop"
543 (
544 <DATASET> pairId = QualifiedName() ifExists = IfExists()
545 {
546 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
547 }
548 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
549 {
550 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
551 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700552 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700553 {
Till Westmann14a20a72013-05-09 00:06:24 -0700554 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700555 }
556 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
557 {
558 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
559 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700560 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700561 {
Till Westmann14a20a72013-05-09 00:06:24 -0700562 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700563 }
564 | "function" funcSig = FunctionSignature() ifExists = IfExists()
565 {
566 stmt = new FunctionDropStatement(funcSig, ifExists);
567 }
568 )
569 {
570 return stmt;
571 }
572}
573
574boolean IfExists() throws ParseException :
575{
576}
577{
578 ( "if" "exists"
579 {
580 return true;
581 }
582 )?
583 {
584 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000585 }
586}
587
588InsertStatement InsertStatement() throws ParseException:
589{
Till Westmann31c21f92013-05-08 09:21:53 -0700590 Pair<Identifier,Identifier> nameComponents = null;
591 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000592}
593{
Till Westmann31c21f92013-05-08 09:21:53 -0700594 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
595 {
596 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
597 }
vinayakb38b7ca42012-03-05 05:44:15 +0000598}
599
600DeleteStatement DeleteStatement() throws ParseException:
601{
Till Westmann31c21f92013-05-08 09:21:53 -0700602 VariableExpr var = null;
603 Expression condition = null;
604 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000605}
606{
Till Westmann31c21f92013-05-08 09:21:53 -0700607 "delete" var = Variable()
608 {
609 getCurrentScope().addNewVarSymbolToScope(var.getVar());
610 }
611 "from" <DATASET> nameComponents = QualifiedName()
612 ("where" condition = Expression())?
613 {
614 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
615 }
vinayakb38b7ca42012-03-05 05:44:15 +0000616}
617
618UpdateStatement UpdateStatement() throws ParseException:
619{
Till Westmann31c21f92013-05-08 09:21:53 -0700620 VariableExpr vars;
621 Expression target;
622 Expression condition;
623 UpdateClause uc;
624 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000625}
626{
Till Westmann31c21f92013-05-08 09:21:53 -0700627 "update" vars = Variable() "in" target = Expression()
628 "where" condition = Expression()
629 <LEFTPAREN> (uc = UpdateClause()
630 {
631 ucs.add(uc);
632 }
633 ("," uc = UpdateClause()
634 {
635 ucs.add(uc);
636 }
637 )*) <RIGHTPAREN>
638 {
639 return new UpdateStatement(vars, target, condition, ucs);
640 }
vinayakb38b7ca42012-03-05 05:44:15 +0000641}
642
vinayakb38b7ca42012-03-05 05:44:15 +0000643UpdateClause UpdateClause() throws ParseException:
644{
Till Westmann31c21f92013-05-08 09:21:53 -0700645 Expression target = null;
646 Expression value = null ;
647 InsertStatement is = null;
648 DeleteStatement ds = null;
649 UpdateStatement us = null;
650 Expression condition = null;
651 UpdateClause ifbranch = null;
652 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000653}
654{
655 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700656 | is = InsertStatement()
657 | ds = DeleteStatement()
658 | us = UpdateStatement()
659 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
660 "then" ifbranch = UpdateClause()
661 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
662 {
663 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
664 }
vinayakb38b7ca42012-03-05 05:44:15 +0000665}
666
vinayakb38b7ca42012-03-05 05:44:15 +0000667Statement SetStatement() throws ParseException:
668{
669 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700670 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000671}
672{
Till Westmann14a20a72013-05-09 00:06:24 -0700673 "set" pn = Identifier() <STRING_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700674 {
675 pv = removeQuotesAndEscapes(token.image);
676 return new SetStatement(pn, pv);
677 }
vinayakb38b7ca42012-03-05 05:44:15 +0000678}
679
680Statement WriteStatement() throws ParseException:
681{
Till Westmann14a20a72013-05-09 00:06:24 -0700682 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000683 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000684 Statement stmt = null;
685 Query query;
686 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000687 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000688}
689{
Till Westmann31c21f92013-05-08 09:21:53 -0700690 "write" ((
Till Westmanna4242bc2013-05-08 17:49:55 -0700691 "output" "to" nodeName = Identifier() ":" <STRING_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700692 {
693 fileName = removeQuotesAndEscapes(token.image);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000694 }
Till Westmann31c21f92013-05-08 09:21:53 -0700695 ( "using" <STRING_LITERAL>
696 {
697 writerClass = removeQuotesAndEscapes(token.image);
698 }
699 )?
700 {
Till Westmann14a20a72013-05-09 00:06:24 -0700701 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
Till Westmann31c21f92013-05-08 09:21:53 -0700702 }
703 ) | (
704 "into" <DATASET>
705 {
706 nameComponents = QualifiedName();
707 }
708 <LEFTPAREN> query = Query() <RIGHTPAREN>
709 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000710 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700711 }
712 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000713 {
714 return stmt;
715 }
716}
717
vinayakb38b7ca42012-03-05 05:44:15 +0000718LoadFromFileStatement LoadStatement() throws ParseException:
719{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000720 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000721 Identifier datasetName = null;
722 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000723 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000724 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000725 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000726}
727{
Till Westmann31c21f92013-05-08 09:21:53 -0700728 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000729 {
Till Westmann31c21f92013-05-08 09:21:53 -0700730 dataverseName = nameComponents.first;
731 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000732 }
Till Westmann31c21f92013-05-08 09:21:53 -0700733 "using" adapterName = AdapterName() properties = Configuration()
734 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000735 {
Till Westmann31c21f92013-05-08 09:21:53 -0700736 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000737 }
738 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700739 {
740 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
741 }
vinayakb38b7ca42012-03-05 05:44:15 +0000742}
743
vinayakb38b7ca42012-03-05 05:44:15 +0000744
Till Westmann31c21f92013-05-08 09:21:53 -0700745String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000746{
ramangrover29669d8f62013-02-11 06:03:32 +0000747 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000748}
749{
Till Westmann14a20a72013-05-09 00:06:24 -0700750 ( adapterName = Identifier() | <STRING_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +0000751 {
Till Westmann31c21f92013-05-08 09:21:53 -0700752 adapterName = removeQuotesAndEscapes(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +0000753 }
ramangrover29669d8f62013-02-11 06:03:32 +0000754 )
vinayakb38b7ca42012-03-05 05:44:15 +0000755 {
Till Westmann31c21f92013-05-08 09:21:53 -0700756 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000757 }
758}
759
Till Westmann31c21f92013-05-08 09:21:53 -0700760Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000761{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000762 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700763 Map<String,String> configuration = null;
764 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000765}
766{
Till Westmann31c21f92013-05-08 09:21:53 -0700767 (
768 "begin" "feed" nameComponents = QualifiedName()
769 {
770 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
771 }
772 | "suspend" "feed" nameComponents = QualifiedName()
773 {
774 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
775 }
776 | "resume" "feed" nameComponents = QualifiedName()
777 {
778 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
779 }
780 | "end" "feed" nameComponents = QualifiedName()
781 {
782 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
783 }
784 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
785 {
786 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
787 }
788 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000789 {
Till Westmann31c21f92013-05-08 09:21:53 -0700790 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000791 }
792}
793
Till Westmann31c21f92013-05-08 09:21:53 -0700794Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000795{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000796 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700797 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000798}
799{
Till Westmann31c21f92013-05-08 09:21:53 -0700800 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000801 {
Till Westmann31c21f92013-05-08 09:21:53 -0700802 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000803 }
Till Westmann31c21f92013-05-08 09:21:53 -0700804 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000805 {
Till Westmann31c21f92013-05-08 09:21:53 -0700806 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000807 }
Till Westmann31c21f92013-05-08 09:21:53 -0700808 )* )? <RIGHTPAREN>
809 {
810 return configuration;
811 }
812}
813
814Pair<String, String> KeyValuePair() throws ParseException:
815{
816 String key;
817 String value;
818}
819{
820 <LEFTPAREN> <STRING_LITERAL>
821 {
822 key = removeQuotesAndEscapes(token.image);
823 }
824 "=" <STRING_LITERAL>
825 {
826 value = removeQuotesAndEscapes(token.image);
827 }
828 <RIGHTPAREN>
829 {
830 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000831 }
Till Westmann31c21f92013-05-08 09:21:53 -0700832}
833
834Map<String,String> Properties() throws ParseException:
835{
836 Map<String,String> properties = new HashMap<String,String>();
837 Pair<String, String> property;
838}
839{
840 ( <LEFTPAREN> property = Property()
841 {
842 properties.put(property.first, property.second);
843 }
844 ( "," property = Property()
845 {
846 properties.put(property.first, property.second);
847 }
848 )* <RIGHTPAREN> )?
849 {
850 return properties;
851 }
852}
853
854Pair<String, String> Property() throws ParseException:
855{
856 String key;
857 String value;
858}
859{
Till Westmann14a20a72013-05-09 00:06:24 -0700860 key = Identifier() "=" ( <STRING_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700861 {
862 value = removeQuotesAndEscapes(token.image);
863 }
864 | <INTEGER_LITERAL>
865 {
866 try {
867 value = "" + Long.valueOf(token.image);
868 } catch (NumberFormatException nfe) {
869 throw new ParseException("inapproriate value: " + token.image);
870 }
871 }
872 )
873 {
874 return new Pair<String, String>(key.toUpperCase(), value);
875 }
vinayakb38b7ca42012-03-05 05:44:15 +0000876}
877
878TypeExpression TypeExpr() throws ParseException:
879{
880 TypeExpression typeExpr = null;
881}
882{
883 (
884 typeExpr = RecordTypeDef()
885 | typeExpr = TypeReference()
886 | typeExpr = OrderedListTypeDef()
887 | typeExpr = UnorderedListTypeDef()
888 )
889 {
890 return typeExpr;
891 }
892}
893
894RecordTypeDefinition RecordTypeDef() throws ParseException:
895{
896 RecordTypeDefinition recType = new RecordTypeDefinition();
897 RecordTypeDefinition.RecordKind recordKind = null;
898}
899{
900 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
901 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
902 "{"
903 {
904 String hint = getHint(token);
905 if (hint != null) {
906 String splits[] = hint.split(" +");
907 if (splits[0].equals(GEN_FIELDS_HINT)) {
908 if (splits.length != 5) {
909 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
910 }
911 if (!splits[1].equals("int")) {
912 throw new ParseException("The only supported type for gen-fields is int.");
913 }
914 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
915 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
916 recType.setUndeclaredFieldsDataGen(ufdg);
917 }
918 }
919
920 }
921 (
922 RecordField(recType)
923 ( "," RecordField(recType) )*
924 )?
925 "}"
926 {
927 if (recordKind == null) {
928 recordKind = RecordTypeDefinition.RecordKind.OPEN;
929 }
930 recType.setRecordKind(recordKind);
931 return recType;
932 }
933}
934
935void RecordField(RecordTypeDefinition recType) throws ParseException:
936{
937 String fieldName;
938 TypeExpression type = null;
939 boolean nullable = false;
940}
941{
Till Westmann14a20a72013-05-09 00:06:24 -0700942 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000943 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700944 fieldName = token.image;
945 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000946 IRecordFieldDataGen rfdg = null;
947 if (hint != null) {
948 String splits[] = hint.split(" +");
949 if (splits[0].equals(VAL_FILE_HINT)) {
950 File[] valFiles = new File[splits.length - 1];
951 for (int k=1; k<splits.length; k++) {
952 valFiles[k-1] = new File(splits[k]);
953 }
954 rfdg = new FieldValFileDataGen(valFiles);
955 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
956 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
957 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
958 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
959 } else if (splits[0].equals(LIST_HINT)) {
960 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
961 } else if (splits[0].equals(INTERVAL_HINT)) {
962 FieldIntervalDataGen.ValueType vt;
963 if (splits[1].equals("int")) {
964 vt = FieldIntervalDataGen.ValueType.INT;
965 } else if (splits[1].equals("long")) {
966 vt = FieldIntervalDataGen.ValueType.LONG;
967 } else if (splits[1].equals("float")) {
968 vt = FieldIntervalDataGen.ValueType.FLOAT;
969 } else if (splits[1].equals("double")) {
970 vt = FieldIntervalDataGen.ValueType.DOUBLE;
971 } else {
972 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
973 }
974 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
975 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
976 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
977 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
978 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
979 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
980 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
981 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
982 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
983 } else if (splits[0].equals(AUTO_HINT)) {
984 rfdg = new AutoDataGen(splits[1]);
985 }
986 }
987 }
988 ":"
989 ( type = TypeExpr() )
990 ("?" { nullable = true; } )?
991 {
992 recType.addField(fieldName, type, nullable, rfdg);
993 }
994}
995
996TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000997{
Till Westmann14a20a72013-05-09 00:06:24 -0700998 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700999}
1000{
1001 id = Identifier()
1002 {
Till Westmann14a20a72013-05-09 00:06:24 -07001003 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -07001004 }
vinayakb38b7ca42012-03-05 05:44:15 +00001005}
1006
1007OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1008{
1009 TypeExpression type = null;
1010}
1011{
1012 "["
1013 ( type = TypeExpr() )
1014 "]"
1015 {
1016 return new OrderedListTypeDefinition(type);
1017 }
1018}
1019
1020
1021UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1022{
1023 TypeExpression type = null;
1024}
1025{
1026 "{{"
1027 ( type = TypeExpr() )
1028 "}}"
1029 {
1030 return new UnorderedListTypeDefinition(type);
1031 }
1032}
1033
Till Westmann31c21f92013-05-08 09:21:53 -07001034
1035Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001036{
Till Westmann31c21f92013-05-08 09:21:53 -07001037 Pair<Identifier,Identifier> name = null;
1038}
1039{
1040 name = QualifiedName()
1041 {
1042 if (name.first == null) {
1043 name.first = new Identifier(defaultDataverse);
1044 }
1045 return name;
1046 }
1047}
1048
Till Westmann14a20a72013-05-09 00:06:24 -07001049String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001050{
1051}
1052{
1053 <IDENTIFIER>
1054 {
Till Westmann14a20a72013-05-09 00:06:24 -07001055 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001056 }
1057}
1058
Till Westmann31c21f92013-05-08 09:21:53 -07001059Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1060{
Till Westmann14a20a72013-05-09 00:06:24 -07001061 String first = null;
1062 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001063}
1064{
Till Westmanna4242bc2013-05-08 17:49:55 -07001065 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001066 {
Till Westmann14a20a72013-05-09 00:06:24 -07001067 Identifier id1 = null;
1068 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001069 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001070 id2 = new Identifier(first);
1071 } else
1072 {
1073 id1 = new Identifier(first);
1074 id2 = new Identifier(second);
1075 }
1076 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001077 }
1078}
1079
Till Westmann31c21f92013-05-08 09:21:53 -07001080Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001081{
Till Westmann14a20a72013-05-09 00:06:24 -07001082 String first = null;
1083 String second = null;
1084 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001085}
1086{
Till Westmanna4242bc2013-05-08 17:49:55 -07001087 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001088 {
Till Westmann14a20a72013-05-09 00:06:24 -07001089 Identifier id1 = null;
1090 Identifier id2 = null;
1091 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001092 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001093 id2 = new Identifier(first);
1094 id3 = new Identifier(second);
1095 } else {
1096 id1 = new Identifier(first);
1097 id2 = new Identifier(second);
1098 id3 = new Identifier(third);
1099 }
1100 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001101 }
1102}
1103
vinayakb38b7ca42012-03-05 05:44:15 +00001104FunctionDecl FunctionDeclaration() throws ParseException:
1105{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001106 FunctionDecl funcDecl;
1107 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001108 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001109 int arity = 0;
1110 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1111 Expression funcBody;
1112 VarIdentifier var = null;
1113 createNewScope();
1114}
1115{
Till Westmann14a20a72013-05-09 00:06:24 -07001116 "declare" "function" functionName = Identifier() <LEFTPAREN> (<VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001117 {
1118 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -07001119 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001120 paramList.add(var);
1121 getCurrentScope().addNewVarSymbolToScope(var);
1122 arity++;
1123 }
Till Westmann31c21f92013-05-08 09:21:53 -07001124 ("," <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001125 {
1126 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -07001127 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001128 paramList.add(var);
1129 getCurrentScope().addNewVarSymbolToScope(var);
1130 arity++;
Till Westmann31c21f92013-05-08 09:21:53 -07001131 }
1132 )*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001133 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001134 signature = new FunctionSignature(defaultDataverse, functionName, arity);
1135 getCurrentScope().addFunctionDescriptor(signature, false);
1136 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1137 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001138 }
1139}
1140
vinayakb38b7ca42012-03-05 05:44:15 +00001141
Till Westmann31c21f92013-05-08 09:21:53 -07001142Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001143{
1144 Query query = new Query();
1145 Expression expr;
1146}
1147{
Till Westmann31c21f92013-05-08 09:21:53 -07001148 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001149 {
1150 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001151 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001152 return query;
1153 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001154
vinayakb38b7ca42012-03-05 05:44:15 +00001155}
1156
1157
1158
1159Expression Expression():
1160{
1161 Expression expr = null;
1162 Expression exprP = null;
1163}
1164{
1165(
1166
1167//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1168 expr = OperatorExpr()
1169 | expr = IfThenElse()
1170 | expr = FLWOGR()
1171 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001172
vinayakb38b7ca42012-03-05 05:44:15 +00001173
1174)
1175 {
1176 return (exprP==null) ? expr : exprP;
1177 }
1178}
1179
1180
1181
1182Expression OperatorExpr()throws ParseException:
1183{
1184 OperatorExpr op = null;
1185 Expression operand = null;
1186}
1187{
1188 operand = AndExpr()
1189 (
1190
1191 "or"
1192 {
1193 if (op == null) {
1194 op = new OperatorExpr();
1195 op.addOperand(operand);
1196 op.setCurrentop(true);
1197 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001198 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001199 }
1200
1201 operand = AndExpr()
1202 {
1203 op.addOperand(operand);
1204 }
1205
1206 )*
1207
1208 {
1209 return op==null? operand: op;
1210 }
1211}
1212
1213Expression AndExpr()throws ParseException:
1214{
1215 OperatorExpr op = null;
1216 Expression operand = null;
1217}
1218{
1219 operand = RelExpr()
1220 (
1221
1222 "and"
1223 {
1224 if (op == null) {
1225 op = new OperatorExpr();
1226 op.addOperand(operand);
1227 op.setCurrentop(true);
1228 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001229 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001230 }
1231
1232 operand = RelExpr()
1233 {
1234 op.addOperand(operand);
1235 }
1236
1237 )*
1238
1239 {
1240 return op==null? operand: op;
1241 }
1242}
1243
1244
1245
1246Expression RelExpr()throws ParseException:
1247{
1248 OperatorExpr op = null;
1249 Expression operand = null;
1250 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001251 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001252}
1253{
1254 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001255 {
1256 if (operand instanceof VariableExpr) {
1257 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001258 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001259 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001260 }
1261 }
1262 }
1263
1264 (
1265 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1266 {
alexander.behm07617fd2012-07-25 10:13:50 +00001267 String mhint = getHint(token);
1268 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1269 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1270 }
vinayakb38b7ca42012-03-05 05:44:15 +00001271 if (op == null) {
1272 op = new OperatorExpr();
1273 op.addOperand(operand, broadcast);
1274 op.setCurrentop(true);
1275 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001276 }
1277 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001278 }
1279
1280 operand = AddExpr()
1281 {
alexander.behm07617fd2012-07-25 10:13:50 +00001282 broadcast = false;
1283 if (operand instanceof VariableExpr) {
1284 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001285 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1286 broadcast = true;
1287 }
alexander.behm07617fd2012-07-25 10:13:50 +00001288 }
vinayakb38b7ca42012-03-05 05:44:15 +00001289 op.addOperand(operand, broadcast);
1290 }
1291 )?
1292
1293 {
alexander.behm07617fd2012-07-25 10:13:50 +00001294 if (annotation != null) {
1295 op.addHint(annotation);
1296 }
vinayakb38b7ca42012-03-05 05:44:15 +00001297 return op==null? operand: op;
1298 }
1299}
1300
1301Expression AddExpr()throws ParseException:
1302{
1303 OperatorExpr op = null;
1304 Expression operand = null;
1305}
1306{
1307 operand = MultExpr()
1308
1309 ( ("+" | "-")
1310 {
1311 if (op == null) {
1312 op = new OperatorExpr();
1313 op.addOperand(operand);
1314 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001315 }
1316 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001317 }
1318
1319 operand = MultExpr()
1320 {
1321 op.addOperand(operand);
1322 }
1323 )*
1324
1325 {
1326 return op==null? operand: op;
1327 }
1328}
1329
1330Expression MultExpr()throws ParseException:
1331{
1332 OperatorExpr op = null;
1333 Expression operand = null;
1334}
1335{
1336 operand = UnionExpr()
1337
1338 (( "*" | "/" | "%" | <CARET> | "idiv")
1339 {
1340 if (op == null) {
1341 op = new OperatorExpr();
1342 op.addOperand(operand);
1343 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001344 }
1345 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001346 }
1347 operand = UnionExpr()
1348 {
1349 op.addOperand(operand);
1350 }
1351 )*
1352
1353 {
1354 return op==null?operand:op;
1355 }
1356}
1357
1358Expression UnionExpr() throws ParseException:
1359{
1360 UnionExpr union = null;
1361 Expression operand1 = null;
1362 Expression operand2 = null;
1363}
1364{
1365 operand1 = UnaryExpr()
1366 ("union"
1367 (operand2 = UnaryExpr()) {
1368 if (union == null) {
1369 union = new UnionExpr();
1370 union.addExpr(operand1);
1371 }
1372 union.addExpr(operand2);
1373 } )*
1374 {
1375 return (union == null)? operand1: union;
1376 }
1377}
1378
1379Expression UnaryExpr() throws ParseException:
1380{
1381 Expression uexpr = null;
1382 Expression expr = null;
1383}
1384{
1385 (( "+"|"-")
1386 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001387 uexpr = new UnaryExpr();
1388 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001389 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001390 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001391 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1392 else
1393 throw new ParseException();
1394 }
1395 )?
1396
1397 expr = ValueExpr()
1398 {
1399 if(uexpr!=null){
1400 ((UnaryExpr)uexpr).setExpr(expr);
1401 return uexpr;
1402 }
1403 else{
1404 return expr;
1405 }
1406 }
1407}
1408
1409Expression ValueExpr() throws ParseException:
1410{
1411 Expression expr;
1412}
1413{
1414 expr = FieldOrIndexAccessor()
1415 {
1416 return expr;
1417 }
1418}
1419
1420
1421Expression FieldOrIndexAccessor()throws ParseException:
1422{
1423 Expression expr = null;
1424 Identifier ident = null;
1425 AbstractAccessor fa = null;
1426 int index;
1427
1428}
1429{
1430 ( expr = PrimaryExpr()
1431
1432 )
1433
1434
1435 (
1436 (
1437 ident = Field()
1438 {
1439 if(fa == null)
1440 fa = new FieldAccessor(expr, ident);
1441 else
1442 fa = new FieldAccessor(fa, ident);
1443 }
1444 )
1445 | (
1446 index = Index()
1447 {
1448 if(fa == null)
1449 fa = new IndexAccessor(expr, index);
1450 else
1451 fa = new IndexAccessor(fa, index);
1452 }
1453 )
1454 )*
1455
1456
1457 {
1458 return fa==null?expr:fa;
1459 }
1460}
1461
1462Identifier Field() throws ParseException:
1463{
Till Westmann14a20a72013-05-09 00:06:24 -07001464 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001465}
1466{
Till Westmanna4242bc2013-05-08 17:49:55 -07001467 "." ident = Identifier()
1468 {
Till Westmann14a20a72013-05-09 00:06:24 -07001469 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001470 }
vinayakb38b7ca42012-03-05 05:44:15 +00001471}
1472
1473int Index() throws ParseException:
1474{
1475 Expression expr = null;
1476 int idx = -2;
1477}
1478{
1479 "[" ( expr = Expression()
1480 {
1481 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1482 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001483 Literal lit = ((LiteralExpr)expr).getValue();
1484 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1485 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001486 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001487 }
vinayakb38b7ca42012-03-05 05:44:15 +00001488 else {
1489 throw new ParseException("Index should be an INTEGER");
1490 }
1491 }
1492
1493 }
1494
1495 | "?"
1496 {
1497 idx = IndexAccessor.ANY;
1498 // ANY
1499 }
1500
1501 )
1502
1503 "]"
1504 {
1505 return idx;
1506 }
1507}
1508
1509
1510Expression PrimaryExpr()throws ParseException:
1511{
1512 Expression expr = null;
1513}
1514{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001515 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | DatasetAccessExpression | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +00001516 (
1517 expr =Literal()
1518 | expr = FunctionCallExpr()
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001519 | expr = DatasetAccessExpression()
vinayakb38b7ca42012-03-05 05:44:15 +00001520 | expr =VariableRef()
1521
1522 {
1523 if(((VariableExpr)expr).getIsNewVar() == true)
1524 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1525 }
1526 | expr = ListConstructor()
1527 | expr = RecordConstructor()
1528 | expr = ParenthesizedExpression()
1529 )
1530 {
1531 return expr;
1532 }
1533}
1534
1535Expression Literal() throws ParseException:
1536{
vinayakb38b7ca42012-03-05 05:44:15 +00001537 LiteralExpr lit = new LiteralExpr();
vinayakb38b7ca42012-03-05 05:44:15 +00001538}
1539{
Till Westmanna4242bc2013-05-08 17:49:55 -07001540 ( <STRING_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001541 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001542 lit.setValue(new StringLiteral(removeQuotesAndEscapes(token.image)));
1543 }
1544 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001545 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001546 try {
1547 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1548 } catch(NumberFormatException ex) {
1549 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1550 }
1551 }
1552 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001553 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001554 lit.setValue(new FloatLiteral(new Float(token.image)));
1555 }
1556 | <DOUBLE_LITERAL>
1557 {
1558 lit.setValue(new DoubleLiteral(new Double(token.image)));
1559 }
1560 | <NULL>
1561 {
1562 lit.setValue(NullLiteral.INSTANCE);
1563 }
1564 | <TRUE>
1565 {
1566 lit.setValue(TrueLiteral.INSTANCE);
1567 }
1568 | <FALSE>
1569 {
1570 lit.setValue(FalseLiteral.INSTANCE);
1571 }
1572 )
vinayakb38b7ca42012-03-05 05:44:15 +00001573 {
1574 return lit;
1575 }
1576}
1577
1578
1579VariableExpr VariableRef() throws ParseException:
1580{
1581 VariableExpr varExp = new VariableExpr();
1582 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001583}
1584{
1585 <VARIABLE>
1586 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001587 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001588 Identifier ident = lookupSymbol(varName);
1589 if (isInForbiddenScopes(varName)) {
1590 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.");
1591 }
1592 if(ident != null) { // exist such ident
1593 varExp.setIsNewVar(false);
1594 varExp.setVar((VarIdentifier)ident);
1595 } else {
1596 varExp.setVar(var);
1597 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001598 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001599 return varExp;
1600 }
1601}
1602
1603
1604VariableExpr Variable() throws ParseException:
1605{
1606 VariableExpr varExp = new VariableExpr();
1607 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001608}
1609{
1610 <VARIABLE>
1611 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001612 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001613 if(ident != null) { // exist such ident
1614 varExp.setIsNewVar(false);
1615 }
1616 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001617 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001618 return varExp;
1619 }
1620}
1621
1622Expression ListConstructor() throws ParseException:
1623{
1624 Expression expr = null;
1625}
1626{
1627 (
1628 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1629 )
1630
1631 {
1632 return expr;
1633 }
1634}
1635
1636
1637ListConstructor OrderedListConstructor() throws ParseException:
1638{
1639 ListConstructor expr = new ListConstructor();
1640 Expression tmp = null;
1641 List<Expression> exprList = new ArrayList<Expression>();
1642 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1643}
1644{
1645
1646 "["
1647 ( tmp = Expression()
1648 {
1649 exprList.add(tmp);
1650 }
1651
1652 ("," tmp = Expression() { exprList.add(tmp); })*
1653 )?
1654
1655 "]"
1656
1657 {
1658 expr.setExprList(exprList);
1659 return expr;
1660 }
1661}
1662
1663ListConstructor UnorderedListConstructor() throws ParseException:
1664{
1665 ListConstructor expr = new ListConstructor();
1666 Expression tmp = null;
1667 List<Expression> exprList = new ArrayList<Expression>();
1668 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1669}
1670{
1671
1672 "{{" ( tmp = Expression()
1673 {
1674 exprList.add(tmp);
1675 }
1676 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1677 {
1678 expr.setExprList(exprList);
1679 return expr;
1680 }
1681}
1682
1683RecordConstructor RecordConstructor() throws ParseException:
1684{
1685 RecordConstructor expr = new RecordConstructor();
1686 FieldBinding tmp = null;
1687 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1688}
1689{
1690 "{" (tmp = FieldBinding()
1691 {
1692 fbList.add(tmp);
1693 }
1694 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1695 {
1696 expr.setFbList(fbList);
1697 return expr;
1698 }
1699}
1700
1701FieldBinding FieldBinding() throws ParseException:
1702{
1703 FieldBinding fb = new FieldBinding();
1704 Expression left, right;
1705}
1706{
1707 left = Expression() ":" right = Expression()
1708 {
1709 fb.setLeftExpr(left);
1710 fb.setRightExpr(right);
1711 return fb;
1712 }
1713}
1714
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001715
vinayakb38b7ca42012-03-05 05:44:15 +00001716Expression FunctionCallExpr() throws ParseException:
1717{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001718 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001719 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001720 Expression tmp;
1721 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001722 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001723 String funcName;
1724 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001725 String hint = null;
1726 String id1 = null;
1727 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001728}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001729{
Till Westmann31c21f92013-05-08 09:21:53 -07001730 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001731 {
Till Westmann31c21f92013-05-08 09:21:53 -07001732 dataverse = funcId.first.getValue();
1733 funcName = funcId.second.getValue();
1734 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001735 }
Till Westmann31c21f92013-05-08 09:21:53 -07001736 <LEFTPAREN> (tmp = Expression()
1737 {
1738 argList.add(tmp);
1739 arity ++;
1740 }
1741 ("," tmp = Expression()
1742 {
1743 argList.add(tmp);
1744 arity++;
1745 }
1746 )*)? <RIGHTPAREN>
1747 {
1748 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1749 if (signature == null) {
1750 signature = new FunctionSignature(dataverse, funcName, arity);
1751 }
1752 callExpr = new CallExpr(signature,argList);
1753 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1754 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1755 }
1756 return callExpr;
1757 }
vinayakb38b7ca42012-03-05 05:44:15 +00001758}
1759
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001760Expression DatasetAccessExpression() throws ParseException:
1761{
1762 CallExpr callExpr;
1763 List<Expression> argList = new ArrayList<Expression>();
1764 String funcName;
1765 String dataverse;
Till Westmann14a20a72013-05-09 00:06:24 -07001766 String arg1 = null;
1767 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001768 LiteralExpr ds;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001769 Expression nameArg;
1770 int arity = 0;
1771}
1772{
Till Westmann14a20a72013-05-09 00:06:24 -07001773 <DATASET>
1774 {
1775 dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1776 funcName = token.image;
1777 }
1778 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1779 {
1780 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
1781 ds = new LiteralExpr();
1782 ds.setValue( new StringLiteral(name) );
1783 argList.add(ds);
1784 arity ++;
1785 }
1786 | ( <LEFTPAREN> nameArg = Expression()
1787 {
1788 argList.add(nameArg);
1789 arity ++;
1790 }
1791 ( "," nameArg = Expression()
1792 {
1793 argList.add(nameArg);
1794 arity++;
1795 }
1796 )* <RIGHTPAREN> ) )
1797 {
1798 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1799 if (signature == null) {
1800 signature = new FunctionSignature(dataverse, funcName, arity);
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001801 }
Till Westmann14a20a72013-05-09 00:06:24 -07001802 callExpr = new CallExpr(signature,argList);
1803 return callExpr;
1804 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001805}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001806
vinayakb38b7ca42012-03-05 05:44:15 +00001807Expression ParenthesizedExpression() throws ParseException:
1808{
1809 Expression expr;
1810}
1811{
1812 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1813 {
1814 return expr;
1815 }
1816}
1817
1818Expression IfThenElse() throws ParseException:
1819{
1820 Expression condExpr;
1821 Expression thenExpr;
1822 Expression elseExpr;
1823 IfExpr ifExpr = new IfExpr();
1824}
1825{
1826 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1827
1828 {
1829 ifExpr.setCondExpr(condExpr);
1830 ifExpr.setThenExpr(thenExpr);
1831 ifExpr.setElseExpr(elseExpr);
1832 return ifExpr;
1833 }
1834}
1835
1836Expression FLWOGR() throws ParseException:
1837{
1838 FLWOGRExpression flworg = new FLWOGRExpression();
1839 List<Clause> clauseList = new ArrayList<Clause>();
1840 Expression returnExpr;
1841 Clause tmp;
1842 createNewScope();
1843}
1844{
1845 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1846 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1847
1848 {
1849 flworg.setClauseList(clauseList);
1850 flworg.setReturnExpr(returnExpr);
1851 removeCurrentScope();
1852 return flworg;
1853 }
1854}
1855
1856Clause Clause()throws ParseException :
1857{
1858 Clause clause;
1859}
1860{
1861 (
1862 clause = ForClause()
1863 | clause = LetClause()
1864 | clause = WhereClause()
1865 | clause = OrderbyClause()
1866 | clause = GroupClause()
1867 | clause = LimitClause()
1868 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001869 )
1870 {
1871 return clause;
1872 }
1873}
1874
1875Clause ForClause()throws ParseException :
1876{
1877 ForClause fc = new ForClause();
1878 VariableExpr varExp;
1879 VariableExpr varPos = null;
1880 Expression inExp;
1881 extendCurrentScope();
1882}
1883{
1884 "for" varExp = Variable()
1885 {
1886 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1887 }
1888 ("at" varPos = Variable()
1889 {
1890 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1891 }
1892 )?
1893 "in" ( inExp = Expression() )
1894 {
1895 fc.setVarExpr(varExp);
1896 fc.setInExpr(inExp);
1897 if (varPos != null) {
1898 fc.setPosExpr(varPos);
1899 }
1900 return fc;
1901 }
1902}
1903
1904Clause LetClause() throws ParseException:
1905{
1906 LetClause lc = new LetClause();
1907 VariableExpr varExp;
1908 Expression beExp;
1909 extendCurrentScope();
1910}
1911{
ilovesoupb2527c12012-07-12 03:21:13 +00001912 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001913 {
1914 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001915 lc.setVarExpr(varExp);
1916 lc.setBeExpr(beExp);
1917 return lc;
1918 }
1919}
1920
1921Clause WhereClause()throws ParseException :
1922{
1923 WhereClause wc = new WhereClause();
1924 Expression whereExpr;
1925}
1926{
1927 "where" whereExpr = Expression()
1928 {
1929 wc.setWhereExpr(whereExpr);
1930 return wc;
1931 }
1932}
1933
1934Clause OrderbyClause()throws ParseException :
1935{
1936 OrderbyClause oc = new OrderbyClause();
1937 Expression orderbyExpr;
1938 List<Expression> orderbyList = new ArrayList<Expression>();
1939 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1940 int numOfOrderby = 0;
1941}
1942{
1943 (
1944 "order"
1945 {
1946 String hint = getHint(token);
1947 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1948 String splits[] = hint.split(" +");
1949 int numFrames = Integer.parseInt(splits[1]);
1950 int numTuples = Integer.parseInt(splits[2]);
1951 oc.setNumFrames(numFrames);
1952 oc.setNumTuples(numTuples);
1953 }
1954 }
1955 "by" orderbyExpr = Expression()
1956 {
1957 orderbyList.add(orderbyExpr);
1958 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1959 }
1960 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1961 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1962 {
1963 modifierList.add(modif);
1964 }
1965
1966 ("," orderbyExpr = Expression()
1967 {
1968 orderbyList.add(orderbyExpr);
1969 modif = OrderbyClause.OrderModifier.ASC;
1970 }
1971 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1972 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1973 {
1974 modifierList.add(modif);
1975 }
1976 )*
1977)
1978 {
1979 oc.setModifierList(modifierList);
1980 oc.setOrderbyList(orderbyList);
1981 return oc;
1982 }
1983}
1984Clause GroupClause()throws ParseException :
1985{
1986 GroupbyClause gbc = new GroupbyClause();
1987 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1988 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1989 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1990 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1991 VariableExpr var = null;
1992 VariableExpr withVar = null;
1993 Expression expr = null;
1994 VariableExpr decorVar = null;
1995 Expression decorExpr = null;
1996}
1997{
1998 {
1999 Scope newScope = extendCurrentScopeNoPush(true);
2000 // extendCurrentScope(true);
2001 }
2002 "group"
2003 {
2004 String hint = getHint(token);
2005 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2006 gbc.setHashGroupByHint(true);
2007 }
2008 }
2009 "by" (LOOKAHEAD(2) var = Variable()
2010 {
2011 newScope.addNewVarSymbolToScope(var.getVar());
2012 } ":=")?
2013 expr = Expression()
2014 {
2015 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2016 vePairList.add(pair1);
2017 }
2018 ("," ( LOOKAHEAD(2) var = Variable()
2019 {
2020 newScope.addNewVarSymbolToScope(var.getVar());
2021 } ":=")?
2022 expr = Expression()
2023 {
2024 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2025 vePairList.add(pair2);
2026 }
2027 )*
2028 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2029 {
2030 newScope.addNewVarSymbolToScope(decorVar.getVar());
2031 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2032 decorPairList.add(pair3);
2033 }
2034 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2035 {
2036 newScope.addNewVarSymbolToScope(decorVar.getVar());
2037 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2038 decorPairList.add(pair4);
2039 }
2040 )*
2041 )?
2042 "with" withVar = VariableRef()
2043 {
2044 if(withVar.getIsNewVar()==true)
2045 throw new ParseException("can't find variable " + withVar.getVar());
2046 withVarList.add(withVar);
2047 newScope.addNewVarSymbolToScope(withVar.getVar());
2048 }
2049 ("," withVar = VariableRef()
2050 {
2051 if(withVar.getIsNewVar()==true)
2052 throw new ParseException("can't find variable " + withVar.getVar());
2053 withVarList.add(withVar);
2054 newScope.addNewVarSymbolToScope(withVar.getVar());
2055 })*
2056 {
2057 gbc.setGbyPairList(vePairList);
2058 gbc.setDecorPairList(decorPairList);
2059 gbc.setWithVarList(withVarList);
2060 replaceCurrentScope(newScope);
2061 return gbc;
2062 }
2063}
2064
2065
2066LimitClause LimitClause() throws ParseException:
2067{
2068 LimitClause lc = new LimitClause();
2069 Expression expr;
2070 pushForbiddenScope(getCurrentScope());
2071}
2072{
2073 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2074 ("offset" expr = Expression() { lc.setOffset(expr); })?
2075
2076 {
2077 popForbiddenScope();
2078 return lc;
2079 }
2080}
2081
2082DistinctClause DistinctClause() throws ParseException:
2083{
2084 List<Expression> exprs = new ArrayList<Expression>();
2085 Expression expr;
2086}
2087{
2088 "distinct" "by" expr = Expression()
2089 {
2090 exprs.add(expr);
2091 }
2092 ("," expr = Expression()
2093 {
2094 exprs.add(expr);
2095 }
2096 )*
2097 {
2098 return new DistinctClause(exprs);
2099 }
2100}
2101
vinayakb38b7ca42012-03-05 05:44:15 +00002102QuantifiedExpression QuantifiedExpression()throws ParseException:
2103{
2104 QuantifiedExpression qc = new QuantifiedExpression();
2105 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2106 Expression satisfiesExpr;
2107 VariableExpr var;
2108 Expression inExpr;
2109 QuantifiedPair pair;
2110}
2111{
2112 {
2113 createNewScope();
2114 }
2115
2116 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2117 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2118 var = Variable() "in" inExpr = Expression()
2119 {
2120 pair = new QuantifiedPair(var, inExpr);
2121 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2122 quantifiedList.add(pair);
2123 }
2124 (
2125 "," var = Variable() "in" inExpr = Expression()
2126 {
2127 pair = new QuantifiedPair(var, inExpr);
2128 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2129 quantifiedList.add(pair);
2130 }
2131 )*
2132 "satisfies" satisfiesExpr = Expression()
2133 {
2134 qc.setSatisfiesExpr(satisfiesExpr);
2135 qc.setQuantifiedList(quantifiedList);
2136 removeCurrentScope();
2137 return qc;
2138 }
2139}
2140
2141TOKEN_MGR_DECLS:
2142{
2143 public int commentDepth = 0;
2144}
2145
2146<DEFAULT>
2147TOKEN :
2148{
2149 <CARET : "^" >
2150}
2151
2152<DEFAULT>
2153TOKEN :
2154{
2155 <DATASET : "dataset" >
2156}
2157
2158<DEFAULT>
2159TOKEN :
2160{
2161 <LEFTPAREN : "(" >
2162}
2163
2164<DEFAULT>
2165TOKEN :
2166{
2167 <RIGHTPAREN : ")" >
2168}
2169
2170
2171<DEFAULT>
2172TOKEN :
2173{
2174 <INTEGER_LITERAL : (<DIGIT>)+ >
2175}
2176
2177
2178<DEFAULT>
2179TOKEN :
2180{
2181 <NULL : "null">
2182}
2183
2184<DEFAULT>
2185TOKEN :
2186{
2187 <TRUE : "true">
2188}
2189
2190<DEFAULT>
2191TOKEN :
2192{
2193 <FALSE : "false">
2194}
2195
2196<DEFAULT>
2197TOKEN :
2198{
2199 <#DIGIT : ["0" - "9"]>
2200}
2201
2202
2203TOKEN:
2204{
2205 < DOUBLE_LITERAL: <INTEGER>
2206 | <INTEGER> ( "." <INTEGER> )?
2207 | "." <INTEGER>
2208 >
2209 |
2210 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2211 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2212 | "." <INTEGER> ( "f" | "F" )
2213 >
2214 |
2215 <INTEGER : (<DIGIT>)+ >
2216}
2217
2218<DEFAULT>
2219TOKEN :
2220{
2221 <#LETTER : ["A" - "Z", "a" - "z"]>
2222}
2223
2224<DEFAULT>
2225TOKEN :
2226{
2227 <SPECIALCHARS : ["$", "_", "-"] >
2228}
2229
2230<DEFAULT>
2231TOKEN :
2232{
2233 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2234 |
2235 < #EscapeQuot: "\\\"" >
2236 |
2237 < #EscapeApos: "\\\'" >
2238}
2239
2240<DEFAULT>
2241TOKEN :
2242{
2243 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2244}
2245
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002246
vinayakb38b7ca42012-03-05 05:44:15 +00002247<DEFAULT>
2248TOKEN :
2249{
2250 <VARIABLE : "$" <IDENTIFIER> >
2251}
2252
2253SKIP:
2254{
2255 " "
2256| "\t"
2257| "\r"
2258| "\n"
2259}
2260
2261SKIP:
2262{
2263 <"//" (~["\n"])* "\n">
2264}
2265
2266SKIP:
2267{
2268 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2269}
2270
2271
2272SKIP:
2273{
2274 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2275}
2276
2277<INSIDE_COMMENT>
2278SPECIAL_TOKEN:
2279{
2280 <"+"(" ")*(~["*"])*>
2281}
2282
2283<INSIDE_COMMENT>
2284SKIP:
2285{
2286 <"/*"> {commentDepth++;}
2287}
2288
2289<INSIDE_COMMENT>
2290SKIP:
2291{
2292 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2293| <~[]>
2294}