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