blob: bd21581542b6d5266301a88c115c9c0a28193cb2 [file] [log] [blame]
vinayakb38b7ca42012-03-05 05:44:15 +00001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package edu.uci.ics.asterix.aql.parser;
12
13import java.io.*;
14import java.util.List;
15import java.util.ArrayList;
16import java.util.Stack;
17
18import java.util.Map;
19import java.util.HashMap;
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +000020import java.util.LinkedHashMap;
vinayakb38b7ca42012-03-05 05:44:15 +000021import edu.uci.ics.asterix.aql.literal.FloatLiteral;
22import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
23import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000024import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000025import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000026import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000027import edu.uci.ics.asterix.aql.literal.NullLiteral;
28import edu.uci.ics.asterix.aql.literal.StringLiteral;
29import edu.uci.ics.asterix.aql.literal.TrueLiteral;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000030import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
vinayakb38b7ca42012-03-05 05:44:15 +000031
32import edu.uci.ics.asterix.aql.base.*;
33import edu.uci.ics.asterix.aql.expression.*;
34import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
35import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
36import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
37import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
38import edu.uci.ics.asterix.aql.base.Statement.Kind;
39import edu.uci.ics.asterix.aql.context.Scope;
40import edu.uci.ics.asterix.aql.context.RootScopeFactory;
41import edu.uci.ics.asterix.common.annotations.*;
42import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000043import edu.uci.ics.asterix.om.functions.AsterixFunction;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000044import edu.uci.ics.asterix.common.functions.FunctionSignature;
alexander.behm07617fd2012-07-25 10:13:50 +000045import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
46import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000047import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
48import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
49import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
50
51
vinayakb38b7ca42012-03-05 05:44:15 +000052
53
54public class AQLParser extends ScopeChecker {
55
vinayakb38b7ca42012-03-05 05:44:15 +000056 // optimizer hints
57 private static final String HASH_GROUP_BY_HINT = "hash";
58 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000059 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000060 private static final String INMEMORY_HINT = "inmem";
61 private static final String VAL_FILE_HINT = "val-files";
62 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
63 private static final String INTERVAL_HINT = "interval";
64 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
65 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
66 private static final String LIST_VAL_FILE_HINT = "list-val-file";
67 private static final String LIST_HINT = "list";
68 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
69 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
70 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
71 private static final String AUTO_HINT = "auto";
72
73 private static final String GEN_FIELDS_HINT = "gen-fields";
74
75 // data generator hints
76 private static final String DGEN_HINT = "dgen";
Till Westmann31c21f92013-05-08 09:21:53 -070077
78 private static class IndexParams {
79 public IndexType type;
80 public int gramLength;
81
82 public IndexParams(IndexType type, int gramLength) {
83 this.type = type;
84 this.gramLength = gramLength;
85 }
86 };
vinayakb38b7ca42012-03-05 05:44:15 +000087
88 private static String getHint(Token t) {
Till Westmann31c21f92013-05-08 09:21:53 -070089 if (t.specialToken == null) {
90 return null;
91 }
92 String s = t.specialToken.image;
93 int n = s.length();
94 if (n < 2) {
95 return null;
96 }
97 return s.substring(1).trim();
vinayakb38b7ca42012-03-05 05:44:15 +000098 }
99
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000100 public AQLParser(String s){
Till Westmann31c21f92013-05-08 09:21:53 -0700101 this(new StringReader(s));
102 super.setInput(s);
103 }
vinayakb38b7ca42012-03-05 05:44:15 +0000104
Till Westmann31c21f92013-05-08 09:21:53 -0700105 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
106 File file = new File(args[0]);
107 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
108 AQLParser parser = new AQLParser(fis);
109 List<Statement> st = parser.Statement();
110 //st.accept(new AQLPrintVisitor(), 0);
111 }
vinayakb38b7ca42012-03-05 05:44:15 +0000112}
113
114PARSER_END(AQLParser)
115
116
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000117List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000118{
vinayakb38b7ca42012-03-05 05:44:15 +0000119 scopeStack.push(RootScopeFactory.createRootScope(this));
120 List<Statement> decls = new ArrayList<Statement>();
Till Westmann31c21f92013-05-08 09:21:53 -0700121 Statement stmt = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000122}
123{
Till Westmann31c21f92013-05-08 09:21:53 -0700124 ( stmt = SingleStatement() (";") ?
vinayakb38b7ca42012-03-05 05:44:15 +0000125 {
Till Westmann31c21f92013-05-08 09:21:53 -0700126 decls.add(stmt);
127 }
128 )*
129 <EOF>
130 {
131 return decls;
132 }
133}
134
135Statement SingleStatement() throws ParseException:
136{
137 Statement stmt = null;
138}
139{
140 (
141 stmt = DataverseDeclaration()
142 | stmt = FunctionDeclaration()
143 | stmt = CreateStatement()
144 | stmt = LoadStatement()
145 | stmt = DropStatement()
146 | stmt = WriteStatement()
147 | stmt = SetStatement()
148 | stmt = InsertStatement()
149 | stmt = DeleteStatement()
150 | stmt = UpdateStatement()
151 | stmt = FeedStatement()
152 | stmt = Query()
153 )
154 {
155 return stmt;
156 }
157}
158
159DataverseDecl DataverseDeclaration() throws ParseException:
160{
Till Westmann14a20a72013-05-09 00:06:24 -0700161 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700162}
163{
Till Westmanna4242bc2013-05-08 17:49:55 -0700164 "use" "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700165 {
Till Westmann14a20a72013-05-09 00:06:24 -0700166 defaultDataverse = dvName;
167 return new DataverseDecl(new Identifier(dvName));
Till Westmann31c21f92013-05-08 09:21:53 -0700168 }
169}
170
171Statement CreateStatement() throws ParseException:
172{
173 String hint = null;
174 boolean dgen = false;
175 Statement stmt = null;
176}
177{
178 "create"
179 (
180 {
181 hint = getHint(token);
182 if (hint != null && hint.startsWith(DGEN_HINT)) {
183 dgen = true;
184 }
185 }
186 stmt = TypeSpecification(hint, dgen)
187 | stmt = NodegroupSpecification()
188 | stmt = DatasetSpecification()
189 | stmt = IndexSpecification()
190 | stmt = DataverseSpecification()
191 | stmt = FunctionSpecification()
192 )
193 {
194 return stmt;
195 }
196}
197
198TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
199{
200 Pair<Identifier,Identifier> nameComponents = null;
201 boolean ifNotExists = false;
202 TypeExpression typeExpr = null;
203}
204{
205 "type" nameComponents = FunctionOrTypeName() ifNotExists = IfNotExists()
206 "as" typeExpr = TypeExpr()
207 {
208 long numValues = -1;
209 String filename = null;
210 if (dgen) {
211 String splits[] = hint.split(" +");
212 if (splits.length != 3) {
213 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
214 }
215 filename = splits[1];
216 numValues = Long.parseLong(splits[2]);
217 }
218 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
219 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
220 }
221}
222
223
224NodegroupDecl NodegroupSpecification() throws ParseException:
225{
Till Westmann14a20a72013-05-09 00:06:24 -0700226 String name = null;
227 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700228 boolean ifNotExists = false;
229 List<Identifier>ncNames = null;
230}
231{
Till Westmanna4242bc2013-05-08 17:49:55 -0700232 "nodegroup" name = Identifier()
233 ifNotExists = IfNotExists() "on" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700234 {
235 ncNames = new ArrayList<Identifier>();
Till Westmann14a20a72013-05-09 00:06:24 -0700236 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700237 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700238 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700239 {
Till Westmann14a20a72013-05-09 00:06:24 -0700240 ncNames.add(new Identifier(tmp));
Till Westmann31c21f92013-05-08 09:21:53 -0700241 }
242 )*
243 {
Till Westmann14a20a72013-05-09 00:06:24 -0700244 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700245 }
246}
247
248DatasetDecl DatasetSpecification() throws ParseException:
249{
250 Pair<Identifier,Identifier> nameComponents = null;
251 boolean ifNotExists = false;
Till Westmann14a20a72013-05-09 00:06:24 -0700252 String typeName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700253 String adapterName = null;
254 Map<String,String> properties = null;
255 FunctionSignature appliedFunction = null;
256 List<String> primaryKeyFields = null;
Till Westmann14a20a72013-05-09 00:06:24 -0700257 String nodeGroupName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700258 Map<String,String> hints = new HashMap<String,String>();
259 DatasetDecl dsetDecl = null;
260}
261{
262 (
Till Westmanna4242bc2013-05-08 17:49:55 -0700263 "external" <DATASET> nameComponents = QualifiedName()
264 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
265 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700266 "using" adapterName = AdapterName() properties = Configuration()
267 ( "hints" hints = Properties() )?
268 {
269 ExternalDetailsDecl edd = new ExternalDetailsDecl();
270 edd.setAdapter(adapterName);
271 edd.setProperties(properties);
Till Westmann14a20a72013-05-09 00:06:24 -0700272 dsetDecl = new DatasetDecl(nameComponents.first,
273 nameComponents.second,
274 new Identifier(typeName),
275 hints,
276 DatasetType.EXTERNAL,
277 edd,
278 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700279 }
280
Till Westmanna4242bc2013-05-08 17:49:55 -0700281 | "feed" <DATASET> nameComponents = QualifiedName()
282 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
283 ifNotExists = IfNotExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700284 "using" adapterName = AdapterName() properties = Configuration()
285 (appliedFunction = ApplyFunction())? primaryKeyFields = PrimaryKey()
Till Westmanna4242bc2013-05-08 17:49:55 -0700286 ( "on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700287 ( "hints" hints = Properties() )?
288 {
Till Westmann14a20a72013-05-09 00:06:24 -0700289 FeedDetailsDecl fdd = new FeedDetailsDecl(adapterName,
290 properties,
291 appliedFunction,
292 nodeGroupName != null
293 ? new Identifier(nodeGroupName)
294 : null,
295 primaryKeyFields);
296 dsetDecl = new DatasetDecl(nameComponents.first,
297 nameComponents.second,
298 new Identifier(typeName),
299 hints,
300 DatasetType.FEED,
301 fdd,
302 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700303 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700304 | ("internal")? <DATASET> nameComponents = QualifiedName()
Till Westmanna4242bc2013-05-08 17:49:55 -0700305 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
306 ifNotExists = IfNotExists()
307 primaryKeyFields = PrimaryKey() ("on" nodeGroupName = Identifier() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700308 ( "hints" hints = Properties() )?
309 {
Till Westmann14a20a72013-05-09 00:06:24 -0700310 InternalDetailsDecl idd = new InternalDetailsDecl(nodeGroupName != null
311 ? new Identifier(nodeGroupName)
312 : null,
313 primaryKeyFields);
314 dsetDecl = new DatasetDecl(nameComponents.first,
315 nameComponents.second,
316 new Identifier(typeName),
317 hints,
318 DatasetType.INTERNAL,
319 idd,
320 ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700321 }
322 )
323 {
324 return dsetDecl;
325 }
326}
327
328CreateIndexStatement IndexSpecification() throws ParseException:
329{
330 CreateIndexStatement cis = new CreateIndexStatement();
Till Westmann14a20a72013-05-09 00:06:24 -0700331 String indexName = null;
332 String fieldExpr = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700333 boolean ifNotExists = false;
334 Pair<Identifier,Identifier> nameComponents = null;
335 IndexParams indexType = null;
336}
337{
Till Westmanna4242bc2013-05-08 17:49:55 -0700338 "index" indexName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700339 ifNotExists = IfNotExists()
340 "on" nameComponents = QualifiedName()
Till Westmann14a20a72013-05-09 00:06:24 -0700341 <LEFTPAREN> ( fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700342 {
Till Westmann14a20a72013-05-09 00:06:24 -0700343 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700344 }
Till Westmann14a20a72013-05-09 00:06:24 -0700345 ) ("," fieldExpr = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700346 {
Till Westmann14a20a72013-05-09 00:06:24 -0700347 cis.addFieldExpr(fieldExpr);
Till Westmann31c21f92013-05-08 09:21:53 -0700348 }
349 )* <RIGHTPAREN> ( "type" indexType = IndexType() )?
350 {
Till Westmann14a20a72013-05-09 00:06:24 -0700351 cis.setIndexName(new Identifier(indexName));
Till Westmann31c21f92013-05-08 09:21:53 -0700352 cis.setIfNotExists(ifNotExists);
353 cis.setDataverseName(nameComponents.first);
354 cis.setDatasetName(nameComponents.second);
355 if (indexType != null) {
356 cis.setIndexType(indexType.type);
357 cis.setGramLength(indexType.gramLength);
358 }
359 return cis;
360 }
361}
362
363IndexParams IndexType() throws ParseException:
364{
365 IndexType type = null;
366 int gramLength = 0;
367}
368{
369 ("btree"
370 {
371 type = IndexType.BTREE;
372 }
373 | "rtree"
374 {
375 type = IndexType.RTREE;
376 }
377 | "keyword"
378 {
Till Westmann31c21f92013-05-08 09:21:53 -0700379 type = IndexType.FUZZY_WORD_INVIX;
380 }
381 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
382 {
Till Westmann31c21f92013-05-08 09:21:53 -0700383 type = IndexType.FUZZY_NGRAM_INVIX;
384 gramLength = Integer.valueOf(token.image);
385 }
386 <RIGHTPAREN>)
387 {
388 return new IndexParams(type, gramLength);
389 }
390}
391
392CreateDataverseStatement DataverseSpecification() throws ParseException :
393{
Till Westmann14a20a72013-05-09 00:06:24 -0700394 String dvName = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700395 boolean ifNotExists = false;
396 String format = null;
397}
398{
Till Westmanna4242bc2013-05-08 17:49:55 -0700399 "dataverse" dvName = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700400 ifNotExists = IfNotExists()
Till Westmann7d535322013-05-09 00:40:02 -0700401 ( "with format" format = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700402 {
Till Westmann14a20a72013-05-09 00:06:24 -0700403 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700404 }
405}
406
407CreateFunctionStatement FunctionSpecification() throws ParseException:
408{
409 FunctionSignature signature;
410 boolean ifNotExists = false;
411 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
412 String functionBody;
Till Westmann31c21f92013-05-08 09:21:53 -0700413 Expression functionBodyExpr;
414 Token beginPos;
415 Token endPos;
416 Pair<Identifier,Identifier> nameComponents=null;
417
418 createNewScope();
419}
420{
421 "function" nameComponents = FunctionOrTypeName()
422 ifNotExists = IfNotExists()
Till Westmannd7dcb122013-05-16 13:19:09 -0700423 paramList = ParameterList()
424 "{"
425 {
426 beginPos = token;
427 }
428 functionBodyExpr = Expression() "}"
429 {
430 endPos = token;
431 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
432 String dataverse = nameComponents.first.getValue();
433 String functionName = nameComponents.second.getValue();
434 signature = new FunctionSignature(dataverse, functionName, paramList.size());
435 getCurrentScope().addFunctionDescriptor(signature, false);
436 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
437 }
438}
439
440List<VarIdentifier> ParameterList() throws ParseException:
441{
442 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
443 VarIdentifier var = null;
444}
445{
Till Westmann31c21f92013-05-08 09:21:53 -0700446 <LEFTPAREN> (<VARIABLE>
447 {
448 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700449 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700450 paramList.add(var);
451 getCurrentScope().addNewVarSymbolToScope(var);
452 }
453 ("," <VARIABLE>
454 {
455 var = new VarIdentifier();
Till Westmanna4242bc2013-05-08 17:49:55 -0700456 var.setValue(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700457 paramList.add(var);
458 getCurrentScope().addNewVarSymbolToScope(var);
459 }
Till Westmannd7dcb122013-05-16 13:19:09 -0700460 )*)? <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700461 {
Till Westmannd7dcb122013-05-16 13:19:09 -0700462 return paramList;
Till Westmann31c21f92013-05-08 09:21:53 -0700463 }
464}
465
466boolean IfNotExists() throws ParseException:
467{
468}
469{
470 ( "if not exists"
471 {
472 return true;
473 }
474 )?
475 {
476 return false;
477 }
478}
479
480FunctionSignature ApplyFunction() throws ParseException:
481{
482 FunctionSignature funcSig = null;
483}
484{
485 "apply" "function" funcSig = FunctionSignature()
486 {
487 return funcSig;
488 }
489}
490
491FunctionSignature FunctionSignature() throws ParseException:
492{
493 Pair<Identifier,Identifier> pairId = null;
494 int arity = 0;
495}
496{
497 pairId = FunctionOrTypeName() "@" <INTEGER_LITERAL>
498 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700499 arity = new Integer(token.image);
Till Westmann31c21f92013-05-08 09:21:53 -0700500 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
501 throw new ParseException(" invalid arity:" + arity);
502 }
503
504 String dataverse = pairId.first.getValue();
505 String functionName = pairId.second.getValue();
506 return new FunctionSignature(dataverse, functionName, arity);
507 }
508}
509
510List<String> PrimaryKey() throws ParseException:
511{
Till Westmann14a20a72013-05-09 00:06:24 -0700512 String tmp = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700513 List<String> primaryKeyFields = new ArrayList<String>();
514}
515{
Till Westmann14a20a72013-05-09 00:06:24 -0700516 "primary" "key" tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700517 {
Till Westmann14a20a72013-05-09 00:06:24 -0700518 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700519 }
Till Westmann14a20a72013-05-09 00:06:24 -0700520 ( "," tmp = Identifier()
Till Westmann31c21f92013-05-08 09:21:53 -0700521 {
Till Westmann14a20a72013-05-09 00:06:24 -0700522 primaryKeyFields.add(tmp);
Till Westmann31c21f92013-05-08 09:21:53 -0700523 }
524 )*
525 {
526 return primaryKeyFields;
527 }
528}
529
530Statement DropStatement() throws ParseException:
531{
Till Westmann14a20a72013-05-09 00:06:24 -0700532 String id = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700533 Pair<Identifier,Identifier> pairId = null;
534 Triple<Identifier,Identifier,Identifier> tripleId = null;
535 FunctionSignature funcSig = null;
536 boolean ifExists = false;
537 Statement stmt = null;
538}
539{
540 "drop"
541 (
542 <DATASET> pairId = QualifiedName() ifExists = IfExists()
543 {
544 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
545 }
546 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
547 {
548 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
549 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700550 | "nodegroup" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700551 {
Till Westmann14a20a72013-05-09 00:06:24 -0700552 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700553 }
554 | "type" pairId = FunctionOrTypeName() ifExists = IfExists()
555 {
556 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
557 }
Till Westmanna4242bc2013-05-08 17:49:55 -0700558 | "dataverse" id = Identifier() ifExists = IfExists()
Till Westmann31c21f92013-05-08 09:21:53 -0700559 {
Till Westmann14a20a72013-05-09 00:06:24 -0700560 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
Till Westmann31c21f92013-05-08 09:21:53 -0700561 }
562 | "function" funcSig = FunctionSignature() ifExists = IfExists()
563 {
564 stmt = new FunctionDropStatement(funcSig, ifExists);
565 }
566 )
567 {
568 return stmt;
569 }
570}
571
572boolean IfExists() throws ParseException :
573{
574}
575{
576 ( "if" "exists"
577 {
578 return true;
579 }
580 )?
581 {
582 return false;
vinayakb38b7ca42012-03-05 05:44:15 +0000583 }
584}
585
586InsertStatement InsertStatement() throws ParseException:
587{
Till Westmann31c21f92013-05-08 09:21:53 -0700588 Pair<Identifier,Identifier> nameComponents = null;
589 Query query;
vinayakb38b7ca42012-03-05 05:44:15 +0000590}
591{
Till Westmann31c21f92013-05-08 09:21:53 -0700592 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
593 {
594 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
595 }
vinayakb38b7ca42012-03-05 05:44:15 +0000596}
597
598DeleteStatement DeleteStatement() throws ParseException:
599{
Till Westmann31c21f92013-05-08 09:21:53 -0700600 VariableExpr var = null;
601 Expression condition = null;
602 Pair<Identifier, Identifier> nameComponents;
vinayakb38b7ca42012-03-05 05:44:15 +0000603}
604{
Till Westmann31c21f92013-05-08 09:21:53 -0700605 "delete" var = Variable()
606 {
607 getCurrentScope().addNewVarSymbolToScope(var.getVar());
608 }
609 "from" <DATASET> nameComponents = QualifiedName()
610 ("where" condition = Expression())?
611 {
612 return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition, getVarCounter());
613 }
vinayakb38b7ca42012-03-05 05:44:15 +0000614}
615
616UpdateStatement UpdateStatement() throws ParseException:
617{
Till Westmann31c21f92013-05-08 09:21:53 -0700618 VariableExpr vars;
619 Expression target;
620 Expression condition;
621 UpdateClause uc;
622 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
vinayakb38b7ca42012-03-05 05:44:15 +0000623}
624{
Till Westmann31c21f92013-05-08 09:21:53 -0700625 "update" vars = Variable() "in" target = Expression()
626 "where" condition = Expression()
627 <LEFTPAREN> (uc = UpdateClause()
628 {
629 ucs.add(uc);
630 }
631 ("," uc = UpdateClause()
632 {
633 ucs.add(uc);
634 }
635 )*) <RIGHTPAREN>
636 {
637 return new UpdateStatement(vars, target, condition, ucs);
638 }
vinayakb38b7ca42012-03-05 05:44:15 +0000639}
640
vinayakb38b7ca42012-03-05 05:44:15 +0000641UpdateClause UpdateClause() throws ParseException:
642{
Till Westmann31c21f92013-05-08 09:21:53 -0700643 Expression target = null;
644 Expression value = null ;
645 InsertStatement is = null;
646 DeleteStatement ds = null;
647 UpdateStatement us = null;
648 Expression condition = null;
649 UpdateClause ifbranch = null;
650 UpdateClause elsebranch = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000651}
652{
653 "set" target = Expression() ":=" value = Expression()
Till Westmann31c21f92013-05-08 09:21:53 -0700654 | is = InsertStatement()
655 | ds = DeleteStatement()
656 | us = UpdateStatement()
657 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN>
658 "then" ifbranch = UpdateClause()
659 [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
660 {
661 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
662 }
vinayakb38b7ca42012-03-05 05:44:15 +0000663}
664
vinayakb38b7ca42012-03-05 05:44:15 +0000665Statement SetStatement() throws ParseException:
666{
667 String pn = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700668 String pv = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000669}
670{
Till Westmann7d535322013-05-09 00:40:02 -0700671 "set" pn = Identifier() pv = StringLiteral()
Till Westmann31c21f92013-05-08 09:21:53 -0700672 {
Till Westmann31c21f92013-05-08 09:21:53 -0700673 return new SetStatement(pn, pv);
674 }
vinayakb38b7ca42012-03-05 05:44:15 +0000675}
676
677Statement WriteStatement() throws ParseException:
678{
Till Westmann14a20a72013-05-09 00:06:24 -0700679 String nodeName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000680 String fileName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000681 Statement stmt = null;
682 Query query;
683 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000684 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000685}
686{
Till Westmann31c21f92013-05-08 09:21:53 -0700687 "write" ((
Till Westmann7d535322013-05-09 00:40:02 -0700688 "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
689 ( "using" writerClass = StringLiteral() )?
Till Westmann31c21f92013-05-08 09:21:53 -0700690 {
Till Westmann14a20a72013-05-09 00:06:24 -0700691 stmt = new WriteStatement(new Identifier(nodeName), fileName, writerClass);
Till Westmann31c21f92013-05-08 09:21:53 -0700692 }
693 ) | (
694 "into" <DATASET>
695 {
696 nameComponents = QualifiedName();
697 }
698 <LEFTPAREN> query = Query() <RIGHTPAREN>
699 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000700 stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
Till Westmann31c21f92013-05-08 09:21:53 -0700701 }
702 ))
vinayakb38b7ca42012-03-05 05:44:15 +0000703 {
704 return stmt;
705 }
706}
707
vinayakb38b7ca42012-03-05 05:44:15 +0000708LoadFromFileStatement LoadStatement() throws ParseException:
709{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000710 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000711 Identifier datasetName = null;
712 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000713 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000714 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000715 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000716}
717{
Till Westmann31c21f92013-05-08 09:21:53 -0700718 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000719 {
Till Westmann31c21f92013-05-08 09:21:53 -0700720 dataverseName = nameComponents.first;
721 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000722 }
Till Westmann31c21f92013-05-08 09:21:53 -0700723 "using" adapterName = AdapterName() properties = Configuration()
724 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000725 {
Till Westmann31c21f92013-05-08 09:21:53 -0700726 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000727 }
728 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700729 {
730 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
731 }
vinayakb38b7ca42012-03-05 05:44:15 +0000732}
733
vinayakb38b7ca42012-03-05 05:44:15 +0000734
Till Westmann31c21f92013-05-08 09:21:53 -0700735String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000736{
ramangrover29669d8f62013-02-11 06:03:32 +0000737 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000738}
739{
Till Westmann68d99652013-05-09 11:15:21 -0700740 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000741 {
Till Westmann7d535322013-05-09 00:40:02 -0700742 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000743 }
vinayakb38b7ca42012-03-05 05:44:15 +0000744}
745
Till Westmann31c21f92013-05-08 09:21:53 -0700746Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000747{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000748 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700749 Map<String,String> configuration = null;
750 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000751}
752{
Till Westmann31c21f92013-05-08 09:21:53 -0700753 (
754 "begin" "feed" nameComponents = QualifiedName()
755 {
756 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
757 }
758 | "suspend" "feed" nameComponents = QualifiedName()
759 {
760 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
761 }
762 | "resume" "feed" nameComponents = QualifiedName()
763 {
764 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
765 }
766 | "end" "feed" nameComponents = QualifiedName()
767 {
768 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
769 }
770 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
771 {
772 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
773 }
774 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000775 {
Till Westmann31c21f92013-05-08 09:21:53 -0700776 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000777 }
778}
779
Till Westmann31c21f92013-05-08 09:21:53 -0700780Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000781{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000782 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700783 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000784}
785{
Till Westmann31c21f92013-05-08 09:21:53 -0700786 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000787 {
Till Westmann31c21f92013-05-08 09:21:53 -0700788 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000789 }
Till Westmann31c21f92013-05-08 09:21:53 -0700790 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000791 {
Till Westmann31c21f92013-05-08 09:21:53 -0700792 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000793 }
Till Westmann31c21f92013-05-08 09:21:53 -0700794 )* )? <RIGHTPAREN>
795 {
796 return configuration;
797 }
798}
799
800Pair<String, String> KeyValuePair() throws ParseException:
801{
802 String key;
803 String value;
804}
805{
Till Westmann7d535322013-05-09 00:40:02 -0700806 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700807 {
808 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000809 }
Till Westmann31c21f92013-05-08 09:21:53 -0700810}
811
812Map<String,String> Properties() throws ParseException:
813{
814 Map<String,String> properties = new HashMap<String,String>();
815 Pair<String, String> property;
816}
817{
818 ( <LEFTPAREN> property = Property()
819 {
820 properties.put(property.first, property.second);
821 }
822 ( "," property = Property()
823 {
824 properties.put(property.first, property.second);
825 }
826 )* <RIGHTPAREN> )?
827 {
828 return properties;
829 }
830}
831
832Pair<String, String> Property() throws ParseException:
833{
834 String key;
835 String value;
836}
837{
Till Westmann7d535322013-05-09 00:40:02 -0700838 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700839 {
840 try {
841 value = "" + Long.valueOf(token.image);
842 } catch (NumberFormatException nfe) {
843 throw new ParseException("inapproriate value: " + token.image);
844 }
845 }
846 )
847 {
848 return new Pair<String, String>(key.toUpperCase(), value);
849 }
vinayakb38b7ca42012-03-05 05:44:15 +0000850}
851
852TypeExpression TypeExpr() throws ParseException:
853{
854 TypeExpression typeExpr = null;
855}
856{
857 (
858 typeExpr = RecordTypeDef()
859 | typeExpr = TypeReference()
860 | typeExpr = OrderedListTypeDef()
861 | typeExpr = UnorderedListTypeDef()
862 )
863 {
864 return typeExpr;
865 }
866}
867
868RecordTypeDefinition RecordTypeDef() throws ParseException:
869{
870 RecordTypeDefinition recType = new RecordTypeDefinition();
871 RecordTypeDefinition.RecordKind recordKind = null;
872}
873{
874 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
875 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
876 "{"
877 {
878 String hint = getHint(token);
879 if (hint != null) {
880 String splits[] = hint.split(" +");
881 if (splits[0].equals(GEN_FIELDS_HINT)) {
882 if (splits.length != 5) {
883 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
884 }
885 if (!splits[1].equals("int")) {
886 throw new ParseException("The only supported type for gen-fields is int.");
887 }
888 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
889 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
890 recType.setUndeclaredFieldsDataGen(ufdg);
891 }
892 }
893
894 }
895 (
896 RecordField(recType)
897 ( "," RecordField(recType) )*
898 )?
899 "}"
900 {
901 if (recordKind == null) {
902 recordKind = RecordTypeDefinition.RecordKind.OPEN;
903 }
904 recType.setRecordKind(recordKind);
905 return recType;
906 }
907}
908
909void RecordField(RecordTypeDefinition recType) throws ParseException:
910{
911 String fieldName;
912 TypeExpression type = null;
913 boolean nullable = false;
914}
915{
Till Westmann14a20a72013-05-09 00:06:24 -0700916 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000917 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700918 fieldName = token.image;
919 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000920 IRecordFieldDataGen rfdg = null;
921 if (hint != null) {
922 String splits[] = hint.split(" +");
923 if (splits[0].equals(VAL_FILE_HINT)) {
924 File[] valFiles = new File[splits.length - 1];
925 for (int k=1; k<splits.length; k++) {
926 valFiles[k-1] = new File(splits[k]);
927 }
928 rfdg = new FieldValFileDataGen(valFiles);
929 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
930 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
931 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
932 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
933 } else if (splits[0].equals(LIST_HINT)) {
934 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
935 } else if (splits[0].equals(INTERVAL_HINT)) {
936 FieldIntervalDataGen.ValueType vt;
937 if (splits[1].equals("int")) {
938 vt = FieldIntervalDataGen.ValueType.INT;
939 } else if (splits[1].equals("long")) {
940 vt = FieldIntervalDataGen.ValueType.LONG;
941 } else if (splits[1].equals("float")) {
942 vt = FieldIntervalDataGen.ValueType.FLOAT;
943 } else if (splits[1].equals("double")) {
944 vt = FieldIntervalDataGen.ValueType.DOUBLE;
945 } else {
946 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
947 }
948 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
949 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
950 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
951 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
952 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
953 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
954 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
955 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
956 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
957 } else if (splits[0].equals(AUTO_HINT)) {
958 rfdg = new AutoDataGen(splits[1]);
959 }
960 }
961 }
962 ":"
963 ( type = TypeExpr() )
964 ("?" { nullable = true; } )?
965 {
966 recType.addField(fieldName, type, nullable, rfdg);
967 }
968}
969
970TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000971{
Till Westmann14a20a72013-05-09 00:06:24 -0700972 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700973}
974{
975 id = Identifier()
976 {
Till Westmann14a20a72013-05-09 00:06:24 -0700977 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700978 }
vinayakb38b7ca42012-03-05 05:44:15 +0000979}
980
981OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
982{
983 TypeExpression type = null;
984}
985{
986 "["
987 ( type = TypeExpr() )
988 "]"
989 {
990 return new OrderedListTypeDefinition(type);
991 }
992}
993
994
995UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
996{
997 TypeExpression type = null;
998}
999{
1000 "{{"
1001 ( type = TypeExpr() )
1002 "}}"
1003 {
1004 return new UnorderedListTypeDefinition(type);
1005 }
1006}
1007
Till Westmann31c21f92013-05-08 09:21:53 -07001008
1009Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001010{
Till Westmann31c21f92013-05-08 09:21:53 -07001011 Pair<Identifier,Identifier> name = null;
1012}
1013{
1014 name = QualifiedName()
1015 {
1016 if (name.first == null) {
1017 name.first = new Identifier(defaultDataverse);
1018 }
1019 return name;
1020 }
1021}
1022
Till Westmann14a20a72013-05-09 00:06:24 -07001023String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001024{
Till Westmann68d99652013-05-09 11:15:21 -07001025 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001026}
1027{
1028 <IDENTIFIER>
1029 {
Till Westmann14a20a72013-05-09 00:06:24 -07001030 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001031 }
Till Westmann68d99652013-05-09 11:15:21 -07001032 | lit = StringLiteral()
1033 {
1034 return lit;
1035 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001036}
1037
Till Westmann7d535322013-05-09 00:40:02 -07001038String StringLiteral() throws ParseException:
1039{
1040}
1041{
1042 <STRING_LITERAL>
1043 {
1044 return removeQuotesAndEscapes(token.image);
1045 }
1046}
1047
Till Westmann31c21f92013-05-08 09:21:53 -07001048Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1049{
Till Westmann14a20a72013-05-09 00:06:24 -07001050 String first = null;
1051 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001052}
1053{
Till Westmanna4242bc2013-05-08 17:49:55 -07001054 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001055 {
Till Westmann14a20a72013-05-09 00:06:24 -07001056 Identifier id1 = null;
1057 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001058 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001059 id2 = new Identifier(first);
1060 } else
1061 {
1062 id1 = new Identifier(first);
1063 id2 = new Identifier(second);
1064 }
1065 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001066 }
1067}
1068
Till Westmann31c21f92013-05-08 09:21:53 -07001069Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001070{
Till Westmann14a20a72013-05-09 00:06:24 -07001071 String first = null;
1072 String second = null;
1073 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001074}
1075{
Till Westmanna4242bc2013-05-08 17:49:55 -07001076 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001077 {
Till Westmann14a20a72013-05-09 00:06:24 -07001078 Identifier id1 = null;
1079 Identifier id2 = null;
1080 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001081 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001082 id2 = new Identifier(first);
1083 id3 = new Identifier(second);
1084 } else {
1085 id1 = new Identifier(first);
1086 id2 = new Identifier(second);
1087 id3 = new Identifier(third);
1088 }
1089 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001090 }
1091}
1092
vinayakb38b7ca42012-03-05 05:44:15 +00001093FunctionDecl FunctionDeclaration() throws ParseException:
1094{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001095 FunctionDecl funcDecl;
1096 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001097 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001098 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1099 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001100 createNewScope();
1101}
1102{
Till Westmannd7dcb122013-05-16 13:19:09 -07001103 "declare" "function" functionName = Identifier()
1104 paramList = ParameterList()
1105 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001106 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001107 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001108 getCurrentScope().addFunctionDescriptor(signature, false);
1109 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001110 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001111 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001112 }
1113}
1114
vinayakb38b7ca42012-03-05 05:44:15 +00001115
Till Westmann31c21f92013-05-08 09:21:53 -07001116Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001117{
1118 Query query = new Query();
1119 Expression expr;
1120}
1121{
Till Westmann31c21f92013-05-08 09:21:53 -07001122 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001123 {
1124 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001125 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001126 return query;
1127 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001128
vinayakb38b7ca42012-03-05 05:44:15 +00001129}
1130
1131
1132
1133Expression Expression():
1134{
1135 Expression expr = null;
1136 Expression exprP = null;
1137}
1138{
1139(
1140
1141//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1142 expr = OperatorExpr()
1143 | expr = IfThenElse()
1144 | expr = FLWOGR()
1145 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001146
vinayakb38b7ca42012-03-05 05:44:15 +00001147
1148)
1149 {
1150 return (exprP==null) ? expr : exprP;
1151 }
1152}
1153
1154
1155
1156Expression OperatorExpr()throws ParseException:
1157{
1158 OperatorExpr op = null;
1159 Expression operand = null;
1160}
1161{
1162 operand = AndExpr()
1163 (
1164
1165 "or"
1166 {
1167 if (op == null) {
1168 op = new OperatorExpr();
1169 op.addOperand(operand);
1170 op.setCurrentop(true);
1171 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001172 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001173 }
1174
1175 operand = AndExpr()
1176 {
1177 op.addOperand(operand);
1178 }
1179
1180 )*
1181
1182 {
1183 return op==null? operand: op;
1184 }
1185}
1186
1187Expression AndExpr()throws ParseException:
1188{
1189 OperatorExpr op = null;
1190 Expression operand = null;
1191}
1192{
1193 operand = RelExpr()
1194 (
1195
1196 "and"
1197 {
1198 if (op == null) {
1199 op = new OperatorExpr();
1200 op.addOperand(operand);
1201 op.setCurrentop(true);
1202 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001203 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001204 }
1205
1206 operand = RelExpr()
1207 {
1208 op.addOperand(operand);
1209 }
1210
1211 )*
1212
1213 {
1214 return op==null? operand: op;
1215 }
1216}
1217
1218
1219
1220Expression RelExpr()throws ParseException:
1221{
1222 OperatorExpr op = null;
1223 Expression operand = null;
1224 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001225 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001226}
1227{
1228 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001229 {
1230 if (operand instanceof VariableExpr) {
1231 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001232 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001233 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001234 }
1235 }
1236 }
1237
1238 (
1239 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1240 {
alexander.behm07617fd2012-07-25 10:13:50 +00001241 String mhint = getHint(token);
1242 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1243 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1244 }
vinayakb38b7ca42012-03-05 05:44:15 +00001245 if (op == null) {
1246 op = new OperatorExpr();
1247 op.addOperand(operand, broadcast);
1248 op.setCurrentop(true);
1249 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001250 }
1251 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001252 }
1253
1254 operand = AddExpr()
1255 {
alexander.behm07617fd2012-07-25 10:13:50 +00001256 broadcast = false;
1257 if (operand instanceof VariableExpr) {
1258 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001259 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1260 broadcast = true;
1261 }
alexander.behm07617fd2012-07-25 10:13:50 +00001262 }
vinayakb38b7ca42012-03-05 05:44:15 +00001263 op.addOperand(operand, broadcast);
1264 }
1265 )?
1266
1267 {
alexander.behm07617fd2012-07-25 10:13:50 +00001268 if (annotation != null) {
1269 op.addHint(annotation);
1270 }
vinayakb38b7ca42012-03-05 05:44:15 +00001271 return op==null? operand: op;
1272 }
1273}
1274
1275Expression AddExpr()throws ParseException:
1276{
1277 OperatorExpr op = null;
1278 Expression operand = null;
1279}
1280{
1281 operand = MultExpr()
1282
1283 ( ("+" | "-")
1284 {
1285 if (op == null) {
1286 op = new OperatorExpr();
1287 op.addOperand(operand);
1288 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001289 }
1290 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001291 }
1292
1293 operand = MultExpr()
1294 {
1295 op.addOperand(operand);
1296 }
1297 )*
1298
1299 {
1300 return op==null? operand: op;
1301 }
1302}
1303
1304Expression MultExpr()throws ParseException:
1305{
1306 OperatorExpr op = null;
1307 Expression operand = null;
1308}
1309{
1310 operand = UnionExpr()
1311
1312 (( "*" | "/" | "%" | <CARET> | "idiv")
1313 {
1314 if (op == null) {
1315 op = new OperatorExpr();
1316 op.addOperand(operand);
1317 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001318 }
1319 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001320 }
1321 operand = UnionExpr()
1322 {
1323 op.addOperand(operand);
1324 }
1325 )*
1326
1327 {
1328 return op==null?operand:op;
1329 }
1330}
1331
1332Expression UnionExpr() throws ParseException:
1333{
1334 UnionExpr union = null;
1335 Expression operand1 = null;
1336 Expression operand2 = null;
1337}
1338{
1339 operand1 = UnaryExpr()
1340 ("union"
1341 (operand2 = UnaryExpr()) {
1342 if (union == null) {
1343 union = new UnionExpr();
1344 union.addExpr(operand1);
1345 }
1346 union.addExpr(operand2);
1347 } )*
1348 {
1349 return (union == null)? operand1: union;
1350 }
1351}
1352
1353Expression UnaryExpr() throws ParseException:
1354{
1355 Expression uexpr = null;
1356 Expression expr = null;
1357}
1358{
1359 (( "+"|"-")
1360 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001361 uexpr = new UnaryExpr();
1362 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001363 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001364 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001365 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1366 else
1367 throw new ParseException();
1368 }
1369 )?
1370
1371 expr = ValueExpr()
1372 {
1373 if(uexpr!=null){
1374 ((UnaryExpr)uexpr).setExpr(expr);
1375 return uexpr;
1376 }
1377 else{
1378 return expr;
1379 }
1380 }
1381}
1382
Till Westmann04478e72013-05-13 23:01:34 -07001383Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001384{
1385 Expression expr = null;
1386 Identifier ident = null;
1387 AbstractAccessor fa = null;
1388 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001389}
1390{
Till Westmann04478e72013-05-13 23:01:34 -07001391 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001392 {
Till Westmann04478e72013-05-13 23:01:34 -07001393 fa = (fa == null ? new FieldAccessor(expr, ident)
1394 : new FieldAccessor(fa, ident));
1395 }
1396 | index = Index()
1397 {
1398 fa = (fa == null ? new IndexAccessor(expr, index)
1399 : new IndexAccessor(fa, index));
1400 }
1401 )*
1402 {
1403 return fa == null ? expr : fa;
1404 }
vinayakb38b7ca42012-03-05 05:44:15 +00001405}
1406
1407Identifier Field() throws ParseException:
1408{
Till Westmann14a20a72013-05-09 00:06:24 -07001409 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001410}
1411{
Till Westmanna4242bc2013-05-08 17:49:55 -07001412 "." ident = Identifier()
1413 {
Till Westmann14a20a72013-05-09 00:06:24 -07001414 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001415 }
vinayakb38b7ca42012-03-05 05:44:15 +00001416}
1417
1418int Index() throws ParseException:
1419{
1420 Expression expr = null;
1421 int idx = -2;
1422}
1423{
1424 "[" ( expr = Expression()
1425 {
1426 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1427 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001428 Literal lit = ((LiteralExpr)expr).getValue();
1429 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1430 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001431 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001432 }
vinayakb38b7ca42012-03-05 05:44:15 +00001433 else {
1434 throw new ParseException("Index should be an INTEGER");
1435 }
1436 }
1437
1438 }
1439
1440 | "?"
1441 {
1442 idx = IndexAccessor.ANY;
1443 // ANY
1444 }
1445
1446 )
1447
1448 "]"
1449 {
1450 return idx;
1451 }
1452}
1453
1454
1455Expression PrimaryExpr()throws ParseException:
1456{
1457 Expression expr = null;
1458}
1459{
Till Westmann68d99652013-05-09 11:15:21 -07001460 ( LOOKAHEAD(2)
1461 expr = FunctionCallExpr()
1462 | expr = Literal()
1463 | expr = DatasetAccessExpression()
1464 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001465 {
1466 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001467 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001468 }
Till Westmann68d99652013-05-09 11:15:21 -07001469 | expr = ListConstructor()
1470 | expr = RecordConstructor()
1471 | expr = ParenthesizedExpression()
1472 )
1473 {
1474 return expr;
1475 }
vinayakb38b7ca42012-03-05 05:44:15 +00001476}
1477
1478Expression Literal() throws ParseException:
1479{
vinayakb38b7ca42012-03-05 05:44:15 +00001480 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001481 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001482}
1483{
Till Westmann7d535322013-05-09 00:40:02 -07001484 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001485 {
Till Westmann7d535322013-05-09 00:40:02 -07001486 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001487 }
1488 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001489 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001490 try {
1491 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1492 } catch(NumberFormatException ex) {
1493 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1494 }
1495 }
1496 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001497 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001498 lit.setValue(new FloatLiteral(new Float(token.image)));
1499 }
1500 | <DOUBLE_LITERAL>
1501 {
1502 lit.setValue(new DoubleLiteral(new Double(token.image)));
1503 }
1504 | <NULL>
1505 {
1506 lit.setValue(NullLiteral.INSTANCE);
1507 }
1508 | <TRUE>
1509 {
1510 lit.setValue(TrueLiteral.INSTANCE);
1511 }
1512 | <FALSE>
1513 {
1514 lit.setValue(FalseLiteral.INSTANCE);
1515 }
1516 )
vinayakb38b7ca42012-03-05 05:44:15 +00001517 {
1518 return lit;
1519 }
1520}
1521
1522
1523VariableExpr VariableRef() throws ParseException:
1524{
1525 VariableExpr varExp = new VariableExpr();
1526 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001527}
1528{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001529 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001530 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001531 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001532 Identifier ident = lookupSymbol(varName);
1533 if (isInForbiddenScopes(varName)) {
1534 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.");
1535 }
1536 if(ident != null) { // exist such ident
1537 varExp.setIsNewVar(false);
1538 varExp.setVar((VarIdentifier)ident);
1539 } else {
1540 varExp.setVar(var);
1541 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001542 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001543 return varExp;
1544 }
1545}
1546
1547
1548VariableExpr Variable() throws ParseException:
1549{
1550 VariableExpr varExp = new VariableExpr();
1551 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001552}
1553{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001554 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001555 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001556 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001557 if(ident != null) { // exist such ident
1558 varExp.setIsNewVar(false);
1559 }
1560 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001561 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001562 return varExp;
1563 }
1564}
1565
1566Expression ListConstructor() throws ParseException:
1567{
1568 Expression expr = null;
1569}
1570{
1571 (
1572 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1573 )
1574
1575 {
1576 return expr;
1577 }
1578}
1579
1580
1581ListConstructor OrderedListConstructor() throws ParseException:
1582{
1583 ListConstructor expr = new ListConstructor();
1584 Expression tmp = null;
1585 List<Expression> exprList = new ArrayList<Expression>();
1586 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1587}
1588{
1589
1590 "["
1591 ( tmp = Expression()
1592 {
1593 exprList.add(tmp);
1594 }
1595
1596 ("," tmp = Expression() { exprList.add(tmp); })*
1597 )?
1598
1599 "]"
1600
1601 {
1602 expr.setExprList(exprList);
1603 return expr;
1604 }
1605}
1606
1607ListConstructor UnorderedListConstructor() throws ParseException:
1608{
1609 ListConstructor expr = new ListConstructor();
1610 Expression tmp = null;
1611 List<Expression> exprList = new ArrayList<Expression>();
1612 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1613}
1614{
1615
1616 "{{" ( tmp = Expression()
1617 {
1618 exprList.add(tmp);
1619 }
1620 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1621 {
1622 expr.setExprList(exprList);
1623 return expr;
1624 }
1625}
1626
1627RecordConstructor RecordConstructor() throws ParseException:
1628{
1629 RecordConstructor expr = new RecordConstructor();
1630 FieldBinding tmp = null;
1631 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1632}
1633{
1634 "{" (tmp = FieldBinding()
1635 {
1636 fbList.add(tmp);
1637 }
1638 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1639 {
1640 expr.setFbList(fbList);
1641 return expr;
1642 }
1643}
1644
1645FieldBinding FieldBinding() throws ParseException:
1646{
1647 FieldBinding fb = new FieldBinding();
1648 Expression left, right;
1649}
1650{
1651 left = Expression() ":" right = Expression()
1652 {
1653 fb.setLeftExpr(left);
1654 fb.setRightExpr(right);
1655 return fb;
1656 }
1657}
1658
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001659
vinayakb38b7ca42012-03-05 05:44:15 +00001660Expression FunctionCallExpr() throws ParseException:
1661{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001662 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001663 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001664 Expression tmp;
1665 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001666 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001667 String funcName;
1668 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001669 String hint = null;
1670 String id1 = null;
1671 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001672}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001673{
Till Westmann31c21f92013-05-08 09:21:53 -07001674 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001675 {
Till Westmann31c21f92013-05-08 09:21:53 -07001676 dataverse = funcId.first.getValue();
1677 funcName = funcId.second.getValue();
1678 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001679 }
Till Westmann31c21f92013-05-08 09:21:53 -07001680 <LEFTPAREN> (tmp = Expression()
1681 {
1682 argList.add(tmp);
1683 arity ++;
1684 }
1685 ("," tmp = Expression()
1686 {
1687 argList.add(tmp);
1688 arity++;
1689 }
1690 )*)? <RIGHTPAREN>
1691 {
1692 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1693 if (signature == null) {
1694 signature = new FunctionSignature(dataverse, funcName, arity);
1695 }
1696 callExpr = new CallExpr(signature,argList);
1697 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1698 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1699 }
1700 return callExpr;
1701 }
vinayakb38b7ca42012-03-05 05:44:15 +00001702}
1703
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001704Expression DatasetAccessExpression() throws ParseException:
1705{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001706 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001707 String arg1 = null;
1708 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001709 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001710}
1711{
Till Westmann14a20a72013-05-09 00:06:24 -07001712 <DATASET>
1713 {
Till Westmann14a20a72013-05-09 00:06:24 -07001714 funcName = token.image;
1715 }
1716 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1717 {
1718 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001719 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001720 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001721 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001722 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001723 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001724 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001725 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1726 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1727 if (signature == null) {
1728 signature = new FunctionSignature(dataverse, funcName, 1);
1729 }
1730 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001731 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001732 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001733 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001734}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001735
vinayakb38b7ca42012-03-05 05:44:15 +00001736Expression ParenthesizedExpression() throws ParseException:
1737{
1738 Expression expr;
1739}
1740{
1741 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1742 {
1743 return expr;
1744 }
1745}
1746
1747Expression IfThenElse() throws ParseException:
1748{
1749 Expression condExpr;
1750 Expression thenExpr;
1751 Expression elseExpr;
1752 IfExpr ifExpr = new IfExpr();
1753}
1754{
1755 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1756
1757 {
1758 ifExpr.setCondExpr(condExpr);
1759 ifExpr.setThenExpr(thenExpr);
1760 ifExpr.setElseExpr(elseExpr);
1761 return ifExpr;
1762 }
1763}
1764
1765Expression FLWOGR() throws ParseException:
1766{
1767 FLWOGRExpression flworg = new FLWOGRExpression();
1768 List<Clause> clauseList = new ArrayList<Clause>();
1769 Expression returnExpr;
1770 Clause tmp;
1771 createNewScope();
1772}
1773{
1774 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1775 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1776
1777 {
1778 flworg.setClauseList(clauseList);
1779 flworg.setReturnExpr(returnExpr);
1780 removeCurrentScope();
1781 return flworg;
1782 }
1783}
1784
1785Clause Clause()throws ParseException :
1786{
1787 Clause clause;
1788}
1789{
1790 (
1791 clause = ForClause()
1792 | clause = LetClause()
1793 | clause = WhereClause()
1794 | clause = OrderbyClause()
1795 | clause = GroupClause()
1796 | clause = LimitClause()
1797 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001798 )
1799 {
1800 return clause;
1801 }
1802}
1803
1804Clause ForClause()throws ParseException :
1805{
1806 ForClause fc = new ForClause();
1807 VariableExpr varExp;
1808 VariableExpr varPos = null;
1809 Expression inExp;
1810 extendCurrentScope();
1811}
1812{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001813 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001814 {
1815 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001816 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001817 fc.setInExpr(inExp);
1818 if (varPos != null) {
1819 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001820 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001821 }
1822 return fc;
1823 }
1824}
1825
1826Clause LetClause() throws ParseException:
1827{
1828 LetClause lc = new LetClause();
1829 VariableExpr varExp;
1830 Expression beExp;
1831 extendCurrentScope();
1832}
1833{
ilovesoupb2527c12012-07-12 03:21:13 +00001834 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001835 {
1836 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001837 lc.setVarExpr(varExp);
1838 lc.setBeExpr(beExp);
1839 return lc;
1840 }
1841}
1842
1843Clause WhereClause()throws ParseException :
1844{
1845 WhereClause wc = new WhereClause();
1846 Expression whereExpr;
1847}
1848{
1849 "where" whereExpr = Expression()
1850 {
1851 wc.setWhereExpr(whereExpr);
1852 return wc;
1853 }
1854}
1855
1856Clause OrderbyClause()throws ParseException :
1857{
1858 OrderbyClause oc = new OrderbyClause();
1859 Expression orderbyExpr;
1860 List<Expression> orderbyList = new ArrayList<Expression>();
1861 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1862 int numOfOrderby = 0;
1863}
1864{
1865 (
1866 "order"
1867 {
1868 String hint = getHint(token);
1869 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1870 String splits[] = hint.split(" +");
1871 int numFrames = Integer.parseInt(splits[1]);
1872 int numTuples = Integer.parseInt(splits[2]);
1873 oc.setNumFrames(numFrames);
1874 oc.setNumTuples(numTuples);
1875 }
1876 }
1877 "by" orderbyExpr = Expression()
1878 {
1879 orderbyList.add(orderbyExpr);
1880 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1881 }
1882 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1883 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1884 {
1885 modifierList.add(modif);
1886 }
1887
1888 ("," orderbyExpr = Expression()
1889 {
1890 orderbyList.add(orderbyExpr);
1891 modif = OrderbyClause.OrderModifier.ASC;
1892 }
1893 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1894 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1895 {
1896 modifierList.add(modif);
1897 }
1898 )*
1899)
1900 {
1901 oc.setModifierList(modifierList);
1902 oc.setOrderbyList(orderbyList);
1903 return oc;
1904 }
1905}
1906Clause GroupClause()throws ParseException :
1907{
1908 GroupbyClause gbc = new GroupbyClause();
1909 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1910 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1911 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1912 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1913 VariableExpr var = null;
1914 VariableExpr withVar = null;
1915 Expression expr = null;
1916 VariableExpr decorVar = null;
1917 Expression decorExpr = null;
1918}
1919{
1920 {
1921 Scope newScope = extendCurrentScopeNoPush(true);
1922 // extendCurrentScope(true);
1923 }
1924 "group"
1925 {
1926 String hint = getHint(token);
1927 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1928 gbc.setHashGroupByHint(true);
1929 }
1930 }
1931 "by" (LOOKAHEAD(2) var = Variable()
1932 {
1933 newScope.addNewVarSymbolToScope(var.getVar());
1934 } ":=")?
1935 expr = Expression()
1936 {
1937 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1938 vePairList.add(pair1);
1939 }
1940 ("," ( LOOKAHEAD(2) var = Variable()
1941 {
1942 newScope.addNewVarSymbolToScope(var.getVar());
1943 } ":=")?
1944 expr = Expression()
1945 {
1946 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1947 vePairList.add(pair2);
1948 }
1949 )*
1950 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1951 {
1952 newScope.addNewVarSymbolToScope(decorVar.getVar());
1953 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1954 decorPairList.add(pair3);
1955 }
1956 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1957 {
1958 newScope.addNewVarSymbolToScope(decorVar.getVar());
1959 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1960 decorPairList.add(pair4);
1961 }
1962 )*
1963 )?
1964 "with" withVar = VariableRef()
1965 {
1966 if(withVar.getIsNewVar()==true)
1967 throw new ParseException("can't find variable " + withVar.getVar());
1968 withVarList.add(withVar);
1969 newScope.addNewVarSymbolToScope(withVar.getVar());
1970 }
1971 ("," withVar = VariableRef()
1972 {
1973 if(withVar.getIsNewVar()==true)
1974 throw new ParseException("can't find variable " + withVar.getVar());
1975 withVarList.add(withVar);
1976 newScope.addNewVarSymbolToScope(withVar.getVar());
1977 })*
1978 {
1979 gbc.setGbyPairList(vePairList);
1980 gbc.setDecorPairList(decorPairList);
1981 gbc.setWithVarList(withVarList);
1982 replaceCurrentScope(newScope);
1983 return gbc;
1984 }
1985}
1986
1987
1988LimitClause LimitClause() throws ParseException:
1989{
1990 LimitClause lc = new LimitClause();
1991 Expression expr;
1992 pushForbiddenScope(getCurrentScope());
1993}
1994{
1995 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1996 ("offset" expr = Expression() { lc.setOffset(expr); })?
1997
1998 {
1999 popForbiddenScope();
2000 return lc;
2001 }
2002}
2003
2004DistinctClause DistinctClause() throws ParseException:
2005{
2006 List<Expression> exprs = new ArrayList<Expression>();
2007 Expression expr;
2008}
2009{
2010 "distinct" "by" expr = Expression()
2011 {
2012 exprs.add(expr);
2013 }
2014 ("," expr = Expression()
2015 {
2016 exprs.add(expr);
2017 }
2018 )*
2019 {
2020 return new DistinctClause(exprs);
2021 }
2022}
2023
vinayakb38b7ca42012-03-05 05:44:15 +00002024QuantifiedExpression QuantifiedExpression()throws ParseException:
2025{
2026 QuantifiedExpression qc = new QuantifiedExpression();
2027 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2028 Expression satisfiesExpr;
2029 VariableExpr var;
2030 Expression inExpr;
2031 QuantifiedPair pair;
2032}
2033{
2034 {
2035 createNewScope();
2036 }
2037
2038 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2039 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2040 var = Variable() "in" inExpr = Expression()
2041 {
2042 pair = new QuantifiedPair(var, inExpr);
2043 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2044 quantifiedList.add(pair);
2045 }
2046 (
2047 "," var = Variable() "in" inExpr = Expression()
2048 {
2049 pair = new QuantifiedPair(var, inExpr);
2050 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2051 quantifiedList.add(pair);
2052 }
2053 )*
2054 "satisfies" satisfiesExpr = Expression()
2055 {
2056 qc.setSatisfiesExpr(satisfiesExpr);
2057 qc.setQuantifiedList(quantifiedList);
2058 removeCurrentScope();
2059 return qc;
2060 }
2061}
2062
2063TOKEN_MGR_DECLS:
2064{
2065 public int commentDepth = 0;
2066}
2067
2068<DEFAULT>
2069TOKEN :
2070{
2071 <CARET : "^" >
2072}
2073
2074<DEFAULT>
2075TOKEN :
2076{
2077 <DATASET : "dataset" >
2078}
2079
2080<DEFAULT>
2081TOKEN :
2082{
2083 <LEFTPAREN : "(" >
2084}
2085
2086<DEFAULT>
2087TOKEN :
2088{
2089 <RIGHTPAREN : ")" >
2090}
2091
2092
2093<DEFAULT>
2094TOKEN :
2095{
2096 <INTEGER_LITERAL : (<DIGIT>)+ >
2097}
2098
2099
2100<DEFAULT>
2101TOKEN :
2102{
2103 <NULL : "null">
2104}
2105
2106<DEFAULT>
2107TOKEN :
2108{
2109 <TRUE : "true">
2110}
2111
2112<DEFAULT>
2113TOKEN :
2114{
2115 <FALSE : "false">
2116}
2117
2118<DEFAULT>
2119TOKEN :
2120{
2121 <#DIGIT : ["0" - "9"]>
2122}
2123
2124
2125TOKEN:
2126{
2127 < DOUBLE_LITERAL: <INTEGER>
2128 | <INTEGER> ( "." <INTEGER> )?
2129 | "." <INTEGER>
2130 >
2131 |
2132 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2133 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2134 | "." <INTEGER> ( "f" | "F" )
2135 >
2136 |
2137 <INTEGER : (<DIGIT>)+ >
2138}
2139
2140<DEFAULT>
2141TOKEN :
2142{
2143 <#LETTER : ["A" - "Z", "a" - "z"]>
2144}
2145
2146<DEFAULT>
2147TOKEN :
2148{
2149 <SPECIALCHARS : ["$", "_", "-"] >
2150}
2151
2152<DEFAULT>
2153TOKEN :
2154{
2155 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2156 |
2157 < #EscapeQuot: "\\\"" >
2158 |
2159 < #EscapeApos: "\\\'" >
2160}
2161
2162<DEFAULT>
2163TOKEN :
2164{
2165 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2166}
2167
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002168
vinayakb38b7ca42012-03-05 05:44:15 +00002169<DEFAULT>
2170TOKEN :
2171{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002172 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002173}
2174
2175SKIP:
2176{
2177 " "
2178| "\t"
2179| "\r"
2180| "\n"
2181}
2182
2183SKIP:
2184{
2185 <"//" (~["\n"])* "\n">
2186}
2187
2188SKIP:
2189{
2190 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2191}
2192
2193
2194SKIP:
2195{
2196 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2197}
2198
2199<INSIDE_COMMENT>
2200SPECIAL_TOKEN:
2201{
2202 <"+"(" ")*(~["*"])*>
2203}
2204
2205<INSIDE_COMMENT>
2206SKIP:
2207{
2208 <"/*"> {commentDepth++;}
2209}
2210
2211<INSIDE_COMMENT>
2212SKIP:
2213{
2214 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2215| <~[]>
2216}