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