blob: 84d46f5cdf6362b6a365c9bac1a708429497a466 [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 {
JIMAHNb75446d2013-06-03 08:35:27 -0700379 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700380 }
381 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
382 {
JIMAHNb75446d2013-06-03 08:35:27 -0700383 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
Till Westmann31c21f92013-05-08 09:21:53 -0700384 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 Query query;
682 String writerClass = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000683 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000684}
685{
Till Westmann35a0f702013-07-01 14:06:34 -0700686 "write" "output" "to" nodeName = Identifier() ":" fileName = StringLiteral()
Till Westmann7d535322013-05-09 00:40:02 -0700687 ( "using" writerClass = StringLiteral() )?
Till Westmann35a0f702013-07-01 14:06:34 -0700688 {
689 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
vinayakb38b7ca42012-03-05 05:44:15 +0000690 }
691}
692
vinayakb38b7ca42012-03-05 05:44:15 +0000693LoadFromFileStatement LoadStatement() throws ParseException:
694{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000695 Identifier dataverseName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000696 Identifier datasetName = null;
697 boolean alreadySorted = false;
ramangrover29669d8f62013-02-11 06:03:32 +0000698 String adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000699 Map<String,String> properties;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000700 Pair<Identifier,Identifier> nameComponents = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000701}
702{
Till Westmann31c21f92013-05-08 09:21:53 -0700703 "load" <DATASET> nameComponents = QualifiedName()
vinayakb38b7ca42012-03-05 05:44:15 +0000704 {
Till Westmann31c21f92013-05-08 09:21:53 -0700705 dataverseName = nameComponents.first;
706 datasetName = nameComponents.second;
vinayakb38b7ca42012-03-05 05:44:15 +0000707 }
Till Westmann31c21f92013-05-08 09:21:53 -0700708 "using" adapterName = AdapterName() properties = Configuration()
709 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000710 {
Till Westmann31c21f92013-05-08 09:21:53 -0700711 alreadySorted = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000712 }
713 )?
Till Westmann31c21f92013-05-08 09:21:53 -0700714 {
715 return new LoadFromFileStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
716 }
vinayakb38b7ca42012-03-05 05:44:15 +0000717}
718
vinayakb38b7ca42012-03-05 05:44:15 +0000719
Till Westmann31c21f92013-05-08 09:21:53 -0700720String AdapterName() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000721{
ramangrover29669d8f62013-02-11 06:03:32 +0000722 String adapterName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000723}
724{
Till Westmann68d99652013-05-09 11:15:21 -0700725 adapterName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000726 {
Till Westmann7d535322013-05-09 00:40:02 -0700727 return adapterName;
vinayakb38b7ca42012-03-05 05:44:15 +0000728 }
vinayakb38b7ca42012-03-05 05:44:15 +0000729}
730
Till Westmann31c21f92013-05-08 09:21:53 -0700731Statement FeedStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000732{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000733 Pair<Identifier,Identifier> nameComponents = null;
Till Westmann31c21f92013-05-08 09:21:53 -0700734 Map<String,String> configuration = null;
735 Statement stmt = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000736}
737{
Till Westmann31c21f92013-05-08 09:21:53 -0700738 (
739 "begin" "feed" nameComponents = QualifiedName()
740 {
741 stmt = new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter());
742 }
743 | "suspend" "feed" nameComponents = QualifiedName()
744 {
745 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, nameComponents.first, nameComponents.second);
746 }
747 | "resume" "feed" nameComponents = QualifiedName()
748 {
749 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, nameComponents.first, nameComponents.second);
750 }
751 | "end" "feed" nameComponents = QualifiedName()
752 {
753 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.END, nameComponents.first, nameComponents.second);
754 }
755 | "alter" "feed" nameComponents = QualifiedName() "set" configuration = Configuration()
756 {
757 stmt = new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
758 }
759 )
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000760 {
Till Westmann31c21f92013-05-08 09:21:53 -0700761 return stmt;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000762 }
763}
764
Till Westmann31c21f92013-05-08 09:21:53 -0700765Map<String,String> Configuration() throws ParseException :
vinayakb38b7ca42012-03-05 05:44:15 +0000766{
diegogiorgini@gmail.comeb1910e2013-02-14 07:43:00 +0000767 Map<String,String> configuration = new LinkedHashMap<String,String>();
Till Westmann31c21f92013-05-08 09:21:53 -0700768 Pair<String, String> keyValuePair = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000769}
770{
Till Westmann31c21f92013-05-08 09:21:53 -0700771 <LEFTPAREN> ( keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000772 {
Till Westmann31c21f92013-05-08 09:21:53 -0700773 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000774 }
Till Westmann31c21f92013-05-08 09:21:53 -0700775 ( "," keyValuePair = KeyValuePair()
vinayakb38b7ca42012-03-05 05:44:15 +0000776 {
Till Westmann31c21f92013-05-08 09:21:53 -0700777 configuration.put(keyValuePair.first, keyValuePair.second);
vinayakb38b7ca42012-03-05 05:44:15 +0000778 }
Till Westmann31c21f92013-05-08 09:21:53 -0700779 )* )? <RIGHTPAREN>
780 {
781 return configuration;
782 }
783}
784
785Pair<String, String> KeyValuePair() throws ParseException:
786{
787 String key;
788 String value;
789}
790{
Till Westmann7d535322013-05-09 00:40:02 -0700791 <LEFTPAREN> key = StringLiteral() "=" value = StringLiteral() <RIGHTPAREN>
Till Westmann31c21f92013-05-08 09:21:53 -0700792 {
793 return new Pair<String, String>(key, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000794 }
Till Westmann31c21f92013-05-08 09:21:53 -0700795}
796
797Map<String,String> Properties() throws ParseException:
798{
799 Map<String,String> properties = new HashMap<String,String>();
800 Pair<String, String> property;
801}
802{
803 ( <LEFTPAREN> property = Property()
804 {
805 properties.put(property.first, property.second);
806 }
807 ( "," property = Property()
808 {
809 properties.put(property.first, property.second);
810 }
811 )* <RIGHTPAREN> )?
812 {
813 return properties;
814 }
815}
816
817Pair<String, String> Property() throws ParseException:
818{
819 String key;
820 String value;
821}
822{
Till Westmann7d535322013-05-09 00:40:02 -0700823 key = Identifier() "=" ( value = StringLiteral() | <INTEGER_LITERAL>
Till Westmann31c21f92013-05-08 09:21:53 -0700824 {
825 try {
826 value = "" + Long.valueOf(token.image);
827 } catch (NumberFormatException nfe) {
828 throw new ParseException("inapproriate value: " + token.image);
829 }
830 }
831 )
832 {
833 return new Pair<String, String>(key.toUpperCase(), value);
834 }
vinayakb38b7ca42012-03-05 05:44:15 +0000835}
836
837TypeExpression TypeExpr() throws ParseException:
838{
839 TypeExpression typeExpr = null;
840}
841{
842 (
843 typeExpr = RecordTypeDef()
844 | typeExpr = TypeReference()
845 | typeExpr = OrderedListTypeDef()
846 | typeExpr = UnorderedListTypeDef()
847 )
848 {
849 return typeExpr;
850 }
851}
852
853RecordTypeDefinition RecordTypeDef() throws ParseException:
854{
855 RecordTypeDefinition recType = new RecordTypeDefinition();
856 RecordTypeDefinition.RecordKind recordKind = null;
857}
858{
859 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
860 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
861 "{"
862 {
863 String hint = getHint(token);
864 if (hint != null) {
865 String splits[] = hint.split(" +");
866 if (splits[0].equals(GEN_FIELDS_HINT)) {
867 if (splits.length != 5) {
868 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
869 }
870 if (!splits[1].equals("int")) {
871 throw new ParseException("The only supported type for gen-fields is int.");
872 }
873 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
874 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
875 recType.setUndeclaredFieldsDataGen(ufdg);
876 }
877 }
878
879 }
880 (
881 RecordField(recType)
882 ( "," RecordField(recType) )*
883 )?
884 "}"
885 {
886 if (recordKind == null) {
887 recordKind = RecordTypeDefinition.RecordKind.OPEN;
888 }
889 recType.setRecordKind(recordKind);
890 return recType;
891 }
892}
893
894void RecordField(RecordTypeDefinition recType) throws ParseException:
895{
896 String fieldName;
897 TypeExpression type = null;
898 boolean nullable = false;
899}
900{
Till Westmann14a20a72013-05-09 00:06:24 -0700901 fieldName = Identifier()
vinayakb38b7ca42012-03-05 05:44:15 +0000902 {
Till Westmanna4242bc2013-05-08 17:49:55 -0700903 fieldName = token.image;
904 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +0000905 IRecordFieldDataGen rfdg = null;
906 if (hint != null) {
907 String splits[] = hint.split(" +");
908 if (splits[0].equals(VAL_FILE_HINT)) {
909 File[] valFiles = new File[splits.length - 1];
910 for (int k=1; k<splits.length; k++) {
911 valFiles[k-1] = new File(splits[k]);
912 }
913 rfdg = new FieldValFileDataGen(valFiles);
914 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
915 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
916 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
917 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
918 } else if (splits[0].equals(LIST_HINT)) {
919 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
920 } else if (splits[0].equals(INTERVAL_HINT)) {
921 FieldIntervalDataGen.ValueType vt;
922 if (splits[1].equals("int")) {
923 vt = FieldIntervalDataGen.ValueType.INT;
924 } else if (splits[1].equals("long")) {
925 vt = FieldIntervalDataGen.ValueType.LONG;
926 } else if (splits[1].equals("float")) {
927 vt = FieldIntervalDataGen.ValueType.FLOAT;
928 } else if (splits[1].equals("double")) {
929 vt = FieldIntervalDataGen.ValueType.DOUBLE;
930 } else {
931 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
932 }
933 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
934 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
935 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
936 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
937 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
938 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
939 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
940 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
941 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
942 } else if (splits[0].equals(AUTO_HINT)) {
943 rfdg = new AutoDataGen(splits[1]);
944 }
945 }
946 }
947 ":"
948 ( type = TypeExpr() )
949 ("?" { nullable = true; } )?
950 {
951 recType.addField(fieldName, type, nullable, rfdg);
952 }
953}
954
955TypeReferenceExpression TypeReference() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000956{
Till Westmann14a20a72013-05-09 00:06:24 -0700957 String id = null;
Till Westmanna4242bc2013-05-08 17:49:55 -0700958}
959{
960 id = Identifier()
961 {
Till Westmann14a20a72013-05-09 00:06:24 -0700962 return new TypeReferenceExpression(new Identifier(id));
Till Westmanna4242bc2013-05-08 17:49:55 -0700963 }
vinayakb38b7ca42012-03-05 05:44:15 +0000964}
965
966OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
967{
968 TypeExpression type = null;
969}
970{
971 "["
972 ( type = TypeExpr() )
973 "]"
974 {
975 return new OrderedListTypeDefinition(type);
976 }
977}
978
979
980UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
981{
982 TypeExpression type = null;
983}
984{
985 "{{"
986 ( type = TypeExpr() )
987 "}}"
988 {
989 return new UnorderedListTypeDefinition(type);
990 }
991}
992
Till Westmann31c21f92013-05-08 09:21:53 -0700993
994Pair<Identifier,Identifier> FunctionOrTypeName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000995{
Till Westmann31c21f92013-05-08 09:21:53 -0700996 Pair<Identifier,Identifier> name = null;
997}
998{
999 name = QualifiedName()
1000 {
1001 if (name.first == null) {
1002 name.first = new Identifier(defaultDataverse);
1003 }
1004 return name;
1005 }
1006}
1007
Till Westmann14a20a72013-05-09 00:06:24 -07001008String Identifier() throws ParseException:
Till Westmanna4242bc2013-05-08 17:49:55 -07001009{
Till Westmann68d99652013-05-09 11:15:21 -07001010 String lit = null;
Till Westmanna4242bc2013-05-08 17:49:55 -07001011}
1012{
1013 <IDENTIFIER>
1014 {
Till Westmann14a20a72013-05-09 00:06:24 -07001015 return token.image;
Till Westmanna4242bc2013-05-08 17:49:55 -07001016 }
Till Westmann68d99652013-05-09 11:15:21 -07001017 | lit = StringLiteral()
1018 {
1019 return lit;
1020 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001021}
1022
Till Westmann7d535322013-05-09 00:40:02 -07001023String StringLiteral() throws ParseException:
1024{
1025}
1026{
1027 <STRING_LITERAL>
1028 {
1029 return removeQuotesAndEscapes(token.image);
1030 }
1031}
1032
Till Westmann31c21f92013-05-08 09:21:53 -07001033Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1034{
Till Westmann14a20a72013-05-09 00:06:24 -07001035 String first = null;
1036 String second = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001037}
1038{
Till Westmanna4242bc2013-05-08 17:49:55 -07001039 first = Identifier() ("." second = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001040 {
Till Westmann14a20a72013-05-09 00:06:24 -07001041 Identifier id1 = null;
1042 Identifier id2 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001043 if (second == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001044 id2 = new Identifier(first);
1045 } else
1046 {
1047 id1 = new Identifier(first);
1048 id2 = new Identifier(second);
1049 }
1050 return new Pair<Identifier,Identifier>(id1, id2);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001051 }
1052}
1053
Till Westmann31c21f92013-05-08 09:21:53 -07001054Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001055{
Till Westmann14a20a72013-05-09 00:06:24 -07001056 String first = null;
1057 String second = null;
1058 String third = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001059}
1060{
Till Westmanna4242bc2013-05-08 17:49:55 -07001061 first = Identifier() "." second = Identifier() ("." third = Identifier())?
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001062 {
Till Westmann14a20a72013-05-09 00:06:24 -07001063 Identifier id1 = null;
1064 Identifier id2 = null;
1065 Identifier id3 = null;
Till Westmann31c21f92013-05-08 09:21:53 -07001066 if (third == null) {
Till Westmann14a20a72013-05-09 00:06:24 -07001067 id2 = new Identifier(first);
1068 id3 = new Identifier(second);
1069 } else {
1070 id1 = new Identifier(first);
1071 id2 = new Identifier(second);
1072 id3 = new Identifier(third);
1073 }
1074 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001075 }
1076}
1077
vinayakb38b7ca42012-03-05 05:44:15 +00001078FunctionDecl FunctionDeclaration() throws ParseException:
1079{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001080 FunctionDecl funcDecl;
1081 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +00001082 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001083 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1084 Expression funcBody;
vinayakb38b7ca42012-03-05 05:44:15 +00001085 createNewScope();
1086}
1087{
Till Westmannd7dcb122013-05-16 13:19:09 -07001088 "declare" "function" functionName = Identifier()
1089 paramList = ParameterList()
1090 "{" funcBody = Expression() "}"
vinayakb38b7ca42012-03-05 05:44:15 +00001091 {
Till Westmannd7dcb122013-05-16 13:19:09 -07001092 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001093 getCurrentScope().addFunctionDescriptor(signature, false);
1094 funcDecl = new FunctionDecl(signature, paramList, funcBody);
Till Westmannc6c4a742013-05-20 17:59:21 -07001095 removeCurrentScope();
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001096 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +00001097 }
1098}
1099
vinayakb38b7ca42012-03-05 05:44:15 +00001100
Till Westmann31c21f92013-05-08 09:21:53 -07001101Query Query() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001102{
1103 Query query = new Query();
1104 Expression expr;
1105}
1106{
Till Westmann31c21f92013-05-08 09:21:53 -07001107 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001108 {
1109 query.setBody(expr);
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001110 query.setVarCounter(getVarCounter());
vinayakb38b7ca42012-03-05 05:44:15 +00001111 return query;
1112 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001113
vinayakb38b7ca42012-03-05 05:44:15 +00001114}
1115
1116
1117
1118Expression Expression():
1119{
1120 Expression expr = null;
1121 Expression exprP = null;
1122}
1123{
1124(
1125
1126//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1127 expr = OperatorExpr()
1128 | expr = IfThenElse()
1129 | expr = FLWOGR()
1130 | expr = QuantifiedExpression()
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001131
vinayakb38b7ca42012-03-05 05:44:15 +00001132
1133)
1134 {
1135 return (exprP==null) ? expr : exprP;
1136 }
1137}
1138
1139
1140
1141Expression OperatorExpr()throws ParseException:
1142{
1143 OperatorExpr op = null;
1144 Expression operand = null;
1145}
1146{
1147 operand = AndExpr()
1148 (
1149
1150 "or"
1151 {
1152 if (op == null) {
1153 op = new OperatorExpr();
1154 op.addOperand(operand);
1155 op.setCurrentop(true);
1156 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001157 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001158 }
1159
1160 operand = AndExpr()
1161 {
1162 op.addOperand(operand);
1163 }
1164
1165 )*
1166
1167 {
1168 return op==null? operand: op;
1169 }
1170}
1171
1172Expression AndExpr()throws ParseException:
1173{
1174 OperatorExpr op = null;
1175 Expression operand = null;
1176}
1177{
1178 operand = RelExpr()
1179 (
1180
1181 "and"
1182 {
1183 if (op == null) {
1184 op = new OperatorExpr();
1185 op.addOperand(operand);
1186 op.setCurrentop(true);
1187 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001188 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001189 }
1190
1191 operand = RelExpr()
1192 {
1193 op.addOperand(operand);
1194 }
1195
1196 )*
1197
1198 {
1199 return op==null? operand: op;
1200 }
1201}
1202
1203
1204
1205Expression RelExpr()throws ParseException:
1206{
1207 OperatorExpr op = null;
1208 Expression operand = null;
1209 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001210 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001211}
1212{
1213 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001214 {
1215 if (operand instanceof VariableExpr) {
1216 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001217 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001218 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001219 }
1220 }
1221 }
1222
1223 (
1224 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1225 {
alexander.behm07617fd2012-07-25 10:13:50 +00001226 String mhint = getHint(token);
1227 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1228 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1229 }
vinayakb38b7ca42012-03-05 05:44:15 +00001230 if (op == null) {
1231 op = new OperatorExpr();
1232 op.addOperand(operand, broadcast);
1233 op.setCurrentop(true);
1234 broadcast = false;
Till Westmanna4242bc2013-05-08 17:49:55 -07001235 }
1236 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001237 }
1238
1239 operand = AddExpr()
1240 {
alexander.behm07617fd2012-07-25 10:13:50 +00001241 broadcast = false;
1242 if (operand instanceof VariableExpr) {
1243 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001244 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1245 broadcast = true;
1246 }
alexander.behm07617fd2012-07-25 10:13:50 +00001247 }
vinayakb38b7ca42012-03-05 05:44:15 +00001248 op.addOperand(operand, broadcast);
1249 }
1250 )?
1251
1252 {
alexander.behm07617fd2012-07-25 10:13:50 +00001253 if (annotation != null) {
1254 op.addHint(annotation);
1255 }
vinayakb38b7ca42012-03-05 05:44:15 +00001256 return op==null? operand: op;
1257 }
1258}
1259
1260Expression AddExpr()throws ParseException:
1261{
1262 OperatorExpr op = null;
1263 Expression operand = null;
1264}
1265{
1266 operand = MultExpr()
1267
1268 ( ("+" | "-")
1269 {
1270 if (op == null) {
1271 op = new OperatorExpr();
1272 op.addOperand(operand);
1273 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001274 }
1275 ((OperatorExpr)op).addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001276 }
1277
1278 operand = MultExpr()
1279 {
1280 op.addOperand(operand);
1281 }
1282 )*
1283
1284 {
1285 return op==null? operand: op;
1286 }
1287}
1288
1289Expression MultExpr()throws ParseException:
1290{
1291 OperatorExpr op = null;
1292 Expression operand = null;
1293}
1294{
1295 operand = UnionExpr()
1296
1297 (( "*" | "/" | "%" | <CARET> | "idiv")
1298 {
1299 if (op == null) {
1300 op = new OperatorExpr();
1301 op.addOperand(operand);
1302 op.setCurrentop(true);
Till Westmanna4242bc2013-05-08 17:49:55 -07001303 }
1304 op.addOperator(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001305 }
1306 operand = UnionExpr()
1307 {
1308 op.addOperand(operand);
1309 }
1310 )*
1311
1312 {
1313 return op==null?operand:op;
1314 }
1315}
1316
1317Expression UnionExpr() throws ParseException:
1318{
1319 UnionExpr union = null;
1320 Expression operand1 = null;
1321 Expression operand2 = null;
1322}
1323{
1324 operand1 = UnaryExpr()
1325 ("union"
1326 (operand2 = UnaryExpr()) {
1327 if (union == null) {
1328 union = new UnionExpr();
1329 union.addExpr(operand1);
1330 }
1331 union.addExpr(operand2);
1332 } )*
1333 {
1334 return (union == null)? operand1: union;
1335 }
1336}
1337
1338Expression UnaryExpr() throws ParseException:
1339{
1340 Expression uexpr = null;
1341 Expression expr = null;
1342}
1343{
1344 (( "+"|"-")
1345 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001346 uexpr = new UnaryExpr();
1347 if("+".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001348 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
Till Westmanna4242bc2013-05-08 17:49:55 -07001349 else if("-".equals(token.image))
vinayakb38b7ca42012-03-05 05:44:15 +00001350 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1351 else
1352 throw new ParseException();
1353 }
1354 )?
1355
1356 expr = ValueExpr()
1357 {
1358 if(uexpr!=null){
1359 ((UnaryExpr)uexpr).setExpr(expr);
1360 return uexpr;
1361 }
1362 else{
1363 return expr;
1364 }
1365 }
1366}
1367
Till Westmann04478e72013-05-13 23:01:34 -07001368Expression ValueExpr()throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +00001369{
1370 Expression expr = null;
1371 Identifier ident = null;
1372 AbstractAccessor fa = null;
1373 int index;
vinayakb38b7ca42012-03-05 05:44:15 +00001374}
1375{
Till Westmann04478e72013-05-13 23:01:34 -07001376 expr = PrimaryExpr() ( ident = Field()
vinayakb38b7ca42012-03-05 05:44:15 +00001377 {
Till Westmann04478e72013-05-13 23:01:34 -07001378 fa = (fa == null ? new FieldAccessor(expr, ident)
1379 : new FieldAccessor(fa, ident));
1380 }
1381 | index = Index()
1382 {
1383 fa = (fa == null ? new IndexAccessor(expr, index)
1384 : new IndexAccessor(fa, index));
1385 }
1386 )*
1387 {
1388 return fa == null ? expr : fa;
1389 }
vinayakb38b7ca42012-03-05 05:44:15 +00001390}
1391
1392Identifier Field() throws ParseException:
1393{
Till Westmann14a20a72013-05-09 00:06:24 -07001394 String ident = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001395}
1396{
Till Westmanna4242bc2013-05-08 17:49:55 -07001397 "." ident = Identifier()
1398 {
Till Westmann14a20a72013-05-09 00:06:24 -07001399 return new Identifier(ident);
Till Westmanna4242bc2013-05-08 17:49:55 -07001400 }
vinayakb38b7ca42012-03-05 05:44:15 +00001401}
1402
1403int Index() throws ParseException:
1404{
1405 Expression expr = null;
1406 int idx = -2;
1407}
1408{
1409 "[" ( expr = Expression()
1410 {
1411 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1412 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001413 Literal lit = ((LiteralExpr)expr).getValue();
1414 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1415 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001416 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001417 }
vinayakb38b7ca42012-03-05 05:44:15 +00001418 else {
1419 throw new ParseException("Index should be an INTEGER");
1420 }
1421 }
1422
1423 }
1424
1425 | "?"
1426 {
1427 idx = IndexAccessor.ANY;
1428 // ANY
1429 }
1430
1431 )
1432
1433 "]"
1434 {
1435 return idx;
1436 }
1437}
1438
1439
1440Expression PrimaryExpr()throws ParseException:
1441{
1442 Expression expr = null;
1443}
1444{
Till Westmann68d99652013-05-09 11:15:21 -07001445 ( LOOKAHEAD(2)
1446 expr = FunctionCallExpr()
1447 | expr = Literal()
1448 | expr = DatasetAccessExpression()
1449 | expr = VariableRef()
vinayakb38b7ca42012-03-05 05:44:15 +00001450 {
1451 if(((VariableExpr)expr).getIsNewVar() == true)
Till Westmann68d99652013-05-09 11:15:21 -07001452 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001453 }
Till Westmann68d99652013-05-09 11:15:21 -07001454 | expr = ListConstructor()
1455 | expr = RecordConstructor()
1456 | expr = ParenthesizedExpression()
1457 )
1458 {
1459 return expr;
1460 }
vinayakb38b7ca42012-03-05 05:44:15 +00001461}
1462
1463Expression Literal() throws ParseException:
1464{
vinayakb38b7ca42012-03-05 05:44:15 +00001465 LiteralExpr lit = new LiteralExpr();
Till Westmann7d535322013-05-09 00:40:02 -07001466 String str = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001467}
1468{
Till Westmann7d535322013-05-09 00:40:02 -07001469 ( str = StringLiteral()
vinayakb38b7ca42012-03-05 05:44:15 +00001470 {
Till Westmann7d535322013-05-09 00:40:02 -07001471 lit.setValue(new StringLiteral(str));
Till Westmanna4242bc2013-05-08 17:49:55 -07001472 }
1473 | <INTEGER_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001474 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001475 try {
1476 lit.setValue(new IntegerLiteral(new Integer(token.image)));
1477 } catch(NumberFormatException ex) {
1478 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1479 }
1480 }
1481 | <FLOAT_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +00001482 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001483 lit.setValue(new FloatLiteral(new Float(token.image)));
1484 }
1485 | <DOUBLE_LITERAL>
1486 {
1487 lit.setValue(new DoubleLiteral(new Double(token.image)));
1488 }
1489 | <NULL>
1490 {
1491 lit.setValue(NullLiteral.INSTANCE);
1492 }
1493 | <TRUE>
1494 {
1495 lit.setValue(TrueLiteral.INSTANCE);
1496 }
1497 | <FALSE>
1498 {
1499 lit.setValue(FalseLiteral.INSTANCE);
1500 }
1501 )
vinayakb38b7ca42012-03-05 05:44:15 +00001502 {
1503 return lit;
1504 }
1505}
1506
1507
1508VariableExpr VariableRef() throws ParseException:
1509{
1510 VariableExpr varExp = new VariableExpr();
1511 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001512}
1513{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001514 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001515 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001516 String varName = token.image;
vinayakb38b7ca42012-03-05 05:44:15 +00001517 Identifier ident = lookupSymbol(varName);
1518 if (isInForbiddenScopes(varName)) {
1519 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.");
1520 }
1521 if(ident != null) { // exist such ident
1522 varExp.setIsNewVar(false);
1523 varExp.setVar((VarIdentifier)ident);
1524 } else {
1525 varExp.setVar(var);
1526 }
Till Westmanna4242bc2013-05-08 17:49:55 -07001527 var.setValue(varName);
vinayakb38b7ca42012-03-05 05:44:15 +00001528 return varExp;
1529 }
1530}
1531
1532
1533VariableExpr Variable() throws ParseException:
1534{
1535 VariableExpr varExp = new VariableExpr();
1536 VarIdentifier var = new VarIdentifier();
vinayakb38b7ca42012-03-05 05:44:15 +00001537}
1538{
Till Westmann4f58e1a2013-05-20 18:29:54 -07001539 <VARIABLE>
vinayakb38b7ca42012-03-05 05:44:15 +00001540 {
Till Westmanna4242bc2013-05-08 17:49:55 -07001541 Identifier ident = lookupSymbol(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001542 if(ident != null) { // exist such ident
1543 varExp.setIsNewVar(false);
1544 }
1545 varExp.setVar(var);
Till Westmanna4242bc2013-05-08 17:49:55 -07001546 var.setValue(token.image);
vinayakb38b7ca42012-03-05 05:44:15 +00001547 return varExp;
1548 }
1549}
1550
1551Expression ListConstructor() throws ParseException:
1552{
1553 Expression expr = null;
1554}
1555{
1556 (
1557 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1558 )
1559
1560 {
1561 return expr;
1562 }
1563}
1564
1565
1566ListConstructor OrderedListConstructor() throws ParseException:
1567{
1568 ListConstructor expr = new ListConstructor();
1569 Expression tmp = null;
1570 List<Expression> exprList = new ArrayList<Expression>();
1571 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1572}
1573{
1574
1575 "["
1576 ( tmp = Expression()
1577 {
1578 exprList.add(tmp);
1579 }
1580
1581 ("," tmp = Expression() { exprList.add(tmp); })*
1582 )?
1583
1584 "]"
1585
1586 {
1587 expr.setExprList(exprList);
1588 return expr;
1589 }
1590}
1591
1592ListConstructor UnorderedListConstructor() throws ParseException:
1593{
1594 ListConstructor expr = new ListConstructor();
1595 Expression tmp = null;
1596 List<Expression> exprList = new ArrayList<Expression>();
1597 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1598}
1599{
1600
1601 "{{" ( tmp = Expression()
1602 {
1603 exprList.add(tmp);
1604 }
1605 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1606 {
1607 expr.setExprList(exprList);
1608 return expr;
1609 }
1610}
1611
1612RecordConstructor RecordConstructor() throws ParseException:
1613{
1614 RecordConstructor expr = new RecordConstructor();
1615 FieldBinding tmp = null;
1616 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1617}
1618{
1619 "{" (tmp = FieldBinding()
1620 {
1621 fbList.add(tmp);
1622 }
1623 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1624 {
1625 expr.setFbList(fbList);
1626 return expr;
1627 }
1628}
1629
1630FieldBinding FieldBinding() throws ParseException:
1631{
1632 FieldBinding fb = new FieldBinding();
1633 Expression left, right;
1634}
1635{
1636 left = Expression() ":" right = Expression()
1637 {
1638 fb.setLeftExpr(left);
1639 fb.setRightExpr(right);
1640 return fb;
1641 }
1642}
1643
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001644
vinayakb38b7ca42012-03-05 05:44:15 +00001645Expression FunctionCallExpr() throws ParseException:
1646{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001647 CallExpr callExpr;
alexander.behm07617fd2012-07-25 10:13:50 +00001648 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001649 Expression tmp;
1650 int arity = 0;
Till Westmann31c21f92013-05-08 09:21:53 -07001651 Pair<Identifier,Identifier> funcId = null;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001652 String funcName;
1653 String dataverse;
Till Westmann31c21f92013-05-08 09:21:53 -07001654 String hint = null;
1655 String id1 = null;
1656 String id2 = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001657}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001658{
Till Westmann31c21f92013-05-08 09:21:53 -07001659 funcId = FunctionOrTypeName()
vinayakb38b7ca42012-03-05 05:44:15 +00001660 {
Till Westmann31c21f92013-05-08 09:21:53 -07001661 dataverse = funcId.first.getValue();
1662 funcName = funcId.second.getValue();
1663 hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001664 }
Till Westmann31c21f92013-05-08 09:21:53 -07001665 <LEFTPAREN> (tmp = Expression()
1666 {
1667 argList.add(tmp);
1668 arity ++;
1669 }
1670 ("," tmp = Expression()
1671 {
1672 argList.add(tmp);
1673 arity++;
1674 }
1675 )*)? <RIGHTPAREN>
1676 {
1677 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, arity);
1678 if (signature == null) {
1679 signature = new FunctionSignature(dataverse, funcName, arity);
1680 }
1681 callExpr = new CallExpr(signature,argList);
1682 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1683 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1684 }
1685 return callExpr;
1686 }
vinayakb38b7ca42012-03-05 05:44:15 +00001687}
1688
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001689Expression DatasetAccessExpression() throws ParseException:
1690{
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001691 String funcName;
Till Westmann14a20a72013-05-09 00:06:24 -07001692 String arg1 = null;
1693 String arg2 = null;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001694 Expression nameArg;
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001695}
1696{
Till Westmann14a20a72013-05-09 00:06:24 -07001697 <DATASET>
1698 {
Till Westmann14a20a72013-05-09 00:06:24 -07001699 funcName = token.image;
1700 }
1701 ( ( arg1 = Identifier() ( "." arg2 = Identifier() )? )
1702 {
1703 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
Till Westmann1f7a2362013-05-24 08:43:40 -07001704 LiteralExpr ds = new LiteralExpr();
Till Westmann14a20a72013-05-09 00:06:24 -07001705 ds.setValue( new StringLiteral(name) );
Till Westmann1f7a2362013-05-24 08:43:40 -07001706 nameArg = ds;
Till Westmann14a20a72013-05-09 00:06:24 -07001707 }
Till Westmann1f7a2362013-05-24 08:43:40 -07001708 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
Till Westmann14a20a72013-05-09 00:06:24 -07001709 {
Till Westmann1f7a2362013-05-24 08:43:40 -07001710 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
1711 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
1712 if (signature == null) {
1713 signature = new FunctionSignature(dataverse, funcName, 1);
1714 }
1715 List<Expression> argList = new ArrayList<Expression>();
Till Westmann14a20a72013-05-09 00:06:24 -07001716 argList.add(nameArg);
Till Westmann1f7a2362013-05-24 08:43:40 -07001717 return new CallExpr(signature, argList);
Till Westmann14a20a72013-05-09 00:06:24 -07001718 }
pouria.pirzadeh@gmail.comfa890742013-03-07 23:59:21 +00001719}
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001720
vinayakb38b7ca42012-03-05 05:44:15 +00001721Expression ParenthesizedExpression() throws ParseException:
1722{
1723 Expression expr;
1724}
1725{
1726 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1727 {
1728 return expr;
1729 }
1730}
1731
1732Expression IfThenElse() throws ParseException:
1733{
1734 Expression condExpr;
1735 Expression thenExpr;
1736 Expression elseExpr;
1737 IfExpr ifExpr = new IfExpr();
1738}
1739{
1740 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1741
1742 {
1743 ifExpr.setCondExpr(condExpr);
1744 ifExpr.setThenExpr(thenExpr);
1745 ifExpr.setElseExpr(elseExpr);
1746 return ifExpr;
1747 }
1748}
1749
1750Expression FLWOGR() throws ParseException:
1751{
1752 FLWOGRExpression flworg = new FLWOGRExpression();
1753 List<Clause> clauseList = new ArrayList<Clause>();
1754 Expression returnExpr;
1755 Clause tmp;
1756 createNewScope();
1757}
1758{
1759 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1760 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1761
1762 {
1763 flworg.setClauseList(clauseList);
1764 flworg.setReturnExpr(returnExpr);
1765 removeCurrentScope();
1766 return flworg;
1767 }
1768}
1769
1770Clause Clause()throws ParseException :
1771{
1772 Clause clause;
1773}
1774{
1775 (
1776 clause = ForClause()
1777 | clause = LetClause()
1778 | clause = WhereClause()
1779 | clause = OrderbyClause()
1780 | clause = GroupClause()
1781 | clause = LimitClause()
1782 | clause = DistinctClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001783 )
1784 {
1785 return clause;
1786 }
1787}
1788
1789Clause ForClause()throws ParseException :
1790{
1791 ForClause fc = new ForClause();
1792 VariableExpr varExp;
1793 VariableExpr varPos = null;
1794 Expression inExp;
1795 extendCurrentScope();
1796}
1797{
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001798 "for" varExp = Variable() ("at" varPos = Variable())? "in" ( inExp = Expression() )
vinayakb38b7ca42012-03-05 05:44:15 +00001799 {
1800 fc.setVarExpr(varExp);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001801 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001802 fc.setInExpr(inExp);
1803 if (varPos != null) {
1804 fc.setPosExpr(varPos);
Vinayak Borkar62e66c02013-05-28 13:56:11 -07001805 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001806 }
1807 return fc;
1808 }
1809}
1810
1811Clause LetClause() throws ParseException:
1812{
1813 LetClause lc = new LetClause();
1814 VariableExpr varExp;
1815 Expression beExp;
1816 extendCurrentScope();
1817}
1818{
ilovesoupb2527c12012-07-12 03:21:13 +00001819 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001820 {
1821 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001822 lc.setVarExpr(varExp);
1823 lc.setBeExpr(beExp);
1824 return lc;
1825 }
1826}
1827
1828Clause WhereClause()throws ParseException :
1829{
1830 WhereClause wc = new WhereClause();
1831 Expression whereExpr;
1832}
1833{
1834 "where" whereExpr = Expression()
1835 {
1836 wc.setWhereExpr(whereExpr);
1837 return wc;
1838 }
1839}
1840
1841Clause OrderbyClause()throws ParseException :
1842{
1843 OrderbyClause oc = new OrderbyClause();
1844 Expression orderbyExpr;
1845 List<Expression> orderbyList = new ArrayList<Expression>();
1846 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1847 int numOfOrderby = 0;
1848}
1849{
1850 (
1851 "order"
1852 {
1853 String hint = getHint(token);
1854 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1855 String splits[] = hint.split(" +");
1856 int numFrames = Integer.parseInt(splits[1]);
1857 int numTuples = Integer.parseInt(splits[2]);
1858 oc.setNumFrames(numFrames);
1859 oc.setNumTuples(numTuples);
1860 }
1861 }
1862 "by" orderbyExpr = Expression()
1863 {
1864 orderbyList.add(orderbyExpr);
1865 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1866 }
1867 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1868 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1869 {
1870 modifierList.add(modif);
1871 }
1872
1873 ("," orderbyExpr = Expression()
1874 {
1875 orderbyList.add(orderbyExpr);
1876 modif = OrderbyClause.OrderModifier.ASC;
1877 }
1878 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1879 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1880 {
1881 modifierList.add(modif);
1882 }
1883 )*
1884)
1885 {
1886 oc.setModifierList(modifierList);
1887 oc.setOrderbyList(orderbyList);
1888 return oc;
1889 }
1890}
1891Clause GroupClause()throws ParseException :
1892{
1893 GroupbyClause gbc = new GroupbyClause();
1894 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1895 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1896 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1897 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1898 VariableExpr var = null;
1899 VariableExpr withVar = null;
1900 Expression expr = null;
1901 VariableExpr decorVar = null;
1902 Expression decorExpr = null;
1903}
1904{
1905 {
1906 Scope newScope = extendCurrentScopeNoPush(true);
1907 // extendCurrentScope(true);
1908 }
1909 "group"
1910 {
1911 String hint = getHint(token);
1912 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1913 gbc.setHashGroupByHint(true);
1914 }
1915 }
1916 "by" (LOOKAHEAD(2) var = Variable()
1917 {
1918 newScope.addNewVarSymbolToScope(var.getVar());
1919 } ":=")?
1920 expr = Expression()
1921 {
1922 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1923 vePairList.add(pair1);
1924 }
1925 ("," ( LOOKAHEAD(2) var = Variable()
1926 {
1927 newScope.addNewVarSymbolToScope(var.getVar());
1928 } ":=")?
1929 expr = Expression()
1930 {
1931 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1932 vePairList.add(pair2);
1933 }
1934 )*
1935 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
1936 {
1937 newScope.addNewVarSymbolToScope(decorVar.getVar());
1938 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1939 decorPairList.add(pair3);
1940 }
1941 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
1942 {
1943 newScope.addNewVarSymbolToScope(decorVar.getVar());
1944 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
1945 decorPairList.add(pair4);
1946 }
1947 )*
1948 )?
1949 "with" withVar = VariableRef()
1950 {
1951 if(withVar.getIsNewVar()==true)
1952 throw new ParseException("can't find variable " + withVar.getVar());
1953 withVarList.add(withVar);
1954 newScope.addNewVarSymbolToScope(withVar.getVar());
1955 }
1956 ("," withVar = VariableRef()
1957 {
1958 if(withVar.getIsNewVar()==true)
1959 throw new ParseException("can't find variable " + withVar.getVar());
1960 withVarList.add(withVar);
1961 newScope.addNewVarSymbolToScope(withVar.getVar());
1962 })*
1963 {
1964 gbc.setGbyPairList(vePairList);
1965 gbc.setDecorPairList(decorPairList);
1966 gbc.setWithVarList(withVarList);
1967 replaceCurrentScope(newScope);
1968 return gbc;
1969 }
1970}
1971
1972
1973LimitClause LimitClause() throws ParseException:
1974{
1975 LimitClause lc = new LimitClause();
1976 Expression expr;
1977 pushForbiddenScope(getCurrentScope());
1978}
1979{
1980 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1981 ("offset" expr = Expression() { lc.setOffset(expr); })?
1982
1983 {
1984 popForbiddenScope();
1985 return lc;
1986 }
1987}
1988
1989DistinctClause DistinctClause() throws ParseException:
1990{
1991 List<Expression> exprs = new ArrayList<Expression>();
1992 Expression expr;
1993}
1994{
1995 "distinct" "by" expr = Expression()
1996 {
1997 exprs.add(expr);
1998 }
1999 ("," expr = Expression()
2000 {
2001 exprs.add(expr);
2002 }
2003 )*
2004 {
2005 return new DistinctClause(exprs);
2006 }
2007}
2008
vinayakb38b7ca42012-03-05 05:44:15 +00002009QuantifiedExpression QuantifiedExpression()throws ParseException:
2010{
2011 QuantifiedExpression qc = new QuantifiedExpression();
2012 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2013 Expression satisfiesExpr;
2014 VariableExpr var;
2015 Expression inExpr;
2016 QuantifiedPair pair;
2017}
2018{
2019 {
2020 createNewScope();
2021 }
2022
2023 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2024 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2025 var = Variable() "in" inExpr = Expression()
2026 {
2027 pair = new QuantifiedPair(var, inExpr);
2028 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2029 quantifiedList.add(pair);
2030 }
2031 (
2032 "," var = Variable() "in" inExpr = Expression()
2033 {
2034 pair = new QuantifiedPair(var, inExpr);
2035 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2036 quantifiedList.add(pair);
2037 }
2038 )*
2039 "satisfies" satisfiesExpr = Expression()
2040 {
2041 qc.setSatisfiesExpr(satisfiesExpr);
2042 qc.setQuantifiedList(quantifiedList);
2043 removeCurrentScope();
2044 return qc;
2045 }
2046}
2047
2048TOKEN_MGR_DECLS:
2049{
2050 public int commentDepth = 0;
2051}
2052
2053<DEFAULT>
2054TOKEN :
2055{
2056 <CARET : "^" >
2057}
2058
2059<DEFAULT>
2060TOKEN :
2061{
2062 <DATASET : "dataset" >
2063}
2064
2065<DEFAULT>
2066TOKEN :
2067{
2068 <LEFTPAREN : "(" >
2069}
2070
2071<DEFAULT>
2072TOKEN :
2073{
2074 <RIGHTPAREN : ")" >
2075}
2076
2077
2078<DEFAULT>
2079TOKEN :
2080{
2081 <INTEGER_LITERAL : (<DIGIT>)+ >
2082}
2083
2084
2085<DEFAULT>
2086TOKEN :
2087{
2088 <NULL : "null">
2089}
2090
2091<DEFAULT>
2092TOKEN :
2093{
2094 <TRUE : "true">
2095}
2096
2097<DEFAULT>
2098TOKEN :
2099{
2100 <FALSE : "false">
2101}
2102
2103<DEFAULT>
2104TOKEN :
2105{
2106 <#DIGIT : ["0" - "9"]>
2107}
2108
2109
2110TOKEN:
2111{
2112 < DOUBLE_LITERAL: <INTEGER>
2113 | <INTEGER> ( "." <INTEGER> )?
2114 | "." <INTEGER>
2115 >
2116 |
2117 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2118 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2119 | "." <INTEGER> ( "f" | "F" )
2120 >
2121 |
2122 <INTEGER : (<DIGIT>)+ >
2123}
2124
2125<DEFAULT>
2126TOKEN :
2127{
2128 <#LETTER : ["A" - "Z", "a" - "z"]>
2129}
2130
2131<DEFAULT>
2132TOKEN :
2133{
2134 <SPECIALCHARS : ["$", "_", "-"] >
2135}
2136
2137<DEFAULT>
2138TOKEN :
2139{
2140 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2141 |
2142 < #EscapeQuot: "\\\"" >
2143 |
2144 < #EscapeApos: "\\\'" >
2145}
2146
2147<DEFAULT>
2148TOKEN :
2149{
2150 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2151}
2152
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00002153
vinayakb38b7ca42012-03-05 05:44:15 +00002154<DEFAULT>
2155TOKEN :
2156{
Till Westmann4f58e1a2013-05-20 18:29:54 -07002157 <VARIABLE : "$" (<LETTER>)+ (<LETTER> | <DIGIT> | "_")* >
vinayakb38b7ca42012-03-05 05:44:15 +00002158}
2159
2160SKIP:
2161{
2162 " "
2163| "\t"
2164| "\r"
2165| "\n"
2166}
2167
2168SKIP:
2169{
2170 <"//" (~["\n"])* "\n">
2171}
2172
2173SKIP:
2174{
2175 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2176}
2177
2178
2179SKIP:
2180{
2181 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2182}
2183
2184<INSIDE_COMMENT>
2185SPECIAL_TOKEN:
2186{
2187 <"+"(" ")*(~["*"])*>
2188}
2189
2190<INSIDE_COMMENT>
2191SKIP:
2192{
2193 <"/*"> {commentDepth++;}
2194}
2195
2196<INSIDE_COMMENT>
2197SKIP:
2198{
2199 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2200| <~[]>
2201}