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