blob: ea95871a966477956e3002b7aafad6e7d5755ac3 [file] [log] [blame]
Yingyi Bu391f09e2015-10-29 13:49:39 -07001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package org.apache.asterix.lang.aql.parser;
12
13// For AQLParserTokenManager
14import org.apache.xerces.util.IntStack;
15
16import java.io.BufferedReader;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.io.Reader;
23import java.io.StringReader;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.LinkedHashMap;
27import java.util.List;
28import java.util.Map;
29
30import org.apache.asterix.common.annotations.AutoDataGen;
31import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
32import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
33import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
34import org.apache.asterix.common.annotations.FieldIntervalDataGen;
35import org.apache.asterix.common.annotations.FieldValFileDataGen;
36import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
37import org.apache.asterix.common.annotations.IRecordFieldDataGen;
38import org.apache.asterix.common.annotations.InsertRandIntDataGen;
39import org.apache.asterix.common.annotations.ListDataGen;
40import org.apache.asterix.common.annotations.ListValFileDataGen;
41import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
42import org.apache.asterix.common.annotations.TypeDataGen;
43import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
44import org.apache.asterix.common.config.DatasetConfig.DatasetType;
45import org.apache.asterix.common.config.DatasetConfig.IndexType;
Abdullah Alamoudie800e6d2016-01-14 11:20:55 +030046import org.apache.asterix.common.config.MetadataConstants;
Yingyi Bu391f09e2015-10-29 13:49:39 -070047import org.apache.asterix.common.exceptions.AsterixException;
48import org.apache.asterix.common.functions.FunctionSignature;
49import org.apache.asterix.lang.aql.clause.DistinctClause;
50import org.apache.asterix.lang.aql.clause.ForClause;
51import org.apache.asterix.lang.aql.expression.FLWOGRExpression;
52import org.apache.asterix.lang.aql.expression.UnionExpr;
53import org.apache.asterix.lang.aql.util.RangeMapBuilder;
54import org.apache.asterix.lang.common.base.Clause;
55import org.apache.asterix.lang.common.base.Expression;
56import org.apache.asterix.lang.common.base.IParser;
57import org.apache.asterix.lang.common.base.Literal;
58import org.apache.asterix.lang.common.base.Statement;
59import org.apache.asterix.lang.common.clause.GroupbyClause;
60import org.apache.asterix.lang.common.clause.LetClause;
61import org.apache.asterix.lang.common.clause.LimitClause;
62import org.apache.asterix.lang.common.clause.OrderbyClause;
63import org.apache.asterix.lang.common.clause.UpdateClause;
64import org.apache.asterix.lang.common.clause.WhereClause;
65import org.apache.asterix.lang.common.context.RootScopeFactory;
66import org.apache.asterix.lang.common.context.Scope;
67import org.apache.asterix.lang.common.expression.AbstractAccessor;
68import org.apache.asterix.lang.common.expression.CallExpr;
69import org.apache.asterix.lang.common.expression.FieldAccessor;
70import org.apache.asterix.lang.common.expression.FieldBinding;
71import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
72import org.apache.asterix.lang.common.expression.IfExpr;
73import org.apache.asterix.lang.common.expression.IndexAccessor;
74import org.apache.asterix.lang.common.expression.ListConstructor;
75import org.apache.asterix.lang.common.expression.LiteralExpr;
76import org.apache.asterix.lang.common.expression.OperatorExpr;
77import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
78import org.apache.asterix.lang.common.expression.QuantifiedExpression;
79import org.apache.asterix.lang.common.expression.RecordConstructor;
80import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
81import org.apache.asterix.lang.common.expression.TypeExpression;
82import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
83import org.apache.asterix.lang.common.expression.UnaryExpr;
84import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
85import org.apache.asterix.lang.common.expression.VariableExpr;
86import org.apache.asterix.lang.common.expression.UnaryExpr.Sign;
87import org.apache.asterix.lang.common.literal.DoubleLiteral;
88import org.apache.asterix.lang.common.literal.FalseLiteral;
89import org.apache.asterix.lang.common.literal.FloatLiteral;
90import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
91import org.apache.asterix.lang.common.literal.NullLiteral;
92import org.apache.asterix.lang.common.literal.StringLiteral;
93import org.apache.asterix.lang.common.literal.TrueLiteral;
94import org.apache.asterix.lang.common.parser.ScopeChecker;
95import org.apache.asterix.lang.common.statement.CompactStatement;
96import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
97import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
98import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
99import org.apache.asterix.lang.common.statement.CreateFeedStatement;
100import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
101import org.apache.asterix.lang.common.statement.CreateIndexStatement;
102import org.apache.asterix.lang.common.statement.CreatePrimaryFeedStatement;
103import org.apache.asterix.lang.common.statement.CreateSecondaryFeedStatement;
104import org.apache.asterix.lang.common.statement.DatasetDecl;
105import org.apache.asterix.lang.common.statement.DataverseDecl;
106import org.apache.asterix.lang.common.statement.DataverseDropStatement;
107import org.apache.asterix.lang.common.statement.DeleteStatement;
108import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
109import org.apache.asterix.lang.common.statement.DropStatement;
110import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
111import org.apache.asterix.lang.common.statement.FeedDropStatement;
112import org.apache.asterix.lang.common.statement.FunctionDecl;
113import org.apache.asterix.lang.common.statement.FunctionDropStatement;
114import org.apache.asterix.lang.common.statement.IndexDropStatement;
115import org.apache.asterix.lang.common.statement.InsertStatement;
116import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
117import org.apache.asterix.lang.common.statement.LoadStatement;
118import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
119import org.apache.asterix.lang.common.statement.NodegroupDecl;
120import org.apache.asterix.lang.common.statement.Query;
121import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
122import org.apache.asterix.lang.common.statement.RunStatement;
123import org.apache.asterix.lang.common.statement.SetStatement;
124import org.apache.asterix.lang.common.statement.TypeDecl;
125import org.apache.asterix.lang.common.statement.TypeDropStatement;
126import org.apache.asterix.lang.common.statement.UpdateStatement;
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300127import org.apache.asterix.lang.common.statement.UpsertStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700128import org.apache.asterix.lang.common.statement.WriteStatement;
129import org.apache.asterix.lang.common.struct.Identifier;
130import org.apache.asterix.lang.common.struct.QuantifiedPair;
131import org.apache.asterix.lang.common.struct.VarIdentifier;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700132import org.apache.hyracks.algebricks.common.utils.Pair;
133import org.apache.hyracks.algebricks.common.utils.Triple;
134import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
135import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
136import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
137
138
Yingyi Bucaea8f02015-11-16 15:12:15 -0800139class AQLParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700140
141 // optimizer hints
142 private static final String AUTO_HINT = "auto";
143 private static final String BROADCAST_JOIN_HINT = "bcast";
144 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
145 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
146 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
147 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
148 private static final String HASH_GROUP_BY_HINT = "hash";
149 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
150 private static final String INMEMORY_HINT = "inmem";
151 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
152 private static final String INTERVAL_HINT = "interval";
153 private static final String LIST_HINT = "list";
154 private static final String LIST_VAL_FILE_HINT = "list-val-file";
155 private static final String RANGE_HINT = "range";
156 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
157 private static final String VAL_FILE_HINT = "val-files";
158 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
159
160 private static final String GEN_FIELDS_HINT = "gen-fields";
161
162 // data generator hints
163 private static final String DGEN_HINT = "dgen";
164
165 private static class IndexParams {
166 public IndexType type;
167 public int gramLength;
168
169 public IndexParams(IndexType type, int gramLength) {
170 this.type = type;
171 this.gramLength = gramLength;
172 }
173 };
174
175 private static class FunctionName {
176 public String dataverse = null;
177 public String library = null;
178 public String function = null;
179 public String hint = null;
180 }
181
182 private static String getHint(Token t) {
183 if (t.specialToken == null) {
184 return null;
185 }
186 String s = t.specialToken.image;
187 int n = s.length();
188 if (n < 2) {
189 return null;
190 }
191 return s.substring(1).trim();
192 }
193
194 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
195 IRecordFieldDataGen rfdg = null;
196 String splits[] = hint.split(" +");
197 if (splits[0].equals(VAL_FILE_HINT)) {
198 File[] valFiles = new File[splits.length - 1];
199 for (int k=1; k<splits.length; k++) {
200 valFiles[k-1] = new File(splits[k]);
201 }
202 rfdg = new FieldValFileDataGen(valFiles);
203 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
204 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
205 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
206 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
207 } else if (splits[0].equals(LIST_HINT)) {
208 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
209 } else if (splits[0].equals(INTERVAL_HINT)) {
210 FieldIntervalDataGen.ValueType vt;
211 if (splits[1].equals("int")) {
212 vt = FieldIntervalDataGen.ValueType.INT;
213 } else if (splits[1].equals("long")) {
214 vt = FieldIntervalDataGen.ValueType.LONG;
215 } else if (splits[1].equals("float")) {
216 vt = FieldIntervalDataGen.ValueType.FLOAT;
217 } else if (splits[1].equals("double")) {
218 vt = FieldIntervalDataGen.ValueType.DOUBLE;
219 } else {
220 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
221 }
222 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
223 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
224 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
225 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
226 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
227 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
228 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
229 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
230 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
231 } else if (splits[0].equals(AUTO_HINT)) {
232 rfdg = new AutoDataGen(splits[1]);
233 }
234 return rfdg;
235 }
236
237 public AQLParser(String s){
238 this(new StringReader(s));
239 super.setInput(s);
240 }
241
242 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
243 File file = new File(args[0]);
244 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
245 AQLParser parser = new AQLParser(fis);
246 List<Statement> st = parser.parse();
247 //st.accept(new AQLPrintVisitor(), 0);
248 }
249
250 public List<Statement> parse() throws AsterixException {
251 try {
252 return Statement();
253 } catch (Error e) {
254 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
255 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
256 throw new AsterixException(new ParseException(e.getMessage()));
257 } catch (ParseException e){
258 throw new AsterixException(e.getMessage());
259 }
260 }
261}
262
263PARSER_END(AQLParser)
264
265
266List<Statement> Statement() throws ParseException:
267{
268 scopeStack.push(RootScopeFactory.createRootScope(this));
269 List<Statement> decls = new ArrayList<Statement>();
270 Statement stmt = null;
271}
272{
273 ( stmt = SingleStatement() (";") ?
274 {
275 decls.add(stmt);
276 }
277 )*
278 <EOF>
279 {
280 return decls;
281 }
282}
283
284Statement SingleStatement() throws ParseException:
285{
286 Statement stmt = null;
287}
288{
289 (
290 stmt = DataverseDeclaration()
291 | stmt = FunctionDeclaration()
292 | stmt = CreateStatement()
293 | stmt = LoadStatement()
294 | stmt = DropStatement()
295 | stmt = WriteStatement()
296 | stmt = SetStatement()
297 | stmt = InsertStatement()
298 | stmt = DeleteStatement()
299 | stmt = UpdateStatement()
300 | stmt = FeedStatement()
301 | stmt = CompactStatement()
302 | stmt = Query()
303 | stmt = RefreshExternalDatasetStatement()
304 | stmt = RunStatement()
305 )
306 {
307 return stmt;
308 }
309}
310
311DataverseDecl DataverseDeclaration() throws ParseException:
312{
313 String dvName = null;
314}
315{
316 "use" "dataverse" dvName = Identifier()
317 {
318 defaultDataverse = dvName;
319 return new DataverseDecl(new Identifier(dvName));
320 }
321}
322
323Statement CreateStatement() throws ParseException:
324{
325 String hint = null;
326 boolean dgen = false;
327 Statement stmt = null;
328}
329{
330 "create"
331 (
332 {
333 hint = getHint(token);
334 if (hint != null && hint.startsWith(DGEN_HINT)) {
335 dgen = true;
336 }
337 }
338 stmt = TypeSpecification(hint, dgen)
339 | stmt = NodegroupSpecification()
340 | stmt = DatasetSpecification()
341 | stmt = IndexSpecification()
342 | stmt = DataverseSpecification()
343 | stmt = FunctionSpecification()
344 | stmt = FeedSpecification()
345 | stmt = FeedPolicySpecification()
346 )
347 {
348 return stmt;
349 }
350}
351
352TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
353{
354 Pair<Identifier,Identifier> nameComponents = null;
355 boolean ifNotExists = false;
356 TypeExpression typeExpr = null;
357}
358{
359 "type" nameComponents = TypeName() ifNotExists = IfNotExists()
360 "as" typeExpr = TypeExpr()
361 {
362 long numValues = -1;
363 String filename = null;
364 if (dgen) {
365 String splits[] = hint.split(" +");
366 if (splits.length != 3) {
367 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
368 }
369 filename = splits[1];
370 numValues = Long.parseLong(splits[2]);
371 }
372 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
373 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
374 }
375}
376
377
378NodegroupDecl NodegroupSpecification() throws ParseException:
379{
380 String name = null;
381 String tmp = null;
382 boolean ifNotExists = false;
383 List<Identifier>ncNames = null;
384}
385{
386 "nodegroup" name = Identifier()
387 ifNotExists = IfNotExists() "on" tmp = Identifier()
388 {
389 ncNames = new ArrayList<Identifier>();
390 ncNames.add(new Identifier(tmp));
391 }
392 ( <COMMA> tmp = Identifier()
393 {
394 ncNames.add(new Identifier(tmp));
395 }
396 )*
397 {
398 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
399 }
400}
401
402DatasetDecl DatasetSpecification() throws ParseException:
403{
404 Pair<Identifier,Identifier> nameComponents = null;
405 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800406 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700407 String adapterName = null;
408 Map<String,String> properties = null;
409 Map<String,String> compactionPolicyProperties = null;
410 FunctionSignature appliedFunction = null;
411 List<List<String>> primaryKeyFields = null;
412 String nodeGroupName = null;
413 Map<String,String> hints = new HashMap<String,String>();
414 DatasetDecl dsetDecl = null;
415 boolean autogenerated = false;
416 String compactionPolicy = null;
417 boolean temp = false;
418 List<String> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800419 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700420}
421{
422 (
423 "external" <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800424 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700425 ifNotExists = IfNotExists()
426 "using" adapterName = AdapterName() properties = Configuration()
427 ("on" nodeGroupName = Identifier() )?
428 ( "hints" hints = Properties() )?
429 ( "using" "compaction" "policy" compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
430 {
431 ExternalDetailsDecl edd = new ExternalDetailsDecl();
432 edd.setAdapter(adapterName);
433 edd.setProperties(properties);
434 dsetDecl = new DatasetDecl(nameComponents.first,
435 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800436 typeComponents.first,
437 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800438 metaTypeComponents.first,
439 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700440 nodeGroupName != null? new Identifier(nodeGroupName): null,
441 compactionPolicy,
442 compactionPolicyProperties,
443 hints,
444 DatasetType.EXTERNAL,
445 edd,
446 ifNotExists);
447 }
448
449 | ("internal" | "temporary" {
450 temp = token.image.toLowerCase().equals("temporary");
451 }
Yingyi Bub9169b62016-02-26 21:21:49 -0800452 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700453 <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800454 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800455 (
456 { String name; }
457 <WITH>
458 name = Identifier()
459 {
460 if(!name.equals("meta")){
461 throw new ParseException("We can only support one additional associated field called \"meta\".");
462 }
463 }
464 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
465 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700466 ifNotExists = IfNotExists()
467 primaryKeyFields = PrimaryKey()
468 ("autogenerated" { autogenerated = true; } )?
469 ("on" nodeGroupName = Identifier() )?
470 ( "hints" hints = Properties() )?
471 ( "using" "compaction" "policy" compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
472 ( "with filter on" filterField = NestedField() )?
473 {
474 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields,
475 autogenerated,
476 filterField,
477 temp);
478 dsetDecl = new DatasetDecl(nameComponents.first,
479 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800480 typeComponents.first,
481 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800482 metaTypeComponents.first,
483 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700484 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
485 compactionPolicy,
486 compactionPolicyProperties,
487 hints,
488 DatasetType.INTERNAL,
489 idd,
490 ifNotExists);
491 }
492 )
493 {
494 return dsetDecl;
495 }
496}
497
498RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
499{
500 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
501 Pair<Identifier,Identifier> nameComponents = null;
502 String datasetName = null;
503}
504{
505 "refresh external" <DATASET> nameComponents = QualifiedName()
506 {
507 redss.setDataverseName(nameComponents.first);
508 redss.setDatasetName(nameComponents.second);
509 return redss;
510 }
511}
512
513RunStatement RunStatement() throws ParseException:
514{
515 String system = null;
516 String tmp;
517 ArrayList<String> parameters = new ArrayList<String>();
518 Pair<Identifier,Identifier> nameComponentsFrom = null;
519 Pair<Identifier,Identifier> nameComponentsTo = null;
520}
521{
522 "run" system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
523 {
524 parameters.add(tmp);
525 }
526 )*<RIGHTPAREN>
527 <FROM> <DATASET> nameComponentsFrom = QualifiedName()
528 "to" <DATASET> nameComponentsTo = QualifiedName()
529 {
530 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
531 }
532}
533
534CreateIndexStatement IndexSpecification() throws ParseException:
535{
536 CreateIndexStatement cis = new CreateIndexStatement();
537 String indexName = null;
538 boolean ifNotExists = false;
539 Pair<Identifier,Identifier> nameComponents = null;
540 Pair<List<String>, TypeExpression> fieldPair = null;
541 IndexParams indexType = null;
542 boolean enforced = false;
543}
544{
545 "index" indexName = Identifier()
546 ifNotExists = IfNotExists()
547 "on" nameComponents = QualifiedName()
548 <LEFTPAREN> ( fieldPair = OpenField()
549 {
550 cis.addFieldExprPair(fieldPair);
551 }
552 ) (<COMMA> fieldPair = OpenField()
553 {
554 cis.addFieldExprPair(fieldPair);
555 }
556 )* <RIGHTPAREN> ( "type" indexType = IndexType() )? ( "enforced" { enforced = true; } )?
557 {
558 cis.setIndexName(new Identifier(indexName));
559 cis.setIfNotExists(ifNotExists);
560 cis.setDataverseName(nameComponents.first);
561 cis.setDatasetName(nameComponents.second);
562 if (indexType != null) {
563 cis.setIndexType(indexType.type);
564 cis.setGramLength(indexType.gramLength);
565 }
566 cis.setEnforced(enforced);
567 return cis;
568 }
569}
570
571String CompactionPolicy() throws ParseException :
572{
573 String compactionPolicy = null;
574}
575{
576 compactionPolicy = Identifier()
577 {
578 return compactionPolicy;
579 }
580}
581
582String FilterField() throws ParseException :
583{
584 String filterField = null;
585}
586{
587 filterField = Identifier()
588 {
589 return filterField;
590 }
591}
592
593IndexParams IndexType() throws ParseException:
594{
595 IndexType type = null;
596 int gramLength = 0;
597}
598{
599 ("btree"
600 {
601 type = IndexType.BTREE;
602 }
603 | "rtree"
604 {
605 type = IndexType.RTREE;
606 }
607 | "keyword"
608 {
609 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
610 }
611 | "ngram" <LEFTPAREN> <INTEGER_LITERAL>
612 {
613 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
614 gramLength = Integer.valueOf(token.image);
615 }
616 <RIGHTPAREN>)
617 {
618 return new IndexParams(type, gramLength);
619 }
620}
621
622CreateDataverseStatement DataverseSpecification() throws ParseException :
623{
624 String dvName = null;
625 boolean ifNotExists = false;
626 String format = null;
627}
628{
629 "dataverse" dvName = Identifier()
630 ifNotExists = IfNotExists()
631 ( "with format" format = StringLiteral() )?
632 {
633 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
634 }
635}
636
637CreateFunctionStatement FunctionSpecification() throws ParseException:
638{
639 FunctionSignature signature;
640 boolean ifNotExists = false;
641 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
642 String functionBody;
643 VarIdentifier var = null;
644 Expression functionBodyExpr;
645 Token beginPos;
646 Token endPos;
647 FunctionName fctName = null;
648
649 createNewScope();
650}
651{
652 "function" fctName = FunctionName()
653 ifNotExists = IfNotExists()
654 paramList = ParameterList()
655 <LEFTBRACE>
656 {
657 beginPos = token;
658 }
659 functionBodyExpr = Expression() <RIGHTBRACE>
660 {
661 endPos = token;
662 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
663 // TODO use fctName.library
664 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
665 getCurrentScope().addFunctionDescriptor(signature, false);
666 removeCurrentScope();
667 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
668 }
669}
670
671CreateFeedStatement FeedSpecification() throws ParseException:
672{
673 Pair<Identifier,Identifier> nameComponents = null;
674 boolean ifNotExists = false;
675 String adapterName = null;
676 Map<String,String> properties = null;
677 FunctionSignature appliedFunction = null;
678 CreateFeedStatement cfs = null;
679 Pair<Identifier,Identifier> sourceNameComponents = null;
680
681}
682{
683 (
684 "secondary" "feed" nameComponents = QualifiedName() ifNotExists = IfNotExists()
685 <FROM> "feed" sourceNameComponents = QualifiedName() (appliedFunction = ApplyFunction())?
686 {
687 cfs = new CreateSecondaryFeedStatement(nameComponents,
688 sourceNameComponents, appliedFunction, ifNotExists);
689 }
690 |
691 ("primary")? "feed" nameComponents = QualifiedName() ifNotExists = IfNotExists()
692 "using" adapterName = AdapterName() properties = Configuration() (appliedFunction = ApplyFunction())?
693 {
694 cfs = new CreatePrimaryFeedStatement(nameComponents,
695 adapterName, properties, appliedFunction, ifNotExists);
696 }
697 )
698 {
699 return cfs;
700 }
701}
702
703CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
704{
705 String policyName = null;
706 String basePolicyName = null;
707 String sourcePolicyFile = null;
708 String definition = null;
709 boolean ifNotExists = false;
710 Map<String,String> properties = null;
711 CreateFeedPolicyStatement cfps = null;
712}
713{
714 (
715 "ingestion" "policy" policyName = Identifier() ifNotExists = IfNotExists()
716 <FROM>
717 ("policy" basePolicyName = Identifier() properties = Configuration() ("definition" definition = StringLiteral())?
718 {
719 cfps = new CreateFeedPolicyStatement(policyName,
720 basePolicyName, properties, definition, ifNotExists);
721 }
722 | "path" sourcePolicyFile = Identifier() ("definition" definition = StringLiteral())?
723 {
724 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
725 }
726 )
727
728 )
729 {
730 return cfps;
731 }
732}
733
734
735
736List<VarIdentifier> ParameterList() throws ParseException:
737{
738 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
739 VarIdentifier var = null;
740}
741{
742 <LEFTPAREN> (<VARIABLE>
743 {
744 var = new VarIdentifier();
745 var.setValue(token.image);
746 paramList.add(var);
747 getCurrentScope().addNewVarSymbolToScope(var);
748 }
749 (<COMMA> <VARIABLE>
750 {
751 var = new VarIdentifier();
752 var.setValue(token.image);
753 paramList.add(var);
754 getCurrentScope().addNewVarSymbolToScope(var);
755 }
756 )*)? <RIGHTPAREN>
757 {
758 return paramList;
759 }
760}
761
762boolean IfNotExists() throws ParseException:
763{
764}
765{
766 ( "if not exists"
767 {
768 return true;
769 }
770 )?
771 {
772 return false;
773 }
774}
775
776FunctionSignature ApplyFunction() throws ParseException:
777{
778 FunctionName functioName = null;
779 FunctionSignature funcSig = null;
780}
781{
782 "apply" "function" functioName = FunctionName()
783 {
784 String fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
785 return new FunctionSignature(functioName.dataverse, fqFunctionName, 1);
786 }
787}
788
789String GetPolicy() throws ParseException:
790{
791 String policy = null;
792}
793{
794 "using" "policy" policy = Identifier()
795 {
796 return policy;
797 }
798
799}
800
801FunctionSignature FunctionSignature() throws ParseException:
802{
803 FunctionName fctName = null;
804 int arity = 0;
805}
806{
807 fctName = FunctionName() "@" <INTEGER_LITERAL>
808 {
809 arity = new Integer(token.image);
810 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
811 throw new ParseException(" invalid arity:" + arity);
812 }
813
814 // TODO use fctName.library
815 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
816 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
817 }
818}
819
820List<List<String>> PrimaryKey() throws ParseException:
821{
822 List<String> tmp = null;
823 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
824}
825{
826 "primary" "key" tmp = NestedField()
827 {
828 primaryKeyFields.add(tmp);
829 }
830 ( <COMMA> tmp = NestedField()
831 {
832 primaryKeyFields.add(tmp);
833 }
834 )*
835 {
836 return primaryKeyFields;
837 }
838}
839
840Statement DropStatement() throws ParseException:
841{
842 String id = null;
843 Pair<Identifier,Identifier> pairId = null;
844 Triple<Identifier,Identifier,Identifier> tripleId = null;
845 FunctionSignature funcSig = null;
846 boolean ifExists = false;
847 Statement stmt = null;
848}
849{
850 "drop"
851 (
852 <DATASET> pairId = QualifiedName() ifExists = IfExists()
853 {
854 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
855 }
856 | "index" tripleId = DoubleQualifiedName() ifExists = IfExists()
857 {
858 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
859 }
860 | "nodegroup" id = Identifier() ifExists = IfExists()
861 {
862 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
863 }
864 | "type" pairId = TypeName() ifExists = IfExists()
865 {
866 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
867 }
868 | "dataverse" id = Identifier() ifExists = IfExists()
869 {
870 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
871 }
872 | "function" funcSig = FunctionSignature() ifExists = IfExists()
873 {
874 stmt = new FunctionDropStatement(funcSig, ifExists);
875 }
876 | "feed" pairId = QualifiedName() ifExists = IfExists()
877 {
878 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
879 }
880 )
881 {
882 return stmt;
883 }
884}
885
886boolean IfExists() throws ParseException :
887{
888}
889{
890 ( <IF> "exists"
891 {
892 return true;
893 }
894 )?
895 {
896 return false;
897 }
898}
899
900InsertStatement InsertStatement() throws ParseException:
901{
902 Pair<Identifier,Identifier> nameComponents = null;
903 Query query;
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300904 boolean upsert = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700905}
906{
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300907 ("insert"|"upsert"{ upsert = true; }) "into" <DATASET> nameComponents = QualifiedName() query = Query()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700908 {
Yingyi Bucaea8f02015-11-16 15:12:15 -0800909 query.setTopLevel(true);
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300910 if(upsert){
911 return new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
912 } else{
913 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
914 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700915 }
916}
917
918DeleteStatement DeleteStatement() throws ParseException:
919{
920 VariableExpr var = null;
921 Expression condition = null;
922 Pair<Identifier, Identifier> nameComponents;
923 // This is related to the new metadata lock management
924 setDataverses(new ArrayList<String>());
925 setDatasets(new ArrayList<String>());
926
927}
928{
929 "delete" var = Variable()
930 {
931 getCurrentScope().addNewVarSymbolToScope(var.getVar());
932 }
933 <FROM> <DATASET> 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() <ASSIGN> 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 = StringLiteral()
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 = StringLiteral()
1019 ( "using" writerClass = StringLiteral() )?
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 ("pre-sorted"
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 = StringLiteral() <EQ> value = StringLiteral() <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 ( <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 = StringLiteral() | <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 ("#" third = Identifier())? | "#" 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
1353
1354Pair<Identifier,Identifier> TypeName() throws ParseException:
1355{
1356 Pair<Identifier,Identifier> name = null;
1357}
1358{
1359 name = QualifiedName()
1360 {
1361 if (name.first == null) {
1362 name.first = new Identifier(defaultDataverse);
1363 }
1364 return name;
1365 }
1366}
1367
1368String Identifier() throws ParseException:
1369{
1370 String lit = null;
1371}
1372{
1373 (<IDENTIFIER>
1374 {
1375 return token.image;
1376 }
1377 | lit = StringLiteral()
1378 {
1379 return lit;
1380 }
1381 )
1382}
1383
1384Pair<List<String>, TypeExpression> OpenField() throws ParseException:
1385{
1386 TypeExpression fieldType = null;
1387 List<String> fieldList = null;
1388}
1389{
1390 fieldList = NestedField()
1391 ( <COLON> fieldType = IndexedTypeExpr() )?
1392 {
1393 return new Pair<List<String>, TypeExpression>(fieldList, fieldType);
1394 }
1395}
1396
1397List<String> NestedField() throws ParseException:
1398{
1399 List<String> exprList = new ArrayList<String>();
1400 String lit = null;
1401}
1402{
1403 lit = Identifier()
1404 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001405 boolean meetParens = false;
1406 }
1407 (
1408 <LEFTPAREN><RIGHTPAREN>
1409 {
1410 if(lit.equals("meta")){
1411 exprList.add(lit+"()");
1412 }else{
1413 throw new ParseException("The string before () has to be \"meta\".");
1414 }
1415 meetParens = true;
1416 }
1417 )?
1418 {
1419 if(!meetParens){
1420 exprList.add(lit);
1421 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001422 }
1423 (<DOT>
1424 lit = Identifier()
1425 {
1426 exprList.add(lit);
1427 }
1428 )*
1429 {
1430 return exprList;
1431 }
1432}
1433
1434
1435
1436String StringLiteral() throws ParseException:
1437{
1438}
1439{
1440 <STRING_LITERAL>
1441 {
1442 return removeQuotesAndEscapes(token.image);
1443 }
1444}
1445
1446Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1447{
1448 String first = null;
1449 String second = null;
1450}
1451{
1452 first = Identifier() (<DOT> second = Identifier())?
1453 {
1454 Identifier id1 = null;
1455 Identifier id2 = null;
1456 if (second == null) {
1457 id2 = new Identifier(first);
1458 } else
1459 {
1460 id1 = new Identifier(first);
1461 id2 = new Identifier(second);
1462 }
1463 return new Pair<Identifier,Identifier>(id1, id2);
1464 }
1465}
1466
1467Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1468{
1469 String first = null;
1470 String second = null;
1471 String third = null;
1472}
1473{
1474 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1475 {
1476 Identifier id1 = null;
1477 Identifier id2 = null;
1478 Identifier id3 = null;
1479 if (third == null) {
1480 id2 = new Identifier(first);
1481 id3 = new Identifier(second);
1482 } else {
1483 id1 = new Identifier(first);
1484 id2 = new Identifier(second);
1485 id3 = new Identifier(third);
1486 }
1487 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1488 }
1489}
1490
1491FunctionDecl FunctionDeclaration() throws ParseException:
1492{
1493 FunctionDecl funcDecl;
1494 FunctionSignature signature;
1495 String functionName;
1496 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1497 Expression funcBody;
1498 createNewScope();
1499}
1500{
1501 "declare" "function" functionName = Identifier()
1502 paramList = ParameterList()
1503 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1504 {
1505 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1506 getCurrentScope().addFunctionDescriptor(signature, false);
1507 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1508 removeCurrentScope();
1509 return funcDecl;
1510 }
1511}
1512
1513
1514Query Query() throws ParseException:
1515{
1516 Query query = new Query();
1517 // we set the pointers to the dataverses and datasets lists to fill them with entities to be locked
1518 setDataverses(query.getDataverses());
1519 setDatasets(query.getDatasets());
1520 Expression expr;
1521}
1522{
1523 expr = Expression()
1524 {
1525 query.setBody(expr);
1526 query.setVarCounter(getVarCounter());
1527 // we remove the pointers to the locked entities before we return the query object
1528 setDataverses(null);
1529 setDatasets(null);
1530 return query;
1531 }
1532
1533}
1534
1535
1536
1537Expression Expression():
1538{
1539 Expression expr = null;
1540 Expression exprP = null;
1541}
1542{
1543(
1544
1545//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1546 expr = OperatorExpr()
1547 | expr = IfThenElse()
1548 | expr = FLWOGR()
1549 | expr = QuantifiedExpression()
1550
1551
1552)
1553 {
1554 return (exprP==null) ? expr : exprP;
1555 }
1556}
1557
1558
1559
1560Expression OperatorExpr()throws ParseException:
1561{
1562 OperatorExpr op = null;
1563 Expression operand = null;
1564}
1565{
1566 operand = AndExpr()
1567 (
1568
1569 <OR>
1570 {
1571 if (op == null) {
1572 op = new OperatorExpr();
1573 op.addOperand(operand);
1574 op.setCurrentop(true);
1575 }
1576 op.addOperator(token.image);
1577 }
1578
1579 operand = AndExpr()
1580 {
1581 op.addOperand(operand);
1582 }
1583
1584 )*
1585
1586 {
1587 return op==null? operand: op;
1588 }
1589}
1590
1591Expression AndExpr()throws ParseException:
1592{
1593 OperatorExpr op = null;
1594 Expression operand = null;
1595}
1596{
1597 operand = RelExpr()
1598 (
1599
1600 <AND>
1601 {
1602 if (op == null) {
1603 op = new OperatorExpr();
1604 op.addOperand(operand);
1605 op.setCurrentop(true);
1606 }
1607 op.addOperator(token.image);
1608 }
1609
1610 operand = RelExpr()
1611 {
1612 op.addOperand(operand);
1613 }
1614
1615 )*
1616
1617 {
1618 return op==null? operand: op;
1619 }
1620}
1621
1622
1623
1624Expression RelExpr()throws ParseException:
1625{
1626 OperatorExpr op = null;
1627 Expression operand = null;
1628 boolean broadcast = false;
1629 IExpressionAnnotation annotation = null;
1630}
1631{
1632 operand = AddExpr()
1633 {
1634 if (operand instanceof VariableExpr) {
1635 String hint = getHint(token);
1636 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1637 broadcast = true;
1638 }
1639 }
1640 }
1641
1642 (
1643 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> |<SIMILAR>)
1644 {
1645 String mhint = getHint(token);
1646 if (mhint != null) {
1647 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1648 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1649 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1650 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1651 }
1652 }
1653 if (op == null) {
1654 op = new OperatorExpr();
1655 op.addOperand(operand, broadcast);
1656 op.setCurrentop(true);
1657 broadcast = false;
1658 }
1659 op.addOperator(token.image);
1660 }
1661
1662 operand = AddExpr()
1663 {
1664 broadcast = false;
1665 if (operand instanceof VariableExpr) {
1666 String hint = getHint(token);
1667 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1668 broadcast = true;
1669 }
1670 }
1671 op.addOperand(operand, broadcast);
1672 }
1673 )?
1674
1675 {
1676 if (annotation != null) {
1677 op.addHint(annotation);
1678 }
1679 return op==null? operand: op;
1680 }
1681}
1682
1683Expression AddExpr()throws ParseException:
1684{
1685 OperatorExpr op = null;
1686 Expression operand = null;
1687}
1688{
1689 operand = MultExpr()
1690
1691 ( (<PLUS> | <MINUS>)
1692 {
1693 if (op == null) {
1694 op = new OperatorExpr();
1695 op.addOperand(operand);
1696 op.setCurrentop(true);
1697 }
1698 ((OperatorExpr)op).addOperator(token.image);
1699 }
1700
1701 operand = MultExpr()
1702 {
1703 op.addOperand(operand);
1704 }
1705 )*
1706
1707 {
1708 return op==null? operand: op;
1709 }
1710}
1711
1712Expression MultExpr()throws ParseException:
1713{
1714 OperatorExpr op = null;
1715 Expression operand = null;
1716}
1717{
1718 operand = UnionExpr()
1719
1720 (( <MUL> | <DIV> | <MOD> | <CARET> | <IDIV>)
1721 {
1722 if (op == null) {
1723 op = new OperatorExpr();
1724 op.addOperand(operand);
1725 op.setCurrentop(true);
1726 }
1727 op.addOperator(token.image);
1728 }
1729 operand = UnionExpr()
1730 {
1731 op.addOperand(operand);
1732 }
1733 )*
1734
1735 {
1736 return op==null?operand:op;
1737 }
1738}
1739
1740Expression UnionExpr() throws ParseException:
1741{
1742 UnionExpr union = null;
1743 Expression operand1 = null;
1744 Expression operand2 = null;
1745}
1746{
1747 operand1 = UnaryExpr()
1748 (<UNION>
1749 (operand2 = UnaryExpr()) {
1750 if (union == null) {
1751 union = new UnionExpr();
1752 union.addExpr(operand1);
1753 }
1754 union.addExpr(operand2);
1755 } )*
1756 {
1757 return (union == null)? operand1: union;
1758 }
1759}
1760
1761Expression UnaryExpr() throws ParseException:
1762{
1763 Expression uexpr = null;
1764 Expression expr = null;
1765}
1766{
1767 ( (<PLUS> | <MINUS>)
1768 {
1769 uexpr = new UnaryExpr();
1770 if("+".equals(token.image))
1771 ((UnaryExpr)uexpr).setSign(Sign.POSITIVE);
1772 else if("-".equals(token.image))
1773 ((UnaryExpr)uexpr).setSign(Sign.NEGATIVE);
1774 else
1775 throw new ParseException();
1776 }
1777 )?
1778
1779 expr = ValueExpr()
1780 {
1781 if(uexpr!=null){
1782 ((UnaryExpr)uexpr).setExpr(expr);
1783 return uexpr;
1784 }
1785 else{
1786 return expr;
1787 }
1788 }
1789}
1790
1791Expression ValueExpr()throws ParseException:
1792{
1793 Expression expr = null;
1794 Identifier ident = null;
1795 AbstractAccessor fa = null;
1796 Expression indexExpr = null;
1797}
1798{
1799 expr = PrimaryExpr() ( ident = Field()
1800 {
1801 fa = (fa == null ? new FieldAccessor(expr, ident)
1802 : new FieldAccessor(fa, ident));
1803 }
1804 | indexExpr = Index()
1805 {
1806 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
1807 : new IndexAccessor(fa, indexExpr));
1808 }
1809 )*
1810 {
1811 return fa == null ? expr : fa;
1812 }
1813}
1814
1815Identifier Field() throws ParseException:
1816{
1817 String ident = null;
1818}
1819{
1820 <DOT> ident = Identifier()
1821 {
1822 return new Identifier(ident);
1823 }
1824}
1825
1826Expression Index() throws ParseException:
1827{
1828 Expression expr = null;
1829}
1830{
1831 <LEFTBRACKET> ( expr = Expression()
1832 {
1833 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1834 {
1835 Literal lit = ((LiteralExpr)expr).getValue();
1836 if(lit.getLiteralType() != Literal.Type.INTEGER &&
1837 lit.getLiteralType() != Literal.Type.LONG) {
1838 throw new ParseException("Index should be an INTEGER");
1839 }
1840 }
1841 }
1842
1843 | <QUES> // ANY
1844
1845 )
1846
1847 <RIGHTBRACKET>
1848 {
1849 return expr;
1850 }
1851}
1852
1853
1854Expression PrimaryExpr()throws ParseException:
1855{
1856 Expression expr = null;
1857}
1858{
1859 ( LOOKAHEAD(2)
1860 expr = FunctionCallExpr()
1861 | expr = Literal()
1862 | expr = DatasetAccessExpression()
1863 | expr = VariableRef()
1864 {
1865 if(((VariableExpr)expr).getIsNewVar() == true)
1866 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1867 }
1868 | expr = ListConstructor()
1869 | expr = RecordConstructor()
1870 | expr = ParenthesizedExpression()
1871 )
1872 {
1873 return expr;
1874 }
1875}
1876
1877Expression Literal() throws ParseException:
1878{
1879 LiteralExpr lit = new LiteralExpr();
1880 String str = null;
1881}
1882{
1883 ( str = StringLiteral()
1884 {
1885 lit.setValue(new StringLiteral(str));
1886 }
1887 | <INTEGER_LITERAL>
1888 {
1889 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1890 }
1891 | <FLOAT_LITERAL>
1892 {
1893 lit.setValue(new FloatLiteral(new Float(token.image)));
1894 }
1895 | <DOUBLE_LITERAL>
1896 {
1897 lit.setValue(new DoubleLiteral(new Double(token.image)));
1898 }
1899 | <NULL>
1900 {
1901 lit.setValue(NullLiteral.INSTANCE);
1902 }
1903 | <TRUE>
1904 {
1905 lit.setValue(TrueLiteral.INSTANCE);
1906 }
1907 | <FALSE>
1908 {
1909 lit.setValue(FalseLiteral.INSTANCE);
1910 }
1911 )
1912 {
1913 return lit;
1914 }
1915}
1916
1917
1918VariableExpr VariableRef() throws ParseException:
1919{
1920 VariableExpr varExp = new VariableExpr();
1921 VarIdentifier var = new VarIdentifier();
1922}
1923{
1924 <VARIABLE>
1925 {
1926 String varName = token.image;
1927 Identifier ident = lookupSymbol(varName);
1928 if (isInForbiddenScopes(varName)) {
1929 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.");
1930 }
1931 if(ident != null) { // exist such ident
1932 varExp.setIsNewVar(false);
1933 varExp.setVar((VarIdentifier)ident);
1934 } else {
1935 varExp.setVar(var);
1936 }
1937 var.setValue(varName);
1938 return varExp;
1939 }
1940}
1941
1942
1943VariableExpr Variable() throws ParseException:
1944{
1945 VariableExpr varExp = new VariableExpr();
1946 VarIdentifier var = new VarIdentifier();
1947}
1948{
1949 <VARIABLE>
1950 {
1951 Identifier ident = lookupSymbol(token.image);
1952 if(ident != null) { // exist such ident
1953 varExp.setIsNewVar(false);
1954 }
1955 varExp.setVar(var);
1956 var.setValue(token.image);
1957 return varExp;
1958 }
1959}
1960
1961Expression ListConstructor() throws ParseException:
1962{
1963 Expression expr = null;
1964}
1965{
1966 (
1967 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
1968 )
1969
1970 {
1971 return expr;
1972 }
1973}
1974
1975
1976ListConstructor OrderedListConstructor() throws ParseException:
1977{
1978 ListConstructor expr = new ListConstructor();
1979 List<Expression> exprList = null;
1980 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
1981}
1982{
1983 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
1984 {
1985 expr.setExprList(exprList);
1986 return expr;
1987 }
1988}
1989
1990ListConstructor UnorderedListConstructor() throws ParseException:
1991{
1992 ListConstructor expr = new ListConstructor();
1993 List<Expression> exprList = null;
1994 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
1995}
1996{
1997 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
1998 {
1999 expr.setExprList(exprList);
2000 return expr;
2001 }
2002}
2003
2004List<Expression> ExpressionList() throws ParseException:
2005{
2006 Expression expr = null;
2007 List<Expression> list = null;
2008 List<Expression> exprList = new ArrayList<Expression>();
2009}
2010{
2011 (
2012 expr = Expression() { exprList.add(expr); }
2013 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
2014 )?
2015 (LOOKAHEAD(1) Comma())?
2016 {
2017 return exprList;
2018 }
2019}
2020
2021void Comma():
2022{}
2023{
2024 <COMMA>
2025}
2026
2027RecordConstructor RecordConstructor() throws ParseException:
2028{
2029 RecordConstructor expr = new RecordConstructor();
2030 FieldBinding tmp = null;
2031 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2032}
2033{
2034 <LEFTBRACE> (tmp = FieldBinding()
2035 {
2036 fbList.add(tmp);
2037 }
2038 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2039 {
2040 expr.setFbList(fbList);
2041 return expr;
2042 }
2043}
2044
2045FieldBinding FieldBinding() throws ParseException:
2046{
2047 FieldBinding fb = new FieldBinding();
2048 Expression left, right;
2049}
2050{
2051 left = Expression() <COLON> right = Expression()
2052 {
2053 fb.setLeftExpr(left);
2054 fb.setRightExpr(right);
2055 return fb;
2056 }
2057}
2058
2059
2060Expression FunctionCallExpr() throws ParseException:
2061{
2062 CallExpr callExpr;
2063 List<Expression> argList = new ArrayList<Expression>();
2064 Expression tmp;
2065 int arity = 0;
2066 FunctionName funcName = null;
2067 String hint = null;
2068}
2069{
2070 funcName = FunctionName()
2071 {
2072 hint = funcName.hint;
2073 }
2074 <LEFTPAREN> (tmp = Expression()
2075 {
2076 argList.add(tmp);
2077 arity ++;
2078 }
2079 (<COMMA> tmp = Expression()
2080 {
2081 argList.add(tmp);
2082 arity++;
2083 }
2084 )*)? <RIGHTPAREN>
2085 {
2086 // TODO use funcName.library
2087 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2088 FunctionSignature signature
2089 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2090 if (signature == null) {
2091 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2092 }
2093 callExpr = new CallExpr(signature,argList);
2094 if (hint != null) {
2095 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2096 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2097 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2098 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2099 }
2100 }
2101 return callExpr;
2102 }
2103}
2104
2105
2106Expression DatasetAccessExpression() throws ParseException:
2107{
2108 String funcName;
2109 String arg1 = null;
2110 String arg2 = null;
2111 Expression nameArg;
2112}
2113{
2114 <DATASET>
2115 {
2116 funcName = token.image;
2117 }
2118 ( ( arg1 = Identifier() ( <DOT> arg2 = Identifier() )? )
2119 {
2120 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
2121 LiteralExpr ds = new LiteralExpr();
2122 ds.setValue( new StringLiteral(name) );
2123 nameArg = ds;
2124 if(arg2 != null){
2125 addDataverse(arg1.toString());
2126 addDataset(name);
2127 } else {
2128 addDataset(defaultDataverse + "." + name);
2129 }
2130 }
2131 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
2132 {
2133 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
2134 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
2135 if (signature == null) {
2136 signature = new FunctionSignature(dataverse, funcName, 1);
2137 }
2138 List<Expression> argList = new ArrayList<Expression>();
2139 argList.add(nameArg);
2140 return new CallExpr(signature, argList);
2141 }
2142}
2143
2144Expression ParenthesizedExpression() throws ParseException:
2145{
2146 Expression expr;
2147}
2148{
2149 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2150 {
2151 return expr;
2152 }
2153}
2154
2155Expression IfThenElse() throws ParseException:
2156{
2157 Expression condExpr;
2158 Expression thenExpr;
2159 Expression elseExpr;
2160 IfExpr ifExpr = new IfExpr();
2161}
2162{
2163 <IF> <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> <THEN> thenExpr = Expression() <ELSE> elseExpr = Expression()
2164
2165 {
2166 ifExpr.setCondExpr(condExpr);
2167 ifExpr.setThenExpr(thenExpr);
2168 ifExpr.setElseExpr(elseExpr);
2169 return ifExpr;
2170 }
2171}
2172
2173Expression FLWOGR() throws ParseException:
2174{
2175 FLWOGRExpression flworg = new FLWOGRExpression();
2176 List<Clause> clauseList = new ArrayList<Clause>();
2177 Expression returnExpr;
2178 Clause tmp;
2179 createNewScope();
2180}
2181{
2182 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2183 (tmp = Clause() {clauseList.add(tmp);})* (<RETURN>|<SELECT>) returnExpr = Expression()
2184
2185 {
2186 flworg.setClauseList(clauseList);
2187 flworg.setReturnExpr(returnExpr);
2188 removeCurrentScope();
2189 return flworg;
2190 }
2191}
2192
2193Clause Clause()throws ParseException :
2194{
2195 Clause clause;
2196}
2197{
2198 (
2199 clause = ForClause()
2200 | clause = LetClause()
2201 | clause = WhereClause()
2202 | clause = OrderbyClause()
2203 | clause = GroupClause()
2204 | clause = LimitClause()
2205 | clause = DistinctClause()
2206 )
2207 {
2208 return clause;
2209 }
2210}
2211
2212Clause ForClause()throws ParseException :
2213{
2214 ForClause fc = new ForClause();
2215 VariableExpr varExp;
2216 VariableExpr varPos = null;
2217 Expression inExp;
2218 extendCurrentScope();
2219}
2220{
2221 (<FOR>|<FROM>) varExp = Variable() (<AT> varPos = Variable())? <IN> ( inExp = Expression() )
2222 {
2223 fc.setVarExpr(varExp);
2224 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2225 fc.setInExpr(inExp);
2226 if (varPos != null) {
2227 fc.setPosExpr(varPos);
2228 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2229 }
2230 return fc;
2231 }
2232}
2233
2234Clause LetClause() throws ParseException:
2235{
2236 LetClause lc = new LetClause();
2237 VariableExpr varExp;
2238 Expression beExp;
2239 extendCurrentScope();
2240}
2241{
2242 (<LET>|<WITH>) varExp = Variable() <ASSIGN> beExp = Expression()
2243 {
2244 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2245 lc.setVarExpr(varExp);
2246 lc.setBindingExpr(beExp);
2247 return lc;
2248 }
2249}
2250
2251Clause WhereClause()throws ParseException :
2252{
2253 WhereClause wc = new WhereClause();
2254 Expression whereExpr;
2255}
2256{
2257 <WHERE> whereExpr = Expression()
2258 {
2259 wc.setWhereExpr(whereExpr);
2260 return wc;
2261 }
2262}
2263
2264Clause OrderbyClause()throws ParseException :
2265{
2266 OrderbyClause oc = new OrderbyClause();
2267 Expression orderbyExpr;
2268 List<Expression> orderbyList = new ArrayList<Expression>();
2269 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2270 int numOfOrderby = 0;
2271}
2272{
2273 (
2274 <ORDER>
2275 {
2276 String hint = getHint(token);
2277 if (hint != null) {
2278 if (hint.startsWith(INMEMORY_HINT)) {
2279 String splits[] = hint.split(" +");
2280 int numFrames = Integer.parseInt(splits[1]);
2281 int numTuples = Integer.parseInt(splits[2]);
2282 oc.setNumFrames(numFrames);
2283 oc.setNumTuples(numTuples);
2284 }
2285 if (hint.startsWith(RANGE_HINT)) {
2286 try{
2287 oc.setRangeMap(RangeMapBuilder.parseHint(hint.substring(RANGE_HINT.length())));
2288 } catch (AsterixException e) {
2289 throw new ParseException(e.getMessage());
2290 }
2291 }
2292 }
2293 }
2294 <BY> orderbyExpr = Expression()
2295 {
2296 orderbyList.add(orderbyExpr);
2297 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2298 }
2299 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2300 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2301 {
2302 modifierList.add(modif);
2303 }
2304
2305 (<COMMA> orderbyExpr = Expression()
2306 {
2307 orderbyList.add(orderbyExpr);
2308 modif = OrderbyClause.OrderModifier.ASC;
2309 }
2310 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2311 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2312 {
2313 modifierList.add(modif);
2314 }
2315 )*
2316)
2317 {
2318 oc.setModifierList(modifierList);
2319 oc.setOrderbyList(orderbyList);
2320 return oc;
2321 }
2322}
2323Clause GroupClause()throws ParseException :
2324{
2325 GroupbyClause gbc = new GroupbyClause();
2326 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2327 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2328 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
2329 List<VariableExpr> withVarList= new ArrayList<VariableExpr>();
2330 VariableExpr var = null;
2331 VariableExpr withVar = null;
2332 Expression expr = null;
2333 VariableExpr decorVar = null;
2334 Expression decorExpr = null;
2335}
2336{
2337 {
2338 Scope newScope = extendCurrentScopeNoPush(true);
2339 // extendCurrentScope(true);
2340 }
2341 <GROUP>
2342 {
2343 String hint = getHint(token);
2344 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2345 gbc.setHashGroupByHint(true);
2346 }
2347 }
2348 <BY> (LOOKAHEAD(2) var = Variable()
2349 {
2350 newScope.addNewVarSymbolToScope(var.getVar());
2351 } <ASSIGN>)?
2352 expr = Expression()
2353 {
2354 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2355 vePairList.add(pair1);
2356 }
2357 (<COMMA> ( LOOKAHEAD(2) var = Variable()
2358 {
2359 newScope.addNewVarSymbolToScope(var.getVar());
2360 } <ASSIGN>)?
2361 expr = Expression()
2362 {
2363 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2364 vePairList.add(pair2);
2365 }
2366 )*
2367 (<DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2368 {
2369 newScope.addNewVarSymbolToScope(decorVar.getVar());
2370 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2371 decorPairList.add(pair3);
2372 }
2373 (<COMMA> <DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2374 {
2375 newScope.addNewVarSymbolToScope(decorVar.getVar());
2376 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2377 decorPairList.add(pair4);
2378 }
2379 )*
2380 )?
2381 (<WITH>|<KEEPING>) withVar = VariableRef()
2382 {
2383 if(withVar.getIsNewVar()==true)
2384 throw new ParseException("can't find variable " + withVar.getVar());
2385 withVarList.add(withVar);
2386 newScope.addNewVarSymbolToScope(withVar.getVar());
2387 }
2388 (<COMMA> withVar = VariableRef()
2389 {
2390 if(withVar.getIsNewVar()==true)
2391 throw new ParseException("can't find variable " + withVar.getVar());
2392 withVarList.add(withVar);
2393 newScope.addNewVarSymbolToScope(withVar.getVar());
2394 })*
2395 {
2396 gbc.setGbyPairList(vePairList);
2397 gbc.setDecorPairList(decorPairList);
2398 gbc.setWithVarList(withVarList);
2399 replaceCurrentScope(newScope);
2400 return gbc;
2401 }
2402}
2403
2404
2405LimitClause LimitClause() throws ParseException:
2406{
2407 LimitClause lc = new LimitClause();
2408 Expression expr;
2409 pushForbiddenScope(getCurrentScope());
2410}
2411{
2412 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
2413 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
2414
2415 {
2416 popForbiddenScope();
2417 return lc;
2418 }
2419}
2420
2421DistinctClause DistinctClause() throws ParseException:
2422{
2423 List<Expression> exprs = new ArrayList<Expression>();
2424 Expression expr;
2425}
2426{
2427 <DISTINCT> <BY> expr = Expression()
2428 {
2429 exprs.add(expr);
2430 }
2431 (<COMMA> expr = Expression()
2432 {
2433 exprs.add(expr);
2434 }
2435 )*
2436 {
2437 return new DistinctClause(exprs);
2438 }
2439}
2440
2441QuantifiedExpression QuantifiedExpression()throws ParseException:
2442{
2443 QuantifiedExpression qc = new QuantifiedExpression();
2444 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2445 Expression satisfiesExpr;
2446 VariableExpr var;
2447 Expression inExpr;
2448 QuantifiedPair pair;
2449}
2450{
2451 {
2452 createNewScope();
2453 }
2454
2455 ( (<SOME> { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2456 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
2457 var = Variable() <IN> inExpr = Expression()
2458 {
2459 pair = new QuantifiedPair(var, inExpr);
2460 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2461 quantifiedList.add(pair);
2462 }
2463 (
2464 <COMMA> var = Variable() <IN> inExpr = Expression()
2465 {
2466 pair = new QuantifiedPair(var, inExpr);
2467 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2468 quantifiedList.add(pair);
2469 }
2470 )*
2471 <SATISFIES> satisfiesExpr = Expression()
2472 {
2473 qc.setSatisfiesExpr(satisfiesExpr);
2474 qc.setQuantifiedList(quantifiedList);
2475 removeCurrentScope();
2476 return qc;
2477 }
2478}
2479
2480TOKEN_MGR_DECLS:
2481{
2482 public int commentDepth = 0;
2483 public IntStack lexerStateStack = new IntStack();
2484
2485 public void pushState() {
2486 lexerStateStack.push( curLexState );
2487 }
2488
2489 public void popState(String token) {
2490 if (lexerStateStack.size() > 0) {
2491 SwitchTo( lexerStateStack.pop() );
2492 } else {
2493 int errorLine = input_stream.getEndLine();
2494 int errorColumn = input_stream.getEndColumn();
2495 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
2496 + "\" but state stack is empty.";
2497 throw new TokenMgrError(msg, -1);
2498 }
2499 }
2500}
2501
2502<DEFAULT,IN_DBL_BRACE>
2503TOKEN :
2504{
2505 <ASC : "asc">
2506 | <AT : "at">
2507 | <BY : "by">
2508 | <DATASET : "dataset">
2509 | <DECOR : "decor">
2510 | <DESC : "desc">
2511 | <DISTINCT : "distinct">
2512 | <ELSE : "else">
2513 | <EVERY : "every">
2514 | <FOR : "for">
2515 | <FROM : "from">
2516 | <GROUP : "group">
2517 | <IF : "if">
2518 | <IN : "in">
2519 | <LET : "let">
2520 | <LIMIT : "limit">
2521 | <OFFSET : "offset">
2522 | <ORDER : "order">
2523 | <RETURN : "return">
2524 | <SATISFIES : "satisfies">
2525 | <SELECT : "select">
2526 | <SOME : "some">
2527 | <THEN : "then">
2528 | <UNION : "union">
2529 | <WHERE : "where">
2530 | <WITH : "with">
2531 | <KEEPING : "keeping">
2532}
2533
2534<DEFAULT,IN_DBL_BRACE>
2535TOKEN :
2536{
2537 <CARET : "^">
2538 | <DIV : "/">
2539 | <IDIV : "idiv">
2540 | <MINUS : "-">
2541 | <MOD : "%">
2542 | <MUL : "*">
2543 | <PLUS : "+">
2544
2545 | <LEFTPAREN : "(">
2546 | <RIGHTPAREN : ")">
2547 | <LEFTBRACKET : "[">
2548 | <RIGHTBRACKET : "]">
2549
2550 | <COLON : ":">
2551 | <COMMA : ",">
2552 | <DOT : ".">
2553 | <QUES : "?">
2554
2555 | <LT : "<">
2556 | <GT : ">">
2557 | <LE : "<=">
2558 | <GE : ">=">
2559 | <EQ : "=">
2560 | <NE : "!=">
2561 | <SIMILAR : "~=">
2562 | <ASSIGN : ":=">
2563
2564 | <AND : "and">
2565 | <OR : "or">
2566}
2567
2568<DEFAULT,IN_DBL_BRACE>
2569TOKEN :
2570{
2571 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
2572}
2573
2574<DEFAULT>
2575TOKEN :
2576{
2577 <RIGHTBRACE : "}"> { popState("}"); }
2578}
2579
2580<DEFAULT,IN_DBL_BRACE>
2581TOKEN :
2582{
2583 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
2584}
2585
2586<IN_DBL_BRACE>
2587TOKEN :
2588{
2589 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
2590}
2591
2592<DEFAULT,IN_DBL_BRACE>
2593TOKEN :
2594{
2595 <INTEGER_LITERAL : (<DIGIT>)+ >
2596}
2597
2598<DEFAULT,IN_DBL_BRACE>
2599TOKEN :
2600{
2601 <NULL : "null">
2602 | <TRUE : "true">
2603 | <FALSE : "false">
2604}
2605
2606<DEFAULT,IN_DBL_BRACE>
2607TOKEN :
2608{
2609 <#DIGIT : ["0" - "9"]>
2610}
2611
2612<DEFAULT,IN_DBL_BRACE>
2613TOKEN:
2614{
2615 < DOUBLE_LITERAL: <DIGITS>
2616 | <DIGITS> ( "." <DIGITS> )?
2617 | "." <DIGITS>
2618 >
2619 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
2620 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
2621 | "." <DIGITS> ( "f" | "F" )
2622 >
2623 | <DIGITS : (<DIGIT>)+ >
2624}
2625
2626<DEFAULT,IN_DBL_BRACE>
2627TOKEN :
2628{
2629 <#LETTER : ["A" - "Z", "a" - "z"]>
2630 | <SPECIALCHARS : ["$", "_", "-"]>
2631}
2632
2633<DEFAULT,IN_DBL_BRACE>
2634TOKEN :
2635{
2636 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
2637 <STRING_LITERAL : ("\"" (
2638 <EscapeQuot>
2639 | <EscapeBslash>
2640 | <EscapeSlash>
2641 | <EscapeBspace>
2642 | <EscapeFormf>
2643 | <EscapeNl>
2644 | <EscapeCr>
2645 | <EscapeTab>
2646 | ~["\"","\\"])* "\"")
2647 | ("\'"(
2648 <EscapeApos>
2649 | <EscapeBslash>
2650 | <EscapeSlash>
2651 | <EscapeBspace>
2652 | <EscapeFormf>
2653 | <EscapeNl>
2654 | <EscapeCr>
2655 | <EscapeTab>
2656 | ~["\'","\\"])* "\'")>
2657 | < #EscapeQuot: "\\\"" >
2658 | < #EscapeApos: "\\\'" >
2659 | < #EscapeBslash: "\\\\" >
2660 | < #EscapeSlash: "\\/" >
2661 | < #EscapeBspace: "\\b" >
2662 | < #EscapeFormf: "\\f" >
2663 | < #EscapeNl: "\\n" >
2664 | < #EscapeCr: "\\r" >
2665 | < #EscapeTab: "\\t" >
2666}
2667
2668<DEFAULT,IN_DBL_BRACE>
2669TOKEN :
2670{
2671 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2672}
2673
2674<DEFAULT,IN_DBL_BRACE>
2675TOKEN :
2676{
2677 <VARIABLE : "$" <LETTER> (<LETTER> | <DIGIT> | "_")*>
2678}
2679
2680<DEFAULT,IN_DBL_BRACE>
2681SKIP:
2682{
2683 " "
2684 | "\t"
2685 | "\r"
2686 | "\n"
2687}
2688
2689<DEFAULT,IN_DBL_BRACE>
2690SKIP:
2691{
2692 <"//" (~["\n"])* "\n">
2693}
2694
2695<DEFAULT,IN_DBL_BRACE>
2696SKIP:
2697{
2698 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2699}
2700
2701<DEFAULT,IN_DBL_BRACE>
2702SKIP:
2703{
2704 <"/*"> { pushState(); } : INSIDE_COMMENT
2705}
2706
2707<INSIDE_COMMENT>
2708SPECIAL_TOKEN:
2709{
2710 <"+"(" ")*(~["*"])*>
2711}
2712
2713<INSIDE_COMMENT>
2714SKIP:
2715{
2716 <"/*"> { pushState(); }
2717}
2718
2719<INSIDE_COMMENT>
2720SKIP:
2721{
2722 <"*/"> { popState("*/"); }
2723 | <~[]>
2724}