blob: 2a5f5340f9d197442b1f5f6e7e0000365b270be2 [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
501FunctionSignature FunctionSignature() throws ParseException:
502{
503 Pair<Identifier,Identifier> pairId = null;
504 int arity = 0;
505}
506{
507 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
508 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700509 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700510 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
511 throw new ParseException(" invalid arity:" + arity);
512 }
513
514 String dataverse = pairId.first.getValue();
515 String functionName = pairId.second.getValue();
516 return new FunctionSignature(dataverse, functionName, arity);
517 }
518}
519
520List<String> PrimaryKey() throws ParseException:
521{
Till Westmann14a20a72013-05-09 00:06:24 -0700522 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700523 List<String> primaryKeyFields = new ArrayList<String>();
524}
525{
Till Westmann14a20a72013-05-09 00:06:24 -0700526 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700527 {
Till Westmann14a20a72013-05-09 00:06:24 -0700528 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700529 }
Till Westmann14a20a72013-05-09 00:06:24 -0700530 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700531 {
Till Westmann14a20a72013-05-09 00:06:24 -0700532 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700533 }
534 )*
535 {
536 return primaryKeyFields;
537 }
538}
539
540Statement DropStatement() throws ParseException:
541{
Till Westmann14a20a72013-05-09 00:06:24 -0700542 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700543 Pair<Identifier,Identifier> pairId = null;
544 Triple<Identifier,Identifier,Identifier> tripleId = null;
545 FunctionSignature funcSig = null;
546 boolean ifExists = false;
547 Statement stmt = null;
548}
549{
550 "drop"
551 (
552 <DATASET> pairId = QualifiedName() ifExists = IfExists()
553 {
554 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
555 }
556 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
557 {
558 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
559 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700560 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700561 {
Till Westmann14a20a72013-05-09 00:06:24 -0700562 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700563 }
564 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
565 {
566 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
567 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700568 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700569 {
Till Westmann14a20a72013-05-09 00:06:24 -0700570 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700571 }
572 | "function" funcSig = FunctionSignature() ifExists = IfExists()
573 {
574 stmt = new FunctionDropStatement(funcSig, ifExists);
575 }
576 )
577 {
578 return stmt;
579 }
580}
581
582boolean IfExists() throws ParseException :
583{
584}
585{
586 ( "if" "exists"
587 {
588 return true;
589 }
590 )?
591 {
592 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000593 }
594}
595
596InsertStatement InsertStatement() throws ParseException:
597{
Till Westmann31c21f92013-05-08 09:21:53 -0700598 Pair<Identifier,Identifier> nameComponents = null;
599 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000600}
601{
Till Westmann31c21f92013-05-08 09:21:53 -0700602 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
603 {
604 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
605 }
vinayakb38b7ca42012-03-05 05:44:15 +0000606}
607
608DeleteStatement DeleteStatement() throws ParseException:
609{
Till Westmann31c21f92013-05-08 09:21:53 -0700610 VariableExpr var = null;
611 Expression condition = null;
612 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000613}
614{
Till Westmann31c21f92013-05-08 09:21:53 -0700615 "delete" var = Variable()
616 {
617 getCurrentScope().addNewVarSymbolToScope(var.getVar());
618 }
619 "from" <DATASET> nameComponents = QualifiedName()
620 ("where" condition = Expression())?
621 {
622 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
623 }
vinayakb38b7ca42012-03-05 05:44:15 +0000624}
625
626UpdateStatement UpdateStatement() throws ParseException:
627{
Till Westmann31c21f92013-05-08 09:21:53 -0700628 VariableExpr vars;
629 Expression target;
630 Expression condition;
631 UpdateClause uc;
632 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000633}
634{
Till Westmann31c21f92013-05-08 09:21:53 -0700635 "update" vars = Variable() "in" target = Expression()
636 "where" condition = Expression()
637 <LEFTPAREN> (uc = UpdateClause()
638 {
639 ucs.add(uc);
640 }
641 ("," uc = UpdateClause()
642 {
643 ucs.add(uc);
644 }
645 )*) <RIGHTPAREN>
646 {
647 return new UpdateStatement(vars, target, condition, ucs);
648 }
vinayakb38b7ca42012-03-05 05:44:15 +0000649}
650
vinayakb38b7ca42012-03-05 05:44:15 +0000651UpdateClause UpdateClause() throws ParseException:
652{
Till Westmann31c21f92013-05-08 09:21:53 -0700653 Expression target = null;
654 Expression value = null ;
655 InsertStatement is = null;
656 DeleteStatement ds = null;
657 UpdateStatement us = null;
658 Expression condition = null;
659 UpdateClause ifbranch = null;
660 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000661}
662{
663 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700664 | is = InsertStatement()
665 | ds = DeleteStatement()
666 | us = UpdateStatement()
667 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
668 "then" ifbranch = UpdateClause()
669 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
670 {
671 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
672 }
vinayakb38b7ca42012-03-05 05:44:15 +0000673}
674
vinayakb38b7ca42012-03-05 05:44:15 +0000675Statement SetStatement() throws ParseException:
676{
677 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700678 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000679}
680{
Till Westmann7d535322013-05-09 00:40:02 -0700681 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700682 {
Till Westmann31c21f92013-05-08 09:21:53 -0700683 return new SetStatement(pn, pv);
684 }
vinayakb38b7ca42012-03-05 05:44:15 +0000685}
686
687Statement WriteStatement() throws ParseException:
688{
Till Westmann14a20a72013-05-09 00:06:24 -0700689 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000690 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000691 Statement stmt = null;
692 Query query;
693 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000694 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000695}
696{
Till Westmann31c21f92013-05-08 09:21:53 -0700697 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700698 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
699 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700700 {
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 Westmann68d99652013-05-09 11:15:21 -0700750 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000751 {
Till Westmann7d535322013-05-09 00:40:02 -0700752 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000753 }
vinayakb38b7ca42012-03-05 05:44:15 +0000754}
755
Till Westmann31c21f92013-05-08 09:21:53 -0700756Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000757{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000758 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700759 Map<String,String> configuration = null;
760 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000761}
762{
Till Westmann31c21f92013-05-08 09:21:53 -0700763 (
764 "begin" "feed" nameComponents = QualifiedName()
765 {
766 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
767 }
768 | "suspend" "feed" nameComponents = QualifiedName()
769 {
770 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
771 }
772 | "resume" "feed" nameComponents = QualifiedName()
773 {
774 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
775 }
776 | "end" "feed" nameComponents = QualifiedName()
777 {
778 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
779 }
780 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
781 {
782 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
783 }
784 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000785 {
Till Westmann31c21f92013-05-08 09:21:53 -0700786 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000787 }
788}
789
Till Westmann31c21f92013-05-08 09:21:53 -0700790Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000791{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000792 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700793 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000794}
795{
Till Westmann31c21f92013-05-08 09:21:53 -0700796 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000797 {
Till Westmann31c21f92013-05-08 09:21:53 -0700798 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000799 }
Till Westmann31c21f92013-05-08 09:21:53 -0700800 ( "," 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 )* )? <RIGHTPAREN>
805 {
806 return configuration;
807 }
808}
809
810Pair<String, String> KeyValuePair() throws ParseException:
811{
812 String key;
813 String value;
814}
815{
Till Westmann7d535322013-05-09 00:40:02 -0700816 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700817 {
818 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000819 }
Till Westmann31c21f92013-05-08 09:21:53 -0700820}
821
822Map<String,String> Properties() throws ParseException:
823{
824 Map<String,String> properties = new HashMap<String,String>();
825 Pair<String, String> property;
826}
827{
828 ( <LEFTPAREN> property = Property()
829 {
830 properties.put(property.first, property.second);
831 }
832 ( "," property = Property()
833 {
834 properties.put(property.first, property.second);
835 }
836 )* <RIGHTPAREN> )?
837 {
838 return properties;
839 }
840}
841
842Pair<String, String> Property() throws ParseException:
843{
844 String key;
845 String value;
846}
847{
Till Westmann7d535322013-05-09 00:40:02 -0700848 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700849 {
850 try {
851 value = "" + Long.valueOf(token.image);
852 } catch (NumberFormatException nfe) {
853 throw new ParseException("inapproriate value: " + token.image);
854 }
855 }
856 )
857 {
858 return new Pair<String, String>(key.toUpperCase(), value);
859 }
vinayakb38b7ca42012-03-05 05:44:15 +0000860}
861
862TypeExpression TypeExpr() throws ParseException:
863{
864 TypeExpression typeExpr = null;
865}
866{
867 (
868 typeExpr = RecordTypeDef()
869 | typeExpr = TypeReference()
870 | typeExpr = OrderedListTypeDef()
871 | typeExpr = UnorderedListTypeDef()
872 )
873 {
874 return typeExpr;
875 }
876}
877
878RecordTypeDefinition RecordTypeDef() throws ParseException:
879{
880 RecordTypeDefinition recType = new RecordTypeDefinition();
881 RecordTypeDefinition.RecordKind recordKind = null;
882}
883{
884 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
885 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
886 "{"
887 {
888 String hint = getHint(token);
889 if (hint != null) {
890 String splits[] = hint.split(" +");
891 if (splits[0].equals(GEN_FIELDS_HINT)) {
892 if (splits.length != 5) {
893 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
894 }
895 if (!splits[1].equals("int")) {
896 throw new ParseException("The only supported type for gen-fields is int.");
897 }
898 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
899 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
900 recType.setUndeclaredFieldsDataGen(ufdg);
901 }
902 }
903
904 }
905 (
906 RecordField(recType)
907 ( "," RecordField(recType) )*
908 )?
909 "}"
910 {
911 if (recordKind == null) {
912 recordKind = RecordTypeDefinition.RecordKind.OPEN;
913 }
914 recType.setRecordKind(recordKind);
915 return recType;
916 }
917}
918
919void RecordField(RecordTypeDefinition recType) throws ParseException:
920{
921 String fieldName;
922 TypeExpression type = null;
923 boolean nullable = false;
924}
925{
Till Westmann14a20a72013-05-09 00:06:24 -0700926 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000927 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700928 fieldName = token.image;
929 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000930 IRecordFieldDataGen rfdg = null;
931 if (hint != null) {
932 String splits[] = hint.split(" +");
933 if (splits[0].equals(VAL_FILE_HINT)) {
934 File[] valFiles = new File[splits.length - 1];
935 for (int k=1; k<splits.length; k++) {
936 valFiles[k-1] = new File(splits[k]);
937 }
938 rfdg = new FieldValFileDataGen(valFiles);
939 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
940 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
941 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
942 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
943 } else if (splits[0].equals(LIST_HINT)) {
944 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
945 } else if (splits[0].equals(INTERVAL_HINT)) {
946 FieldIntervalDataGen.ValueType vt;
947 if (splits[1].equals("int")) {
948 vt = FieldIntervalDataGen.ValueType.INT;
949 } else if (splits[1].equals("long")) {
950 vt = FieldIntervalDataGen.ValueType.LONG;
951 } else if (splits[1].equals("float")) {
952 vt = FieldIntervalDataGen.ValueType.FLOAT;
953 } else if (splits[1].equals("double")) {
954 vt = FieldIntervalDataGen.ValueType.DOUBLE;
955 } else {
956 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
957 }
958 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
959 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
960 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
961 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
962 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
963 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
964 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
965 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
966 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
967 } else if (splits[0].equals(AUTO_HINT)) {
968 rfdg = new AutoDataGen(splits[1]);
969 }
970 }
971 }
972 ":"
973 ( type = TypeExpr() )
974 ("?" { nullable = true; } )?
975 {
976 recType.addField(fieldName, type, nullable, rfdg);
977 }
978}
979
980TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000981{
Till Westmann14a20a72013-05-09 00:06:24 -0700982 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700983}
984{
985 id = Identifier()
986 {
Till Westmann14a20a72013-05-09 00:06:24 -0700987 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700988 }
vinayakb38b7ca42012-03-05 05:44:15 +0000989}
990
991OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
992{
993 TypeExpression type = null;
994}
995{
996 "["
997 ( type = TypeExpr() )
998 "]"
999 {
1000 return new OrderedListTypeDefinition(type);
1001 }
1002}
1003
1004
1005UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1006{
1007 TypeExpression type = null;
1008}
1009{
1010 "{{"
1011 ( type = TypeExpr() )
1012 "}}"
1013 {
1014 return new UnorderedListTypeDefinition(type);
1015 }
1016}
1017
Till Westmann31c21f92013-05-08 09:21:53 -07001018
1019Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001020{
Till Westmann31c21f92013-05-08 09:21:53 -07001021 Pair<Identifier,Identifier> name = null;
1022}
1023{
1024 name = QualifiedName()
1025 {
1026 if (name.first == null) {
1027 name.first = new Identifier(defaultDataverse);
1028 }
1029 return name;
1030 }
1031}
1032
Till Westmann14a20a72013-05-09 00:06:24 -07001033String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001034{
Till Westmann68d99652013-05-09 11:15:21 -07001035 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001036}
1037{
1038 <IDENTIFIER>
1039 {
Till Westmann14a20a72013-05-09 00:06:24 -07001040 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001041 }
Till Westmann68d99652013-05-09 11:15:21 -07001042 | lit = StringLiteral()
1043 {
1044 return lit;
1045 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001046}
1047
Till Westmann7d535322013-05-09 00:40:02 -07001048String StringLiteral() throws ParseException:
1049{
1050}
1051{
1052 <STRING_LITERAL>
1053 {
1054 return removeQuotesAndEscapes(token.image);
1055 }
1056}
1057
Till Westmann31c21f92013-05-08 09:21:53 -07001058Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1059{
Till Westmann14a20a72013-05-09 00:06:24 -07001060 String first = null;
1061 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001062}
1063{
Till Westmanna4242bc2013-05-08 17:49:55 -07001064 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001065 {
Till Westmann14a20a72013-05-09 00:06:24 -07001066 Identifier id1 = null;
1067 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001068 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001069 id2 = new Identifier(first);
1070 } else
1071 {
1072 id1 = new Identifier(first);
1073 id2 = new Identifier(second);
1074 }
1075 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001076 }
1077}
1078
Till Westmann31c21f92013-05-08 09:21:53 -07001079Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001080{
Till Westmann14a20a72013-05-09 00:06:24 -07001081 String first = null;
1082 String second = null;
1083 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001084}
1085{
Till Westmanna4242bc2013-05-08 17:49:55 -07001086 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001087 {
Till Westmann14a20a72013-05-09 00:06:24 -07001088 Identifier id1 = null;
1089 Identifier id2 = null;
1090 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001091 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001092 id2 = new Identifier(first);
1093 id3 = new Identifier(second);
1094 } else {
1095 id1 = new Identifier(first);
1096 id2 = new Identifier(second);
1097 id3 = new Identifier(third);
1098 }
1099 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001100 }
1101}
1102
vinayakb38b7ca42012-03-05 05:44:15 +00001103FunctionDecl FunctionDeclaration() throws ParseException:
1104{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001105 FunctionDecl funcDecl;
1106 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001107 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001108 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1109 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001110 createNewScope();
1111}
1112{
Till Westmannd7dcb122013-05-16 13:19:09 -07001113 "declare" "function" functionName = Identifier()
1114 paramList = ParameterList()
1115 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001116 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001117 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001118 getCurrentScope().addFunctionDescriptor(signature, false);
1119 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001120 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001121 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001122 }
1123}
1124
vinayakb38b7ca42012-03-05 05:44:15 +00001125
Till Westmann31c21f92013-05-08 09:21:53 -07001126Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001127{
1128 Query query = new Query();
1129 Expression expr;
1130}
1131{
Till Westmann31c21f92013-05-08 09:21:53 -07001132 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001133 {
1134 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001135 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001136 return query;
1137 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001138
vinayakb38b7ca42012-03-05 05:44:15 +00001139}
1140
1141
1142
1143Expression Expression():
1144{
1145 Expression expr = null;
1146 Expression exprP = null;
1147}
1148{
1149(
1150
1151//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1152 expr = OperatorExpr()
1153 | expr = IfThenElse()
1154 | expr = FLWOGR()
1155 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001156
vinayakb38b7ca42012-03-05 05:44:15 +00001157
1158)
1159 {
1160 return (exprP==null) ? expr : exprP;
1161 }
1162}
1163
1164
1165
1166Expression OperatorExpr()throws ParseException:
1167{
1168 OperatorExpr op = null;
1169 Expression operand = null;
1170}
1171{
1172 operand = AndExpr()
1173 (
1174
1175 "or"
1176 {
1177 if (op == null) {
1178 op = new OperatorExpr();
1179 op.addOperand(operand);
1180 op.setCurrentop(true);
1181 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001182 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001183 }
1184
1185 operand = AndExpr()
1186 {
1187 op.addOperand(operand);
1188 }
1189
1190 )*
1191
1192 {
1193 return op==null? operand: op;
1194 }
1195}
1196
1197Expression AndExpr()throws ParseException:
1198{
1199 OperatorExpr op = null;
1200 Expression operand = null;
1201}
1202{
1203 operand = RelExpr()
1204 (
1205
1206 "and"
1207 {
1208 if (op == null) {
1209 op = new OperatorExpr();
1210 op.addOperand(operand);
1211 op.setCurrentop(true);
1212 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001213 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001214 }
1215
1216 operand = RelExpr()
1217 {
1218 op.addOperand(operand);
1219 }
1220
1221 )*
1222
1223 {
1224 return op==null? operand: op;
1225 }
1226}
1227
1228
1229
1230Expression RelExpr()throws ParseException:
1231{
1232 OperatorExpr op = null;
1233 Expression operand = null;
1234 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001235 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001236}
1237{
1238 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001239 {
1240 if (operand instanceof VariableExpr) {
1241 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001242 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001243 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001244 }
1245 }
1246 }
1247
1248 (
1249 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1250 {
alexander.behm07617fd2012-07-25 10:13:50 +00001251 String mhint = getHint(token);
1252 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1253 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1254 }
vinayakb38b7ca42012-03-05 05:44:15 +00001255 if (op == null) {
1256 op = new OperatorExpr();
1257 op.addOperand(operand, broadcast);
1258 op.setCurrentop(true);
1259 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001260 }
1261 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001262 }
1263
1264 operand = AddExpr()
1265 {
alexander.behm07617fd2012-07-25 10:13:50 +00001266 broadcast = false;
1267 if (operand instanceof VariableExpr) {
1268 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001269 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1270 broadcast = true;
1271 }
alexander.behm07617fd2012-07-25 10:13:50 +00001272 }
vinayakb38b7ca42012-03-05 05:44:15 +00001273 op.addOperand(operand, broadcast);
1274 }
1275 )?
1276
1277 {
alexander.behm07617fd2012-07-25 10:13:50 +00001278 if (annotation != null) {
1279 op.addHint(annotation);
1280 }
vinayakb38b7ca42012-03-05 05:44:15 +00001281 return op==null? operand: op;
1282 }
1283}
1284
1285Expression AddExpr()throws ParseException:
1286{
1287 OperatorExpr op = null;
1288 Expression operand = null;
1289}
1290{
1291 operand = MultExpr()
1292
1293 ( ("+" | "-")
1294 {
1295 if (op == null) {
1296 op = new OperatorExpr();
1297 op.addOperand(operand);
1298 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001299 }
1300 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001301 }
1302
1303 operand = MultExpr()
1304 {
1305 op.addOperand(operand);
1306 }
1307 )*
1308
1309 {
1310 return op==null? operand: op;
1311 }
1312}
1313
1314Expression MultExpr()throws ParseException:
1315{
1316 OperatorExpr op = null;
1317 Expression operand = null;
1318}
1319{
1320 operand = UnionExpr()
1321
1322 (( "*" | "/" | "%" | <CARET> | "idiv")
1323 {
1324 if (op == null) {
1325 op = new OperatorExpr();
1326 op.addOperand(operand);
1327 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001328 }
1329 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001330 }
1331 operand = UnionExpr()
1332 {
1333 op.addOperand(operand);
1334 }
1335 )*
1336
1337 {
1338 return op==null?operand:op;
1339 }
1340}
1341
1342Expression UnionExpr() throws ParseException:
1343{
1344 UnionExpr union = null;
1345 Expression operand1 = null;
1346 Expression operand2 = null;
1347}
1348{
1349 operand1 = UnaryExpr()
1350 ("union"
1351 (operand2 = UnaryExpr()) {
1352 if (union == null) {
1353 union = new UnionExpr();
1354 union.addExpr(operand1);
1355 }
1356 union.addExpr(operand2);
1357 } )*
1358 {
1359 return (union == null)? operand1: union;
1360 }
1361}
1362
1363Expression UnaryExpr() throws ParseException:
1364{
1365 Expression uexpr = null;
1366 Expression expr = null;
1367}
1368{
1369 (( "+"|"-")
1370 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001371 uexpr = new UnaryExpr();
1372 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001373 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001374 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001375 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1376 else
1377 throw new ParseException();
1378 }
1379 )?
1380
1381 expr = ValueExpr()
1382 {
1383 if(uexpr!=null){
1384 ((UnaryExpr)uexpr).setExpr(expr);
1385 return uexpr;
1386 }
1387 else{
1388 return expr;
1389 }
1390 }
1391}
1392
Till Westmann04478e72013-05-13 23:01:34 -07001393Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001394{
1395 Expression expr = null;
1396 Identifier ident = null;
1397 AbstractAccessor fa = null;
1398 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001399}
1400{
Till Westmann04478e72013-05-13 23:01:34 -07001401 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001402 {
Till Westmann04478e72013-05-13 23:01:34 -07001403 fa = (fa == null ? new FieldAccessor(expr, ident)
1404 : new FieldAccessor(fa, ident));
1405 }
1406 | index = Index()
1407 {
1408 fa = (fa == null ? new IndexAccessor(expr, index)
1409 : new IndexAccessor(fa, index));
1410 }
1411 )*
1412 {
1413 return fa == null ? expr : fa;
1414 }
vinayakb38b7ca42012-03-05 05:44:15 +00001415}
1416
1417Identifier Field() throws ParseException:
1418{
Till Westmann14a20a72013-05-09 00:06:24 -07001419 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001420}
1421{
Till Westmanna4242bc2013-05-08 17:49:55 -07001422 "." ident = Identifier()
1423 {
Till Westmann14a20a72013-05-09 00:06:24 -07001424 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001425 }
vinayakb38b7ca42012-03-05 05:44:15 +00001426}
1427
1428int Index() throws ParseException:
1429{
1430 Expression expr = null;
1431 int idx = -2;
1432}
1433{
1434 "[" ( expr = Expression()
1435 {
1436 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1437 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001438 Literal lit = ((LiteralExpr)expr).getValue();
1439 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1440 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001441 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001442 }
vinayakb38b7ca42012-03-05 05:44:15 +00001443 else {
1444 throw new ParseException("Index should be an INTEGER");
1445 }
1446 }
1447
1448 }
1449
1450 | "?"
1451 {
1452 idx = IndexAccessor.ANY;
1453 // ANY
1454 }
1455
1456 )
1457
1458 "]"
1459 {
1460 return idx;
1461 }
1462}
1463
1464
1465Expression PrimaryExpr()throws ParseException:
1466{
1467 Expression expr = null;
1468}
1469{
Till Westmann68d99652013-05-09 11:15:21 -07001470 ( LOOKAHEAD(2)
1471 expr = FunctionCallExpr()
1472 | expr = Literal()
1473 | expr = DatasetAccessExpression()
1474 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001475 {
1476 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001477 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001478 }
Till Westmann68d99652013-05-09 11:15:21 -07001479 | expr = ListConstructor()
1480 | expr = RecordConstructor()
1481 | expr = ParenthesizedExpression()
1482 )
1483 {
1484 return expr;
1485 }
vinayakb38b7ca42012-03-05 05:44:15 +00001486}
1487
1488Expression Literal() throws ParseException:
1489{
vinayakb38b7ca42012-03-05 05:44:15 +00001490 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001491 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001492}
1493{
Till Westmann7d535322013-05-09 00:40:02 -07001494 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001495 {
Till Westmann7d535322013-05-09 00:40:02 -07001496 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001497 }
1498 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001499 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001500 try {
1501 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1502 } catch(NumberFormatException ex) {
1503 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1504 }
1505 }
1506 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001507 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001508 lit.setValue(new FloatLiteral(new Float(token.image)));
1509 }
1510 | <DOUBLE_LITERAL>
1511 {
1512 lit.setValue(new DoubleLiteral(new Double(token.image)));
1513 }
1514 | <NULL>
1515 {
1516 lit.setValue(NullLiteral.INSTANCE);
1517 }
1518 | <TRUE>
1519 {
1520 lit.setValue(TrueLiteral.INSTANCE);
1521 }
1522 | <FALSE>
1523 {
1524 lit.setValue(FalseLiteral.INSTANCE);
1525 }
1526 )
vinayakb38b7ca42012-03-05 05:44:15 +00001527 {
1528 return lit;
1529 }
1530}
1531
1532
1533VariableExpr VariableRef() throws ParseException:
1534{
1535 VariableExpr varExp = new VariableExpr();
1536 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001537}
1538{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001539 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001540 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001541 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001542 Identifier ident = lookupSymbol(varName);
1543 if (isInForbiddenScopes(varName)) {
1544 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.");
1545 }
1546 if(ident != null) { // exist such ident
1547 varExp.setIsNewVar(false);
1548 varExp.setVar((VarIdentifier)ident);
1549 } else {
1550 varExp.setVar(var);
1551 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001552 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001553 return varExp;
1554 }
1555}
1556
1557
1558VariableExpr Variable() throws ParseException:
1559{
1560 VariableExpr varExp = new VariableExpr();
1561 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001562}
1563{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001564 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001565 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001566 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001567 if(ident != null) { // exist such ident
1568 varExp.setIsNewVar(false);
1569 }
1570 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001571 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001572 return varExp;
1573 }
1574}
1575
1576Expression ListConstructor() throws ParseException:
1577{
1578 Expression expr = null;
1579}
1580{
1581 (
1582 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1583 )
1584
1585 {
1586 return expr;
1587 }
1588}
1589
1590
1591ListConstructor OrderedListConstructor() throws ParseException:
1592{
1593 ListConstructor expr = new ListConstructor();
1594 Expression tmp = null;
1595 List<Expression> exprList = new ArrayList<Expression>();
1596 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1597}
1598{
1599
1600 "["
1601 ( tmp = Expression()
1602 {
1603 exprList.add(tmp);
1604 }
1605
1606 ("," tmp = Expression() { exprList.add(tmp); })*
1607 )?
1608
1609 "]"
1610
1611 {
1612 expr.setExprList(exprList);
1613 return expr;
1614 }
1615}
1616
1617ListConstructor UnorderedListConstructor() throws ParseException:
1618{
1619 ListConstructor expr = new ListConstructor();
1620 Expression tmp = null;
1621 List<Expression> exprList = new ArrayList<Expression>();
1622 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1623}
1624{
1625
1626 "{{" ( tmp = Expression()
1627 {
1628 exprList.add(tmp);
1629 }
1630 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1631 {
1632 expr.setExprList(exprList);
1633 return expr;
1634 }
1635}
1636
1637RecordConstructor RecordConstructor() throws ParseException:
1638{
1639 RecordConstructor expr = new RecordConstructor();
1640 FieldBinding tmp = null;
1641 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1642}
1643{
1644 "{" (tmp = FieldBinding()
1645 {
1646 fbList.add(tmp);
1647 }
1648 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1649 {
1650 expr.setFbList(fbList);
1651 return expr;
1652 }
1653}
1654
1655FieldBinding FieldBinding() throws ParseException:
1656{
1657 FieldBinding fb = new FieldBinding();
1658 Expression left, right;
1659}
1660{
1661 left = Expression() ":" right = Expression()
1662 {
1663 fb.setLeftExpr(left);
1664 fb.setRightExpr(right);
1665 return fb;
1666 }
1667}
1668
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001669
vinayakb38b7ca42012-03-05 05:44:15 +00001670Expression FunctionCallExpr() throws ParseException:
1671{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001672 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001673 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001674 Expression tmp;
1675 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001676 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001677 String funcName;
1678 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001679 String hint = null;
1680 String id1 = null;
1681 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001682}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001683{
Till Westmann31c21f92013-05-08 09:21:53 -07001684 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001685 {
Till Westmann31c21f92013-05-08 09:21:53 -07001686 dataverse = funcId.first.getValue();
1687 funcName = funcId.second.getValue();
1688 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001689 }
Till Westmann31c21f92013-05-08 09:21:53 -07001690 <LEFTPAREN> (tmp = Expression()
1691 {
1692 argList.add(tmp);
1693 arity ++;
1694 }
1695 ("," tmp = Expression()
1696 {
1697 argList.add(tmp);
1698 arity++;
1699 }
1700 )*)? <RIGHTPAREN>
1701 {
1702 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1703 if (signature == null) {
1704 signature = new FunctionSignature(dataverse, funcName, arity);
1705 }
1706 callExpr = new CallExpr(signature,argList);
1707 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1708 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1709 }
1710 return callExpr;
1711 }
vinayakb38b7ca42012-03-05 05:44:15 +00001712}
1713
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001714Expression DatasetAccessExpression() throws ParseException:
1715{
1716 CallExpr callExpr;
1717 List<Expression> argList = new ArrayList<Expression>();
1718 String funcName;
1719 String dataverse;
Till Westmann14a20a72013-05-09 00:06:24 -07001720 String arg1 = null;
1721 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001722 LiteralExpr ds;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001723 Expression nameArg;
1724 int arity = 0;
1725}
1726{
Till Westmann14a20a72013-05-09 00:06:24 -07001727 <DATASET>
1728 {
1729 dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1730 funcName = token.image;
1731 }
1732 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1733 {
1734 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
1735 ds = new LiteralExpr();
1736 ds.setValue( new StringLiteral(name) );
1737 argList.add(ds);
1738 arity ++;
1739 }
1740 | ( <LEFTPAREN> nameArg = Expression()
1741 {
1742 argList.add(nameArg);
1743 arity ++;
1744 }
1745 ( "," nameArg = Expression()
1746 {
1747 argList.add(nameArg);
1748 arity++;
1749 }
1750 )* <RIGHTPAREN> ) )
1751 {
1752 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1753 if (signature == null) {
1754 signature = new FunctionSignature(dataverse, funcName, arity);
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001755 }
Till Westmann14a20a72013-05-09 00:06:24 -07001756 callExpr = new CallExpr(signature,argList);
1757 return callExpr;
1758 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001759}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001760
vinayakb38b7ca42012-03-05 05:44:15 +00001761Expression ParenthesizedExpression() throws ParseException:
1762{
1763 Expression expr;
1764}
1765{
1766 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1767 {
1768 return expr;
1769 }
1770}
1771
1772Expression IfThenElse() throws ParseException:
1773{
1774 Expression condExpr;
1775 Expression thenExpr;
1776 Expression elseExpr;
1777 IfExpr ifExpr = new IfExpr();
1778}
1779{
1780 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1781
1782 {
1783 ifExpr.setCondExpr(condExpr);
1784 ifExpr.setThenExpr(thenExpr);
1785 ifExpr.setElseExpr(elseExpr);
1786 return ifExpr;
1787 }
1788}
1789
1790Expression FLWOGR() throws ParseException:
1791{
1792 FLWOGRExpression flworg = new FLWOGRExpression();
1793 List<Clause> clauseList = new ArrayList<Clause>();
1794 Expression returnExpr;
1795 Clause tmp;
1796 createNewScope();
1797}
1798{
1799 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1800 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1801
1802 {
1803 flworg.setClauseList(clauseList);
1804 flworg.setReturnExpr(returnExpr);
1805 removeCurrentScope();
1806 return flworg;
1807 }
1808}
1809
1810Clause Clause()throws ParseException :
1811{
1812 Clause clause;
1813}
1814{
1815 (
1816 clause = ForClause()
1817 | clause = LetClause()
1818 | clause = WhereClause()
1819 | clause = OrderbyClause()
1820 | clause = GroupClause()
1821 | clause = LimitClause()
1822 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001823 )
1824 {
1825 return clause;
1826 }
1827}
1828
1829Clause ForClause()throws ParseException :
1830{
1831 ForClause fc = new ForClause();
1832 VariableExpr varExp;
1833 VariableExpr varPos = null;
1834 Expression inExp;
1835 extendCurrentScope();
1836}
1837{
1838 "for" varExp = Variable()
1839 {
1840 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1841 }
1842 ("at" varPos = Variable()
1843 {
1844 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1845 }
1846 )?
1847 "in" ( inExp = Expression() )
1848 {
1849 fc.setVarExpr(varExp);
1850 fc.setInExpr(inExp);
1851 if (varPos != null) {
1852 fc.setPosExpr(varPos);
1853 }
1854 return fc;
1855 }
1856}
1857
1858Clause LetClause() throws ParseException:
1859{
1860 LetClause lc = new LetClause();
1861 VariableExpr varExp;
1862 Expression beExp;
1863 extendCurrentScope();
1864}
1865{
ilovesoupb2527c12012-07-12 03:21:13 +00001866 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001867 {
1868 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001869 lc.setVarExpr(varExp);
1870 lc.setBeExpr(beExp);
1871 return lc;
1872 }
1873}
1874
1875Clause WhereClause()throws ParseException :
1876{
1877 WhereClause wc = new WhereClause();
1878 Expression whereExpr;
1879}
1880{
1881 "where" whereExpr = Expression()
1882 {
1883 wc.setWhereExpr(whereExpr);
1884 return wc;
1885 }
1886}
1887
1888Clause OrderbyClause()throws ParseException :
1889{
1890 OrderbyClause oc = new OrderbyClause();
1891 Expression orderbyExpr;
1892 List<Expression> orderbyList = new ArrayList<Expression>();
1893 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1894 int numOfOrderby = 0;
1895}
1896{
1897 (
1898 "order"
1899 {
1900 String hint = getHint(token);
1901 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1902 String splits[] = hint.split(" +");
1903 int numFrames = Integer.parseInt(splits[1]);
1904 int numTuples = Integer.parseInt(splits[2]);
1905 oc.setNumFrames(numFrames);
1906 oc.setNumTuples(numTuples);
1907 }
1908 }
1909 "by" orderbyExpr = Expression()
1910 {
1911 orderbyList.add(orderbyExpr);
1912 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1913 }
1914 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1915 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1916 {
1917 modifierList.add(modif);
1918 }
1919
1920 ("," orderbyExpr = Expression()
1921 {
1922 orderbyList.add(orderbyExpr);
1923 modif = OrderbyClause.OrderModifier.ASC;
1924 }
1925 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1926 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1927 {
1928 modifierList.add(modif);
1929 }
1930 )*
1931)
1932 {
1933 oc.setModifierList(modifierList);
1934 oc.setOrderbyList(orderbyList);
1935 return oc;
1936 }
1937}
1938Clause GroupClause()throws ParseException :
1939{
1940 GroupbyClause gbc = new GroupbyClause();
1941 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1942 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1943 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1944 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1945 VariableExpr var = null;
1946 VariableExpr withVar = null;
1947 Expression expr = null;
1948 VariableExpr decorVar = null;
1949 Expression decorExpr = null;
1950}
1951{
1952 {
1953 Scope newScope = extendCurrentScopeNoPush(true);
1954 // extendCurrentScope(true);
1955 }
1956 "group"
1957 {
1958 String hint = getHint(token);
1959 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1960 gbc.setHashGroupByHint(true);
1961 }
1962 }
1963 "by" (LOOKAHEAD(2) var = Variable()
1964 {
1965 newScope.addNewVarSymbolToScope(var.getVar());
1966 } ":=")?
1967 expr = Expression()
1968 {
1969 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1970 vePairList.add(pair1);
1971 }
1972 ("," ( LOOKAHEAD(2) var = Variable()
1973 {
1974 newScope.addNewVarSymbolToScope(var.getVar());
1975 } ":=")?
1976 expr = Expression()
1977 {
1978 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1979 vePairList.add(pair2);
1980 }
1981 )*
1982 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1983 {
1984 newScope.addNewVarSymbolToScope(decorVar.getVar());
1985 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1986 decorPairList.add(pair3);
1987 }
1988 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1989 {
1990 newScope.addNewVarSymbolToScope(decorVar.getVar());
1991 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1992 decorPairList.add(pair4);
1993 }
1994 )*
1995 )?
1996 "with" withVar = VariableRef()
1997 {
1998 if(withVar.getIsNewVar()==true)
1999 throw new ParseException("can't find variable " + withVar.getVar());
2000 withVarList.add(withVar);
2001 newScope.addNewVarSymbolToScope(withVar.getVar());
2002 }
2003 ("," withVar = VariableRef()
2004 {
2005 if(withVar.getIsNewVar()==true)
2006 throw new ParseException("can't find variable " + withVar.getVar());
2007 withVarList.add(withVar);
2008 newScope.addNewVarSymbolToScope(withVar.getVar());
2009 })*
2010 {
2011 gbc.setGbyPairList(vePairList);
2012 gbc.setDecorPairList(decorPairList);
2013 gbc.setWithVarList(withVarList);
2014 replaceCurrentScope(newScope);
2015 return gbc;
2016 }
2017}
2018
2019
2020LimitClause LimitClause() throws ParseException:
2021{
2022 LimitClause lc = new LimitClause();
2023 Expression expr;
2024 pushForbiddenScope(getCurrentScope());
2025}
2026{
2027 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2028 ("offset" expr = Expression() { lc.setOffset(expr); })?
2029
2030 {
2031 popForbiddenScope();
2032 return lc;
2033 }
2034}
2035
2036DistinctClause DistinctClause() throws ParseException:
2037{
2038 List<Expression> exprs = new ArrayList<Expression>();
2039 Expression expr;
2040}
2041{
2042 "distinct" "by" expr = Expression()
2043 {
2044 exprs.add(expr);
2045 }
2046 ("," expr = Expression()
2047 {
2048 exprs.add(expr);
2049 }
2050 )*
2051 {
2052 return new DistinctClause(exprs);
2053 }
2054}
2055
vinayakb38b7ca42012-03-05 05:44:15 +00002056QuantifiedExpression QuantifiedExpression()throws ParseException:
2057{
2058 QuantifiedExpression qc = new QuantifiedExpression();
2059 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2060 Expression satisfiesExpr;
2061 VariableExpr var;
2062 Expression inExpr;
2063 QuantifiedPair pair;
2064}
2065{
2066 {
2067 createNewScope();
2068 }
2069
2070 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2071 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2072 var = Variable() "in" inExpr = Expression()
2073 {
2074 pair = new QuantifiedPair(var, inExpr);
2075 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2076 quantifiedList.add(pair);
2077 }
2078 (
2079 "," var = Variable() "in" inExpr = Expression()
2080 {
2081 pair = new QuantifiedPair(var, inExpr);
2082 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2083 quantifiedList.add(pair);
2084 }
2085 )*
2086 "satisfies" satisfiesExpr = Expression()
2087 {
2088 qc.setSatisfiesExpr(satisfiesExpr);
2089 qc.setQuantifiedList(quantifiedList);
2090 removeCurrentScope();
2091 return qc;
2092 }
2093}
2094
2095TOKEN_MGR_DECLS:
2096{
2097 public int commentDepth = 0;
2098}
2099
2100<DEFAULT>
2101TOKEN :
2102{
2103 <CARET : "^" >
2104}
2105
2106<DEFAULT>
2107TOKEN :
2108{
2109 <DATASET : "dataset" >
2110}
2111
2112<DEFAULT>
2113TOKEN :
2114{
2115 <LEFTPAREN : "(" >
2116}
2117
2118<DEFAULT>
2119TOKEN :
2120{
2121 <RIGHTPAREN : ")" >
2122}
2123
2124
2125<DEFAULT>
2126TOKEN :
2127{
2128 <INTEGER_LITERAL : (<DIGIT>)+ >
2129}
2130
2131
2132<DEFAULT>
2133TOKEN :
2134{
2135 <NULL : "null">
2136}
2137
2138<DEFAULT>
2139TOKEN :
2140{
2141 <TRUE : "true">
2142}
2143
2144<DEFAULT>
2145TOKEN :
2146{
2147 <FALSE : "false">
2148}
2149
2150<DEFAULT>
2151TOKEN :
2152{
2153 <#DIGIT : ["0" - "9"]>
2154}
2155
2156
2157TOKEN:
2158{
2159 < DOUBLE_LITERAL: <INTEGER>
2160 | <INTEGER> ( "." <INTEGER> )?
2161 | "." <INTEGER>
2162 >
2163 |
2164 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2165 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2166 | "." <INTEGER> ( "f" | "F" )
2167 >
2168 |
2169 <INTEGER : (<DIGIT>)+ >
2170}
2171
2172<DEFAULT>
2173TOKEN :
2174{
2175 <#LETTER : ["A" - "Z", "a" - "z"]>
2176}
2177
2178<DEFAULT>
2179TOKEN :
2180{
2181 <SPECIALCHARS : ["$", "_", "-"] >
2182}
2183
2184<DEFAULT>
2185TOKEN :
2186{
2187 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2188 |
2189 < #EscapeQuot: "\\\"" >
2190 |
2191 < #EscapeApos: "\\\'" >
2192}
2193
2194<DEFAULT>
2195TOKEN :
2196{
2197 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2198}
2199
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002200
vinayakb38b7ca42012-03-05 05:44:15 +00002201<DEFAULT>
2202TOKEN :
2203{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002204 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002205}
2206
2207SKIP:
2208{
2209 " "
2210| "\t"
2211| "\r"
2212| "\n"
2213}
2214
2215SKIP:
2216{
2217 <"//" (~["\n"])* "\n">
2218}
2219
2220SKIP:
2221{
2222 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2223}
2224
2225
2226SKIP:
2227{
2228 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2229}
2230
2231<INSIDE_COMMENT>
2232SPECIAL_TOKEN:
2233{
2234 <"+"(" ")*(~["*"])*>
2235}
2236
2237<INSIDE_COMMENT>
2238SKIP:
2239{
2240 <"/*"> {commentDepth++;}
2241}
2242
2243<INSIDE_COMMENT>
2244SKIP:
2245{
2246 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2247| <~[]>
2248}