blob: d496027b69bf18bee0387d1ee5bf48d4b587bd19 [file] [log] [blame]
Yingyi Bu391f09e2015-10-29 13:49:39 -07001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(SQLPPParser)
10
11package org.apache.asterix.lang.sqlpp.parser;
12
13// For SQL++ ParserTokenManager
14import org.apache.xerces.util.IntStack;
15
16import java.io.BufferedReader;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.io.Reader;
23import java.io.StringReader;
24import java.util.ArrayList;
Yingyi Budaa549c2016-06-28 22:30:52 -070025import java.util.Collections;
Yingyi Bu391f09e2015-10-29 13:49:39 -070026import java.util.HashMap;
27import java.util.Iterator;
28import java.util.LinkedHashMap;
29import java.util.List;
30import java.util.Map;
31
32import org.apache.asterix.common.annotations.AutoDataGen;
33import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
34import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
35import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
36import org.apache.asterix.common.annotations.FieldIntervalDataGen;
37import org.apache.asterix.common.annotations.FieldValFileDataGen;
38import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
39import org.apache.asterix.common.annotations.IRecordFieldDataGen;
40import org.apache.asterix.common.annotations.InsertRandIntDataGen;
41import org.apache.asterix.common.annotations.ListDataGen;
42import org.apache.asterix.common.annotations.ListValFileDataGen;
43import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
44import org.apache.asterix.common.annotations.TypeDataGen;
45import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
46import org.apache.asterix.common.config.DatasetConfig.DatasetType;
47import org.apache.asterix.common.config.DatasetConfig.IndexType;
Abdullah Alamoudie800e6d2016-01-14 11:20:55 +030048import org.apache.asterix.common.config.MetadataConstants;
Yingyi Bu391f09e2015-10-29 13:49:39 -070049import org.apache.asterix.common.exceptions.AsterixException;
50import org.apache.asterix.common.functions.FunctionSignature;
51import org.apache.asterix.lang.common.base.Expression;
52import org.apache.asterix.lang.common.base.Literal;
53import org.apache.asterix.lang.common.base.IParser;
54import org.apache.asterix.lang.common.base.Statement;
55import org.apache.asterix.lang.common.clause.GroupbyClause;
56import org.apache.asterix.lang.common.clause.LetClause;
57import org.apache.asterix.lang.common.clause.LimitClause;
58import org.apache.asterix.lang.common.clause.OrderbyClause;
59import org.apache.asterix.lang.common.clause.UpdateClause;
60import org.apache.asterix.lang.common.clause.WhereClause;
61import org.apache.asterix.lang.common.context.RootScopeFactory;
62import org.apache.asterix.lang.common.context.Scope;
63import org.apache.asterix.lang.common.expression.AbstractAccessor;
64import org.apache.asterix.lang.common.expression.CallExpr;
65import org.apache.asterix.lang.common.expression.FieldAccessor;
66import org.apache.asterix.lang.common.expression.FieldBinding;
67import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
68import org.apache.asterix.lang.common.expression.IfExpr;
69import org.apache.asterix.lang.common.expression.IndexAccessor;
70import org.apache.asterix.lang.common.expression.ListConstructor;
71import org.apache.asterix.lang.common.expression.LiteralExpr;
72import org.apache.asterix.lang.common.expression.OperatorExpr;
73import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
74import org.apache.asterix.lang.common.expression.QuantifiedExpression;
75import org.apache.asterix.lang.common.expression.RecordConstructor;
76import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
77import org.apache.asterix.lang.common.expression.TypeExpression;
78import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
79import org.apache.asterix.lang.common.expression.UnaryExpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -070080import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
81import org.apache.asterix.lang.common.expression.VariableExpr;
82import org.apache.asterix.lang.common.literal.DoubleLiteral;
83import org.apache.asterix.lang.common.literal.FalseLiteral;
84import org.apache.asterix.lang.common.literal.FloatLiteral;
85import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
Yingyi Bu535d86b2016-05-23 16:44:25 -070086import org.apache.asterix.lang.common.literal.MissingLiteral;
Yingyi Bu391f09e2015-10-29 13:49:39 -070087import org.apache.asterix.lang.common.literal.NullLiteral;
88import org.apache.asterix.lang.common.literal.StringLiteral;
89import org.apache.asterix.lang.common.literal.TrueLiteral;
90import org.apache.asterix.lang.common.parser.ScopeChecker;
91import org.apache.asterix.lang.common.statement.CompactStatement;
92import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
93import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
94import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
95import org.apache.asterix.lang.common.statement.CreateFeedStatement;
96import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
97import org.apache.asterix.lang.common.statement.CreateIndexStatement;
98import org.apache.asterix.lang.common.statement.CreatePrimaryFeedStatement;
99import org.apache.asterix.lang.common.statement.CreateSecondaryFeedStatement;
100import org.apache.asterix.lang.common.statement.DatasetDecl;
101import org.apache.asterix.lang.common.statement.DataverseDecl;
102import org.apache.asterix.lang.common.statement.DataverseDropStatement;
103import org.apache.asterix.lang.common.statement.DeleteStatement;
104import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
105import org.apache.asterix.lang.common.statement.DropStatement;
106import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
107import org.apache.asterix.lang.common.statement.FeedDropStatement;
108import org.apache.asterix.lang.common.statement.FunctionDecl;
109import org.apache.asterix.lang.common.statement.FunctionDropStatement;
110import org.apache.asterix.lang.common.statement.IndexDropStatement;
111import org.apache.asterix.lang.common.statement.InsertStatement;
112import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
113import org.apache.asterix.lang.common.statement.LoadStatement;
114import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
115import org.apache.asterix.lang.common.statement.NodegroupDecl;
116import org.apache.asterix.lang.common.statement.Query;
117import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
118import org.apache.asterix.lang.common.statement.RunStatement;
119import org.apache.asterix.lang.common.statement.SetStatement;
120import org.apache.asterix.lang.common.statement.TypeDecl;
121import org.apache.asterix.lang.common.statement.TypeDropStatement;
122import org.apache.asterix.lang.common.statement.UpdateStatement;
123import org.apache.asterix.lang.common.statement.WriteStatement;
124import org.apache.asterix.lang.common.struct.Identifier;
125import org.apache.asterix.lang.common.struct.QuantifiedPair;
126import org.apache.asterix.lang.common.struct.VarIdentifier;
127import org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause;
128import org.apache.asterix.lang.sqlpp.clause.FromClause;
129import org.apache.asterix.lang.sqlpp.clause.FromTerm;
130import org.apache.asterix.lang.sqlpp.clause.HavingClause;
131import org.apache.asterix.lang.sqlpp.clause.JoinClause;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700132import org.apache.asterix.lang.sqlpp.clause.Projection;
133import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
134import org.apache.asterix.lang.sqlpp.clause.SelectClause;
135import org.apache.asterix.lang.sqlpp.clause.SelectElement;
136import org.apache.asterix.lang.sqlpp.clause.SelectRegular;
137import org.apache.asterix.lang.sqlpp.clause.SelectSetOperation;
138import org.apache.asterix.lang.sqlpp.clause.UnnestClause;
Yingyi Buc8c067c2016-07-25 23:37:19 -0700139import org.apache.asterix.lang.sqlpp.expression.CaseExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700140import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
141import org.apache.asterix.lang.sqlpp.optype.JoinType;
142import org.apache.asterix.lang.sqlpp.optype.SetOpType;
143import org.apache.asterix.lang.sqlpp.struct.SetOperationInput;
144import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
Yingyi Bu9e3f9be2016-07-01 10:07:37 -0700145import org.apache.asterix.lang.sqlpp.util.ExpressionToVariableUtil;
Yingyi Buacc12a92016-03-26 17:25:05 -0700146import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700147import org.apache.hyracks.algebricks.common.utils.Pair;
148import org.apache.hyracks.algebricks.common.utils.Triple;
149import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
150import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
151import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
152
153
154
Yingyi Bucaea8f02015-11-16 15:12:15 -0800155class SQLPPParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700156
157 // optimizer hints
158 private static final String AUTO_HINT = "auto";
159 private static final String BROADCAST_JOIN_HINT = "bcast";
160 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
161 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
162 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
163 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
164 private static final String HASH_GROUP_BY_HINT = "hash";
165 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
166 private static final String INMEMORY_HINT = "inmem";
167 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
168 private static final String INTERVAL_HINT = "interval";
169 private static final String LIST_HINT = "list";
170 private static final String LIST_VAL_FILE_HINT = "list-val-file";
171 private static final String RANGE_HINT = "range";
172 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
173 private static final String VAL_FILE_HINT = "val-files";
174 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
175
176 private static final String GEN_FIELDS_HINT = "gen-fields";
177
178 // data generator hints
179 private static final String DGEN_HINT = "dgen";
180
181 private static class IndexParams {
182 public IndexType type;
183 public int gramLength;
184
185 public IndexParams(IndexType type, int gramLength) {
186 this.type = type;
187 this.gramLength = gramLength;
188 }
189 };
190
191 private static class FunctionName {
192 public String dataverse = null;
193 public String library = null;
194 public String function = null;
195 public String hint = null;
196 }
197
198 private static String getHint(Token t) {
199 if (t.specialToken == null) {
200 return null;
201 }
202 String s = t.specialToken.image;
203 int n = s.length();
204 if (n < 2) {
205 return null;
206 }
207 return s.substring(1).trim();
208 }
209
210 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
211 IRecordFieldDataGen rfdg = null;
212 String splits[] = hint.split(" +");
213 if (splits[0].equals(VAL_FILE_HINT)) {
214 File[] valFiles = new File[splits.length - 1];
215 for (int k=1; k<splits.length; k++) {
216 valFiles[k-1] = new File(splits[k]);
217 }
218 rfdg = new FieldValFileDataGen(valFiles);
219 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
220 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
221 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
222 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
223 } else if (splits[0].equals(LIST_HINT)) {
224 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
225 } else if (splits[0].equals(INTERVAL_HINT)) {
226 FieldIntervalDataGen.ValueType vt;
227 if (splits[1].equals("int")) {
228 vt = FieldIntervalDataGen.ValueType.INT;
229 } else if (splits[1].equals("long")) {
230 vt = FieldIntervalDataGen.ValueType.LONG;
231 } else if (splits[1].equals("float")) {
232 vt = FieldIntervalDataGen.ValueType.FLOAT;
233 } else if (splits[1].equals("double")) {
234 vt = FieldIntervalDataGen.ValueType.DOUBLE;
235 } else {
236 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
237 }
238 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
239 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
240 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
241 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
242 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
243 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
244 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
245 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
246 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
247 } else if (splits[0].equals(AUTO_HINT)) {
248 rfdg = new AutoDataGen(splits[1]);
249 }
250 return rfdg;
251 }
252
253 public SQLPPParser(String s){
254 this(new StringReader(s));
255 super.setInput(s);
256 }
257
258 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
259 File file = new File(args[0]);
260 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
261 SQLPPParser parser = new SQLPPParser(fis);
262 List<Statement> st = parser.parse();
263 //st.accept(new SQLPPPrintVisitor(), 0);
264 }
265
266 public List<Statement> parse() throws AsterixException {
267 try {
268 return Statement();
269 } catch (Error e) {
270 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
271 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
272 throw new AsterixException(new ParseException(e.getMessage()));
273 } catch (ParseException e) {
274 throw new AsterixException(e.getMessage());
275 }
276 }
277}
278
279PARSER_END(SQLPPParser)
280
281
282List<Statement> Statement() throws ParseException:
283{
284 scopeStack.push(RootScopeFactory.createRootScope(this));
285 List<Statement> decls = new ArrayList<Statement>();
286 Statement stmt = null;
287}
288{
289 ( stmt = SingleStatement() (<SEMICOLON>)*
290 {
291 decls.add(stmt);
292 }
293 )*
294 <EOF>
295 {
296 return decls;
297 }
298}
299
300Statement SingleStatement() throws ParseException:
301{
302 Statement stmt = null;
303}
304{
305 (
306 stmt = DataverseDeclaration()
307 | stmt = FunctionDeclaration()
308 | stmt = CreateStatement()
309 | stmt = LoadStatement()
310 | stmt = DropStatement()
311 | stmt = WriteStatement()
312 | stmt = SetStatement()
313 | stmt = InsertStatement()
314 | stmt = DeleteStatement()
315 | stmt = UpdateStatement()
316 | stmt = FeedStatement()
317 | stmt = CompactStatement()
318 | stmt = Query() <SEMICOLON>
319 | stmt = RefreshExternalDatasetStatement()
320 | stmt = RunStatement()
321 )
322 {
323 return stmt;
324 }
325}
326
327DataverseDecl DataverseDeclaration() throws ParseException:
328{
329 String dvName = null;
330}
331{
332 <USE> dvName = Identifier()
333 {
334 defaultDataverse = dvName;
335 return new DataverseDecl(new Identifier(dvName));
336 }
337}
338
339Statement CreateStatement() throws ParseException:
340{
341 String hint = null;
342 boolean dgen = false;
343 Statement stmt = null;
344}
345{
346 <CREATE>
347 (
348 {
349 hint = getHint(token);
350 if (hint != null && hint.startsWith(DGEN_HINT)) {
351 dgen = true;
352 }
353 }
354 stmt = TypeSpecification(hint, dgen)
355 | stmt = NodegroupSpecification()
356 | stmt = DatasetSpecification()
357 | stmt = IndexSpecification()
358 | stmt = DataverseSpecification()
359 | stmt = FunctionSpecification()
360 | stmt = FeedSpecification()
361 | stmt = FeedPolicySpecification()
362 )
363 {
364 return stmt;
365 }
366}
367
368TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
369{
370 Pair<Identifier,Identifier> nameComponents = null;
371 boolean ifNotExists = false;
372 TypeExpression typeExpr = null;
373}
374{
375 <TYPE> nameComponents = TypeName() ifNotExists = IfNotExists()
376 <AS> typeExpr = TypeExpr()
377 {
378 long numValues = -1;
379 String filename = null;
380 if (dgen) {
381 String splits[] = hint.split(" +");
382 if (splits.length != 3) {
383 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
384 }
385 filename = splits[1];
386 numValues = Long.parseLong(splits[2]);
387 }
388 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
389 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
390 }
391}
392
393
394NodegroupDecl NodegroupSpecification() throws ParseException:
395{
396 String name = null;
397 String tmp = null;
398 boolean ifNotExists = false;
399 List<Identifier>ncNames = null;
400}
401{
402 <NODEGROUP> name = Identifier()
403 ifNotExists = IfNotExists() <ON> tmp = Identifier()
404 {
405 ncNames = new ArrayList<Identifier>();
406 ncNames.add(new Identifier(tmp));
407 }
408 ( <COMMA> tmp = Identifier()
409 {
410 ncNames.add(new Identifier(tmp));
411 }
412 )*
413 {
414 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
415 }
416}
417
418DatasetDecl DatasetSpecification() throws ParseException:
419{
420 Pair<Identifier,Identifier> nameComponents = null;
421 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800422 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700423 String adapterName = null;
424 Map<String,String> properties = null;
425 Map<String,String> compactionPolicyProperties = null;
426 FunctionSignature appliedFunction = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800427 Pair<List<Integer>, List<List<String>>> primaryKeyFields = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700428 String nodeGroupName = null;
429 Map<String,String> hints = new HashMap<String,String>();
430 DatasetDecl dsetDecl = null;
431 boolean autogenerated = false;
432 String compactionPolicy = null;
433 boolean temp = false;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800434 Pair<Integer, List<String>> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800435 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700436}
437{
438 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700439 <EXTERNAL> Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800440 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700441 ifNotExists = IfNotExists()
442 <USING> adapterName = AdapterName() properties = Configuration()
443 (<ON> nodeGroupName = Identifier() )?
444 ( <HINTS> hints = Properties() )?
445 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (LOOKAHEAD(1) compactionPolicyProperties = Configuration())? )?
446 {
447 ExternalDetailsDecl edd = new ExternalDetailsDecl();
448 edd.setAdapter(adapterName);
449 edd.setProperties(properties);
450 dsetDecl = new DatasetDecl(nameComponents.first,
451 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800452 typeComponents.first,
453 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800454 metaTypeComponents.first,
455 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700456 nodeGroupName != null? new Identifier(nodeGroupName): null,
457 compactionPolicy,
458 compactionPolicyProperties,
459 hints,
460 DatasetType.EXTERNAL,
461 edd,
462 ifNotExists);
463 }
464
Yingyi Bub9169b62016-02-26 21:21:49 -0800465 | (<INTERNAL> | <TEMPORARY> { temp = true; })?
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700466 Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800467 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800468 (
469 { String name; }
470 <WITH>
471 name = Identifier()
472 {
473 if(!name.toLowerCase().equals("meta")){
474 throw new ParseException("We can only support one additional associated field called \"meta\".");
475 }
476 }
477 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
478 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700479 ifNotExists = IfNotExists()
480 primaryKeyFields = PrimaryKey()
481 (<AUTOGENERATED> { autogenerated = true; } )?
482 (<ON> nodeGroupName = Identifier() )?
483 ( <HINTS> hints = Properties() )?
484 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (LOOKAHEAD(1) compactionPolicyProperties = Configuration())? )?
485 ( LOOKAHEAD(2) <WITH> <FILTER> <ON> filterField = NestedField() )?
486 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800487 if(filterField!=null && filterField.first!=0){
488 throw new ParseException("A filter field can only be a field in the main record of the dataset.");
489 }
490 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields.second,
491 primaryKeyFields.first,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700492 autogenerated,
Yingyi Buc9bfe252016-03-01 00:02:40 -0800493 filterField == null? null : filterField.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700494 temp);
495 dsetDecl = new DatasetDecl(nameComponents.first,
496 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800497 typeComponents.first,
498 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800499 metaTypeComponents.first,
500 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700501 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
502 compactionPolicy,
503 compactionPolicyProperties,
504 hints,
505 DatasetType.INTERNAL,
506 idd,
507 ifNotExists);
508 }
509 )
510 {
511 return dsetDecl;
512 }
513}
514
515RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
516{
517 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
518 Pair<Identifier,Identifier> nameComponents = null;
519 String datasetName = null;
520}
521{
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700522 <REFRESH> <EXTERNAL> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700523 {
524 redss.setDataverseName(nameComponents.first);
525 redss.setDatasetName(nameComponents.second);
526 return redss;
527 }
528}
529
530RunStatement RunStatement() throws ParseException:
531{
532 String system = null;
533 String tmp;
534 ArrayList<String> parameters = new ArrayList<String>();
535 Pair<Identifier,Identifier> nameComponentsFrom = null;
536 Pair<Identifier,Identifier> nameComponentsTo = null;
537}
538{
539 <RUN> system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
540 {
541 parameters.add(tmp);
542 }
543 )*<RIGHTPAREN>
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700544 <FROM> Dataset() nameComponentsFrom = QualifiedName()
545 <TO> Dataset() nameComponentsTo = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700546 {
547 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
548 }
549}
550
551CreateIndexStatement IndexSpecification() throws ParseException:
552{
553 CreateIndexStatement cis = new CreateIndexStatement();
554 String indexName = null;
555 boolean ifNotExists = false;
556 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800557 Pair<Integer, Pair<List<String>, TypeExpression>> fieldPair = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700558 IndexParams indexType = null;
559 boolean enforced = false;
560}
561{
562 <INDEX> indexName = Identifier()
563 ifNotExists = IfNotExists()
564 <ON> nameComponents = QualifiedName()
565 <LEFTPAREN> ( fieldPair = OpenField()
566 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800567 cis.addFieldExprPair(fieldPair.second);
568 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700569 }
570 ) (<COMMA> fieldPair = OpenField()
571 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800572 cis.addFieldExprPair(fieldPair.second);
573 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700574 }
575 )* <RIGHTPAREN> ( <TYPE> indexType = IndexType() )? ( <ENFORCED> { enforced = true; } )?
576 {
577 cis.setIndexName(new Identifier(indexName));
578 cis.setIfNotExists(ifNotExists);
579 cis.setDataverseName(nameComponents.first);
580 cis.setDatasetName(nameComponents.second);
581 if (indexType != null) {
582 cis.setIndexType(indexType.type);
583 cis.setGramLength(indexType.gramLength);
584 }
585 cis.setEnforced(enforced);
586 return cis;
587 }
588}
589
590String CompactionPolicy() throws ParseException :
591{
592 String compactionPolicy = null;
593}
594{
595 compactionPolicy = Identifier()
596 {
597 return compactionPolicy;
598 }
599}
600
601String FilterField() throws ParseException :
602{
603 String filterField = null;
604}
605{
606 filterField = Identifier()
607 {
608 return filterField;
609 }
610}
611
612IndexParams IndexType() throws ParseException:
613{
614 IndexType type = null;
615 int gramLength = 0;
616}
617{
618 (<BTREE>
619 {
620 type = IndexType.BTREE;
621 }
622 | <RTREE>
623 {
624 type = IndexType.RTREE;
625 }
626 | <KEYWORD>
627 {
628 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
629 }
630 | <NGRAM> <LEFTPAREN> <INTEGER_LITERAL>
631 {
632 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
633 gramLength = Integer.valueOf(token.image);
634 }
635 <RIGHTPAREN>)
636 {
637 return new IndexParams(type, gramLength);
638 }
639}
640
641CreateDataverseStatement DataverseSpecification() throws ParseException :
642{
643 String dvName = null;
644 boolean ifNotExists = false;
645 String format = null;
646}
647{
648 <DATAVERSE> dvName = Identifier()
649 ifNotExists = IfNotExists()
Yingyi Bu6d57e492016-06-06 21:24:42 -0700650 ( LOOKAHEAD(1) <WITH> <FORMAT> format = ConstantString() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700651 {
652 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
653 }
654}
655
656CreateFunctionStatement FunctionSpecification() throws ParseException:
657{
658 FunctionSignature signature;
659 boolean ifNotExists = false;
660 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
661 String functionBody;
662 VarIdentifier var = null;
663 Expression functionBodyExpr;
664 Token beginPos;
665 Token endPos;
666 FunctionName fctName = null;
667
668 createNewScope();
669}
670{
671 <FUNCTION> fctName = FunctionName()
672 ifNotExists = IfNotExists()
673 paramList = ParameterList()
674 <LEFTBRACE>
675 {
676 beginPos = token;
677 }
678 functionBodyExpr = Expression() <RIGHTBRACE>
679 {
680 endPos = token;
681 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
682 // TODO use fctName.library
683 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
684 getCurrentScope().addFunctionDescriptor(signature, false);
685 removeCurrentScope();
686 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
687 }
688}
689
690CreateFeedStatement FeedSpecification() throws ParseException:
691{
692 Pair<Identifier,Identifier> nameComponents = null;
693 boolean ifNotExists = false;
694 String adapterName = null;
695 Map<String,String> properties = null;
696 FunctionSignature appliedFunction = null;
697 CreateFeedStatement cfs = null;
698 Pair<Identifier,Identifier> sourceNameComponents = null;
Michael Blowd6cf6412016-06-30 02:44:35 -0400699
Yingyi Bu391f09e2015-10-29 13:49:39 -0700700}
701{
702 (
703 <SECONDARY> <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
704 <FROM> <FEED> sourceNameComponents = QualifiedName() (appliedFunction = ApplyFunction())?
705 {
706 cfs = new CreateSecondaryFeedStatement(nameComponents,
707 sourceNameComponents, appliedFunction, ifNotExists);
708 }
709 |
710 (<PRIMARY>)? <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
Michael Blowd6cf6412016-06-30 02:44:35 -0400711 <USING> adapterName = AdapterName() properties = Configuration() (appliedFunction = ApplyFunction())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700712 {
713 cfs = new CreatePrimaryFeedStatement(nameComponents,
714 adapterName, properties, appliedFunction, ifNotExists);
715 }
716 )
717 {
718 return cfs;
719 }
720}
721
722CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
723{
Michael Blowd6cf6412016-06-30 02:44:35 -0400724 String policyName = null;
725 String basePolicyName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700726 String sourcePolicyFile = null;
727 String definition = null;
728 boolean ifNotExists = false;
729 Map<String,String> properties = null;
730 CreateFeedPolicyStatement cfps = null;
731}
732{
733 (
734 <INGESTION> <POLICY> policyName = Identifier() ifNotExists = IfNotExists()
Yingyi Bu6d57e492016-06-06 21:24:42 -0700735 <FROM>
736 (<POLICY> basePolicyName = Identifier() properties = Configuration() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700737 {
738 cfps = new CreateFeedPolicyStatement(policyName,
739 basePolicyName, properties, definition, ifNotExists);
740 }
Yingyi Bu6d57e492016-06-06 21:24:42 -0700741 | <PATH> sourcePolicyFile = Identifier() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700742 {
743 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
744 }
Yingyi Bu6d57e492016-06-06 21:24:42 -0700745 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700746 )
747 {
748 return cfps;
749 }
750}
751
752
753
754List<VarIdentifier> ParameterList() throws ParseException:
755{
756 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
757 VarIdentifier var = null;
758}
759{
760 <LEFTPAREN> (<IDENTIFIER>
761 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700762 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700763 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700764 }
765 (<COMMA> <IDENTIFIER>
766 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700767 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700768 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700769 }
770 )*)? <RIGHTPAREN>
771 {
772 return paramList;
773 }
774}
775
776boolean IfNotExists() throws ParseException:
777{
778}
779{
Yingyi Budaa549c2016-06-28 22:30:52 -0700780 ( LOOKAHEAD(1) <IF> <NOT> <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700781 {
782 return true;
783 }
784 )?
785 {
786 return false;
787 }
788}
789
790FunctionSignature ApplyFunction() throws ParseException:
791{
792 FunctionName functioName = null;
793 FunctionSignature funcSig = null;
794}
795{
796 <APPLY> <FUNCTION> functioName = FunctionName()
797 {
798 String fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
799 return new FunctionSignature(functioName.dataverse, fqFunctionName, 1);
800 }
801}
802
803String GetPolicy() throws ParseException:
804{
805 String policy = null;
806}
807{
808 <USING> <POLICY> policy = Identifier()
809 {
810 return policy;
811 }
812
813}
814
815FunctionSignature FunctionSignature() throws ParseException:
816{
817 FunctionName fctName = null;
818 int arity = 0;
819}
820{
821 fctName = FunctionName() <ATT> <INTEGER_LITERAL>
822 {
823 arity = new Integer(token.image);
824 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
825 throw new ParseException(" invalid arity:" + arity);
826 }
827
828 // TODO use fctName.library
829 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
830 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
831 }
832}
833
Yingyi Buc9bfe252016-03-01 00:02:40 -0800834Pair<List<Integer>, List<List<String>>> PrimaryKey() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700835{
Yingyi Buc9bfe252016-03-01 00:02:40 -0800836 Pair<Integer, List<String>> tmp = null;
837 List<Integer> keyFieldSourceIndicators = new ArrayList<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -0700838 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
839}
840{
841 <PRIMARY> <KEY> tmp = NestedField()
842 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800843 keyFieldSourceIndicators.add(tmp.first);
844 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700845 }
846 ( <COMMA> tmp = NestedField()
847 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800848 keyFieldSourceIndicators.add(tmp.first);
849 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700850 }
851 )*
852 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800853 return new Pair<List<Integer>, List<List<String>>> (keyFieldSourceIndicators, primaryKeyFields);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700854 }
855}
856
857Statement DropStatement() throws ParseException:
858{
859 String id = null;
860 Pair<Identifier,Identifier> pairId = null;
861 Triple<Identifier,Identifier,Identifier> tripleId = null;
862 FunctionSignature funcSig = null;
863 boolean ifExists = false;
864 Statement stmt = null;
865}
866{
867 <DROP>
868 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700869 Dataset() pairId = QualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700870 {
871 stmt = new DropStatement(pairId.first, pairId.second, ifExists);
872 }
873 | <INDEX> tripleId = DoubleQualifiedName() ifExists = IfExists()
874 {
875 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
876 }
877 | <NODEGROUP> id = Identifier() ifExists = IfExists()
878 {
879 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
880 }
881 | <TYPE> pairId = TypeName() ifExists = IfExists()
882 {
883 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
884 }
885 | <DATAVERSE> id = Identifier() ifExists = IfExists()
886 {
887 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
888 }
889 | <FUNCTION> funcSig = FunctionSignature() ifExists = IfExists()
890 {
891 stmt = new FunctionDropStatement(funcSig, ifExists);
892 }
893 | <FEED> pairId = QualifiedName() ifExists = IfExists()
894 {
895 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
896 }
897 )
898 {
899 return stmt;
900 }
901}
902
903boolean IfExists() throws ParseException :
904{
905}
906{
907 ( LOOKAHEAD(1) <IF> <EXISTS>
908 {
909 return true;
910 }
911 )?
912 {
913 return false;
914 }
915}
916
917InsertStatement InsertStatement() throws ParseException:
918{
919 Pair<Identifier,Identifier> nameComponents = null;
920 Query query;
921}
922{
923 <INSERT> <INTO> nameComponents = QualifiedName() query = Query()
924 {
Yingyi Bucaea8f02015-11-16 15:12:15 -0800925 query.setTopLevel(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700926 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
927 }
928}
929
930DeleteStatement DeleteStatement() throws ParseException:
931{
Yingyi Bu20e085b2016-07-06 12:57:27 -0700932 VariableExpr varExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700933 Expression condition = null;
934 Pair<Identifier, Identifier> nameComponents;
935 // This is related to the new metadata lock management
936 setDataverses(new ArrayList<String>());
937 setDatasets(new ArrayList<String>());
938
939}
940{
Yingyi Bu20e085b2016-07-06 12:57:27 -0700941 <DELETE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700942 <FROM> nameComponents = QualifiedName()
Yingyi Bu20e085b2016-07-06 12:57:27 -0700943 ((<AS>)? varExpr = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700944 (<WHERE> condition = Expression())?
Yingyi Bu20e085b2016-07-06 12:57:27 -0700945 {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700946 // First we get the dataverses and datasets that we want to lock
947 List<String> dataverses = getDataverses();
948 List<String> datasets = getDatasets();
949 // we remove the pointer to the dataverses and datasets
950 setDataverses(null);
951 setDatasets(null);
Yingyi Bu20e085b2016-07-06 12:57:27 -0700952
953 if(varExpr == null){
954 varExpr = new VariableExpr();
955 VarIdentifier var = SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue());
956 varExpr.setVar(var);
957 }
958 return new DeleteStatement(varExpr, nameComponents.first, nameComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700959 condition, getVarCounter(), dataverses, datasets);
Yingyi Bu20e085b2016-07-06 12:57:27 -0700960 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700961}
962
963UpdateStatement UpdateStatement() throws ParseException:
964{
965 VariableExpr vars;
966 Expression target;
967 Expression condition;
968 UpdateClause uc;
969 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
970}
971{
972 <UPDATE> vars = Variable() <IN> target = Expression()
973 <WHERE> condition = Expression()
974 <LEFTPAREN> (uc = UpdateClause()
975 {
976 ucs.add(uc);
977 }
978 (<COMMA> uc = UpdateClause()
979 {
980 ucs.add(uc);
981 }
982 )*) <RIGHTPAREN>
983 {
984 return new UpdateStatement(vars, target, condition, ucs);
985 }
986}
987
988UpdateClause UpdateClause() throws ParseException:
989{
990 Expression target = null;
991 Expression value = null ;
992 InsertStatement is = null;
993 DeleteStatement ds = null;
994 UpdateStatement us = null;
995 Expression condition = null;
996 UpdateClause ifbranch = null;
997 UpdateClause elsebranch = null;
998}
999{
1000 (<SET> target = Expression() <EQ> value = Expression()
1001 | is = InsertStatement()
1002 | ds = DeleteStatement()
1003 | us = UpdateStatement()
1004 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
1005 <THEN> ifbranch = UpdateClause()
1006 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
1007 {
1008 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
1009 }
1010 )
1011}
1012
1013Statement SetStatement() throws ParseException:
1014{
1015 String pn = null;
1016 String pv = null;
1017}
1018{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001019 <SET> pn = Identifier() pv = ConstantString()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001020 {
1021 return new SetStatement(pn, pv);
1022 }
1023}
1024
1025Statement WriteStatement() throws ParseException:
1026{
1027 String nodeName = null;
1028 String fileName = null;
1029 Query query;
1030 String writerClass = null;
1031 Pair<Identifier,Identifier> nameComponents = null;
1032}
1033{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001034 <WRITE> <OUTPUT> <TO> nodeName = Identifier() <COLON> fileName = ConstantString()
1035 ( <USING> writerClass = ConstantString() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001036 {
1037 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
1038 }
1039}
1040
1041LoadStatement LoadStatement() throws ParseException:
1042{
1043 Identifier dataverseName = null;
1044 Identifier datasetName = null;
1045 boolean alreadySorted = false;
1046 String adapterName;
1047 Map<String,String> properties;
1048 Pair<Identifier,Identifier> nameComponents = null;
1049}
1050{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001051 <LOAD> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001052 {
1053 dataverseName = nameComponents.first;
1054 datasetName = nameComponents.second;
1055 }
1056 <USING> adapterName = AdapterName() properties = Configuration()
1057 (<PRESORTED>
1058 {
1059 alreadySorted = true;
1060 }
1061 )?
1062 {
1063 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1064 }
1065}
1066
1067
1068String AdapterName() throws ParseException :
1069{
1070 String adapterName = null;
1071}
1072{
1073 adapterName = Identifier()
1074 {
1075 return adapterName;
1076 }
1077}
1078
1079Statement CompactStatement() throws ParseException:
1080{
1081 Pair<Identifier,Identifier> nameComponents = null;
1082 Statement stmt = null;
1083}
1084{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001085 <COMPACT> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001086 {
1087 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1088 }
1089 {
1090 return stmt;
1091 }
1092}
1093
1094Statement FeedStatement() throws ParseException:
1095{
1096 Pair<Identifier,Identifier> feedNameComponents = null;
1097 Pair<Identifier,Identifier> datasetNameComponents = null;
1098
1099 Map<String,String> configuration = null;
1100 Statement stmt = null;
1101 String policy = null;
1102}
1103{
1104 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001105 <CONNECT> <FEED> feedNameComponents = QualifiedName() <TO> Dataset() datasetNameComponents = QualifiedName() (policy = GetPolicy())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001106 {
1107 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, policy, getVarCounter());
1108 }
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001109 | <DISCONNECT> <FEED> feedNameComponents = QualifiedName() <FROM> Dataset() datasetNameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001110 {
1111 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1112 }
1113 )
1114 {
1115 return stmt;
1116 }
1117}
1118
1119Map<String,String> Configuration() throws ParseException :
1120{
1121 Map<String,String> configuration = new LinkedHashMap<String,String>();
1122 Pair<String, String> keyValuePair = null;
1123}
1124{
1125 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1126 {
1127 configuration.put(keyValuePair.first, keyValuePair.second);
1128 }
1129 ( <COMMA> keyValuePair = KeyValuePair()
1130 {
1131 configuration.put(keyValuePair.first, keyValuePair.second);
1132 }
1133 )* )? <RIGHTPAREN>
1134 {
1135 return configuration;
1136 }
1137}
1138
1139Pair<String, String> KeyValuePair() throws ParseException:
1140{
1141 String key;
1142 String value;
1143}
1144{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001145 <LEFTPAREN> key = ConstantString() <EQ> value = ConstantString() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001146 {
1147 return new Pair<String, String>(key, value);
1148 }
1149}
1150
1151Map<String,String> Properties() throws ParseException:
1152{
1153 Map<String,String> properties = new HashMap<String,String>();
1154 Pair<String, String> property;
1155}
1156{
1157 (LOOKAHEAD(1) <LEFTPAREN> property = Property()
1158 {
1159 properties.put(property.first, property.second);
1160 }
1161 ( <COMMA> property = Property()
1162 {
1163 properties.put(property.first, property.second);
1164 }
1165 )* <RIGHTPAREN> )?
1166 {
1167 return properties;
1168 }
1169}
1170
1171Pair<String, String> Property() throws ParseException:
1172{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001173 String key = null;
1174 String value = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001175}
1176{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001177 (key = Identifier() | key = StringLiteral())
1178 <EQ>
1179 ( value = ConstantString() | <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001180 {
1181 try {
1182 value = "" + Long.valueOf(token.image);
1183 } catch (NumberFormatException nfe) {
1184 throw new ParseException("inapproriate value: " + token.image);
1185 }
1186 }
1187 )
1188 {
1189 return new Pair<String, String>(key.toUpperCase(), value);
1190 }
1191}
1192
1193TypeExpression IndexedTypeExpr() throws ParseException:
1194{
1195 TypeExpression typeExpr = null;
1196}
1197{
1198 (
1199 typeExpr = TypeReference()
1200 | typeExpr = OrderedListTypeDef()
1201 | typeExpr = UnorderedListTypeDef()
1202 )
1203 {
1204 return typeExpr;
1205 }
1206}
1207
1208TypeExpression TypeExpr() throws ParseException:
1209{
1210 TypeExpression typeExpr = null;
1211}
1212{
1213 (
1214 typeExpr = RecordTypeDef()
1215 | typeExpr = TypeReference()
1216 | typeExpr = OrderedListTypeDef()
1217 | typeExpr = UnorderedListTypeDef()
1218 )
1219 {
1220 return typeExpr;
1221 }
1222}
1223
1224RecordTypeDefinition RecordTypeDef() throws ParseException:
1225{
1226 RecordTypeDefinition recType = new RecordTypeDefinition();
1227 RecordTypeDefinition.RecordKind recordKind = null;
1228}
1229{
1230 ( <CLOSED> { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1231 | <OPEN> { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1232 <LEFTBRACE>
1233 {
1234 String hint = getHint(token);
1235 if (hint != null) {
1236 String splits[] = hint.split(" +");
1237 if (splits[0].equals(GEN_FIELDS_HINT)) {
1238 if (splits.length != 5) {
1239 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1240 }
1241 if (!splits[1].equals("int")) {
1242 throw new ParseException("The only supported type for gen-fields is int.");
1243 }
1244 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1245 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1246 recType.setUndeclaredFieldsDataGen(ufdg);
1247 }
1248 }
1249
1250 }
1251 (
1252 RecordField(recType)
1253 ( <COMMA> RecordField(recType) )*
1254 )?
1255 <RIGHTBRACE>
1256 {
1257 if (recordKind == null) {
1258 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1259 }
1260 recType.setRecordKind(recordKind);
1261 return recType;
1262 }
1263}
1264
1265void RecordField(RecordTypeDefinition recType) throws ParseException:
1266{
1267 String fieldName;
1268 TypeExpression type = null;
1269 boolean nullable = false;
1270}
1271{
1272 fieldName = Identifier()
1273 {
1274 String hint = getHint(token);
1275 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1276 }
1277 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1278 {
1279 recType.addField(fieldName, type, nullable, rfdg);
1280 }
1281}
1282
1283TypeReferenceExpression TypeReference() throws ParseException:
1284{
1285 String id = null;
1286}
1287{
1288 id = Identifier()
1289 {
1290 if (id.equalsIgnoreCase("int")) {
1291 id = "int64";
1292 }
1293
1294 return new TypeReferenceExpression(new Identifier(id));
1295 }
1296}
1297
1298OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1299{
1300 TypeExpression type = null;
1301}
1302{
1303 <LEFTBRACKET>
1304 ( type = TypeExpr() )
1305 <RIGHTBRACKET>
1306 {
1307 return new OrderedListTypeDefinition(type);
1308 }
1309}
1310
1311
1312UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1313{
1314 TypeExpression type = null;
1315}
1316{
1317 <LEFTDBLBRACE>
1318 ( type = TypeExpr() )
1319 <RIGHTDBLBRACE>
1320 {
1321 return new UnorderedListTypeDefinition(type);
1322 }
1323}
1324
1325FunctionName FunctionName() throws ParseException:
1326{
1327 String first = null;
1328 String second = null;
1329 String third = null;
1330 boolean secondAfterDot = false;
1331}
1332{
Michael Blowd6cf6412016-06-30 02:44:35 -04001333 first = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001334 {
1335 FunctionName result = new FunctionName();
1336 result.hint = getHint(token);
Michael Blowd6cf6412016-06-30 02:44:35 -04001337 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001338 ( <DOT> second = Identifier()
1339 {
1340 secondAfterDot = true;
1341 }
1342 (<SHARP> third = Identifier())? | <SHARP> second = Identifier() )?
1343 {
1344 if (second == null) {
1345 result.dataverse = defaultDataverse;
1346 result.library = null;
1347 result.function = first;
1348 } else if (third == null) {
1349 if (secondAfterDot) {
1350 result.dataverse = first;
1351 result.library = null;
1352 result.function = second;
1353 } else {
1354 result.dataverse = defaultDataverse;
1355 result.library = first;
1356 result.function = second;
1357 }
1358 } else {
1359 result.dataverse = first;
1360 result.library = second;
1361 result.function = third;
1362 }
1363
1364 if (result.function.equalsIgnoreCase("int")) {
1365 result.function = "int64";
1366 }
1367 return result;
1368 }
1369}
1370
1371Pair<Identifier,Identifier> TypeName() throws ParseException:
1372{
1373 Pair<Identifier,Identifier> name = null;
1374}
1375{
1376 name = QualifiedName()
1377 {
1378 if (name.first == null) {
1379 name.first = new Identifier(defaultDataverse);
1380 }
1381 return name;
1382 }
1383}
1384
1385String Identifier() throws ParseException:
1386{
1387 String lit = null;
1388}
1389{
1390 (<IDENTIFIER>
1391 {
1392 return token.image;
1393 }
1394 | lit = QuotedString()
1395 {
1396 return lit;
1397 }
1398 )
1399}
1400
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001401void Dataset() throws ParseException:
1402{
1403}
1404{
1405 (<DATASET>|<COLLECTION>)
1406}
1407
Yingyi Buc9bfe252016-03-01 00:02:40 -08001408Pair<Integer, Pair<List<String>, TypeExpression>> OpenField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001409{
1410 TypeExpression fieldType = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001411 Pair<Integer, List<String>> fieldList = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001412}
1413{
1414 fieldList = NestedField()
1415 ( <COLON> fieldType = IndexedTypeExpr() )?
1416 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001417 return new Pair<Integer, Pair<List<String>, TypeExpression>>
1418 (fieldList.first, new Pair<List<String>, TypeExpression>(fieldList.second, fieldType));
Yingyi Bu391f09e2015-10-29 13:49:39 -07001419 }
1420}
1421
Yingyi Buc9bfe252016-03-01 00:02:40 -08001422Pair<Integer, List<String>> NestedField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001423{
1424 List<String> exprList = new ArrayList<String>();
1425 String lit = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001426 int source = 0;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001427}
1428{
1429 lit = Identifier()
1430 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001431 boolean meetParens = false;
1432 }
1433 (
Yingyi Buc9bfe252016-03-01 00:02:40 -08001434 LOOKAHEAD(1)
Yingyi Bub9169b62016-02-26 21:21:49 -08001435 <LEFTPAREN><RIGHTPAREN>
1436 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001437 if(!lit.toLowerCase().equals("meta")){
Yingyi Bub9169b62016-02-26 21:21:49 -08001438 throw new ParseException("The string before () has to be \"meta\".");
1439 }
1440 meetParens = true;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001441 source = 1;
Yingyi Bub9169b62016-02-26 21:21:49 -08001442 }
1443 )?
1444 {
1445 if(!meetParens){
1446 exprList.add(lit);
1447 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001448 }
1449 (<DOT>
1450 lit = Identifier()
1451 {
1452 exprList.add(lit);
1453 }
1454 )*
1455 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001456 return new Pair<Integer, List<String>>(source, exprList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001457 }
1458}
1459
Yingyi Bu6d57e492016-06-06 21:24:42 -07001460String ConstantString() throws ParseException:
1461{
1462 String value = null;
1463}
1464{
1465 (value = QuotedString() | value = StringLiteral())
1466 {
1467 return value;
1468 }
1469}
1470
Yingyi Bu391f09e2015-10-29 13:49:39 -07001471
1472String QuotedString() throws ParseException:
1473{
1474}
1475{
1476 <QUOTED_STRING>
1477 {
1478 return removeQuotesAndEscapes(token.image);
1479 }
1480}
1481
1482
1483String StringLiteral() throws ParseException:
1484{
1485}
1486{
1487 <STRING_LITERAL>
1488 {
1489 return removeQuotesAndEscapes(token.image);
1490 }
1491}
1492
1493Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1494{
1495 String first = null;
1496 String second = null;
1497}
1498{
1499 first = Identifier() (<DOT> second = Identifier())?
1500 {
1501 Identifier id1 = null;
1502 Identifier id2 = null;
1503 if (second == null) {
1504 id2 = new Identifier(first);
1505 } else
1506 {
1507 id1 = new Identifier(first);
1508 id2 = new Identifier(second);
1509 }
1510 return new Pair<Identifier,Identifier>(id1, id2);
1511 }
1512}
1513
1514Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1515{
1516 String first = null;
1517 String second = null;
1518 String third = null;
1519}
1520{
1521 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1522 {
1523 Identifier id1 = null;
1524 Identifier id2 = null;
1525 Identifier id3 = null;
1526 if (third == null) {
1527 id2 = new Identifier(first);
1528 id3 = new Identifier(second);
1529 } else {
1530 id1 = new Identifier(first);
1531 id2 = new Identifier(second);
1532 id3 = new Identifier(third);
1533 }
1534 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1535 }
1536}
1537
1538FunctionDecl FunctionDeclaration() throws ParseException:
1539{
1540 FunctionDecl funcDecl;
1541 FunctionSignature signature;
1542 String functionName;
1543 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1544 Expression funcBody;
1545 createNewScope();
1546}
1547{
1548 <DECLARE> <FUNCTION> functionName = Identifier()
1549 paramList = ParameterList()
1550 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1551 {
1552 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1553 getCurrentScope().addFunctionDescriptor(signature, false);
1554 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1555 removeCurrentScope();
1556 return funcDecl;
1557 }
1558}
1559
1560
1561Query Query() throws ParseException:
1562{
1563 Query query = new Query();
1564 // we set the pointers to the dataverses and datasets lists to fill them with entities to be locked
1565 setDataverses(query.getDataverses());
1566 setDatasets(query.getDatasets());
1567 Expression expr;
1568}
1569{
1570 (
1571 expr = Expression()
1572 |
1573 expr = SelectExpression(false)
1574 )
1575 {
1576 query.setBody(expr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001577 // we remove the pointers to the locked entities before we return the query object
1578 setDataverses(null);
1579 setDatasets(null);
1580 return query;
1581 }
1582}
1583
1584
1585
1586Expression Expression():
1587{
1588 Expression expr = null;
1589 Expression exprP = null;
1590}
1591{
1592(
1593 LOOKAHEAD(2)
1594 expr = OperatorExpr()
Yingyi Buc8c067c2016-07-25 23:37:19 -07001595 | expr = CaseExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001596 | expr = QuantifiedExpression()
1597)
1598 {
1599 return (exprP==null) ? expr : exprP;
1600 }
1601}
1602
1603
1604
1605Expression OperatorExpr()throws ParseException:
1606{
1607 OperatorExpr op = null;
1608 Expression operand = null;
1609}
1610{
1611 operand = AndExpr()
1612 (
1613
1614 <OR>
1615 {
1616 if (op == null) {
1617 op = new OperatorExpr();
1618 op.addOperand(operand);
1619 op.setCurrentop(true);
1620 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001621 try{
1622 op.addOperator(token.image.toLowerCase());
1623 } catch (Exception e){
1624 throw new ParseException(e.getMessage());
1625 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001626 }
1627
1628 operand = AndExpr()
1629 {
1630 op.addOperand(operand);
1631 }
1632
1633 )*
1634
1635 {
1636 return op==null? operand: op;
1637 }
1638}
1639
1640Expression AndExpr()throws ParseException:
1641{
1642 OperatorExpr op = null;
1643 Expression operand = null;
1644}
1645{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001646 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001647 (
1648
1649 <AND>
1650 {
1651 if (op == null) {
1652 op = new OperatorExpr();
1653 op.addOperand(operand);
Yingyi Bu196db5d2016-07-15 19:07:20 -07001654 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001655 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001656 try{
1657 op.addOperator(token.image.toLowerCase());
1658 } catch (AsterixException e){
1659 throw new ParseException(e.getMessage());
1660 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001661 }
1662
Yingyi Bu196db5d2016-07-15 19:07:20 -07001663 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001664 {
1665 op.addOperand(operand);
1666 }
1667
1668 )*
1669
1670 {
1671 return op==null? operand: op;
1672 }
1673}
1674
Yingyi Bu196db5d2016-07-15 19:07:20 -07001675Expression NotExpr()throws ParseException:
1676{
1677 Expression inputExpr;
1678 boolean not = false;
1679}
1680{
1681 (<NOT> { not = true; } )? inputExpr = RelExpr()
1682 {
1683 if(not){
1684 FunctionSignature signature = new FunctionSignature(null, "not", 1);
1685 return new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(inputExpr)));
1686 } else {
1687 return inputExpr;
1688 }
1689 }
1690}
Yingyi Bu391f09e2015-10-29 13:49:39 -07001691
1692Expression RelExpr()throws ParseException:
1693{
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001694 boolean not = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001695 OperatorExpr op = null;
1696 Expression operand = null;
1697 boolean broadcast = false;
1698 IExpressionAnnotation annotation = null;
1699}
1700{
Yingyi Budaa549c2016-06-28 22:30:52 -07001701 operand = IsExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001702 {
1703 if (operand instanceof VariableExpr) {
1704 String hint = getHint(token);
1705 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1706 broadcast = true;
1707 }
1708 }
1709 }
1710
1711 (
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001712 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> |<SIMILAR> | (<NOT> { not = true; })? (<LIKE>|<IN>))
Yingyi Bu391f09e2015-10-29 13:49:39 -07001713 {
1714 String mhint = getHint(token);
1715 if (mhint != null) {
1716 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001717 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1718 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1719 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1720 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001721 }
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001722 String operator = token.image.toLowerCase();
1723 if(not){
1724 operator = "not_" + operator;
1725 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001726 if (op == null) {
1727 op = new OperatorExpr();
1728 op.addOperand(operand, broadcast);
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001729 op.setCurrentop(true);
1730 broadcast = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001731 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001732 try{
1733 op.addOperator(operator);
1734 } catch (AsterixException e){
1735 throw new ParseException(e.getMessage());
1736 }
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001737 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001738
Yingyi Budaa549c2016-06-28 22:30:52 -07001739 operand = IsExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001740 {
1741 broadcast = false;
1742 if (operand instanceof VariableExpr) {
1743 String hint = getHint(token);
1744 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1745 broadcast = true;
1746 }
1747 }
1748 op.addOperand(operand, broadcast);
1749 }
1750 )?
1751
1752 {
1753 if (annotation != null) {
1754 op.addHint(annotation);
1755 }
1756 return op==null? operand: op;
1757 }
1758}
1759
Yingyi Budaa549c2016-06-28 22:30:52 -07001760Expression IsExpr() throws ParseException:
1761{
1762 Expression expr = null;
1763 Expression operand = null;
1764 boolean not = false;
1765}
1766{
1767 operand = AddExpr()
1768 ( <IS> (<NOT> { not = true; })? (<NULL> | <MISSING> | <UNKOWN>)
1769 {
1770 String functionName = "is-" + token.image.toLowerCase();
1771 FunctionSignature signature = new FunctionSignature(null, functionName, 1);
1772 expr = new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(operand)));
1773 if(not) {
1774 FunctionSignature notSignature = new FunctionSignature(null, "not", 1);
1775 expr = new CallExpr(notSignature, new ArrayList<Expression>(Collections.singletonList(expr)));
1776 }
1777 }
1778 )?
1779 {
1780 return expr = expr==null? operand : expr;
1781 }
1782}
1783
1784
Yingyi Bu391f09e2015-10-29 13:49:39 -07001785Expression AddExpr()throws ParseException:
1786{
1787 OperatorExpr op = null;
1788 Expression operand = null;
1789}
1790{
1791 operand = MultExpr()
Yingyi Budaa549c2016-06-28 22:30:52 -07001792 (
Yingyi Bu391f09e2015-10-29 13:49:39 -07001793 LOOKAHEAD(1)
1794 (<PLUS> | <MINUS>)
1795 {
1796 if (op == null) {
1797 op = new OperatorExpr();
1798 op.addOperand(operand);
1799 op.setCurrentop(true);
1800 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001801 try{
1802 ((OperatorExpr)op).addOperator(token.image);
1803 } catch (Exception e){
1804 throw new ParseException(e.getMessage());
1805 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001806 }
1807
1808 operand = MultExpr()
1809 {
1810 op.addOperand(operand);
1811 }
1812 )*
1813
1814 {
1815 return op==null? operand: op;
1816 }
1817}
1818
1819Expression MultExpr()throws ParseException:
1820{
1821 OperatorExpr op = null;
1822 Expression operand = null;
1823}
1824{
1825 operand = UnaryExpr()
1826
1827 (( <MUL> | <DIV> | <MOD> | <CARET> | <IDIV>)
1828 {
1829 if (op == null) {
1830 op = new OperatorExpr();
1831 op.addOperand(operand);
1832 op.setCurrentop(true);
1833 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001834 try{
1835 op.addOperator(token.image);
1836 } catch (Exception e){
1837 throw new ParseException(e.getMessage());
1838 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001839 }
1840 operand = UnaryExpr()
1841 {
1842 op.addOperand(operand);
1843 }
1844 )*
1845
1846 {
1847 return op==null?operand:op;
1848 }
1849}
1850
1851Expression UnaryExpr() throws ParseException:
1852{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001853 boolean not = false;
1854 UnaryExpr uexpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001855 Expression expr = null;
1856}
Yingyi Budaa549c2016-06-28 22:30:52 -07001857{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001858 ( (<PLUS> | <MINUS> | (<NOT> { not = true; } )? <EXISTS> )
Yingyi Bu391f09e2015-10-29 13:49:39 -07001859 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07001860 String exprType = token.image.toLowerCase();
1861 if(not){
1862 exprType = "not_" + exprType;
1863 }
1864 uexpr = new UnaryExpr();
1865 try{
1866 uexpr.setExprType(exprType);
1867 } catch (AsterixException e){
1868 throw new ParseException(e.getMessage());
Yingyi Budaa549c2016-06-28 22:30:52 -07001869 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001870 }
1871 )?
1872
1873 expr = ValueExpr()
1874 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07001875 if(uexpr==null){
Yingyi Bu391f09e2015-10-29 13:49:39 -07001876 return expr;
Yingyi Bu196db5d2016-07-15 19:07:20 -07001877 }
1878 uexpr.setExpr(expr);
1879 return uexpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001880 }
1881}
1882
1883Expression ValueExpr()throws ParseException:
1884{
1885 Expression expr = null;
1886 Identifier ident = null;
1887 AbstractAccessor fa = null;
1888 Expression indexExpr = null;
1889}
1890{
1891 expr = PrimaryExpr() (
1892 ident = Field()
1893 {
1894 fa = (fa == null ? new FieldAccessor(expr, ident)
1895 : new FieldAccessor(fa, ident));
1896 }
1897 | indexExpr = Index()
1898 {
1899 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
1900 : new IndexAccessor(fa, indexExpr));
1901 }
1902 )*
1903 {
1904 return fa == null ? expr : fa;
1905 }
1906}
1907
1908Identifier Field() throws ParseException:
1909{
1910 String ident = null;
1911}
1912{
1913 <DOT> ident = Identifier()
1914 {
1915 return new Identifier(ident);
1916 }
1917}
1918
1919Expression Index() throws ParseException:
1920{
1921 Expression expr = null;
1922}
1923{
1924 <LEFTBRACKET> ( expr = Expression()
1925 {
1926 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1927 {
1928 Literal lit = ((LiteralExpr)expr).getValue();
1929 if(lit.getLiteralType() != Literal.Type.INTEGER &&
1930 lit.getLiteralType() != Literal.Type.LONG) {
1931 throw new ParseException("Index should be an INTEGER");
1932 }
1933 }
1934 }
1935
1936 | <QUES> // ANY
1937
1938 )
1939
1940 <RIGHTBRACKET>
1941 {
1942 return expr;
1943 }
1944}
1945
1946
1947Expression PrimaryExpr()throws ParseException:
1948{
1949 Expression expr = null;
1950}
1951{
1952 ( LOOKAHEAD(4)
1953 expr = FunctionCallExpr()
1954 | expr = Literal()
1955 | expr = VariableRef()
1956 | expr = ListConstructor()
1957 | expr = RecordConstructor()
1958 | expr = ParenthesizedExpression()
1959 )
1960 {
1961 return expr;
1962 }
1963}
1964
1965Expression Literal() throws ParseException:
1966{
1967 LiteralExpr lit = new LiteralExpr();
1968 String str = null;
1969}
1970{
1971 ( str = StringLiteral()
1972 {
1973 lit.setValue(new StringLiteral(str));
1974 }
1975 | <INTEGER_LITERAL>
1976 {
1977 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
1978 }
1979 | <FLOAT_LITERAL>
1980 {
1981 lit.setValue(new FloatLiteral(new Float(token.image)));
1982 }
1983 | <DOUBLE_LITERAL>
1984 {
1985 lit.setValue(new DoubleLiteral(new Double(token.image)));
1986 }
Yingyi Bu535d86b2016-05-23 16:44:25 -07001987 | <MISSING>
1988 {
1989 lit.setValue(MissingLiteral.INSTANCE);
1990 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001991 | <NULL>
1992 {
1993 lit.setValue(NullLiteral.INSTANCE);
1994 }
1995 | <TRUE>
1996 {
1997 lit.setValue(TrueLiteral.INSTANCE);
1998 }
1999 | <FALSE>
2000 {
2001 lit.setValue(FalseLiteral.INSTANCE);
2002 }
2003 )
2004 {
2005 return lit;
2006 }
2007}
2008
2009
2010VariableExpr VariableRef() throws ParseException:
2011{
2012 VariableExpr varExp = new VariableExpr();
2013 VarIdentifier var = new VarIdentifier();
2014}
2015{
2016 { String id = null; }
2017 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2018 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002019 id = SqlppVariableUtil.toInternalVariableName(id); // Prefix user-defined variables with "$"
Yingyi Bu391f09e2015-10-29 13:49:39 -07002020 Identifier ident = lookupSymbol(id);
2021 if (isInForbiddenScopes(id)) {
2022 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.");
2023 }
2024 if(ident != null) { // exist such ident
Yingyi Bu391f09e2015-10-29 13:49:39 -07002025 varExp.setVar((VarIdentifier)ident);
2026 } else {
2027 varExp.setVar(var);
Yingyi Buacc12a92016-03-26 17:25:05 -07002028 varExp.setIsNewVar(false);
2029 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002030 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002031 return varExp;
2032 }
2033}
2034
2035
2036VariableExpr Variable() throws ParseException:
2037{
2038 VariableExpr varExp = new VariableExpr();
2039 VarIdentifier var = new VarIdentifier();
2040}
2041{
2042 { String id = null; }
2043 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2044 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002045 id = SqlppVariableUtil.toInternalVariableName(id); // prefix user-defined variables with "$".
Yingyi Bu391f09e2015-10-29 13:49:39 -07002046 Identifier ident = lookupSymbol(id);
2047 if(ident != null) { // exist such ident
2048 varExp.setIsNewVar(false);
2049 }
2050 varExp.setVar(var);
Yingyi Bucaea8f02015-11-16 15:12:15 -08002051 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002052 return varExp;
2053 }
2054}
2055
2056Expression ListConstructor() throws ParseException:
2057{
2058 Expression expr = null;
2059}
2060{
2061 (
2062 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
2063 )
2064
2065 {
2066 return expr;
2067 }
2068}
2069
2070
2071ListConstructor OrderedListConstructor() throws ParseException:
2072{
2073 ListConstructor expr = new ListConstructor();
2074 List<Expression> exprList = null;
2075 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
2076}
2077{
2078 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
2079 {
2080 expr.setExprList(exprList);
2081 return expr;
2082 }
2083}
2084
2085ListConstructor UnorderedListConstructor() throws ParseException:
2086{
2087 ListConstructor expr = new ListConstructor();
2088 List<Expression> exprList = null;
2089 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
2090}
2091{
2092 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
2093 {
2094 expr.setExprList(exprList);
2095 return expr;
2096 }
2097}
2098
2099List<Expression> ExpressionList() throws ParseException:
2100{
2101 Expression expr = null;
2102 List<Expression> list = null;
2103 List<Expression> exprList = new ArrayList<Expression>();
2104}
2105{
2106 (
2107 expr = Expression() { exprList.add(expr); }
2108 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
2109 )?
2110 (LOOKAHEAD(1) Comma())?
2111 {
2112 return exprList;
2113 }
2114}
2115
2116void Comma():
2117{}
2118{
2119 <COMMA>
2120}
2121
2122RecordConstructor RecordConstructor() throws ParseException:
2123{
2124 RecordConstructor expr = new RecordConstructor();
2125 FieldBinding tmp = null;
2126 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2127}
2128{
2129 <LEFTBRACE> (tmp = FieldBinding()
2130 {
2131 fbList.add(tmp);
2132 }
2133 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2134 {
2135 expr.setFbList(fbList);
2136 return expr;
2137 }
2138}
2139
2140FieldBinding FieldBinding() throws ParseException:
2141{
2142 FieldBinding fb = new FieldBinding();
2143 Expression left, right;
2144}
2145{
2146 left = Expression() <COLON> right = Expression()
2147 {
2148 fb.setLeftExpr(left);
2149 fb.setRightExpr(right);
2150 return fb;
2151 }
2152}
2153
2154
2155Expression FunctionCallExpr() throws ParseException:
2156{
2157 CallExpr callExpr;
2158 List<Expression> argList = new ArrayList<Expression>();
2159 Expression tmp;
2160 int arity = 0;
2161 FunctionName funcName = null;
2162 String hint = null;
2163}
2164{
2165 funcName = FunctionName()
2166 {
2167 hint = funcName.hint;
2168 }
2169 <LEFTPAREN> (tmp = Expression()
2170 {
2171 argList.add(tmp);
2172 arity ++;
2173 }
2174 (<COMMA> tmp = Expression()
2175 {
2176 argList.add(tmp);
2177 arity++;
2178 }
2179 )*)? <RIGHTPAREN>
2180 {
2181 // TODO use funcName.library
2182 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2183 FunctionSignature signature
2184 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2185 if (signature == null) {
2186 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2187 }
2188 callExpr = new CallExpr(signature,argList);
2189 if (hint != null) {
2190 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2191 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2192 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2193 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2194 }
2195 }
2196 return callExpr;
2197 }
2198}
2199
2200Expression ParenthesizedExpression() throws ParseException:
2201{
2202 Expression expr;
2203}
2204{
2205 (
2206 LOOKAHEAD(2)
2207 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2208 |
2209 expr = Subquery()
2210 )
2211 {
2212 return expr;
2213 }
2214}
2215
Yingyi Buc8c067c2016-07-25 23:37:19 -07002216
2217Expression CaseExpr() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002218{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002219 Expression conditionExpr = new LiteralExpr(TrueLiteral.INSTANCE);
2220 List<Expression> whenExprs = new ArrayList<Expression>();
2221 List<Expression> thenExprs = new ArrayList<Expression>();
2222 Expression elseExpr = null;
2223
2224 Expression whenExpr = null;
2225 Expression thenExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002226}
2227{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002228 <CASE> ( conditionExpr = Expression() )?
2229 (
2230 <WHEN> whenExpr = Expression()
2231 {
2232 whenExprs.add(whenExpr);
2233 }
2234 <THEN> thenExpr = Expression()
2235 {
2236 thenExprs.add(thenExpr);
2237 }
2238 )*
2239 (<ELSE> elseExpr = Expression() )?
2240 <END>
2241 {
2242 return new CaseExpression(conditionExpr, whenExprs, thenExprs, elseExpr);
2243 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002244}
2245
2246SelectExpression SelectExpression(boolean subquery) throws ParseException: {
2247 List<LetClause> letClauses = new ArrayList<LetClause>();
2248 SelectSetOperation selectSetOperation;
2249 OrderbyClause orderbyClause = null;
2250 LimitClause limitClause = null;
2251 createNewScope();
2252} {
2253 ( letClauses = LetClause() )?
2254 selectSetOperation = SelectSetOperation()
2255 (orderbyClause = OrderbyClause() {})?
2256 (limitClause = LimitClause() {})?
2257 {
2258 return new SelectExpression(letClauses, selectSetOperation, orderbyClause, limitClause, subquery);
2259 }
2260}
2261
2262SelectSetOperation SelectSetOperation() throws ParseException: {
2263 SetOperationInput setOperationInputLeft;
2264 List<SetOperationRight> setOperationRights = new ArrayList<SetOperationRight>();
2265}
2266{
2267 {
2268 SelectBlock selectBlockLeft = null;
2269 SelectExpression subqueryLeft = null;
2270 Expression expr = null;
2271 }
2272 selectBlockLeft = SelectBlock()
2273 {
2274 setOperationInputLeft = new SetOperationInput(selectBlockLeft, subqueryLeft);
2275 }
2276 (
2277 {
2278 SetOpType opType = SetOpType.UNION;
2279 boolean setSemantics = true;
2280 SelectBlock selectBlockRight = null;
2281 SelectExpression subqueryRight = null;
2282 }
2283 (<UNION> {opType = SetOpType.UNION;} |<INTERSECT> {opType = SetOpType.INTERSECT;} |<EXCEPT> {opType = SetOpType.EXCEPT;}) (<ALL> {setSemantics = false;} )?
2284 (selectBlockRight = SelectBlock()| subqueryRight = Subquery())
2285 {
2286 setOperationRights.add(new SetOperationRight(opType, setSemantics, new SetOperationInput(selectBlockRight, subqueryRight)));
2287 }
2288 )*
2289 {
2290 return new SelectSetOperation(setOperationInputLeft, setOperationRights);
2291 }
2292}
2293
2294SelectExpression Subquery() throws ParseException: {
2295 SelectExpression selectExpr = null;
2296}
2297{
2298 <LEFTPAREN> selectExpr = SelectExpression(true) {} <RIGHTPAREN>
2299 {
2300 return selectExpr;
2301 }
2302}
2303
2304SelectBlock SelectBlock() throws ParseException: {
2305 SelectClause selectClause = null;
2306 FromClause fromClause = null;
2307 List<LetClause> fromLetClauses = null;
2308 WhereClause whereClause = null;
2309 GroupbyClause groupbyClause = null;
2310 List<LetClause> gbyLetClauses = null;
2311 HavingClause havingClause = null;
2312}
2313{
2314 (
2315 selectClause = SelectClause()
2316 (
2317 LOOKAHEAD(1)
2318 fromClause = FromClause()
2319 (
2320 LOOKAHEAD(1)
2321 fromLetClauses = LetClause()
2322 )?
2323 )?
2324 (whereClause = WhereClause())?
2325 (
2326 groupbyClause = GroupbyClause()
2327 (
2328 LOOKAHEAD(1)
2329 gbyLetClauses = LetClause()
2330 )?
2331 (havingClause = HavingClause())?
2332 )?
2333 |
2334 fromClause = FromClause()
2335 (
2336 LOOKAHEAD(1)
2337 fromLetClauses = LetClause()
2338 )?
2339 (whereClause = WhereClause())?
2340 (
2341 groupbyClause = GroupbyClause()
2342 (
2343 gbyLetClauses = LetClause()
2344 )?
2345 (havingClause = HavingClause())?
2346 )?
2347 selectClause = SelectClause()
2348 )
2349 {
2350 return new SelectBlock(selectClause, fromClause, fromLetClauses, whereClause, groupbyClause, gbyLetClauses, havingClause);
2351 }
2352}
2353
2354SelectClause SelectClause() throws ParseException: {
2355 SelectRegular selectRegular = null;
2356 SelectElement selectElement = null;
2357 boolean distinct = false;
2358}
2359{
Michael Blowd6cf6412016-06-30 02:44:35 -04002360 <SELECT> (<ALL>|<DISTINCT> {distinct = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002361 (
2362 selectRegular = SelectRegular()
Michael Blowd6cf6412016-06-30 02:44:35 -04002363 |
Yingyi Bu391f09e2015-10-29 13:49:39 -07002364 selectElement = SelectElement()
Yingyi Bua89fae62016-07-06 07:58:55 -07002365 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002366 {
Yingyi Bua89fae62016-07-06 07:58:55 -07002367 if(selectRegular == null && selectElement == null){
2368 Projection projection = new Projection(null, null, true, false);
2369 List<Projection> projections = new ArrayList<Projection>();
2370 projections.add(projection);
2371 selectRegular = new SelectRegular(projections);
2372 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002373 return new SelectClause(selectElement, selectRegular, distinct);
2374 }
2375}
2376
2377SelectRegular SelectRegular() throws ParseException: {
Michael Blowd6cf6412016-06-30 02:44:35 -04002378 List<Projection> projections = new ArrayList<Projection>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002379}
2380{
2381 {
2382 Projection projection = null;
2383 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002384 projection = Projection() { projections.add(projection); }
Yingyi Bua89fae62016-07-06 07:58:55 -07002385 ( LOOKAHEAD(2) <COMMA>
2386 projection = Projection() {projections.add(projection);}
2387 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -07002388 {
2389 return new SelectRegular(projections);
2390 }
2391}
2392
2393SelectElement SelectElement() throws ParseException: {
2394 Expression expr = null;
2395 String name = null;
2396}
2397{
2398 (<RAW>|<ELEMENT>|<VALUE>) expr = Expression()
2399 {
2400 return new SelectElement(expr);
2401 }
2402}
2403
2404Projection Projection() throws ParseException: {
2405 Expression expr = null;
2406 Identifier identifier = null;
2407 String name = null;
2408 boolean star = false;
2409 boolean exprStar = false;
2410}
2411{
2412 (
2413 LOOKAHEAD(2)
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002414 expr = Expression() ((<AS>)? name = Identifier())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002415 | expr = Expression() <DOT> <MUL> {exprStar = true; }
2416 | <MUL> {star = true; }
2417 )
2418 {
Yingyi Bua89fae62016-07-06 07:58:55 -07002419 if(!star && name == null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002420 String generatedColumnIdentifier = ExpressionToVariableUtil.getGeneratedIdentifier(expr, false);
2421 if(generatedColumnIdentifier != null){
2422 name = SqlppVariableUtil.toUserDefinedName(generatedColumnIdentifier);
2423 }
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002424 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002425 return new Projection(expr, name, star, exprStar);
2426 }
2427}
2428
2429FromClause FromClause() throws ParseException :
2430{
2431 List<FromTerm> fromTerms = new ArrayList<FromTerm>();
2432 extendCurrentScope();
2433}
2434{
2435 {
2436 FromTerm fromTerm = null;
2437 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002438 <FROM> fromTerm = FromTerm() { fromTerms.add(fromTerm); }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002439 (LOOKAHEAD(2) <COMMA> fromTerm = FromTerm() { fromTerms.add(fromTerm); } )*
2440 {
2441 return new FromClause(fromTerms);
2442 }
2443}
2444
2445FromTerm FromTerm() throws ParseException :
2446{
2447 Expression leftExpr = null;
Michael Blowd6cf6412016-06-30 02:44:35 -04002448 VariableExpr leftVar = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002449 VariableExpr posVar = null;
2450 List<AbstractBinaryCorrelateClause> correlateClauses = new ArrayList<AbstractBinaryCorrelateClause>();
2451}
2452{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002453 leftExpr = Expression() ((<AS>)? leftVar = Variable())? (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002454 (
2455 {JoinType joinType = JoinType.INNER; }
2456 (joinType = JoinType())?
2457 {
2458 AbstractBinaryCorrelateClause correlateClause = null;
2459 }
2460 (correlateClause = JoinClause(joinType)
Yingyi Bu6d999ed2016-07-13 11:59:06 -07002461 | correlateClause = UnnestClause(joinType)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002462 )
2463 {
2464 correlateClauses.add(correlateClause);
2465 }
2466 )*
2467 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002468 if(leftVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002469 leftVar = ExpressionToVariableUtil.getGeneratedVariable(leftExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002470 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002471 return new FromTerm(leftExpr, leftVar, posVar, correlateClauses);
2472 }
2473}
2474
2475JoinClause JoinClause(JoinType joinType) throws ParseException :
2476{
2477 Expression rightExpr = null;
2478 VariableExpr rightVar = null;
2479 VariableExpr posVar = null;
2480 Expression conditionExpr = null;
2481}
2482{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002483 <JOIN> rightExpr = Expression() ((<AS>)? rightVar = Variable())? (<AT> posVar = Variable())? <ON> conditionExpr = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002484 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002485 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002486 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002487 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002488 return new JoinClause(joinType, rightExpr, rightVar, posVar, conditionExpr);
2489 }
2490}
2491
Yingyi Bu391f09e2015-10-29 13:49:39 -07002492UnnestClause UnnestClause(JoinType joinType) throws ParseException :
2493{
2494 Expression rightExpr;
2495 VariableExpr rightVar;
2496 VariableExpr posVar = null;
2497}
2498{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002499 (<UNNEST>|<CORRELATE>|<FLATTEN>) rightExpr = Expression() ((<AS>)? rightVar = Variable()) (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002500 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002501 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002502 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002503 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002504 return new UnnestClause(joinType, rightExpr, rightVar, posVar);
2505 }
2506}
2507
2508
2509JoinType JoinType() throws ParseException :
2510{
2511 JoinType joinType = JoinType.INNER;
2512}
2513{
2514 (<INNER>|<LEFT> (<OUTER>)? {joinType = JoinType.LEFTOUTER; })
2515 {
2516 return joinType;
2517 }
2518}
2519
2520List<LetClause> LetClause() throws ParseException:
2521{
2522 List<LetClause> letList = new ArrayList<LetClause>();
2523 LetClause letClause;
2524}
2525{
2526 (
2527 (<LET>|<LETTING>) letClause = LetElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = LetElement() { letList.add(letClause); })*
2528 |
2529 <WITH> letClause = WithElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = WithElement() { letList.add(letClause); })*
2530 )
2531 {
2532 return letList;
2533 }
2534}
2535
2536WhereClause WhereClause()throws ParseException :
2537{
2538 WhereClause wc = new WhereClause();
2539 Expression whereExpr;
2540}
2541{
2542 <WHERE> whereExpr = Expression()
2543 {
2544 wc.setWhereExpr(whereExpr);
2545 return wc;
2546 }
2547}
2548
2549OrderbyClause OrderbyClause()throws ParseException :
2550{
2551 OrderbyClause oc = new OrderbyClause();
2552 Expression orderbyExpr;
2553 List<Expression> orderbyList = new ArrayList<Expression>();
2554 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2555 int numOfOrderby = 0;
2556}
2557{
2558 <ORDER>
2559 {
2560 String hint = getHint(token);
2561 if (hint != null) {
2562 if (hint.startsWith(INMEMORY_HINT)) {
2563 String splits[] = hint.split(" +");
2564 int numFrames = Integer.parseInt(splits[1]);
2565 int numTuples = Integer.parseInt(splits[2]);
2566 oc.setNumFrames(numFrames);
2567 oc.setNumTuples(numTuples);
2568 }
2569 }
2570 }
2571 <BY> orderbyExpr = Expression()
2572 {
2573 orderbyList.add(orderbyExpr);
2574 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2575 }
2576 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2577 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2578 {
2579 modifierList.add(modif);
2580 }
2581
2582 (LOOKAHEAD(2) <COMMA> orderbyExpr = Expression()
2583 {
2584 orderbyList.add(orderbyExpr);
2585 modif = OrderbyClause.OrderModifier.ASC;
2586 }
2587 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2588 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2589 {
2590 modifierList.add(modif);
2591 }
2592 )*
2593
2594 {
2595 oc.setModifierList(modifierList);
2596 oc.setOrderbyList(orderbyList);
2597 return oc;
2598 }
2599}
2600
2601GroupbyClause GroupbyClause()throws ParseException :
2602{
2603 GroupbyClause gbc = new GroupbyClause();
2604 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2605 VariableExpr var = null;
2606 VariableExpr withVar = null;
2607 Expression expr = null;
2608 VariableExpr decorVar = null;
2609 Expression decorExpr = null;
Yingyi Buacc12a92016-03-26 17:25:05 -07002610
2611 VariableExpr groupVar = null;
2612 List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<Pair<Expression, Identifier>>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002613}
2614{
2615 {
2616 Scope newScope = extendCurrentScopeNoPush(true);
2617 // extendCurrentScope(true);
2618 }
2619 <GROUP>
2620 {
2621 String hint = getHint(token);
2622 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2623 gbc.setHashGroupByHint(true);
2624 }
2625 }
2626 <BY> (
2627 expr = Expression()
2628 (LOOKAHEAD(1) (<AS>)?
2629 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08002630 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002631 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002632 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002633 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002634 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002635 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2636 vePairList.add(pair1);
2637 }
2638 ( LOOKAHEAD(1) <COMMA>
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002639 {
2640 var = null;
2641 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002642 expr = Expression()
2643 (LOOKAHEAD(1) (<AS>)?
2644 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08002645 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002646 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002647 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002648 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002649 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002650 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2651 vePairList.add(pair2);
2652 }
2653 )*
2654 )
Yingyi Buacc12a92016-03-26 17:25:05 -07002655 (<GROUP> <AS> groupVar = Variable()
2656 ( LOOKAHEAD(1)
2657 {
2658 VariableExpr fieldVarExpr = null;
2659 String fieldIdentifierStr = null;
2660 }
2661 <LEFTPAREN>
2662 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
2663 {
2664 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
2665 }
2666 (<COMMA>
2667 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
2668 {
2669 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
2670 }
2671 )*
2672 <RIGHTPAREN>
2673 )?
2674 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002675 {
2676 gbc.setGbyPairList(vePairList);
2677 gbc.setDecorPairList(new ArrayList<GbyVariableExpressionPair>());
2678 gbc.setWithVarList(new ArrayList<VariableExpr>());
Yingyi Buacc12a92016-03-26 17:25:05 -07002679 gbc.setGroupVar(groupVar);
2680 gbc.setGroupFieldList(groupFieldList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002681 replaceCurrentScope(newScope);
2682 return gbc;
2683 }
2684}
2685
2686HavingClause HavingClause() throws ParseException:
2687{
2688 Expression filterExpr = null;
2689}
2690{
2691 <HAVING> filterExpr = Expression()
2692 {
2693 return new HavingClause(filterExpr);
2694 }
2695}
2696
2697LimitClause LimitClause() throws ParseException:
2698{
2699 LimitClause lc = new LimitClause();
2700 Expression expr;
2701 pushForbiddenScope(getCurrentScope());
2702}
2703{
2704 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
2705 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
2706
2707 {
2708 popForbiddenScope();
2709 return lc;
2710 }
2711}
2712
2713QuantifiedExpression QuantifiedExpression()throws ParseException:
2714{
2715 QuantifiedExpression qc = new QuantifiedExpression();
2716 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2717 Expression satisfiesExpr;
2718 VariableExpr var;
2719 Expression inExpr;
2720 QuantifiedPair pair;
2721}
2722{
2723 {
2724 createNewScope();
2725 }
2726
Michael Blowd6cf6412016-06-30 02:44:35 -04002727 ( (<SOME> { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2728 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
Yingyi Bu391f09e2015-10-29 13:49:39 -07002729 var = Variable() <IN> inExpr = Expression()
2730 {
2731 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002732 quantifiedList.add(pair);
2733 }
2734 (
2735 <COMMA> var = Variable() <IN> inExpr = Expression()
2736 {
2737 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002738 quantifiedList.add(pair);
2739 }
2740 )*
2741 <SATISFIES> satisfiesExpr = Expression()
2742 {
2743 qc.setSatisfiesExpr(satisfiesExpr);
2744 qc.setQuantifiedList(quantifiedList);
2745 removeCurrentScope();
2746 return qc;
2747 }
2748}
2749
2750LetClause LetElement() throws ParseException:
2751{
2752 LetClause lc = new LetClause();
2753 VariableExpr varExp;
2754 Expression beExp;
2755 extendCurrentScope();
2756}
2757{
2758 varExp = Variable() <EQ> beExp = Expression()
2759 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002760 lc.setVarExpr(varExp);
2761 lc.setBindingExpr(beExp);
2762 return lc;
2763 }
2764}
2765
2766LetClause WithElement() throws ParseException:
2767{
2768 LetClause lc = new LetClause();
2769 VariableExpr varExp;
2770 Expression beExp;
2771 extendCurrentScope();
2772}
2773{
2774 varExp = Variable() <AS> beExp = Expression()
2775 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002776 lc.setVarExpr(varExp);
2777 lc.setBindingExpr(beExp);
2778 return lc;
2779 }
2780}
2781
2782TOKEN_MGR_DECLS:
2783{
2784 public int commentDepth = 0;
2785 public IntStack lexerStateStack = new IntStack();
2786
2787 public void pushState() {
2788 lexerStateStack.push( curLexState );
2789 }
2790
2791 public void popState(String token) {
2792 if (lexerStateStack.size() > 0) {
2793 SwitchTo( lexerStateStack.pop() );
2794 } else {
2795 int errorLine = input_stream.getEndLine();
2796 int errorColumn = input_stream.getEndColumn();
2797 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
2798 + "\" but state stack is empty.";
2799 throw new TokenMgrError(msg, -1);
2800 }
2801 }
2802}
2803
2804<DEFAULT,IN_DBL_BRACE>
2805TOKEN [IGNORE_CASE]:
2806{
2807 <ALL : "all">
2808 | <AND : "and">
2809 | <APPLY : "apply">
2810 | <AS : "as">
2811 | <ASC : "asc">
2812 | <AT : "at">
2813 | <AUTOGENERATED : "autogenerated">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07002814 | <BETWEEN : "between">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002815 | <BTREE : "btree">
2816 | <BY : "by">
2817 | <CASE : "case">
2818 | <CLOSED : "closed">
2819 | <CREATE : "create">
2820 | <COMPACTION : "compaction">
2821 | <COMPACT : "compact">
2822 | <CONNECT : "connect">
2823 | <CORRELATE : "correlate">
2824 | <DATASET : "table">
Yingyi Bu1c0fff52016-03-25 20:23:30 -07002825 | <COLLECTION : "collection">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002826 | <DATAVERSE : "database">
2827 | <DECLARE : "declare">
2828 | <DEFINITION : "definition">
2829 | <DELETE : "delete">
2830 | <DESC : "desc">
2831 | <DISCONNECT : "disconnect">
2832 | <DISTINCT : "distinct">
2833 | <DROP : "drop">
2834 | <ELEMENT : "element">
2835 | <ELSE : "else">
2836 | <ENFORCED : "enforced">
Yingyi Buc8c067c2016-07-25 23:37:19 -07002837 | <END : "end">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002838 | <EVERY : "every">
2839 | <EXCEPT : "except">
2840 | <EXISTS : "exists">
2841 | <EXTERNAL : "external">
2842 | <FEED : "feed">
2843 | <FILTER : "filter">
2844 | <FLATTEN : "flatten">
2845 | <FOR : "for">
2846 | <FORMAT : "format">
2847 | <FROM : "from">
2848 | <FULL : "full">
2849 | <FUNCTION : "function">
2850 | <GROUP : "group">
2851 | <HAVING : "having">
2852 | <HINTS : "hints">
2853 | <IF : "if">
2854 | <INTO : "into">
2855 | <IN : "in">
2856 | <INDEX : "index">
2857 | <INGESTION : "ingestion">
2858 | <INNER : "inner">
2859 | <INSERT : "insert">
2860 | <INTERNAL : "internal">
2861 | <INTERSECT : "intersect">
Yingyi Budaa549c2016-06-28 22:30:52 -07002862 | <IS : "is">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002863 | <JOIN : "join">
2864 | <KEYWORD : "keyword">
2865 | <KEY : "key">
2866 | <LEFT : "left">
2867 | <LETTING : "letting">
2868 | <LET : "let">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07002869 | <LIKE : "like">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002870 | <LIMIT : "limit">
2871 | <LOAD : "load">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002872 | <NODEGROUP : "nodegroup">
2873 | <NGRAM : "ngram">
Yingyi Budaa549c2016-06-28 22:30:52 -07002874 | <NOT : "not">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002875 | <OFFSET : "offset">
2876 | <ON : "on">
2877 | <OPEN : "open">
2878 | <OR : "or">
2879 | <ORDER : "order">
2880 | <OUTER : "outer">
2881 | <OUTPUT : "output">
2882 | <PATH : "path">
2883 | <POLICY : "policy">
2884 | <PRESORTED : "pre-sorted">
2885 | <PRIMARY : "primary">
2886 | <RAW : "raw">
2887 | <REFRESH : "refresh">
2888 | <RETURN : "return">
2889 | <RTREE : "rtree">
2890 | <RUN : "run">
2891 | <SATISFIES : "satisfies">
2892 | <SECONDARY : "secondary">
2893 | <SELECT : "select">
2894 | <SET : "set">
2895 | <SOME : "some">
2896 | <TEMPORARY : "temporary">
2897 | <THEN : "then">
2898 | <TYPE : "type">
2899 | <TO : "to">
2900 | <UNION : "union">
Yingyi Budaa549c2016-06-28 22:30:52 -07002901 | <UNKOWN : "unknown">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002902 | <UNNEST : "unnest">
Yingyi Budaa549c2016-06-28 22:30:52 -07002903 | <UPDATE : "update">
2904 | <USE : "use">
2905 | <USING : "using">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002906 | <VALUE : "value">
2907 | <WHEN : "when">
2908 | <WHERE : "where">
2909 | <WITH : "with">
2910 | <WRITE : "write">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002911}
2912
2913<DEFAULT,IN_DBL_BRACE>
2914TOKEN :
2915{
2916 <CARET : "^">
2917 | <DIV : "/">
2918 | <IDIV : "idiv">
2919 | <MINUS : "-">
2920 | <MOD : "%">
2921 | <MUL : "*">
2922 | <PLUS : "+">
2923
2924 | <LEFTPAREN : "(">
2925 | <RIGHTPAREN : ")">
2926 | <LEFTBRACKET : "[">
2927 | <RIGHTBRACKET : "]">
2928
2929 | <ATT : "@">
2930 | <COLON : ":">
2931 | <COMMA : ",">
2932 | <DOT : ".">
2933 | <QUES : "?">
2934 | <SEMICOLON : ";">
2935 | <SHARP : "#">
2936
2937 | <LT : "<">
2938 | <GT : ">">
2939 | <LE : "<=">
2940 | <GE : ">=">
2941 | <EQ : "=">
2942 | <NE : "!=">
2943 | <SIMILAR : "~=">
2944}
2945
2946<DEFAULT,IN_DBL_BRACE>
2947TOKEN :
2948{
2949 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
2950}
2951
2952<DEFAULT>
2953TOKEN :
2954{
2955 <RIGHTBRACE : "}"> { popState("}"); }
2956}
2957
2958<DEFAULT,IN_DBL_BRACE>
2959TOKEN :
2960{
2961 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
2962}
2963
2964<IN_DBL_BRACE>
2965TOKEN :
2966{
2967 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
2968}
2969
2970<DEFAULT,IN_DBL_BRACE>
2971TOKEN :
2972{
2973 <INTEGER_LITERAL : (<DIGIT>)+ >
2974}
2975
2976<DEFAULT,IN_DBL_BRACE>
Yingyi Bue311a632016-06-07 18:23:16 -07002977TOKEN [IGNORE_CASE]:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002978{
Yingyi Bu535d86b2016-05-23 16:44:25 -07002979 <MISSING : "missing">
2980 | <NULL : "null">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002981 | <TRUE : "true">
2982 | <FALSE : "false">
2983}
2984
2985<DEFAULT,IN_DBL_BRACE>
2986TOKEN :
2987{
2988 <#DIGIT : ["0" - "9"]>
2989}
2990
2991<DEFAULT,IN_DBL_BRACE>
2992TOKEN:
2993{
2994 < DOUBLE_LITERAL: <DIGITS>
2995 | <DIGITS> ( "." <DIGITS> )?
2996 | "." <DIGITS>
2997 >
2998 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
2999 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
3000 | "." <DIGITS> ( "f" | "F" )
3001 >
3002 | <DIGITS : (<DIGIT>)+ >
3003}
3004
3005<DEFAULT,IN_DBL_BRACE>
3006TOKEN :
3007{
3008 <#LETTER : ["A" - "Z", "a" - "z"]>
3009 | <SPECIALCHARS : ["$", "_"]>
3010}
3011
3012<DEFAULT,IN_DBL_BRACE>
3013TOKEN :
3014{
3015 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
Yingyi Bu6d57e492016-06-06 21:24:42 -07003016 <QUOTED_STRING : "`" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003017 <EscapeQuot>
3018 | <EscapeBslash>
3019 | <EscapeSlash>
3020 | <EscapeBspace>
3021 | <EscapeFormf>
3022 | <EscapeNl>
3023 | <EscapeCr>
3024 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003025 | ~["`","\\"])* "`">
3026 | <STRING_LITERAL : ("\"" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003027 <EscapeQuot>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003028 | <EscapeBslash>
3029 | <EscapeSlash>
3030 | <EscapeBspace>
3031 | <EscapeFormf>
3032 | <EscapeNl>
3033 | <EscapeCr>
3034 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003035 | ~["\"","\\"])* "\"")
3036 | ("\'"(
3037 <EscapeApos>
3038 | <EscapeBslash>
3039 | <EscapeSlash>
3040 | <EscapeBspace>
3041 | <EscapeFormf>
3042 | <EscapeNl>
3043 | <EscapeCr>
3044 | <EscapeTab>
3045 | ~["\'","\\"])* "\'")>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003046 | < #EscapeQuot: "\\\"" >
3047 | < #EscapeApos: "\\\'" >
3048 | < #EscapeBslash: "\\\\" >
3049 | < #EscapeSlash: "\\/" >
3050 | < #EscapeBspace: "\\b" >
3051 | < #EscapeFormf: "\\f" >
3052 | < #EscapeNl: "\\n" >
3053 | < #EscapeCr: "\\r" >
3054 | < #EscapeTab: "\\t" >
3055}
3056
3057<DEFAULT,IN_DBL_BRACE>
3058TOKEN :
3059{
3060 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
3061}
3062
3063<DEFAULT,IN_DBL_BRACE>
3064SKIP:
3065{
3066 " "
3067 | "\t"
3068 | "\r"
3069 | "\n"
3070}
3071
3072<DEFAULT,IN_DBL_BRACE>
3073SKIP:
3074{
3075 <"//" (~["\n"])* "\n">
3076}
3077
3078<DEFAULT,IN_DBL_BRACE>
3079SKIP:
3080{
3081 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3082}
3083
3084<DEFAULT,IN_DBL_BRACE>
3085SKIP:
3086{
3087 <"/*"> { pushState(); } : INSIDE_COMMENT
3088}
3089
3090<INSIDE_COMMENT>
3091SPECIAL_TOKEN:
3092{
3093 <"+"(" ")*(~["*"])*>
3094}
3095
3096<INSIDE_COMMENT>
3097SKIP:
3098{
3099 <"/*"> { pushState(); }
3100}
3101
3102<INSIDE_COMMENT>
3103SKIP:
3104{
3105 <"*/"> { popState("*/"); }
3106 | <~[]>
3107}