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