blob: bb574d090b1f981edcf193190f074cbee8098077 [file] [log] [blame]
Yingyi Bu391f09e2015-10-29 13:49:39 -07001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package org.apache.asterix.lang.aql.parser;
12
13// For AQLParserTokenManager
14import org.apache.xerces.util.IntStack;
15
16import java.io.BufferedReader;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.io.Reader;
23import java.io.StringReader;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.LinkedHashMap;
27import java.util.List;
28import java.util.Map;
29
30import org.apache.asterix.common.annotations.AutoDataGen;
31import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
32import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
33import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
34import org.apache.asterix.common.annotations.FieldIntervalDataGen;
35import org.apache.asterix.common.annotations.FieldValFileDataGen;
36import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
37import org.apache.asterix.common.annotations.IRecordFieldDataGen;
38import org.apache.asterix.common.annotations.InsertRandIntDataGen;
39import org.apache.asterix.common.annotations.ListDataGen;
40import org.apache.asterix.common.annotations.ListValFileDataGen;
41import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
42import org.apache.asterix.common.annotations.TypeDataGen;
43import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
44import org.apache.asterix.common.config.DatasetConfig.DatasetType;
45import org.apache.asterix.common.config.DatasetConfig.IndexType;
46import org.apache.asterix.common.exceptions.AsterixException;
47import org.apache.asterix.common.functions.FunctionSignature;
48import org.apache.asterix.lang.aql.clause.DistinctClause;
49import org.apache.asterix.lang.aql.clause.ForClause;
50import org.apache.asterix.lang.aql.expression.FLWOGRExpression;
51import org.apache.asterix.lang.aql.expression.UnionExpr;
52import org.apache.asterix.lang.aql.util.RangeMapBuilder;
53import org.apache.asterix.lang.common.base.Clause;
54import org.apache.asterix.lang.common.base.Expression;
55import org.apache.asterix.lang.common.base.IParser;
56import org.apache.asterix.lang.common.base.Literal;
57import org.apache.asterix.lang.common.base.Statement;
58import org.apache.asterix.lang.common.clause.GroupbyClause;
59import org.apache.asterix.lang.common.clause.LetClause;
60import org.apache.asterix.lang.common.clause.LimitClause;
61import org.apache.asterix.lang.common.clause.OrderbyClause;
62import org.apache.asterix.lang.common.clause.UpdateClause;
63import org.apache.asterix.lang.common.clause.WhereClause;
64import org.apache.asterix.lang.common.context.RootScopeFactory;
65import org.apache.asterix.lang.common.context.Scope;
66import org.apache.asterix.lang.common.expression.AbstractAccessor;
67import org.apache.asterix.lang.common.expression.CallExpr;
68import org.apache.asterix.lang.common.expression.FieldAccessor;
69import org.apache.asterix.lang.common.expression.FieldBinding;
70import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
71import org.apache.asterix.lang.common.expression.IfExpr;
72import org.apache.asterix.lang.common.expression.IndexAccessor;
73import org.apache.asterix.lang.common.expression.ListConstructor;
74import org.apache.asterix.lang.common.expression.LiteralExpr;
75import org.apache.asterix.lang.common.expression.OperatorExpr;
76import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
77import org.apache.asterix.lang.common.expression.QuantifiedExpression;
78import org.apache.asterix.lang.common.expression.RecordConstructor;
79import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
80import org.apache.asterix.lang.common.expression.TypeExpression;
81import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
82import org.apache.asterix.lang.common.expression.UnaryExpr;
83import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
84import org.apache.asterix.lang.common.expression.VariableExpr;
85import org.apache.asterix.lang.common.expression.UnaryExpr.Sign;
86import org.apache.asterix.lang.common.literal.DoubleLiteral;
87import org.apache.asterix.lang.common.literal.FalseLiteral;
88import org.apache.asterix.lang.common.literal.FloatLiteral;
89import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
90import org.apache.asterix.lang.common.literal.NullLiteral;
91import org.apache.asterix.lang.common.literal.StringLiteral;
92import org.apache.asterix.lang.common.literal.TrueLiteral;
93import org.apache.asterix.lang.common.parser.ScopeChecker;
94import org.apache.asterix.lang.common.statement.CompactStatement;
95import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
96import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
97import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
98import org.apache.asterix.lang.common.statement.CreateFeedStatement;
99import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
100import org.apache.asterix.lang.common.statement.CreateIndexStatement;
101import org.apache.asterix.lang.common.statement.CreatePrimaryFeedStatement;
102import org.apache.asterix.lang.common.statement.CreateSecondaryFeedStatement;
103import org.apache.asterix.lang.common.statement.DatasetDecl;
104import org.apache.asterix.lang.common.statement.DataverseDecl;
105import org.apache.asterix.lang.common.statement.DataverseDropStatement;
106import org.apache.asterix.lang.common.statement.DeleteStatement;
107import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
108import org.apache.asterix.lang.common.statement.DropStatement;
109import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
110import org.apache.asterix.lang.common.statement.FeedDropStatement;
111import org.apache.asterix.lang.common.statement.FunctionDecl;
112import org.apache.asterix.lang.common.statement.FunctionDropStatement;
113import org.apache.asterix.lang.common.statement.IndexDropStatement;
114import org.apache.asterix.lang.common.statement.InsertStatement;
115import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
116import org.apache.asterix.lang.common.statement.LoadStatement;
117import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
118import org.apache.asterix.lang.common.statement.NodegroupDecl;
119import org.apache.asterix.lang.common.statement.Query;
120import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
121import org.apache.asterix.lang.common.statement.RunStatement;
122import org.apache.asterix.lang.common.statement.SetStatement;
123import org.apache.asterix.lang.common.statement.TypeDecl;
124import org.apache.asterix.lang.common.statement.TypeDropStatement;
125import org.apache.asterix.lang.common.statement.UpdateStatement;
126import org.apache.asterix.lang.common.statement.WriteStatement;
127import org.apache.asterix.lang.common.struct.Identifier;
128import org.apache.asterix.lang.common.struct.QuantifiedPair;
129import org.apache.asterix.lang.common.struct.VarIdentifier;
130import org.apache.asterix.metadata.bootstrap.MetadataConstants;
131import org.apache.hyracks.algebricks.common.utils.Pair;
132import org.apache.hyracks.algebricks.common.utils.Triple;
133import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
134import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
135import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
136
137
138public class AQLParser extends ScopeChecker implements IParser {
139
140 // optimizer hints
141 private static final String AUTO_HINT = "auto";
142 private static final String BROADCAST_JOIN_HINT = "bcast";
143 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
144 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
145 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
146 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
147 private static final String HASH_GROUP_BY_HINT = "hash";
148 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
149 private static final String INMEMORY_HINT = "inmem";
150 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
151 private static final String INTERVAL_HINT = "interval";
152 private static final String LIST_HINT = "list";
153 private static final String LIST_VAL_FILE_HINT = "list-val-file";
154 private static final String RANGE_HINT = "range";
155 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
156 private static final String VAL_FILE_HINT = "val-files";
157 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
158
159 private static final String GEN_FIELDS_HINT = "gen-fields";
160
161 // data generator hints
162 private static final String DGEN_HINT = "dgen";
163
164 private static class IndexParams {
165 public IndexType type;
166 public int gramLength;
167
168 public IndexParams(IndexType type, int gramLength) {
169 this.type = type;
170 this.gramLength = gramLength;
171 }
172 };
173
174 private static class FunctionName {
175 public String dataverse = null;
176 public String library = null;
177 public String function = null;
178 public String hint = null;
179 }
180
181 private static String getHint(Token t) {
182 if (t.specialToken == null) {
183 return null;
184 }
185 String s = t.specialToken.image;
186 int n = s.length();
187 if (n < 2) {
188 return null;
189 }
190 return s.substring(1).trim();
191 }
192
193 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
194 IRecordFieldDataGen rfdg = null;
195 String splits[] = hint.split(" +");
196 if (splits[0].equals(VAL_FILE_HINT)) {
197 File[] valFiles = new File[splits.length - 1];
198 for (int k=1; k<splits.length; k++) {
199 valFiles[k-1] = new File(splits[k]);
200 }
201 rfdg = new FieldValFileDataGen(valFiles);
202 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
203 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
204 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
205 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
206 } else if (splits[0].equals(LIST_HINT)) {
207 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
208 } else if (splits[0].equals(INTERVAL_HINT)) {
209 FieldIntervalDataGen.ValueType vt;
210 if (splits[1].equals("int")) {
211 vt = FieldIntervalDataGen.ValueType.INT;
212 } else if (splits[1].equals("long")) {
213 vt = FieldIntervalDataGen.ValueType.LONG;
214 } else if (splits[1].equals("float")) {
215 vt = FieldIntervalDataGen.ValueType.FLOAT;
216 } else if (splits[1].equals("double")) {
217 vt = FieldIntervalDataGen.ValueType.DOUBLE;
218 } else {
219 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
220 }
221 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
222 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
223 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
224 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
225 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
226 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
227 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
228 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
229 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
230 } else if (splits[0].equals(AUTO_HINT)) {
231 rfdg = new AutoDataGen(splits[1]);
232 }
233 return rfdg;
234 }
235
236 public AQLParser(String s){
237 this(new StringReader(s));
238 super.setInput(s);
239 }
240
241 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
242 File file = new File(args[0]);
243 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
244 AQLParser parser = new AQLParser(fis);
245 List<Statement> st = parser.parse();
246 //st.accept(new AQLPrintVisitor(), 0);
247 }
248
249 public List<Statement> parse() throws AsterixException {
250 try {
251 return Statement();
252 } catch (Error e) {
253 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
254 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
255 throw new AsterixException(new ParseException(e.getMessage()));
256 } catch (ParseException e){
257 throw new AsterixException(e.getMessage());
258 }
259 }
260}
261
262PARSER_END(AQLParser)
263
264
265List<Statement> Statement() throws ParseException:
266{
267 scopeStack.push(RootScopeFactory.createRootScope(this));
268 List<Statement> decls = new ArrayList<Statement>();
269 Statement stmt = null;
270}
271{
272 ( stmt = SingleStatement() (";") ?
273 {
274 decls.add(stmt);
275 }
276 )*
277 <EOF>
278 {
279 return decls;
280 }
281}
282
283Statement SingleStatement() throws ParseException:
284{
285 Statement stmt = null;
286}
287{
288 (
289 stmt = DataverseDeclaration()
290 | stmt = FunctionDeclaration()
291 | stmt = CreateStatement()
292 | stmt = LoadStatement()
293 | stmt = DropStatement()
294 | stmt = WriteStatement()
295 | stmt = SetStatement()
296 | stmt = InsertStatement()
297 | stmt = DeleteStatement()
298 | stmt = UpdateStatement()
299 | stmt = FeedStatement()
300 | stmt = CompactStatement()
301 | stmt = Query()
302 | stmt = RefreshExternalDatasetStatement()
303 | stmt = RunStatement()
304 )
305 {
306 return stmt;
307 }
308}
309
310DataverseDecl DataverseDeclaration() throws ParseException:
311{
312 String dvName = null;
313}
314{
315 "use" "dataverse" dvName = Identifier()
316 {
317 defaultDataverse = dvName;
318 return new DataverseDecl(new Identifier(dvName));
319 }
320}
321
322Statement CreateStatement() throws ParseException:
323{
324 String hint = null;
325 boolean dgen = false;
326 Statement stmt = null;
327}
328{
329 "create"
330 (
331 {
332 hint = getHint(token);
333 if (hint != null && hint.startsWith(DGEN_HINT)) {
334 dgen = true;
335 }
336 }
337 stmt = TypeSpecification(hint, dgen)
338 | stmt = NodegroupSpecification()
339 | stmt = DatasetSpecification()
340 | stmt = IndexSpecification()
341 | stmt = DataverseSpecification()
342 | stmt = FunctionSpecification()
343 | stmt = FeedSpecification()
344 | stmt = FeedPolicySpecification()
345 )
346 {
347 return stmt;
348 }
349}
350
351TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
352{
353 Pair<Identifier,Identifier> nameComponents = null;
354 boolean ifNotExists = false;
355 TypeExpression typeExpr = null;
356}
357{
358 "type" nameComponents = TypeName() ifNotExists = IfNotExists()
359 "as" typeExpr = TypeExpr()
360 {
361 long numValues = -1;
362 String filename = null;
363 if (dgen) {
364 String splits[] = hint.split(" +");
365 if (splits.length != 3) {
366 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
367 }
368 filename = splits[1];
369 numValues = Long.parseLong(splits[2]);
370 }
371 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
372 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
373 }
374}
375
376
377NodegroupDecl NodegroupSpecification() throws ParseException:
378{
379 String name = null;
380 String tmp = null;
381 boolean ifNotExists = false;
382 List<Identifier>ncNames = null;
383}
384{
385 "nodegroup" name = Identifier()
386 ifNotExists = IfNotExists() "on" tmp = Identifier()
387 {
388 ncNames = new ArrayList<Identifier>();
389 ncNames.add(new Identifier(tmp));
390 }
391 ( <COMMA> tmp = Identifier()
392 {
393 ncNames.add(new Identifier(tmp));
394 }
395 )*
396 {
397 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
398 }
399}
400
401DatasetDecl DatasetSpecification() throws ParseException:
402{
403 Pair<Identifier,Identifier> nameComponents = null;
404 boolean ifNotExists = false;
405 String typeName = null;
406 String adapterName = null;
407 Map<String,String> properties = null;
408 Map<String,String> compactionPolicyProperties = null;
409 FunctionSignature appliedFunction = null;
410 List<List<String>> primaryKeyFields = null;
411 String nodeGroupName = null;
412 Map<String,String> hints = new HashMap<String,String>();
413 DatasetDecl dsetDecl = null;
414 boolean autogenerated = false;
415 String compactionPolicy = null;
416 boolean temp = false;
417 List<String> filterField = null;
418}
419{
420 (
421 "external" <DATASET> nameComponents = QualifiedName()
422 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
423 ifNotExists = IfNotExists()
424 "using" adapterName = AdapterName() properties = Configuration()
425 ("on" nodeGroupName = Identifier() )?
426 ( "hints" hints = Properties() )?
427 ( "using" "compaction" "policy" compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
428 {
429 ExternalDetailsDecl edd = new ExternalDetailsDecl();
430 edd.setAdapter(adapterName);
431 edd.setProperties(properties);
432 dsetDecl = new DatasetDecl(nameComponents.first,
433 nameComponents.second,
434 new Identifier(typeName),
435 nodeGroupName != null? new Identifier(nodeGroupName): null,
436 compactionPolicy,
437 compactionPolicyProperties,
438 hints,
439 DatasetType.EXTERNAL,
440 edd,
441 ifNotExists);
442 }
443
444 | ("internal" | "temporary" {
445 temp = token.image.toLowerCase().equals("temporary");
446 }
447 )?
448 <DATASET> nameComponents = QualifiedName()
449 <LEFTPAREN> typeName = Identifier() <RIGHTPAREN>
450 ifNotExists = IfNotExists()
451 primaryKeyFields = PrimaryKey()
452 ("autogenerated" { autogenerated = true; } )?
453 ("on" nodeGroupName = Identifier() )?
454 ( "hints" hints = Properties() )?
455 ( "using" "compaction" "policy" compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
456 ( "with filter on" filterField = NestedField() )?
457 {
458 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields,
459 autogenerated,
460 filterField,
461 temp);
462 dsetDecl = new DatasetDecl(nameComponents.first,
463 nameComponents.second,
464 new Identifier(typeName),
465 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
466 compactionPolicy,
467 compactionPolicyProperties,
468 hints,
469 DatasetType.INTERNAL,
470 idd,
471 ifNotExists);
472 }
473 )
474 {
475 return dsetDecl;
476 }
477}
478
479RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
480{
481 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
482 Pair<Identifier,Identifier> nameComponents = null;
483 String datasetName = null;
484}
485{
486 "refresh external" <DATASET> nameComponents = QualifiedName()
487 {
488 redss.setDataverseName(nameComponents.first);
489 redss.setDatasetName(nameComponents.second);
490 return redss;
491 }
492}
493
494RunStatement RunStatement() throws ParseException:
495{
496 String system = null;
497 String tmp;
498 ArrayList<String> parameters = new ArrayList<String>();
499 Pair<Identifier,Identifier> nameComponentsFrom = null;
500 Pair<Identifier,Identifier> nameComponentsTo = null;
501}
502{
503 "run" system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
504 {
505 parameters.add(tmp);
506 }
507 )*<RIGHTPAREN>
508 <FROM> <DATASET> nameComponentsFrom = QualifiedName()
509 "to" <DATASET> nameComponentsTo = QualifiedName()
510 {
511 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
512 }
513}
514
515CreateIndexStatement IndexSpecification() throws ParseException:
516{
517 CreateIndexStatement cis = new CreateIndexStatement();
518 String indexName = null;
519 boolean ifNotExists = false;
520 Pair<Identifier,Identifier> nameComponents = null;
521 Pair<List<String>, TypeExpression> fieldPair = null;
522 IndexParams indexType = null;
523 boolean enforced = false;
524}
525{
526 "index" indexName = Identifier()
527 ifNotExists = IfNotExists()
528 "on" nameComponents = QualifiedName()
529 <LEFTPAREN> ( fieldPair = OpenField()
530 {
531 cis.addFieldExprPair(fieldPair);
532 }
533 ) (<COMMA> fieldPair = OpenField()
534 {
535 cis.addFieldExprPair(fieldPair);
536 }
537 )* <RIGHTPAREN> ( "type" indexType = IndexType() )? ( "enforced" { enforced = true; } )?
538 {
539 cis.setIndexName(new Identifier(indexName));
540 cis.setIfNotExists(ifNotExists);
541 cis.setDataverseName(nameComponents.first);
542 cis.setDatasetName(nameComponents.second);
543 if (indexType != null) {
544 cis.setIndexType(indexType.type);
545 cis.setGramLength(indexType.gramLength);
546 }
547 cis.setEnforced(enforced);
548 return cis;
549 }
550}
551
552String CompactionPolicy() throws ParseException :
553{
554 String compactionPolicy = null;
555}
556{
557 compactionPolicy = Identifier()
558 {
559 return compactionPolicy;
560 }
561}
562
563String FilterField() throws ParseException :
564{
565 String filterField = null;
566}
567{
568 filterField = Identifier()
569 {
570 return filterField;
571 }
572}
573
574IndexParams IndexType() throws ParseException:
575{
576 IndexType type = null;
577 int gramLength = 0;
578}
579{
580 ("btree"
581 {
582 type = IndexType.BTREE;
583 }
584 | "rtree"
585 {
586 type = IndexType.RTREE;
587 }
588 | "keyword"
589 {
590 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
591 }
592 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
593 {
594 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
595 gramLength = Integer.valueOf(token.image);
596 }
597 <RIGHTPAREN>)
598 {
599 return new IndexParams(type, gramLength);
600 }
601}
602
603CreateDataverseStatement DataverseSpecification() throws ParseException :
604{
605 String dvName = null;
606 boolean ifNotExists = false;
607 String format = null;
608}
609{
610 "dataverse" dvName = Identifier()
611 ifNotExists = IfNotExists()
612 ( "with format" format = StringLiteral() )?
613 {
614 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
615 }
616}
617
618CreateFunctionStatement FunctionSpecification() throws ParseException:
619{
620 FunctionSignature signature;
621 boolean ifNotExists = false;
622 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
623 String functionBody;
624 VarIdentifier var = null;
625 Expression functionBodyExpr;
626 Token beginPos;
627 Token endPos;
628 FunctionName fctName = null;
629
630 createNewScope();
631}
632{
633 "function" fctName = FunctionName()
634 ifNotExists = IfNotExists()
635 paramList = ParameterList()
636 <LEFTBRACE>
637 {
638 beginPos = token;
639 }
640 functionBodyExpr = Expression() <RIGHTBRACE>
641 {
642 endPos = token;
643 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
644 // TODO use fctName.library
645 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
646 getCurrentScope().addFunctionDescriptor(signature, false);
647 removeCurrentScope();
648 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
649 }
650}
651
652CreateFeedStatement FeedSpecification() throws ParseException:
653{
654 Pair<Identifier,Identifier> nameComponents = null;
655 boolean ifNotExists = false;
656 String adapterName = null;
657 Map<String,String> properties = null;
658 FunctionSignature appliedFunction = null;
659 CreateFeedStatement cfs = null;
660 Pair<Identifier,Identifier> sourceNameComponents = null;
661
662}
663{
664 (
665 "secondary" "feed" nameComponents = QualifiedName() ifNotExists = IfNotExists()
666 <FROM> "feed" sourceNameComponents = QualifiedName() (appliedFunction = ApplyFunction())?
667 {
668 cfs = new CreateSecondaryFeedStatement(nameComponents,
669 sourceNameComponents, appliedFunction, ifNotExists);
670 }
671 |
672 ("primary")? "feed" nameComponents = QualifiedName() ifNotExists = IfNotExists()
673 "using" adapterName = AdapterName() properties = Configuration() (appliedFunction = ApplyFunction())?
674 {
675 cfs = new CreatePrimaryFeedStatement(nameComponents,
676 adapterName, properties, appliedFunction, ifNotExists);
677 }
678 )
679 {
680 return cfs;
681 }
682}
683
684CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
685{
686 String policyName = null;
687 String basePolicyName = null;
688 String sourcePolicyFile = null;
689 String definition = null;
690 boolean ifNotExists = false;
691 Map<String,String> properties = null;
692 CreateFeedPolicyStatement cfps = null;
693}
694{
695 (
696 "ingestion" "policy" policyName = Identifier() ifNotExists = IfNotExists()
697 <FROM>
698 ("policy" basePolicyName = Identifier() properties = Configuration() ("definition" definition = StringLiteral())?
699 {
700 cfps = new CreateFeedPolicyStatement(policyName,
701 basePolicyName, properties, definition, ifNotExists);
702 }
703 | "path" sourcePolicyFile = Identifier() ("definition" definition = StringLiteral())?
704 {
705 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
706 }
707 )
708
709 )
710 {
711 return cfps;
712 }
713}
714
715
716
717List<VarIdentifier> ParameterList() throws ParseException:
718{
719 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
720 VarIdentifier var = null;
721}
722{
723 <LEFTPAREN> (<VARIABLE>
724 {
725 var = new VarIdentifier();
726 var.setValue(token.image);
727 paramList.add(var);
728 getCurrentScope().addNewVarSymbolToScope(var);
729 }
730 (<COMMA> <VARIABLE>
731 {
732 var = new VarIdentifier();
733 var.setValue(token.image);
734 paramList.add(var);
735 getCurrentScope().addNewVarSymbolToScope(var);
736 }
737 )*)? <RIGHTPAREN>
738 {
739 return paramList;
740 }
741}
742
743boolean IfNotExists() throws ParseException:
744{
745}
746{
747 ( "if not exists"
748 {
749 return true;
750 }
751 )?
752 {
753 return false;
754 }
755}
756
757FunctionSignature ApplyFunction() throws ParseException:
758{
759 FunctionName functioName = null;
760 FunctionSignature funcSig = null;
761}
762{
763 "apply" "function" functioName = FunctionName()
764 {
765 String fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
766 return new FunctionSignature(functioName.dataverse, fqFunctionName, 1);
767 }
768}
769
770String GetPolicy() throws ParseException:
771{
772 String policy = null;
773}
774{
775 "using" "policy" policy = Identifier()
776 {
777 return policy;
778 }
779
780}
781
782FunctionSignature FunctionSignature() throws ParseException:
783{
784 FunctionName fctName = null;
785 int arity = 0;
786}
787{
788 fctName = FunctionName() "@" <INTEGER_LITERAL>
789 {
790 arity = new Integer(token.image);
791 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
792 throw new ParseException(" invalid arity:" + arity);
793 }
794
795 // TODO use fctName.library
796 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
797 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
798 }
799}
800
801List<List<String>> PrimaryKey() throws ParseException:
802{
803 List<String> tmp = null;
804 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
805}
806{
807 "primary" "key" tmp = NestedField()
808 {
809 primaryKeyFields.add(tmp);
810 }
811 ( <COMMA> tmp = NestedField()
812 {
813 primaryKeyFields.add(tmp);
814 }
815 )*
816 {
817 return primaryKeyFields;
818 }
819}
820
821Statement DropStatement() throws ParseException:
822{
823 String id = null;
824 Pair<Identifier,Identifier> pairId = null;
825 Triple<Identifier,Identifier,Identifier> tripleId = null;
826 FunctionSignature funcSig = null;
827 boolean ifExists = false;
828 Statement stmt = null;
829}
830{
831 "drop"
832 (
833 <DATASET> pairId = QualifiedName() ifExists = IfExists()
834 {
835 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
836 }
837 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
838 {
839 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
840 }
841 | "nodegroup" id = Identifier() ifExists = IfExists()
842 {
843 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
844 }
845 | "type" pairId = TypeName() ifExists = IfExists()
846 {
847 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
848 }
849 | "dataverse" id = Identifier() ifExists = IfExists()
850 {
851 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
852 }
853 | "function" funcSig = FunctionSignature() ifExists = IfExists()
854 {
855 stmt = new FunctionDropStatement(funcSig, ifExists);
856 }
857 | "feed" pairId = QualifiedName() ifExists = IfExists()
858 {
859 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
860 }
861 )
862 {
863 return stmt;
864 }
865}
866
867boolean IfExists() throws ParseException :
868{
869}
870{
871 ( <IF> "exists"
872 {
873 return true;
874 }
875 )?
876 {
877 return false;
878 }
879}
880
881InsertStatement InsertStatement() throws ParseException:
882{
883 Pair<Identifier,Identifier> nameComponents = null;
884 Query query;
885}
886{
887 "insert" "into" <DATASET> nameComponents = QualifiedName() query = Query()
888 {
889 query.setTopLevel(false);
890 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
891 }
892}
893
894DeleteStatement DeleteStatement() throws ParseException:
895{
896 VariableExpr var = null;
897 Expression condition = null;
898 Pair<Identifier, Identifier> nameComponents;
899 // This is related to the new metadata lock management
900 setDataverses(new ArrayList<String>());
901 setDatasets(new ArrayList<String>());
902
903}
904{
905 "delete" var = Variable()
906 {
907 getCurrentScope().addNewVarSymbolToScope(var.getVar());
908 }
909 <FROM> <DATASET> nameComponents = QualifiedName()
910 (<WHERE> condition = Expression())?
911 {
912 // First we get the dataverses and datasets that we want to lock
913 List<String> dataverses = getDataverses();
914 List<String> datasets = getDatasets();
915 // we remove the pointer to the dataverses and datasets
916 setDataverses(null);
917 setDatasets(null);
918 return new DeleteStatement(var, nameComponents.first, nameComponents.second,
919 condition, getVarCounter(), dataverses, datasets);
920 }
921}
922
923UpdateStatement UpdateStatement() throws ParseException:
924{
925 VariableExpr vars;
926 Expression target;
927 Expression condition;
928 UpdateClause uc;
929 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
930}
931{
932 "update" vars = Variable() <IN> target = Expression()
933 <WHERE> condition = Expression()
934 <LEFTPAREN> (uc = UpdateClause()
935 {
936 ucs.add(uc);
937 }
938 (<COMMA> uc = UpdateClause()
939 {
940 ucs.add(uc);
941 }
942 )*) <RIGHTPAREN>
943 {
944 return new UpdateStatement(vars, target, condition, ucs);
945 }
946}
947
948UpdateClause UpdateClause() throws ParseException:
949{
950 Expression target = null;
951 Expression value = null ;
952 InsertStatement is = null;
953 DeleteStatement ds = null;
954 UpdateStatement us = null;
955 Expression condition = null;
956 UpdateClause ifbranch = null;
957 UpdateClause elsebranch = null;
958}
959{
960 ("set" target = Expression() <ASSIGN> value = Expression()
961 | is = InsertStatement()
962 | ds = DeleteStatement()
963 | us = UpdateStatement()
964 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
965 <THEN> ifbranch = UpdateClause()
966 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
967 {
968 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
969 }
970 )
971}
972
973Statement SetStatement() throws ParseException:
974{
975 String pn = null;
976 String pv = null;
977}
978{
979 "set" pn = Identifier() pv = StringLiteral()
980 {
981 return new SetStatement(pn, pv);
982 }
983}
984
985Statement WriteStatement() throws ParseException:
986{
987 String nodeName = null;
988 String fileName = null;
989 Query query;
990 String writerClass = null;
991 Pair<Identifier,Identifier> nameComponents = null;
992}
993{
994 "write" "output" "to" nodeName = Identifier() <COLON> fileName = StringLiteral()
995 ( "using" writerClass = StringLiteral() )?
996 {
997 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
998 }
999}
1000
1001LoadStatement LoadStatement() throws ParseException:
1002{
1003 Identifier dataverseName = null;
1004 Identifier datasetName = null;
1005 boolean alreadySorted = false;
1006 String adapterName;
1007 Map<String,String> properties;
1008 Pair<Identifier,Identifier> nameComponents = null;
1009}
1010{
1011 "load" <DATASET> nameComponents = QualifiedName()
1012 {
1013 dataverseName = nameComponents.first;
1014 datasetName = nameComponents.second;
1015 }
1016 "using" adapterName = AdapterName() properties = Configuration()
1017 ("pre-sorted"
1018 {
1019 alreadySorted = true;
1020 }
1021 )?
1022 {
1023 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1024 }
1025}
1026
1027
1028String AdapterName() throws ParseException :
1029{
1030 String adapterName = null;
1031}
1032{
1033 adapterName = Identifier()
1034 {
1035 return adapterName;
1036 }
1037}
1038
1039Statement CompactStatement() throws ParseException:
1040{
1041 Pair<Identifier,Identifier> nameComponents = null;
1042 Statement stmt = null;
1043}
1044{
1045 "compact" <DATASET> nameComponents = QualifiedName()
1046 {
1047 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1048 }
1049 {
1050 return stmt;
1051 }
1052}
1053
1054Statement FeedStatement() throws ParseException:
1055{
1056 Pair<Identifier,Identifier> feedNameComponents = null;
1057 Pair<Identifier,Identifier> datasetNameComponents = null;
1058
1059 Map<String,String> configuration = null;
1060 Statement stmt = null;
1061 String policy = null;
1062}
1063{
1064 (
1065 "connect" "feed" feedNameComponents = QualifiedName() "to" <DATASET> datasetNameComponents = QualifiedName() (policy = GetPolicy())?
1066 {
1067 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, policy, getVarCounter());
1068 }
1069 | "disconnect" "feed" feedNameComponents = QualifiedName() <FROM> <DATASET> datasetNameComponents = QualifiedName()
1070 {
1071 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1072 }
1073 )
1074 {
1075 return stmt;
1076 }
1077}
1078
1079Map<String,String> Configuration() throws ParseException :
1080{
1081 Map<String,String> configuration = new LinkedHashMap<String,String>();
1082 Pair<String, String> keyValuePair = null;
1083}
1084{
1085 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1086 {
1087 configuration.put(keyValuePair.first, keyValuePair.second);
1088 }
1089 ( <COMMA> keyValuePair = KeyValuePair()
1090 {
1091 configuration.put(keyValuePair.first, keyValuePair.second);
1092 }
1093 )* )? <RIGHTPAREN>
1094 {
1095 return configuration;
1096 }
1097}
1098
1099Pair<String, String> KeyValuePair() throws ParseException:
1100{
1101 String key;
1102 String value;
1103}
1104{
1105 <LEFTPAREN> key = StringLiteral() <EQ> value = StringLiteral() <RIGHTPAREN>
1106 {
1107 return new Pair<String, String>(key, value);
1108 }
1109}
1110
1111Map<String,String> Properties() throws ParseException:
1112{
1113 Map<String,String> properties = new HashMap<String,String>();
1114 Pair<String, String> property;
1115}
1116{
1117 ( <LEFTPAREN> property = Property()
1118 {
1119 properties.put(property.first, property.second);
1120 }
1121 ( <COMMA> property = Property()
1122 {
1123 properties.put(property.first, property.second);
1124 }
1125 )* <RIGHTPAREN> )?
1126 {
1127 return properties;
1128 }
1129}
1130
1131Pair<String, String> Property() throws ParseException:
1132{
1133 String key;
1134 String value;
1135}
1136{
1137 key = Identifier() <EQ> ( value = StringLiteral() | <INTEGER_LITERAL>
1138 {
1139 try {
1140 value = "" + Long.valueOf(token.image);
1141 } catch (NumberFormatException nfe) {
1142 throw new ParseException("inapproriate value: " + token.image);
1143 }
1144 }
1145 )
1146 {
1147 return new Pair<String, String>(key.toUpperCase(), value);
1148 }
1149}
1150
1151TypeExpression IndexedTypeExpr() throws ParseException:
1152{
1153 TypeExpression typeExpr = null;
1154}
1155{
1156 (
1157 typeExpr = TypeReference()
1158 | typeExpr = OrderedListTypeDef()
1159 | typeExpr = UnorderedListTypeDef()
1160 )
1161 {
1162 return typeExpr;
1163 }
1164}
1165
1166TypeExpression TypeExpr() throws ParseException:
1167{
1168 TypeExpression typeExpr = null;
1169}
1170{
1171 (
1172 typeExpr = RecordTypeDef()
1173 | typeExpr = TypeReference()
1174 | typeExpr = OrderedListTypeDef()
1175 | typeExpr = UnorderedListTypeDef()
1176 )
1177 {
1178 return typeExpr;
1179 }
1180}
1181
1182RecordTypeDefinition RecordTypeDef() throws ParseException:
1183{
1184 RecordTypeDefinition recType = new RecordTypeDefinition();
1185 RecordTypeDefinition.RecordKind recordKind = null;
1186}
1187{
1188 ( "closed" { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1189 | "open" { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1190 <LEFTBRACE>
1191 {
1192 String hint = getHint(token);
1193 if (hint != null) {
1194 String splits[] = hint.split(" +");
1195 if (splits[0].equals(GEN_FIELDS_HINT)) {
1196 if (splits.length != 5) {
1197 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1198 }
1199 if (!splits[1].equals("int")) {
1200 throw new ParseException("The only supported type for gen-fields is int.");
1201 }
1202 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1203 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1204 recType.setUndeclaredFieldsDataGen(ufdg);
1205 }
1206 }
1207
1208 }
1209 (
1210 RecordField(recType)
1211 ( <COMMA> RecordField(recType) )*
1212 )?
1213 <RIGHTBRACE>
1214 {
1215 if (recordKind == null) {
1216 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1217 }
1218 recType.setRecordKind(recordKind);
1219 return recType;
1220 }
1221}
1222
1223void RecordField(RecordTypeDefinition recType) throws ParseException:
1224{
1225 String fieldName;
1226 TypeExpression type = null;
1227 boolean nullable = false;
1228}
1229{
1230 fieldName = Identifier()
1231 {
1232 String hint = getHint(token);
1233 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1234 }
1235 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1236 {
1237 recType.addField(fieldName, type, nullable, rfdg);
1238 }
1239}
1240
1241TypeReferenceExpression TypeReference() throws ParseException:
1242{
1243 String id = null;
1244}
1245{
1246 id = Identifier()
1247 {
1248 if (id.equalsIgnoreCase("int")) {
1249 id = "int64";
1250 }
1251
1252 return new TypeReferenceExpression(new Identifier(id));
1253 }
1254}
1255
1256OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1257{
1258 TypeExpression type = null;
1259}
1260{
1261 <LEFTBRACKET>
1262 ( type = TypeExpr() )
1263 <RIGHTBRACKET>
1264 {
1265 return new OrderedListTypeDefinition(type);
1266 }
1267}
1268
1269
1270UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1271{
1272 TypeExpression type = null;
1273}
1274{
1275 <LEFTDBLBRACE>
1276 ( type = TypeExpr() )
1277 <RIGHTDBLBRACE>
1278 {
1279 return new UnorderedListTypeDefinition(type);
1280 }
1281}
1282
1283FunctionName FunctionName() throws ParseException:
1284{
1285 String first = null;
1286 String second = null;
1287 String third = null;
1288 boolean secondAfterDot = false;
1289}
1290{
1291 first = Identifier()
1292 {
1293 FunctionName result = new FunctionName();
1294 result.hint = getHint(token);
1295 }
1296 ( <DOT> second = Identifier()
1297 {
1298 secondAfterDot = true;
1299 }
1300 ("#" third = Identifier())? | "#" second = Identifier() )?
1301 {
1302 if (second == null) {
1303 result.dataverse = defaultDataverse;
1304 result.library = null;
1305 result.function = first;
1306 } else if (third == null) {
1307 if (secondAfterDot) {
1308 result.dataverse = first;
1309 result.library = null;
1310 result.function = second;
1311 } else {
1312 result.dataverse = defaultDataverse;
1313 result.library = first;
1314 result.function = second;
1315 }
1316 } else {
1317 result.dataverse = first;
1318 result.library = second;
1319 result.function = third;
1320 }
1321
1322 if (result.function.equalsIgnoreCase("int")) {
1323 result.function = "int64";
1324 }
1325 return result;
1326 }
1327}
1328
1329
1330Pair<Identifier,Identifier> TypeName() throws ParseException:
1331{
1332 Pair<Identifier,Identifier> name = null;
1333}
1334{
1335 name = QualifiedName()
1336 {
1337 if (name.first == null) {
1338 name.first = new Identifier(defaultDataverse);
1339 }
1340 return name;
1341 }
1342}
1343
1344String Identifier() throws ParseException:
1345{
1346 String lit = null;
1347}
1348{
1349 (<IDENTIFIER>
1350 {
1351 return token.image;
1352 }
1353 | lit = StringLiteral()
1354 {
1355 return lit;
1356 }
1357 )
1358}
1359
1360Pair<List<String>, TypeExpression> OpenField() throws ParseException:
1361{
1362 TypeExpression fieldType = null;
1363 List<String> fieldList = null;
1364}
1365{
1366 fieldList = NestedField()
1367 ( <COLON> fieldType = IndexedTypeExpr() )?
1368 {
1369 return new Pair<List<String>, TypeExpression>(fieldList, fieldType);
1370 }
1371}
1372
1373List<String> NestedField() throws ParseException:
1374{
1375 List<String> exprList = new ArrayList<String>();
1376 String lit = null;
1377}
1378{
1379 lit = Identifier()
1380 {
1381 exprList.add(lit);
1382 }
1383 (<DOT>
1384 lit = Identifier()
1385 {
1386 exprList.add(lit);
1387 }
1388 )*
1389 {
1390 return exprList;
1391 }
1392}
1393
1394
1395
1396String StringLiteral() throws ParseException:
1397{
1398}
1399{
1400 <STRING_LITERAL>
1401 {
1402 return removeQuotesAndEscapes(token.image);
1403 }
1404}
1405
1406Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1407{
1408 String first = null;
1409 String second = null;
1410}
1411{
1412 first = Identifier() (<DOT> second = Identifier())?
1413 {
1414 Identifier id1 = null;
1415 Identifier id2 = null;
1416 if (second == null) {
1417 id2 = new Identifier(first);
1418 } else
1419 {
1420 id1 = new Identifier(first);
1421 id2 = new Identifier(second);
1422 }
1423 return new Pair<Identifier,Identifier>(id1, id2);
1424 }
1425}
1426
1427Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1428{
1429 String first = null;
1430 String second = null;
1431 String third = null;
1432}
1433{
1434 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1435 {
1436 Identifier id1 = null;
1437 Identifier id2 = null;
1438 Identifier id3 = null;
1439 if (third == null) {
1440 id2 = new Identifier(first);
1441 id3 = new Identifier(second);
1442 } else {
1443 id1 = new Identifier(first);
1444 id2 = new Identifier(second);
1445 id3 = new Identifier(third);
1446 }
1447 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1448 }
1449}
1450
1451FunctionDecl FunctionDeclaration() throws ParseException:
1452{
1453 FunctionDecl funcDecl;
1454 FunctionSignature signature;
1455 String functionName;
1456 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1457 Expression funcBody;
1458 createNewScope();
1459}
1460{
1461 "declare" "function" functionName = Identifier()
1462 paramList = ParameterList()
1463 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1464 {
1465 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1466 getCurrentScope().addFunctionDescriptor(signature, false);
1467 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1468 removeCurrentScope();
1469 return funcDecl;
1470 }
1471}
1472
1473
1474Query Query() throws ParseException:
1475{
1476 Query query = new Query();
1477 // we set the pointers to the dataverses and datasets lists to fill them with entities to be locked
1478 setDataverses(query.getDataverses());
1479 setDatasets(query.getDatasets());
1480 Expression expr;
1481}
1482{
1483 expr = Expression()
1484 {
1485 query.setBody(expr);
1486 query.setVarCounter(getVarCounter());
1487 // we remove the pointers to the locked entities before we return the query object
1488 setDataverses(null);
1489 setDatasets(null);
1490 return query;
1491 }
1492
1493}
1494
1495
1496
1497Expression Expression():
1498{
1499 Expression expr = null;
1500 Expression exprP = null;
1501}
1502{
1503(
1504
1505//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1506 expr = OperatorExpr()
1507 | expr = IfThenElse()
1508 | expr = FLWOGR()
1509 | expr = QuantifiedExpression()
1510
1511
1512)
1513 {
1514 return (exprP==null) ? expr : exprP;
1515 }
1516}
1517
1518
1519
1520Expression OperatorExpr()throws ParseException:
1521{
1522 OperatorExpr op = null;
1523 Expression operand = null;
1524}
1525{
1526 operand = AndExpr()
1527 (
1528
1529 <OR>
1530 {
1531 if (op == null) {
1532 op = new OperatorExpr();
1533 op.addOperand(operand);
1534 op.setCurrentop(true);
1535 }
1536 op.addOperator(token.image);
1537 }
1538
1539 operand = AndExpr()
1540 {
1541 op.addOperand(operand);
1542 }
1543
1544 )*
1545
1546 {
1547 return op==null? operand: op;
1548 }
1549}
1550
1551Expression AndExpr()throws ParseException:
1552{
1553 OperatorExpr op = null;
1554 Expression operand = null;
1555}
1556{
1557 operand = RelExpr()
1558 (
1559
1560 <AND>
1561 {
1562 if (op == null) {
1563 op = new OperatorExpr();
1564 op.addOperand(operand);
1565 op.setCurrentop(true);
1566 }
1567 op.addOperator(token.image);
1568 }
1569
1570 operand = RelExpr()
1571 {
1572 op.addOperand(operand);
1573 }
1574
1575 )*
1576
1577 {
1578 return op==null? operand: op;
1579 }
1580}
1581
1582
1583
1584Expression RelExpr()throws ParseException:
1585{
1586 OperatorExpr op = null;
1587 Expression operand = null;
1588 boolean broadcast = false;
1589 IExpressionAnnotation annotation = null;
1590}
1591{
1592 operand = AddExpr()
1593 {
1594 if (operand instanceof VariableExpr) {
1595 String hint = getHint(token);
1596 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1597 broadcast = true;
1598 }
1599 }
1600 }
1601
1602 (
1603 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> |<SIMILAR>)
1604 {
1605 String mhint = getHint(token);
1606 if (mhint != null) {
1607 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1608 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1609 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1610 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1611 }
1612 }
1613 if (op == null) {
1614 op = new OperatorExpr();
1615 op.addOperand(operand, broadcast);
1616 op.setCurrentop(true);
1617 broadcast = false;
1618 }
1619 op.addOperator(token.image);
1620 }
1621
1622 operand = AddExpr()
1623 {
1624 broadcast = false;
1625 if (operand instanceof VariableExpr) {
1626 String hint = getHint(token);
1627 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1628 broadcast = true;
1629 }
1630 }
1631 op.addOperand(operand, broadcast);
1632 }
1633 )?
1634
1635 {
1636 if (annotation != null) {
1637 op.addHint(annotation);
1638 }
1639 return op==null? operand: op;
1640 }
1641}
1642
1643Expression AddExpr()throws ParseException:
1644{
1645 OperatorExpr op = null;
1646 Expression operand = null;
1647}
1648{
1649 operand = MultExpr()
1650
1651 ( (<PLUS> | <MINUS>)
1652 {
1653 if (op == null) {
1654 op = new OperatorExpr();
1655 op.addOperand(operand);
1656 op.setCurrentop(true);
1657 }
1658 ((OperatorExpr)op).addOperator(token.image);
1659 }
1660
1661 operand = MultExpr()
1662 {
1663 op.addOperand(operand);
1664 }
1665 )*
1666
1667 {
1668 return op==null? operand: op;
1669 }
1670}
1671
1672Expression MultExpr()throws ParseException:
1673{
1674 OperatorExpr op = null;
1675 Expression operand = null;
1676}
1677{
1678 operand = UnionExpr()
1679
1680 (( <MUL> | <DIV> | <MOD> | <CARET> | <IDIV>)
1681 {
1682 if (op == null) {
1683 op = new OperatorExpr();
1684 op.addOperand(operand);
1685 op.setCurrentop(true);
1686 }
1687 op.addOperator(token.image);
1688 }
1689 operand = UnionExpr()
1690 {
1691 op.addOperand(operand);
1692 }
1693 )*
1694
1695 {
1696 return op==null?operand:op;
1697 }
1698}
1699
1700Expression UnionExpr() throws ParseException:
1701{
1702 UnionExpr union = null;
1703 Expression operand1 = null;
1704 Expression operand2 = null;
1705}
1706{
1707 operand1 = UnaryExpr()
1708 (<UNION>
1709 (operand2 = UnaryExpr()) {
1710 if (union == null) {
1711 union = new UnionExpr();
1712 union.addExpr(operand1);
1713 }
1714 union.addExpr(operand2);
1715 } )*
1716 {
1717 return (union == null)? operand1: union;
1718 }
1719}
1720
1721Expression UnaryExpr() throws ParseException:
1722{
1723 Expression uexpr = null;
1724 Expression expr = null;
1725}
1726{
1727 ( (<PLUS> | <MINUS>)
1728 {
1729 uexpr = new UnaryExpr();
1730 if("+".equals(token.image))
1731 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1732 else if("-".equals(token.image))
1733 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1734 else
1735 throw new ParseException();
1736 }
1737 )?
1738
1739 expr = ValueExpr()
1740 {
1741 if(uexpr!=null){
1742 ((UnaryExpr)uexpr).setExpr(expr);
1743 return uexpr;
1744 }
1745 else{
1746 return expr;
1747 }
1748 }
1749}
1750
1751Expression ValueExpr()throws ParseException:
1752{
1753 Expression expr = null;
1754 Identifier ident = null;
1755 AbstractAccessor fa = null;
1756 Expression indexExpr = null;
1757}
1758{
1759 expr = PrimaryExpr() ( ident = Field()
1760 {
1761 fa = (fa == null ? new FieldAccessor(expr, ident)
1762 : new FieldAccessor(fa, ident));
1763 }
1764 | indexExpr = Index()
1765 {
1766 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
1767 : new IndexAccessor(fa, indexExpr));
1768 }
1769 )*
1770 {
1771 return fa == null ? expr : fa;
1772 }
1773}
1774
1775Identifier Field() throws ParseException:
1776{
1777 String ident = null;
1778}
1779{
1780 <DOT> ident = Identifier()
1781 {
1782 return new Identifier(ident);
1783 }
1784}
1785
1786Expression Index() throws ParseException:
1787{
1788 Expression expr = null;
1789}
1790{
1791 <LEFTBRACKET> ( expr = Expression()
1792 {
1793 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1794 {
1795 Literal lit = ((LiteralExpr)expr).getValue();
1796 if(lit.getLiteralType() != Literal.Type.INTEGER &&
1797 lit.getLiteralType() != Literal.Type.LONG) {
1798 throw new ParseException("Index should be an INTEGER");
1799 }
1800 }
1801 }
1802
1803 | <QUES> // ANY
1804
1805 )
1806
1807 <RIGHTBRACKET>
1808 {
1809 return expr;
1810 }
1811}
1812
1813
1814Expression PrimaryExpr()throws ParseException:
1815{
1816 Expression expr = null;
1817}
1818{
1819 ( LOOKAHEAD(2)
1820 expr = FunctionCallExpr()
1821 | expr = Literal()
1822 | expr = DatasetAccessExpression()
1823 | expr = VariableRef()
1824 {
1825 if(((VariableExpr)expr).getIsNewVar() == true)
1826 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1827 }
1828 | expr = ListConstructor()
1829 | expr = RecordConstructor()
1830 | expr = ParenthesizedExpression()
1831 )
1832 {
1833 return expr;
1834 }
1835}
1836
1837Expression Literal() throws ParseException:
1838{
1839 LiteralExpr lit = new LiteralExpr();
1840 String str = null;
1841}
1842{
1843 ( str = StringLiteral()
1844 {
1845 lit.setValue(new StringLiteral(str));
1846 }
1847 | <INTEGER_LITERAL>
1848 {
1849 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1850 }
1851 | <FLOAT_LITERAL>
1852 {
1853 lit.setValue(new FloatLiteral(new Float(token.image)));
1854 }
1855 | <DOUBLE_LITERAL>
1856 {
1857 lit.setValue(new DoubleLiteral(new Double(token.image)));
1858 }
1859 | <NULL>
1860 {
1861 lit.setValue(NullLiteral.INSTANCE);
1862 }
1863 | <TRUE>
1864 {
1865 lit.setValue(TrueLiteral.INSTANCE);
1866 }
1867 | <FALSE>
1868 {
1869 lit.setValue(FalseLiteral.INSTANCE);
1870 }
1871 )
1872 {
1873 return lit;
1874 }
1875}
1876
1877
1878VariableExpr VariableRef() throws ParseException:
1879{
1880 VariableExpr varExp = new VariableExpr();
1881 VarIdentifier var = new VarIdentifier();
1882}
1883{
1884 <VARIABLE>
1885 {
1886 String varName = token.image;
1887 Identifier ident = lookupSymbol(varName);
1888 if (isInForbiddenScopes(varName)) {
1889 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.");
1890 }
1891 if(ident != null) { // exist such ident
1892 varExp.setIsNewVar(false);
1893 varExp.setVar((VarIdentifier)ident);
1894 } else {
1895 varExp.setVar(var);
1896 }
1897 var.setValue(varName);
1898 return varExp;
1899 }
1900}
1901
1902
1903VariableExpr Variable() throws ParseException:
1904{
1905 VariableExpr varExp = new VariableExpr();
1906 VarIdentifier var = new VarIdentifier();
1907}
1908{
1909 <VARIABLE>
1910 {
1911 Identifier ident = lookupSymbol(token.image);
1912 if(ident != null) { // exist such ident
1913 varExp.setIsNewVar(false);
1914 }
1915 varExp.setVar(var);
1916 var.setValue(token.image);
1917 return varExp;
1918 }
1919}
1920
1921Expression ListConstructor() throws ParseException:
1922{
1923 Expression expr = null;
1924}
1925{
1926 (
1927 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1928 )
1929
1930 {
1931 return expr;
1932 }
1933}
1934
1935
1936ListConstructor OrderedListConstructor() throws ParseException:
1937{
1938 ListConstructor expr = new ListConstructor();
1939 List<Expression> exprList = null;
1940 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1941}
1942{
1943 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
1944 {
1945 expr.setExprList(exprList);
1946 return expr;
1947 }
1948}
1949
1950ListConstructor UnorderedListConstructor() throws ParseException:
1951{
1952 ListConstructor expr = new ListConstructor();
1953 List<Expression> exprList = null;
1954 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1955}
1956{
1957 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
1958 {
1959 expr.setExprList(exprList);
1960 return expr;
1961 }
1962}
1963
1964List<Expression> ExpressionList() throws ParseException:
1965{
1966 Expression expr = null;
1967 List<Expression> list = null;
1968 List<Expression> exprList = new ArrayList<Expression>();
1969}
1970{
1971 (
1972 expr = Expression() { exprList.add(expr); }
1973 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
1974 )?
1975 (LOOKAHEAD(1) Comma())?
1976 {
1977 return exprList;
1978 }
1979}
1980
1981void Comma():
1982{}
1983{
1984 <COMMA>
1985}
1986
1987RecordConstructor RecordConstructor() throws ParseException:
1988{
1989 RecordConstructor expr = new RecordConstructor();
1990 FieldBinding tmp = null;
1991 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
1992}
1993{
1994 <LEFTBRACE> (tmp = FieldBinding()
1995 {
1996 fbList.add(tmp);
1997 }
1998 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
1999 {
2000 expr.setFbList(fbList);
2001 return expr;
2002 }
2003}
2004
2005FieldBinding FieldBinding() throws ParseException:
2006{
2007 FieldBinding fb = new FieldBinding();
2008 Expression left, right;
2009}
2010{
2011 left = Expression() <COLON> right = Expression()
2012 {
2013 fb.setLeftExpr(left);
2014 fb.setRightExpr(right);
2015 return fb;
2016 }
2017}
2018
2019
2020Expression FunctionCallExpr() throws ParseException:
2021{
2022 CallExpr callExpr;
2023 List<Expression> argList = new ArrayList<Expression>();
2024 Expression tmp;
2025 int arity = 0;
2026 FunctionName funcName = null;
2027 String hint = null;
2028}
2029{
2030 funcName = FunctionName()
2031 {
2032 hint = funcName.hint;
2033 }
2034 <LEFTPAREN> (tmp = Expression()
2035 {
2036 argList.add(tmp);
2037 arity ++;
2038 }
2039 (<COMMA> tmp = Expression()
2040 {
2041 argList.add(tmp);
2042 arity++;
2043 }
2044 )*)? <RIGHTPAREN>
2045 {
2046 // TODO use funcName.library
2047 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2048 FunctionSignature signature
2049 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2050 if (signature == null) {
2051 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2052 }
2053 callExpr = new CallExpr(signature,argList);
2054 if (hint != null) {
2055 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2056 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2057 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2058 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2059 }
2060 }
2061 return callExpr;
2062 }
2063}
2064
2065
2066Expression DatasetAccessExpression() throws ParseException:
2067{
2068 String funcName;
2069 String arg1 = null;
2070 String arg2 = null;
2071 Expression nameArg;
2072}
2073{
2074 <DATASET>
2075 {
2076 funcName = token.image;
2077 }
2078 ( ( arg1 = Identifier() ( <DOT> arg2 = Identifier() )? )
2079 {
2080 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
2081 LiteralExpr ds = new LiteralExpr();
2082 ds.setValue( new StringLiteral(name) );
2083 nameArg = ds;
2084 if(arg2 != null){
2085 addDataverse(arg1.toString());
2086 addDataset(name);
2087 } else {
2088 addDataset(defaultDataverse + "." + name);
2089 }
2090 }
2091 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
2092 {
2093 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
2094 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
2095 if (signature == null) {
2096 signature = new FunctionSignature(dataverse, funcName, 1);
2097 }
2098 List<Expression> argList = new ArrayList<Expression>();
2099 argList.add(nameArg);
2100 return new CallExpr(signature, argList);
2101 }
2102}
2103
2104Expression ParenthesizedExpression() throws ParseException:
2105{
2106 Expression expr;
2107}
2108{
2109 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2110 {
2111 return expr;
2112 }
2113}
2114
2115Expression IfThenElse() throws ParseException:
2116{
2117 Expression condExpr;
2118 Expression thenExpr;
2119 Expression elseExpr;
2120 IfExpr ifExpr = new IfExpr();
2121}
2122{
2123 <IF> <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> <THEN> thenExpr = Expression() <ELSE> elseExpr = Expression()
2124
2125 {
2126 ifExpr.setCondExpr(condExpr);
2127 ifExpr.setThenExpr(thenExpr);
2128 ifExpr.setElseExpr(elseExpr);
2129 return ifExpr;
2130 }
2131}
2132
2133Expression FLWOGR() throws ParseException:
2134{
2135 FLWOGRExpression flworg = new FLWOGRExpression();
2136 List<Clause> clauseList = new ArrayList<Clause>();
2137 Expression returnExpr;
2138 Clause tmp;
2139 createNewScope();
2140}
2141{
2142 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2143 (tmp = Clause() {clauseList.add(tmp);})* (<RETURN>|<SELECT>) returnExpr = Expression()
2144
2145 {
2146 flworg.setClauseList(clauseList);
2147 flworg.setReturnExpr(returnExpr);
2148 removeCurrentScope();
2149 return flworg;
2150 }
2151}
2152
2153Clause Clause()throws ParseException :
2154{
2155 Clause clause;
2156}
2157{
2158 (
2159 clause = ForClause()
2160 | clause = LetClause()
2161 | clause = WhereClause()
2162 | clause = OrderbyClause()
2163 | clause = GroupClause()
2164 | clause = LimitClause()
2165 | clause = DistinctClause()
2166 )
2167 {
2168 return clause;
2169 }
2170}
2171
2172Clause ForClause()throws ParseException :
2173{
2174 ForClause fc = new ForClause();
2175 VariableExpr varExp;
2176 VariableExpr varPos = null;
2177 Expression inExp;
2178 extendCurrentScope();
2179}
2180{
2181 (<FOR>|<FROM>) varExp = Variable() (<AT> varPos = Variable())? <IN> ( inExp = Expression() )
2182 {
2183 fc.setVarExpr(varExp);
2184 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2185 fc.setInExpr(inExp);
2186 if (varPos != null) {
2187 fc.setPosExpr(varPos);
2188 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2189 }
2190 return fc;
2191 }
2192}
2193
2194Clause LetClause() throws ParseException:
2195{
2196 LetClause lc = new LetClause();
2197 VariableExpr varExp;
2198 Expression beExp;
2199 extendCurrentScope();
2200}
2201{
2202 (<LET>|<WITH>) varExp = Variable() <ASSIGN> beExp = Expression()
2203 {
2204 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2205 lc.setVarExpr(varExp);
2206 lc.setBindingExpr(beExp);
2207 return lc;
2208 }
2209}
2210
2211Clause WhereClause()throws ParseException :
2212{
2213 WhereClause wc = new WhereClause();
2214 Expression whereExpr;
2215}
2216{
2217 <WHERE> whereExpr = Expression()
2218 {
2219 wc.setWhereExpr(whereExpr);
2220 return wc;
2221 }
2222}
2223
2224Clause OrderbyClause()throws ParseException :
2225{
2226 OrderbyClause oc = new OrderbyClause();
2227 Expression orderbyExpr;
2228 List<Expression> orderbyList = new ArrayList<Expression>();
2229 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2230 int numOfOrderby = 0;
2231}
2232{
2233 (
2234 <ORDER>
2235 {
2236 String hint = getHint(token);
2237 if (hint != null) {
2238 if (hint.startsWith(INMEMORY_HINT)) {
2239 String splits[] = hint.split(" +");
2240 int numFrames = Integer.parseInt(splits[1]);
2241 int numTuples = Integer.parseInt(splits[2]);
2242 oc.setNumFrames(numFrames);
2243 oc.setNumTuples(numTuples);
2244 }
2245 if (hint.startsWith(RANGE_HINT)) {
2246 try{
2247 oc.setRangeMap(RangeMapBuilder.parseHint(hint.substring(RANGE_HINT.length())));
2248 } catch (AsterixException e) {
2249 throw new ParseException(e.getMessage());
2250 }
2251 }
2252 }
2253 }
2254 <BY> orderbyExpr = Expression()
2255 {
2256 orderbyList.add(orderbyExpr);
2257 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2258 }
2259 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2260 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2261 {
2262 modifierList.add(modif);
2263 }
2264
2265 (<COMMA> orderbyExpr = Expression()
2266 {
2267 orderbyList.add(orderbyExpr);
2268 modif = OrderbyClause.OrderModifier.ASC;
2269 }
2270 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2271 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2272 {
2273 modifierList.add(modif);
2274 }
2275 )*
2276)
2277 {
2278 oc.setModifierList(modifierList);
2279 oc.setOrderbyList(orderbyList);
2280 return oc;
2281 }
2282}
2283Clause GroupClause()throws ParseException :
2284{
2285 GroupbyClause gbc = new GroupbyClause();
2286 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2287 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2288 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
2289 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
2290 VariableExpr var = null;
2291 VariableExpr withVar = null;
2292 Expression expr = null;
2293 VariableExpr decorVar = null;
2294 Expression decorExpr = null;
2295}
2296{
2297 {
2298 Scope newScope = extendCurrentScopeNoPush(true);
2299 // extendCurrentScope(true);
2300 }
2301 <GROUP>
2302 {
2303 String hint = getHint(token);
2304 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2305 gbc.setHashGroupByHint(true);
2306 }
2307 }
2308 <BY> (LOOKAHEAD(2) var = Variable()
2309 {
2310 newScope.addNewVarSymbolToScope(var.getVar());
2311 } <ASSIGN>)?
2312 expr = Expression()
2313 {
2314 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2315 vePairList.add(pair1);
2316 }
2317 (<COMMA> ( LOOKAHEAD(2) var = Variable()
2318 {
2319 newScope.addNewVarSymbolToScope(var.getVar());
2320 } <ASSIGN>)?
2321 expr = Expression()
2322 {
2323 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2324 vePairList.add(pair2);
2325 }
2326 )*
2327 (<DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2328 {
2329 newScope.addNewVarSymbolToScope(decorVar.getVar());
2330 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2331 decorPairList.add(pair3);
2332 }
2333 (<COMMA> <DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2334 {
2335 newScope.addNewVarSymbolToScope(decorVar.getVar());
2336 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2337 decorPairList.add(pair4);
2338 }
2339 )*
2340 )?
2341 (<WITH>|<KEEPING>) withVar = VariableRef()
2342 {
2343 if(withVar.getIsNewVar()==true)
2344 throw new ParseException("can't find variable " + withVar.getVar());
2345 withVarList.add(withVar);
2346 newScope.addNewVarSymbolToScope(withVar.getVar());
2347 }
2348 (<COMMA> withVar = VariableRef()
2349 {
2350 if(withVar.getIsNewVar()==true)
2351 throw new ParseException("can't find variable " + withVar.getVar());
2352 withVarList.add(withVar);
2353 newScope.addNewVarSymbolToScope(withVar.getVar());
2354 })*
2355 {
2356 gbc.setGbyPairList(vePairList);
2357 gbc.setDecorPairList(decorPairList);
2358 gbc.setWithVarList(withVarList);
2359 replaceCurrentScope(newScope);
2360 return gbc;
2361 }
2362}
2363
2364
2365LimitClause LimitClause() throws ParseException:
2366{
2367 LimitClause lc = new LimitClause();
2368 Expression expr;
2369 pushForbiddenScope(getCurrentScope());
2370}
2371{
2372 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
2373 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
2374
2375 {
2376 popForbiddenScope();
2377 return lc;
2378 }
2379}
2380
2381DistinctClause DistinctClause() throws ParseException:
2382{
2383 List<Expression> exprs = new ArrayList<Expression>();
2384 Expression expr;
2385}
2386{
2387 <DISTINCT> <BY> expr = Expression()
2388 {
2389 exprs.add(expr);
2390 }
2391 (<COMMA> expr = Expression()
2392 {
2393 exprs.add(expr);
2394 }
2395 )*
2396 {
2397 return new DistinctClause(exprs);
2398 }
2399}
2400
2401QuantifiedExpression QuantifiedExpression()throws ParseException:
2402{
2403 QuantifiedExpression qc = new QuantifiedExpression();
2404 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2405 Expression satisfiesExpr;
2406 VariableExpr var;
2407 Expression inExpr;
2408 QuantifiedPair pair;
2409}
2410{
2411 {
2412 createNewScope();
2413 }
2414
2415 ( (<SOME> { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2416 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2417 var = Variable() <IN> inExpr = Expression()
2418 {
2419 pair = new QuantifiedPair(var, inExpr);
2420 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2421 quantifiedList.add(pair);
2422 }
2423 (
2424 <COMMA> var = Variable() <IN> inExpr = Expression()
2425 {
2426 pair = new QuantifiedPair(var, inExpr);
2427 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2428 quantifiedList.add(pair);
2429 }
2430 )*
2431 <SATISFIES> satisfiesExpr = Expression()
2432 {
2433 qc.setSatisfiesExpr(satisfiesExpr);
2434 qc.setQuantifiedList(quantifiedList);
2435 removeCurrentScope();
2436 return qc;
2437 }
2438}
2439
2440TOKEN_MGR_DECLS:
2441{
2442 public int commentDepth = 0;
2443 public IntStack lexerStateStack = new IntStack();
2444
2445 public void pushState() {
2446 lexerStateStack.push( curLexState );
2447 }
2448
2449 public void popState(String token) {
2450 if (lexerStateStack.size() > 0) {
2451 SwitchTo( lexerStateStack.pop() );
2452 } else {
2453 int errorLine = input_stream.getEndLine();
2454 int errorColumn = input_stream.getEndColumn();
2455 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
2456 + "\" but state stack is empty.";
2457 throw new TokenMgrError(msg, -1);
2458 }
2459 }
2460}
2461
2462<DEFAULT,IN_DBL_BRACE>
2463TOKEN :
2464{
2465 <ASC : "asc">
2466 | <AT : "at">
2467 | <BY : "by">
2468 | <DATASET : "dataset">
2469 | <DECOR : "decor">
2470 | <DESC : "desc">
2471 | <DISTINCT : "distinct">
2472 | <ELSE : "else">
2473 | <EVERY : "every">
2474 | <FOR : "for">
2475 | <FROM : "from">
2476 | <GROUP : "group">
2477 | <IF : "if">
2478 | <IN : "in">
2479 | <LET : "let">
2480 | <LIMIT : "limit">
2481 | <OFFSET : "offset">
2482 | <ORDER : "order">
2483 | <RETURN : "return">
2484 | <SATISFIES : "satisfies">
2485 | <SELECT : "select">
2486 | <SOME : "some">
2487 | <THEN : "then">
2488 | <UNION : "union">
2489 | <WHERE : "where">
2490 | <WITH : "with">
2491 | <KEEPING : "keeping">
2492}
2493
2494<DEFAULT,IN_DBL_BRACE>
2495TOKEN :
2496{
2497 <CARET : "^">
2498 | <DIV : "/">
2499 | <IDIV : "idiv">
2500 | <MINUS : "-">
2501 | <MOD : "%">
2502 | <MUL : "*">
2503 | <PLUS : "+">
2504
2505 | <LEFTPAREN : "(">
2506 | <RIGHTPAREN : ")">
2507 | <LEFTBRACKET : "[">
2508 | <RIGHTBRACKET : "]">
2509
2510 | <COLON : ":">
2511 | <COMMA : ",">
2512 | <DOT : ".">
2513 | <QUES : "?">
2514
2515 | <LT : "<">
2516 | <GT : ">">
2517 | <LE : "<=">
2518 | <GE : ">=">
2519 | <EQ : "=">
2520 | <NE : "!=">
2521 | <SIMILAR : "~=">
2522 | <ASSIGN : ":=">
2523
2524 | <AND : "and">
2525 | <OR : "or">
2526}
2527
2528<DEFAULT,IN_DBL_BRACE>
2529TOKEN :
2530{
2531 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
2532}
2533
2534<DEFAULT>
2535TOKEN :
2536{
2537 <RIGHTBRACE : "}"> { popState("}"); }
2538}
2539
2540<DEFAULT,IN_DBL_BRACE>
2541TOKEN :
2542{
2543 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
2544}
2545
2546<IN_DBL_BRACE>
2547TOKEN :
2548{
2549 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
2550}
2551
2552<DEFAULT,IN_DBL_BRACE>
2553TOKEN :
2554{
2555 <INTEGER_LITERAL : (<DIGIT>)+ >
2556}
2557
2558<DEFAULT,IN_DBL_BRACE>
2559TOKEN :
2560{
2561 <NULL : "null">
2562 | <TRUE : "true">
2563 | <FALSE : "false">
2564}
2565
2566<DEFAULT,IN_DBL_BRACE>
2567TOKEN :
2568{
2569 <#DIGIT : ["0" - "9"]>
2570}
2571
2572<DEFAULT,IN_DBL_BRACE>
2573TOKEN:
2574{
2575 < DOUBLE_LITERAL: <DIGITS>
2576 | <DIGITS> ( "." <DIGITS> )?
2577 | "." <DIGITS>
2578 >
2579 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
2580 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
2581 | "." <DIGITS> ( "f" | "F" )
2582 >
2583 | <DIGITS : (<DIGIT>)+ >
2584}
2585
2586<DEFAULT,IN_DBL_BRACE>
2587TOKEN :
2588{
2589 <#LETTER : ["A" - "Z", "a" - "z"]>
2590 | <SPECIALCHARS : ["$", "_", "-"]>
2591}
2592
2593<DEFAULT,IN_DBL_BRACE>
2594TOKEN :
2595{
2596 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
2597 <STRING_LITERAL : ("\"" (
2598 <EscapeQuot>
2599 | <EscapeBslash>
2600 | <EscapeSlash>
2601 | <EscapeBspace>
2602 | <EscapeFormf>
2603 | <EscapeNl>
2604 | <EscapeCr>
2605 | <EscapeTab>
2606 | ~["\"","\\"])* "\"")
2607 | ("\'"(
2608 <EscapeApos>
2609 | <EscapeBslash>
2610 | <EscapeSlash>
2611 | <EscapeBspace>
2612 | <EscapeFormf>
2613 | <EscapeNl>
2614 | <EscapeCr>
2615 | <EscapeTab>
2616 | ~["\'","\\"])* "\'")>
2617 | < #EscapeQuot: "\\\"" >
2618 | < #EscapeApos: "\\\'" >
2619 | < #EscapeBslash: "\\\\" >
2620 | < #EscapeSlash: "\\/" >
2621 | < #EscapeBspace: "\\b" >
2622 | < #EscapeFormf: "\\f" >
2623 | < #EscapeNl: "\\n" >
2624 | < #EscapeCr: "\\r" >
2625 | < #EscapeTab: "\\t" >
2626}
2627
2628<DEFAULT,IN_DBL_BRACE>
2629TOKEN :
2630{
2631 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2632}
2633
2634<DEFAULT,IN_DBL_BRACE>
2635TOKEN :
2636{
2637 <VARIABLE : "$" <LETTER> (<LETTER> | <DIGIT> | "_")*>
2638}
2639
2640<DEFAULT,IN_DBL_BRACE>
2641SKIP:
2642{
2643 " "
2644 | "\t"
2645 | "\r"
2646 | "\n"
2647}
2648
2649<DEFAULT,IN_DBL_BRACE>
2650SKIP:
2651{
2652 <"//" (~["\n"])* "\n">
2653}
2654
2655<DEFAULT,IN_DBL_BRACE>
2656SKIP:
2657{
2658 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2659}
2660
2661<DEFAULT,IN_DBL_BRACE>
2662SKIP:
2663{
2664 <"/*"> { pushState(); } : INSIDE_COMMENT
2665}
2666
2667<INSIDE_COMMENT>
2668SPECIAL_TOKEN:
2669{
2670 <"+"(" ")*(~["*"])*>
2671}
2672
2673<INSIDE_COMMENT>
2674SKIP:
2675{
2676 <"/*"> { pushState(); }
2677}
2678
2679<INSIDE_COMMENT>
2680SKIP:
2681{
2682 <"*/"> { popState("*/"); }
2683 | <~[]>
2684}