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