blob: 0b1a7c1441120f290b12b255a38ca01eb05a7ceb [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
ramangrover299f76a5e2013-06-18 10:25:17 -070088 private static class FunctionName {
89 public String dataverse = null;
90 public String library = null;
91 public String function = null;
92 }
93
vinayakb38b7ca42012-03-05 05:44:15 +000094 private static String getHint(Token t) {
Till Westmann31c21f92013-05-08 09:21:53 -070095 if (t.specialToken == null) {
96 return null;
97 }
98 String s = t.specialToken.image;
99 int n = s.length();
100 if (n < 2) {
101 return null;
102 }
103 return s.substring(1).trim();
vinayakb38b7ca42012-03-05 05:44:15 +0000104 }
105
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000106 public AQLParser(String s){
Till Westmann31c21f92013-05-08 09:21:53 -0700107 this(new StringReader(s));
108 super.setInput(s);
109 }
vinayakb38b7ca42012-03-05 05:44:15 +0000110
Till Westmann31c21f92013-05-08 09:21:53 -0700111 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
112 File file = new File(args[0]);
113 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
114 AQLParser parser = new AQLParser(fis);
115 List<Statement> st = parser.Statement();
116 //st.accept(new AQLPrintVisitor(), 0);
117 }
vinayakb38b7ca42012-03-05 05:44:15 +0000118}
119
120PARSER_END(AQLParser)
121
122
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000123List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000124{
vinayakb38b7ca42012-03-05 05:44:15 +0000125 scopeStack.push(RootScopeFactory.createRootScope(this));
126 List<Statement> decls = new ArrayList<Statement>();
Till Westmann31c21f92013-05-08 09:21:53 -0700127 Statement stmt = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000128}
129{
Till Westmann31c21f92013-05-08 09:21:53 -0700130 ( stmt = SingleStatement() (";") ?
vinayakb38b7ca42012-03-05 05:44:15 +0000131 {
Till Westmann31c21f92013-05-08 09:21:53 -0700132 decls.add(stmt);
133 }
134 )*
135 <EOF>
136 {
137 return decls;
138 }
139}
140
141Statement SingleStatement() throws ParseException:
142{
143 Statement stmt = null;
144}
145{
146 (
147 stmt = DataverseDeclaration()
148 | stmt = FunctionDeclaration()
149 | stmt = CreateStatement()
150 | stmt = LoadStatement()
151 | stmt = DropStatement()
152 | stmt = WriteStatement()
153 | stmt = SetStatement()
154 | stmt = InsertStatement()
155 | stmt = DeleteStatement()
156 | stmt = UpdateStatement()
157 | stmt = FeedStatement()
158 | stmt = Query()
159 )
160 {
161 return stmt;
162 }
163}
164
165DataverseDecl DataverseDeclaration() throws ParseException:
166{
Till Westmann14a20a72013-05-09 00:06:24 -0700167 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700168}
169{
Till Westmanna4242bc2013-05-08 17:49:55 -0700170 "use" "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700171 {
Till Westmann14a20a72013-05-09 00:06:24 -0700172 defaultDataverse = dvName;
173 return new DataverseDecl(new Identifier(dvName));
Till Westmann31c21f92013-05-08 09:21:53 -0700174 }
175}
176
177Statement CreateStatement() throws ParseException:
178{
179 String hint = null;
180 boolean dgen = false;
181 Statement stmt = null;
182}
183{
184 "create"
185 (
186 {
187 hint = getHint(token);
188 if (hint != null && hint.startsWith(DGEN_HINT)) {
189 dgen = true;
190 }
191 }
192 stmt = TypeSpecification(hint, dgen)
193 | stmt = NodegroupSpecification()
194 | stmt = DatasetSpecification()
195 | stmt = IndexSpecification()
196 | stmt = DataverseSpecification()
197 | stmt = FunctionSpecification()
198 )
199 {
200 return stmt;
201 }
202}
203
204TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
205{
206 Pair<Identifier,Identifier> nameComponents = null;
207 boolean ifNotExists = false;
208 TypeExpression typeExpr = null;
209}
210{
ramangrover299f76a5e2013-06-18 10:25:17 -0700211 "type" nameComponents = TypeName() ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700212 "as" typeExpr = TypeExpr()
213 {
214 long numValues = -1;
215 String filename = null;
216 if (dgen) {
217 String splits[] = hint.split(" +");
218 if (splits.length != 3) {
219 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
220 }
221 filename = splits[1];
222 numValues = Long.parseLong(splits[2]);
223 }
224 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
225 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
226 }
227}
228
229
230NodegroupDecl NodegroupSpecification() throws ParseException:
231{
Till Westmann14a20a72013-05-09 00:06:24 -0700232 String name = null;
233 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700234 boolean ifNotExists = false;
235 List<Identifier>ncNames = null;
236}
237{
Till Westmanna4242bc2013-05-08 17:49:55 -0700238 "nodegroup" name = Identifier()
239 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700240 {
241 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700242 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700243 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700244 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700245 {
Till Westmann14a20a72013-05-09 00:06:24 -0700246 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700247 }
248 )*
249 {
Till Westmann14a20a72013-05-09 00:06:24 -0700250 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700251 }
252}
253
254DatasetDecl DatasetSpecification() throws ParseException:
255{
256 Pair<Identifier,Identifier> nameComponents = null;
257 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700258 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700259 String adapterName = null;
260 Map<String,String> properties = null;
261 FunctionSignature appliedFunction = null;
262 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700263 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700264 Map<String,String> hints = new HashMap<String,String>();
265 DatasetDecl dsetDecl = null;
266}
267{
268 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700269 "external" <DATASET> nameComponents = QualifiedName()
270 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
271 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700272 "using" adapterName = AdapterName() properties = Configuration()
273 ( "hints" hints = Properties() )?
274 {
275 ExternalDetailsDecl edd = new ExternalDetailsDecl();
276 edd.setAdapter(adapterName);
277 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700278 dsetDecl = new DatasetDecl(nameComponents.first,
279 nameComponents.second,
280 new Identifier(typeName),
281 hints,
282 DatasetType.EXTERNAL,
283 edd,
284 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700285 }
286
Till Westmanna4242bc2013-05-08 17:49:55 -0700287 | "feed" <DATASET> nameComponents = QualifiedName()
288 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
289 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700290 "using" adapterName = AdapterName() properties = Configuration()
291 (appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
Till Westmanna4242bc2013-05-08 17:49:55 -0700292 ( "on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700293 ( "hints" hints = Properties() )?
294 {
Till Westmann14a20a72013-05-09 00:06:24 -0700295 FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
296 properties,
297 appliedFunction,
298 nodeGroupName != null
299 ? new Identifier(nodeGroupName)
300 : null,
301 primaryKeyFields);
302 dsetDecl = new DatasetDecl(nameComponents.first,
303 nameComponents.second,
304 new Identifier(typeName),
305 hints,
306 DatasetType.FEED,
307 fdd,
308 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700309 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700310 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700311 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
312 ifNotExists = IfNotExists()
313 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700314 ( "hints" hints = Properties() )?
315 {
Till Westmann14a20a72013-05-09 00:06:24 -0700316 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
317 ? new Identifier(nodeGroupName)
318 : null,
319 primaryKeyFields);
320 dsetDecl = new DatasetDecl(nameComponents.first,
321 nameComponents.second,
322 new Identifier(typeName),
323 hints,
324 DatasetType.INTERNAL,
325 idd,
326 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700327 }
328 )
329 {
330 return dsetDecl;
331 }
332}
333
334CreateIndexStatement IndexSpecification() throws ParseException:
335{
336 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700337 String indexName = null;
338 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700339 boolean ifNotExists = false;
340 Pair<Identifier,Identifier> nameComponents = null;
341 IndexParams indexType = null;
342}
343{
Till Westmanna4242bc2013-05-08 17:49:55 -0700344 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700345 ifNotExists = IfNotExists()
346 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700347 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700348 {
Till Westmann14a20a72013-05-09 00:06:24 -0700349 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700350 }
Till Westmann14a20a72013-05-09 00:06:24 -0700351 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700352 {
Till Westmann14a20a72013-05-09 00:06:24 -0700353 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700354 }
355 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
356 {
Till Westmann14a20a72013-05-09 00:06:24 -0700357 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700358 cis.setIfNotExists(ifNotExists);
359 cis.setDataverseName(nameComponents.first);
360 cis.setDatasetName(nameComponents.second);
361 if (indexType != null) {
362 cis.setIndexType(indexType.type);
363 cis.setGramLength(indexType.gramLength);
364 }
365 return cis;
366 }
367}
368
369IndexParams IndexType() throws ParseException:
370{
371 IndexType type = null;
372 int gramLength = 0;
373}
374{
375 ("btree"
376 {
377 type = IndexType.BTREE;
378 }
379 | "rtree"
380 {
381 type = IndexType.RTREE;
382 }
383 | "keyword"
384 {
Till Westmann31c21f92013-05-08 09:21:53 -0700385 type = IndexType.FUZZY_WORD_INVIX;
386 }
387 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
388 {
Till Westmann31c21f92013-05-08 09:21:53 -0700389 type = IndexType.FUZZY_NGRAM_INVIX;
390 gramLength = Integer.valueOf(token.image);
391 }
392 <RIGHTPAREN>)
393 {
394 return new IndexParams(type, gramLength);
395 }
396}
397
398CreateDataverseStatement DataverseSpecification() throws ParseException :
399{
Till Westmann14a20a72013-05-09 00:06:24 -0700400 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700401 boolean ifNotExists = false;
402 String format = null;
403}
404{
Till Westmanna4242bc2013-05-08 17:49:55 -0700405 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700406 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700407 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700408 {
Till Westmann14a20a72013-05-09 00:06:24 -0700409 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700410 }
411}
412
413CreateFunctionStatement FunctionSpecification() throws ParseException:
414{
415 FunctionSignature signature;
416 boolean ifNotExists = false;
417 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
418 String functionBody;
ramangrover29bdba1a82013-06-21 17:17:52 -0700419 VarIdentifier var = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700420 Expression functionBodyExpr;
421 Token beginPos;
422 Token endPos;
ramangrover299f76a5e2013-06-18 10:25:17 -0700423 FunctionName fctName = null;
424
Till Westmann31c21f92013-05-08 09:21:53 -0700425 createNewScope();
426}
427{
ramangrover299f76a5e2013-06-18 10:25:17 -0700428 "function" fctName = FunctionName()
Till Westmann31c21f92013-05-08 09:21:53 -0700429 ifNotExists = IfNotExists()
ramangrover299f76a5e2013-06-18 10:25:17 -0700430 <LEFTPAREN> (<VARIABLE>
431 {
432 var = new VarIdentifier();
433 var.setValue(token.image);
434 paramList.add(var);
435 getCurrentScope().addNewVarSymbolToScope(var);
436 }
437 ("," <VARIABLE>
438 {
439 var = new VarIdentifier();
440 var.setValue(token.image);
441 paramList.add(var);
442 getCurrentScope().addNewVarSymbolToScope(var);
443 }
444 )*)? <RIGHTPAREN> "{"
Till Westmannd7dcb122013-05-16 13:19:09 -0700445 {
446 beginPos = token;
447 }
448 functionBodyExpr = Expression() "}"
449 {
450 endPos = token;
451 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
ramangrover299f76a5e2013-06-18 10:25:17 -0700452 // TODO use fctName.library
453 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
Till Westmannd7dcb122013-05-16 13:19:09 -0700454 getCurrentScope().addFunctionDescriptor(signature, false);
455 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
456 }
457}
458
459List<VarIdentifier> ParameterList() throws ParseException:
460{
461 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
462 VarIdentifier var = null;
463}
464{
Till Westmann31c21f92013-05-08 09:21:53 -0700465 <LEFTPAREN> (<VARIABLE>
466 {
467 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700468 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700469 paramList.add(var);
470 getCurrentScope().addNewVarSymbolToScope(var);
471 }
472 ("," <VARIABLE>
473 {
474 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700475 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700476 paramList.add(var);
477 getCurrentScope().addNewVarSymbolToScope(var);
478 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700479 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700480 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700481 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700482 }
483}
484
485boolean IfNotExists() throws ParseException:
486{
487}
488{
489 ( "if not exists"
490 {
491 return true;
492 }
493 )?
494 {
495 return false;
496 }
497}
498
499FunctionSignature ApplyFunction() throws ParseException:
500{
501 FunctionSignature funcSig = null;
502}
503{
504 "apply" "function" funcSig = FunctionSignature()
505 {
506 return funcSig;
507 }
508}
509
ramangrover29566b3a92013-05-28 09:07:10 -0700510String GetPolicy() throws ParseException:
511{
512 String policy = null;
513}
514{
515 "using" "policy" policy = Identifier()
516 {
517 return policy;
518 }
519
520}
521
Till Westmann31c21f92013-05-08 09:21:53 -0700522FunctionSignature FunctionSignature() throws ParseException:
523{
ramangrover299f76a5e2013-06-18 10:25:17 -0700524 FunctionName fctName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700525 int arity = 0;
526}
527{
ramangrover299f76a5e2013-06-18 10:25:17 -0700528 fctName = FunctionName() "@" <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700529 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700530 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700531 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
532 throw new ParseException(" invalid arity:" + arity);
533 }
534
ramangrover299f76a5e2013-06-18 10:25:17 -0700535 // TODO use fctName.library
536 return new FunctionSignature(fctName.dataverse, fctName.function, arity);
Till Westmann31c21f92013-05-08 09:21:53 -0700537 }
538}
539
540List<String> PrimaryKey() throws ParseException:
541{
Till Westmann14a20a72013-05-09 00:06:24 -0700542 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700543 List<String> primaryKeyFields = new ArrayList<String>();
544}
545{
Till Westmann14a20a72013-05-09 00:06:24 -0700546 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700547 {
Till Westmann14a20a72013-05-09 00:06:24 -0700548 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700549 }
Till Westmann14a20a72013-05-09 00:06:24 -0700550 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700551 {
Till Westmann14a20a72013-05-09 00:06:24 -0700552 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700553 }
554 )*
555 {
556 return primaryKeyFields;
557 }
558}
559
560Statement DropStatement() throws ParseException:
561{
Till Westmann14a20a72013-05-09 00:06:24 -0700562 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700563 Pair<Identifier,Identifier> pairId = null;
564 Triple<Identifier,Identifier,Identifier> tripleId = null;
565 FunctionSignature funcSig = null;
566 boolean ifExists = false;
567 Statement stmt = null;
568}
569{
570 "drop"
571 (
572 <DATASET> pairId = QualifiedName() ifExists = IfExists()
573 {
574 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
575 }
576 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
577 {
578 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
579 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700580 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700581 {
Till Westmann14a20a72013-05-09 00:06:24 -0700582 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700583 }
ramangrover299f76a5e2013-06-18 10:25:17 -0700584 | "type" pairId = TypeName() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700585 {
586 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
587 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700588 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700589 {
Till Westmann14a20a72013-05-09 00:06:24 -0700590 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700591 }
592 | "function" funcSig = FunctionSignature() ifExists = IfExists()
593 {
594 stmt = new FunctionDropStatement(funcSig, ifExists);
595 }
596 )
597 {
598 return stmt;
599 }
600}
601
602boolean IfExists() throws ParseException :
603{
604}
605{
606 ( "if" "exists"
607 {
608 return true;
609 }
610 )?
611 {
612 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000613 }
614}
615
616InsertStatement InsertStatement() throws ParseException:
617{
Till Westmann31c21f92013-05-08 09:21:53 -0700618 Pair<Identifier,Identifier> nameComponents = null;
619 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000620}
621{
Till Westmann31c21f92013-05-08 09:21:53 -0700622 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
623 {
624 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
625 }
vinayakb38b7ca42012-03-05 05:44:15 +0000626}
627
628DeleteStatement DeleteStatement() throws ParseException:
629{
Till Westmann31c21f92013-05-08 09:21:53 -0700630 VariableExpr var = null;
631 Expression condition = null;
632 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000633}
634{
Till Westmann31c21f92013-05-08 09:21:53 -0700635 "delete" var = Variable()
636 {
637 getCurrentScope().addNewVarSymbolToScope(var.getVar());
638 }
639 "from" <DATASET> nameComponents = QualifiedName()
640 ("where" condition = Expression())?
641 {
642 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
643 }
vinayakb38b7ca42012-03-05 05:44:15 +0000644}
645
646UpdateStatement UpdateStatement() throws ParseException:
647{
Till Westmann31c21f92013-05-08 09:21:53 -0700648 VariableExpr vars;
649 Expression target;
650 Expression condition;
651 UpdateClause uc;
652 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000653}
654{
Till Westmann31c21f92013-05-08 09:21:53 -0700655 "update" vars = Variable() "in" target = Expression()
656 "where" condition = Expression()
657 <LEFTPAREN> (uc = UpdateClause()
658 {
659 ucs.add(uc);
660 }
661 ("," uc = UpdateClause()
662 {
663 ucs.add(uc);
664 }
665 )*) <RIGHTPAREN>
666 {
667 return new UpdateStatement(vars, target, condition, ucs);
668 }
vinayakb38b7ca42012-03-05 05:44:15 +0000669}
670
vinayakb38b7ca42012-03-05 05:44:15 +0000671UpdateClause UpdateClause() throws ParseException:
672{
Till Westmann31c21f92013-05-08 09:21:53 -0700673 Expression target = null;
674 Expression value = null ;
675 InsertStatement is = null;
676 DeleteStatement ds = null;
677 UpdateStatement us = null;
678 Expression condition = null;
679 UpdateClause ifbranch = null;
680 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000681}
682{
683 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700684 | is = InsertStatement()
685 | ds = DeleteStatement()
686 | us = UpdateStatement()
687 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
688 "then" ifbranch = UpdateClause()
689 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
690 {
691 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
692 }
vinayakb38b7ca42012-03-05 05:44:15 +0000693}
694
vinayakb38b7ca42012-03-05 05:44:15 +0000695Statement SetStatement() throws ParseException:
696{
697 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700698 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000699}
700{
Till Westmann7d535322013-05-09 00:40:02 -0700701 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700702 {
Till Westmann31c21f92013-05-08 09:21:53 -0700703 return new SetStatement(pn, pv);
704 }
vinayakb38b7ca42012-03-05 05:44:15 +0000705}
706
707Statement WriteStatement() throws ParseException:
708{
Till Westmann14a20a72013-05-09 00:06:24 -0700709 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000710 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000711 Statement stmt = null;
712 Query query;
713 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000714 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000715}
716{
Till Westmann31c21f92013-05-08 09:21:53 -0700717 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700718 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
719 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700720 {
Till Westmann14a20a72013-05-09 00:06:24 -0700721 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
Till Westmann31c21f92013-05-08 09:21:53 -0700722 }
723 ) | (
724 "into" <DATASET>
725 {
726 nameComponents = QualifiedName();
727 }
728 <LEFTPAREN> query = Query() <RIGHTPAREN>
729 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000730 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700731 }
732 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000733 {
734 return stmt;
735 }
736}
737
vinayakb38b7ca42012-03-05 05:44:15 +0000738LoadFromFileStatement LoadStatement() throws ParseException:
739{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000740 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000741 Identifier datasetName = null;
742 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000743 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000744 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000745 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000746}
747{
Till Westmann31c21f92013-05-08 09:21:53 -0700748 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000749 {
Till Westmann31c21f92013-05-08 09:21:53 -0700750 dataverseName = nameComponents.first;
751 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000752 }
Till Westmann31c21f92013-05-08 09:21:53 -0700753 "using" adapterName = AdapterName() properties = Configuration()
754 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000755 {
Till Westmann31c21f92013-05-08 09:21:53 -0700756 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000757 }
758 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700759 {
760 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
761 }
vinayakb38b7ca42012-03-05 05:44:15 +0000762}
763
vinayakb38b7ca42012-03-05 05:44:15 +0000764
Till Westmann31c21f92013-05-08 09:21:53 -0700765String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000766{
ramangrover29669d8f62013-02-11 06:03:32 +0000767 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000768}
769{
Till Westmann68d99652013-05-09 11:15:21 -0700770 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000771 {
Till Westmann7d535322013-05-09 00:40:02 -0700772 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000773 }
vinayakb38b7ca42012-03-05 05:44:15 +0000774}
775
Till Westmann31c21f92013-05-08 09:21:53 -0700776Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000777{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000778 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700779 Map<String,String> configuration = null;
780 Statement stmt = null;
ramangrover29566b3a92013-05-28 09:07:10 -0700781 String policy = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000782}
783{
Till Westmann31c21f92013-05-08 09:21:53 -0700784 (
ramangrover29566b3a92013-05-28 09:07:10 -0700785 "begin" "feed" nameComponents = QualifiedName() (policy = GetPolicy())?
Till Westmann31c21f92013-05-08 09:21:53 -0700786 {
ramangrover29566b3a92013-05-28 09:07:10 -0700787 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, policy, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700788 }
789 | "suspend" "feed" nameComponents = QualifiedName()
790 {
791 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
792 }
793 | "resume" "feed" nameComponents = QualifiedName()
794 {
795 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
796 }
797 | "end" "feed" nameComponents = QualifiedName()
798 {
799 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
800 }
801 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
802 {
803 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
804 }
805 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000806 {
Till Westmann31c21f92013-05-08 09:21:53 -0700807 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000808 }
809}
810
Till Westmann31c21f92013-05-08 09:21:53 -0700811Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000812{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000813 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700814 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000815}
816{
Till Westmann31c21f92013-05-08 09:21:53 -0700817 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000818 {
Till Westmann31c21f92013-05-08 09:21:53 -0700819 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000820 }
Till Westmann31c21f92013-05-08 09:21:53 -0700821 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000822 {
Till Westmann31c21f92013-05-08 09:21:53 -0700823 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000824 }
Till Westmann31c21f92013-05-08 09:21:53 -0700825 )* )? <RIGHTPAREN>
826 {
827 return configuration;
828 }
829}
830
831Pair<String, String> KeyValuePair() throws ParseException:
832{
833 String key;
834 String value;
835}
836{
Till Westmann7d535322013-05-09 00:40:02 -0700837 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700838 {
839 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000840 }
Till Westmann31c21f92013-05-08 09:21:53 -0700841}
842
843Map<String,String> Properties() throws ParseException:
844{
845 Map<String,String> properties = new HashMap<String,String>();
846 Pair<String, String> property;
847}
848{
849 ( <LEFTPAREN> property = Property()
850 {
851 properties.put(property.first, property.second);
852 }
853 ( "," property = Property()
854 {
855 properties.put(property.first, property.second);
856 }
857 )* <RIGHTPAREN> )?
858 {
859 return properties;
860 }
861}
862
863Pair<String, String> Property() throws ParseException:
864{
865 String key;
866 String value;
867}
868{
Till Westmann7d535322013-05-09 00:40:02 -0700869 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700870 {
871 try {
872 value = "" + Long.valueOf(token.image);
873 } catch (NumberFormatException nfe) {
874 throw new ParseException("inapproriate value: " + token.image);
875 }
876 }
877 )
878 {
879 return new Pair<String, String>(key.toUpperCase(), value);
880 }
vinayakb38b7ca42012-03-05 05:44:15 +0000881}
882
883TypeExpression TypeExpr() throws ParseException:
884{
885 TypeExpression typeExpr = null;
886}
887{
888 (
889 typeExpr = RecordTypeDef()
890 | typeExpr = TypeReference()
891 | typeExpr = OrderedListTypeDef()
892 | typeExpr = UnorderedListTypeDef()
893 )
894 {
895 return typeExpr;
896 }
897}
898
899RecordTypeDefinition RecordTypeDef() throws ParseException:
900{
901 RecordTypeDefinition recType = new RecordTypeDefinition();
902 RecordTypeDefinition.RecordKind recordKind = null;
903}
904{
905 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
906 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
907 "{"
908 {
909 String hint = getHint(token);
910 if (hint != null) {
911 String splits[] = hint.split(" +");
912 if (splits[0].equals(GEN_FIELDS_HINT)) {
913 if (splits.length != 5) {
914 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
915 }
916 if (!splits[1].equals("int")) {
917 throw new ParseException("The only supported type for gen-fields is int.");
918 }
919 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
920 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
921 recType.setUndeclaredFieldsDataGen(ufdg);
922 }
923 }
924
925 }
926 (
927 RecordField(recType)
928 ( "," RecordField(recType) )*
929 )?
930 "}"
931 {
932 if (recordKind == null) {
933 recordKind = RecordTypeDefinition.RecordKind.OPEN;
934 }
935 recType.setRecordKind(recordKind);
936 return recType;
937 }
938}
939
940void RecordField(RecordTypeDefinition recType) throws ParseException:
941{
942 String fieldName;
943 TypeExpression type = null;
944 boolean nullable = false;
945}
946{
Till Westmann14a20a72013-05-09 00:06:24 -0700947 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000948 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700949 fieldName = token.image;
950 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000951 IRecordFieldDataGen rfdg = null;
952 if (hint != null) {
953 String splits[] = hint.split(" +");
954 if (splits[0].equals(VAL_FILE_HINT)) {
955 File[] valFiles = new File[splits.length - 1];
956 for (int k=1; k<splits.length; k++) {
957 valFiles[k-1] = new File(splits[k]);
958 }
959 rfdg = new FieldValFileDataGen(valFiles);
960 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
961 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
962 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
963 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
964 } else if (splits[0].equals(LIST_HINT)) {
965 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
966 } else if (splits[0].equals(INTERVAL_HINT)) {
967 FieldIntervalDataGen.ValueType vt;
968 if (splits[1].equals("int")) {
969 vt = FieldIntervalDataGen.ValueType.INT;
970 } else if (splits[1].equals("long")) {
971 vt = FieldIntervalDataGen.ValueType.LONG;
972 } else if (splits[1].equals("float")) {
973 vt = FieldIntervalDataGen.ValueType.FLOAT;
974 } else if (splits[1].equals("double")) {
975 vt = FieldIntervalDataGen.ValueType.DOUBLE;
976 } else {
977 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
978 }
979 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
980 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
981 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
982 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
983 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
984 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
985 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
986 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
987 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
988 } else if (splits[0].equals(AUTO_HINT)) {
989 rfdg = new AutoDataGen(splits[1]);
990 }
991 }
992 }
993 ":"
994 ( type = TypeExpr() )
995 ("?" { nullable = true; } )?
996 {
997 recType.addField(fieldName, type, nullable, rfdg);
998 }
999}
1000
1001TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001002{
Till Westmann14a20a72013-05-09 00:06:24 -07001003 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001004}
1005{
1006 id = Identifier()
1007 {
Till Westmann14a20a72013-05-09 00:06:24 -07001008 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -07001009 }
vinayakb38b7ca42012-03-05 05:44:15 +00001010}
1011
1012OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1013{
1014 TypeExpression type = null;
1015}
1016{
1017 "["
1018 ( type = TypeExpr() )
1019 "]"
1020 {
1021 return new OrderedListTypeDefinition(type);
1022 }
1023}
1024
1025
1026UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1027{
1028 TypeExpression type = null;
1029}
1030{
1031 "{{"
1032 ( type = TypeExpr() )
1033 "}}"
1034 {
1035 return new UnorderedListTypeDefinition(type);
1036 }
1037}
1038
ramangrover299f76a5e2013-06-18 10:25:17 -07001039FunctionName FunctionName() throws ParseException:
1040{
1041 String first = null;
1042 String second = null;
1043 String third = null;
1044 boolean secondAfterDot = false;
1045}
1046{
1047 first = Identifier() ( "." second = Identifier()
1048 {
1049 secondAfterDot = true;
1050 }
1051 ("#" third = Identifier())? | "#" second = Identifier() )?
1052 {
1053 FunctionName result = new FunctionName();
1054 if (second == null) {
1055 result.dataverse = defaultDataverse;
1056 result.library = null;
1057 result.function = first;
1058 } else if (third == null) {
1059 if (secondAfterDot) {
1060 result.dataverse = first;
1061 result.library = null;
1062 result.function = second;
1063 } else {
1064 result.dataverse = defaultDataverse;
1065 result.library = first;
1066 result.function = second;
1067 }
1068 } else {
1069 result.dataverse = first;
1070 result.library = second;
1071 result.function = third;
1072 }
1073 return result;
1074 }
1075}
Till Westmann31c21f92013-05-08 09:21:53 -07001076
ramangrover299f76a5e2013-06-18 10:25:17 -07001077
1078Pair<Identifier,Identifier> TypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001079{
Till Westmann31c21f92013-05-08 09:21:53 -07001080 Pair<Identifier,Identifier> name = null;
1081}
1082{
1083 name = QualifiedName()
1084 {
1085 if (name.first == null) {
1086 name.first = new Identifier(defaultDataverse);
1087 }
1088 return name;
1089 }
1090}
1091
Till Westmann14a20a72013-05-09 00:06:24 -07001092String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001093{
Till Westmann68d99652013-05-09 11:15:21 -07001094 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001095}
1096{
1097 <IDENTIFIER>
1098 {
Till Westmann14a20a72013-05-09 00:06:24 -07001099 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001100 }
Till Westmann68d99652013-05-09 11:15:21 -07001101 | lit = StringLiteral()
1102 {
1103 return lit;
1104 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001105}
1106
Till Westmann7d535322013-05-09 00:40:02 -07001107String StringLiteral() throws ParseException:
1108{
1109}
1110{
1111 <STRING_LITERAL>
1112 {
1113 return removeQuotesAndEscapes(token.image);
1114 }
1115}
1116
Till Westmann31c21f92013-05-08 09:21:53 -07001117Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1118{
Till Westmann14a20a72013-05-09 00:06:24 -07001119 String first = null;
1120 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001121}
1122{
Till Westmanna4242bc2013-05-08 17:49:55 -07001123 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001124 {
Till Westmann14a20a72013-05-09 00:06:24 -07001125 Identifier id1 = null;
1126 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001127 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001128 id2 = new Identifier(first);
1129 } else
1130 {
1131 id1 = new Identifier(first);
1132 id2 = new Identifier(second);
1133 }
1134 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001135 }
1136}
1137
Till Westmann31c21f92013-05-08 09:21:53 -07001138Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001139{
Till Westmann14a20a72013-05-09 00:06:24 -07001140 String first = null;
1141 String second = null;
1142 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001143}
1144{
Till Westmanna4242bc2013-05-08 17:49:55 -07001145 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001146 {
Till Westmann14a20a72013-05-09 00:06:24 -07001147 Identifier id1 = null;
1148 Identifier id2 = null;
1149 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001150 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001151 id2 = new Identifier(first);
1152 id3 = new Identifier(second);
1153 } else {
1154 id1 = new Identifier(first);
1155 id2 = new Identifier(second);
1156 id3 = new Identifier(third);
1157 }
1158 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001159 }
1160}
1161
vinayakb38b7ca42012-03-05 05:44:15 +00001162FunctionDecl FunctionDeclaration() throws ParseException:
1163{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001164 FunctionDecl funcDecl;
1165 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001166 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001167 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1168 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001169 createNewScope();
1170}
1171{
Till Westmannd7dcb122013-05-16 13:19:09 -07001172 "declare" "function" functionName = Identifier()
1173 paramList = ParameterList()
1174 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001175 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001176 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001177 getCurrentScope().addFunctionDescriptor(signature, false);
1178 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001179 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001180 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001181 }
1182}
1183
vinayakb38b7ca42012-03-05 05:44:15 +00001184
Till Westmann31c21f92013-05-08 09:21:53 -07001185Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001186{
1187 Query query = new Query();
1188 Expression expr;
1189}
1190{
Till Westmann31c21f92013-05-08 09:21:53 -07001191 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001192 {
1193 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001194 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001195 return query;
1196 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001197
vinayakb38b7ca42012-03-05 05:44:15 +00001198}
1199
1200
1201
1202Expression Expression():
1203{
1204 Expression expr = null;
1205 Expression exprP = null;
1206}
1207{
1208(
1209
1210//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1211 expr = OperatorExpr()
1212 | expr = IfThenElse()
1213 | expr = FLWOGR()
1214 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001215
vinayakb38b7ca42012-03-05 05:44:15 +00001216
1217)
1218 {
1219 return (exprP==null) ? expr : exprP;
1220 }
1221}
1222
1223
1224
1225Expression OperatorExpr()throws ParseException:
1226{
1227 OperatorExpr op = null;
1228 Expression operand = null;
1229}
1230{
1231 operand = AndExpr()
1232 (
1233
1234 "or"
1235 {
1236 if (op == null) {
1237 op = new OperatorExpr();
1238 op.addOperand(operand);
1239 op.setCurrentop(true);
1240 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001241 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001242 }
1243
1244 operand = AndExpr()
1245 {
1246 op.addOperand(operand);
1247 }
1248
1249 )*
1250
1251 {
1252 return op==null? operand: op;
1253 }
1254}
1255
1256Expression AndExpr()throws ParseException:
1257{
1258 OperatorExpr op = null;
1259 Expression operand = null;
1260}
1261{
1262 operand = RelExpr()
1263 (
1264
1265 "and"
1266 {
1267 if (op == null) {
1268 op = new OperatorExpr();
1269 op.addOperand(operand);
1270 op.setCurrentop(true);
1271 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001272 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001273 }
1274
1275 operand = RelExpr()
1276 {
1277 op.addOperand(operand);
1278 }
1279
1280 )*
1281
1282 {
1283 return op==null? operand: op;
1284 }
1285}
1286
1287
1288
1289Expression RelExpr()throws ParseException:
1290{
1291 OperatorExpr op = null;
1292 Expression operand = null;
1293 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001294 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001295}
1296{
1297 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001298 {
1299 if (operand instanceof VariableExpr) {
1300 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001301 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001302 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001303 }
1304 }
1305 }
1306
1307 (
1308 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1309 {
alexander.behm07617fd2012-07-25 10:13:50 +00001310 String mhint = getHint(token);
1311 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1312 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1313 }
vinayakb38b7ca42012-03-05 05:44:15 +00001314 if (op == null) {
1315 op = new OperatorExpr();
1316 op.addOperand(operand, broadcast);
1317 op.setCurrentop(true);
1318 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001319 }
1320 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001321 }
1322
1323 operand = AddExpr()
1324 {
alexander.behm07617fd2012-07-25 10:13:50 +00001325 broadcast = false;
1326 if (operand instanceof VariableExpr) {
1327 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001328 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1329 broadcast = true;
1330 }
alexander.behm07617fd2012-07-25 10:13:50 +00001331 }
vinayakb38b7ca42012-03-05 05:44:15 +00001332 op.addOperand(operand, broadcast);
1333 }
1334 )?
1335
1336 {
alexander.behm07617fd2012-07-25 10:13:50 +00001337 if (annotation != null) {
1338 op.addHint(annotation);
1339 }
vinayakb38b7ca42012-03-05 05:44:15 +00001340 return op==null? operand: op;
1341 }
1342}
1343
1344Expression AddExpr()throws ParseException:
1345{
1346 OperatorExpr op = null;
1347 Expression operand = null;
1348}
1349{
1350 operand = MultExpr()
1351
1352 ( ("+" | "-")
1353 {
1354 if (op == null) {
1355 op = new OperatorExpr();
1356 op.addOperand(operand);
1357 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001358 }
1359 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001360 }
1361
1362 operand = MultExpr()
1363 {
1364 op.addOperand(operand);
1365 }
1366 )*
1367
1368 {
1369 return op==null? operand: op;
1370 }
1371}
1372
1373Expression MultExpr()throws ParseException:
1374{
1375 OperatorExpr op = null;
1376 Expression operand = null;
1377}
1378{
1379 operand = UnionExpr()
1380
1381 (( "*" | "/" | "%" | <CARET> | "idiv")
1382 {
1383 if (op == null) {
1384 op = new OperatorExpr();
1385 op.addOperand(operand);
1386 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001387 }
1388 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001389 }
1390 operand = UnionExpr()
1391 {
1392 op.addOperand(operand);
1393 }
1394 )*
1395
1396 {
1397 return op==null?operand:op;
1398 }
1399}
1400
1401Expression UnionExpr() throws ParseException:
1402{
1403 UnionExpr union = null;
1404 Expression operand1 = null;
1405 Expression operand2 = null;
1406}
1407{
1408 operand1 = UnaryExpr()
1409 ("union"
1410 (operand2 = UnaryExpr()) {
1411 if (union == null) {
1412 union = new UnionExpr();
1413 union.addExpr(operand1);
1414 }
1415 union.addExpr(operand2);
1416 } )*
1417 {
1418 return (union == null)? operand1: union;
1419 }
1420}
1421
1422Expression UnaryExpr() throws ParseException:
1423{
1424 Expression uexpr = null;
1425 Expression expr = null;
1426}
1427{
1428 (( "+"|"-")
1429 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001430 uexpr = new UnaryExpr();
1431 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001432 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001433 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001434 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1435 else
1436 throw new ParseException();
1437 }
1438 )?
1439
1440 expr = ValueExpr()
1441 {
1442 if(uexpr!=null){
1443 ((UnaryExpr)uexpr).setExpr(expr);
1444 return uexpr;
1445 }
1446 else{
1447 return expr;
1448 }
1449 }
1450}
1451
Till Westmann04478e72013-05-13 23:01:34 -07001452Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001453{
1454 Expression expr = null;
1455 Identifier ident = null;
1456 AbstractAccessor fa = null;
1457 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001458}
1459{
Till Westmann04478e72013-05-13 23:01:34 -07001460 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001461 {
Till Westmann04478e72013-05-13 23:01:34 -07001462 fa = (fa == null ? new FieldAccessor(expr, ident)
1463 : new FieldAccessor(fa, ident));
1464 }
1465 | index = Index()
1466 {
1467 fa = (fa == null ? new IndexAccessor(expr, index)
1468 : new IndexAccessor(fa, index));
1469 }
1470 )*
1471 {
1472 return fa == null ? expr : fa;
1473 }
vinayakb38b7ca42012-03-05 05:44:15 +00001474}
1475
1476Identifier Field() throws ParseException:
1477{
Till Westmann14a20a72013-05-09 00:06:24 -07001478 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001479}
1480{
Till Westmanna4242bc2013-05-08 17:49:55 -07001481 "." ident = Identifier()
1482 {
Till Westmann14a20a72013-05-09 00:06:24 -07001483 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001484 }
vinayakb38b7ca42012-03-05 05:44:15 +00001485}
1486
1487int Index() throws ParseException:
1488{
1489 Expression expr = null;
1490 int idx = -2;
1491}
1492{
1493 "[" ( expr = Expression()
1494 {
1495 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1496 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001497 Literal lit = ((LiteralExpr)expr).getValue();
1498 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1499 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001500 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001501 }
vinayakb38b7ca42012-03-05 05:44:15 +00001502 else {
1503 throw new ParseException("Index should be an INTEGER");
1504 }
1505 }
1506
1507 }
1508
1509 | "?"
1510 {
1511 idx = IndexAccessor.ANY;
1512 // ANY
1513 }
1514
1515 )
1516
1517 "]"
1518 {
1519 return idx;
1520 }
1521}
1522
1523
1524Expression PrimaryExpr()throws ParseException:
1525{
1526 Expression expr = null;
1527}
1528{
Till Westmann68d99652013-05-09 11:15:21 -07001529 ( LOOKAHEAD(2)
1530 expr = FunctionCallExpr()
1531 | expr = Literal()
1532 | expr = DatasetAccessExpression()
1533 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001534 {
1535 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001536 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001537 }
Till Westmann68d99652013-05-09 11:15:21 -07001538 | expr = ListConstructor()
1539 | expr = RecordConstructor()
1540 | expr = ParenthesizedExpression()
1541 )
1542 {
1543 return expr;
1544 }
vinayakb38b7ca42012-03-05 05:44:15 +00001545}
1546
1547Expression Literal() throws ParseException:
1548{
vinayakb38b7ca42012-03-05 05:44:15 +00001549 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001550 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001551}
1552{
Till Westmann7d535322013-05-09 00:40:02 -07001553 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001554 {
Till Westmann7d535322013-05-09 00:40:02 -07001555 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001556 }
1557 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001558 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001559 try {
1560 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1561 } catch(NumberFormatException ex) {
1562 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1563 }
1564 }
1565 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001566 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001567 lit.setValue(new FloatLiteral(new Float(token.image)));
1568 }
1569 | <DOUBLE_LITERAL>
1570 {
1571 lit.setValue(new DoubleLiteral(new Double(token.image)));
1572 }
1573 | <NULL>
1574 {
1575 lit.setValue(NullLiteral.INSTANCE);
1576 }
1577 | <TRUE>
1578 {
1579 lit.setValue(TrueLiteral.INSTANCE);
1580 }
1581 | <FALSE>
1582 {
1583 lit.setValue(FalseLiteral.INSTANCE);
1584 }
1585 )
vinayakb38b7ca42012-03-05 05:44:15 +00001586 {
1587 return lit;
1588 }
1589}
1590
1591
1592VariableExpr VariableRef() throws ParseException:
1593{
1594 VariableExpr varExp = new VariableExpr();
1595 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001596}
1597{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001598 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001599 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001600 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001601 Identifier ident = lookupSymbol(varName);
1602 if (isInForbiddenScopes(varName)) {
1603 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.");
1604 }
1605 if(ident != null) { // exist such ident
1606 varExp.setIsNewVar(false);
1607 varExp.setVar((VarIdentifier)ident);
1608 } else {
1609 varExp.setVar(var);
1610 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001611 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001612 return varExp;
1613 }
1614}
1615
1616
1617VariableExpr Variable() throws ParseException:
1618{
1619 VariableExpr varExp = new VariableExpr();
1620 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001621}
1622{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001623 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001624 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001625 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001626 if(ident != null) { // exist such ident
1627 varExp.setIsNewVar(false);
1628 }
1629 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001630 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001631 return varExp;
1632 }
1633}
1634
1635Expression ListConstructor() throws ParseException:
1636{
1637 Expression expr = null;
1638}
1639{
1640 (
1641 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1642 )
1643
1644 {
1645 return expr;
1646 }
1647}
1648
1649
1650ListConstructor OrderedListConstructor() throws ParseException:
1651{
1652 ListConstructor expr = new ListConstructor();
1653 Expression tmp = null;
1654 List<Expression> exprList = new ArrayList<Expression>();
1655 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1656}
1657{
1658
1659 "["
1660 ( tmp = Expression()
1661 {
1662 exprList.add(tmp);
1663 }
1664
1665 ("," tmp = Expression() { exprList.add(tmp); })*
1666 )?
1667
1668 "]"
1669
1670 {
1671 expr.setExprList(exprList);
1672 return expr;
1673 }
1674}
1675
1676ListConstructor UnorderedListConstructor() throws ParseException:
1677{
1678 ListConstructor expr = new ListConstructor();
1679 Expression tmp = null;
1680 List<Expression> exprList = new ArrayList<Expression>();
1681 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1682}
1683{
1684
1685 "{{" ( tmp = Expression()
1686 {
1687 exprList.add(tmp);
1688 }
1689 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1690 {
1691 expr.setExprList(exprList);
1692 return expr;
1693 }
1694}
1695
1696RecordConstructor RecordConstructor() throws ParseException:
1697{
1698 RecordConstructor expr = new RecordConstructor();
1699 FieldBinding tmp = null;
1700 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1701}
1702{
1703 "{" (tmp = FieldBinding()
1704 {
1705 fbList.add(tmp);
1706 }
1707 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1708 {
1709 expr.setFbList(fbList);
1710 return expr;
1711 }
1712}
1713
1714FieldBinding FieldBinding() throws ParseException:
1715{
1716 FieldBinding fb = new FieldBinding();
1717 Expression left, right;
1718}
1719{
1720 left = Expression() ":" right = Expression()
1721 {
1722 fb.setLeftExpr(left);
1723 fb.setRightExpr(right);
1724 return fb;
1725 }
1726}
1727
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001728
vinayakb38b7ca42012-03-05 05:44:15 +00001729Expression FunctionCallExpr() throws ParseException:
1730{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001731 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001732 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001733 Expression tmp;
1734 int arity = 0;
ramangrover299f76a5e2013-06-18 10:25:17 -07001735 FunctionName funcName = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001736 String hint = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001737}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001738{
ramangrover299f76a5e2013-06-18 10:25:17 -07001739 funcName = FunctionName()
vinayakb38b7ca42012-03-05 05:44:15 +00001740 {
Till Westmann31c21f92013-05-08 09:21:53 -07001741 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001742 }
Till Westmann31c21f92013-05-08 09:21:53 -07001743 <LEFTPAREN> (tmp = Expression()
1744 {
1745 argList.add(tmp);
1746 arity ++;
1747 }
1748 ("," tmp = Expression()
1749 {
1750 argList.add(tmp);
1751 arity++;
1752 }
1753 )*)? <RIGHTPAREN>
1754 {
ramangrover299f76a5e2013-06-18 10:25:17 -07001755 // TODO use funcName.library
ramangrover29bdba1a82013-06-21 17:17:52 -07001756 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
ramangrover299f76a5e2013-06-18 10:25:17 -07001757 FunctionSignature signature
ramangrover29bdba1a82013-06-21 17:17:52 -07001758 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001759 if (signature == null) {
ramangrover29bdba1a82013-06-21 17:17:52 -07001760 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001761 }
1762 callExpr = new CallExpr(signature,argList);
1763 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1764 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1765 }
1766 return callExpr;
1767 }
vinayakb38b7ca42012-03-05 05:44:15 +00001768}
1769
ramangrover29@gmail.com5f248e12013-04-11 01:03:09 +00001770
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001771Expression DatasetAccessExpression() throws ParseException:
1772{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001773 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001774 String arg1 = null;
1775 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001776 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001777}
1778{
Till Westmann14a20a72013-05-09 00:06:24 -07001779 <DATASET>
1780 {
Till Westmann14a20a72013-05-09 00:06:24 -07001781 funcName = token.image;
1782 }
1783 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1784 {
1785 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001786 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001787 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001788 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001789 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001790 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001791 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001792 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1793 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1794 if (signature == null) {
1795 signature = new FunctionSignature(dataverse, funcName, 1);
1796 }
1797 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001798 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001799 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001800 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001801}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001802
vinayakb38b7ca42012-03-05 05:44:15 +00001803Expression ParenthesizedExpression() throws ParseException:
1804{
1805 Expression expr;
1806}
1807{
1808 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1809 {
1810 return expr;
1811 }
1812}
1813
1814Expression IfThenElse() throws ParseException:
1815{
1816 Expression condExpr;
1817 Expression thenExpr;
1818 Expression elseExpr;
1819 IfExpr ifExpr = new IfExpr();
1820}
1821{
1822 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1823
1824 {
1825 ifExpr.setCondExpr(condExpr);
1826 ifExpr.setThenExpr(thenExpr);
1827 ifExpr.setElseExpr(elseExpr);
1828 return ifExpr;
1829 }
1830}
1831
1832Expression FLWOGR() throws ParseException:
1833{
1834 FLWOGRExpression flworg = new FLWOGRExpression();
1835 List<Clause> clauseList = new ArrayList<Clause>();
1836 Expression returnExpr;
1837 Clause tmp;
1838 createNewScope();
1839}
1840{
1841 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1842 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1843
1844 {
1845 flworg.setClauseList(clauseList);
1846 flworg.setReturnExpr(returnExpr);
1847 removeCurrentScope();
1848 return flworg;
1849 }
1850}
1851
1852Clause Clause()throws ParseException :
1853{
1854 Clause clause;
1855}
1856{
1857 (
1858 clause = ForClause()
1859 | clause = LetClause()
1860 | clause = WhereClause()
1861 | clause = OrderbyClause()
1862 | clause = GroupClause()
1863 | clause = LimitClause()
1864 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001865 )
1866 {
1867 return clause;
1868 }
1869}
1870
1871Clause ForClause()throws ParseException :
1872{
1873 ForClause fc = new ForClause();
1874 VariableExpr varExp;
1875 VariableExpr varPos = null;
1876 Expression inExp;
1877 extendCurrentScope();
1878}
1879{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001880 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001881 {
1882 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001883 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001884 fc.setInExpr(inExp);
1885 if (varPos != null) {
1886 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001887 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001888 }
1889 return fc;
1890 }
1891}
1892
1893Clause LetClause() throws ParseException:
1894{
1895 LetClause lc = new LetClause();
1896 VariableExpr varExp;
1897 Expression beExp;
1898 extendCurrentScope();
1899}
1900{
ilovesoupb2527c12012-07-12 03:21:13 +00001901 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001902 {
1903 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001904 lc.setVarExpr(varExp);
1905 lc.setBeExpr(beExp);
1906 return lc;
1907 }
1908}
1909
1910Clause WhereClause()throws ParseException :
1911{
1912 WhereClause wc = new WhereClause();
1913 Expression whereExpr;
1914}
1915{
1916 "where" whereExpr = Expression()
1917 {
1918 wc.setWhereExpr(whereExpr);
1919 return wc;
1920 }
1921}
1922
1923Clause OrderbyClause()throws ParseException :
1924{
1925 OrderbyClause oc = new OrderbyClause();
1926 Expression orderbyExpr;
1927 List<Expression> orderbyList = new ArrayList<Expression>();
1928 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1929 int numOfOrderby = 0;
1930}
1931{
1932 (
1933 "order"
1934 {
1935 String hint = getHint(token);
1936 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1937 String splits[] = hint.split(" +");
1938 int numFrames = Integer.parseInt(splits[1]);
1939 int numTuples = Integer.parseInt(splits[2]);
1940 oc.setNumFrames(numFrames);
1941 oc.setNumTuples(numTuples);
1942 }
1943 }
1944 "by" orderbyExpr = Expression()
1945 {
1946 orderbyList.add(orderbyExpr);
1947 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1948 }
1949 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1950 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1951 {
1952 modifierList.add(modif);
1953 }
1954
1955 ("," orderbyExpr = Expression()
1956 {
1957 orderbyList.add(orderbyExpr);
1958 modif = OrderbyClause.OrderModifier.ASC;
1959 }
1960 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1961 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1962 {
1963 modifierList.add(modif);
1964 }
1965 )*
1966)
1967 {
1968 oc.setModifierList(modifierList);
1969 oc.setOrderbyList(orderbyList);
1970 return oc;
1971 }
1972}
1973Clause GroupClause()throws ParseException :
1974{
1975 GroupbyClause gbc = new GroupbyClause();
1976 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1977 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1978 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1979 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1980 VariableExpr var = null;
1981 VariableExpr withVar = null;
1982 Expression expr = null;
1983 VariableExpr decorVar = null;
1984 Expression decorExpr = null;
1985}
1986{
1987 {
1988 Scope newScope = extendCurrentScopeNoPush(true);
1989 // extendCurrentScope(true);
1990 }
1991 "group"
1992 {
1993 String hint = getHint(token);
1994 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1995 gbc.setHashGroupByHint(true);
1996 }
1997 }
1998 "by" (LOOKAHEAD(2) var = Variable()
1999 {
2000 newScope.addNewVarSymbolToScope(var.getVar());
2001 } ":=")?
2002 expr = Expression()
2003 {
2004 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2005 vePairList.add(pair1);
2006 }
2007 ("," ( LOOKAHEAD(2) var = Variable()
2008 {
2009 newScope.addNewVarSymbolToScope(var.getVar());
2010 } ":=")?
2011 expr = Expression()
2012 {
2013 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2014 vePairList.add(pair2);
2015 }
2016 )*
2017 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2018 {
2019 newScope.addNewVarSymbolToScope(decorVar.getVar());
2020 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2021 decorPairList.add(pair3);
2022 }
2023 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2024 {
2025 newScope.addNewVarSymbolToScope(decorVar.getVar());
2026 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2027 decorPairList.add(pair4);
2028 }
2029 )*
2030 )?
2031 "with" withVar = VariableRef()
2032 {
2033 if(withVar.getIsNewVar()==true)
2034 throw new ParseException("can't find variable " + withVar.getVar());
2035 withVarList.add(withVar);
2036 newScope.addNewVarSymbolToScope(withVar.getVar());
2037 }
2038 ("," withVar = VariableRef()
2039 {
2040 if(withVar.getIsNewVar()==true)
2041 throw new ParseException("can't find variable " + withVar.getVar());
2042 withVarList.add(withVar);
2043 newScope.addNewVarSymbolToScope(withVar.getVar());
2044 })*
2045 {
2046 gbc.setGbyPairList(vePairList);
2047 gbc.setDecorPairList(decorPairList);
2048 gbc.setWithVarList(withVarList);
2049 replaceCurrentScope(newScope);
2050 return gbc;
2051 }
2052}
2053
2054
2055LimitClause LimitClause() throws ParseException:
2056{
2057 LimitClause lc = new LimitClause();
2058 Expression expr;
2059 pushForbiddenScope(getCurrentScope());
2060}
2061{
2062 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2063 ("offset" expr = Expression() { lc.setOffset(expr); })?
2064
2065 {
2066 popForbiddenScope();
2067 return lc;
2068 }
2069}
2070
2071DistinctClause DistinctClause() throws ParseException:
2072{
2073 List<Expression> exprs = new ArrayList<Expression>();
2074 Expression expr;
2075}
2076{
2077 "distinct" "by" expr = Expression()
2078 {
2079 exprs.add(expr);
2080 }
2081 ("," expr = Expression()
2082 {
2083 exprs.add(expr);
2084 }
2085 )*
2086 {
2087 return new DistinctClause(exprs);
2088 }
2089}
2090
vinayakb38b7ca42012-03-05 05:44:15 +00002091QuantifiedExpression QuantifiedExpression()throws ParseException:
2092{
2093 QuantifiedExpression qc = new QuantifiedExpression();
2094 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2095 Expression satisfiesExpr;
2096 VariableExpr var;
2097 Expression inExpr;
2098 QuantifiedPair pair;
2099}
2100{
2101 {
2102 createNewScope();
2103 }
2104
2105 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2106 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2107 var = Variable() "in" inExpr = Expression()
2108 {
2109 pair = new QuantifiedPair(var, inExpr);
2110 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2111 quantifiedList.add(pair);
2112 }
2113 (
2114 "," var = Variable() "in" inExpr = Expression()
2115 {
2116 pair = new QuantifiedPair(var, inExpr);
2117 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2118 quantifiedList.add(pair);
2119 }
2120 )*
2121 "satisfies" satisfiesExpr = Expression()
2122 {
2123 qc.setSatisfiesExpr(satisfiesExpr);
2124 qc.setQuantifiedList(quantifiedList);
2125 removeCurrentScope();
2126 return qc;
2127 }
2128}
2129
2130TOKEN_MGR_DECLS:
2131{
2132 public int commentDepth = 0;
2133}
2134
2135<DEFAULT>
2136TOKEN :
2137{
2138 <CARET : "^" >
2139}
2140
2141<DEFAULT>
2142TOKEN :
2143{
2144 <DATASET : "dataset" >
2145}
2146
2147<DEFAULT>
2148TOKEN :
2149{
2150 <LEFTPAREN : "(" >
2151}
2152
2153<DEFAULT>
2154TOKEN :
2155{
2156 <RIGHTPAREN : ")" >
2157}
2158
2159
2160<DEFAULT>
2161TOKEN :
2162{
2163 <INTEGER_LITERAL : (<DIGIT>)+ >
2164}
2165
2166
2167<DEFAULT>
2168TOKEN :
2169{
2170 <NULL : "null">
2171}
2172
2173<DEFAULT>
2174TOKEN :
2175{
2176 <TRUE : "true">
2177}
2178
2179<DEFAULT>
2180TOKEN :
2181{
2182 <FALSE : "false">
2183}
2184
2185<DEFAULT>
2186TOKEN :
2187{
2188 <#DIGIT : ["0" - "9"]>
2189}
2190
2191
2192TOKEN:
2193{
2194 < DOUBLE_LITERAL: <INTEGER>
2195 | <INTEGER> ( "." <INTEGER> )?
2196 | "." <INTEGER>
2197 >
2198 |
2199 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2200 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2201 | "." <INTEGER> ( "f" | "F" )
2202 >
2203 |
2204 <INTEGER : (<DIGIT>)+ >
2205}
2206
2207<DEFAULT>
2208TOKEN :
2209{
2210 <#LETTER : ["A" - "Z", "a" - "z"]>
2211}
2212
2213<DEFAULT>
2214TOKEN :
2215{
2216 <SPECIALCHARS : ["$", "_", "-"] >
2217}
2218
2219<DEFAULT>
2220TOKEN :
2221{
2222 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2223 |
2224 < #EscapeQuot: "\\\"" >
2225 |
2226 < #EscapeApos: "\\\'" >
2227}
2228
2229<DEFAULT>
2230TOKEN :
2231{
2232 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2233}
2234
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002235
vinayakb38b7ca42012-03-05 05:44:15 +00002236<DEFAULT>
2237TOKEN :
2238{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002239 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002240}
2241
2242SKIP:
2243{
2244 " "
2245| "\t"
2246| "\r"
2247| "\n"
2248}
2249
2250SKIP:
2251{
2252 <"//" (~["\n"])* "\n">
2253}
2254
2255SKIP:
2256{
2257 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2258}
2259
2260
2261SKIP:
2262{
2263 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2264}
2265
2266<INSIDE_COMMENT>
2267SPECIAL_TOKEN:
2268{
2269 <"+"(" ")*(~["*"])*>
2270}
2271
2272<INSIDE_COMMENT>
2273SKIP:
2274{
2275 <"/*"> {commentDepth++;}
2276}
2277
2278<INSIDE_COMMENT>
2279SKIP:
2280{
2281 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2282| <~[]>
2283}