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