blob: b34a1de8c1d125c113b2f9cb71c3789cb236e443 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package edu.uci.ics.asterix.aql.parser;
12
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17
18import java.util.Map;
19import java.util.HashMap;
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +000020import java.util.LinkedHashMap;
vinayakb38b7ca42012-03-05 05:44:15 +000021import edu.uci.ics.asterix.aql.literal.FloatLiteral;
22import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
23import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000024import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000025import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000026import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000027import edu.uci.ics.asterix.aql.literal.NullLiteral;
28import edu.uci.ics.asterix.aql.literal.StringLiteral;
29import edu.uci.ics.asterix.aql.literal.TrueLiteral;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000030import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
vinayakb38b7ca42012-03-05 05:44:15 +000031
32import edu.uci.ics.asterix.aql.base.*;
33import edu.uci.ics.asterix.aql.expression.*;
34import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
35import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
36import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
37import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
38import edu.uci.ics.asterix.aql.base.Statement.Kind;
39import edu.uci.ics.asterix.aql.context.Scope;
40import edu.uci.ics.asterix.aql.context.RootScopeFactory;
41import edu.uci.ics.asterix.common.annotations.*;
42import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000043import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000044import edu.uci.ics.asterix.common.functions.FunctionSignature;
alexander.behm07617fd2012-07-25 10:13:50 +000045import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
46import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000047import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
48import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
49import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
50
51
vinayakb38b7ca42012-03-05 05:44:15 +000052
53
54public class AQLParser extends ScopeChecker {
55
vinayakb38b7ca42012-03-05 05:44:15 +000056 // optimizer hints
57 private static final String HASH_GROUP_BY_HINT = "hash";
58 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000059 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000060 private static final String INMEMORY_HINT = "inmem";
61 private static final String VAL_FILE_HINT = "val-files";
62 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
63 private static final String INTERVAL_HINT = "interval";
64 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
65 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
66 private static final String LIST_VAL_FILE_HINT = "list-val-file";
67 private static final String LIST_HINT = "list";
68 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
69 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
70 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
71 private static final String AUTO_HINT = "auto";
72
73 private static final String GEN_FIELDS_HINT = "gen-fields";
74
75 // data generator hints
76 private static final String DGEN_HINT = "dgen";
Till Westmann31c21f92013-05-08 09:21:53 -070077
78 private static class IndexParams {
79 public IndexType type;
80 public int gramLength;
81
82 public IndexParams(IndexType type, int gramLength) {
83 this.type = type;
84 this.gramLength = gramLength;
85 }
86 };
vinayakb38b7ca42012-03-05 05:44:15 +000087
ramangrover299f76a5e2013-06-18 10:25:17 -070088 private static class FunctionName {
89 public String dataverse = null;
90 public String library = null;
91 public String function = null;
92 }
93
vinayakb38b7ca42012-03-05 05:44:15 +000094 private static String getHint(Token t) {
Till Westmann31c21f92013-05-08 09:21:53 -070095 if (t.specialToken == null) {
96 return null;
97 }
98 String s = t.specialToken.image;
99 int n = s.length();
100 if (n < 2) {
101 return null;
102 }
103 return s.substring(1).trim();
vinayakb38b7ca42012-03-05 05:44:15 +0000104 }
105
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000106 public AQLParser(String s){
Till Westmann31c21f92013-05-08 09:21:53 -0700107 this(new StringReader(s));
108 super.setInput(s);
109 }
vinayakb38b7ca42012-03-05 05:44:15 +0000110
Till Westmann31c21f92013-05-08 09:21:53 -0700111 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
112 File file = new File(args[0]);
113 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
114 AQLParser parser = new AQLParser(fis);
115 List<Statement> st = parser.Statement();
116 //st.accept(new AQLPrintVisitor(), 0);
117 }
vinayakb38b7ca42012-03-05 05:44:15 +0000118}
119
120PARSER_END(AQLParser)
121
122
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000123List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000124{
vinayakb38b7ca42012-03-05 05:44:15 +0000125 scopeStack.push(RootScopeFactory.createRootScope(this));
126 List<Statement> decls = new ArrayList<Statement>();
Till Westmann31c21f92013-05-08 09:21:53 -0700127 Statement stmt = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000128}
129{
Till Westmann31c21f92013-05-08 09:21:53 -0700130 ( stmt = SingleStatement() (";") ?
vinayakb38b7ca42012-03-05 05:44:15 +0000131 {
Till Westmann31c21f92013-05-08 09:21:53 -0700132 decls.add(stmt);
133 }
134 )*
135 <EOF>
136 {
137 return decls;
138 }
139}
140
141Statement SingleStatement() throws ParseException:
142{
143 Statement stmt = null;
144}
145{
146 (
147 stmt = DataverseDeclaration()
148 | stmt = FunctionDeclaration()
149 | stmt = CreateStatement()
150 | stmt = LoadStatement()
151 | stmt = DropStatement()
152 | stmt = WriteStatement()
153 | stmt = SetStatement()
154 | stmt = InsertStatement()
155 | stmt = DeleteStatement()
156 | stmt = UpdateStatement()
157 | stmt = FeedStatement()
158 | stmt = Query()
159 )
160 {
161 return stmt;
162 }
163}
164
165DataverseDecl DataverseDeclaration() throws ParseException:
166{
Till Westmann14a20a72013-05-09 00:06:24 -0700167 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700168}
169{
Till Westmanna4242bc2013-05-08 17:49:55 -0700170 "use" "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700171 {
Till Westmann14a20a72013-05-09 00:06:24 -0700172 defaultDataverse = dvName;
173 return new DataverseDecl(new Identifier(dvName));
Till Westmann31c21f92013-05-08 09:21:53 -0700174 }
175}
176
177Statement CreateStatement() throws ParseException:
178{
179 String hint = null;
180 boolean dgen = false;
181 Statement stmt = null;
182}
183{
184 "create"
185 (
186 {
187 hint = getHint(token);
188 if (hint != null && hint.startsWith(DGEN_HINT)) {
189 dgen = true;
190 }
191 }
192 stmt = TypeSpecification(hint, dgen)
193 | stmt = NodegroupSpecification()
194 | stmt = DatasetSpecification()
195 | stmt = IndexSpecification()
196 | stmt = DataverseSpecification()
197 | stmt = FunctionSpecification()
198 )
199 {
200 return stmt;
201 }
202}
203
204TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
205{
206 Pair<Identifier,Identifier> nameComponents = null;
207 boolean ifNotExists = false;
208 TypeExpression typeExpr = null;
209}
210{
ramangrover299f76a5e2013-06-18 10:25:17 -0700211 "type" nameComponents = TypeName() ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700212 "as" typeExpr = TypeExpr()
213 {
214 long numValues = -1;
215 String filename = null;
216 if (dgen) {
217 String splits[] = hint.split(" +");
218 if (splits.length != 3) {
219 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
220 }
221 filename = splits[1];
222 numValues = Long.parseLong(splits[2]);
223 }
224 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
225 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
226 }
227}
228
229
230NodegroupDecl NodegroupSpecification() throws ParseException:
231{
Till Westmann14a20a72013-05-09 00:06:24 -0700232 String name = null;
233 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700234 boolean ifNotExists = false;
235 List<Identifier>ncNames = null;
236}
237{
Till Westmanna4242bc2013-05-08 17:49:55 -0700238 "nodegroup" name = Identifier()
239 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700240 {
241 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700242 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700243 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700244 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700245 {
Till Westmann14a20a72013-05-09 00:06:24 -0700246 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700247 }
248 )*
249 {
Till Westmann14a20a72013-05-09 00:06:24 -0700250 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700251 }
252}
253
254DatasetDecl DatasetSpecification() throws ParseException:
255{
256 Pair<Identifier,Identifier> nameComponents = null;
257 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700258 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700259 String adapterName = null;
260 Map<String,String> properties = null;
261 FunctionSignature appliedFunction = null;
262 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700263 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700264 Map<String,String> hints = new HashMap<String,String>();
265 DatasetDecl dsetDecl = null;
266}
267{
268 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700269 "external" <DATASET> nameComponents = QualifiedName()
270 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
271 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700272 "using" adapterName = AdapterName() properties = Configuration()
273 ( "hints" hints = Properties() )?
274 {
275 ExternalDetailsDecl edd = new ExternalDetailsDecl();
276 edd.setAdapter(adapterName);
277 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700278 dsetDecl = new DatasetDecl(nameComponents.first,
279 nameComponents.second,
280 new Identifier(typeName),
281 hints,
282 DatasetType.EXTERNAL,
283 edd,
284 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700285 }
286
Till Westmanna4242bc2013-05-08 17:49:55 -0700287 | "feed" <DATASET> nameComponents = QualifiedName()
288 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
289 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700290 "using" adapterName = AdapterName() properties = Configuration()
291 (appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
Till Westmanna4242bc2013-05-08 17:49:55 -0700292 ( "on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700293 ( "hints" hints = Properties() )?
294 {
Till Westmann14a20a72013-05-09 00:06:24 -0700295 FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
296 properties,
297 appliedFunction,
298 nodeGroupName != null
299 ? new Identifier(nodeGroupName)
300 : null,
301 primaryKeyFields);
302 dsetDecl = new DatasetDecl(nameComponents.first,
303 nameComponents.second,
304 new Identifier(typeName),
305 hints,
306 DatasetType.FEED,
307 fdd,
308 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700309 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700310 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700311 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
312 ifNotExists = IfNotExists()
313 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700314 ( "hints" hints = Properties() )?
315 {
Till Westmann14a20a72013-05-09 00:06:24 -0700316 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
317 ? new Identifier(nodeGroupName)
318 : null,
319 primaryKeyFields);
320 dsetDecl = new DatasetDecl(nameComponents.first,
321 nameComponents.second,
322 new Identifier(typeName),
323 hints,
324 DatasetType.INTERNAL,
325 idd,
326 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700327 }
328 )
329 {
330 return dsetDecl;
331 }
332}
333
334CreateIndexStatement IndexSpecification() throws ParseException:
335{
336 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700337 String indexName = null;
338 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700339 boolean ifNotExists = false;
340 Pair<Identifier,Identifier> nameComponents = null;
341 IndexParams indexType = null;
342}
343{
Till Westmanna4242bc2013-05-08 17:49:55 -0700344 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700345 ifNotExists = IfNotExists()
346 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700347 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700348 {
Till Westmann14a20a72013-05-09 00:06:24 -0700349 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700350 }
Till Westmann14a20a72013-05-09 00:06:24 -0700351 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700352 {
Till Westmann14a20a72013-05-09 00:06:24 -0700353 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700354 }
355 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
356 {
Till Westmann14a20a72013-05-09 00:06:24 -0700357 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700358 cis.setIfNotExists(ifNotExists);
359 cis.setDataverseName(nameComponents.first);
360 cis.setDatasetName(nameComponents.second);
361 if (indexType != null) {
362 cis.setIndexType(indexType.type);
363 cis.setGramLength(indexType.gramLength);
364 }
365 return cis;
366 }
367}
368
369IndexParams IndexType() throws ParseException:
370{
371 IndexType type = null;
372 int gramLength = 0;
373}
374{
375 ("btree"
376 {
377 type = IndexType.BTREE;
378 }
379 | "rtree"
380 {
381 type = IndexType.RTREE;
382 }
383 | "keyword"
384 {
Till Westmann31c21f92013-05-08 09:21:53 -0700385 type = IndexType.FUZZY_WORD_INVIX;
386 }
387 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
388 {
Till Westmann31c21f92013-05-08 09:21:53 -0700389 type = IndexType.FUZZY_NGRAM_INVIX;
390 gramLength = Integer.valueOf(token.image);
391 }
392 <RIGHTPAREN>)
393 {
394 return new IndexParams(type, gramLength);
395 }
396}
397
398CreateDataverseStatement DataverseSpecification() throws ParseException :
399{
Till Westmann14a20a72013-05-09 00:06:24 -0700400 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700401 boolean ifNotExists = false;
402 String format = null;
403}
404{
Till Westmanna4242bc2013-05-08 17:49:55 -0700405 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700406 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700407 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700408 {
Till Westmann14a20a72013-05-09 00:06:24 -0700409 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700410 }
411}
412
413CreateFunctionStatement FunctionSpecification() throws ParseException:
414{
415 FunctionSignature signature;
416 boolean ifNotExists = false;
417 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
418 String functionBody;
ramangrover29bdba1a82013-06-21 17:17:52 -0700419 VarIdentifier var = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700420 Expression functionBodyExpr;
421 Token beginPos;
422 Token endPos;
ramangrover299f76a5e2013-06-18 10:25:17 -0700423 FunctionName fctName = null;
424
Till Westmann31c21f92013-05-08 09:21:53 -0700425 createNewScope();
426}
427{
ramangrover299f76a5e2013-06-18 10:25:17 -0700428 "function" fctName = FunctionName()
Till Westmann31c21f92013-05-08 09:21:53 -0700429 ifNotExists = IfNotExists()
ramangrover299f76a5e2013-06-18 10:25:17 -0700430 <LEFTPAREN> (<VARIABLE>
431 {
432 var = new VarIdentifier();
433 var.setValue(token.image);
434 paramList.add(var);
435 getCurrentScope().addNewVarSymbolToScope(var);
436 }
437 ("," <VARIABLE>
438 {
439 var = new VarIdentifier();
440 var.setValue(token.image);
441 paramList.add(var);
442 getCurrentScope().addNewVarSymbolToScope(var);
443 }
444 )*)? <RIGHTPAREN> "{"
Till Westmannd7dcb122013-05-16 13:19:09 -0700445 {
446 beginPos = token;
447 }
448 functionBodyExpr = Expression() "}"
449 {
450 endPos = token;
451 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
ramangrover299f76a5e2013-06-18 10:25:17 -0700452 // TODO use fctName.library
453 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
Till Westmannd7dcb122013-05-16 13:19:09 -0700454 getCurrentScope().addFunctionDescriptor(signature, false);
455 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
456 }
457}
458
459List<VarIdentifier> ParameterList() throws ParseException:
460{
461 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
462 VarIdentifier var = null;
463}
464{
Till Westmann31c21f92013-05-08 09:21:53 -0700465 <LEFTPAREN> (<VARIABLE>
466 {
467 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700468 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700469 paramList.add(var);
470 getCurrentScope().addNewVarSymbolToScope(var);
471 }
472 ("," <VARIABLE>
473 {
474 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700475 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700476 paramList.add(var);
477 getCurrentScope().addNewVarSymbolToScope(var);
478 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700479 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700480 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700481 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700482 }
483}
484
485boolean IfNotExists() throws ParseException:
486{
487}
488{
489 ( "if not exists"
490 {
491 return true;
492 }
493 )?
494 {
495 return false;
496 }
497}
498
499FunctionSignature ApplyFunction() throws ParseException:
500{
501 FunctionSignature funcSig = null;
502}
503{
504 "apply" "function" funcSig = FunctionSignature()
505 {
506 return funcSig;
507 }
508}
509
510FunctionSignature FunctionSignature() throws ParseException:
511{
ramangrover299f76a5e2013-06-18 10:25:17 -0700512 FunctionName fctName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700513 int arity = 0;
514}
515{
ramangrover299f76a5e2013-06-18 10:25:17 -0700516 fctName = FunctionName() "@" <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700517 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700518 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700519 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
520 throw new ParseException(" invalid arity:" + arity);
521 }
522
ramangrover299f76a5e2013-06-18 10:25:17 -0700523 // TODO use fctName.library
524 return new FunctionSignature(fctName.dataverse, fctName.function, arity);
Till Westmann31c21f92013-05-08 09:21:53 -0700525 }
526}
527
528List<String> PrimaryKey() throws ParseException:
529{
Till Westmann14a20a72013-05-09 00:06:24 -0700530 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700531 List<String> primaryKeyFields = new ArrayList<String>();
532}
533{
Till Westmann14a20a72013-05-09 00:06:24 -0700534 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700535 {
Till Westmann14a20a72013-05-09 00:06:24 -0700536 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700537 }
Till Westmann14a20a72013-05-09 00:06:24 -0700538 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700539 {
Till Westmann14a20a72013-05-09 00:06:24 -0700540 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700541 }
542 )*
543 {
544 return primaryKeyFields;
545 }
546}
547
548Statement DropStatement() throws ParseException:
549{
Till Westmann14a20a72013-05-09 00:06:24 -0700550 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700551 Pair<Identifier,Identifier> pairId = null;
552 Triple<Identifier,Identifier,Identifier> tripleId = null;
553 FunctionSignature funcSig = null;
554 boolean ifExists = false;
555 Statement stmt = null;
556}
557{
558 "drop"
559 (
560 <DATASET> pairId = QualifiedName() ifExists = IfExists()
561 {
562 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
563 }
564 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
565 {
566 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
567 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700568 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700569 {
Till Westmann14a20a72013-05-09 00:06:24 -0700570 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700571 }
ramangrover299f76a5e2013-06-18 10:25:17 -0700572 | "type" pairId = TypeName() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700573 {
574 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
575 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700576 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700577 {
Till Westmann14a20a72013-05-09 00:06:24 -0700578 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700579 }
580 | "function" funcSig = FunctionSignature() ifExists = IfExists()
581 {
582 stmt = new FunctionDropStatement(funcSig, ifExists);
583 }
584 )
585 {
586 return stmt;
587 }
588}
589
590boolean IfExists() throws ParseException :
591{
592}
593{
594 ( "if" "exists"
595 {
596 return true;
597 }
598 )?
599 {
600 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000601 }
602}
603
604InsertStatement InsertStatement() throws ParseException:
605{
Till Westmann31c21f92013-05-08 09:21:53 -0700606 Pair<Identifier,Identifier> nameComponents = null;
607 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000608}
609{
Till Westmann31c21f92013-05-08 09:21:53 -0700610 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
611 {
612 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
613 }
vinayakb38b7ca42012-03-05 05:44:15 +0000614}
615
616DeleteStatement DeleteStatement() throws ParseException:
617{
Till Westmann31c21f92013-05-08 09:21:53 -0700618 VariableExpr var = null;
619 Expression condition = null;
620 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000621}
622{
Till Westmann31c21f92013-05-08 09:21:53 -0700623 "delete" var = Variable()
624 {
625 getCurrentScope().addNewVarSymbolToScope(var.getVar());
626 }
627 "from" <DATASET> nameComponents = QualifiedName()
628 ("where" condition = Expression())?
629 {
630 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
631 }
vinayakb38b7ca42012-03-05 05:44:15 +0000632}
633
634UpdateStatement UpdateStatement() throws ParseException:
635{
Till Westmann31c21f92013-05-08 09:21:53 -0700636 VariableExpr vars;
637 Expression target;
638 Expression condition;
639 UpdateClause uc;
640 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000641}
642{
Till Westmann31c21f92013-05-08 09:21:53 -0700643 "update" vars = Variable() "in" target = Expression()
644 "where" condition = Expression()
645 <LEFTPAREN> (uc = UpdateClause()
646 {
647 ucs.add(uc);
648 }
649 ("," uc = UpdateClause()
650 {
651 ucs.add(uc);
652 }
653 )*) <RIGHTPAREN>
654 {
655 return new UpdateStatement(vars, target, condition, ucs);
656 }
vinayakb38b7ca42012-03-05 05:44:15 +0000657}
658
vinayakb38b7ca42012-03-05 05:44:15 +0000659UpdateClause UpdateClause() throws ParseException:
660{
Till Westmann31c21f92013-05-08 09:21:53 -0700661 Expression target = null;
662 Expression value = null ;
663 InsertStatement is = null;
664 DeleteStatement ds = null;
665 UpdateStatement us = null;
666 Expression condition = null;
667 UpdateClause ifbranch = null;
668 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000669}
670{
671 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700672 | is = InsertStatement()
673 | ds = DeleteStatement()
674 | us = UpdateStatement()
675 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
676 "then" ifbranch = UpdateClause()
677 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
678 {
679 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
680 }
vinayakb38b7ca42012-03-05 05:44:15 +0000681}
682
vinayakb38b7ca42012-03-05 05:44:15 +0000683Statement SetStatement() throws ParseException:
684{
685 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700686 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000687}
688{
Till Westmann7d535322013-05-09 00:40:02 -0700689 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700690 {
Till Westmann31c21f92013-05-08 09:21:53 -0700691 return new SetStatement(pn, pv);
692 }
vinayakb38b7ca42012-03-05 05:44:15 +0000693}
694
695Statement WriteStatement() throws ParseException:
696{
Till Westmann14a20a72013-05-09 00:06:24 -0700697 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000698 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000699 Statement stmt = null;
700 Query query;
701 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000702 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000703}
704{
Till Westmann31c21f92013-05-08 09:21:53 -0700705 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700706 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
707 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700708 {
Till Westmann14a20a72013-05-09 00:06:24 -0700709 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000710 }
Till Westmann31c21f92013-05-08 09:21:53 -0700711 ) | (
712 "into" <DATASET>
713 {
714 nameComponents = QualifiedName();
715 }
716 <LEFTPAREN> query = Query() <RIGHTPAREN>
717 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000718 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700719 }
720 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000721 {
722 return stmt;
723 }
724}
725
vinayakb38b7ca42012-03-05 05:44:15 +0000726LoadFromFileStatement LoadStatement() throws ParseException:
727{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000728 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000729 Identifier datasetName = null;
730 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000731 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000732 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000733 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000734}
735{
Till Westmann31c21f92013-05-08 09:21:53 -0700736 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000737 {
Till Westmann31c21f92013-05-08 09:21:53 -0700738 dataverseName = nameComponents.first;
739 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000740 }
Till Westmann31c21f92013-05-08 09:21:53 -0700741 "using" adapterName = AdapterName() properties = Configuration()
742 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000743 {
Till Westmann31c21f92013-05-08 09:21:53 -0700744 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000745 }
746 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700747 {
748 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
749 }
vinayakb38b7ca42012-03-05 05:44:15 +0000750}
751
vinayakb38b7ca42012-03-05 05:44:15 +0000752
Till Westmann31c21f92013-05-08 09:21:53 -0700753String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000754{
ramangrover29669d8f62013-02-11 06:03:32 +0000755 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000756}
757{
Till Westmann68d99652013-05-09 11:15:21 -0700758 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000759 {
Till Westmann7d535322013-05-09 00:40:02 -0700760 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000761 }
vinayakb38b7ca42012-03-05 05:44:15 +0000762}
763
Till Westmann31c21f92013-05-08 09:21:53 -0700764Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000765{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000766 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700767 Map<String,String> configuration = null;
768 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000769}
770{
Till Westmann31c21f92013-05-08 09:21:53 -0700771 (
772 "begin" "feed" nameComponents = QualifiedName()
773 {
774 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
775 }
776 | "suspend" "feed" nameComponents = QualifiedName()
777 {
778 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
779 }
780 | "resume" "feed" nameComponents = QualifiedName()
781 {
782 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
783 }
784 | "end" "feed" nameComponents = QualifiedName()
785 {
786 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
787 }
788 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
789 {
790 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
791 }
792 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000793 {
Till Westmann31c21f92013-05-08 09:21:53 -0700794 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000795 }
796}
797
Till Westmann31c21f92013-05-08 09:21:53 -0700798Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000799{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000800 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700801 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000802}
803{
Till Westmann31c21f92013-05-08 09:21:53 -0700804 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000805 {
Till Westmann31c21f92013-05-08 09:21:53 -0700806 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000807 }
Till Westmann31c21f92013-05-08 09:21:53 -0700808 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000809 {
Till Westmann31c21f92013-05-08 09:21:53 -0700810 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000811 }
Till Westmann31c21f92013-05-08 09:21:53 -0700812 )* )? <RIGHTPAREN>
813 {
814 return configuration;
815 }
816}
817
818Pair<String, String> KeyValuePair() throws ParseException:
819{
820 String key;
821 String value;
822}
823{
Till Westmann7d535322013-05-09 00:40:02 -0700824 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700825 {
826 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000827 }
Till Westmann31c21f92013-05-08 09:21:53 -0700828}
829
830Map<String,String> Properties() throws ParseException:
831{
832 Map<String,String> properties = new HashMap<String,String>();
833 Pair<String, String> property;
834}
835{
836 ( <LEFTPAREN> property = Property()
837 {
838 properties.put(property.first, property.second);
839 }
840 ( "," property = Property()
841 {
842 properties.put(property.first, property.second);
843 }
844 )* <RIGHTPAREN> )?
845 {
846 return properties;
847 }
848}
849
850Pair<String, String> Property() throws ParseException:
851{
852 String key;
853 String value;
854}
855{
Till Westmann7d535322013-05-09 00:40:02 -0700856 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700857 {
858 try {
859 value = "" + Long.valueOf(token.image);
860 } catch (NumberFormatException nfe) {
861 throw new ParseException("inapproriate value: " + token.image);
862 }
863 }
864 )
865 {
866 return new Pair<String, String>(key.toUpperCase(), value);
867 }
vinayakb38b7ca42012-03-05 05:44:15 +0000868}
869
870TypeExpression TypeExpr() throws ParseException:
871{
872 TypeExpression typeExpr = null;
873}
874{
875 (
876 typeExpr = RecordTypeDef()
877 | typeExpr = TypeReference()
878 | typeExpr = OrderedListTypeDef()
879 | typeExpr = UnorderedListTypeDef()
880 )
881 {
882 return typeExpr;
883 }
884}
885
886RecordTypeDefinition RecordTypeDef() throws ParseException:
887{
888 RecordTypeDefinition recType = new RecordTypeDefinition();
889 RecordTypeDefinition.RecordKind recordKind = null;
890}
891{
892 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
893 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
894 "{"
895 {
896 String hint = getHint(token);
897 if (hint != null) {
898 String splits[] = hint.split(" +");
899 if (splits[0].equals(GEN_FIELDS_HINT)) {
900 if (splits.length != 5) {
901 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
902 }
903 if (!splits[1].equals("int")) {
904 throw new ParseException("The only supported type for gen-fields is int.");
905 }
906 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
907 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
908 recType.setUndeclaredFieldsDataGen(ufdg);
909 }
910 }
911
912 }
913 (
914 RecordField(recType)
915 ( "," RecordField(recType) )*
916 )?
917 "}"
918 {
919 if (recordKind == null) {
920 recordKind = RecordTypeDefinition.RecordKind.OPEN;
921 }
922 recType.setRecordKind(recordKind);
923 return recType;
924 }
925}
926
927void RecordField(RecordTypeDefinition recType) throws ParseException:
928{
929 String fieldName;
930 TypeExpression type = null;
931 boolean nullable = false;
932}
933{
Till Westmann14a20a72013-05-09 00:06:24 -0700934 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000935 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700936 fieldName = token.image;
937 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000938 IRecordFieldDataGen rfdg = null;
939 if (hint != null) {
940 String splits[] = hint.split(" +");
941 if (splits[0].equals(VAL_FILE_HINT)) {
942 File[] valFiles = new File[splits.length - 1];
943 for (int k=1; k<splits.length; k++) {
944 valFiles[k-1] = new File(splits[k]);
945 }
946 rfdg = new FieldValFileDataGen(valFiles);
947 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
948 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
949 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
950 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
951 } else if (splits[0].equals(LIST_HINT)) {
952 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
953 } else if (splits[0].equals(INTERVAL_HINT)) {
954 FieldIntervalDataGen.ValueType vt;
955 if (splits[1].equals("int")) {
956 vt = FieldIntervalDataGen.ValueType.INT;
957 } else if (splits[1].equals("long")) {
958 vt = FieldIntervalDataGen.ValueType.LONG;
959 } else if (splits[1].equals("float")) {
960 vt = FieldIntervalDataGen.ValueType.FLOAT;
961 } else if (splits[1].equals("double")) {
962 vt = FieldIntervalDataGen.ValueType.DOUBLE;
963 } else {
964 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
965 }
966 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
967 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
968 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
969 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
970 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
971 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
972 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
973 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
974 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
975 } else if (splits[0].equals(AUTO_HINT)) {
976 rfdg = new AutoDataGen(splits[1]);
977 }
978 }
979 }
980 ":"
981 ( type = TypeExpr() )
982 ("?" { nullable = true; } )?
983 {
984 recType.addField(fieldName, type, nullable, rfdg);
985 }
986}
987
988TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000989{
Till Westmann14a20a72013-05-09 00:06:24 -0700990 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700991}
992{
993 id = Identifier()
994 {
Till Westmann14a20a72013-05-09 00:06:24 -0700995 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700996 }
vinayakb38b7ca42012-03-05 05:44:15 +0000997}
998
999OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1000{
1001 TypeExpression type = null;
1002}
1003{
1004 "["
1005 ( type = TypeExpr() )
1006 "]"
1007 {
1008 return new OrderedListTypeDefinition(type);
1009 }
1010}
1011
1012
1013UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1014{
1015 TypeExpression type = null;
1016}
1017{
1018 "{{"
1019 ( type = TypeExpr() )
1020 "}}"
1021 {
1022 return new UnorderedListTypeDefinition(type);
1023 }
1024}
1025
ramangrover299f76a5e2013-06-18 10:25:17 -07001026FunctionName FunctionName() throws ParseException:
1027{
1028 String first = null;
1029 String second = null;
1030 String third = null;
1031 boolean secondAfterDot = false;
1032}
1033{
1034 first = Identifier() ( "." second = Identifier()
1035 {
1036 secondAfterDot = true;
1037 }
1038 ("#" third = Identifier())? | "#" second = Identifier() )?
1039 {
1040 FunctionName result = new FunctionName();
1041 if (second == null) {
1042 result.dataverse = defaultDataverse;
1043 result.library = null;
1044 result.function = first;
1045 } else if (third == null) {
1046 if (secondAfterDot) {
1047 result.dataverse = first;
1048 result.library = null;
1049 result.function = second;
1050 } else {
1051 result.dataverse = defaultDataverse;
1052 result.library = first;
1053 result.function = second;
1054 }
1055 } else {
1056 result.dataverse = first;
1057 result.library = second;
1058 result.function = third;
1059 }
1060 return result;
1061 }
1062}
Till Westmann31c21f92013-05-08 09:21:53 -07001063
ramangrover299f76a5e2013-06-18 10:25:17 -07001064
1065Pair<Identifier,Identifier> TypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001066{
Till Westmann31c21f92013-05-08 09:21:53 -07001067 Pair<Identifier,Identifier> name = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001068}
1069{
Till Westmann31c21f92013-05-08 09:21:53 -07001070 name = QualifiedName()
1071 {
1072 if (name.first == null) {
1073 name.first = new Identifier(defaultDataverse);
1074 }
1075 return name;
1076 }
1077}
1078
Till Westmann14a20a72013-05-09 00:06:24 -07001079String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001080{
Till Westmann68d99652013-05-09 11:15:21 -07001081 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001082}
1083{
1084 <IDENTIFIER>
1085 {
Till Westmann14a20a72013-05-09 00:06:24 -07001086 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001087 }
Till Westmann68d99652013-05-09 11:15:21 -07001088 | lit = StringLiteral()
1089 {
1090 return lit;
1091 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001092}
1093
Till Westmann7d535322013-05-09 00:40:02 -07001094String StringLiteral() throws ParseException:
1095{
1096}
1097{
1098 <STRING_LITERAL>
1099 {
1100 return removeQuotesAndEscapes(token.image);
1101 }
1102}
1103
Till Westmann31c21f92013-05-08 09:21:53 -07001104Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1105{
Till Westmann14a20a72013-05-09 00:06:24 -07001106 String first = null;
1107 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001108}
1109{
Till Westmanna4242bc2013-05-08 17:49:55 -07001110 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001111 {
Till Westmann14a20a72013-05-09 00:06:24 -07001112 Identifier id1 = null;
1113 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001114 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001115 id2 = new Identifier(first);
1116 } else
1117 {
1118 id1 = new Identifier(first);
1119 id2 = new Identifier(second);
1120 }
1121 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001122 }
1123}
1124
Till Westmann31c21f92013-05-08 09:21:53 -07001125Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001126{
Till Westmann14a20a72013-05-09 00:06:24 -07001127 String first = null;
1128 String second = null;
1129 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001130}
1131{
Till Westmanna4242bc2013-05-08 17:49:55 -07001132 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001133 {
Till Westmann14a20a72013-05-09 00:06:24 -07001134 Identifier id1 = null;
1135 Identifier id2 = null;
1136 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001137 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001138 id2 = new Identifier(first);
1139 id3 = new Identifier(second);
1140 } else {
1141 id1 = new Identifier(first);
1142 id2 = new Identifier(second);
1143 id3 = new Identifier(third);
1144 }
1145 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001146 }
1147}
1148
vinayakb38b7ca42012-03-05 05:44:15 +00001149FunctionDecl FunctionDeclaration() throws ParseException:
1150{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001151 FunctionDecl funcDecl;
1152 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001153 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001154 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1155 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001156 createNewScope();
1157}
1158{
Till Westmannd7dcb122013-05-16 13:19:09 -07001159 "declare" "function" functionName = Identifier()
1160 paramList = ParameterList()
1161 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001162 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001163 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001164 getCurrentScope().addFunctionDescriptor(signature, false);
1165 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001166 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001167 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001168 }
1169}
1170
vinayakb38b7ca42012-03-05 05:44:15 +00001171
Till Westmann31c21f92013-05-08 09:21:53 -07001172Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001173{
1174 Query query = new Query();
1175 Expression expr;
1176}
1177{
Till Westmann31c21f92013-05-08 09:21:53 -07001178 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001179 {
1180 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001181 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001182 return query;
1183 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001184
vinayakb38b7ca42012-03-05 05:44:15 +00001185}
1186
1187
1188
1189Expression Expression():
1190{
1191 Expression expr = null;
1192 Expression exprP = null;
1193}
1194{
1195(
1196
1197//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1198 expr = OperatorExpr()
1199 | expr = IfThenElse()
1200 | expr = FLWOGR()
1201 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001202
vinayakb38b7ca42012-03-05 05:44:15 +00001203
1204)
1205 {
1206 return (exprP==null) ? expr : exprP;
1207 }
1208}
1209
1210
1211
1212Expression OperatorExpr()throws ParseException:
1213{
1214 OperatorExpr op = null;
1215 Expression operand = null;
1216}
1217{
1218 operand = AndExpr()
1219 (
1220
1221 "or"
1222 {
1223 if (op == null) {
1224 op = new OperatorExpr();
1225 op.addOperand(operand);
1226 op.setCurrentop(true);
1227 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001228 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001229 }
1230
1231 operand = AndExpr()
1232 {
1233 op.addOperand(operand);
1234 }
1235
1236 )*
1237
1238 {
1239 return op==null? operand: op;
1240 }
1241}
1242
1243Expression AndExpr()throws ParseException:
1244{
1245 OperatorExpr op = null;
1246 Expression operand = null;
1247}
1248{
1249 operand = RelExpr()
1250 (
1251
1252 "and"
1253 {
1254 if (op == null) {
1255 op = new OperatorExpr();
1256 op.addOperand(operand);
1257 op.setCurrentop(true);
1258 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001259 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001260 }
1261
1262 operand = RelExpr()
1263 {
1264 op.addOperand(operand);
1265 }
1266
1267 )*
1268
1269 {
1270 return op==null? operand: op;
1271 }
1272}
1273
1274
1275
1276Expression RelExpr()throws ParseException:
1277{
1278 OperatorExpr op = null;
1279 Expression operand = null;
1280 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001281 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001282}
1283{
1284 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001285 {
1286 if (operand instanceof VariableExpr) {
1287 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001288 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001289 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001290 }
1291 }
1292 }
1293
1294 (
1295 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1296 {
alexander.behm07617fd2012-07-25 10:13:50 +00001297 String mhint = getHint(token);
1298 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1299 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1300 }
vinayakb38b7ca42012-03-05 05:44:15 +00001301 if (op == null) {
1302 op = new OperatorExpr();
1303 op.addOperand(operand, broadcast);
1304 op.setCurrentop(true);
1305 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001306 }
1307 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001308 }
1309
1310 operand = AddExpr()
1311 {
alexander.behm07617fd2012-07-25 10:13:50 +00001312 broadcast = false;
1313 if (operand instanceof VariableExpr) {
1314 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001315 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1316 broadcast = true;
1317 }
alexander.behm07617fd2012-07-25 10:13:50 +00001318 }
vinayakb38b7ca42012-03-05 05:44:15 +00001319 op.addOperand(operand, broadcast);
1320 }
1321 )?
1322
1323 {
alexander.behm07617fd2012-07-25 10:13:50 +00001324 if (annotation != null) {
1325 op.addHint(annotation);
1326 }
vinayakb38b7ca42012-03-05 05:44:15 +00001327 return op==null? operand: op;
1328 }
1329}
1330
1331Expression AddExpr()throws ParseException:
1332{
1333 OperatorExpr op = null;
1334 Expression operand = null;
1335}
1336{
1337 operand = MultExpr()
1338
1339 ( ("+" | "-")
1340 {
1341 if (op == null) {
1342 op = new OperatorExpr();
1343 op.addOperand(operand);
1344 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001345 }
1346 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001347 }
1348
1349 operand = MultExpr()
1350 {
1351 op.addOperand(operand);
1352 }
1353 )*
1354
1355 {
1356 return op==null? operand: op;
1357 }
1358}
1359
1360Expression MultExpr()throws ParseException:
1361{
1362 OperatorExpr op = null;
1363 Expression operand = null;
1364}
1365{
1366 operand = UnionExpr()
1367
1368 (( "*" | "/" | "%" | <CARET> | "idiv")
1369 {
1370 if (op == null) {
1371 op = new OperatorExpr();
1372 op.addOperand(operand);
1373 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001374 }
1375 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001376 }
1377 operand = UnionExpr()
1378 {
1379 op.addOperand(operand);
1380 }
1381 )*
1382
1383 {
1384 return op==null?operand:op;
1385 }
1386}
1387
1388Expression UnionExpr() throws ParseException:
1389{
1390 UnionExpr union = null;
1391 Expression operand1 = null;
1392 Expression operand2 = null;
1393}
1394{
1395 operand1 = UnaryExpr()
1396 ("union"
1397 (operand2 = UnaryExpr()) {
1398 if (union == null) {
1399 union = new UnionExpr();
1400 union.addExpr(operand1);
1401 }
1402 union.addExpr(operand2);
1403 } )*
1404 {
1405 return (union == null)? operand1: union;
1406 }
1407}
1408
1409Expression UnaryExpr() throws ParseException:
1410{
1411 Expression uexpr = null;
1412 Expression expr = null;
1413}
1414{
1415 (( "+"|"-")
1416 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001417 uexpr = new UnaryExpr();
1418 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001419 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001420 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001421 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1422 else
1423 throw new ParseException();
1424 }
1425 )?
1426
1427 expr = ValueExpr()
1428 {
1429 if(uexpr!=null){
1430 ((UnaryExpr)uexpr).setExpr(expr);
1431 return uexpr;
1432 }
1433 else{
1434 return expr;
1435 }
1436 }
1437}
1438
Till Westmann04478e72013-05-13 23:01:34 -07001439Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001440{
1441 Expression expr = null;
1442 Identifier ident = null;
1443 AbstractAccessor fa = null;
1444 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001445}
1446{
Till Westmann04478e72013-05-13 23:01:34 -07001447 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001448 {
Till Westmann04478e72013-05-13 23:01:34 -07001449 fa = (fa == null ? new FieldAccessor(expr, ident)
1450 : new FieldAccessor(fa, ident));
1451 }
1452 | index = Index()
1453 {
1454 fa = (fa == null ? new IndexAccessor(expr, index)
1455 : new IndexAccessor(fa, index));
1456 }
1457 )*
1458 {
1459 return fa == null ? expr : fa;
1460 }
vinayakb38b7ca42012-03-05 05:44:15 +00001461}
1462
1463Identifier Field() throws ParseException:
1464{
Till Westmann14a20a72013-05-09 00:06:24 -07001465 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001466}
1467{
Till Westmanna4242bc2013-05-08 17:49:55 -07001468 "." ident = Identifier()
1469 {
Till Westmann14a20a72013-05-09 00:06:24 -07001470 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001471 }
vinayakb38b7ca42012-03-05 05:44:15 +00001472}
1473
1474int Index() throws ParseException:
1475{
1476 Expression expr = null;
1477 int idx = -2;
1478}
1479{
1480 "[" ( expr = Expression()
1481 {
1482 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1483 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001484 Literal lit = ((LiteralExpr)expr).getValue();
1485 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1486 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001487 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001488 }
vinayakb38b7ca42012-03-05 05:44:15 +00001489 else {
1490 throw new ParseException("Index should be an INTEGER");
1491 }
1492 }
1493
1494 }
1495
1496 | "?"
1497 {
1498 idx = IndexAccessor.ANY;
1499 // ANY
1500 }
1501
1502 )
1503
1504 "]"
1505 {
1506 return idx;
1507 }
1508}
1509
1510
1511Expression PrimaryExpr()throws ParseException:
1512{
1513 Expression expr = null;
1514}
1515{
Till Westmann68d99652013-05-09 11:15:21 -07001516 ( LOOKAHEAD(2)
1517 expr = FunctionCallExpr()
1518 | expr = Literal()
1519 | expr = DatasetAccessExpression()
1520 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001521 {
1522 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001523 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001524 }
Till Westmann68d99652013-05-09 11:15:21 -07001525 | expr = ListConstructor()
1526 | expr = RecordConstructor()
1527 | expr = ParenthesizedExpression()
1528 )
1529 {
1530 return expr;
1531 }
vinayakb38b7ca42012-03-05 05:44:15 +00001532}
1533
1534Expression Literal() throws ParseException:
1535{
vinayakb38b7ca42012-03-05 05:44:15 +00001536 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001537 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001538}
1539{
Till Westmann7d535322013-05-09 00:40:02 -07001540 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001541 {
Till Westmann7d535322013-05-09 00:40:02 -07001542 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001543 }
1544 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001545 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001546 try {
1547 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1548 } catch(NumberFormatException ex) {
1549 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1550 }
1551 }
1552 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001553 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001554 lit.setValue(new FloatLiteral(new Float(token.image)));
1555 }
1556 | <DOUBLE_LITERAL>
1557 {
1558 lit.setValue(new DoubleLiteral(new Double(token.image)));
1559 }
1560 | <NULL>
1561 {
1562 lit.setValue(NullLiteral.INSTANCE);
1563 }
1564 | <TRUE>
1565 {
1566 lit.setValue(TrueLiteral.INSTANCE);
1567 }
1568 | <FALSE>
1569 {
1570 lit.setValue(FalseLiteral.INSTANCE);
1571 }
1572 )
vinayakb38b7ca42012-03-05 05:44:15 +00001573 {
1574 return lit;
1575 }
1576}
1577
1578
1579VariableExpr VariableRef() throws ParseException:
1580{
1581 VariableExpr varExp = new VariableExpr();
1582 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001583}
1584{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001585 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001586 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001587 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001588 Identifier ident = lookupSymbol(varName);
1589 if (isInForbiddenScopes(varName)) {
1590 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.");
1591 }
1592 if(ident != null) { // exist such ident
1593 varExp.setIsNewVar(false);
1594 varExp.setVar((VarIdentifier)ident);
1595 } else {
1596 varExp.setVar(var);
1597 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001598 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001599 return varExp;
1600 }
1601}
1602
1603
1604VariableExpr Variable() throws ParseException:
1605{
1606 VariableExpr varExp = new VariableExpr();
1607 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001608}
1609{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001610 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001611 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001612 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001613 if(ident != null) { // exist such ident
1614 varExp.setIsNewVar(false);
1615 }
1616 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001617 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001618 return varExp;
1619 }
1620}
1621
1622Expression ListConstructor() throws ParseException:
1623{
1624 Expression expr = null;
1625}
1626{
1627 (
1628 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1629 )
1630
1631 {
1632 return expr;
1633 }
1634}
1635
1636
1637ListConstructor OrderedListConstructor() throws ParseException:
1638{
1639 ListConstructor expr = new ListConstructor();
1640 Expression tmp = null;
1641 List<Expression> exprList = new ArrayList<Expression>();
1642 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1643}
1644{
1645
1646 "["
1647 ( tmp = Expression()
1648 {
1649 exprList.add(tmp);
1650 }
1651
1652 ("," tmp = Expression() { exprList.add(tmp); })*
1653 )?
1654
1655 "]"
1656
1657 {
1658 expr.setExprList(exprList);
1659 return expr;
1660 }
1661}
1662
1663ListConstructor UnorderedListConstructor() throws ParseException:
1664{
1665 ListConstructor expr = new ListConstructor();
1666 Expression tmp = null;
1667 List<Expression> exprList = new ArrayList<Expression>();
1668 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1669}
1670{
1671
1672 "{{" ( tmp = Expression()
1673 {
1674 exprList.add(tmp);
1675 }
1676 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1677 {
1678 expr.setExprList(exprList);
1679 return expr;
1680 }
1681}
1682
1683RecordConstructor RecordConstructor() throws ParseException:
1684{
1685 RecordConstructor expr = new RecordConstructor();
1686 FieldBinding tmp = null;
1687 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1688}
1689{
1690 "{" (tmp = FieldBinding()
1691 {
1692 fbList.add(tmp);
1693 }
1694 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1695 {
1696 expr.setFbList(fbList);
1697 return expr;
1698 }
1699}
1700
1701FieldBinding FieldBinding() throws ParseException:
1702{
1703 FieldBinding fb = new FieldBinding();
1704 Expression left, right;
1705}
1706{
1707 left = Expression() ":" right = Expression()
1708 {
1709 fb.setLeftExpr(left);
1710 fb.setRightExpr(right);
1711 return fb;
1712 }
1713}
1714
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001715
vinayakb38b7ca42012-03-05 05:44:15 +00001716Expression FunctionCallExpr() throws ParseException:
1717{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001718 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001719 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001720 Expression tmp;
1721 int arity = 0;
ramangrover299f76a5e2013-06-18 10:25:17 -07001722 FunctionName funcName = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001723 String hint = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001724}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001725{
ramangrover299f76a5e2013-06-18 10:25:17 -07001726 funcName = FunctionName()
vinayakb38b7ca42012-03-05 05:44:15 +00001727 {
Till Westmann31c21f92013-05-08 09:21:53 -07001728 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001729 }
Till Westmann31c21f92013-05-08 09:21:53 -07001730 <LEFTPAREN> (tmp = Expression()
1731 {
1732 argList.add(tmp);
1733 arity ++;
1734 }
1735 ("," tmp = Expression()
1736 {
1737 argList.add(tmp);
1738 arity++;
1739 }
1740 )*)? <RIGHTPAREN>
1741 {
ramangrover299f76a5e2013-06-18 10:25:17 -07001742 // TODO use funcName.library
ramangrover29bdba1a82013-06-21 17:17:52 -07001743 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
ramangrover299f76a5e2013-06-18 10:25:17 -07001744 FunctionSignature signature
ramangrover29bdba1a82013-06-21 17:17:52 -07001745 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001746 if (signature == null) {
ramangrover29bdba1a82013-06-21 17:17:52 -07001747 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
Till Westmann31c21f92013-05-08 09:21:53 -07001748 }
1749 callExpr = new CallExpr(signature,argList);
1750 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1751 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1752 }
1753 return callExpr;
1754 }
vinayakb38b7ca42012-03-05 05:44:15 +00001755}
1756
ramangrover29@gmail.com5f248e12013-04-11 01:03:09 +00001757
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001758Expression DatasetAccessExpression() throws ParseException:
1759{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001760 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001761 String arg1 = null;
1762 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001763 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001764}
1765{
Till Westmann14a20a72013-05-09 00:06:24 -07001766 <DATASET>
1767 {
Till Westmann14a20a72013-05-09 00:06:24 -07001768 funcName = token.image;
1769 }
1770 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1771 {
1772 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001773 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001774 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001775 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001776 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001777 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001778 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001779 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1780 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1781 if (signature == null) {
1782 signature = new FunctionSignature(dataverse, funcName, 1);
1783 }
1784 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001785 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001786 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001787 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001788}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001789
vinayakb38b7ca42012-03-05 05:44:15 +00001790Expression ParenthesizedExpression() throws ParseException:
1791{
1792 Expression expr;
1793}
1794{
1795 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1796 {
1797 return expr;
1798 }
1799}
1800
1801Expression IfThenElse() throws ParseException:
1802{
1803 Expression condExpr;
1804 Expression thenExpr;
1805 Expression elseExpr;
1806 IfExpr ifExpr = new IfExpr();
1807}
1808{
1809 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1810
1811 {
1812 ifExpr.setCondExpr(condExpr);
1813 ifExpr.setThenExpr(thenExpr);
1814 ifExpr.setElseExpr(elseExpr);
1815 return ifExpr;
1816 }
1817}
1818
1819Expression FLWOGR() throws ParseException:
1820{
1821 FLWOGRExpression flworg = new FLWOGRExpression();
1822 List<Clause> clauseList = new ArrayList<Clause>();
1823 Expression returnExpr;
1824 Clause tmp;
1825 createNewScope();
1826}
1827{
1828 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1829 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1830
1831 {
1832 flworg.setClauseList(clauseList);
1833 flworg.setReturnExpr(returnExpr);
1834 removeCurrentScope();
1835 return flworg;
1836 }
1837}
1838
1839Clause Clause()throws ParseException :
1840{
1841 Clause clause;
1842}
1843{
1844 (
1845 clause = ForClause()
1846 | clause = LetClause()
1847 | clause = WhereClause()
1848 | clause = OrderbyClause()
1849 | clause = GroupClause()
1850 | clause = LimitClause()
1851 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001852 )
1853 {
1854 return clause;
1855 }
1856}
1857
1858Clause ForClause()throws ParseException :
1859{
1860 ForClause fc = new ForClause();
1861 VariableExpr varExp;
1862 VariableExpr varPos = null;
1863 Expression inExp;
1864 extendCurrentScope();
1865}
1866{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001867 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001868 {
1869 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001870 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001871 fc.setInExpr(inExp);
1872 if (varPos != null) {
1873 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001874 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001875 }
1876 return fc;
1877 }
1878}
1879
1880Clause LetClause() throws ParseException:
1881{
1882 LetClause lc = new LetClause();
1883 VariableExpr varExp;
1884 Expression beExp;
1885 extendCurrentScope();
1886}
1887{
ilovesoupb2527c12012-07-12 03:21:13 +00001888 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001889 {
1890 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001891 lc.setVarExpr(varExp);
1892 lc.setBeExpr(beExp);
1893 return lc;
1894 }
1895}
1896
1897Clause WhereClause()throws ParseException :
1898{
1899 WhereClause wc = new WhereClause();
1900 Expression whereExpr;
1901}
1902{
1903 "where" whereExpr = Expression()
1904 {
1905 wc.setWhereExpr(whereExpr);
1906 return wc;
1907 }
1908}
1909
1910Clause OrderbyClause()throws ParseException :
1911{
1912 OrderbyClause oc = new OrderbyClause();
1913 Expression orderbyExpr;
1914 List<Expression> orderbyList = new ArrayList<Expression>();
1915 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1916 int numOfOrderby = 0;
1917}
1918{
1919 (
1920 "order"
1921 {
1922 String hint = getHint(token);
1923 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1924 String splits[] = hint.split(" +");
1925 int numFrames = Integer.parseInt(splits[1]);
1926 int numTuples = Integer.parseInt(splits[2]);
1927 oc.setNumFrames(numFrames);
1928 oc.setNumTuples(numTuples);
1929 }
1930 }
1931 "by" orderbyExpr = Expression()
1932 {
1933 orderbyList.add(orderbyExpr);
1934 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1935 }
1936 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1937 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1938 {
1939 modifierList.add(modif);
1940 }
1941
1942 ("," orderbyExpr = Expression()
1943 {
1944 orderbyList.add(orderbyExpr);
1945 modif = OrderbyClause.OrderModifier.ASC;
1946 }
1947 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1948 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1949 {
1950 modifierList.add(modif);
1951 }
1952 )*
1953)
1954 {
1955 oc.setModifierList(modifierList);
1956 oc.setOrderbyList(orderbyList);
1957 return oc;
1958 }
1959}
1960Clause GroupClause()throws ParseException :
1961{
1962 GroupbyClause gbc = new GroupbyClause();
1963 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1964 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1965 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1966 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1967 VariableExpr var = null;
1968 VariableExpr withVar = null;
1969 Expression expr = null;
1970 VariableExpr decorVar = null;
1971 Expression decorExpr = null;
1972}
1973{
1974 {
1975 Scope newScope = extendCurrentScopeNoPush(true);
1976 // extendCurrentScope(true);
1977 }
1978 "group"
1979 {
1980 String hint = getHint(token);
1981 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1982 gbc.setHashGroupByHint(true);
1983 }
1984 }
1985 "by" (LOOKAHEAD(2) var = Variable()
1986 {
1987 newScope.addNewVarSymbolToScope(var.getVar());
1988 } ":=")?
1989 expr = Expression()
1990 {
1991 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1992 vePairList.add(pair1);
1993 }
1994 ("," ( LOOKAHEAD(2) var = Variable()
1995 {
1996 newScope.addNewVarSymbolToScope(var.getVar());
1997 } ":=")?
1998 expr = Expression()
1999 {
2000 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2001 vePairList.add(pair2);
2002 }
2003 )*
2004 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2005 {
2006 newScope.addNewVarSymbolToScope(decorVar.getVar());
2007 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2008 decorPairList.add(pair3);
2009 }
2010 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2011 {
2012 newScope.addNewVarSymbolToScope(decorVar.getVar());
2013 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2014 decorPairList.add(pair4);
2015 }
2016 )*
2017 )?
2018 "with" withVar = VariableRef()
2019 {
2020 if(withVar.getIsNewVar()==true)
2021 throw new ParseException("can't find variable " + withVar.getVar());
2022 withVarList.add(withVar);
2023 newScope.addNewVarSymbolToScope(withVar.getVar());
2024 }
2025 ("," withVar = VariableRef()
2026 {
2027 if(withVar.getIsNewVar()==true)
2028 throw new ParseException("can't find variable " + withVar.getVar());
2029 withVarList.add(withVar);
2030 newScope.addNewVarSymbolToScope(withVar.getVar());
2031 })*
2032 {
2033 gbc.setGbyPairList(vePairList);
2034 gbc.setDecorPairList(decorPairList);
2035 gbc.setWithVarList(withVarList);
2036 replaceCurrentScope(newScope);
2037 return gbc;
2038 }
2039}
2040
2041
2042LimitClause LimitClause() throws ParseException:
2043{
2044 LimitClause lc = new LimitClause();
2045 Expression expr;
2046 pushForbiddenScope(getCurrentScope());
2047}
2048{
2049 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2050 ("offset" expr = Expression() { lc.setOffset(expr); })?
2051
2052 {
2053 popForbiddenScope();
2054 return lc;
2055 }
2056}
2057
2058DistinctClause DistinctClause() throws ParseException:
2059{
2060 List<Expression> exprs = new ArrayList<Expression>();
2061 Expression expr;
2062}
2063{
2064 "distinct" "by" expr = Expression()
2065 {
2066 exprs.add(expr);
2067 }
2068 ("," expr = Expression()
2069 {
2070 exprs.add(expr);
2071 }
2072 )*
2073 {
2074 return new DistinctClause(exprs);
2075 }
2076}
2077
vinayakb38b7ca42012-03-05 05:44:15 +00002078QuantifiedExpression QuantifiedExpression()throws ParseException:
2079{
2080 QuantifiedExpression qc = new QuantifiedExpression();
2081 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2082 Expression satisfiesExpr;
2083 VariableExpr var;
2084 Expression inExpr;
2085 QuantifiedPair pair;
2086}
2087{
2088 {
2089 createNewScope();
2090 }
2091
2092 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2093 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2094 var = Variable() "in" inExpr = Expression()
2095 {
2096 pair = new QuantifiedPair(var, inExpr);
2097 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2098 quantifiedList.add(pair);
2099 }
2100 (
2101 "," var = Variable() "in" inExpr = Expression()
2102 {
2103 pair = new QuantifiedPair(var, inExpr);
2104 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2105 quantifiedList.add(pair);
2106 }
2107 )*
2108 "satisfies" satisfiesExpr = Expression()
2109 {
2110 qc.setSatisfiesExpr(satisfiesExpr);
2111 qc.setQuantifiedList(quantifiedList);
2112 removeCurrentScope();
2113 return qc;
2114 }
2115}
2116
2117TOKEN_MGR_DECLS:
2118{
2119 public int commentDepth = 0;
2120}
2121
2122<DEFAULT>
2123TOKEN :
2124{
2125 <CARET : "^" >
2126}
2127
2128<DEFAULT>
2129TOKEN :
2130{
2131 <DATASET : "dataset" >
2132}
2133
2134<DEFAULT>
2135TOKEN :
2136{
2137 <LEFTPAREN : "(" >
2138}
2139
2140<DEFAULT>
2141TOKEN :
2142{
2143 <RIGHTPAREN : ")" >
2144}
2145
2146
2147<DEFAULT>
2148TOKEN :
2149{
2150 <INTEGER_LITERAL : (<DIGIT>)+ >
2151}
2152
2153
2154<DEFAULT>
2155TOKEN :
2156{
2157 <NULL : "null">
2158}
2159
2160<DEFAULT>
2161TOKEN :
2162{
2163 <TRUE : "true">
2164}
2165
2166<DEFAULT>
2167TOKEN :
2168{
2169 <FALSE : "false">
2170}
2171
2172<DEFAULT>
2173TOKEN :
2174{
2175 <#DIGIT : ["0" - "9"]>
2176}
2177
2178
2179TOKEN:
2180{
2181 < DOUBLE_LITERAL: <INTEGER>
2182 | <INTEGER> ( "." <INTEGER> )?
2183 | "." <INTEGER>
2184 >
2185 |
2186 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2187 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2188 | "." <INTEGER> ( "f" | "F" )
2189 >
2190 |
2191 <INTEGER : (<DIGIT>)+ >
2192}
2193
2194<DEFAULT>
2195TOKEN :
2196{
2197 <#LETTER : ["A" - "Z", "a" - "z"]>
2198}
2199
2200<DEFAULT>
2201TOKEN :
2202{
2203 <SPECIALCHARS : ["$", "_", "-"] >
2204}
2205
2206<DEFAULT>
2207TOKEN :
2208{
2209 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2210 |
2211 < #EscapeQuot: "\\\"" >
2212 |
2213 < #EscapeApos: "\\\'" >
2214}
2215
2216<DEFAULT>
2217TOKEN :
2218{
2219 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2220}
2221
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002222
vinayakb38b7ca42012-03-05 05:44:15 +00002223<DEFAULT>
2224TOKEN :
2225{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002226 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002227}
2228
2229SKIP:
2230{
2231 " "
2232| "\t"
2233| "\r"
2234| "\n"
2235}
2236
2237SKIP:
2238{
2239 <"//" (~["\n"])* "\n">
2240}
2241
2242SKIP:
2243{
2244 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2245}
2246
2247
2248SKIP:
2249{
2250 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2251}
2252
2253<INSIDE_COMMENT>
2254SPECIAL_TOKEN:
2255{
2256 <"+"(" ")*(~["*"])*>
2257}
2258
2259<INSIDE_COMMENT>
2260SKIP:
2261{
2262 <"/*"> {commentDepth++;}
2263}
2264
2265<INSIDE_COMMENT>
2266SKIP:
2267{
2268 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2269| <~[]>
2270}