blob: 87a442dfa25ed6be5645f5a798c35f424a245ecc [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);
1120 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001121 }
1122}
1123
vinayakb38b7ca42012-03-05 05:44:15 +00001124
Till Westmann31c21f92013-05-08 09:21:53 -07001125Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001126{
1127 Query query = new Query();
1128 Expression expr;
1129}
1130{
Till Westmann31c21f92013-05-08 09:21:53 -07001131 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001132 {
1133 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001134 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001135 return query;
1136 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001137
vinayakb38b7ca42012-03-05 05:44:15 +00001138}
1139
1140
1141
1142Expression Expression():
1143{
1144 Expression expr = null;
1145 Expression exprP = null;
1146}
1147{
1148(
1149
1150//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1151 expr = OperatorExpr()
1152 | expr = IfThenElse()
1153 | expr = FLWOGR()
1154 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001155
vinayakb38b7ca42012-03-05 05:44:15 +00001156
1157)
1158 {
1159 return (exprP==null) ? expr : exprP;
1160 }
1161}
1162
1163
1164
1165Expression OperatorExpr()throws ParseException:
1166{
1167 OperatorExpr op = null;
1168 Expression operand = null;
1169}
1170{
1171 operand = AndExpr()
1172 (
1173
1174 "or"
1175 {
1176 if (op == null) {
1177 op = new OperatorExpr();
1178 op.addOperand(operand);
1179 op.setCurrentop(true);
1180 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001181 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001182 }
1183
1184 operand = AndExpr()
1185 {
1186 op.addOperand(operand);
1187 }
1188
1189 )*
1190
1191 {
1192 return op==null? operand: op;
1193 }
1194}
1195
1196Expression AndExpr()throws ParseException:
1197{
1198 OperatorExpr op = null;
1199 Expression operand = null;
1200}
1201{
1202 operand = RelExpr()
1203 (
1204
1205 "and"
1206 {
1207 if (op == null) {
1208 op = new OperatorExpr();
1209 op.addOperand(operand);
1210 op.setCurrentop(true);
1211 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001212 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001213 }
1214
1215 operand = RelExpr()
1216 {
1217 op.addOperand(operand);
1218 }
1219
1220 )*
1221
1222 {
1223 return op==null? operand: op;
1224 }
1225}
1226
1227
1228
1229Expression RelExpr()throws ParseException:
1230{
1231 OperatorExpr op = null;
1232 Expression operand = null;
1233 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001234 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001235}
1236{
1237 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001238 {
1239 if (operand instanceof VariableExpr) {
1240 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001241 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001242 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001243 }
1244 }
1245 }
1246
1247 (
1248 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1249 {
alexander.behm07617fd2012-07-25 10:13:50 +00001250 String mhint = getHint(token);
1251 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1252 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1253 }
vinayakb38b7ca42012-03-05 05:44:15 +00001254 if (op == null) {
1255 op = new OperatorExpr();
1256 op.addOperand(operand, broadcast);
1257 op.setCurrentop(true);
1258 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001259 }
1260 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001261 }
1262
1263 operand = AddExpr()
1264 {
alexander.behm07617fd2012-07-25 10:13:50 +00001265 broadcast = false;
1266 if (operand instanceof VariableExpr) {
1267 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001268 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1269 broadcast = true;
1270 }
alexander.behm07617fd2012-07-25 10:13:50 +00001271 }
vinayakb38b7ca42012-03-05 05:44:15 +00001272 op.addOperand(operand, broadcast);
1273 }
1274 )?
1275
1276 {
alexander.behm07617fd2012-07-25 10:13:50 +00001277 if (annotation != null) {
1278 op.addHint(annotation);
1279 }
vinayakb38b7ca42012-03-05 05:44:15 +00001280 return op==null? operand: op;
1281 }
1282}
1283
1284Expression AddExpr()throws ParseException:
1285{
1286 OperatorExpr op = null;
1287 Expression operand = null;
1288}
1289{
1290 operand = MultExpr()
1291
1292 ( ("+" | "-")
1293 {
1294 if (op == null) {
1295 op = new OperatorExpr();
1296 op.addOperand(operand);
1297 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001298 }
1299 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001300 }
1301
1302 operand = MultExpr()
1303 {
1304 op.addOperand(operand);
1305 }
1306 )*
1307
1308 {
1309 return op==null? operand: op;
1310 }
1311}
1312
1313Expression MultExpr()throws ParseException:
1314{
1315 OperatorExpr op = null;
1316 Expression operand = null;
1317}
1318{
1319 operand = UnionExpr()
1320
1321 (( "*" | "/" | "%" | <CARET> | "idiv")
1322 {
1323 if (op == null) {
1324 op = new OperatorExpr();
1325 op.addOperand(operand);
1326 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001327 }
1328 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001329 }
1330 operand = UnionExpr()
1331 {
1332 op.addOperand(operand);
1333 }
1334 )*
1335
1336 {
1337 return op==null?operand:op;
1338 }
1339}
1340
1341Expression UnionExpr() throws ParseException:
1342{
1343 UnionExpr union = null;
1344 Expression operand1 = null;
1345 Expression operand2 = null;
1346}
1347{
1348 operand1 = UnaryExpr()
1349 ("union"
1350 (operand2 = UnaryExpr()) {
1351 if (union == null) {
1352 union = new UnionExpr();
1353 union.addExpr(operand1);
1354 }
1355 union.addExpr(operand2);
1356 } )*
1357 {
1358 return (union == null)? operand1: union;
1359 }
1360}
1361
1362Expression UnaryExpr() throws ParseException:
1363{
1364 Expression uexpr = null;
1365 Expression expr = null;
1366}
1367{
1368 (( "+"|"-")
1369 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001370 uexpr = new UnaryExpr();
1371 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001372 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001373 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001374 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1375 else
1376 throw new ParseException();
1377 }
1378 )?
1379
1380 expr = ValueExpr()
1381 {
1382 if(uexpr!=null){
1383 ((UnaryExpr)uexpr).setExpr(expr);
1384 return uexpr;
1385 }
1386 else{
1387 return expr;
1388 }
1389 }
1390}
1391
Till Westmann04478e72013-05-13 23:01:34 -07001392Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001393{
1394 Expression expr = null;
1395 Identifier ident = null;
1396 AbstractAccessor fa = null;
1397 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001398}
1399{
Till Westmann04478e72013-05-13 23:01:34 -07001400 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001401 {
Till Westmann04478e72013-05-13 23:01:34 -07001402 fa = (fa == null ? new FieldAccessor(expr, ident)
1403 : new FieldAccessor(fa, ident));
1404 }
1405 | index = Index()
1406 {
1407 fa = (fa == null ? new IndexAccessor(expr, index)
1408 : new IndexAccessor(fa, index));
1409 }
1410 )*
1411 {
1412 return fa == null ? expr : fa;
1413 }
vinayakb38b7ca42012-03-05 05:44:15 +00001414}
1415
1416Identifier Field() throws ParseException:
1417{
Till Westmann14a20a72013-05-09 00:06:24 -07001418 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001419}
1420{
Till Westmanna4242bc2013-05-08 17:49:55 -07001421 "." ident = Identifier()
1422 {
Till Westmann14a20a72013-05-09 00:06:24 -07001423 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001424 }
vinayakb38b7ca42012-03-05 05:44:15 +00001425}
1426
1427int Index() throws ParseException:
1428{
1429 Expression expr = null;
1430 int idx = -2;
1431}
1432{
1433 "[" ( expr = Expression()
1434 {
1435 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1436 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001437 Literal lit = ((LiteralExpr)expr).getValue();
1438 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1439 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001440 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001441 }
vinayakb38b7ca42012-03-05 05:44:15 +00001442 else {
1443 throw new ParseException("Index should be an INTEGER");
1444 }
1445 }
1446
1447 }
1448
1449 | "?"
1450 {
1451 idx = IndexAccessor.ANY;
1452 // ANY
1453 }
1454
1455 )
1456
1457 "]"
1458 {
1459 return idx;
1460 }
1461}
1462
1463
1464Expression PrimaryExpr()throws ParseException:
1465{
1466 Expression expr = null;
1467}
1468{
Till Westmann68d99652013-05-09 11:15:21 -07001469 ( LOOKAHEAD(2)
1470 expr = FunctionCallExpr()
1471 | expr = Literal()
1472 | expr = DatasetAccessExpression()
1473 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001474 {
1475 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001476 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001477 }
Till Westmann68d99652013-05-09 11:15:21 -07001478 | expr = ListConstructor()
1479 | expr = RecordConstructor()
1480 | expr = ParenthesizedExpression()
1481 )
1482 {
1483 return expr;
1484 }
vinayakb38b7ca42012-03-05 05:44:15 +00001485}
1486
1487Expression Literal() throws ParseException:
1488{
vinayakb38b7ca42012-03-05 05:44:15 +00001489 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001490 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001491}
1492{
Till Westmann7d535322013-05-09 00:40:02 -07001493 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001494 {
Till Westmann7d535322013-05-09 00:40:02 -07001495 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001496 }
1497 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001498 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001499 try {
1500 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1501 } catch(NumberFormatException ex) {
1502 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1503 }
1504 }
1505 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001506 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001507 lit.setValue(new FloatLiteral(new Float(token.image)));
1508 }
1509 | <DOUBLE_LITERAL>
1510 {
1511 lit.setValue(new DoubleLiteral(new Double(token.image)));
1512 }
1513 | <NULL>
1514 {
1515 lit.setValue(NullLiteral.INSTANCE);
1516 }
1517 | <TRUE>
1518 {
1519 lit.setValue(TrueLiteral.INSTANCE);
1520 }
1521 | <FALSE>
1522 {
1523 lit.setValue(FalseLiteral.INSTANCE);
1524 }
1525 )
vinayakb38b7ca42012-03-05 05:44:15 +00001526 {
1527 return lit;
1528 }
1529}
1530
1531
1532VariableExpr VariableRef() throws ParseException:
1533{
1534 VariableExpr varExp = new VariableExpr();
1535 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001536}
1537{
1538 <VARIABLE>
1539 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001540 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001541 Identifier ident = lookupSymbol(varName);
1542 if (isInForbiddenScopes(varName)) {
1543 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.");
1544 }
1545 if(ident != null) { // exist such ident
1546 varExp.setIsNewVar(false);
1547 varExp.setVar((VarIdentifier)ident);
1548 } else {
1549 varExp.setVar(var);
1550 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001551 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001552 return varExp;
1553 }
1554}
1555
1556
1557VariableExpr Variable() throws ParseException:
1558{
1559 VariableExpr varExp = new VariableExpr();
1560 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001561}
1562{
1563 <VARIABLE>
1564 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001565 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001566 if(ident != null) { // exist such ident
1567 varExp.setIsNewVar(false);
1568 }
1569 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001570 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001571 return varExp;
1572 }
1573}
1574
1575Expression ListConstructor() throws ParseException:
1576{
1577 Expression expr = null;
1578}
1579{
1580 (
1581 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1582 )
1583
1584 {
1585 return expr;
1586 }
1587}
1588
1589
1590ListConstructor OrderedListConstructor() throws ParseException:
1591{
1592 ListConstructor expr = new ListConstructor();
1593 Expression tmp = null;
1594 List<Expression> exprList = new ArrayList<Expression>();
1595 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1596}
1597{
1598
1599 "["
1600 ( tmp = Expression()
1601 {
1602 exprList.add(tmp);
1603 }
1604
1605 ("," tmp = Expression() { exprList.add(tmp); })*
1606 )?
1607
1608 "]"
1609
1610 {
1611 expr.setExprList(exprList);
1612 return expr;
1613 }
1614}
1615
1616ListConstructor UnorderedListConstructor() throws ParseException:
1617{
1618 ListConstructor expr = new ListConstructor();
1619 Expression tmp = null;
1620 List<Expression> exprList = new ArrayList<Expression>();
1621 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1622}
1623{
1624
1625 "{{" ( tmp = Expression()
1626 {
1627 exprList.add(tmp);
1628 }
1629 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1630 {
1631 expr.setExprList(exprList);
1632 return expr;
1633 }
1634}
1635
1636RecordConstructor RecordConstructor() throws ParseException:
1637{
1638 RecordConstructor expr = new RecordConstructor();
1639 FieldBinding tmp = null;
1640 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1641}
1642{
1643 "{" (tmp = FieldBinding()
1644 {
1645 fbList.add(tmp);
1646 }
1647 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1648 {
1649 expr.setFbList(fbList);
1650 return expr;
1651 }
1652}
1653
1654FieldBinding FieldBinding() throws ParseException:
1655{
1656 FieldBinding fb = new FieldBinding();
1657 Expression left, right;
1658}
1659{
1660 left = Expression() ":" right = Expression()
1661 {
1662 fb.setLeftExpr(left);
1663 fb.setRightExpr(right);
1664 return fb;
1665 }
1666}
1667
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001668
vinayakb38b7ca42012-03-05 05:44:15 +00001669Expression FunctionCallExpr() throws ParseException:
1670{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001671 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001672 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001673 Expression tmp;
1674 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001675 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001676 String funcName;
1677 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001678 String hint = null;
1679 String id1 = null;
1680 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001681}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001682{
Till Westmann31c21f92013-05-08 09:21:53 -07001683 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001684 {
Till Westmann31c21f92013-05-08 09:21:53 -07001685 dataverse = funcId.first.getValue();
1686 funcName = funcId.second.getValue();
1687 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001688 }
Till Westmann31c21f92013-05-08 09:21:53 -07001689 <LEFTPAREN> (tmp = Expression()
1690 {
1691 argList.add(tmp);
1692 arity ++;
1693 }
1694 ("," tmp = Expression()
1695 {
1696 argList.add(tmp);
1697 arity++;
1698 }
1699 )*)? <RIGHTPAREN>
1700 {
1701 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1702 if (signature == null) {
1703 signature = new FunctionSignature(dataverse, funcName, arity);
1704 }
1705 callExpr = new CallExpr(signature,argList);
1706 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1707 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1708 }
1709 return callExpr;
1710 }
vinayakb38b7ca42012-03-05 05:44:15 +00001711}
1712
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001713Expression DatasetAccessExpression() throws ParseException:
1714{
1715 CallExpr callExpr;
1716 List<Expression> argList = new ArrayList<Expression>();
1717 String funcName;
1718 String dataverse;
Till Westmann14a20a72013-05-09 00:06:24 -07001719 String arg1 = null;
1720 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001721 LiteralExpr ds;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001722 Expression nameArg;
1723 int arity = 0;
1724}
1725{
Till Westmann14a20a72013-05-09 00:06:24 -07001726 <DATASET>
1727 {
1728 dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1729 funcName = token.image;
1730 }
1731 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1732 {
1733 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
1734 ds = new LiteralExpr();
1735 ds.setValue( new StringLiteral(name) );
1736 argList.add(ds);
1737 arity ++;
1738 }
1739 | ( <LEFTPAREN> nameArg = Expression()
1740 {
1741 argList.add(nameArg);
1742 arity ++;
1743 }
1744 ( "," nameArg = Expression()
1745 {
1746 argList.add(nameArg);
1747 arity++;
1748 }
1749 )* <RIGHTPAREN> ) )
1750 {
1751 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1752 if (signature == null) {
1753 signature = new FunctionSignature(dataverse, funcName, arity);
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001754 }
Till Westmann14a20a72013-05-09 00:06:24 -07001755 callExpr = new CallExpr(signature,argList);
1756 return callExpr;
1757 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001758}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001759
vinayakb38b7ca42012-03-05 05:44:15 +00001760Expression ParenthesizedExpression() throws ParseException:
1761{
1762 Expression expr;
1763}
1764{
1765 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1766 {
1767 return expr;
1768 }
1769}
1770
1771Expression IfThenElse() throws ParseException:
1772{
1773 Expression condExpr;
1774 Expression thenExpr;
1775 Expression elseExpr;
1776 IfExpr ifExpr = new IfExpr();
1777}
1778{
1779 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1780
1781 {
1782 ifExpr.setCondExpr(condExpr);
1783 ifExpr.setThenExpr(thenExpr);
1784 ifExpr.setElseExpr(elseExpr);
1785 return ifExpr;
1786 }
1787}
1788
1789Expression FLWOGR() throws ParseException:
1790{
1791 FLWOGRExpression flworg = new FLWOGRExpression();
1792 List<Clause> clauseList = new ArrayList<Clause>();
1793 Expression returnExpr;
1794 Clause tmp;
1795 createNewScope();
1796}
1797{
1798 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1799 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1800
1801 {
1802 flworg.setClauseList(clauseList);
1803 flworg.setReturnExpr(returnExpr);
1804 removeCurrentScope();
1805 return flworg;
1806 }
1807}
1808
1809Clause Clause()throws ParseException :
1810{
1811 Clause clause;
1812}
1813{
1814 (
1815 clause = ForClause()
1816 | clause = LetClause()
1817 | clause = WhereClause()
1818 | clause = OrderbyClause()
1819 | clause = GroupClause()
1820 | clause = LimitClause()
1821 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001822 )
1823 {
1824 return clause;
1825 }
1826}
1827
1828Clause ForClause()throws ParseException :
1829{
1830 ForClause fc = new ForClause();
1831 VariableExpr varExp;
1832 VariableExpr varPos = null;
1833 Expression inExp;
1834 extendCurrentScope();
1835}
1836{
1837 "for" varExp = Variable()
1838 {
1839 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1840 }
1841 ("at" varPos = Variable()
1842 {
1843 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1844 }
1845 )?
1846 "in" ( inExp = Expression() )
1847 {
1848 fc.setVarExpr(varExp);
1849 fc.setInExpr(inExp);
1850 if (varPos != null) {
1851 fc.setPosExpr(varPos);
1852 }
1853 return fc;
1854 }
1855}
1856
1857Clause LetClause() throws ParseException:
1858{
1859 LetClause lc = new LetClause();
1860 VariableExpr varExp;
1861 Expression beExp;
1862 extendCurrentScope();
1863}
1864{
ilovesoupb2527c12012-07-12 03:21:13 +00001865 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001866 {
1867 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001868 lc.setVarExpr(varExp);
1869 lc.setBeExpr(beExp);
1870 return lc;
1871 }
1872}
1873
1874Clause WhereClause()throws ParseException :
1875{
1876 WhereClause wc = new WhereClause();
1877 Expression whereExpr;
1878}
1879{
1880 "where" whereExpr = Expression()
1881 {
1882 wc.setWhereExpr(whereExpr);
1883 return wc;
1884 }
1885}
1886
1887Clause OrderbyClause()throws ParseException :
1888{
1889 OrderbyClause oc = new OrderbyClause();
1890 Expression orderbyExpr;
1891 List<Expression> orderbyList = new ArrayList<Expression>();
1892 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1893 int numOfOrderby = 0;
1894}
1895{
1896 (
1897 "order"
1898 {
1899 String hint = getHint(token);
1900 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1901 String splits[] = hint.split(" +");
1902 int numFrames = Integer.parseInt(splits[1]);
1903 int numTuples = Integer.parseInt(splits[2]);
1904 oc.setNumFrames(numFrames);
1905 oc.setNumTuples(numTuples);
1906 }
1907 }
1908 "by" orderbyExpr = Expression()
1909 {
1910 orderbyList.add(orderbyExpr);
1911 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1912 }
1913 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1914 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1915 {
1916 modifierList.add(modif);
1917 }
1918
1919 ("," orderbyExpr = Expression()
1920 {
1921 orderbyList.add(orderbyExpr);
1922 modif = OrderbyClause.OrderModifier.ASC;
1923 }
1924 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1925 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1926 {
1927 modifierList.add(modif);
1928 }
1929 )*
1930)
1931 {
1932 oc.setModifierList(modifierList);
1933 oc.setOrderbyList(orderbyList);
1934 return oc;
1935 }
1936}
1937Clause GroupClause()throws ParseException :
1938{
1939 GroupbyClause gbc = new GroupbyClause();
1940 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1941 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1942 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1943 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1944 VariableExpr var = null;
1945 VariableExpr withVar = null;
1946 Expression expr = null;
1947 VariableExpr decorVar = null;
1948 Expression decorExpr = null;
1949}
1950{
1951 {
1952 Scope newScope = extendCurrentScopeNoPush(true);
1953 // extendCurrentScope(true);
1954 }
1955 "group"
1956 {
1957 String hint = getHint(token);
1958 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1959 gbc.setHashGroupByHint(true);
1960 }
1961 }
1962 "by" (LOOKAHEAD(2) var = Variable()
1963 {
1964 newScope.addNewVarSymbolToScope(var.getVar());
1965 } ":=")?
1966 expr = Expression()
1967 {
1968 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1969 vePairList.add(pair1);
1970 }
1971 ("," ( LOOKAHEAD(2) var = Variable()
1972 {
1973 newScope.addNewVarSymbolToScope(var.getVar());
1974 } ":=")?
1975 expr = Expression()
1976 {
1977 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1978 vePairList.add(pair2);
1979 }
1980 )*
1981 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1982 {
1983 newScope.addNewVarSymbolToScope(decorVar.getVar());
1984 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1985 decorPairList.add(pair3);
1986 }
1987 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1988 {
1989 newScope.addNewVarSymbolToScope(decorVar.getVar());
1990 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1991 decorPairList.add(pair4);
1992 }
1993 )*
1994 )?
1995 "with" withVar = VariableRef()
1996 {
1997 if(withVar.getIsNewVar()==true)
1998 throw new ParseException("can't find variable " + withVar.getVar());
1999 withVarList.add(withVar);
2000 newScope.addNewVarSymbolToScope(withVar.getVar());
2001 }
2002 ("," withVar = VariableRef()
2003 {
2004 if(withVar.getIsNewVar()==true)
2005 throw new ParseException("can't find variable " + withVar.getVar());
2006 withVarList.add(withVar);
2007 newScope.addNewVarSymbolToScope(withVar.getVar());
2008 })*
2009 {
2010 gbc.setGbyPairList(vePairList);
2011 gbc.setDecorPairList(decorPairList);
2012 gbc.setWithVarList(withVarList);
2013 replaceCurrentScope(newScope);
2014 return gbc;
2015 }
2016}
2017
2018
2019LimitClause LimitClause() throws ParseException:
2020{
2021 LimitClause lc = new LimitClause();
2022 Expression expr;
2023 pushForbiddenScope(getCurrentScope());
2024}
2025{
2026 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2027 ("offset" expr = Expression() { lc.setOffset(expr); })?
2028
2029 {
2030 popForbiddenScope();
2031 return lc;
2032 }
2033}
2034
2035DistinctClause DistinctClause() throws ParseException:
2036{
2037 List<Expression> exprs = new ArrayList<Expression>();
2038 Expression expr;
2039}
2040{
2041 "distinct" "by" expr = Expression()
2042 {
2043 exprs.add(expr);
2044 }
2045 ("," expr = Expression()
2046 {
2047 exprs.add(expr);
2048 }
2049 )*
2050 {
2051 return new DistinctClause(exprs);
2052 }
2053}
2054
vinayakb38b7ca42012-03-05 05:44:15 +00002055QuantifiedExpression QuantifiedExpression()throws ParseException:
2056{
2057 QuantifiedExpression qc = new QuantifiedExpression();
2058 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2059 Expression satisfiesExpr;
2060 VariableExpr var;
2061 Expression inExpr;
2062 QuantifiedPair pair;
2063}
2064{
2065 {
2066 createNewScope();
2067 }
2068
2069 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2070 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2071 var = Variable() "in" inExpr = Expression()
2072 {
2073 pair = new QuantifiedPair(var, inExpr);
2074 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2075 quantifiedList.add(pair);
2076 }
2077 (
2078 "," var = Variable() "in" inExpr = Expression()
2079 {
2080 pair = new QuantifiedPair(var, inExpr);
2081 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2082 quantifiedList.add(pair);
2083 }
2084 )*
2085 "satisfies" satisfiesExpr = Expression()
2086 {
2087 qc.setSatisfiesExpr(satisfiesExpr);
2088 qc.setQuantifiedList(quantifiedList);
2089 removeCurrentScope();
2090 return qc;
2091 }
2092}
2093
2094TOKEN_MGR_DECLS:
2095{
2096 public int commentDepth = 0;
2097}
2098
2099<DEFAULT>
2100TOKEN :
2101{
2102 <CARET : "^" >
2103}
2104
2105<DEFAULT>
2106TOKEN :
2107{
2108 <DATASET : "dataset" >
2109}
2110
2111<DEFAULT>
2112TOKEN :
2113{
2114 <LEFTPAREN : "(" >
2115}
2116
2117<DEFAULT>
2118TOKEN :
2119{
2120 <RIGHTPAREN : ")" >
2121}
2122
2123
2124<DEFAULT>
2125TOKEN :
2126{
2127 <INTEGER_LITERAL : (<DIGIT>)+ >
2128}
2129
2130
2131<DEFAULT>
2132TOKEN :
2133{
2134 <NULL : "null">
2135}
2136
2137<DEFAULT>
2138TOKEN :
2139{
2140 <TRUE : "true">
2141}
2142
2143<DEFAULT>
2144TOKEN :
2145{
2146 <FALSE : "false">
2147}
2148
2149<DEFAULT>
2150TOKEN :
2151{
2152 <#DIGIT : ["0" - "9"]>
2153}
2154
2155
2156TOKEN:
2157{
2158 < DOUBLE_LITERAL: <INTEGER>
2159 | <INTEGER> ( "." <INTEGER> )?
2160 | "." <INTEGER>
2161 >
2162 |
2163 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2164 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2165 | "." <INTEGER> ( "f" | "F" )
2166 >
2167 |
2168 <INTEGER : (<DIGIT>)+ >
2169}
2170
2171<DEFAULT>
2172TOKEN :
2173{
2174 <#LETTER : ["A" - "Z", "a" - "z"]>
2175}
2176
2177<DEFAULT>
2178TOKEN :
2179{
2180 <SPECIALCHARS : ["$", "_", "-"] >
2181}
2182
2183<DEFAULT>
2184TOKEN :
2185{
2186 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2187 |
2188 < #EscapeQuot: "\\\"" >
2189 |
2190 < #EscapeApos: "\\\'" >
2191}
2192
2193<DEFAULT>
2194TOKEN :
2195{
2196 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2197}
2198
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002199
vinayakb38b7ca42012-03-05 05:44:15 +00002200<DEFAULT>
2201TOKEN :
2202{
2203 <VARIABLE : "$" <IDENTIFIER> >
2204}
2205
2206SKIP:
2207{
2208 " "
2209| "\t"
2210| "\r"
2211| "\n"
2212}
2213
2214SKIP:
2215{
2216 <"//" (~["\n"])* "\n">
2217}
2218
2219SKIP:
2220{
2221 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2222}
2223
2224
2225SKIP:
2226{
2227 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2228}
2229
2230<INSIDE_COMMENT>
2231SPECIAL_TOKEN:
2232{
2233 <"+"(" ")*(~["*"])*>
2234}
2235
2236<INSIDE_COMMENT>
2237SKIP:
2238{
2239 <"/*"> {commentDepth++;}
2240}
2241
2242<INSIDE_COMMENT>
2243SKIP:
2244{
2245 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2246| <~[]>
2247}