blob: 9a47c771713ca168caa5943f80d0d2de4d9a4032 [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;
20import edu.uci.ics.asterix.aql.literal.FloatLiteral;
21import edu.uci.ics.asterix.aql.literal.DoubleLiteral;
22import edu.uci.ics.asterix.aql.literal.FalseLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000023import edu.uci.ics.asterix.aql.base.Literal;
vinayakb38b7ca42012-03-05 05:44:15 +000024import edu.uci.ics.asterix.aql.literal.IntegerLiteral;
ilovesoupc9fef1d2012-07-08 19:30:42 +000025import edu.uci.ics.asterix.aql.literal.LongIntegerLiteral;
vinayakb38b7ca42012-03-05 05:44:15 +000026import edu.uci.ics.asterix.aql.literal.NullLiteral;
27import edu.uci.ics.asterix.aql.literal.StringLiteral;
28import edu.uci.ics.asterix.aql.literal.TrueLiteral;
29
30import edu.uci.ics.asterix.aql.base.*;
31import edu.uci.ics.asterix.aql.expression.*;
32import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
33import edu.uci.ics.asterix.common.config.DatasetConfig.IndexType;
34import edu.uci.ics.asterix.aql.expression.visitor.AQLPrintVisitor;
35import edu.uci.ics.asterix.aql.expression.UnaryExpr.Sign;
36import edu.uci.ics.asterix.aql.base.Statement.Kind;
37import edu.uci.ics.asterix.aql.context.Scope;
38import edu.uci.ics.asterix.aql.context.RootScopeFactory;
39import edu.uci.ics.asterix.common.annotations.*;
40import edu.uci.ics.asterix.common.exceptions.AsterixException;
ramangrover29a13f2422012-03-15 23:01:27 +000041import edu.uci.ics.asterix.om.functions.AsterixFunction;
alexander.behm07617fd2012-07-25 10:13:50 +000042import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
43import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
vinayakb38b7ca42012-03-05 05:44:15 +000044
45
46public class AQLParser extends ScopeChecker {
47
48/*
49 private void printHints(Token t) {
50 //System.err.println("token="+t.image+"\t special="+t.specialToken);
51 if (t.specialToken == null) return;
52 Token tmp_t = t.specialToken;
53 while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;
54 while (tmp_t != null) {
55 System.out.println(tmp_t.image);
56 tmp_t = tmp_t.next;
57 }
58 }
59*/
60
61 // optimizer hints
62 private static final String HASH_GROUP_BY_HINT = "hash";
63 private static final String BROADCAST_JOIN_HINT = "bcast";
alexander.behm07617fd2012-07-25 10:13:50 +000064 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
vinayakb38b7ca42012-03-05 05:44:15 +000065 private static final String INMEMORY_HINT = "inmem";
66 private static final String VAL_FILE_HINT = "val-files";
67 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
68 private static final String INTERVAL_HINT = "interval";
69 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
70 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
71 private static final String LIST_VAL_FILE_HINT = "list-val-file";
72 private static final String LIST_HINT = "list";
73 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
74 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
75 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
76 private static final String AUTO_HINT = "auto";
77
78 private static final String GEN_FIELDS_HINT = "gen-fields";
79
80 // data generator hints
81 private static final String DGEN_HINT = "dgen";
82
83 private static String getHint(Token t) {
84 if (t.specialToken == null) {
85 return null;
86 }
87 String s = t.specialToken.image;
88 int n = s.length();
89 if (n < 2) {
90 return null;
91 }
92 return s.substring(1).trim();
93 }
94
95 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
96 File file = new File(args[0]);
97 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
98 AQLParser parser = new AQLParser(fis);
99 Statement st = parser.Statement();
100 st.accept(new AQLPrintVisitor(), 0);
101 }
102
103
104}
105
106PARSER_END(AQLParser)
107
108
109Statement Statement() throws ParseException:
110{
111 Query query = null;
112 scopeStack.push(RootScopeFactory.createRootScope(this));
113 List<Statement> decls = new ArrayList<Statement>();
114}
115{
116 (
117 (
118 (
119 "use"
120 {
121 decls.add(DataverseDeclaration());
122 }
123 | "declare" "function" {
124 decls.add(FunctionDeclaration());
125 }
126 | "create" (
127 {
128 String hint = getHint(token);
129 boolean dgen = false;
130 if (hint != null && hint.startsWith(DGEN_HINT)) {
131 dgen = true;
132 }
133 }
134 "type"
135 {
136 decls.add(TypeDeclaration(dgen, hint));
137 }
138 | "nodegroup"
139 {
140 decls.add(NodegroupDeclaration());
141 }
142 | "external" <DATASET>
143 {
144 decls.add(DatasetDeclaration(DatasetType.EXTERNAL));
145 }
146 | "feed" <DATASET>
147 {
148 decls.add(DatasetDeclaration(DatasetType.FEED));
149 }
150 | <DATASET>
151 {
152 decls.add(DatasetDeclaration(DatasetType.INTERNAL));
153 }
154 | "index"
155 {
156 decls.add(CreateIndexStatement());
157 }
158 | "dataverse"
159 {
160 decls.add(CreateDataverseStatement());
161 }
162 | "function"
163 {
164 decls.add(FunctionCreation());
165 }
166 )
167 | "load" {
168 decls.add(LoadStatement());
169 }
170
171 | "drop"
172 (
173 <DATASET>
174 {
175 decls.add(DropStatement());
176 }
177 | "index"
178 {
179 decls.add(IndexDropStatement());
180 }
181 | "nodegroup"
182 {
183 decls.add(NodeGroupDropStatement());
184 }
185 | "type"
186 {
187 decls.add(TypeDropStatement());
188 }
189 | "dataverse"
190 {
191 decls.add(DataverseDropStatement());
192 }
193 )
194 | "write" {
195 decls.add(WriteStatement());
196 }
197 | "set" {
198 decls.add(SetStatement());
199 }
200 | "insert" {
201 decls.add(InsertStatement());
202 }
203 | "delete" {
204 decls.add(DeleteStatement());
205 }
206 | "update" {
207 decls.add(UpdateStatement());
208 }
209 | "begin" "feed" <IDENTIFIER> {
210 Identifier datasetName = new Identifier(token.image);
211 decls.add(new BeginFeedStatement(datasetName, getVarCounter()));
212 } ";"
213 | "suspend" "feed" <IDENTIFIER> {
214 datasetName = new Identifier(token.image);
215 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, datasetName));
216 } ";"
217 | "resume" "feed" <IDENTIFIER> {
218 datasetName = new Identifier(token.image);
219 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, datasetName));
220 } ";"
221 | "end" "feed" <IDENTIFIER> {
222 datasetName = new Identifier(token.image);
223 decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.END, datasetName));
224 } ";"
225 | "alter" "feed" <IDENTIFIER> {
226 datasetName = new Identifier(token.image);
227 decls.add(AlterFeedDeclaration(datasetName));
228 }
229
230 )*
231 (query = Query())?
232 )
233
234 <EOF>
235 )
236 {
237 if (query == null) {
238 query = new Query(true);
239 }
240 query.setPrologDeclList(decls);
241
242 return query;
243 }
244}
245
246InsertStatement InsertStatement() throws ParseException:
247{
248 Identifier datasetName;
249 Query query;
250}
251{
252 "into" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
253 <LEFTPAREN> query = Query() <RIGHTPAREN> ";"
254 {return new InsertStatement(datasetName, query, getVarCounter());}
255}
256
257DeleteStatement DeleteStatement() throws ParseException:
258{
259 VariableExpr var = null;
260 Identifier datasetName = null;
261 Expression condition = null;
262 Clause dieClause = null;
263}
264{
265 var = Variable() { getCurrentScope().addNewVarSymbolToScope(var.getVar()); }
266 "from" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
267 ("where" condition = Expression())? (dieClause = DieClause())? ";"
268 {return new DeleteStatement(var, datasetName, condition, dieClause, getVarCounter()); }
269}
270
271UpdateStatement UpdateStatement() throws ParseException:
272{
273 VariableExpr vars;
274 Expression target;
275 Expression condition;
276 UpdateClause uc;
277 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
278}
279{
280 vars = Variable() "in" target = Expression()
281 "where" condition = Expression()
282 <LEFTPAREN> (uc=UpdateClause() {ucs.add(uc); } ("," uc=UpdateClause() {ucs.add(uc); } )*) <RIGHTPAREN> ";"
283 {return new UpdateStatement(vars, target, condition, ucs);}
284}
285
286
287
288UpdateClause UpdateClause() throws ParseException:
289{
290 Expression target = null;
291 Expression value = null ;
292 InsertStatement is = null;
293 DeleteStatement ds = null;
294 UpdateStatement us = null;
295 Expression condition = null;
296 UpdateClause ifbranch = null;
297 UpdateClause elsebranch = null;
298}
299{
300 "set" target = Expression() ":=" value = Expression()
301 | "insert" is = InsertStatement()
302 | "delete" ds = DeleteStatement()
303 | "update" us = UpdateStatement()
304 | "if" <LEFTPAREN> condition = Expression() <RIGHTPAREN> "then" ifbranch = UpdateClause() [LOOKAHEAD(1) "else" elsebranch = UpdateClause()]
305 {return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);}
306}
307
308
309Statement SetStatement() throws ParseException:
310{
311 String pn = null;
312 Statement stmt = null;
313}
314{
315 <IDENTIFIER> { pn = token.image; }
316 <STRING_LITERAL>
317 { String pv = removeQuotesAndEscapes(token.image); }
318 ";"
319 {
320 return new SetStatement(pn, pv);
321 }
322}
323
324Statement WriteStatement() throws ParseException:
325{
326 Identifier nodeName = null;
327 String fileName = null;
328 Identifier datasetName = null;
329 Statement stmt = null;
330 Query query;
331 String writerClass = null;
332}
333{
334 (( "output" "to"
335 <IDENTIFIER> { nodeName = new Identifier(token.image); }
336 ":" <STRING_LITERAL> { fileName = removeQuotesAndEscapes(token.image); }
337 ( "using" <STRING_LITERAL> { writerClass = removeQuotesAndEscapes(token.image); } )?
338 {
339 stmt = new WriteStatement(nodeName, fileName, writerClass);
340 } )
341 |
342 ( "into"
343 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
344 <LEFTPAREN> query = Query() <RIGHTPAREN>
345 {
346 stmt = new WriteFromQueryResultStatement(datasetName, query, getVarCounter());
347 } ))
348
349 ";"
350 {
351 return stmt;
352 }
353}
354
355CreateIndexStatement CreateIndexStatement() throws ParseException:
356{
357 CreateIndexStatement cis = new CreateIndexStatement();
358}
359{
360 <IDENTIFIER> { cis.setIndexName(new Identifier(token.image)); }
361 (
362 "if not exists"
363 {
364 cis.setIfNotExists(true);
365 }
366 )?
367 "on"
368 <IDENTIFIER> { cis.setDatasetName(new Identifier(token.image)); }
369 <LEFTPAREN>
370 ( <IDENTIFIER> { cis.addFieldExpr(token.image); } )
371 ("," <IDENTIFIER> { cis.addFieldExpr(token.image); })*
372 <RIGHTPAREN>
373 ("type"
374 ("btree" { cis.setIndexType(IndexType.BTREE); }
alexander.behmc576c602012-07-06 02:41:15 +0000375 | "keyword" { cis.setIndexType(IndexType.WORD_INVIX); }
376 | "rtree" { cis.setIndexType(IndexType.RTREE); }
377 | "ngram"
378 <LEFTPAREN>
379 (<INTEGER_LITERAL>
380 {
381 cis.setIndexType(IndexType.NGRAM_INVIX);
382 cis.setGramLength(Integer.valueOf(token.image));
383 }
384 )
385 <RIGHTPAREN>
386 )
vinayakb38b7ca42012-03-05 05:44:15 +0000387 ";"
388 | ";"
389 )
390 {
391 return cis;
392 }
393}
394
395DataverseDecl DataverseDeclaration() throws ParseException:
396{
397 Identifier dvName = null;
398}
399{
400 "dataverse" <IDENTIFIER> { dvName = new Identifier(token.image); }
401 ";"
402 {
403 return new DataverseDecl(dvName);
404 }
405}
406
407DropStatement DropStatement() throws ParseException :
408{
409 Identifier datasetName = null;
410 boolean ifExists = false;
411}
412{
413 < IDENTIFIER >
414 {
415 datasetName = new Identifier(token.image);
416 }
417 (
418 "if exists"
419 {
420 ifExists = true;
421 }
422 )? ";"
423 {
424 return new DropStatement(datasetName, ifExists);
425 }
426}
427
428IndexDropStatement IndexDropStatement() throws ParseException :
429{
430 Identifier datasetName = null;
431 Identifier indexName = null;
432 boolean ifExists = false;
433}
434{
435 < IDENTIFIER >
436 {
437 datasetName = new Identifier(token.image);
438 }
439 "." < IDENTIFIER >
440 {
441 indexName = new Identifier(token.image);
442 }
443 (
444 "if exists"
445 {
446 ifExists = true;
447 }
448 )? ";"
449 {
450 return new IndexDropStatement(datasetName, indexName, ifExists);
451 }
452}
453
454NodeGroupDropStatement NodeGroupDropStatement() throws ParseException :
455{
456 Identifier groupName = null;
457 boolean ifExists = false;
458}
459{
460 < IDENTIFIER >
461 {
462 groupName = new Identifier(token.image);
463 }
464 (
465 "if exists"
466 {
467 ifExists = true;
468 }
469 )? ";"
470 {
471 return new NodeGroupDropStatement(groupName, ifExists);
472 }
473}
474
475TypeDropStatement TypeDropStatement() throws ParseException :
476{
477 Identifier typeName = null;
478 boolean ifExists = false;
479}
480{
481 < IDENTIFIER >
482 {
483 typeName = new Identifier(token.image);
484 }
485 (
486 "if exists"
487 {
488 ifExists = true;
489 }
490 )? ";"
491 {
492 return new TypeDropStatement(typeName, ifExists);
493 }
494}
495
496DataverseDropStatement DataverseDropStatement() throws ParseException :
497{
498 Identifier dataverseName = null;
499 boolean ifExists = false;
500}
501{
502 < IDENTIFIER >
503 {
504 dataverseName = new Identifier(token.image);
505 }
506 (
507 "if exists"
508 {
509 ifExists = true;
510 }
511 )? ";"
512 {
513 return new DataverseDropStatement(dataverseName, ifExists);
514 }
515}
516
517CreateDataverseStatement CreateDataverseStatement() throws ParseException :
518{
519 Identifier dvName = null;
520 boolean ifNotExists = false;
521 String format = null;
522}
523{
524 < IDENTIFIER >
525 {
526 dvName = new Identifier(token.image);
527 }
528 (
529 "if not exists"
530 {
531 ifNotExists = true;
532 }
533 )?
534 (
535 "with format" < STRING_LITERAL >
536 {
537 format = removeQuotesAndEscapes(token.image);
538 }
539 )?
540 ";"
541 {
542 return new CreateDataverseStatement(dvName, format, ifNotExists);
543 }
544}
545
546LoadFromFileStatement LoadStatement() throws ParseException:
547{
548 Identifier datasetName = null;
549 boolean alreadySorted = false;
550 String adapterClassname;
551 Map<String,String> properties;
552}
553{
554 <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
555
556 "using"
557
558 <STRING_LITERAL>
559 {
560 adapterClassname = removeQuotesAndEscapes(token.image);
561 }
562
563 {
564 properties = getConfiguration();
565 }
566
567 ("pre-sorted"
568 { alreadySorted = true; }
569 )?
570
571 ";"
572 {
573 return new LoadFromFileStatement(datasetName, adapterClassname, properties, alreadySorted);
574 }
575}
576
577
578
579DatasetDecl DatasetDeclaration(DatasetType datasetType) throws ParseException :
580{
581 DatasetDecl dd = null;
582 Identifier datasetName = null;
583 Identifier itemTypeName = null;
584 boolean ifNotExists = false;
585 IDatasetDetailsDecl idd = null;
586}
587{
588 < IDENTIFIER >
589 {
590 datasetName = new Identifier(token.image);
591 }
592 (
593 "if not exists"
594 {
595 ifNotExists = true;
596 }
597 )?
598 (
599 < LEFTPAREN > < IDENTIFIER >
600 {
601 itemTypeName = new Identifier(token.image);
602 }
603 < RIGHTPAREN >
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000604 )
vinayakb38b7ca42012-03-05 05:44:15 +0000605 {
606 if(datasetType == DatasetType.INTERNAL) {
607 idd = InternalDatasetDeclaration();
608 dd = new DatasetDecl(datasetName, itemTypeName, idd, ifNotExists);
609 }
610 else if(datasetType == DatasetType.EXTERNAL) {
611 idd = ExternalDatasetDeclaration();
612 dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
613 }
614 else if(datasetType == DatasetType.FEED) {
615 idd = FeedDatasetDeclaration();
616 dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
617 }
618 dd.setDatasetType(datasetType);
619 }
620 {
621 return dd;
622 }
623}
624
625InternalDetailsDecl InternalDatasetDeclaration() throws ParseException :
626{
627 InternalDetailsDecl idd = null;
628}
629{
630 {
631 idd = new InternalDetailsDecl();
632 }
633 "partitioned" "by" "key"
634 < IDENTIFIER >
635 {
636 idd.addPartitioningExpr(token.image);
637 }
638 (
639 "," < IDENTIFIER >
640 {
641 idd.addPartitioningExpr(token.image);
642 }
643 )*
644 (
645 "on" < IDENTIFIER >
646 {
647 idd.setNodegroupName(new Identifier(token.image));
648 }
649 )?
650 ";"
651 {
652 return idd;
653 }
654}
655
656ExternalDetailsDecl ExternalDatasetDeclaration() throws ParseException :
657{
658 ExternalDetailsDecl edd = null;
659 String adapterClassname = null;
660 Map < String, String > properties;
661}
662{
663 {
664 edd = new ExternalDetailsDecl();
665 }
666
667 "using"
668
669 <STRING_LITERAL>
670 {
671 adapterClassname = removeQuotesAndEscapes(token.image);
672 }
673
674 {
675 properties = getConfiguration();
676 }
677
678 {
679 edd = new ExternalDetailsDecl();
680 edd.setAdapter(adapterClassname);
681 edd.setProperties(properties);
682 }
683 ";"
684
685 {
686 return edd;
687 }
688}
689
690FeedDetailsDecl FeedDatasetDeclaration() throws ParseException :
691{
692 FeedDetailsDecl fdd = null;
693 String adapterClassname = null;
694 Map < String, String > properties;
695}
696{
697 {
698 fdd = new FeedDetailsDecl();
699 }
700
701 "using"
702
703 <STRING_LITERAL>
704 {
705 adapterClassname = removeQuotesAndEscapes(token.image);
706 }
707
708 {
709 properties = getConfiguration();
710 }
711
712 ("apply" "function"
713 < IDENTIFIER >
714 {
715 fdd.setFunctionIdentifier(token.image);
716 }
717 )?
718
719 "partitioned" "by" "key"
720 < IDENTIFIER >
721 {
722 fdd.addPartitioningExpr(token.image);
723 }
724 (
725 "," < IDENTIFIER >
726 {
727 fdd.addPartitioningExpr(token.image);
728 }
729 )*
730 (
731 "on" < IDENTIFIER >
732 {
733 fdd.setNodegroupName(new Identifier(token.image));
734 }
735 )?
736 ";"
737 {
738 fdd.setAdapterClassname(adapterClassname);
739 fdd.setProperties(properties);
740 return fdd;
741 }
742}
743
744ControlFeedStatement AlterFeedDeclaration(Identifier datasetName) throws ParseException :
745{
746 String name = null;
747 String value = null;
748 Map < String, String > configuration = new HashMap < String, String > ();
749}
750{
751 "set"
752 {
753 configuration = getConfiguration();
754 }
755 ";"
756 {
757 return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, datasetName, configuration);
758 }
759}
760
761Map<String,String> getConfiguration() throws ParseException :
762{
763 Map<String,String> configuration = new HashMap<String,String>();
764 String key;
765 String value;
766}
767{
768
769<LEFTPAREN>
770 (
771 (
772 <LEFTPAREN>
773 (
774 <STRING_LITERAL>
775 {
776 key = removeQuotesAndEscapes(token.image);
777 }
778 "=" <STRING_LITERAL>
779 {
780 value = removeQuotesAndEscapes(token.image);
781 }
782 )
783 <RIGHTPAREN>
784 {
785 configuration.put(key, value);
786 }
787 )
788 (
789 "," <LEFTPAREN>
790 (
791 <STRING_LITERAL>
792 {
793 key = removeQuotesAndEscapes(token.image);
794 }
795 "=" <STRING_LITERAL>
796 {
797 value = removeQuotesAndEscapes(token.image);
798 }
799 )
800 <RIGHTPAREN>
801 {
802 configuration.put(key, value);
803 }
804 )*
805 )?
806 <RIGHTPAREN>
807 {
808 return configuration;
809 }
810}
811
812
813NodegroupDecl NodegroupDeclaration() throws ParseException :
814{
815 Identifier name = null;
816 List < Identifier > ncNames = new ArrayList < Identifier > ();
817 boolean ifNotExists = false;
818}
819{
820 < IDENTIFIER >
821 {
822 name = new Identifier(token.image);
823 }
824 (
825 "if not exists"
826 {
827 ifNotExists = true;
828 }
829 )?
830 "on" < IDENTIFIER >
831 {
832 ncNames.add(new Identifier(token.image));
833 }
834 (
835 "," < IDENTIFIER >
836 {
837 ncNames.add(new Identifier(token.image));
838 }
839 )*
840 ";"
841 {
842 return new NodegroupDecl(name, ncNames, ifNotExists);
843 }
844}
845
846
847TypeDecl TypeDeclaration(boolean dgen, String hint) throws ParseException:
848{
849 Identifier ident;
850 TypeExpression typeExpr;
851 boolean ifNotExists = false;
852}
853{
854 <IDENTIFIER>
855 {
856 ident = new Identifier(token.image.toString());
857 }
858 (
859 "if not exists"
860 {
861 ifNotExists = true;
862 }
863 )?
864 "as"
865 ( typeExpr = TypeExpr() )
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +0000866 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +0000867 {
868 long numValues = -1;
869 String filename = null;
870 if (dgen) {
871 String splits[] = hint.split(" +");
872 if (splits.length != 3) {
873 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
874 }
875 filename = splits[1];
876 numValues = Long.parseLong(splits[2]);
877 }
878 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
879 return new TypeDecl(ident, typeExpr, tddg, ifNotExists);
880 }
881}
882
883TypeExpression TypeExpr() throws ParseException:
884{
885 TypeExpression typeExpr = null;
886}
887{
888 (
889 typeExpr = RecordTypeDef()
890 | typeExpr = TypeReference()
891 | typeExpr = OrderedListTypeDef()
892 | typeExpr = UnorderedListTypeDef()
893 )
894 {
895 return typeExpr;
896 }
897}
898
899RecordTypeDefinition RecordTypeDef() throws ParseException:
900{
901 RecordTypeDefinition recType = new RecordTypeDefinition();
902 RecordTypeDefinition.RecordKind recordKind = null;
903}
904{
905 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
906 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
907 "{"
908 {
909 String hint = getHint(token);
910 if (hint != null) {
911 String splits[] = hint.split(" +");
912 if (splits[0].equals(GEN_FIELDS_HINT)) {
913 if (splits.length != 5) {
914 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
915 }
916 if (!splits[1].equals("int")) {
917 throw new ParseException("The only supported type for gen-fields is int.");
918 }
919 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
920 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
921 recType.setUndeclaredFieldsDataGen(ufdg);
922 }
923 }
924
925 }
926 (
927 RecordField(recType)
928 ( "," RecordField(recType) )*
929 )?
930 "}"
931 {
932 if (recordKind == null) {
933 recordKind = RecordTypeDefinition.RecordKind.OPEN;
934 }
935 recType.setRecordKind(recordKind);
936 return recType;
937 }
938}
939
940void RecordField(RecordTypeDefinition recType) throws ParseException:
941{
942 String fieldName;
943 TypeExpression type = null;
944 boolean nullable = false;
945}
946{
947 <IDENTIFIER>
948 {
949 Token t = getToken(0);
950 fieldName = t.toString();
951 String hint = getHint(t);
952 IRecordFieldDataGen rfdg = null;
953 if (hint != null) {
954 String splits[] = hint.split(" +");
955 if (splits[0].equals(VAL_FILE_HINT)) {
956 File[] valFiles = new File[splits.length - 1];
957 for (int k=1; k<splits.length; k++) {
958 valFiles[k-1] = new File(splits[k]);
959 }
960 rfdg = new FieldValFileDataGen(valFiles);
961 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
962 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
963 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
964 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
965 } else if (splits[0].equals(LIST_HINT)) {
966 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
967 } else if (splits[0].equals(INTERVAL_HINT)) {
968 FieldIntervalDataGen.ValueType vt;
969 if (splits[1].equals("int")) {
970 vt = FieldIntervalDataGen.ValueType.INT;
971 } else if (splits[1].equals("long")) {
972 vt = FieldIntervalDataGen.ValueType.LONG;
973 } else if (splits[1].equals("float")) {
974 vt = FieldIntervalDataGen.ValueType.FLOAT;
975 } else if (splits[1].equals("double")) {
976 vt = FieldIntervalDataGen.ValueType.DOUBLE;
977 } else {
978 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
979 }
980 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
981 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
982 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
983 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
984 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
985 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
986 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
987 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
988 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
989 } else if (splits[0].equals(AUTO_HINT)) {
990 rfdg = new AutoDataGen(splits[1]);
991 }
992 }
993 }
994 ":"
995 ( type = TypeExpr() )
996 ("?" { nullable = true; } )?
997 {
998 recType.addField(fieldName, type, nullable, rfdg);
999 }
1000}
1001
1002TypeReferenceExpression TypeReference() throws ParseException:
1003{}
1004{
1005 <IDENTIFIER>
1006 {
1007 Token t = getToken(0);
1008 Identifier id = new Identifier(t.toString());
1009 return new TypeReferenceExpression(id);
1010 }
1011}
1012
1013OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1014{
1015 TypeExpression type = null;
1016}
1017{
1018 "["
1019 ( type = TypeExpr() )
1020 "]"
1021 {
1022 return new OrderedListTypeDefinition(type);
1023 }
1024}
1025
1026
1027UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1028{
1029 TypeExpression type = null;
1030}
1031{
1032 "{{"
1033 ( type = TypeExpr() )
1034 "}}"
1035 {
1036 return new UnorderedListTypeDefinition(type);
1037 }
1038}
1039
1040
1041FunctionDecl FunctionDeclaration() throws ParseException:
1042{
1043 FunctionDecl func = new FunctionDecl();
ramangrover29a13f2422012-03-15 23:01:27 +00001044 AsterixFunction ident;
1045 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001046 int arity = 0;
1047 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1048 Expression funcBody;
1049 VarIdentifier var = null;
1050 createNewScope();
1051}
1052{
1053
1054 <IDENTIFIER>
1055 {
1056 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001057 functionName = t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001058 }
1059 <LEFTPAREN> (<VARIABLE>
1060 {
1061 var = new VarIdentifier();
1062 var.setValue(getToken(0).toString());
1063 paramList.add(var);
1064 getCurrentScope().addNewVarSymbolToScope(var);
1065 arity++;
1066 }
1067 ("," <VARIABLE>
1068 {
1069 var = new VarIdentifier();
1070 var.setValue(getToken(0).toString());
1071 paramList.add(var);
1072 getCurrentScope().addNewVarSymbolToScope(var);
1073 arity++;
1074 })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
1075
1076 {
ramangrover29a13f2422012-03-15 23:01:27 +00001077 ident = new AsterixFunction(functionName,arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001078 getCurrentScope().addFunctionDescriptor(ident, false);
1079 func.setIdent(ident);
1080 func.setFuncBody(funcBody);
1081 func.setParamList(paramList);
1082 return func;
1083 }
1084}
1085
1086CreateFunctionStatement FunctionCreation() throws ParseException:
1087{
1088 CreateFunctionStatement cfs = null;
ramangrover29a13f2422012-03-15 23:01:27 +00001089 AsterixFunction ident;
1090 String functionName;
vinayakb38b7ca42012-03-05 05:44:15 +00001091 int arity = 0;
1092 boolean ifNotExists = false;
1093 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1094 String funcBody;
1095 VarIdentifier var = null;
1096 createNewScope();
1097}
1098{
1099
1100 <IDENTIFIER>
1101 {
1102 Token t = getToken(0);
ramangrover29a13f2422012-03-15 23:01:27 +00001103 functionName= t.toString();
vinayakb38b7ca42012-03-05 05:44:15 +00001104 }
1105
1106 (
1107 "if not exists"
1108 {
1109 ifNotExists = true;
1110 }
1111 )?
1112
1113 <LEFTPAREN> (<VARIABLE>
1114 {
1115 var = new VarIdentifier();
1116 var.setValue(getToken(0).toString());
1117 paramList.add(var);
1118 getCurrentScope().addNewVarSymbolToScope(var);
1119 arity++;
1120 }
1121 ("," <VARIABLE>
1122 {
1123 var = new VarIdentifier();
1124 var.setValue(getToken(0).toString());
1125 paramList.add(var);
1126 getCurrentScope().addNewVarSymbolToScope(var);
1127 arity++;
1128 })*)? <RIGHTPAREN> "{" <STRING_LITERAL>
1129 {
1130 funcBody = removeQuotesAndEscapes(token.image);
1131 }
1132 "}"
1133 {
ramangrover29a13f2422012-03-15 23:01:27 +00001134 ident = new AsterixFunction(functionName, arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001135 getCurrentScope().addFunctionDescriptor(ident, false);
1136 cfs = new CreateFunctionStatement(ident, paramList, funcBody, ifNotExists);
1137 return cfs;
1138 }
1139}
1140
1141
1142
1143Query Query()throws ParseException:
1144{
1145 Query query = new Query();
1146 Expression expr;
1147}
1148{
1149 expr = Expression()
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001150 (";")?
vinayakb38b7ca42012-03-05 05:44:15 +00001151 {
1152 query.setBody(expr);
1153 return query;
1154 }
RamanGrover29@gmail.comc022a0d2012-07-28 05:13:44 +00001155
vinayakb38b7ca42012-03-05 05:44:15 +00001156}
1157
1158
1159
1160Expression Expression():
1161{
1162 Expression expr = null;
1163 Expression exprP = null;
1164}
1165{
1166(
1167
1168//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1169 expr = OperatorExpr()
1170 | expr = IfThenElse()
1171 | expr = FLWOGR()
1172 | expr = QuantifiedExpression()
1173
1174
1175)
1176 {
1177 return (exprP==null) ? expr : exprP;
1178 }
1179}
1180
1181
1182
1183Expression OperatorExpr()throws ParseException:
1184{
1185 OperatorExpr op = null;
1186 Expression operand = null;
1187}
1188{
1189 operand = AndExpr()
1190 (
1191
1192 "or"
1193 {
1194 if (op == null) {
1195 op = new OperatorExpr();
1196 op.addOperand(operand);
1197 op.setCurrentop(true);
1198 }
1199 Token t = getToken(0);
1200 op.addOperator(t.toString());
1201 }
1202
1203 operand = AndExpr()
1204 {
1205 op.addOperand(operand);
1206 }
1207
1208 )*
1209
1210 {
1211 return op==null? operand: op;
1212 }
1213}
1214
1215Expression AndExpr()throws ParseException:
1216{
1217 OperatorExpr op = null;
1218 Expression operand = null;
1219}
1220{
1221 operand = RelExpr()
1222 (
1223
1224 "and"
1225 {
1226 if (op == null) {
1227 op = new OperatorExpr();
1228 op.addOperand(operand);
1229 op.setCurrentop(true);
1230 }
1231 Token t = getToken(0);
1232 op.addOperator(t.toString());
1233 }
1234
1235 operand = RelExpr()
1236 {
1237 op.addOperand(operand);
1238 }
1239
1240 )*
1241
1242 {
1243 return op==null? operand: op;
1244 }
1245}
1246
1247
1248
1249Expression RelExpr()throws ParseException:
1250{
1251 OperatorExpr op = null;
1252 Expression operand = null;
1253 boolean broadcast = false;
alexander.behm07617fd2012-07-25 10:13:50 +00001254 IExpressionAnnotation annotation = null;
vinayakb38b7ca42012-03-05 05:44:15 +00001255}
1256{
1257 operand = AddExpr()
alexander.behm07617fd2012-07-25 10:13:50 +00001258 {
1259 if (operand instanceof VariableExpr) {
1260 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001261 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
alexander.behm07617fd2012-07-25 10:13:50 +00001262 broadcast = true;
vinayakb38b7ca42012-03-05 05:44:15 +00001263 }
1264 }
1265 }
1266
1267 (
1268 LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
1269 {
alexander.behm07617fd2012-07-25 10:13:50 +00001270 String mhint = getHint(token);
1271 if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1272 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1273 }
vinayakb38b7ca42012-03-05 05:44:15 +00001274 if (op == null) {
1275 op = new OperatorExpr();
1276 op.addOperand(operand, broadcast);
1277 op.setCurrentop(true);
1278 broadcast = false;
1279 }
1280 Token t = getToken(0);
1281 op.addOperator(t.toString());
1282 }
1283
1284 operand = AddExpr()
1285 {
alexander.behm07617fd2012-07-25 10:13:50 +00001286 broadcast = false;
1287 if (operand instanceof VariableExpr) {
1288 String hint = getHint(token);
vinayakb38b7ca42012-03-05 05:44:15 +00001289 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1290 broadcast = true;
1291 }
alexander.behm07617fd2012-07-25 10:13:50 +00001292 }
vinayakb38b7ca42012-03-05 05:44:15 +00001293 op.addOperand(operand, broadcast);
1294 }
1295 )?
1296
1297 {
alexander.behm07617fd2012-07-25 10:13:50 +00001298 if (annotation != null) {
1299 op.addHint(annotation);
1300 }
vinayakb38b7ca42012-03-05 05:44:15 +00001301 return op==null? operand: op;
1302 }
1303}
1304
1305Expression AddExpr()throws ParseException:
1306{
1307 OperatorExpr op = null;
1308 Expression operand = null;
1309}
1310{
1311 operand = MultExpr()
1312
1313 ( ("+" | "-")
1314 {
1315 if (op == null) {
1316 op = new OperatorExpr();
1317 op.addOperand(operand);
1318 op.setCurrentop(true);
1319 }
1320 Token t = getToken(0);
1321 ((OperatorExpr)op).addOperator(t.toString());
1322 }
1323
1324 operand = MultExpr()
1325 {
1326 op.addOperand(operand);
1327 }
1328 )*
1329
1330 {
1331 return op==null? operand: op;
1332 }
1333}
1334
1335Expression MultExpr()throws ParseException:
1336{
1337 OperatorExpr op = null;
1338 Expression operand = null;
1339}
1340{
1341 operand = UnionExpr()
1342
1343 (( "*" | "/" | "%" | <CARET> | "idiv")
1344 {
1345 if (op == null) {
1346 op = new OperatorExpr();
1347 op.addOperand(operand);
1348 op.setCurrentop(true);
1349 }
1350 Token t = getToken(0);
1351 op.addOperator(t.toString());
1352 }
1353 operand = UnionExpr()
1354 {
1355 op.addOperand(operand);
1356 }
1357 )*
1358
1359 {
1360 return op==null?operand:op;
1361 }
1362}
1363
1364Expression UnionExpr() throws ParseException:
1365{
1366 UnionExpr union = null;
1367 Expression operand1 = null;
1368 Expression operand2 = null;
1369}
1370{
1371 operand1 = UnaryExpr()
1372 ("union"
1373 (operand2 = UnaryExpr()) {
1374 if (union == null) {
1375 union = new UnionExpr();
1376 union.addExpr(operand1);
1377 }
1378 union.addExpr(operand2);
1379 } )*
1380 {
1381 return (union == null)? operand1: union;
1382 }
1383}
1384
1385Expression UnaryExpr() throws ParseException:
1386{
1387 Expression uexpr = null;
1388 Expression expr = null;
1389}
1390{
1391 (( "+"|"-")
1392 {
1393 uexpr = new UnaryExpr();
1394 Token t = getToken(0);
1395 if("+".equals(t.toString()))
1396 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1397 else if("-".equals(t.toString()))
1398 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1399 else
1400 throw new ParseException();
1401 }
1402 )?
1403
1404 expr = ValueExpr()
1405 {
1406 if(uexpr!=null){
1407 ((UnaryExpr)uexpr).setExpr(expr);
1408 return uexpr;
1409 }
1410 else{
1411 return expr;
1412 }
1413 }
1414}
1415
1416Expression ValueExpr() throws ParseException:
1417{
1418 Expression expr;
1419}
1420{
1421 expr = FieldOrIndexAccessor()
1422 {
1423 return expr;
1424 }
1425}
1426
1427
1428Expression FieldOrIndexAccessor()throws ParseException:
1429{
1430 Expression expr = null;
1431 Identifier ident = null;
1432 AbstractAccessor fa = null;
1433 int index;
1434
1435}
1436{
1437 ( expr = PrimaryExpr()
1438
1439 )
1440
1441
1442 (
1443 (
1444 ident = Field()
1445 {
1446 if(fa == null)
1447 fa = new FieldAccessor(expr, ident);
1448 else
1449 fa = new FieldAccessor(fa, ident);
1450 }
1451 )
1452 | (
1453 index = Index()
1454 {
1455 if(fa == null)
1456 fa = new IndexAccessor(expr, index);
1457 else
1458 fa = new IndexAccessor(fa, index);
1459 }
1460 )
1461 )*
1462
1463
1464 {
1465 return fa==null?expr:fa;
1466 }
1467}
1468
1469Identifier Field() throws ParseException:
1470{
1471 Identifier ident = null;
1472
1473}
1474{
1475 "." < IDENTIFIER >
1476 {
1477
1478 ident = new Identifier();
1479 ident.setValue(getToken(0).toString());
1480
1481 return ident;
1482 }
1483}
1484
1485int Index() throws ParseException:
1486{
1487 Expression expr = null;
1488 int idx = -2;
1489}
1490{
1491 "[" ( expr = Expression()
1492 {
1493 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1494 {
ilovesoupc9fef1d2012-07-08 19:30:42 +00001495 Literal lit = ((LiteralExpr)expr).getValue();
1496 if(lit.getLiteralType() == Literal.Type.INTEGER ||
1497 lit.getLiteralType() == Literal.Type.LONG) {
vinayakb38b7ca42012-03-05 05:44:15 +00001498 idx = Integer.valueOf(lit.getStringValue());
ilovesoupc9fef1d2012-07-08 19:30:42 +00001499 }
vinayakb38b7ca42012-03-05 05:44:15 +00001500 else {
1501 throw new ParseException("Index should be an INTEGER");
1502 }
1503 }
1504
1505 }
1506
1507 | "?"
1508 {
1509 idx = IndexAccessor.ANY;
1510 // ANY
1511 }
1512
1513 )
1514
1515 "]"
1516 {
1517 return idx;
1518 }
1519}
1520
1521
1522Expression PrimaryExpr()throws ParseException:
1523{
1524 Expression expr = null;
1525}
1526{
ilovesoupc9fef1d2012-07-08 19:30:42 +00001527 //Literal | VariableRef | ListConstructor | RecordConstructor | FunctionCallExpr | ParenthesizedExpression
vinayakb38b7ca42012-03-05 05:44:15 +00001528 (
1529 expr =Literal()
1530 | expr = FunctionCallExpr()
1531 | expr =VariableRef()
1532
1533 {
1534 if(((VariableExpr)expr).getIsNewVar() == true)
1535 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1536 }
1537 | expr = ListConstructor()
1538 | expr = RecordConstructor()
1539 | expr = ParenthesizedExpression()
1540 )
1541 {
1542 return expr;
1543 }
1544}
1545
1546Expression Literal() throws ParseException:
1547{
1548
1549 LiteralExpr lit = new LiteralExpr();
1550 Token t;
1551}
1552{
1553(
1554 <STRING_LITERAL>
1555 {
1556 t= getToken(0);
1557 lit.setValue( new StringLiteral(removeQuotesAndEscapes(t.image)));
1558 }
1559
1560 | <INTEGER_LITERAL>
1561 {
1562 t= getToken(0);
ilovesoupc9fef1d2012-07-08 19:30:42 +00001563 try {
1564 lit.setValue(new IntegerLiteral(new Integer(t.image)));
1565 } catch(NumberFormatException ex) {
1566 lit.setValue(new LongIntegerLiteral(new Long(t.image)));
1567 }
vinayakb38b7ca42012-03-05 05:44:15 +00001568 }
1569 | < FLOAT_LITERAL >
1570 {
1571 t= getToken(0);
1572 lit.setValue(new FloatLiteral(new Float(t.image)));
1573 }
1574 | < DOUBLE_LITERAL >
1575 {
1576 t= getToken(0);
1577 lit.setValue(new DoubleLiteral(new Double(t.image)));
1578 }
1579 | <NULL>
1580 {
1581 t= getToken(0);
1582 lit.setValue(NullLiteral.INSTANCE);
1583 }
1584 | <TRUE>
1585 {
1586 t= getToken(0);
1587 lit.setValue(TrueLiteral.INSTANCE);
1588 }
1589 | <FALSE>
1590 {
1591 t= getToken(0);
1592 lit.setValue(FalseLiteral.INSTANCE);
1593 }
1594)
1595 {
1596 return lit;
1597 }
1598}
1599
1600
1601VariableExpr VariableRef() throws ParseException:
1602{
1603 VariableExpr varExp = new VariableExpr();
1604 VarIdentifier var = new VarIdentifier();
1605 Token t;
1606}
1607{
1608 <VARIABLE>
1609 {
1610 t = getToken(0);//get current token
1611 String varName = t.toString();
1612 Identifier ident = lookupSymbol(varName);
1613 if (isInForbiddenScopes(varName)) {
1614 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.");
1615 }
1616 if(ident != null) { // exist such ident
1617 varExp.setIsNewVar(false);
1618 varExp.setVar((VarIdentifier)ident);
1619 } else {
1620 varExp.setVar(var);
1621 }
1622 var.setValue(t.toString());
1623 return varExp;
1624 }
1625}
1626
1627
1628VariableExpr Variable() throws ParseException:
1629{
1630 VariableExpr varExp = new VariableExpr();
1631 VarIdentifier var = new VarIdentifier();
1632 Token t;
1633}
1634{
1635 <VARIABLE>
1636 {
1637 t = getToken(0);//get current token
1638 Identifier ident = lookupSymbol(t.toString());
1639 if(ident != null) { // exist such ident
1640 varExp.setIsNewVar(false);
1641 }
1642 varExp.setVar(var);
1643 var.setValue(t.toString());
1644 return varExp;
1645 }
1646}
1647
1648Expression ListConstructor() throws ParseException:
1649{
1650 Expression expr = null;
1651}
1652{
1653 (
1654 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1655 )
1656
1657 {
1658 return expr;
1659 }
1660}
1661
1662
1663ListConstructor OrderedListConstructor() throws ParseException:
1664{
1665 ListConstructor expr = new ListConstructor();
1666 Expression tmp = null;
1667 List<Expression> exprList = new ArrayList<Expression>();
1668 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1669}
1670{
1671
1672 "["
1673 ( tmp = Expression()
1674 {
1675 exprList.add(tmp);
1676 }
1677
1678 ("," tmp = Expression() { exprList.add(tmp); })*
1679 )?
1680
1681 "]"
1682
1683 {
1684 expr.setExprList(exprList);
1685 return expr;
1686 }
1687}
1688
1689ListConstructor UnorderedListConstructor() throws ParseException:
1690{
1691 ListConstructor expr = new ListConstructor();
1692 Expression tmp = null;
1693 List<Expression> exprList = new ArrayList<Expression>();
1694 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1695}
1696{
1697
1698 "{{" ( tmp = Expression()
1699 {
1700 exprList.add(tmp);
1701 }
1702 ("," tmp = Expression() { exprList.add(tmp); })*)? "}}"
1703 {
1704 expr.setExprList(exprList);
1705 return expr;
1706 }
1707}
1708
1709RecordConstructor RecordConstructor() throws ParseException:
1710{
1711 RecordConstructor expr = new RecordConstructor();
1712 FieldBinding tmp = null;
1713 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1714}
1715{
1716 "{" (tmp = FieldBinding()
1717 {
1718 fbList.add(tmp);
1719 }
1720 ("," tmp = FieldBinding() { fbList.add(tmp); })*)? "}"
1721 {
1722 expr.setFbList(fbList);
1723 return expr;
1724 }
1725}
1726
1727FieldBinding FieldBinding() throws ParseException:
1728{
1729 FieldBinding fb = new FieldBinding();
1730 Expression left, right;
1731}
1732{
1733 left = Expression() ":" right = Expression()
1734 {
1735 fb.setLeftExpr(left);
1736 fb.setRightExpr(right);
1737 return fb;
1738 }
1739}
1740
1741Expression FunctionCallExpr() throws ParseException:
1742{
1743 CallExpr pf = new CallExpr();
alexander.behm07617fd2012-07-25 10:13:50 +00001744 List<Expression> argList = new ArrayList<Expression>();
vinayakb38b7ca42012-03-05 05:44:15 +00001745 Expression tmp;
1746 int arity = 0;
1747 Token funcName;
1748}
alexander.behm07617fd2012-07-25 10:13:50 +00001749{
vinayakb38b7ca42012-03-05 05:44:15 +00001750 ( <IDENTIFIER> | <DATASET> )
1751 {
alexander.behm07617fd2012-07-25 10:13:50 +00001752 String hint = getHint(token);
1753 if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1754 pf.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
1755 }
vinayakb38b7ca42012-03-05 05:44:15 +00001756 funcName = getToken(0);
1757 }
1758 <LEFTPAREN> (tmp = Expression()
1759 {
1760 argList.add(tmp);
1761 arity ++;
1762 } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
1763
alexander.behm07617fd2012-07-25 10:13:50 +00001764 {
ramangrover29a13f2422012-03-15 23:01:27 +00001765 AsterixFunction fd = lookupFunctionSignature(funcName.toString(), arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001766 if(fd == null)
1767 {
ramangrover29a13f2422012-03-15 23:01:27 +00001768 fd = new AsterixFunction(funcName.toString(), arity);
vinayakb38b7ca42012-03-05 05:44:15 +00001769// notFoundFunctionList.add(fd);
1770 }
1771// throw new ParseException("can't find function "+ funcName.toString() + "@" + arity);
1772 pf.setIdent(fd);
1773 pf.setExprList(argList);
1774 return pf;
1775 }
1776}
1777
1778
1779Expression ParenthesizedExpression() throws ParseException:
1780{
1781 Expression expr;
1782}
1783{
1784 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
1785 {
1786 return expr;
1787 }
1788}
1789
1790Expression IfThenElse() throws ParseException:
1791{
1792 Expression condExpr;
1793 Expression thenExpr;
1794 Expression elseExpr;
1795 IfExpr ifExpr = new IfExpr();
1796}
1797{
1798 "if" <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> "then" thenExpr = Expression() "else" elseExpr = Expression()
1799
1800 {
1801 ifExpr.setCondExpr(condExpr);
1802 ifExpr.setThenExpr(thenExpr);
1803 ifExpr.setElseExpr(elseExpr);
1804 return ifExpr;
1805 }
1806}
1807
1808Expression FLWOGR() throws ParseException:
1809{
1810 FLWOGRExpression flworg = new FLWOGRExpression();
1811 List<Clause> clauseList = new ArrayList<Clause>();
1812 Expression returnExpr;
1813 Clause tmp;
1814 createNewScope();
1815}
1816{
1817 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
1818 (tmp = Clause() {clauseList.add(tmp);})* "return" returnExpr = Expression()
1819
1820 {
1821 flworg.setClauseList(clauseList);
1822 flworg.setReturnExpr(returnExpr);
1823 removeCurrentScope();
1824 return flworg;
1825 }
1826}
1827
1828Clause Clause()throws ParseException :
1829{
1830 Clause clause;
1831}
1832{
1833 (
1834 clause = ForClause()
1835 | clause = LetClause()
1836 | clause = WhereClause()
1837 | clause = OrderbyClause()
1838 | clause = GroupClause()
1839 | clause = LimitClause()
1840 | clause = DistinctClause()
1841 | clause = DieClause()
1842 )
1843 {
1844 return clause;
1845 }
1846}
1847
1848Clause ForClause()throws ParseException :
1849{
1850 ForClause fc = new ForClause();
1851 VariableExpr varExp;
1852 VariableExpr varPos = null;
1853 Expression inExp;
1854 extendCurrentScope();
1855}
1856{
1857 "for" varExp = Variable()
1858 {
1859 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
1860 }
1861 ("at" varPos = Variable()
1862 {
1863 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
1864 }
1865 )?
1866 "in" ( inExp = Expression() )
1867 {
1868 fc.setVarExpr(varExp);
1869 fc.setInExpr(inExp);
1870 if (varPos != null) {
1871 fc.setPosExpr(varPos);
1872 }
1873 return fc;
1874 }
1875}
1876
1877Clause LetClause() throws ParseException:
1878{
1879 LetClause lc = new LetClause();
1880 VariableExpr varExp;
1881 Expression beExp;
1882 extendCurrentScope();
1883}
1884{
ilovesoupb2527c12012-07-12 03:21:13 +00001885 "let" varExp = Variable() ":=" beExp = Expression()
vinayakb38b7ca42012-03-05 05:44:15 +00001886 {
1887 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
vinayakb38b7ca42012-03-05 05:44:15 +00001888 lc.setVarExpr(varExp);
1889 lc.setBeExpr(beExp);
1890 return lc;
1891 }
1892}
1893
1894Clause WhereClause()throws ParseException :
1895{
1896 WhereClause wc = new WhereClause();
1897 Expression whereExpr;
1898}
1899{
1900 "where" whereExpr = Expression()
1901 {
1902 wc.setWhereExpr(whereExpr);
1903 return wc;
1904 }
1905}
1906
1907Clause OrderbyClause()throws ParseException :
1908{
1909 OrderbyClause oc = new OrderbyClause();
1910 Expression orderbyExpr;
1911 List<Expression> orderbyList = new ArrayList<Expression>();
1912 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
1913 int numOfOrderby = 0;
1914}
1915{
1916 (
1917 "order"
1918 {
1919 String hint = getHint(token);
1920 if (hint != null && hint.startsWith(INMEMORY_HINT)) {
1921 String splits[] = hint.split(" +");
1922 int numFrames = Integer.parseInt(splits[1]);
1923 int numTuples = Integer.parseInt(splits[2]);
1924 oc.setNumFrames(numFrames);
1925 oc.setNumTuples(numTuples);
1926 }
1927 }
1928 "by" orderbyExpr = Expression()
1929 {
1930 orderbyList.add(orderbyExpr);
1931 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
1932 }
1933 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1934 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1935 {
1936 modifierList.add(modif);
1937 }
1938
1939 ("," orderbyExpr = Expression()
1940 {
1941 orderbyList.add(orderbyExpr);
1942 modif = OrderbyClause.OrderModifier.ASC;
1943 }
1944 ( ("asc" { modif = OrderbyClause.OrderModifier.ASC; })
1945 | ("desc" { modif = OrderbyClause.OrderModifier.DESC; }))?
1946 {
1947 modifierList.add(modif);
1948 }
1949 )*
1950)
1951 {
1952 oc.setModifierList(modifierList);
1953 oc.setOrderbyList(orderbyList);
1954 return oc;
1955 }
1956}
1957Clause GroupClause()throws ParseException :
1958{
1959 GroupbyClause gbc = new GroupbyClause();
1960 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
1961 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
1962 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
1963 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
1964 VariableExpr var = null;
1965 VariableExpr withVar = null;
1966 Expression expr = null;
1967 VariableExpr decorVar = null;
1968 Expression decorExpr = null;
1969}
1970{
1971 {
1972 Scope newScope = extendCurrentScopeNoPush(true);
1973 // extendCurrentScope(true);
1974 }
1975 "group"
1976 {
1977 String hint = getHint(token);
1978 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
1979 gbc.setHashGroupByHint(true);
1980 }
1981 }
1982 "by" (LOOKAHEAD(2) var = Variable()
1983 {
1984 newScope.addNewVarSymbolToScope(var.getVar());
1985 } ":=")?
1986 expr = Expression()
1987 {
1988 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
1989 vePairList.add(pair1);
1990 }
1991 ("," ( LOOKAHEAD(2) var = Variable()
1992 {
1993 newScope.addNewVarSymbolToScope(var.getVar());
1994 } ":=")?
1995 expr = Expression()
1996 {
1997 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
1998 vePairList.add(pair2);
1999 }
2000 )*
2001 ("decor" decorVar = Variable() ":=" decorExpr = Expression()
2002 {
2003 newScope.addNewVarSymbolToScope(decorVar.getVar());
2004 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2005 decorPairList.add(pair3);
2006 }
2007 ("," "decor" decorVar = Variable() ":=" decorExpr = Expression()
2008 {
2009 newScope.addNewVarSymbolToScope(decorVar.getVar());
2010 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2011 decorPairList.add(pair4);
2012 }
2013 )*
2014 )?
2015 "with" withVar = VariableRef()
2016 {
2017 if(withVar.getIsNewVar()==true)
2018 throw new ParseException("can't find variable " + withVar.getVar());
2019 withVarList.add(withVar);
2020 newScope.addNewVarSymbolToScope(withVar.getVar());
2021 }
2022 ("," withVar = VariableRef()
2023 {
2024 if(withVar.getIsNewVar()==true)
2025 throw new ParseException("can't find variable " + withVar.getVar());
2026 withVarList.add(withVar);
2027 newScope.addNewVarSymbolToScope(withVar.getVar());
2028 })*
2029 {
2030 gbc.setGbyPairList(vePairList);
2031 gbc.setDecorPairList(decorPairList);
2032 gbc.setWithVarList(withVarList);
2033 replaceCurrentScope(newScope);
2034 return gbc;
2035 }
2036}
2037
2038
2039LimitClause LimitClause() throws ParseException:
2040{
2041 LimitClause lc = new LimitClause();
2042 Expression expr;
2043 pushForbiddenScope(getCurrentScope());
2044}
2045{
2046 "limit" expr = Expression() { lc.setLimitExpr(expr); }
2047 ("offset" expr = Expression() { lc.setOffset(expr); })?
2048
2049 {
2050 popForbiddenScope();
2051 return lc;
2052 }
2053}
2054
2055DistinctClause DistinctClause() throws ParseException:
2056{
2057 List<Expression> exprs = new ArrayList<Expression>();
2058 Expression expr;
2059}
2060{
2061 "distinct" "by" expr = Expression()
2062 {
2063 exprs.add(expr);
2064 }
2065 ("," expr = Expression()
2066 {
2067 exprs.add(expr);
2068 }
2069 )*
2070 {
2071 return new DistinctClause(exprs);
2072 }
2073}
2074
2075DieClause DieClause() throws ParseException:
2076{
2077 DieClause lc = new DieClause();
2078 Expression expr;
2079 pushForbiddenScope(getCurrentScope());
2080}
2081{
2082 "die" "after" expr = Expression() { lc.setDieExpr(expr); }
2083 {
2084 popForbiddenScope();
2085 return lc;
2086 }
2087}
2088
2089
2090QuantifiedExpression QuantifiedExpression()throws ParseException:
2091{
2092 QuantifiedExpression qc = new QuantifiedExpression();
2093 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2094 Expression satisfiesExpr;
2095 VariableExpr var;
2096 Expression inExpr;
2097 QuantifiedPair pair;
2098}
2099{
2100 {
2101 createNewScope();
2102 }
2103
2104 ( ("some" { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2105 | ("every" { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2106 var = Variable() "in" inExpr = Expression()
2107 {
2108 pair = new QuantifiedPair(var, inExpr);
2109 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2110 quantifiedList.add(pair);
2111 }
2112 (
2113 "," var = Variable() "in" inExpr = Expression()
2114 {
2115 pair = new QuantifiedPair(var, inExpr);
2116 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2117 quantifiedList.add(pair);
2118 }
2119 )*
2120 "satisfies" satisfiesExpr = Expression()
2121 {
2122 qc.setSatisfiesExpr(satisfiesExpr);
2123 qc.setQuantifiedList(quantifiedList);
2124 removeCurrentScope();
2125 return qc;
2126 }
2127}
2128
2129TOKEN_MGR_DECLS:
2130{
2131 public int commentDepth = 0;
2132}
2133
2134<DEFAULT>
2135TOKEN :
2136{
2137 <CARET : "^" >
2138}
2139
2140<DEFAULT>
2141TOKEN :
2142{
2143 <DATASET : "dataset" >
2144}
2145
2146<DEFAULT>
2147TOKEN :
2148{
2149 <LEFTPAREN : "(" >
2150}
2151
2152<DEFAULT>
2153TOKEN :
2154{
2155 <RIGHTPAREN : ")" >
2156}
2157
2158
2159<DEFAULT>
2160TOKEN :
2161{
2162 <INTEGER_LITERAL : (<DIGIT>)+ >
2163}
2164
2165
2166<DEFAULT>
2167TOKEN :
2168{
2169 <NULL : "null">
2170}
2171
2172<DEFAULT>
2173TOKEN :
2174{
2175 <TRUE : "true">
2176}
2177
2178<DEFAULT>
2179TOKEN :
2180{
2181 <FALSE : "false">
2182}
2183
2184<DEFAULT>
2185TOKEN :
2186{
2187 <#DIGIT : ["0" - "9"]>
2188}
2189
2190
2191TOKEN:
2192{
2193 < DOUBLE_LITERAL: <INTEGER>
2194 | <INTEGER> ( "." <INTEGER> )?
2195 | "." <INTEGER>
2196 >
2197 |
2198 < FLOAT_LITERAL: <INTEGER> ( "f" | "F" )
2199 | <INTEGER> ( "." <INTEGER> ( "f" | "F" ) )?
2200 | "." <INTEGER> ( "f" | "F" )
2201 >
2202 |
2203 <INTEGER : (<DIGIT>)+ >
2204}
2205
2206<DEFAULT>
2207TOKEN :
2208{
2209 <#LETTER : ["A" - "Z", "a" - "z"]>
2210}
2211
2212<DEFAULT>
2213TOKEN :
2214{
2215 <SPECIALCHARS : ["$", "_", "-"] >
2216}
2217
2218<DEFAULT>
2219TOKEN :
2220{
2221 <STRING_LITERAL : ("\"" (<EscapeQuot> | ~["\""])* "\"") | ("\'"(<EscapeApos> | ~["\'"])* "\'")>
2222 |
2223 < #EscapeQuot: "\\\"" >
2224 |
2225 < #EscapeApos: "\\\'" >
2226}
2227
2228<DEFAULT>
2229TOKEN :
2230{
2231 <IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2232}
2233
2234<DEFAULT>
2235TOKEN :
2236{
2237 <VARIABLE : "$" <IDENTIFIER> >
2238}
2239
2240SKIP:
2241{
2242 " "
2243| "\t"
2244| "\r"
2245| "\n"
2246}
2247
2248SKIP:
2249{
2250 <"//" (~["\n"])* "\n">
2251}
2252
2253SKIP:
2254{
2255 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2256}
2257
2258
2259SKIP:
2260{
2261 <"/*"> {commentDepth=1;}: INSIDE_COMMENT
2262}
2263
2264<INSIDE_COMMENT>
2265SPECIAL_TOKEN:
2266{
2267 <"+"(" ")*(~["*"])*>
2268}
2269
2270<INSIDE_COMMENT>
2271SKIP:
2272{
2273 <"/*"> {commentDepth++;}
2274}
2275
2276<INSIDE_COMMENT>
2277SKIP:
2278{
2279 <"*/"> {commentDepth--; if (commentDepth == 0) SwitchTo(DEFAULT);}
2280| <~[]>
2281}