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