blob: 6b7b361c144980c8a0d715ebd327c88d9b727014 [file] [log] [blame]
Till Westmann5b431ca2015-10-01 19:16:11 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
vinayakb38b7ca42012-03-05 05:44:15 +000020options {
21
Taewoo Kima12d8cd2015-03-04 13:47:08 -080022
vinayakb38b7ca42012-03-05 05:44:15 +000023 STATIC = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080024
vinayakb38b7ca42012-03-05 05:44:15 +000025}
26
27
28PARSER_BEGIN(AQLPlusParser)
29
Ian Maxonf18bba22015-08-21 12:35:14 -070030package org.apache.asterix.aqlplus.parser;
vinayakb38b7ca42012-03-05 05:44:15 +000031
32import java.io.*;
33import java.util.List;
34import java.util.ArrayList;
35import java.util.Stack;
36import java.util.Map;
37import java.util.HashMap;
38
Ian Maxonf18bba22015-08-21 12:35:14 -070039import org.apache.asterix.aql.literal.FloatLiteral;
40import org.apache.asterix.aql.literal.DoubleLiteral;
41import org.apache.asterix.aql.literal.FalseLiteral;
42import org.apache.asterix.aql.base.Literal;
43import org.apache.asterix.aql.literal.IntegerLiteral;
44import org.apache.asterix.aql.literal.LongIntegerLiteral;
45import org.apache.asterix.aql.literal.NullLiteral;
46import org.apache.asterix.aql.literal.StringLiteral;
47import org.apache.asterix.aql.literal.TrueLiteral;
48import org.apache.asterix.aql.parser.ScopeChecker;
49import org.apache.asterix.aql.base.*;
50import org.apache.asterix.aql.expression.*;
51import org.apache.asterix.aql.expression.visitor.AQLPrintVisitor;
52import org.apache.asterix.aql.expression.UnaryExpr.Sign;
53import org.apache.asterix.aql.expression.TypeExpression.TypeExprKind;
54import org.apache.asterix.aql.base.Statement.Kind;
55import org.apache.asterix.aql.context.Scope;
56import org.apache.asterix.aql.context.RootScopeFactory;
57import org.apache.asterix.common.exceptions.AsterixException;
58import org.apache.asterix.om.functions.AsterixFunction;
59import org.apache.asterix.common.functions.FunctionSignature;
60import org.apache.asterix.metadata.bootstrap.MetadataConstants;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000061
Ian Maxonf18bba22015-08-21 12:35:14 -070062import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
63import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
64import org.apache.hyracks.algebricks.common.utils.Pair;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000065
66
ramangrover29a13f2422012-03-15 23:01:27 +000067
vinayakb38b7ca42012-03-05 05:44:15 +000068public class AQLPlusParser extends ScopeChecker {
69
70/*
71 private void printHints(Token t) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -080072 //System.err.println("token="+t.image+"\t special="+t.specialToken);
vinayakb38b7ca42012-03-05 05:44:15 +000073 if (t.specialToken == null) return;
74 Token tmp_t = t.specialToken;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080075 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
vinayakb38b7ca42012-03-05 05:44:15 +000076 while (tmp_t != null) {
77 System.out.println(tmp_t.image);
78 tmp_t = tmp_t.next;
79 }
80 }
81*/
Taewoo Kima12d8cd2015-03-04 13:47:08 -080082
vinayakb38b7ca42012-03-05 05:44:15 +000083 private static final String HASH_GROUP_BY_HINT = "hash";
84 private static final String BROADCAST_JOIN_HINT = "bcast";
85 private static final String INMEMORY_HINT = "inmem";
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +000086 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
Taewoo Kima12d8cd2015-03-04 13:47:08 -080087
88
89
vinayakb38b7ca42012-03-05 05:44:15 +000090 private static String getHint(Token t) {
91 if (t.specialToken == null) {
92 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080093 }
vinayakb38b7ca42012-03-05 05:44:15 +000094 String s = t.specialToken.image;
95 int n = s.length();
96 if (n < 2) {
97 return null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -080098 }
vinayakb38b7ca42012-03-05 05:44:15 +000099 return s.substring(1).trim();
100 }
101
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800102 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
103 File file = new File(args[0]);
104 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
105 AQLPlusParser parser = new AQLPlusParser(fis);
106 List<Statement> st = parser.Statement();
107 }
vinayakb38b7ca42012-03-05 05:44:15 +0000108
109 public void initScope() {
110 scopeStack.push(RootScopeFactory.createRootScope(this));
111 }
112}
113
114PARSER_END(AQLPlusParser)
115
116
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000117List<Statement> Statement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000118{
119 Query query = null;
120 // scopeStack.push(RootScopeFactory.createRootScope(this));
121 initScope();
122 List<Statement> decls = new ArrayList<Statement>();
123}
124{
125 (
126 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800127 (
vinayakb38b7ca42012-03-05 05:44:15 +0000128 "use"
129 {
130 decls.add(DataverseDeclaration());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800131 }
vinayakb38b7ca42012-03-05 05:44:15 +0000132 | "declare"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800133 ( "function" {
134 decls.add(FunctionDeclaration());
vinayakb38b7ca42012-03-05 05:44:15 +0000135 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800136 | "type" {
137 decls.add(TypeDeclaration());
138 }
139 )
vinayakb38b7ca42012-03-05 05:44:15 +0000140 | "load" {
141 decls.add(LoadStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800142 }
143
vinayakb38b7ca42012-03-05 05:44:15 +0000144 | "write" {
145 decls.add(WriteStatement());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800146 }
vinayakb38b7ca42012-03-05 05:44:15 +0000147 | "set" {
148 decls.add(SetStatement());
149 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800150 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000151 {
152 decls.add(Query()) ;
153 } ";"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800154
155
vinayakb38b7ca42012-03-05 05:44:15 +0000156 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000157 )
158
159 <EOF>
160 )
161 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800162
163 return decls;
vinayakb38b7ca42012-03-05 05:44:15 +0000164 }
165}
166
167Statement SetStatement() throws ParseException:
168{
169 String pn = null;
170 Statement stmt = null;
171}
172{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800173 <IDENTIFIER> { pn = token.image; }
vinayakb38b7ca42012-03-05 05:44:15 +0000174 <STRING_LITERAL>
175 { String pv = removeQuotesAndEscapes(token.image); }
176 ";"
177 {
178 return new SetStatement(pn, pv);
179 }
180}
181
182Statement WriteStatement() throws ParseException:
183{
184 Identifier nodeName = null;
185 String fileName = null;
186 Identifier datasetName = null;
187 Statement stmt = null;
188 Query query;
189}
190{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800191 ( "output" "to"
192 <IDENTIFIER> { nodeName = new Identifier(token.image); }
193 ":" <STRING_LITERAL> {
vinayakb38b7ca42012-03-05 05:44:15 +0000194 fileName = removeQuotesAndEscapes(token.image);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800195 stmt = new WriteStatement(nodeName, fileName, null);
Till Westmann35a0f702013-07-01 14:06:34 -0700196 }
197 ) ";"
vinayakb38b7ca42012-03-05 05:44:15 +0000198 {
199 return stmt;
200 }
201}
202
203DataverseDecl DataverseDeclaration() throws ParseException:
204{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800205 Identifier dvName = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000206}
207{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000208 "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
vinayakb38b7ca42012-03-05 05:44:15 +0000209 ";"
210 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000211 return new DataverseDecl(new Identifier(defaultDataverse));
vinayakb38b7ca42012-03-05 05:44:15 +0000212 }
213}
214
zheilbron28e026f2013-11-20 10:15:15 -0800215LoadStatement LoadStatement() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000216{
217 Identifier datasetName = null;
218 boolean alreadySorted = false;
219 String adapter;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800220 Map<String,String> properties = new HashMap<String,String>();
vinayakb38b7ca42012-03-05 05:44:15 +0000221 String name;
222 String value;
223}
224{
225 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800226
227 "using"
vinayakb38b7ca42012-03-05 05:44:15 +0000228 (
229 <STRING_LITERAL>
230 {
231 adapter = removeQuotesAndEscapes(token.image);
232 }
233 <LEFTPAREN>
234 (
235 (
236 <LEFTPAREN>
237 (
238 <STRING_LITERAL>
239 {
240 name = removeQuotesAndEscapes(token.image);
241 }
242 "=" <STRING_LITERAL>
243 {
244 value = removeQuotesAndEscapes(token.image);
245 }
246 )
247 <RIGHTPAREN>
248 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800249 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000250 }
251 )
252 (
253 "," <LEFTPAREN>
254 (
255 <STRING_LITERAL>
256 {
257 name = removeQuotesAndEscapes(token.image);
258 }
259 "=" <STRING_LITERAL>
260 {
261 value = removeQuotesAndEscapes(token.image);
262 }
263 )
264 <RIGHTPAREN>
265 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800266 properties.put(name, value);
vinayakb38b7ca42012-03-05 05:44:15 +0000267 }
268 )*
269 )?
270 <RIGHTPAREN>
271 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800272
273 ("pre-sorted"
vinayakb38b7ca42012-03-05 05:44:15 +0000274 { alreadySorted = true; }
275 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800276
vinayakb38b7ca42012-03-05 05:44:15 +0000277 ";"
278 {
zheilbron28e026f2013-11-20 10:15:15 -0800279 return new LoadStatement(null, datasetName, adapter, properties, alreadySorted);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800280 }
vinayakb38b7ca42012-03-05 05:44:15 +0000281}
282
283TypeDecl TypeDeclaration() throws ParseException:
284{
285 Identifier ident;
286 TypeExpression typeExpr;
287}
288{
289 <IDENTIFIER>
290 {
291 ident = new Identifier(token.image.toString());
292 }
293 "as"
294 ( typeExpr = TypeExpr() )
295 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000296 return new TypeDecl(null, ident, typeExpr);
vinayakb38b7ca42012-03-05 05:44:15 +0000297 }
298}
299
300TypeExpression TypeExpr() throws ParseException:
301{
302 TypeExpression typeExpr = null;
303}
304{
305 (
306 typeExpr = RecordTypeDef()
307 | typeExpr = TypeReference()
308 | typeExpr = OrderedListTypeDef()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800309 | typeExpr = UnorderedListTypeDef()
310 )
vinayakb38b7ca42012-03-05 05:44:15 +0000311 {
312 return typeExpr;
313 }
314}
315
316RecordTypeDefinition RecordTypeDef() throws ParseException:
317{
318 RecordTypeDefinition recType = new RecordTypeDefinition();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800319 RecordTypeDefinition.RecordKind recordKind = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000320}
321{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800322 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
vinayakb38b7ca42012-03-05 05:44:15 +0000323 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
324 "{"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800325 (
326 RecordField(recType)
327 ( "," RecordField(recType) )*
328 )?
vinayakb38b7ca42012-03-05 05:44:15 +0000329 "}"
330 {
331 if (recordKind == null) {
332 recordKind = RecordTypeDefinition.RecordKind.OPEN;
333 }
334 recType.setRecordKind(recordKind);
335 return recType;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800336 }
vinayakb38b7ca42012-03-05 05:44:15 +0000337}
338
339void RecordField(RecordTypeDefinition recType) throws ParseException:
340{
341 String fieldName;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800342 TypeExpression type = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000343 boolean nullable = false;
344}
345{
346 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800347 {
348 Token t = getToken(0);
349 fieldName = t.toString();
350 }
351 ":"
352 ( type = TypeExpr() )
353 ("?" { nullable = true; } )?
354 {
355
356 recType.addField(fieldName, type, nullable);
357 }
vinayakb38b7ca42012-03-05 05:44:15 +0000358}
359
360TypeReferenceExpression TypeReference() throws ParseException:
361{}
362{
363 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800364 {
365 Token t = getToken(0);
366 Identifier id;
367 if (t.toString().equalsIgnoreCase("int")) {
368 id = new Identifier("int64");
369 } else {
370 id = new Identifier(t.toString());
371 }
372 return new TypeReferenceExpression(id);
373 }
vinayakb38b7ca42012-03-05 05:44:15 +0000374}
375
376OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800377{
vinayakb38b7ca42012-03-05 05:44:15 +0000378 TypeExpression type = null;
379}
380{
381 "["
382 ( type = TypeExpr() )
383 "]"
384 {
385 return new OrderedListTypeDefinition(type);
386 }
387}
388
389
390UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800391{
vinayakb38b7ca42012-03-05 05:44:15 +0000392 TypeExpression type = null;
393}
394{
395 "<"
396 ( type = TypeExpr() )
397 ">"
398 {
399 return new UnorderedListTypeDefinition(type);
400 }
401}
402
403
404FunctionDecl FunctionDeclaration() throws ParseException:
405{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000406 FunctionDecl funcDecl;
407 FunctionSignature signature;
ramangrover29a13f2422012-03-15 23:01:27 +0000408 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +0000409 int arity = 0;
410 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
411 Expression funcBody;
412 VarIdentifier var = null;
413 createNewScope();
414}
415{
416
417 <IDENTIFIER>
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800418 {
419 Token t = getToken(0);
420 functionName = t.toString();
421 if (functionName.equalsIgnoreCase("int")) {
422 functionName = "int64";
423 }
424 }
vinayakb38b7ca42012-03-05 05:44:15 +0000425 <LEFTPAREN> (<VARIABLE>
426 {
427 var = new VarIdentifier();
428 var.setValue(getToken(0).toString());
429 paramList.add(var);
430 getCurrentScope().addNewVarSymbolToScope(var);
431 arity++;
432 }
433 ("," <VARIABLE>
434 {
435 var = new VarIdentifier();
436 var.setValue(getToken(0).toString());
437 paramList.add(var);
438 getCurrentScope().addNewVarSymbolToScope(var);
439 arity++;
440 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
441
442 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +0000443 signature = new FunctionSignature(defaultDataverse, functionName, arity);
444 getCurrentScope().addFunctionDescriptor(signature, false);
445 funcDecl = new FunctionDecl(signature, paramList, funcBody);
446 return funcDecl;
vinayakb38b7ca42012-03-05 05:44:15 +0000447 }
448}
449
450Query Query()throws ParseException:
451{
452 Query query = new Query();
453 Expression expr;
454}
455{
456 expr = Expression()
457
458 {
459 query.setBody(expr);
460 return query;
461 }
462}
463
464
465
466Expression Expression():
467{
468 Expression expr = null;
469 Expression exprP = null;
470}
471{
472(
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800473
vinayakb38b7ca42012-03-05 05:44:15 +0000474//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
475 expr = OperatorExpr()
476 | expr = IfThenElse()
477 | expr = FLWOGR()
478 | expr = QuantifiedExpression()
479
480
481)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800482 {
483 return (exprP==null) ? expr : exprP;
484 }
vinayakb38b7ca42012-03-05 05:44:15 +0000485}
486
487
488
489Expression OperatorExpr()throws ParseException:
490{
491 OperatorExpr op = null;
492 Expression operand = null;
493}
494{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800495 operand = AndExpr()
496 (
497
498 "or"
499 {
500 if (op == null) {
501 op = new OperatorExpr();
502 op.addOperand(operand);
503 op.setCurrentop(true);
504 }
505 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000506 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800507 }
vinayakb38b7ca42012-03-05 05:44:15 +0000508
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800509 operand = AndExpr()
510 {
511 op.addOperand(operand);
512 }
vinayakb38b7ca42012-03-05 05:44:15 +0000513
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800514 )*
515
516 {
517 return op==null? operand: op;
518 }
vinayakb38b7ca42012-03-05 05:44:15 +0000519}
520
521Expression AndExpr()throws ParseException:
522{
523 OperatorExpr op = null;
524 Expression operand = null;
525}
526{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800527 operand = RelExpr()
528 (
529
530 "and"
531 {
532 if (op == null) {
533 op = new OperatorExpr();
534 op.addOperand(operand);
535 op.setCurrentop(true);
536 }
537 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000538 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800539 }
vinayakb38b7ca42012-03-05 05:44:15 +0000540
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800541 operand = RelExpr()
542 {
543 op.addOperand(operand);
544 }
vinayakb38b7ca42012-03-05 05:44:15 +0000545
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800546 )*
547
548 {
549 return op==null? operand: op;
550 }
vinayakb38b7ca42012-03-05 05:44:15 +0000551}
552
553
554
555Expression RelExpr()throws ParseException:
556{
557 OperatorExpr op = null;
558 Expression operand = null;
559 boolean broadcast = false;
560}
561{
562 operand = AddExpr()
563 {
564 if (operand instanceof VariableExpr) {
565 String hint = getHint(token);
566 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800567 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +0000568 }
569 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800570 }
vinayakb38b7ca42012-03-05 05:44:15 +0000571
572 (
573 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800574 {
575 if (op == null) {
576 op = new OperatorExpr();
577 op.addOperand(operand, broadcast);
vinayakb38b7ca42012-03-05 05:44:15 +0000578 op.setCurrentop(true);
579 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800580 }
581 Token t = getToken(0);
vinayakb38b7ca42012-03-05 05:44:15 +0000582 op.addOperator(t.toString());
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800583 }
584
585 operand = AddExpr()
586 {
vinayakb38b7ca42012-03-05 05:44:15 +0000587 broadcast = false;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800588 if (operand instanceof VariableExpr) {
vinayakb38b7ca42012-03-05 05:44:15 +0000589 String hint = getHint(token);
590 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
591 broadcast = true;
592 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800593 }
vinayakb38b7ca42012-03-05 05:44:15 +0000594 op.addOperand(operand, broadcast);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800595 }
vinayakb38b7ca42012-03-05 05:44:15 +0000596 )?
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800597
598 {
599 return op==null? operand: op;
600 }
vinayakb38b7ca42012-03-05 05:44:15 +0000601}
602
603Expression AddExpr()throws ParseException:
604{
605 OperatorExpr op = null;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800606 Expression operand = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000607}
608{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800609 operand = MultExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000610
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800611 ( ("+" | "-")
612 {
613 if (op == null) {
614 op = new OperatorExpr();
615 op.addOperand(operand);
616 op.setCurrentop(true);
617 }
618 Token t = getToken(0);
619 ((OperatorExpr)op).addOperator(t.toString());
620 }
vinayakb38b7ca42012-03-05 05:44:15 +0000621
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800622 operand = MultExpr()
623 {
624 op.addOperand(operand);
625 }
626 )*
627
628 {
629 return op==null? operand: op;
630 }
vinayakb38b7ca42012-03-05 05:44:15 +0000631}
632
633Expression MultExpr()throws ParseException:
634{
635 OperatorExpr op = null;
636 Expression operand = null;
637}
638{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800639 operand = UnionExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000640
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800641 (( "*" | "/" | "%" | <CARET> | "idiv")
642 {
643 if (op == null) {
644 op = new OperatorExpr();
vinayakb38b7ca42012-03-05 05:44:15 +0000645 op.addOperand(operand);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800646 op.setCurrentop(true);
647 }
648 Token t = getToken(0);
649 op.addOperator(t.toString());
650 }
651 operand = UnionExpr()
652 {
653 op.addOperand(operand);
654 }
655 )*
656
657 {
658 return op==null?operand:op;
659 }
vinayakb38b7ca42012-03-05 05:44:15 +0000660}
661
662Expression UnionExpr() throws ParseException:
663{
664 UnionExpr union = null;
665 Expression operand1 = null;
666 Expression operand2 = null;
667}
668{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800669 operand1 = UnaryExpr()
670 ("union"
vinayakb38b7ca42012-03-05 05:44:15 +0000671 (operand2 = UnaryExpr()) {
672 if (union == null) {
673 union = new UnionExpr();
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800674 union.addExpr(operand1);
vinayakb38b7ca42012-03-05 05:44:15 +0000675 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800676 union.addExpr(operand2);
vinayakb38b7ca42012-03-05 05:44:15 +0000677 } )*
678 {
679 return (union == null)? operand1: union;
680 }
681}
682
683Expression UnaryExpr() throws ParseException:
684{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800685 Expression uexpr = null;
686 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000687}
688{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800689 (( "+"|"-")
690 {
691 uexpr = new UnaryExpr();
692 Token t = getToken(0);
693 if("+".equals(t.toString()))
694 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
695 else if("-".equals(t.toString()))
696 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
697 else
698 throw new ParseException();
699 }
700 )?
701
702 expr = ValueExpr()
703 {
704 if(uexpr!=null){
705 ((UnaryExpr)uexpr).setExpr(expr);
706 return uexpr;
707 }
708 else{
709 return expr;
710 }
711 }
vinayakb38b7ca42012-03-05 05:44:15 +0000712}
713
714Expression ValueExpr() throws ParseException:
715{
716 Expression expr;
717}
718{
719 expr = FieldOrIndexAccessor()
720 {
721 return expr;
722 }
723}
724
725
726Expression FieldOrIndexAccessor()throws ParseException:
727{
728 Expression expr = null;
729 Identifier ident = null;
730 AbstractAccessor fa = null;
icetindila611ac72014-05-16 10:10:11 -0700731 Expression indexExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000732
733}
734{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800735 ( expr = PrimaryExpr()
vinayakb38b7ca42012-03-05 05:44:15 +0000736
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800737 )
vinayakb38b7ca42012-03-05 05:44:15 +0000738
739
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800740 (
741 (
742 ident = Field()
743 {
744 if(fa == null)
745 fa = new FieldAccessor(expr, ident);
746 else
747 fa = new FieldAccessor(fa, ident);
748 }
749 )
750 | (
751 indexExpr = Index()
752 {
753 if(fa == null)
754 fa = new IndexAccessor(expr, indexExpr);
755 else
756 fa = new IndexAccessor(fa, indexExpr);
757 }
758 )
759 )*
vinayakb38b7ca42012-03-05 05:44:15 +0000760
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800761
762 {
763 return fa==null?expr:fa;
764 }
vinayakb38b7ca42012-03-05 05:44:15 +0000765}
766
767Identifier Field() throws ParseException:
768{
769 Identifier ident = null;
770
771}
772{
773 "." < IDENTIFIER >
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800774 {
vinayakb38b7ca42012-03-05 05:44:15 +0000775
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800776 ident = new Identifier();
777 ident.setValue(getToken(0).toString());
778
779 return ident;
780 }
vinayakb38b7ca42012-03-05 05:44:15 +0000781}
782
icetindila611ac72014-05-16 10:10:11 -0700783Expression Index() throws ParseException:
vinayakb38b7ca42012-03-05 05:44:15 +0000784{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800785 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000786}
787{
788 "[" ( expr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800789 {
790 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
791 {
792 Literal lit = ((LiteralExpr)expr).getValue();
793 if(lit.getLiteralType() != Literal.Type.INTEGER &&
794 lit.getLiteralType() != Literal.Type.LONG) {
795 throw new ParseException("Index should be an INTEGER");
vinayakb38b7ca42012-03-05 05:44:15 +0000796 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800797 }
798 }
vinayakb38b7ca42012-03-05 05:44:15 +0000799
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800800 | "?" // ANY
801
802 )
vinayakb38b7ca42012-03-05 05:44:15 +0000803
804 "]"
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800805 {
806 return expr;
807 }
vinayakb38b7ca42012-03-05 05:44:15 +0000808}
809
810
811Expression PrimaryExpr()throws ParseException:
812{
813 Expression expr = null;
814}
815{
ilovesoupc9fef1d2012-07-08 19:30:42 +0000816 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800817 (
818 expr =Literal()
819 | expr = FunctionCallExpr()
820 | expr =VariableRef()
821
vinayakb38b7ca42012-03-05 05:44:15 +0000822 {
823 if(((VariableExpr)expr).getIsNewVar() == true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800824 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
vinayakb38b7ca42012-03-05 05:44:15 +0000825 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800826 | expr = ListConstructor()
827 | expr = RecordConstructor()
828 | expr = ParenthesizedExpression()
829 | expr = MetaVariableRef()
830 )
831 {
832 return expr;
833 }
vinayakb38b7ca42012-03-05 05:44:15 +0000834}
835
836Expression Literal() throws ParseException:
837{
838
839 LiteralExpr lit = new LiteralExpr();
840 Token t;
841}
842{
843(
844 <STRING_LITERAL>
vinayakb38b7ca42012-03-05 05:44:15 +0000845 {
846 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800847 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
848 }
849
850 | <INTEGER_LITERAL>
851 {
852 t= getToken(0);
853 try {
854 lit.setValue(new IntegerLiteral(new Integer(t.image)));
855 } catch(NumberFormatException ex) {
856 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
857 }
858 }
vinayakb38b7ca42012-03-05 05:44:15 +0000859 | < FLOAT_LITERAL >
860 {
861 t= getToken(0);
862 lit.setValue(new FloatLiteral(new Float(t.image)));
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800863 }
864 | < DOUBLE_LITERAL >
vinayakb38b7ca42012-03-05 05:44:15 +0000865 {
866 t= getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800867 lit.setValue(new DoubleLiteral(new Double(t.image)));
868 }
869 | <NULL>
870 {
871 t= getToken(0);
872 lit.setValue(NullLiteral.INSTANCE);
873 }
874 | <TRUE>
875 {
876 t= getToken(0);
877 lit.setValue(TrueLiteral.INSTANCE);
878 }
879 | <FALSE>
880 {
881 t= getToken(0);
882 lit.setValue(FalseLiteral.INSTANCE);
883 }
vinayakb38b7ca42012-03-05 05:44:15 +0000884)
885 {
886 return lit;
887 }
888}
889
890
891VariableExpr VariableRef() throws ParseException:
892{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800893 VariableExpr varExp = new VariableExpr();
894 VarIdentifier var = new VarIdentifier();
895 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000896}
897{
898 <VARIABLE>
899 {
900 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800901 String varName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +0000902 Identifier ident = lookupSymbol(varName);
903 if (isInForbiddenScopes(varName)) {
904 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.");
905 }
906 if(ident != null) { // exist such ident
907 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800908 varExp.setVar((VarIdentifier)ident);
vinayakb38b7ca42012-03-05 05:44:15 +0000909 } else {
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800910 varExp.setVar(var);
vinayakb38b7ca42012-03-05 05:44:15 +0000911 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800912 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000913 return varExp;
914 }
915}
916
917
918VariableExpr Variable() throws ParseException:
919{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800920 VariableExpr varExp = new VariableExpr();
921 VarIdentifier var = new VarIdentifier();
922 Token t;
vinayakb38b7ca42012-03-05 05:44:15 +0000923}
924{
925 <VARIABLE>
926 {
927 t = getToken(0);//get current token
928 Identifier ident = lookupSymbol(t.toString());
929 if(ident != null) { // exist such ident
930 varExp.setIsNewVar(false);
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800931 }
932 varExp.setVar(var);
933 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000934 return varExp;
935 }
936}
937
938MetaVariableExpr MetaVariableRef() throws ParseException:
939{
940 MetaVariableExpr metaVarExp = new MetaVariableExpr();
941 VarIdentifier var = new VarIdentifier();
942 Token t;
943}
944{
945 <METAVARIABLE>
946 {
947 t = getToken(0);//get current token
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800948 metaVarExp.setVar(var);
949 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +0000950 return metaVarExp;
951 }
952}
953
954Expression ListConstructor() throws ParseException:
955{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800956 Expression expr = null;
vinayakb38b7ca42012-03-05 05:44:15 +0000957}
958{
959 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800960 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
vinayakb38b7ca42012-03-05 05:44:15 +0000961 )
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800962
vinayakb38b7ca42012-03-05 05:44:15 +0000963 {
964 return expr;
965 }
966}
967
968
969ListConstructor OrderedListConstructor() throws ParseException:
970{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800971 ListConstructor expr = new ListConstructor();
972 Expression tmp = null;
973 List<Expression> exprList = new ArrayList<Expression>();
974 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +0000975}
976{
977
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800978 "["
979 ( tmp = Expression()
980 {
981 exprList.add(tmp);
982 }
983
984 ("," tmp = Expression() { exprList.add(tmp); })*
985 )?
986
vinayakb38b7ca42012-03-05 05:44:15 +0000987 "]"
988
989 {
990 expr.setExprList(exprList);
991 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800992 }
vinayakb38b7ca42012-03-05 05:44:15 +0000993}
994
995ListConstructor UnorderedListConstructor() throws ParseException:
996{
Taewoo Kima12d8cd2015-03-04 13:47:08 -0800997 ListConstructor expr = new ListConstructor();
998 Expression tmp = null;
999 List<Expression> exprList = new ArrayList<Expression>();
1000 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
vinayakb38b7ca42012-03-05 05:44:15 +00001001}
1002{
1003
1004 "{{" ( tmp = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001005 {
1006 exprList.add(tmp);
1007 }
vinayakb38b7ca42012-03-05 05:44:15 +00001008 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1009 {
1010 expr.setExprList(exprList);
1011 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001012 }
vinayakb38b7ca42012-03-05 05:44:15 +00001013}
1014
1015RecordConstructor RecordConstructor() throws ParseException:
1016{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001017 RecordConstructor expr = new RecordConstructor();
1018 FieldBinding tmp = null;
1019 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
vinayakb38b7ca42012-03-05 05:44:15 +00001020}
1021{
1022 "{" (tmp = FieldBinding()
1023 {
1024 fbList.add(tmp);
1025 }
1026 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1027 {
1028 expr.setFbList(fbList);
1029 return expr;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001030 }
vinayakb38b7ca42012-03-05 05:44:15 +00001031}
1032
1033FieldBinding FieldBinding() throws ParseException:
1034{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001035 FieldBinding fb = new FieldBinding();
1036 Expression left, right;
vinayakb38b7ca42012-03-05 05:44:15 +00001037}
1038{
1039 left = Expression() ":" right = Expression()
1040 {
1041 fb.setLeftExpr(left);
1042 fb.setRightExpr(right);
1043 return fb;
1044 }
1045}
1046
1047Expression FunctionCallExpr() throws ParseException:
1048{
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001049 CallExpr callExpr;
1050 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001051 Expression tmp;
1052 int arity = 0;
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001053 String funcName;
1054 String dataverse;
1055 String hint=null;
1056 String id1=null;
1057 String id2=null;
vinayakb38b7ca42012-03-05 05:44:15 +00001058}
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001059{
1060 ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;}
1061 ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})?
1062 |
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001063 <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();}
1064 )
vinayakb38b7ca42012-03-05 05:44:15 +00001065 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001066 hint=getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001067 }
1068 <LEFTPAREN> (tmp = Expression()
1069 {
1070 argList.add(tmp);
1071 arity ++;
1072 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1073
1074 {
RamanGrover29@gmail.com58cf3302012-11-09 00:27:45 +00001075 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
1076 if(signature == null)
1077 {
1078 signature = new FunctionSignature(dataverse, funcName.toString(), arity);
1079 }
1080 callExpr = new CallExpr(signature,argList);
1081 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1082 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1083 }
1084 return callExpr;
vinayakb38b7ca42012-03-05 05:44:15 +00001085 }
1086}
1087
1088
1089Expression ParenthesizedExpression() throws ParseException:
1090{
1091 Expression expr;
1092}
1093{
1094 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1095 {
1096 return expr;
1097 }
1098}
1099
1100Expression IfThenElse() throws ParseException:
1101{
1102 Expression condExpr;
1103 Expression thenExpr;
1104 Expression elseExpr;
1105 IfExpr ifExpr = new IfExpr();
1106}
1107{
1108 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1109
1110 {
1111 ifExpr.setCondExpr(condExpr);
1112 ifExpr.setThenExpr(thenExpr);
1113 ifExpr.setElseExpr(elseExpr);
1114 return ifExpr;
1115 }
1116}
1117
1118Expression FLWOGR() throws ParseException:
1119{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001120 FLWOGRExpression flworg = new FLWOGRExpression();
1121 List<Clause> clauseList = new ArrayList<Clause>();
1122 Expression returnExpr;
1123 Clause tmp;
1124 createNewScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001125}
1126{
1127 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);} | tmp = MetaVariableClause() {clauseList.add(tmp);})
1128 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1129
1130 {
1131 flworg.setClauseList(clauseList);
1132 flworg.setReturnExpr(returnExpr);
1133 removeCurrentScope();
1134 return flworg;
1135 }
1136}
1137
1138List<Clause> Clauses() throws ParseException:
1139{
1140 List<Clause> clauses = new ArrayList<Clause>();
1141 Clause c = null;
1142}
1143{
1144 (
1145 (
1146 c = Clause() {
1147 clauses.add(c);
1148 }
1149 )*
1150 )
1151 {
1152 return clauses;
1153 }
1154}
1155
1156Clause Clause() throws ParseException :
1157{
1158 Clause clause;
1159}
1160{
1161 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001162 clause = ForClause()
1163 | clause = LetClause()
1164 | clause = WhereClause()
1165 | clause = OrderbyClause()
1166 | clause = GroupClause()
vinayakb38b7ca42012-03-05 05:44:15 +00001167 | clause = LimitClause()
1168 | clause = DistinctClause()
1169 | clause = MetaVariableClause()
1170 | clause = JoinClause()
1171 )
1172 {
1173 return clause;
1174 }
1175}
1176
1177Clause MetaVariableClause() throws ParseException :
1178{
1179 MetaVariableClause mc = new MetaVariableClause();
1180 VarIdentifier var = new VarIdentifier();
1181 Token t;
1182}
1183{
1184 <METAVARIABLECLAUSE>
1185 {
1186 t = getToken(0);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001187 mc.setVar(var);
1188 var.setValue(t.toString());
vinayakb38b7ca42012-03-05 05:44:15 +00001189 return mc;
1190 }
1191}
1192
1193Clause JoinClause() throws ParseException :
1194{
1195 Expression whereExpr;
1196 List<Clause> leftClauses, rightClauses;
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001197 JoinClause.JoinKind kind = JoinClause.JoinKind.INNER;
vinayakb38b7ca42012-03-05 05:44:15 +00001198}
1199{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001200 ("join" | "loj" { kind = JoinClause.JoinKind.LEFT_OUTER; } )
1201 <LEFTPAREN> <LEFTPAREN> leftClauses = Clauses() <RIGHTPAREN> ","
1202 <LEFTPAREN> rightClauses = Clauses() <RIGHTPAREN> ","
vinayakb38b7ca42012-03-05 05:44:15 +00001203 whereExpr = Expression() <RIGHTPAREN>
1204 {
1205 JoinClause jc = new JoinClause(kind);
1206 jc.setLeftClauses(leftClauses);
1207 jc.setRightClauses(rightClauses);
1208 jc.setWhereExpr(whereExpr);
1209 return jc;
1210 }
1211}
1212
1213Clause ForClause()throws ParseException :
1214{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001215 ForClause fc = new ForClause();
1216 VariableExpr varExp;
1217 VariableExpr varPos = null;
1218 Expression inExp;
1219 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001220}
1221{
1222 "for" varExp = Variable()
1223 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001224 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1225 }
1226 ("at" varPos = Variable()
1227 {
1228 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1229 }
1230 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001231 "in" ( inExp = Expression() )
1232 {
1233 fc.setVarExpr(varExp);
1234 fc.setInExpr(inExp);
1235 if (varPos != null) {
1236 fc.setPosExpr(varPos);
1237 }
1238 return fc;
1239 }
1240}
1241
1242Clause LetClause() throws ParseException:
1243{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001244 LetClause lc = new LetClause();
1245 VariableExpr varExp;
1246 Expression beExp;
1247 extendCurrentScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001248}
1249{
ilovesoupb2527c12012-07-12 03:21:13 +00001250 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001251 {
1252 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001253 lc.setVarExpr(varExp);
1254 lc.setBeExpr(beExp);
1255 return lc;
1256 }
1257}
1258
1259Clause WhereClause()throws ParseException :
1260{
1261 WhereClause wc = new WhereClause();
1262 Expression whereExpr;
1263}
1264{
1265 "where" whereExpr = Expression()
1266 {
1267 wc.setWhereExpr(whereExpr);
1268 return wc;
1269 }
1270}
1271
1272Clause OrderbyClause()throws ParseException :
1273{
1274 OrderbyClause oc = new OrderbyClause();
1275 Expression orderbyExpr;
1276 List<Expression> orderbyList = new ArrayList<Expression>();
1277 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1278 int numOfOrderby = 0;
1279}
1280{
1281 (
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001282 "order"
vinayakb38b7ca42012-03-05 05:44:15 +00001283 {
1284 String hint = getHint(token);
1285 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001286 String splits[] = hint.split(" +");
vinayakb38b7ca42012-03-05 05:44:15 +00001287 int numFrames = Integer.parseInt(splits[1]);
1288 int numTuples = Integer.parseInt(splits[2]);
1289 oc.setNumFrames(numFrames);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001290 oc.setNumTuples(numTuples);
1291 }
1292 }
vinayakb38b7ca42012-03-05 05:44:15 +00001293 "by" orderbyExpr = Expression()
1294 {
1295 orderbyList.add(orderbyExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001296 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
vinayakb38b7ca42012-03-05 05:44:15 +00001297 }
1298 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1299 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1300 {
1301 modifierList.add(modif);
1302 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001303
vinayakb38b7ca42012-03-05 05:44:15 +00001304 ("," orderbyExpr = Expression()
1305 {
1306 orderbyList.add(orderbyExpr);
1307 modif = OrderbyClause.OrderModifier.ASC;
1308 }
1309 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1310 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1311 {
1312 modifierList.add(modif);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001313 }
vinayakb38b7ca42012-03-05 05:44:15 +00001314 )*
1315)
1316 {
1317 oc.setModifierList(modifierList);
1318 oc.setOrderbyList(orderbyList);
1319 return oc;
1320 }
1321}
1322Clause GroupClause()throws ParseException :
1323{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001324 GroupbyClause gbc = new GroupbyClause();
1325 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1326 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
vinayakb38b7ca42012-03-05 05:44:15 +00001327 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001328 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1329 VariableExpr var = null;
1330 VariableExpr withVar = null;
1331 Expression expr = null;
1332 VariableExpr decorVar = null;
1333 Expression decorExpr = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001334}
1335{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001336 {
1337 Scope newScope = extendCurrentScopeNoPush(true);
1338 // extendCurrentScope(true);
1339 }
vinayakb38b7ca42012-03-05 05:44:15 +00001340 "group"
1341 {
1342 String hint = getHint(token);
1343 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001344 gbc.setHashGroupByHint(true);
1345 }
1346 }
vinayakb38b7ca42012-03-05 05:44:15 +00001347 "by" (LOOKAHEAD(2) var = Variable()
1348 {
1349 newScope.addNewVarSymbolToScope(var.getVar());
1350 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001351 expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001352 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001353 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001354 vePairList.add(pair1);
1355 }
1356 ("," ( LOOKAHEAD(2) var = Variable()
1357 {
1358 newScope.addNewVarSymbolToScope(var.getVar());
1359 } ":=")?
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001360 expr = Expression()
1361 {
1362 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
vinayakb38b7ca42012-03-05 05:44:15 +00001363 vePairList.add(pair2);
1364 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001365 )*
vinayakb38b7ca42012-03-05 05:44:15 +00001366 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001367 {
1368 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001369 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
1370 decorPairList.add(pair3);
1371 }
1372 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001373 {
1374 newScope.addNewVarSymbolToScope(decorVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001375 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001376 decorPairList.add(pair4);
vinayakb38b7ca42012-03-05 05:44:15 +00001377 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001378 )*
1379 )?
vinayakb38b7ca42012-03-05 05:44:15 +00001380 "with" withVar = VariableRef()
1381 {
1382 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001383 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001384 withVarList.add(withVar);
1385 newScope.addNewVarSymbolToScope(withVar.getVar());
1386 }
1387 ("," withVar = VariableRef()
1388 {
1389 if(withVar.getIsNewVar()==true)
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001390 throw new ParseException("can't find variable " + withVar.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001391 withVarList.add(withVar);
1392 newScope.addNewVarSymbolToScope(withVar.getVar());
1393 })*
1394 {
1395 gbc.setGbyPairList(vePairList);
1396 gbc.setDecorPairList(decorPairList);
1397 gbc.setWithVarList(withVarList);
1398 replaceCurrentScope(newScope);
1399 return gbc;
1400 }
1401}
1402
1403
1404LimitClause LimitClause() throws ParseException:
1405{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001406 LimitClause lc = new LimitClause();
1407 Expression expr;
1408 pushForbiddenScope(getCurrentScope());
vinayakb38b7ca42012-03-05 05:44:15 +00001409}
1410{
1411 "limit" expr = Expression() { lc.setLimitExpr(expr); }
1412 ("offset" expr = Expression() { lc.setOffset(expr); })?
1413
1414 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001415 popForbiddenScope();
vinayakb38b7ca42012-03-05 05:44:15 +00001416 return lc;
1417 }
1418}
1419
1420DistinctClause DistinctClause() throws ParseException:
1421{
1422 List<Expression> exprs = new ArrayList<Expression>();
1423 Expression expr;
1424}
1425{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001426 "distinct" "by" expr = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001427 {
1428 exprs.add(expr);
1429 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001430 ("," expr = Expression()
1431 {
1432 exprs.add(expr);
1433 }
vinayakb38b7ca42012-03-05 05:44:15 +00001434 )*
1435 {
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001436 return new DistinctClause(exprs);
vinayakb38b7ca42012-03-05 05:44:15 +00001437 }
1438}
1439
1440
1441QuantifiedExpression QuantifiedExpression()throws ParseException:
1442{
1443 QuantifiedExpression qc = new QuantifiedExpression();
1444 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
1445 Expression satisfiesExpr;
1446 VariableExpr var;
1447 Expression inExpr;
1448 QuantifiedPair pair;
1449}
1450{
1451 {
1452 createNewScope();
1453 }
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001454
vinayakb38b7ca42012-03-05 05:44:15 +00001455 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001456 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
1457 var = Variable() "in" inExpr = Expression()
1458 {
vinayakb38b7ca42012-03-05 05:44:15 +00001459 pair = new QuantifiedPair(var, inExpr);
1460 getCurrentScope().addNewVarSymbolToScope(var.getVar());
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001461 quantifiedList.add(pair);
1462 }
1463 (
1464 "," var = Variable() "in" inExpr = Expression()
1465 {
1466 pair = new QuantifiedPair(var, inExpr);
1467 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1468 quantifiedList.add(pair);
1469 }
1470 )*
1471 "satisfies" satisfiesExpr = Expression()
1472 {
1473 qc.setSatisfiesExpr(satisfiesExpr);
1474 qc.setQuantifiedList(quantifiedList);
1475 removeCurrentScope();
1476 return qc;
1477 }
vinayakb38b7ca42012-03-05 05:44:15 +00001478}
1479
1480TOKEN_MGR_DECLS:
1481{
1482 public int commentDepth = 0;
1483}
1484
1485<DEFAULT>
1486TOKEN :
1487{
1488 <CARET : "^" >
1489}
1490
1491<DEFAULT>
1492TOKEN :
1493{
1494 <DATASET : "dataset" >
1495}
1496
1497<DEFAULT>
1498TOKEN :
1499{
1500 <LEFTPAREN : "(" >
1501}
1502
1503<DEFAULT>
1504TOKEN :
1505{
1506 <RIGHTPAREN : ")" >
1507}
1508
1509
1510<DEFAULT>
1511TOKEN :
1512{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001513 <INTEGER_LITERAL : (<DIGIT>)+ >
vinayakb38b7ca42012-03-05 05:44:15 +00001514}
1515
1516
1517<DEFAULT>
1518TOKEN :
1519{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001520 <NULL : "null">
vinayakb38b7ca42012-03-05 05:44:15 +00001521}
1522
1523<DEFAULT>
1524TOKEN :
1525{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001526 <TRUE : "true">
vinayakb38b7ca42012-03-05 05:44:15 +00001527}
1528
1529<DEFAULT>
1530TOKEN :
1531{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001532 <FALSE : "false">
vinayakb38b7ca42012-03-05 05:44:15 +00001533}
1534
1535<DEFAULT>
1536TOKEN :
1537{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001538 <#DIGIT : ["0" - "9"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001539}
1540
1541
1542TOKEN:
1543{
1544 < DOUBLE_LITERAL: <INTEGER>
1545 | <INTEGER> ( "." <INTEGER> )?
1546 | "." <INTEGER>
1547 >
1548 |
1549 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
1550 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
1551 | "." <INTEGER> ( "f" | "F" )
1552 >
1553 |
1554 <INTEGER : (<DIGIT>)+ >
1555}
1556
1557<DEFAULT>
1558TOKEN :
1559{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001560 <#LETTER : ["A" - "Z", "a" - "z"]>
vinayakb38b7ca42012-03-05 05:44:15 +00001561}
1562
1563<DEFAULT>
1564TOKEN :
1565{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001566 <SPECIALCHARS : ["$", "_", "-"] >
vinayakb38b7ca42012-03-05 05:44:15 +00001567}
1568
1569<DEFAULT>
1570TOKEN :
1571{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001572 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
1573 |
1574 < #EscapeQuot: "\\\"" >
1575 |
vinayakb38b7ca42012-03-05 05:44:15 +00001576 < #EscapeApos: "\\\'" >
1577}
1578
1579<DEFAULT>
1580TOKEN :
1581{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001582 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
vinayakb38b7ca42012-03-05 05:44:15 +00001583}
1584
1585<DEFAULT>
1586TOKEN :
1587{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001588 <VARIABLE : "$" <IDENTIFIER> >
vinayakb38b7ca42012-03-05 05:44:15 +00001589}
1590
1591<DEFAULT>
1592TOKEN :
1593{
1594 <METAVARIABLECLAUSE : "#" <IDENTIFIER> >
1595}
1596
1597<DEFAULT>
1598TOKEN :
1599{
1600 <METAVARIABLE : "$$" <IDENTIFIER> >
1601}
1602
1603SKIP:
1604{
1605 " "
1606| "\t"
1607| "\r"
1608| "\n"
1609}
1610
1611SKIP:
1612{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001613 <"//" (~["\n"])* "\n">
vinayakb38b7ca42012-03-05 05:44:15 +00001614}
1615
1616SKIP:
1617{
Taewoo Kima12d8cd2015-03-04 13:47:08 -08001618 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
vinayakb38b7ca42012-03-05 05:44:15 +00001619}
1620
1621
1622SKIP:
1623{
1624 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
1625}
1626
1627<INSIDE_COMMENT>
1628SPECIAL_TOKEN:
1629{
1630 <"+"(" ")*(~["/","*"])*>
1631}
1632
1633<INSIDE_COMMENT>
1634SKIP:
1635{
1636 <"/*"> {commentDepth++;}
1637}
1638
1639<INSIDE_COMMENT>
1640SKIP:
1641{
1642 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
1643| <~[]>
1644}