blob: c62edcf6ce016fa2ce9d4202ef99b88af2ae4049 [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 {
JIMAHNb75446d2013-06-03 08:35:27 -0700385 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700386 }
387 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
388 {
JIMAHNb75446d2013-06-03 08:35:27 -0700389 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700390 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
ramangrover2993dd8232013-07-03 22:51:25 -0700536 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
537 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -0700538 }
539}
540
541List<String> PrimaryKey() throws ParseException:
542{
Till Westmann14a20a72013-05-09 00:06:24 -0700543 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700544 List<String> primaryKeyFields = new ArrayList<String>();
545}
546{
Till Westmann14a20a72013-05-09 00:06:24 -0700547 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700548 {
Till Westmann14a20a72013-05-09 00:06:24 -0700549 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700550 }
Till Westmann14a20a72013-05-09 00:06:24 -0700551 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700552 {
Till Westmann14a20a72013-05-09 00:06:24 -0700553 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700554 }
555 )*
556 {
557 return primaryKeyFields;
558 }
559}
560
561Statement DropStatement() throws ParseException:
562{
Till Westmann14a20a72013-05-09 00:06:24 -0700563 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700564 Pair<Identifier,Identifier> pairId = null;
565 Triple<Identifier,Identifier,Identifier> tripleId = null;
566 FunctionSignature funcSig = null;
567 boolean ifExists = false;
568 Statement stmt = null;
569}
570{
571 "drop"
572 (
573 <DATASET> pairId = QualifiedName() ifExists = IfExists()
574 {
575 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
576 }
577 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
578 {
579 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
580 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700581 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700582 {
Till Westmann14a20a72013-05-09 00:06:24 -0700583 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700584 }
ramangrover299f76a5e2013-06-18 10:25:17 -0700585 | "type" pairId = TypeName() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700586 {
587 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
588 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700589 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700590 {
Till Westmann14a20a72013-05-09 00:06:24 -0700591 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700592 }
593 | "function" funcSig = FunctionSignature() ifExists = IfExists()
594 {
595 stmt = new FunctionDropStatement(funcSig, ifExists);
596 }
597 )
598 {
599 return stmt;
600 }
601}
602
603boolean IfExists() throws ParseException :
604{
605}
606{
607 ( "if" "exists"
608 {
609 return true;
610 }
611 )?
612 {
613 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000614 }
615}
616
617InsertStatement InsertStatement() throws ParseException:
618{
Till Westmann31c21f92013-05-08 09:21:53 -0700619 Pair<Identifier,Identifier> nameComponents = null;
620 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000621}
622{
Till Westmann31c21f92013-05-08 09:21:53 -0700623 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
624 {
625 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
626 }
vinayakb38b7ca42012-03-05 05:44:15 +0000627}
628
629DeleteStatement DeleteStatement() throws ParseException:
630{
Till Westmann31c21f92013-05-08 09:21:53 -0700631 VariableExpr var = null;
632 Expression condition = null;
633 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000634}
635{
Till Westmann31c21f92013-05-08 09:21:53 -0700636 "delete" var = Variable()
637 {
638 getCurrentScope().addNewVarSymbolToScope(var.getVar());
639 }
640 "from" <DATASET> nameComponents = QualifiedName()
641 ("where" condition = Expression())?
642 {
643 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
644 }
vinayakb38b7ca42012-03-05 05:44:15 +0000645}
646
647UpdateStatement UpdateStatement() throws ParseException:
648{
Till Westmann31c21f92013-05-08 09:21:53 -0700649 VariableExpr vars;
650 Expression target;
651 Expression condition;
652 UpdateClause uc;
653 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000654}
655{
Till Westmann31c21f92013-05-08 09:21:53 -0700656 "update" vars = Variable() "in" target = Expression()
657 "where" condition = Expression()
658 <LEFTPAREN> (uc = UpdateClause()
659 {
660 ucs.add(uc);
661 }
662 ("," uc = UpdateClause()
663 {
664 ucs.add(uc);
665 }
666 )*) <RIGHTPAREN>
667 {
668 return new UpdateStatement(vars, target, condition, ucs);
669 }
vinayakb38b7ca42012-03-05 05:44:15 +0000670}
671
vinayakb38b7ca42012-03-05 05:44:15 +0000672UpdateClause UpdateClause() throws ParseException:
673{
Till Westmann31c21f92013-05-08 09:21:53 -0700674 Expression target = null;
675 Expression value = null ;
676 InsertStatement is = null;
677 DeleteStatement ds = null;
678 UpdateStatement us = null;
679 Expression condition = null;
680 UpdateClause ifbranch = null;
681 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000682}
683{
684 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700685 | is = InsertStatement()
686 | ds = DeleteStatement()
687 | us = UpdateStatement()
688 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
689 "then" ifbranch = UpdateClause()
690 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
691 {
692 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
693 }
vinayakb38b7ca42012-03-05 05:44:15 +0000694}
695
vinayakb38b7ca42012-03-05 05:44:15 +0000696Statement SetStatement() throws ParseException:
697{
698 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700699 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000700}
701{
Till Westmann7d535322013-05-09 00:40:02 -0700702 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700703 {
Till Westmann31c21f92013-05-08 09:21:53 -0700704 return new SetStatement(pn, pv);
705 }
vinayakb38b7ca42012-03-05 05:44:15 +0000706}
707
708Statement WriteStatement() throws ParseException:
709{
Till Westmann14a20a72013-05-09 00:06:24 -0700710 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000711 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000712 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 Westmann35a0f702013-07-01 14:06:34 -0700717 "write" "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
Till Westmann7d535322013-05-09 00:40:02 -0700718 ( "using" writerClass = StringLiteral() )?
Till Westmann35a0f702013-07-01 14:06:34 -0700719 {
720 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
vinayakb38b7ca42012-03-05 05:44:15 +0000721 }
722}
723
vinayakb38b7ca42012-03-05 05:44:15 +0000724LoadFromFileStatement LoadStatement() throws ParseException:
725{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000726 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000727 Identifier datasetName = null;
728 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000729 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000730 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000731 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000732}
733{
Till Westmann31c21f92013-05-08 09:21:53 -0700734 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000735 {
Till Westmann31c21f92013-05-08 09:21:53 -0700736 dataverseName = nameComponents.first;
737 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000738 }
Till Westmann31c21f92013-05-08 09:21:53 -0700739 "using" adapterName = AdapterName() properties = Configuration()
740 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000741 {
Till Westmann31c21f92013-05-08 09:21:53 -0700742 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000743 }
744 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700745 {
746 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
747 }
vinayakb38b7ca42012-03-05 05:44:15 +0000748}
749
vinayakb38b7ca42012-03-05 05:44:15 +0000750
Till Westmann31c21f92013-05-08 09:21:53 -0700751String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000752{
ramangrover29669d8f62013-02-11 06:03:32 +0000753 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000754}
755{
Till Westmann68d99652013-05-09 11:15:21 -0700756 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000757 {
Till Westmann7d535322013-05-09 00:40:02 -0700758 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000759 }
vinayakb38b7ca42012-03-05 05:44:15 +0000760}
761
Till Westmann31c21f92013-05-08 09:21:53 -0700762Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000763{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000764 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700765 Map<String,String> configuration = null;
766 Statement stmt = null;
ramangrover29566b3a92013-05-28 09:07:10 -0700767 String policy = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000768}
769{
Till Westmann31c21f92013-05-08 09:21:53 -0700770 (
ramangrover29566b3a92013-05-28 09:07:10 -0700771 "begin" "feed" nameComponents = QualifiedName() (policy = GetPolicy())?
Till Westmann31c21f92013-05-08 09:21:53 -0700772 {
ramangrover29566b3a92013-05-28 09:07:10 -0700773 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, policy, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700774 }
775 | "suspend" "feed" nameComponents = QualifiedName()
776 {
777 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
778 }
779 | "resume" "feed" nameComponents = QualifiedName()
780 {
781 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
782 }
783 | "end" "feed" nameComponents = QualifiedName()
784 {
785 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
786 }
787 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
788 {
789 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
790 }
791 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000792 {
Till Westmann31c21f92013-05-08 09:21:53 -0700793 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000794 }
795}
796
Till Westmann31c21f92013-05-08 09:21:53 -0700797Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000798{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000799 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700800 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000801}
802{
Till Westmann31c21f92013-05-08 09:21:53 -0700803 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000804 {
Till Westmann31c21f92013-05-08 09:21:53 -0700805 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000806 }
Till Westmann31c21f92013-05-08 09:21:53 -0700807 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000808 {
Till Westmann31c21f92013-05-08 09:21:53 -0700809 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000810 }
Till Westmann31c21f92013-05-08 09:21:53 -0700811 )* )? <RIGHTPAREN>
812 {
813 return configuration;
814 }
815}
816
817Pair<String, String> KeyValuePair() throws ParseException:
818{
819 String key;
820 String value;
821}
822{
Till Westmann7d535322013-05-09 00:40:02 -0700823 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700824 {
825 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000826 }
Till Westmann31c21f92013-05-08 09:21:53 -0700827}
828
829Map<String,String> Properties() throws ParseException:
830{
831 Map<String,String> properties = new HashMap<String,String>();
832 Pair<String, String> property;
833}
834{
835 ( <LEFTPAREN> property = Property()
836 {
837 properties.put(property.first, property.second);
838 }
839 ( "," property = Property()
840 {
841 properties.put(property.first, property.second);
842 }
843 )* <RIGHTPAREN> )?
844 {
845 return properties;
846 }
847}
848
849Pair<String, String> Property() throws ParseException:
850{
851 String key;
852 String value;
853}
854{
Till Westmann7d535322013-05-09 00:40:02 -0700855 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700856 {
857 try {
858 value = "" + Long.valueOf(token.image);
859 } catch (NumberFormatException nfe) {
860 throw new ParseException("inapproriate value: " + token.image);
861 }
862 }
863 )
864 {
865 return new Pair<String, String>(key.toUpperCase(), value);
866 }
vinayakb38b7ca42012-03-05 05:44:15 +0000867}
868
869TypeExpression TypeExpr() throws ParseException:
870{
871 TypeExpression typeExpr = null;
872}
873{
874 (
875 typeExpr = RecordTypeDef()
876 | typeExpr = TypeReference()
877 | typeExpr = OrderedListTypeDef()
878 | typeExpr = UnorderedListTypeDef()
879 )
880 {
881 return typeExpr;
882 }
883}
884
885RecordTypeDefinition RecordTypeDef() throws ParseException:
886{
887 RecordTypeDefinition recType = new RecordTypeDefinition();
888 RecordTypeDefinition.RecordKind recordKind = null;
889}
890{
891 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
892 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
893 "{"
894 {
895 String hint = getHint(token);
896 if (hint != null) {
897 String splits[] = hint.split(" +");
898 if (splits[0].equals(GEN_FIELDS_HINT)) {
899 if (splits.length != 5) {
900 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
901 }
902 if (!splits[1].equals("int")) {
903 throw new ParseException("The only supported type for gen-fields is int.");
904 }
905 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
906 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
907 recType.setUndeclaredFieldsDataGen(ufdg);
908 }
909 }
910
911 }
912 (
913 RecordField(recType)
914 ( "," RecordField(recType) )*
915 )?
916 "}"
917 {
918 if (recordKind == null) {
919 recordKind = RecordTypeDefinition.RecordKind.OPEN;
920 }
921 recType.setRecordKind(recordKind);
922 return recType;
923 }
924}
925
926void RecordField(RecordTypeDefinition recType) throws ParseException:
927{
928 String fieldName;
929 TypeExpression type = null;
930 boolean nullable = false;
931}
932{
Till Westmann14a20a72013-05-09 00:06:24 -0700933 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000934 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700935 fieldName = token.image;
936 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000937 IRecordFieldDataGen rfdg = null;
938 if (hint != null) {
939 String splits[] = hint.split(" +");
940 if (splits[0].equals(VAL_FILE_HINT)) {
941 File[] valFiles = new File[splits.length - 1];
942 for (int k=1; k<splits.length; k++) {
943 valFiles[k-1] = new File(splits[k]);
944 }
945 rfdg = new FieldValFileDataGen(valFiles);
946 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
947 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
948 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
949 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
950 } else if (splits[0].equals(LIST_HINT)) {
951 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
952 } else if (splits[0].equals(INTERVAL_HINT)) {
953 FieldIntervalDataGen.ValueType vt;
954 if (splits[1].equals("int")) {
955 vt = FieldIntervalDataGen.ValueType.INT;
956 } else if (splits[1].equals("long")) {
957 vt = FieldIntervalDataGen.ValueType.LONG;
958 } else if (splits[1].equals("float")) {
959 vt = FieldIntervalDataGen.ValueType.FLOAT;
960 } else if (splits[1].equals("double")) {
961 vt = FieldIntervalDataGen.ValueType.DOUBLE;
962 } else {
963 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
964 }
965 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
966 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
967 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
968 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
969 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
970 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
971 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
972 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
973 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
974 } else if (splits[0].equals(AUTO_HINT)) {
975 rfdg = new AutoDataGen(splits[1]);
976 }
977 }
978 }
979 ":"
980 ( type = TypeExpr() )
981 ("?" { nullable = true; } )?
982 {
983 recType.addField(fieldName, type, nullable, rfdg);
984 }
985}
986
987TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000988{
Till Westmann14a20a72013-05-09 00:06:24 -0700989 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700990}
991{
992 id = Identifier()
993 {
Till Westmann14a20a72013-05-09 00:06:24 -0700994 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700995 }
vinayakb38b7ca42012-03-05 05:44:15 +0000996}
997
998OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
999{
1000 TypeExpression type = null;
1001}
1002{
1003 "["
1004 ( type = TypeExpr() )
1005 "]"
1006 {
1007 return new OrderedListTypeDefinition(type);
1008 }
1009}
1010
1011
1012UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1013{
1014 TypeExpression type = null;
1015}
1016{
1017 "{{"
1018 ( type = TypeExpr() )
1019 "}}"
1020 {
1021 return new UnorderedListTypeDefinition(type);
1022 }
1023}
1024
ramangrover299f76a5e2013-06-18 10:25:17 -07001025FunctionName FunctionName() throws ParseException:
1026{
1027 String first = null;
1028 String second = null;
1029 String third = null;
1030 boolean secondAfterDot = false;
1031}
1032{
1033 first = Identifier() ( "." second = Identifier()
1034 {
1035 secondAfterDot = true;
1036 }
1037 ("#" third = Identifier())? | "#" second = Identifier() )?
1038 {
1039 FunctionName result = new FunctionName();
1040 if (second == null) {
1041 result.dataverse = defaultDataverse;
1042 result.library = null;
1043 result.function = first;
1044 } else if (third == null) {
1045 if (secondAfterDot) {
1046 result.dataverse = first;
1047 result.library = null;
1048 result.function = second;
1049 } else {
1050 result.dataverse = defaultDataverse;
1051 result.library = first;
1052 result.function = second;
1053 }
1054 } else {
1055 result.dataverse = first;
1056 result.library = second;
1057 result.function = third;
1058 }
1059 return result;
1060 }
1061}
Till Westmann31c21f92013-05-08 09:21:53 -07001062
ramangrover299f76a5e2013-06-18 10:25:17 -07001063
1064Pair<Identifier,Identifier> TypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001065{
Till Westmann31c21f92013-05-08 09:21:53 -07001066 Pair<Identifier,Identifier> name = null;
1067}
1068{
1069 name = QualifiedName()
1070 {
1071 if (name.first == null) {
1072 name.first = new Identifier(defaultDataverse);
1073 }
1074 return name;
1075 }
1076}
1077
Till Westmann14a20a72013-05-09 00:06:24 -07001078String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001079{
Till Westmann68d99652013-05-09 11:15:21 -07001080 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001081}
1082{
1083 <IDENTIFIER>
1084 {
Till Westmann14a20a72013-05-09 00:06:24 -07001085 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001086 }
Till Westmann68d99652013-05-09 11:15:21 -07001087 | lit = StringLiteral()
1088 {
1089 return lit;
1090 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001091}
1092
Till Westmann7d535322013-05-09 00:40:02 -07001093String StringLiteral() throws ParseException:
1094{
1095}
1096{
1097 <STRING_LITERAL>
1098 {
1099 return removeQuotesAndEscapes(token.image);
1100 }
1101}
1102
Till Westmann31c21f92013-05-08 09:21:53 -07001103Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1104{
Till Westmann14a20a72013-05-09 00:06:24 -07001105 String first = null;
1106 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001107}
1108{
Till Westmanna4242bc2013-05-08 17:49:55 -07001109 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001110 {
Till Westmann14a20a72013-05-09 00:06:24 -07001111 Identifier id1 = null;
1112 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001113 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001114 id2 = new Identifier(first);
1115 } else
1116 {
1117 id1 = new Identifier(first);
1118 id2 = new Identifier(second);
1119 }
1120 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001121 }
1122}
1123
Till Westmann31c21f92013-05-08 09:21:53 -07001124Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001125{
Till Westmann14a20a72013-05-09 00:06:24 -07001126 String first = null;
1127 String second = null;
1128 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001129}
1130{
Till Westmanna4242bc2013-05-08 17:49:55 -07001131 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001132 {
Till Westmann14a20a72013-05-09 00:06:24 -07001133 Identifier id1 = null;
1134 Identifier id2 = null;
1135 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001136 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001137 id2 = new Identifier(first);
1138 id3 = new Identifier(second);
1139 } else {
1140 id1 = new Identifier(first);
1141 id2 = new Identifier(second);
1142 id3 = new Identifier(third);
1143 }
1144 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001145 }
1146}
1147
vinayakb38b7ca42012-03-05 05:44:15 +00001148FunctionDecl FunctionDeclaration() throws ParseException:
1149{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001150 FunctionDecl funcDecl;
1151 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001152 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001153 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1154 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001155 createNewScope();
1156}
1157{
Till Westmannd7dcb122013-05-16 13:19:09 -07001158 "declare" "function" functionName = Identifier()
1159 paramList = ParameterList()
1160 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001161 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001162 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001163 getCurrentScope().addFunctionDescriptor(signature, false);
1164 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001165 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001166 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001167 }
1168}
1169
vinayakb38b7ca42012-03-05 05:44:15 +00001170
Till Westmann31c21f92013-05-08 09:21:53 -07001171Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001172{
1173 Query query = new Query();
1174 Expression expr;
1175}
1176{
Till Westmann31c21f92013-05-08 09:21:53 -07001177 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001178 {
1179 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001180 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001181 return query;
1182 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001183
vinayakb38b7ca42012-03-05 05:44:15 +00001184}
1185
1186
1187
1188Expression Expression():
1189{
1190 Expression expr = null;
1191 Expression exprP = null;
1192}
1193{
1194(
1195
1196//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1197 expr = OperatorExpr()
1198 | expr = IfThenElse()
1199 | expr = FLWOGR()
1200 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001201
vinayakb38b7ca42012-03-05 05:44:15 +00001202
1203)
1204 {
1205 return (exprP==null) ? expr : exprP;
1206 }
1207}
1208
1209
1210
1211Expression OperatorExpr()throws ParseException:
1212{
1213 OperatorExpr op = null;
1214 Expression operand = null;
1215}
1216{
1217 operand = AndExpr()
1218 (
1219
1220 "or"
1221 {
1222 if (op == null) {
1223 op = new OperatorExpr();
1224 op.addOperand(operand);
1225 op.setCurrentop(true);
1226 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001227 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001228 }
1229
1230 operand = AndExpr()
1231 {
1232 op.addOperand(operand);
1233 }
1234
1235 )*
1236
1237 {
1238 return op==null? operand: op;
1239 }
1240}
1241
1242Expression AndExpr()throws ParseException:
1243{
1244 OperatorExpr op = null;
1245 Expression operand = null;
1246}
1247{
1248 operand = RelExpr()
1249 (
1250
1251 "and"
1252 {
1253 if (op == null) {
1254 op = new OperatorExpr();
1255 op.addOperand(operand);
1256 op.setCurrentop(true);
1257 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001258 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001259 }
1260
1261 operand = RelExpr()
1262 {
1263 op.addOperand(operand);
1264 }
1265
1266 )*
1267
1268 {
1269 return op==null? operand: op;
1270 }
1271}
1272
1273
1274
1275Expression RelExpr()throws ParseException:
1276{
1277 OperatorExpr op = null;
1278 Expression operand = null;
1279 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001280 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001281}
1282{
1283 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001284 {
1285 if (operand instanceof VariableExpr) {
1286 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001287 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001288 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001289 }
1290 }
1291 }
1292
1293 (
1294 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1295 {
alexander.behm07617fd2012-07-25 10:13:50 +00001296 String mhint = getHint(token);
1297 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1298 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1299 }
vinayakb38b7ca42012-03-05 05:44:15 +00001300 if (op == null) {
1301 op = new OperatorExpr();
1302 op.addOperand(operand, broadcast);
1303 op.setCurrentop(true);
1304 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001305 }
1306 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001307 }
1308
1309 operand = AddExpr()
1310 {
alexander.behm07617fd2012-07-25 10:13:50 +00001311 broadcast = false;
1312 if (operand instanceof VariableExpr) {
1313 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001314 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1315 broadcast = true;
1316 }
alexander.behm07617fd2012-07-25 10:13:50 +00001317 }
vinayakb38b7ca42012-03-05 05:44:15 +00001318 op.addOperand(operand, broadcast);
1319 }
1320 )?
1321
1322 {
alexander.behm07617fd2012-07-25 10:13:50 +00001323 if (annotation != null) {
1324 op.addHint(annotation);
1325 }
vinayakb38b7ca42012-03-05 05:44:15 +00001326 return op==null? operand: op;
1327 }
1328}
1329
1330Expression AddExpr()throws ParseException:
1331{
1332 OperatorExpr op = null;
1333 Expression operand = null;
1334}
1335{
1336 operand = MultExpr()
1337
1338 ( ("+" | "-")
1339 {
1340 if (op == null) {
1341 op = new OperatorExpr();
1342 op.addOperand(operand);
1343 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001344 }
1345 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001346 }
1347
1348 operand = MultExpr()
1349 {
1350 op.addOperand(operand);
1351 }
1352 )*
1353
1354 {
1355 return op==null? operand: op;
1356 }
1357}
1358
1359Expression MultExpr()throws ParseException:
1360{
1361 OperatorExpr op = null;
1362 Expression operand = null;
1363}
1364{
1365 operand = UnionExpr()
1366
1367 (( "*" | "/" | "%" | <CARET> | "idiv")
1368 {
1369 if (op == null) {
1370 op = new OperatorExpr();
1371 op.addOperand(operand);
1372 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001373 }
1374 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001375 }
1376 operand = UnionExpr()
1377 {
1378 op.addOperand(operand);
1379 }
1380 )*
1381
1382 {
1383 return op==null?operand:op;
1384 }
1385}
1386
1387Expression UnionExpr() throws ParseException:
1388{
1389 UnionExpr union = null;
1390 Expression operand1 = null;
1391 Expression operand2 = null;
1392}
1393{
1394 operand1 = UnaryExpr()
1395 ("union"
1396 (operand2 = UnaryExpr()) {
1397 if (union == null) {
1398 union = new UnionExpr();
1399 union.addExpr(operand1);
1400 }
1401 union.addExpr(operand2);
1402 } )*
1403 {
1404 return (union == null)? operand1: union;
1405 }
1406}
1407
1408Expression UnaryExpr() throws ParseException:
1409{
1410 Expression uexpr = null;
1411 Expression expr = null;
1412}
1413{
1414 (( "+"|"-")
1415 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001416 uexpr = new UnaryExpr();
1417 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001418 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001419 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001420 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1421 else
1422 throw new ParseException();
1423 }
1424 )?
1425
1426 expr = ValueExpr()
1427 {
1428 if(uexpr!=null){
1429 ((UnaryExpr)uexpr).setExpr(expr);
1430 return uexpr;
1431 }
1432 else{
1433 return expr;
1434 }
1435 }
1436}
1437
Till Westmann04478e72013-05-13 23:01:34 -07001438Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001439{
1440 Expression expr = null;
1441 Identifier ident = null;
1442 AbstractAccessor fa = null;
1443 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001444}
1445{
Till Westmann04478e72013-05-13 23:01:34 -07001446 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001447 {
Till Westmann04478e72013-05-13 23:01:34 -07001448 fa = (fa == null ? new FieldAccessor(expr, ident)
1449 : new FieldAccessor(fa, ident));
1450 }
1451 | index = Index()
1452 {
1453 fa = (fa == null ? new IndexAccessor(expr, index)
1454 : new IndexAccessor(fa, index));
1455 }
1456 )*
1457 {
1458 return fa == null ? expr : fa;
1459 }
vinayakb38b7ca42012-03-05 05:44:15 +00001460}
1461
1462Identifier Field() throws ParseException:
1463{
Till Westmann14a20a72013-05-09 00:06:24 -07001464 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001465}
1466{
Till Westmanna4242bc2013-05-08 17:49:55 -07001467 "." ident = Identifier()
1468 {
Till Westmann14a20a72013-05-09 00:06:24 -07001469 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001470 }
vinayakb38b7ca42012-03-05 05:44:15 +00001471}
1472
1473int Index() throws ParseException:
1474{
1475 Expression expr = null;
1476 int idx = -2;
1477}
1478{
1479 "[" ( expr = Expression()
1480 {
1481 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1482 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001483 Literal lit = ((LiteralExpr)expr).getValue();
1484 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1485 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001486 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001487 }
vinayakb38b7ca42012-03-05 05:44:15 +00001488 else {
1489 throw new ParseException("Index should be an INTEGER");
1490 }
1491 }
1492
1493 }
1494
1495 | "?"
1496 {
1497 idx = IndexAccessor.ANY;
1498 // ANY
1499 }
1500
1501 )
1502
1503 "]"
1504 {
1505 return idx;
1506 }
1507}
1508
1509
1510Expression PrimaryExpr()throws ParseException:
1511{
1512 Expression expr = null;
1513}
1514{
Till Westmann68d99652013-05-09 11:15:21 -07001515 ( LOOKAHEAD(2)
1516 expr = FunctionCallExpr()
1517 | expr = Literal()
1518 | expr = DatasetAccessExpression()
1519 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001520 {
1521 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001522 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001523 }
Till Westmann68d99652013-05-09 11:15:21 -07001524 | expr = ListConstructor()
1525 | expr = RecordConstructor()
1526 | expr = ParenthesizedExpression()
1527 )
1528 {
1529 return expr;
1530 }
vinayakb38b7ca42012-03-05 05:44:15 +00001531}
1532
1533Expression Literal() throws ParseException:
1534{
vinayakb38b7ca42012-03-05 05:44:15 +00001535 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001536 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001537}
1538{
Till Westmann7d535322013-05-09 00:40:02 -07001539 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001540 {
Till Westmann7d535322013-05-09 00:40:02 -07001541 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001542 }
1543 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001544 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001545 try {
1546 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1547 } catch(NumberFormatException ex) {
1548 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1549 }
1550 }
1551 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001552 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001553 lit.setValue(new FloatLiteral(new Float(token.image)));
1554 }
1555 | <DOUBLE_LITERAL>
1556 {
1557 lit.setValue(new DoubleLiteral(new Double(token.image)));
1558 }
1559 | <NULL>
1560 {
1561 lit.setValue(NullLiteral.INSTANCE);
1562 }
1563 | <TRUE>
1564 {
1565 lit.setValue(TrueLiteral.INSTANCE);
1566 }
1567 | <FALSE>
1568 {
1569 lit.setValue(FalseLiteral.INSTANCE);
1570 }
1571 )
vinayakb38b7ca42012-03-05 05:44:15 +00001572 {
1573 return lit;
1574 }
1575}
1576
1577
1578VariableExpr VariableRef() throws ParseException:
1579{
1580 VariableExpr varExp = new VariableExpr();
1581 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001582}
1583{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001584 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001585 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001586 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001587 Identifier ident = lookupSymbol(varName);
1588 if (isInForbiddenScopes(varName)) {
1589 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.");
1590 }
1591 if(ident != null) { // exist such ident
1592 varExp.setIsNewVar(false);
1593 varExp.setVar((VarIdentifier)ident);
1594 } else {
1595 varExp.setVar(var);
1596 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001597 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001598 return varExp;
1599 }
1600}
1601
1602
1603VariableExpr Variable() throws ParseException:
1604{
1605 VariableExpr varExp = new VariableExpr();
1606 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001607}
1608{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001609 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001610 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001611 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001612 if(ident != null) { // exist such ident
1613 varExp.setIsNewVar(false);
1614 }
1615 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001616 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001617 return varExp;
1618 }
1619}
1620
1621Expression ListConstructor() throws ParseException:
1622{
1623 Expression expr = null;
1624}
1625{
1626 (
1627 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1628 )
1629
1630 {
1631 return expr;
1632 }
1633}
1634
1635
1636ListConstructor OrderedListConstructor() throws ParseException:
1637{
1638 ListConstructor expr = new ListConstructor();
1639 Expression tmp = null;
1640 List<Expression> exprList = new ArrayList<Expression>();
1641 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1642}
1643{
1644
1645 "["
1646 ( tmp = Expression()
1647 {
1648 exprList.add(tmp);
1649 }
1650
1651 ("," tmp = Expression() { exprList.add(tmp); })*
1652 )?
1653
1654 "]"
1655
1656 {
1657 expr.setExprList(exprList);
1658 return expr;
1659 }
1660}
1661
1662ListConstructor UnorderedListConstructor() throws ParseException:
1663{
1664 ListConstructor expr = new ListConstructor();
1665 Expression tmp = null;
1666 List<Expression> exprList = new ArrayList<Expression>();
1667 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1668}
1669{
1670
1671 "{{" ( tmp = Expression()
1672 {
1673 exprList.add(tmp);
1674 }
1675 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1676 {
1677 expr.setExprList(exprList);
1678 return expr;
1679 }
1680}
1681
1682RecordConstructor RecordConstructor() throws ParseException:
1683{
1684 RecordConstructor expr = new RecordConstructor();
1685 FieldBinding tmp = null;
1686 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1687}
1688{
1689 "{" (tmp = FieldBinding()
1690 {
1691 fbList.add(tmp);
1692 }
1693 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1694 {
1695 expr.setFbList(fbList);
1696 return expr;
1697 }
1698}
1699
1700FieldBinding FieldBinding() throws ParseException:
1701{
1702 FieldBinding fb = new FieldBinding();
1703 Expression left, right;
1704}
1705{
1706 left = Expression() ":" right = Expression()
1707 {
1708 fb.setLeftExpr(left);
1709 fb.setRightExpr(right);
1710 return fb;
1711 }
1712}
1713
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001714
vinayakb38b7ca42012-03-05 05:44:15 +00001715Expression FunctionCallExpr() throws ParseException:
1716{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001717 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001718 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001719 Expression tmp;
1720 int arity = 0;
ramangrover299f76a5e2013-06-18 10:25:17 -07001721 FunctionName funcName = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001722 String hint = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001723}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001724{
ramangrover299f76a5e2013-06-18 10:25:17 -07001725 funcName = FunctionName()
vinayakb38b7ca42012-03-05 05:44:15 +00001726 {
Till Westmann31c21f92013-05-08 09:21:53 -07001727 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001728 }
Till Westmann31c21f92013-05-08 09:21:53 -07001729 <LEFTPAREN> (tmp = Expression()
1730 {
1731 argList.add(tmp);
1732 arity ++;
1733 }
1734 ("," tmp = Expression()
1735 {
1736 argList.add(tmp);
1737 arity++;
1738 }
1739 )*)? <RIGHTPAREN>
1740 {
ramangrover299f76a5e2013-06-18 10:25:17 -07001741 // TODO use funcName.library
ramangrover29bdba1a82013-06-21 17:17:52 -07001742 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
ramangrover299f76a5e2013-06-18 10:25:17 -07001743 FunctionSignature signature
ramangrover29bdba1a82013-06-21 17:17:52 -07001744 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001745 if (signature == null) {
ramangrover29bdba1a82013-06-21 17:17:52 -07001746 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001747 }
1748 callExpr = new CallExpr(signature,argList);
1749 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1750 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1751 }
1752 return callExpr;
1753 }
vinayakb38b7ca42012-03-05 05:44:15 +00001754}
1755
ramangrover29@gmail.com5f248e12013-04-11 01:03:09 +00001756
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001757Expression DatasetAccessExpression() throws ParseException:
1758{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001759 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001760 String arg1 = null;
1761 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001762 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001763}
1764{
Till Westmann14a20a72013-05-09 00:06:24 -07001765 <DATASET>
1766 {
Till Westmann14a20a72013-05-09 00:06:24 -07001767 funcName = token.image;
1768 }
1769 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1770 {
1771 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001772 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001773 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001774 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001775 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001776 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001777 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001778 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1779 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1780 if (signature == null) {
1781 signature = new FunctionSignature(dataverse, funcName, 1);
1782 }
1783 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001784 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001785 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001786 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001787}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001788
vinayakb38b7ca42012-03-05 05:44:15 +00001789Expression ParenthesizedExpression() throws ParseException:
1790{
1791 Expression expr;
1792}
1793{
1794 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1795 {
1796 return expr;
1797 }
1798}
1799
1800Expression IfThenElse() throws ParseException:
1801{
1802 Expression condExpr;
1803 Expression thenExpr;
1804 Expression elseExpr;
1805 IfExpr ifExpr = new IfExpr();
1806}
1807{
1808 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1809
1810 {
1811 ifExpr.setCondExpr(condExpr);
1812 ifExpr.setThenExpr(thenExpr);
1813 ifExpr.setElseExpr(elseExpr);
1814 return ifExpr;
1815 }
1816}
1817
1818Expression FLWOGR() throws ParseException:
1819{
1820 FLWOGRExpression flworg = new FLWOGRExpression();
1821 List<Clause> clauseList = new ArrayList<Clause>();
1822 Expression returnExpr;
1823 Clause tmp;
1824 createNewScope();
1825}
1826{
1827 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1828 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1829
1830 {
1831 flworg.setClauseList(clauseList);
1832 flworg.setReturnExpr(returnExpr);
1833 removeCurrentScope();
1834 return flworg;
1835 }
1836}
1837
1838Clause Clause()throws ParseException :
1839{
1840 Clause clause;
1841}
1842{
1843 (
1844 clause = ForClause()
1845 | clause = LetClause()
1846 | clause = WhereClause()
1847 | clause = OrderbyClause()
1848 | clause = GroupClause()
1849 | clause = LimitClause()
1850 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001851 )
1852 {
1853 return clause;
1854 }
1855}
1856
1857Clause ForClause()throws ParseException :
1858{
1859 ForClause fc = new ForClause();
1860 VariableExpr varExp;
1861 VariableExpr varPos = null;
1862 Expression inExp;
1863 extendCurrentScope();
1864}
1865{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001866 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001867 {
1868 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001869 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001870 fc.setInExpr(inExp);
1871 if (varPos != null) {
1872 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001873 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001874 }
1875 return fc;
1876 }
1877}
1878
1879Clause LetClause() throws ParseException:
1880{
1881 LetClause lc = new LetClause();
1882 VariableExpr varExp;
1883 Expression beExp;
1884 extendCurrentScope();
1885}
1886{
ilovesoupb2527c12012-07-12 03:21:13 +00001887 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001888 {
1889 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001890 lc.setVarExpr(varExp);
1891 lc.setBeExpr(beExp);
1892 return lc;
1893 }
1894}
1895
1896Clause WhereClause()throws ParseException :
1897{
1898 WhereClause wc = new WhereClause();
1899 Expression whereExpr;
1900}
1901{
1902 "where" whereExpr = Expression()
1903 {
1904 wc.setWhereExpr(whereExpr);
1905 return wc;
1906 }
1907}
1908
1909Clause OrderbyClause()throws ParseException :
1910{
1911 OrderbyClause oc = new OrderbyClause();
1912 Expression orderbyExpr;
1913 List<Expression> orderbyList = new ArrayList<Expression>();
1914 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1915 int numOfOrderby = 0;
1916}
1917{
1918 (
1919 "order"
1920 {
1921 String hint = getHint(token);
1922 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1923 String splits[] = hint.split(" +");
1924 int numFrames = Integer.parseInt(splits[1]);
1925 int numTuples = Integer.parseInt(splits[2]);
1926 oc.setNumFrames(numFrames);
1927 oc.setNumTuples(numTuples);
1928 }
1929 }
1930 "by" orderbyExpr = Expression()
1931 {
1932 orderbyList.add(orderbyExpr);
1933 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1934 }
1935 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1936 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1937 {
1938 modifierList.add(modif);
1939 }
1940
1941 ("," orderbyExpr = Expression()
1942 {
1943 orderbyList.add(orderbyExpr);
1944 modif = OrderbyClause.OrderModifier.ASC;
1945 }
1946 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1947 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1948 {
1949 modifierList.add(modif);
1950 }
1951 )*
1952)
1953 {
1954 oc.setModifierList(modifierList);
1955 oc.setOrderbyList(orderbyList);
1956 return oc;
1957 }
1958}
1959Clause GroupClause()throws ParseException :
1960{
1961 GroupbyClause gbc = new GroupbyClause();
1962 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1963 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1964 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1965 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1966 VariableExpr var = null;
1967 VariableExpr withVar = null;
1968 Expression expr = null;
1969 VariableExpr decorVar = null;
1970 Expression decorExpr = null;
1971}
1972{
1973 {
1974 Scope newScope = extendCurrentScopeNoPush(true);
1975 // extendCurrentScope(true);
1976 }
1977 "group"
1978 {
1979 String hint = getHint(token);
1980 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1981 gbc.setHashGroupByHint(true);
1982 }
1983 }
1984 "by" (LOOKAHEAD(2) var = Variable()
1985 {
1986 newScope.addNewVarSymbolToScope(var.getVar());
1987 } ":=")?
1988 expr = Expression()
1989 {
1990 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1991 vePairList.add(pair1);
1992 }
1993 ("," ( LOOKAHEAD(2) var = Variable()
1994 {
1995 newScope.addNewVarSymbolToScope(var.getVar());
1996 } ":=")?
1997 expr = Expression()
1998 {
1999 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2000 vePairList.add(pair2);
2001 }
2002 )*
2003 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2004 {
2005 newScope.addNewVarSymbolToScope(decorVar.getVar());
2006 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2007 decorPairList.add(pair3);
2008 }
2009 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2010 {
2011 newScope.addNewVarSymbolToScope(decorVar.getVar());
2012 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2013 decorPairList.add(pair4);
2014 }
2015 )*
2016 )?
2017 "with" withVar = VariableRef()
2018 {
2019 if(withVar.getIsNewVar()==true)
2020 throw new ParseException("can't find variable " + withVar.getVar());
2021 withVarList.add(withVar);
2022 newScope.addNewVarSymbolToScope(withVar.getVar());
2023 }
2024 ("," withVar = VariableRef()
2025 {
2026 if(withVar.getIsNewVar()==true)
2027 throw new ParseException("can't find variable " + withVar.getVar());
2028 withVarList.add(withVar);
2029 newScope.addNewVarSymbolToScope(withVar.getVar());
2030 })*
2031 {
2032 gbc.setGbyPairList(vePairList);
2033 gbc.setDecorPairList(decorPairList);
2034 gbc.setWithVarList(withVarList);
2035 replaceCurrentScope(newScope);
2036 return gbc;
2037 }
2038}
2039
2040
2041LimitClause LimitClause() throws ParseException:
2042{
2043 LimitClause lc = new LimitClause();
2044 Expression expr;
2045 pushForbiddenScope(getCurrentScope());
2046}
2047{
2048 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2049 ("offset" expr = Expression() { lc.setOffset(expr); })?
2050
2051 {
2052 popForbiddenScope();
2053 return lc;
2054 }
2055}
2056
2057DistinctClause DistinctClause() throws ParseException:
2058{
2059 List<Expression> exprs = new ArrayList<Expression>();
2060 Expression expr;
2061}
2062{
2063 "distinct" "by" expr = Expression()
2064 {
2065 exprs.add(expr);
2066 }
2067 ("," expr = Expression()
2068 {
2069 exprs.add(expr);
2070 }
2071 )*
2072 {
2073 return new DistinctClause(exprs);
2074 }
2075}
2076
vinayakb38b7ca42012-03-05 05:44:15 +00002077QuantifiedExpression QuantifiedExpression()throws ParseException:
2078{
2079 QuantifiedExpression qc = new QuantifiedExpression();
2080 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2081 Expression satisfiesExpr;
2082 VariableExpr var;
2083 Expression inExpr;
2084 QuantifiedPair pair;
2085}
2086{
2087 {
2088 createNewScope();
2089 }
2090
2091 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2092 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2093 var = Variable() "in" inExpr = Expression()
2094 {
2095 pair = new QuantifiedPair(var, inExpr);
2096 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2097 quantifiedList.add(pair);
2098 }
2099 (
2100 "," var = Variable() "in" inExpr = Expression()
2101 {
2102 pair = new QuantifiedPair(var, inExpr);
2103 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2104 quantifiedList.add(pair);
2105 }
2106 )*
2107 "satisfies" satisfiesExpr = Expression()
2108 {
2109 qc.setSatisfiesExpr(satisfiesExpr);
2110 qc.setQuantifiedList(quantifiedList);
2111 removeCurrentScope();
2112 return qc;
2113 }
2114}
2115
2116TOKEN_MGR_DECLS:
2117{
2118 public int commentDepth = 0;
2119}
2120
2121<DEFAULT>
2122TOKEN :
2123{
2124 <CARET : "^" >
2125}
2126
2127<DEFAULT>
2128TOKEN :
2129{
2130 <DATASET : "dataset" >
2131}
2132
2133<DEFAULT>
2134TOKEN :
2135{
2136 <LEFTPAREN : "(" >
2137}
2138
2139<DEFAULT>
2140TOKEN :
2141{
2142 <RIGHTPAREN : ")" >
2143}
2144
2145
2146<DEFAULT>
2147TOKEN :
2148{
2149 <INTEGER_LITERAL : (<DIGIT>)+ >
2150}
2151
2152
2153<DEFAULT>
2154TOKEN :
2155{
2156 <NULL : "null">
2157}
2158
2159<DEFAULT>
2160TOKEN :
2161{
2162 <TRUE : "true">
2163}
2164
2165<DEFAULT>
2166TOKEN :
2167{
2168 <FALSE : "false">
2169}
2170
2171<DEFAULT>
2172TOKEN :
2173{
2174 <#DIGIT : ["0" - "9"]>
2175}
2176
2177
2178TOKEN:
2179{
2180 < DOUBLE_LITERAL: <INTEGER>
2181 | <INTEGER> ( "." <INTEGER> )?
2182 | "." <INTEGER>
2183 >
2184 |
2185 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2186 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2187 | "." <INTEGER> ( "f" | "F" )
2188 >
2189 |
2190 <INTEGER : (<DIGIT>)+ >
2191}
2192
2193<DEFAULT>
2194TOKEN :
2195{
2196 <#LETTER : ["A" - "Z", "a" - "z"]>
2197}
2198
2199<DEFAULT>
2200TOKEN :
2201{
2202 <SPECIALCHARS : ["$", "_", "-"] >
2203}
2204
2205<DEFAULT>
2206TOKEN :
2207{
2208 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2209 |
2210 < #EscapeQuot: "\\\"" >
2211 |
2212 < #EscapeApos: "\\\'" >
2213}
2214
2215<DEFAULT>
2216TOKEN :
2217{
2218 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2219}
2220
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002221
vinayakb38b7ca42012-03-05 05:44:15 +00002222<DEFAULT>
2223TOKEN :
2224{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002225 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002226}
2227
2228SKIP:
2229{
2230 " "
2231| "\t"
2232| "\r"
2233| "\n"
2234}
2235
2236SKIP:
2237{
2238 <"//" (~["\n"])* "\n">
2239}
2240
2241SKIP:
2242{
2243 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2244}
2245
2246
2247SKIP:
2248{
2249 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2250}
2251
2252<INSIDE_COMMENT>
2253SPECIAL_TOKEN:
2254{
2255 <"+"(" ")*(~["*"])*>
2256}
2257
2258<INSIDE_COMMENT>
2259SKIP:
2260{
2261 <"/*"> {commentDepth++;}
2262}
2263
2264<INSIDE_COMMENT>
2265SKIP:
2266{
2267 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2268| <~[]>
2269}