blob: a00ec65b6557144b12d8ec40a92c7edbfe34b1fc [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 Westmannd7dcb122013-05-16 13:19:09 -0700304 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700305 <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;
Till Westmann31c21f92013-05-08 09:21:53 -0700423 Expression functionBodyExpr;
424 Token beginPos;
425 Token endPos;
426 Pair<Identifier,Identifier> nameComponents=null;
427
428 createNewScope();
429}
430{
431 "function" nameComponents = FunctionOrTypeName()
432 ifNotExists = IfNotExists()
Till Westmannd7dcb122013-05-16 13:19:09 -0700433 paramList = ParameterList()
434 "{"
435 {
436 beginPos = token;
437 }
438 functionBodyExpr = Expression() "}"
439 {
440 endPos = token;
441 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
442 String dataverse = nameComponents.first.getValue();
443 String functionName = nameComponents.second.getValue();
444 signature = new FunctionSignature(dataverse, functionName, paramList.size());
445 getCurrentScope().addFunctionDescriptor(signature, false);
446 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
447 }
448}
449
450List<VarIdentifier> ParameterList() throws ParseException:
451{
452 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
453 VarIdentifier var = null;
454}
455{
Till Westmann31c21f92013-05-08 09:21:53 -0700456 <LEFTPAREN> (<VARIABLE>
457 {
458 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700459 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700460 paramList.add(var);
461 getCurrentScope().addNewVarSymbolToScope(var);
462 }
463 ("," <VARIABLE>
464 {
465 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700466 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700467 paramList.add(var);
468 getCurrentScope().addNewVarSymbolToScope(var);
469 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700470 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700471 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700472 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700473 }
474}
475
476boolean IfNotExists() throws ParseException:
477{
478}
479{
480 ( "if not exists"
481 {
482 return true;
483 }
484 )?
485 {
486 return false;
487 }
488}
489
490FunctionSignature ApplyFunction() throws ParseException:
491{
492 FunctionSignature funcSig = null;
493}
494{
495 "apply" "function" funcSig = FunctionSignature()
496 {
497 return funcSig;
498 }
499}
500
ramangrover29566b3a92013-05-28 09:07:10 -0700501String GetPolicy() throws ParseException:
502{
503 String policy = null;
504}
505{
506 "using" "policy" policy = Identifier()
507 {
508 return policy;
509 }
510
511}
512
Till Westmann31c21f92013-05-08 09:21:53 -0700513FunctionSignature FunctionSignature() throws ParseException:
514{
515 Pair<Identifier,Identifier> pairId = null;
516 int arity = 0;
517}
518{
519 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
520 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700521 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700522 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
523 throw new ParseException(" invalid arity:" + arity);
524 }
525
526 String dataverse = pairId.first.getValue();
527 String functionName = pairId.second.getValue();
528 return new FunctionSignature(dataverse, functionName, arity);
529 }
530}
531
532List<String> PrimaryKey() throws ParseException:
533{
Till Westmann14a20a72013-05-09 00:06:24 -0700534 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700535 List<String> primaryKeyFields = new ArrayList<String>();
536}
537{
Till Westmann14a20a72013-05-09 00:06:24 -0700538 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700539 {
Till Westmann14a20a72013-05-09 00:06:24 -0700540 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700541 }
Till Westmann14a20a72013-05-09 00:06:24 -0700542 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700543 {
Till Westmann14a20a72013-05-09 00:06:24 -0700544 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700545 }
546 )*
547 {
548 return primaryKeyFields;
549 }
550}
551
552Statement DropStatement() throws ParseException:
553{
Till Westmann14a20a72013-05-09 00:06:24 -0700554 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700555 Pair<Identifier,Identifier> pairId = null;
556 Triple<Identifier,Identifier,Identifier> tripleId = null;
557 FunctionSignature funcSig = null;
558 boolean ifExists = false;
559 Statement stmt = null;
560}
561{
562 "drop"
563 (
564 <DATASET> pairId = QualifiedName() ifExists = IfExists()
565 {
566 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
567 }
568 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
569 {
570 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
571 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700572 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700573 {
Till Westmann14a20a72013-05-09 00:06:24 -0700574 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700575 }
576 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
577 {
578 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
579 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700580 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700581 {
Till Westmann14a20a72013-05-09 00:06:24 -0700582 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700583 }
584 | "function" funcSig = FunctionSignature() ifExists = IfExists()
585 {
586 stmt = new FunctionDropStatement(funcSig, ifExists);
587 }
588 )
589 {
590 return stmt;
591 }
592}
593
594boolean IfExists() throws ParseException :
595{
596}
597{
598 ( "if" "exists"
599 {
600 return true;
601 }
602 )?
603 {
604 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000605 }
606}
607
608InsertStatement InsertStatement() throws ParseException:
609{
Till Westmann31c21f92013-05-08 09:21:53 -0700610 Pair<Identifier,Identifier> nameComponents = null;
611 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000612}
613{
Till Westmann31c21f92013-05-08 09:21:53 -0700614 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
615 {
616 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
617 }
vinayakb38b7ca42012-03-05 05:44:15 +0000618}
619
620DeleteStatement DeleteStatement() throws ParseException:
621{
Till Westmann31c21f92013-05-08 09:21:53 -0700622 VariableExpr var = null;
623 Expression condition = null;
624 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000625}
626{
Till Westmann31c21f92013-05-08 09:21:53 -0700627 "delete" var = Variable()
628 {
629 getCurrentScope().addNewVarSymbolToScope(var.getVar());
630 }
631 "from" <DATASET> nameComponents = QualifiedName()
632 ("where" condition = Expression())?
633 {
634 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
635 }
vinayakb38b7ca42012-03-05 05:44:15 +0000636}
637
638UpdateStatement UpdateStatement() throws ParseException:
639{
Till Westmann31c21f92013-05-08 09:21:53 -0700640 VariableExpr vars;
641 Expression target;
642 Expression condition;
643 UpdateClause uc;
644 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000645}
646{
Till Westmann31c21f92013-05-08 09:21:53 -0700647 "update" vars = Variable() "in" target = Expression()
648 "where" condition = Expression()
649 <LEFTPAREN> (uc = UpdateClause()
650 {
651 ucs.add(uc);
652 }
653 ("," uc = UpdateClause()
654 {
655 ucs.add(uc);
656 }
657 )*) <RIGHTPAREN>
658 {
659 return new UpdateStatement(vars, target, condition, ucs);
660 }
vinayakb38b7ca42012-03-05 05:44:15 +0000661}
662
vinayakb38b7ca42012-03-05 05:44:15 +0000663UpdateClause UpdateClause() throws ParseException:
664{
Till Westmann31c21f92013-05-08 09:21:53 -0700665 Expression target = null;
666 Expression value = null ;
667 InsertStatement is = null;
668 DeleteStatement ds = null;
669 UpdateStatement us = null;
670 Expression condition = null;
671 UpdateClause ifbranch = null;
672 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000673}
674{
675 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700676 | is = InsertStatement()
677 | ds = DeleteStatement()
678 | us = UpdateStatement()
679 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
680 "then" ifbranch = UpdateClause()
681 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
682 {
683 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
684 }
vinayakb38b7ca42012-03-05 05:44:15 +0000685}
686
vinayakb38b7ca42012-03-05 05:44:15 +0000687Statement SetStatement() throws ParseException:
688{
689 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700690 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000691}
692{
Till Westmann7d535322013-05-09 00:40:02 -0700693 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700694 {
Till Westmann31c21f92013-05-08 09:21:53 -0700695 return new SetStatement(pn, pv);
696 }
vinayakb38b7ca42012-03-05 05:44:15 +0000697}
698
699Statement WriteStatement() throws ParseException:
700{
Till Westmann14a20a72013-05-09 00:06:24 -0700701 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000702 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000703 Statement stmt = null;
704 Query query;
705 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000706 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000707}
708{
Till Westmann31c21f92013-05-08 09:21:53 -0700709 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700710 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
711 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700712 {
Till Westmann14a20a72013-05-09 00:06:24 -0700713 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
Till Westmann31c21f92013-05-08 09:21:53 -0700714 }
715 ) | (
716 "into" <DATASET>
717 {
718 nameComponents = QualifiedName();
719 }
720 <LEFTPAREN> query = Query() <RIGHTPAREN>
721 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000722 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700723 }
724 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000725 {
726 return stmt;
727 }
728}
729
vinayakb38b7ca42012-03-05 05:44:15 +0000730LoadFromFileStatement LoadStatement() throws ParseException:
731{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000732 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000733 Identifier datasetName = null;
734 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000735 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000736 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000737 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000738}
739{
Till Westmann31c21f92013-05-08 09:21:53 -0700740 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000741 {
Till Westmann31c21f92013-05-08 09:21:53 -0700742 dataverseName = nameComponents.first;
743 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000744 }
Till Westmann31c21f92013-05-08 09:21:53 -0700745 "using" adapterName = AdapterName() properties = Configuration()
746 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000747 {
Till Westmann31c21f92013-05-08 09:21:53 -0700748 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000749 }
750 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700751 {
752 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
753 }
vinayakb38b7ca42012-03-05 05:44:15 +0000754}
755
vinayakb38b7ca42012-03-05 05:44:15 +0000756
Till Westmann31c21f92013-05-08 09:21:53 -0700757String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000758{
ramangrover29669d8f62013-02-11 06:03:32 +0000759 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000760}
761{
Till Westmann68d99652013-05-09 11:15:21 -0700762 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000763 {
Till Westmann7d535322013-05-09 00:40:02 -0700764 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000765 }
vinayakb38b7ca42012-03-05 05:44:15 +0000766}
767
Till Westmann31c21f92013-05-08 09:21:53 -0700768Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000769{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000770 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700771 Map<String,String> configuration = null;
772 Statement stmt = null;
ramangrover29566b3a92013-05-28 09:07:10 -0700773 String policy = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000774}
775{
Till Westmann31c21f92013-05-08 09:21:53 -0700776 (
ramangrover29566b3a92013-05-28 09:07:10 -0700777 "begin" "feed" nameComponents = QualifiedName() (policy = GetPolicy())?
Till Westmann31c21f92013-05-08 09:21:53 -0700778 {
ramangrover29566b3a92013-05-28 09:07:10 -0700779 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, policy, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700780 }
781 | "suspend" "feed" nameComponents = QualifiedName()
782 {
783 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
784 }
785 | "resume" "feed" nameComponents = QualifiedName()
786 {
787 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
788 }
789 | "end" "feed" nameComponents = QualifiedName()
790 {
791 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
792 }
793 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
794 {
795 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
796 }
797 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000798 {
Till Westmann31c21f92013-05-08 09:21:53 -0700799 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000800 }
801}
802
Till Westmann31c21f92013-05-08 09:21:53 -0700803Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000804{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000805 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700806 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000807}
808{
Till Westmann31c21f92013-05-08 09:21:53 -0700809 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000810 {
Till Westmann31c21f92013-05-08 09:21:53 -0700811 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000812 }
Till Westmann31c21f92013-05-08 09:21:53 -0700813 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000814 {
Till Westmann31c21f92013-05-08 09:21:53 -0700815 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000816 }
Till Westmann31c21f92013-05-08 09:21:53 -0700817 )* )? <RIGHTPAREN>
818 {
819 return configuration;
820 }
821}
822
823Pair<String, String> KeyValuePair() throws ParseException:
824{
825 String key;
826 String value;
827}
828{
Till Westmann7d535322013-05-09 00:40:02 -0700829 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700830 {
831 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000832 }
Till Westmann31c21f92013-05-08 09:21:53 -0700833}
834
835Map<String,String> Properties() throws ParseException:
836{
837 Map<String,String> properties = new HashMap<String,String>();
838 Pair<String, String> property;
839}
840{
841 ( <LEFTPAREN> property = Property()
842 {
843 properties.put(property.first, property.second);
844 }
845 ( "," property = Property()
846 {
847 properties.put(property.first, property.second);
848 }
849 )* <RIGHTPAREN> )?
850 {
851 return properties;
852 }
853}
854
855Pair<String, String> Property() throws ParseException:
856{
857 String key;
858 String value;
859}
860{
Till Westmann7d535322013-05-09 00:40:02 -0700861 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700862 {
863 try {
864 value = "" + Long.valueOf(token.image);
865 } catch (NumberFormatException nfe) {
866 throw new ParseException("inapproriate value: " + token.image);
867 }
868 }
869 )
870 {
871 return new Pair<String, String>(key.toUpperCase(), value);
872 }
vinayakb38b7ca42012-03-05 05:44:15 +0000873}
874
875TypeExpression TypeExpr() throws ParseException:
876{
877 TypeExpression typeExpr = null;
878}
879{
880 (
881 typeExpr = RecordTypeDef()
882 | typeExpr = TypeReference()
883 | typeExpr = OrderedListTypeDef()
884 | typeExpr = UnorderedListTypeDef()
885 )
886 {
887 return typeExpr;
888 }
889}
890
891RecordTypeDefinition RecordTypeDef() throws ParseException:
892{
893 RecordTypeDefinition recType = new RecordTypeDefinition();
894 RecordTypeDefinition.RecordKind recordKind = null;
895}
896{
897 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
898 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
899 "{"
900 {
901 String hint = getHint(token);
902 if (hint != null) {
903 String splits[] = hint.split(" +");
904 if (splits[0].equals(GEN_FIELDS_HINT)) {
905 if (splits.length != 5) {
906 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
907 }
908 if (!splits[1].equals("int")) {
909 throw new ParseException("The only supported type for gen-fields is int.");
910 }
911 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
912 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
913 recType.setUndeclaredFieldsDataGen(ufdg);
914 }
915 }
916
917 }
918 (
919 RecordField(recType)
920 ( "," RecordField(recType) )*
921 )?
922 "}"
923 {
924 if (recordKind == null) {
925 recordKind = RecordTypeDefinition.RecordKind.OPEN;
926 }
927 recType.setRecordKind(recordKind);
928 return recType;
929 }
930}
931
932void RecordField(RecordTypeDefinition recType) throws ParseException:
933{
934 String fieldName;
935 TypeExpression type = null;
936 boolean nullable = false;
937}
938{
Till Westmann14a20a72013-05-09 00:06:24 -0700939 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000940 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700941 fieldName = token.image;
942 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000943 IRecordFieldDataGen rfdg = null;
944 if (hint != null) {
945 String splits[] = hint.split(" +");
946 if (splits[0].equals(VAL_FILE_HINT)) {
947 File[] valFiles = new File[splits.length - 1];
948 for (int k=1; k<splits.length; k++) {
949 valFiles[k-1] = new File(splits[k]);
950 }
951 rfdg = new FieldValFileDataGen(valFiles);
952 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
953 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
954 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
955 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
956 } else if (splits[0].equals(LIST_HINT)) {
957 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
958 } else if (splits[0].equals(INTERVAL_HINT)) {
959 FieldIntervalDataGen.ValueType vt;
960 if (splits[1].equals("int")) {
961 vt = FieldIntervalDataGen.ValueType.INT;
962 } else if (splits[1].equals("long")) {
963 vt = FieldIntervalDataGen.ValueType.LONG;
964 } else if (splits[1].equals("float")) {
965 vt = FieldIntervalDataGen.ValueType.FLOAT;
966 } else if (splits[1].equals("double")) {
967 vt = FieldIntervalDataGen.ValueType.DOUBLE;
968 } else {
969 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
970 }
971 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
972 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
973 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
974 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
975 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
976 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
977 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
978 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
979 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
980 } else if (splits[0].equals(AUTO_HINT)) {
981 rfdg = new AutoDataGen(splits[1]);
982 }
983 }
984 }
985 ":"
986 ( type = TypeExpr() )
987 ("?" { nullable = true; } )?
988 {
989 recType.addField(fieldName, type, nullable, rfdg);
990 }
991}
992
993TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000994{
Till Westmann14a20a72013-05-09 00:06:24 -0700995 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700996}
997{
998 id = Identifier()
999 {
Till Westmann14a20a72013-05-09 00:06:24 -07001000 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -07001001 }
vinayakb38b7ca42012-03-05 05:44:15 +00001002}
1003
1004OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1005{
1006 TypeExpression type = null;
1007}
1008{
1009 "["
1010 ( type = TypeExpr() )
1011 "]"
1012 {
1013 return new OrderedListTypeDefinition(type);
1014 }
1015}
1016
1017
1018UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1019{
1020 TypeExpression type = null;
1021}
1022{
1023 "{{"
1024 ( type = TypeExpr() )
1025 "}}"
1026 {
1027 return new UnorderedListTypeDefinition(type);
1028 }
1029}
1030
Till Westmann31c21f92013-05-08 09:21:53 -07001031
1032Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001033{
Till Westmann31c21f92013-05-08 09:21:53 -07001034 Pair<Identifier,Identifier> name = null;
1035}
1036{
1037 name = QualifiedName()
1038 {
1039 if (name.first == null) {
1040 name.first = new Identifier(defaultDataverse);
1041 }
1042 return name;
1043 }
1044}
1045
Till Westmann14a20a72013-05-09 00:06:24 -07001046String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001047{
Till Westmann68d99652013-05-09 11:15:21 -07001048 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001049}
1050{
1051 <IDENTIFIER>
1052 {
Till Westmann14a20a72013-05-09 00:06:24 -07001053 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001054 }
Till Westmann68d99652013-05-09 11:15:21 -07001055 | lit = StringLiteral()
1056 {
1057 return lit;
1058 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001059}
1060
Till Westmann7d535322013-05-09 00:40:02 -07001061String StringLiteral() throws ParseException:
1062{
1063}
1064{
1065 <STRING_LITERAL>
1066 {
1067 return removeQuotesAndEscapes(token.image);
1068 }
1069}
1070
Till Westmann31c21f92013-05-08 09:21:53 -07001071Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1072{
Till Westmann14a20a72013-05-09 00:06:24 -07001073 String first = null;
1074 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001075}
1076{
Till Westmanna4242bc2013-05-08 17:49:55 -07001077 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001078 {
Till Westmann14a20a72013-05-09 00:06:24 -07001079 Identifier id1 = null;
1080 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001081 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001082 id2 = new Identifier(first);
1083 } else
1084 {
1085 id1 = new Identifier(first);
1086 id2 = new Identifier(second);
1087 }
1088 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001089 }
1090}
1091
Till Westmann31c21f92013-05-08 09:21:53 -07001092Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001093{
Till Westmann14a20a72013-05-09 00:06:24 -07001094 String first = null;
1095 String second = null;
1096 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001097}
1098{
Till Westmanna4242bc2013-05-08 17:49:55 -07001099 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001100 {
Till Westmann14a20a72013-05-09 00:06:24 -07001101 Identifier id1 = null;
1102 Identifier id2 = null;
1103 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001104 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001105 id2 = new Identifier(first);
1106 id3 = new Identifier(second);
1107 } else {
1108 id1 = new Identifier(first);
1109 id2 = new Identifier(second);
1110 id3 = new Identifier(third);
1111 }
1112 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001113 }
1114}
1115
vinayakb38b7ca42012-03-05 05:44:15 +00001116FunctionDecl FunctionDeclaration() throws ParseException:
1117{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001118 FunctionDecl funcDecl;
1119 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001120 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001121 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1122 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001123 createNewScope();
1124}
1125{
Till Westmannd7dcb122013-05-16 13:19:09 -07001126 "declare" "function" functionName = Identifier()
1127 paramList = ParameterList()
1128 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001129 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001130 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001131 getCurrentScope().addFunctionDescriptor(signature, false);
1132 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001133 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001134 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001135 }
1136}
1137
vinayakb38b7ca42012-03-05 05:44:15 +00001138
Till Westmann31c21f92013-05-08 09:21:53 -07001139Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001140{
1141 Query query = new Query();
1142 Expression expr;
1143}
1144{
Till Westmann31c21f92013-05-08 09:21:53 -07001145 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001146 {
1147 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001148 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001149 return query;
1150 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001151
vinayakb38b7ca42012-03-05 05:44:15 +00001152}
1153
1154
1155
1156Expression Expression():
1157{
1158 Expression expr = null;
1159 Expression exprP = null;
1160}
1161{
1162(
1163
1164//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1165 expr = OperatorExpr()
1166 | expr = IfThenElse()
1167 | expr = FLWOGR()
1168 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001169
vinayakb38b7ca42012-03-05 05:44:15 +00001170
1171)
1172 {
1173 return (exprP==null) ? expr : exprP;
1174 }
1175}
1176
1177
1178
1179Expression OperatorExpr()throws ParseException:
1180{
1181 OperatorExpr op = null;
1182 Expression operand = null;
1183}
1184{
1185 operand = AndExpr()
1186 (
1187
1188 "or"
1189 {
1190 if (op == null) {
1191 op = new OperatorExpr();
1192 op.addOperand(operand);
1193 op.setCurrentop(true);
1194 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001195 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001196 }
1197
1198 operand = AndExpr()
1199 {
1200 op.addOperand(operand);
1201 }
1202
1203 )*
1204
1205 {
1206 return op==null? operand: op;
1207 }
1208}
1209
1210Expression AndExpr()throws ParseException:
1211{
1212 OperatorExpr op = null;
1213 Expression operand = null;
1214}
1215{
1216 operand = RelExpr()
1217 (
1218
1219 "and"
1220 {
1221 if (op == null) {
1222 op = new OperatorExpr();
1223 op.addOperand(operand);
1224 op.setCurrentop(true);
1225 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001226 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001227 }
1228
1229 operand = RelExpr()
1230 {
1231 op.addOperand(operand);
1232 }
1233
1234 )*
1235
1236 {
1237 return op==null? operand: op;
1238 }
1239}
1240
1241
1242
1243Expression RelExpr()throws ParseException:
1244{
1245 OperatorExpr op = null;
1246 Expression operand = null;
1247 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001248 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001249}
1250{
1251 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001252 {
1253 if (operand instanceof VariableExpr) {
1254 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001255 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001256 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001257 }
1258 }
1259 }
1260
1261 (
1262 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1263 {
alexander.behm07617fd2012-07-25 10:13:50 +00001264 String mhint = getHint(token);
1265 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1266 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1267 }
vinayakb38b7ca42012-03-05 05:44:15 +00001268 if (op == null) {
1269 op = new OperatorExpr();
1270 op.addOperand(operand, broadcast);
1271 op.setCurrentop(true);
1272 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001273 }
1274 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001275 }
1276
1277 operand = AddExpr()
1278 {
alexander.behm07617fd2012-07-25 10:13:50 +00001279 broadcast = false;
1280 if (operand instanceof VariableExpr) {
1281 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001282 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1283 broadcast = true;
1284 }
alexander.behm07617fd2012-07-25 10:13:50 +00001285 }
vinayakb38b7ca42012-03-05 05:44:15 +00001286 op.addOperand(operand, broadcast);
1287 }
1288 )?
1289
1290 {
alexander.behm07617fd2012-07-25 10:13:50 +00001291 if (annotation != null) {
1292 op.addHint(annotation);
1293 }
vinayakb38b7ca42012-03-05 05:44:15 +00001294 return op==null? operand: op;
1295 }
1296}
1297
1298Expression AddExpr()throws ParseException:
1299{
1300 OperatorExpr op = null;
1301 Expression operand = null;
1302}
1303{
1304 operand = MultExpr()
1305
1306 ( ("+" | "-")
1307 {
1308 if (op == null) {
1309 op = new OperatorExpr();
1310 op.addOperand(operand);
1311 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001312 }
1313 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001314 }
1315
1316 operand = MultExpr()
1317 {
1318 op.addOperand(operand);
1319 }
1320 )*
1321
1322 {
1323 return op==null? operand: op;
1324 }
1325}
1326
1327Expression MultExpr()throws ParseException:
1328{
1329 OperatorExpr op = null;
1330 Expression operand = null;
1331}
1332{
1333 operand = UnionExpr()
1334
1335 (( "*" | "/" | "%" | <CARET> | "idiv")
1336 {
1337 if (op == null) {
1338 op = new OperatorExpr();
1339 op.addOperand(operand);
1340 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001341 }
1342 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001343 }
1344 operand = UnionExpr()
1345 {
1346 op.addOperand(operand);
1347 }
1348 )*
1349
1350 {
1351 return op==null?operand:op;
1352 }
1353}
1354
1355Expression UnionExpr() throws ParseException:
1356{
1357 UnionExpr union = null;
1358 Expression operand1 = null;
1359 Expression operand2 = null;
1360}
1361{
1362 operand1 = UnaryExpr()
1363 ("union"
1364 (operand2 = UnaryExpr()) {
1365 if (union == null) {
1366 union = new UnionExpr();
1367 union.addExpr(operand1);
1368 }
1369 union.addExpr(operand2);
1370 } )*
1371 {
1372 return (union == null)? operand1: union;
1373 }
1374}
1375
1376Expression UnaryExpr() throws ParseException:
1377{
1378 Expression uexpr = null;
1379 Expression expr = null;
1380}
1381{
1382 (( "+"|"-")
1383 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001384 uexpr = new UnaryExpr();
1385 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001386 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001387 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001388 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1389 else
1390 throw new ParseException();
1391 }
1392 )?
1393
1394 expr = ValueExpr()
1395 {
1396 if(uexpr!=null){
1397 ((UnaryExpr)uexpr).setExpr(expr);
1398 return uexpr;
1399 }
1400 else{
1401 return expr;
1402 }
1403 }
1404}
1405
Till Westmann04478e72013-05-13 23:01:34 -07001406Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001407{
1408 Expression expr = null;
1409 Identifier ident = null;
1410 AbstractAccessor fa = null;
1411 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001412}
1413{
Till Westmann04478e72013-05-13 23:01:34 -07001414 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001415 {
Till Westmann04478e72013-05-13 23:01:34 -07001416 fa = (fa == null ? new FieldAccessor(expr, ident)
1417 : new FieldAccessor(fa, ident));
1418 }
1419 | index = Index()
1420 {
1421 fa = (fa == null ? new IndexAccessor(expr, index)
1422 : new IndexAccessor(fa, index));
1423 }
1424 )*
1425 {
1426 return fa == null ? expr : fa;
1427 }
vinayakb38b7ca42012-03-05 05:44:15 +00001428}
1429
1430Identifier Field() throws ParseException:
1431{
Till Westmann14a20a72013-05-09 00:06:24 -07001432 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001433}
1434{
Till Westmanna4242bc2013-05-08 17:49:55 -07001435 "." ident = Identifier()
1436 {
Till Westmann14a20a72013-05-09 00:06:24 -07001437 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001438 }
vinayakb38b7ca42012-03-05 05:44:15 +00001439}
1440
1441int Index() throws ParseException:
1442{
1443 Expression expr = null;
1444 int idx = -2;
1445}
1446{
1447 "[" ( expr = Expression()
1448 {
1449 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1450 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001451 Literal lit = ((LiteralExpr)expr).getValue();
1452 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1453 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001454 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001455 }
vinayakb38b7ca42012-03-05 05:44:15 +00001456 else {
1457 throw new ParseException("Index should be an INTEGER");
1458 }
1459 }
1460
1461 }
1462
1463 | "?"
1464 {
1465 idx = IndexAccessor.ANY;
1466 // ANY
1467 }
1468
1469 )
1470
1471 "]"
1472 {
1473 return idx;
1474 }
1475}
1476
1477
1478Expression PrimaryExpr()throws ParseException:
1479{
1480 Expression expr = null;
1481}
1482{
Till Westmann68d99652013-05-09 11:15:21 -07001483 ( LOOKAHEAD(2)
1484 expr = FunctionCallExpr()
1485 | expr = Literal()
1486 | expr = DatasetAccessExpression()
1487 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001488 {
1489 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001490 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001491 }
Till Westmann68d99652013-05-09 11:15:21 -07001492 | expr = ListConstructor()
1493 | expr = RecordConstructor()
1494 | expr = ParenthesizedExpression()
1495 )
1496 {
1497 return expr;
1498 }
vinayakb38b7ca42012-03-05 05:44:15 +00001499}
1500
1501Expression Literal() throws ParseException:
1502{
vinayakb38b7ca42012-03-05 05:44:15 +00001503 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001504 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001505}
1506{
Till Westmann7d535322013-05-09 00:40:02 -07001507 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001508 {
Till Westmann7d535322013-05-09 00:40:02 -07001509 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001510 }
1511 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001512 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001513 try {
1514 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1515 } catch(NumberFormatException ex) {
1516 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1517 }
1518 }
1519 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001520 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001521 lit.setValue(new FloatLiteral(new Float(token.image)));
1522 }
1523 | <DOUBLE_LITERAL>
1524 {
1525 lit.setValue(new DoubleLiteral(new Double(token.image)));
1526 }
1527 | <NULL>
1528 {
1529 lit.setValue(NullLiteral.INSTANCE);
1530 }
1531 | <TRUE>
1532 {
1533 lit.setValue(TrueLiteral.INSTANCE);
1534 }
1535 | <FALSE>
1536 {
1537 lit.setValue(FalseLiteral.INSTANCE);
1538 }
1539 )
vinayakb38b7ca42012-03-05 05:44:15 +00001540 {
1541 return lit;
1542 }
1543}
1544
1545
1546VariableExpr VariableRef() throws ParseException:
1547{
1548 VariableExpr varExp = new VariableExpr();
1549 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001550}
1551{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001552 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001553 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001554 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001555 Identifier ident = lookupSymbol(varName);
1556 if (isInForbiddenScopes(varName)) {
1557 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.");
1558 }
1559 if(ident != null) { // exist such ident
1560 varExp.setIsNewVar(false);
1561 varExp.setVar((VarIdentifier)ident);
1562 } else {
1563 varExp.setVar(var);
1564 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001565 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001566 return varExp;
1567 }
1568}
1569
1570
1571VariableExpr Variable() throws ParseException:
1572{
1573 VariableExpr varExp = new VariableExpr();
1574 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001575}
1576{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001577 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001578 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001579 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001580 if(ident != null) { // exist such ident
1581 varExp.setIsNewVar(false);
1582 }
1583 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001584 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001585 return varExp;
1586 }
1587}
1588
1589Expression ListConstructor() throws ParseException:
1590{
1591 Expression expr = null;
1592}
1593{
1594 (
1595 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1596 )
1597
1598 {
1599 return expr;
1600 }
1601}
1602
1603
1604ListConstructor OrderedListConstructor() throws ParseException:
1605{
1606 ListConstructor expr = new ListConstructor();
1607 Expression tmp = null;
1608 List<Expression> exprList = new ArrayList<Expression>();
1609 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1610}
1611{
1612
1613 "["
1614 ( tmp = Expression()
1615 {
1616 exprList.add(tmp);
1617 }
1618
1619 ("," tmp = Expression() { exprList.add(tmp); })*
1620 )?
1621
1622 "]"
1623
1624 {
1625 expr.setExprList(exprList);
1626 return expr;
1627 }
1628}
1629
1630ListConstructor UnorderedListConstructor() throws ParseException:
1631{
1632 ListConstructor expr = new ListConstructor();
1633 Expression tmp = null;
1634 List<Expression> exprList = new ArrayList<Expression>();
1635 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1636}
1637{
1638
1639 "{{" ( tmp = Expression()
1640 {
1641 exprList.add(tmp);
1642 }
1643 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1644 {
1645 expr.setExprList(exprList);
1646 return expr;
1647 }
1648}
1649
1650RecordConstructor RecordConstructor() throws ParseException:
1651{
1652 RecordConstructor expr = new RecordConstructor();
1653 FieldBinding tmp = null;
1654 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1655}
1656{
1657 "{" (tmp = FieldBinding()
1658 {
1659 fbList.add(tmp);
1660 }
1661 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1662 {
1663 expr.setFbList(fbList);
1664 return expr;
1665 }
1666}
1667
1668FieldBinding FieldBinding() throws ParseException:
1669{
1670 FieldBinding fb = new FieldBinding();
1671 Expression left, right;
1672}
1673{
1674 left = Expression() ":" right = Expression()
1675 {
1676 fb.setLeftExpr(left);
1677 fb.setRightExpr(right);
1678 return fb;
1679 }
1680}
1681
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001682
vinayakb38b7ca42012-03-05 05:44:15 +00001683Expression FunctionCallExpr() throws ParseException:
1684{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001685 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001686 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001687 Expression tmp;
1688 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001689 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001690 String funcName;
1691 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001692 String hint = null;
1693 String id1 = null;
1694 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001695}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001696{
Till Westmann31c21f92013-05-08 09:21:53 -07001697 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001698 {
Till Westmann31c21f92013-05-08 09:21:53 -07001699 dataverse = funcId.first.getValue();
1700 funcName = funcId.second.getValue();
1701 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001702 }
Till Westmann31c21f92013-05-08 09:21:53 -07001703 <LEFTPAREN> (tmp = Expression()
1704 {
1705 argList.add(tmp);
1706 arity ++;
1707 }
1708 ("," tmp = Expression()
1709 {
1710 argList.add(tmp);
1711 arity++;
1712 }
1713 )*)? <RIGHTPAREN>
1714 {
1715 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1716 if (signature == null) {
1717 signature = new FunctionSignature(dataverse, funcName, arity);
1718 }
1719 callExpr = new CallExpr(signature,argList);
1720 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1721 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1722 }
1723 return callExpr;
1724 }
vinayakb38b7ca42012-03-05 05:44:15 +00001725}
1726
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001727Expression DatasetAccessExpression() throws ParseException:
1728{
1729 CallExpr callExpr;
1730 List<Expression> argList = new ArrayList<Expression>();
1731 String funcName;
1732 String dataverse;
Till Westmann14a20a72013-05-09 00:06:24 -07001733 String arg1 = null;
1734 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001735 LiteralExpr ds;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001736 Expression nameArg;
1737 int arity = 0;
1738}
1739{
Till Westmann14a20a72013-05-09 00:06:24 -07001740 <DATASET>
1741 {
1742 dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1743 funcName = token.image;
1744 }
1745 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1746 {
1747 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
1748 ds = new LiteralExpr();
1749 ds.setValue( new StringLiteral(name) );
1750 argList.add(ds);
1751 arity ++;
1752 }
1753 | ( <LEFTPAREN> nameArg = Expression()
1754 {
1755 argList.add(nameArg);
1756 arity ++;
1757 }
1758 ( "," nameArg = Expression()
1759 {
1760 argList.add(nameArg);
1761 arity++;
1762 }
1763 )* <RIGHTPAREN> ) )
1764 {
1765 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1766 if (signature == null) {
1767 signature = new FunctionSignature(dataverse, funcName, arity);
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001768 }
Till Westmann14a20a72013-05-09 00:06:24 -07001769 callExpr = new CallExpr(signature,argList);
1770 return callExpr;
1771 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001772}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001773
vinayakb38b7ca42012-03-05 05:44:15 +00001774Expression ParenthesizedExpression() throws ParseException:
1775{
1776 Expression expr;
1777}
1778{
1779 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1780 {
1781 return expr;
1782 }
1783}
1784
1785Expression IfThenElse() throws ParseException:
1786{
1787 Expression condExpr;
1788 Expression thenExpr;
1789 Expression elseExpr;
1790 IfExpr ifExpr = new IfExpr();
1791}
1792{
1793 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1794
1795 {
1796 ifExpr.setCondExpr(condExpr);
1797 ifExpr.setThenExpr(thenExpr);
1798 ifExpr.setElseExpr(elseExpr);
1799 return ifExpr;
1800 }
1801}
1802
1803Expression FLWOGR() throws ParseException:
1804{
1805 FLWOGRExpression flworg = new FLWOGRExpression();
1806 List<Clause> clauseList = new ArrayList<Clause>();
1807 Expression returnExpr;
1808 Clause tmp;
1809 createNewScope();
1810}
1811{
1812 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1813 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1814
1815 {
1816 flworg.setClauseList(clauseList);
1817 flworg.setReturnExpr(returnExpr);
1818 removeCurrentScope();
1819 return flworg;
1820 }
1821}
1822
1823Clause Clause()throws ParseException :
1824{
1825 Clause clause;
1826}
1827{
1828 (
1829 clause = ForClause()
1830 | clause = LetClause()
1831 | clause = WhereClause()
1832 | clause = OrderbyClause()
1833 | clause = GroupClause()
1834 | clause = LimitClause()
1835 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001836 )
1837 {
1838 return clause;
1839 }
1840}
1841
1842Clause ForClause()throws ParseException :
1843{
1844 ForClause fc = new ForClause();
1845 VariableExpr varExp;
1846 VariableExpr varPos = null;
1847 Expression inExp;
1848 extendCurrentScope();
1849}
1850{
1851 "for" varExp = Variable()
1852 {
1853 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1854 }
1855 ("at" varPos = Variable()
1856 {
1857 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1858 }
1859 )?
1860 "in" ( inExp = Expression() )
1861 {
1862 fc.setVarExpr(varExp);
1863 fc.setInExpr(inExp);
1864 if (varPos != null) {
1865 fc.setPosExpr(varPos);
1866 }
1867 return fc;
1868 }
1869}
1870
1871Clause LetClause() throws ParseException:
1872{
1873 LetClause lc = new LetClause();
1874 VariableExpr varExp;
1875 Expression beExp;
1876 extendCurrentScope();
1877}
1878{
ilovesoupb2527c12012-07-12 03:21:13 +00001879 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001880 {
1881 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001882 lc.setVarExpr(varExp);
1883 lc.setBeExpr(beExp);
1884 return lc;
1885 }
1886}
1887
1888Clause WhereClause()throws ParseException :
1889{
1890 WhereClause wc = new WhereClause();
1891 Expression whereExpr;
1892}
1893{
1894 "where" whereExpr = Expression()
1895 {
1896 wc.setWhereExpr(whereExpr);
1897 return wc;
1898 }
1899}
1900
1901Clause OrderbyClause()throws ParseException :
1902{
1903 OrderbyClause oc = new OrderbyClause();
1904 Expression orderbyExpr;
1905 List<Expression> orderbyList = new ArrayList<Expression>();
1906 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1907 int numOfOrderby = 0;
1908}
1909{
1910 (
1911 "order"
1912 {
1913 String hint = getHint(token);
1914 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1915 String splits[] = hint.split(" +");
1916 int numFrames = Integer.parseInt(splits[1]);
1917 int numTuples = Integer.parseInt(splits[2]);
1918 oc.setNumFrames(numFrames);
1919 oc.setNumTuples(numTuples);
1920 }
1921 }
1922 "by" orderbyExpr = Expression()
1923 {
1924 orderbyList.add(orderbyExpr);
1925 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1926 }
1927 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1928 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1929 {
1930 modifierList.add(modif);
1931 }
1932
1933 ("," orderbyExpr = Expression()
1934 {
1935 orderbyList.add(orderbyExpr);
1936 modif = OrderbyClause.OrderModifier.ASC;
1937 }
1938 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1939 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1940 {
1941 modifierList.add(modif);
1942 }
1943 )*
1944)
1945 {
1946 oc.setModifierList(modifierList);
1947 oc.setOrderbyList(orderbyList);
1948 return oc;
1949 }
1950}
1951Clause GroupClause()throws ParseException :
1952{
1953 GroupbyClause gbc = new GroupbyClause();
1954 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1955 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1956 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1957 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1958 VariableExpr var = null;
1959 VariableExpr withVar = null;
1960 Expression expr = null;
1961 VariableExpr decorVar = null;
1962 Expression decorExpr = null;
1963}
1964{
1965 {
1966 Scope newScope = extendCurrentScopeNoPush(true);
1967 // extendCurrentScope(true);
1968 }
1969 "group"
1970 {
1971 String hint = getHint(token);
1972 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1973 gbc.setHashGroupByHint(true);
1974 }
1975 }
1976 "by" (LOOKAHEAD(2) var = Variable()
1977 {
1978 newScope.addNewVarSymbolToScope(var.getVar());
1979 } ":=")?
1980 expr = Expression()
1981 {
1982 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1983 vePairList.add(pair1);
1984 }
1985 ("," ( LOOKAHEAD(2) var = Variable()
1986 {
1987 newScope.addNewVarSymbolToScope(var.getVar());
1988 } ":=")?
1989 expr = Expression()
1990 {
1991 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1992 vePairList.add(pair2);
1993 }
1994 )*
1995 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1996 {
1997 newScope.addNewVarSymbolToScope(decorVar.getVar());
1998 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1999 decorPairList.add(pair3);
2000 }
2001 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2002 {
2003 newScope.addNewVarSymbolToScope(decorVar.getVar());
2004 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2005 decorPairList.add(pair4);
2006 }
2007 )*
2008 )?
2009 "with" withVar = VariableRef()
2010 {
2011 if(withVar.getIsNewVar()==true)
2012 throw new ParseException("can't find variable " + withVar.getVar());
2013 withVarList.add(withVar);
2014 newScope.addNewVarSymbolToScope(withVar.getVar());
2015 }
2016 ("," withVar = VariableRef()
2017 {
2018 if(withVar.getIsNewVar()==true)
2019 throw new ParseException("can't find variable " + withVar.getVar());
2020 withVarList.add(withVar);
2021 newScope.addNewVarSymbolToScope(withVar.getVar());
2022 })*
2023 {
2024 gbc.setGbyPairList(vePairList);
2025 gbc.setDecorPairList(decorPairList);
2026 gbc.setWithVarList(withVarList);
2027 replaceCurrentScope(newScope);
2028 return gbc;
2029 }
2030}
2031
2032
2033LimitClause LimitClause() throws ParseException:
2034{
2035 LimitClause lc = new LimitClause();
2036 Expression expr;
2037 pushForbiddenScope(getCurrentScope());
2038}
2039{
2040 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2041 ("offset" expr = Expression() { lc.setOffset(expr); })?
2042
2043 {
2044 popForbiddenScope();
2045 return lc;
2046 }
2047}
2048
2049DistinctClause DistinctClause() throws ParseException:
2050{
2051 List<Expression> exprs = new ArrayList<Expression>();
2052 Expression expr;
2053}
2054{
2055 "distinct" "by" expr = Expression()
2056 {
2057 exprs.add(expr);
2058 }
2059 ("," expr = Expression()
2060 {
2061 exprs.add(expr);
2062 }
2063 )*
2064 {
2065 return new DistinctClause(exprs);
2066 }
2067}
2068
vinayakb38b7ca42012-03-05 05:44:15 +00002069QuantifiedExpression QuantifiedExpression()throws ParseException:
2070{
2071 QuantifiedExpression qc = new QuantifiedExpression();
2072 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2073 Expression satisfiesExpr;
2074 VariableExpr var;
2075 Expression inExpr;
2076 QuantifiedPair pair;
2077}
2078{
2079 {
2080 createNewScope();
2081 }
2082
2083 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2084 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2085 var = Variable() "in" inExpr = Expression()
2086 {
2087 pair = new QuantifiedPair(var, inExpr);
2088 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2089 quantifiedList.add(pair);
2090 }
2091 (
2092 "," var = Variable() "in" inExpr = Expression()
2093 {
2094 pair = new QuantifiedPair(var, inExpr);
2095 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2096 quantifiedList.add(pair);
2097 }
2098 )*
2099 "satisfies" satisfiesExpr = Expression()
2100 {
2101 qc.setSatisfiesExpr(satisfiesExpr);
2102 qc.setQuantifiedList(quantifiedList);
2103 removeCurrentScope();
2104 return qc;
2105 }
2106}
2107
2108TOKEN_MGR_DECLS:
2109{
2110 public int commentDepth = 0;
2111}
2112
2113<DEFAULT>
2114TOKEN :
2115{
2116 <CARET : "^" >
2117}
2118
2119<DEFAULT>
2120TOKEN :
2121{
2122 <DATASET : "dataset" >
2123}
2124
2125<DEFAULT>
2126TOKEN :
2127{
2128 <LEFTPAREN : "(" >
2129}
2130
2131<DEFAULT>
2132TOKEN :
2133{
2134 <RIGHTPAREN : ")" >
2135}
2136
2137
2138<DEFAULT>
2139TOKEN :
2140{
2141 <INTEGER_LITERAL : (<DIGIT>)+ >
2142}
2143
2144
2145<DEFAULT>
2146TOKEN :
2147{
2148 <NULL : "null">
2149}
2150
2151<DEFAULT>
2152TOKEN :
2153{
2154 <TRUE : "true">
2155}
2156
2157<DEFAULT>
2158TOKEN :
2159{
2160 <FALSE : "false">
2161}
2162
2163<DEFAULT>
2164TOKEN :
2165{
2166 <#DIGIT : ["0" - "9"]>
2167}
2168
2169
2170TOKEN:
2171{
2172 < DOUBLE_LITERAL: <INTEGER>
2173 | <INTEGER> ( "." <INTEGER> )?
2174 | "." <INTEGER>
2175 >
2176 |
2177 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2178 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2179 | "." <INTEGER> ( "f" | "F" )
2180 >
2181 |
2182 <INTEGER : (<DIGIT>)+ >
2183}
2184
2185<DEFAULT>
2186TOKEN :
2187{
2188 <#LETTER : ["A" - "Z", "a" - "z"]>
2189}
2190
2191<DEFAULT>
2192TOKEN :
2193{
2194 <SPECIALCHARS : ["$", "_", "-"] >
2195}
2196
2197<DEFAULT>
2198TOKEN :
2199{
2200 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2201 |
2202 < #EscapeQuot: "\\\"" >
2203 |
2204 < #EscapeApos: "\\\'" >
2205}
2206
2207<DEFAULT>
2208TOKEN :
2209{
2210 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2211}
2212
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002213
vinayakb38b7ca42012-03-05 05:44:15 +00002214<DEFAULT>
2215TOKEN :
2216{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002217 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002218}
2219
2220SKIP:
2221{
2222 " "
2223| "\t"
2224| "\r"
2225| "\n"
2226}
2227
2228SKIP:
2229{
2230 <"//" (~["\n"])* "\n">
2231}
2232
2233SKIP:
2234{
2235 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2236}
2237
2238
2239SKIP:
2240{
2241 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2242}
2243
2244<INSIDE_COMMENT>
2245SPECIAL_TOKEN:
2246{
2247 <"+"(" ")*(~["*"])*>
2248}
2249
2250<INSIDE_COMMENT>
2251SKIP:
2252{
2253 <"/*"> {commentDepth++;}
2254}
2255
2256<INSIDE_COMMENT>
2257SKIP:
2258{
2259 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2260| <~[]>
2261}