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