blob: d71db1780160b8205f88270a0f7b7617c0ad3b3f [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()
ramangrover29a774ef22013-07-17 09:29:18 -0700198 | stmt = FeedSpecification()
Till Westmann31c21f92013-05-08 09:21:53 -0700199 )
200 {
201 return stmt;
202 }
203}
204
205TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
206{
207 Pair<Identifier,Identifier> nameComponents = null;
208 boolean ifNotExists = false;
209 TypeExpression typeExpr = null;
210}
211{
ramangrover299f76a5e2013-06-18 10:25:17 -0700212 "type" nameComponents = TypeName() ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700213 "as" typeExpr = TypeExpr()
214 {
215 long numValues = -1;
216 String filename = null;
217 if (dgen) {
218 String splits[] = hint.split(" +");
219 if (splits.length != 3) {
220 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
221 }
222 filename = splits[1];
223 numValues = Long.parseLong(splits[2]);
224 }
225 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
226 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
227 }
228}
229
230
231NodegroupDecl NodegroupSpecification() throws ParseException:
232{
Till Westmann14a20a72013-05-09 00:06:24 -0700233 String name = null;
234 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700235 boolean ifNotExists = false;
236 List<Identifier>ncNames = null;
237}
238{
Till Westmanna4242bc2013-05-08 17:49:55 -0700239 "nodegroup" name = Identifier()
240 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700241 {
242 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700243 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700244 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700245 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700246 {
Till Westmann14a20a72013-05-09 00:06:24 -0700247 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700248 }
249 )*
250 {
Till Westmann14a20a72013-05-09 00:06:24 -0700251 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700252 }
253}
254
255DatasetDecl DatasetSpecification() throws ParseException:
256{
257 Pair<Identifier,Identifier> nameComponents = null;
258 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700259 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700260 String adapterName = null;
261 Map<String,String> properties = null;
262 FunctionSignature appliedFunction = null;
263 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700264 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700265 Map<String,String> hints = new HashMap<String,String>();
266 DatasetDecl dsetDecl = null;
267}
268{
269 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700270 "external" <DATASET> nameComponents = QualifiedName()
271 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
272 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700273 "using" adapterName = AdapterName() properties = Configuration()
274 ( "hints" hints = Properties() )?
275 {
276 ExternalDetailsDecl edd = new ExternalDetailsDecl();
277 edd.setAdapter(adapterName);
278 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700279 dsetDecl = new DatasetDecl(nameComponents.first,
280 nameComponents.second,
281 new Identifier(typeName),
282 hints,
283 DatasetType.EXTERNAL,
284 edd,
285 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700286 }
287
Till Westmannd7dcb122013-05-16 13:19:09 -0700288 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700289 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
290 ifNotExists = IfNotExists()
291 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700292 ( "hints" hints = Properties() )?
293 {
Till Westmann14a20a72013-05-09 00:06:24 -0700294 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
295 ? new Identifier(nodeGroupName)
296 : null,
297 primaryKeyFields);
298 dsetDecl = new DatasetDecl(nameComponents.first,
299 nameComponents.second,
300 new Identifier(typeName),
301 hints,
302 DatasetType.INTERNAL,
303 idd,
304 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700305 }
306 )
307 {
308 return dsetDecl;
309 }
310}
311
312CreateIndexStatement IndexSpecification() throws ParseException:
313{
314 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700315 String indexName = null;
316 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700317 boolean ifNotExists = false;
318 Pair<Identifier,Identifier> nameComponents = null;
319 IndexParams indexType = null;
320}
321{
Till Westmanna4242bc2013-05-08 17:49:55 -0700322 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700323 ifNotExists = IfNotExists()
324 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700325 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700326 {
Till Westmann14a20a72013-05-09 00:06:24 -0700327 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700328 }
Till Westmann14a20a72013-05-09 00:06:24 -0700329 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700330 {
Till Westmann14a20a72013-05-09 00:06:24 -0700331 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700332 }
333 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
334 {
Till Westmann14a20a72013-05-09 00:06:24 -0700335 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700336 cis.setIfNotExists(ifNotExists);
337 cis.setDataverseName(nameComponents.first);
338 cis.setDatasetName(nameComponents.second);
339 if (indexType != null) {
340 cis.setIndexType(indexType.type);
341 cis.setGramLength(indexType.gramLength);
342 }
343 return cis;
344 }
345}
346
347IndexParams IndexType() throws ParseException:
348{
349 IndexType type = null;
350 int gramLength = 0;
351}
352{
353 ("btree"
354 {
355 type = IndexType.BTREE;
356 }
357 | "rtree"
358 {
359 type = IndexType.RTREE;
360 }
361 | "keyword"
362 {
JIMAHNb75446d2013-06-03 08:35:27 -0700363 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700364 }
365 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
366 {
JIMAHNb75446d2013-06-03 08:35:27 -0700367 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700368 gramLength = Integer.valueOf(token.image);
369 }
370 <RIGHTPAREN>)
371 {
372 return new IndexParams(type, gramLength);
373 }
374}
375
376CreateDataverseStatement DataverseSpecification() throws ParseException :
377{
Till Westmann14a20a72013-05-09 00:06:24 -0700378 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700379 boolean ifNotExists = false;
380 String format = null;
381}
382{
Till Westmanna4242bc2013-05-08 17:49:55 -0700383 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700384 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700385 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700386 {
Till Westmann14a20a72013-05-09 00:06:24 -0700387 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700388 }
389}
390
391CreateFunctionStatement FunctionSpecification() throws ParseException:
392{
393 FunctionSignature signature;
394 boolean ifNotExists = false;
395 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
396 String functionBody;
ramangrover29bdba1a82013-06-21 17:17:52 -0700397 VarIdentifier var = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700398 Expression functionBodyExpr;
399 Token beginPos;
400 Token endPos;
ramangrover299f76a5e2013-06-18 10:25:17 -0700401 FunctionName fctName = null;
402
Till Westmann31c21f92013-05-08 09:21:53 -0700403 createNewScope();
404}
405{
ramangrover299f76a5e2013-06-18 10:25:17 -0700406 "function" fctName = FunctionName()
Till Westmann31c21f92013-05-08 09:21:53 -0700407 ifNotExists = IfNotExists()
ramangrover299f76a5e2013-06-18 10:25:17 -0700408 <LEFTPAREN> (<VARIABLE>
409 {
410 var = new VarIdentifier();
411 var.setValue(token.image);
412 paramList.add(var);
413 getCurrentScope().addNewVarSymbolToScope(var);
414 }
415 ("," <VARIABLE>
416 {
417 var = new VarIdentifier();
418 var.setValue(token.image);
419 paramList.add(var);
420 getCurrentScope().addNewVarSymbolToScope(var);
421 }
422 )*)? <RIGHTPAREN> "{"
Till Westmannd7dcb122013-05-16 13:19:09 -0700423 {
424 beginPos = token;
425 }
426 functionBodyExpr = Expression() "}"
427 {
428 endPos = token;
429 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
ramangrover299f76a5e2013-06-18 10:25:17 -0700430 // TODO use fctName.library
431 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
Till Westmannd7dcb122013-05-16 13:19:09 -0700432 getCurrentScope().addFunctionDescriptor(signature, false);
433 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
434 }
435}
436
ramangrover29a774ef22013-07-17 09:29:18 -0700437CreateFeedStatement FeedSpecification() throws ParseException:
438{
439 Pair<Identifier,Identifier> nameComponents = null;
440 boolean ifNotExists = false;
441 String adaptorName = null;
442 Map<String,String> properties = null;
443 FunctionSignature appliedFunction = null;
444 CreateFeedStatement cfs = null;
445}
446{
447 (
448 "feed" nameComponents = QualifiedName()
449 ifNotExists = IfNotExists()
450 "using" adaptorName = AdapterName() properties = Configuration()
451 (appliedFunction = ApplyFunction())?
452 {
453 cfs = new CreateFeedStatement(nameComponents.first,
454 nameComponents.second, adaptorName, properties, appliedFunction, ifNotExists);
455 }
456
457 )
458 {
459 return cfs;
460 }
461}
462
463
464
Till Westmannd7dcb122013-05-16 13:19:09 -0700465List<VarIdentifier> ParameterList() throws ParseException:
466{
467 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
468 VarIdentifier var = null;
469}
470{
Till Westmann31c21f92013-05-08 09:21:53 -0700471 <LEFTPAREN> (<VARIABLE>
472 {
473 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700474 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700475 paramList.add(var);
476 getCurrentScope().addNewVarSymbolToScope(var);
477 }
478 ("," <VARIABLE>
479 {
480 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700481 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700482 paramList.add(var);
483 getCurrentScope().addNewVarSymbolToScope(var);
484 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700485 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700486 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700487 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700488 }
489}
490
491boolean IfNotExists() throws ParseException:
492{
493}
494{
495 ( "if not exists"
496 {
497 return true;
498 }
499 )?
500 {
501 return false;
502 }
503}
504
505FunctionSignature ApplyFunction() throws ParseException:
506{
507 FunctionSignature funcSig = null;
508}
509{
510 "apply" "function" funcSig = FunctionSignature()
511 {
512 return funcSig;
513 }
514}
515
ramangrover29566b3a92013-05-28 09:07:10 -0700516String GetPolicy() throws ParseException:
517{
518 String policy = null;
519}
520{
521 "using" "policy" policy = Identifier()
522 {
523 return policy;
524 }
525
526}
527
Till Westmann31c21f92013-05-08 09:21:53 -0700528FunctionSignature FunctionSignature() throws ParseException:
529{
ramangrover299f76a5e2013-06-18 10:25:17 -0700530 FunctionName fctName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700531 int arity = 0;
532}
533{
ramangrover299f76a5e2013-06-18 10:25:17 -0700534 fctName = FunctionName() "@" <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700535 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700536 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700537 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
538 throw new ParseException(" invalid arity:" + arity);
539 }
540
ramangrover299f76a5e2013-06-18 10:25:17 -0700541 // TODO use fctName.library
ramangrover2993dd8232013-07-03 22:51:25 -0700542 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
543 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -0700544 }
545}
546
547List<String> PrimaryKey() throws ParseException:
548{
Till Westmann14a20a72013-05-09 00:06:24 -0700549 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700550 List<String> primaryKeyFields = new ArrayList<String>();
551}
552{
Till Westmann14a20a72013-05-09 00:06:24 -0700553 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700554 {
Till Westmann14a20a72013-05-09 00:06:24 -0700555 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700556 }
Till Westmann14a20a72013-05-09 00:06:24 -0700557 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700558 {
Till Westmann14a20a72013-05-09 00:06:24 -0700559 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700560 }
561 )*
562 {
563 return primaryKeyFields;
564 }
565}
566
567Statement DropStatement() throws ParseException:
568{
Till Westmann14a20a72013-05-09 00:06:24 -0700569 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700570 Pair<Identifier,Identifier> pairId = null;
571 Triple<Identifier,Identifier,Identifier> tripleId = null;
572 FunctionSignature funcSig = null;
573 boolean ifExists = false;
574 Statement stmt = null;
575}
576{
577 "drop"
578 (
579 <DATASET> pairId = QualifiedName() ifExists = IfExists()
580 {
581 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
582 }
583 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
584 {
585 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
586 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700587 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700588 {
Till Westmann14a20a72013-05-09 00:06:24 -0700589 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700590 }
ramangrover299f76a5e2013-06-18 10:25:17 -0700591 | "type" pairId = TypeName() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700592 {
593 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
594 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700595 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700596 {
Till Westmann14a20a72013-05-09 00:06:24 -0700597 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700598 }
599 | "function" funcSig = FunctionSignature() ifExists = IfExists()
600 {
601 stmt = new FunctionDropStatement(funcSig, ifExists);
602 }
ramangrover29a774ef22013-07-17 09:29:18 -0700603 | "feed" pairId = QualifiedName() ifExists = IfExists()
604 {
605 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
606 }
Till Westmann31c21f92013-05-08 09:21:53 -0700607 )
608 {
609 return stmt;
610 }
611}
612
613boolean IfExists() throws ParseException :
614{
615}
616{
617 ( "if" "exists"
618 {
619 return true;
620 }
621 )?
622 {
623 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000624 }
625}
626
627InsertStatement InsertStatement() throws ParseException:
628{
Till Westmann31c21f92013-05-08 09:21:53 -0700629 Pair<Identifier,Identifier> nameComponents = null;
630 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000631}
632{
Till Westmann31c21f92013-05-08 09:21:53 -0700633 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
634 {
635 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
636 }
vinayakb38b7ca42012-03-05 05:44:15 +0000637}
638
639DeleteStatement DeleteStatement() throws ParseException:
640{
Till Westmann31c21f92013-05-08 09:21:53 -0700641 VariableExpr var = null;
642 Expression condition = null;
643 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000644}
645{
Till Westmann31c21f92013-05-08 09:21:53 -0700646 "delete" var = Variable()
647 {
648 getCurrentScope().addNewVarSymbolToScope(var.getVar());
649 }
650 "from" <DATASET> nameComponents = QualifiedName()
651 ("where" condition = Expression())?
652 {
653 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
654 }
vinayakb38b7ca42012-03-05 05:44:15 +0000655}
656
657UpdateStatement UpdateStatement() throws ParseException:
658{
Till Westmann31c21f92013-05-08 09:21:53 -0700659 VariableExpr vars;
660 Expression target;
661 Expression condition;
662 UpdateClause uc;
663 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000664}
665{
Till Westmann31c21f92013-05-08 09:21:53 -0700666 "update" vars = Variable() "in" target = Expression()
667 "where" condition = Expression()
668 <LEFTPAREN> (uc = UpdateClause()
669 {
670 ucs.add(uc);
671 }
672 ("," uc = UpdateClause()
673 {
674 ucs.add(uc);
675 }
676 )*) <RIGHTPAREN>
677 {
678 return new UpdateStatement(vars, target, condition, ucs);
679 }
vinayakb38b7ca42012-03-05 05:44:15 +0000680}
681
vinayakb38b7ca42012-03-05 05:44:15 +0000682UpdateClause UpdateClause() throws ParseException:
683{
Till Westmann31c21f92013-05-08 09:21:53 -0700684 Expression target = null;
685 Expression value = null ;
686 InsertStatement is = null;
687 DeleteStatement ds = null;
688 UpdateStatement us = null;
689 Expression condition = null;
690 UpdateClause ifbranch = null;
691 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000692}
693{
694 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700695 | is = InsertStatement()
696 | ds = DeleteStatement()
697 | us = UpdateStatement()
698 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
699 "then" ifbranch = UpdateClause()
700 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
701 {
702 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
703 }
vinayakb38b7ca42012-03-05 05:44:15 +0000704}
705
vinayakb38b7ca42012-03-05 05:44:15 +0000706Statement SetStatement() throws ParseException:
707{
708 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700709 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000710}
711{
Till Westmann7d535322013-05-09 00:40:02 -0700712 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700713 {
Till Westmann31c21f92013-05-08 09:21:53 -0700714 return new SetStatement(pn, pv);
715 }
vinayakb38b7ca42012-03-05 05:44:15 +0000716}
717
718Statement WriteStatement() throws ParseException:
719{
Till Westmann14a20a72013-05-09 00:06:24 -0700720 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000721 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000722 Query query;
723 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000724 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000725}
726{
Till Westmann35a0f702013-07-01 14:06:34 -0700727 "write" "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
Till Westmann7d535322013-05-09 00:40:02 -0700728 ( "using" writerClass = StringLiteral() )?
Till Westmann35a0f702013-07-01 14:06:34 -0700729 {
730 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
vinayakb38b7ca42012-03-05 05:44:15 +0000731 }
732}
733
vinayakb38b7ca42012-03-05 05:44:15 +0000734LoadFromFileStatement LoadStatement() throws ParseException:
735{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000736 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000737 Identifier datasetName = null;
738 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000739 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000740 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000741 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000742}
743{
Till Westmann31c21f92013-05-08 09:21:53 -0700744 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000745 {
Till Westmann31c21f92013-05-08 09:21:53 -0700746 dataverseName = nameComponents.first;
747 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000748 }
Till Westmann31c21f92013-05-08 09:21:53 -0700749 "using" adapterName = AdapterName() properties = Configuration()
750 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000751 {
Till Westmann31c21f92013-05-08 09:21:53 -0700752 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000753 }
754 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700755 {
756 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
757 }
vinayakb38b7ca42012-03-05 05:44:15 +0000758}
759
vinayakb38b7ca42012-03-05 05:44:15 +0000760
Till Westmann31c21f92013-05-08 09:21:53 -0700761String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000762{
ramangrover29669d8f62013-02-11 06:03:32 +0000763 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000764}
765{
Till Westmann68d99652013-05-09 11:15:21 -0700766 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000767 {
Till Westmann7d535322013-05-09 00:40:02 -0700768 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000769 }
vinayakb38b7ca42012-03-05 05:44:15 +0000770}
771
Till Westmann31c21f92013-05-08 09:21:53 -0700772Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000773{
ramangrover29a774ef22013-07-17 09:29:18 -0700774 Pair<Identifier,Identifier> feedNameComponents = null;
775 Pair<Identifier,Identifier> datasetNameComponents = null;
776
Till Westmann31c21f92013-05-08 09:21:53 -0700777 Map<String,String> configuration = null;
778 Statement stmt = null;
ramangrover29566b3a92013-05-28 09:07:10 -0700779 String policy = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000780}
781{
Till Westmann31c21f92013-05-08 09:21:53 -0700782 (
ramangrover29a774ef22013-07-17 09:29:18 -0700783 "connect" "feed" feedNameComponents = QualifiedName() "to" <DATASET> datasetNameComponents = QualifiedName() (policy = GetPolicy())?
Till Westmann31c21f92013-05-08 09:21:53 -0700784 {
ramangrover29a774ef22013-07-17 09:29:18 -0700785 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, policy, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700786 }
ramangrover29a774ef22013-07-17 09:29:18 -0700787 | "disconnect" "feed" feedNameComponents = QualifiedName() "from" <DATASET> datasetNameComponents = QualifiedName()
Till Westmann31c21f92013-05-08 09:21:53 -0700788 {
ramangrover29a774ef22013-07-17 09:29:18 -0700789 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
Till Westmann31c21f92013-05-08 09:21:53 -0700790 }
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}