blob: f4cdfb8650cd1ea02736f20dc078608ae45fc4fb [file] [log] [blame]
Yingyi Bu391f09e2015-10-29 13:49:39 -07001options {
2
3
4 STATIC = false;
5
6}
7
8
9PARSER_BEGIN(AQLParser)
10
11package org.apache.asterix.lang.aql.parser;
12
13// For AQLParserTokenManager
Till Westmanne9b2adf2016-10-15 12:39:01 -070014import java.util.ArrayDeque;
Yingyi Bu391f09e2015-10-29 13:49:39 -070015
16import java.io.BufferedReader;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.io.Reader;
23import java.io.StringReader;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.LinkedHashMap;
27import java.util.List;
28import java.util.Map;
29
30import org.apache.asterix.common.annotations.AutoDataGen;
31import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
32import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
33import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
34import org.apache.asterix.common.annotations.FieldIntervalDataGen;
35import org.apache.asterix.common.annotations.FieldValFileDataGen;
36import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
37import org.apache.asterix.common.annotations.IRecordFieldDataGen;
38import org.apache.asterix.common.annotations.InsertRandIntDataGen;
39import org.apache.asterix.common.annotations.ListDataGen;
40import org.apache.asterix.common.annotations.ListValFileDataGen;
41import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
42import org.apache.asterix.common.annotations.TypeDataGen;
43import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
44import org.apache.asterix.common.config.DatasetConfig.DatasetType;
45import org.apache.asterix.common.config.DatasetConfig.IndexType;
Taewoo Kime65e6ca2017-01-14 17:53:28 -080046import org.apache.asterix.common.exceptions.CompilationException;
Yingyi Bu391f09e2015-10-29 13:49:39 -070047import org.apache.asterix.common.functions.FunctionSignature;
48import org.apache.asterix.lang.aql.clause.DistinctClause;
49import org.apache.asterix.lang.aql.clause.ForClause;
50import org.apache.asterix.lang.aql.expression.FLWOGRExpression;
51import org.apache.asterix.lang.aql.expression.UnionExpr;
52import org.apache.asterix.lang.aql.util.RangeMapBuilder;
Yingyi Bucb5bf332017-01-02 22:19:50 -080053import org.apache.asterix.lang.aql.util.AQLFormatPrintUtil;
Yingyi Bu391f09e2015-10-29 13:49:39 -070054import org.apache.asterix.lang.common.base.Clause;
55import org.apache.asterix.lang.common.base.Expression;
Yingyi Bucb5bf332017-01-02 22:19:50 -080056import org.apache.asterix.lang.common.base.ILangExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -070057import org.apache.asterix.lang.common.base.IParser;
58import org.apache.asterix.lang.common.base.Literal;
59import org.apache.asterix.lang.common.base.Statement;
60import org.apache.asterix.lang.common.clause.GroupbyClause;
61import org.apache.asterix.lang.common.clause.LetClause;
62import org.apache.asterix.lang.common.clause.LimitClause;
63import org.apache.asterix.lang.common.clause.OrderbyClause;
64import org.apache.asterix.lang.common.clause.UpdateClause;
65import org.apache.asterix.lang.common.clause.WhereClause;
66import org.apache.asterix.lang.common.context.RootScopeFactory;
67import org.apache.asterix.lang.common.context.Scope;
68import org.apache.asterix.lang.common.expression.AbstractAccessor;
69import org.apache.asterix.lang.common.expression.CallExpr;
70import org.apache.asterix.lang.common.expression.FieldAccessor;
71import org.apache.asterix.lang.common.expression.FieldBinding;
72import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
73import org.apache.asterix.lang.common.expression.IfExpr;
74import org.apache.asterix.lang.common.expression.IndexAccessor;
75import org.apache.asterix.lang.common.expression.ListConstructor;
76import org.apache.asterix.lang.common.expression.LiteralExpr;
77import org.apache.asterix.lang.common.expression.OperatorExpr;
78import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
79import org.apache.asterix.lang.common.expression.QuantifiedExpression;
80import org.apache.asterix.lang.common.expression.RecordConstructor;
81import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
82import org.apache.asterix.lang.common.expression.TypeExpression;
83import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
84import org.apache.asterix.lang.common.expression.UnaryExpr;
85import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
86import org.apache.asterix.lang.common.expression.VariableExpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -070087import org.apache.asterix.lang.common.literal.DoubleLiteral;
88import org.apache.asterix.lang.common.literal.FalseLiteral;
89import org.apache.asterix.lang.common.literal.FloatLiteral;
90import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
Yingyi Bu535d86b2016-05-23 16:44:25 -070091import org.apache.asterix.lang.common.literal.MissingLiteral;
Yingyi Bu391f09e2015-10-29 13:49:39 -070092import org.apache.asterix.lang.common.literal.NullLiteral;
93import org.apache.asterix.lang.common.literal.StringLiteral;
94import org.apache.asterix.lang.common.literal.TrueLiteral;
95import org.apache.asterix.lang.common.parser.ScopeChecker;
96import org.apache.asterix.lang.common.statement.CompactStatement;
97import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
98import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
99import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
100import org.apache.asterix.lang.common.statement.CreateFeedStatement;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800101import org.apache.asterix.lang.common.statement.StartFeedStatement;
102import org.apache.asterix.lang.common.statement.StopFeedStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700103import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
104import org.apache.asterix.lang.common.statement.CreateIndexStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700105import org.apache.asterix.lang.common.statement.DatasetDecl;
106import org.apache.asterix.lang.common.statement.DataverseDecl;
107import org.apache.asterix.lang.common.statement.DataverseDropStatement;
108import org.apache.asterix.lang.common.statement.DeleteStatement;
109import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
Yingyi Buab817482016-08-19 21:29:31 -0700110import org.apache.asterix.lang.common.statement.DropDatasetStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700111import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
112import org.apache.asterix.lang.common.statement.FeedDropStatement;
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300113import org.apache.asterix.lang.common.statement.FeedPolicyDropStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700114import org.apache.asterix.lang.common.statement.FunctionDecl;
115import org.apache.asterix.lang.common.statement.FunctionDropStatement;
116import org.apache.asterix.lang.common.statement.IndexDropStatement;
117import org.apache.asterix.lang.common.statement.InsertStatement;
118import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
119import org.apache.asterix.lang.common.statement.LoadStatement;
120import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
121import org.apache.asterix.lang.common.statement.NodegroupDecl;
122import org.apache.asterix.lang.common.statement.Query;
123import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
124import org.apache.asterix.lang.common.statement.RunStatement;
125import org.apache.asterix.lang.common.statement.SetStatement;
126import org.apache.asterix.lang.common.statement.TypeDecl;
127import org.apache.asterix.lang.common.statement.TypeDropStatement;
128import org.apache.asterix.lang.common.statement.UpdateStatement;
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300129import org.apache.asterix.lang.common.statement.UpsertStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700130import org.apache.asterix.lang.common.statement.WriteStatement;
131import org.apache.asterix.lang.common.struct.Identifier;
132import org.apache.asterix.lang.common.struct.QuantifiedPair;
133import org.apache.asterix.lang.common.struct.VarIdentifier;
Yingyi Buab817482016-08-19 21:29:31 -0700134import org.apache.asterix.metadata.utils.MetadataConstants;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700135import org.apache.hyracks.algebricks.common.utils.Pair;
136import org.apache.hyracks.algebricks.common.utils.Triple;
137import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
138import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
139import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
140
141
Yingyi Bucaea8f02015-11-16 15:12:15 -0800142class AQLParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700143
144 // optimizer hints
145 private static final String AUTO_HINT = "auto";
146 private static final String BROADCAST_JOIN_HINT = "bcast";
147 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
148 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
149 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
150 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
151 private static final String HASH_GROUP_BY_HINT = "hash";
152 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
153 private static final String INMEMORY_HINT = "inmem";
154 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
155 private static final String INTERVAL_HINT = "interval";
156 private static final String LIST_HINT = "list";
157 private static final String LIST_VAL_FILE_HINT = "list-val-file";
158 private static final String RANGE_HINT = "range";
159 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
160 private static final String VAL_FILE_HINT = "val-files";
161 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
Yingyi Bu391f09e2015-10-29 13:49:39 -0700162 private static final String GEN_FIELDS_HINT = "gen-fields";
Yingyi Bu391f09e2015-10-29 13:49:39 -0700163 // data generator hints
164 private static final String DGEN_HINT = "dgen";
165
166 private static class IndexParams {
167 public IndexType type;
168 public int gramLength;
169
170 public IndexParams(IndexType type, int gramLength) {
171 this.type = type;
172 this.gramLength = gramLength;
173 }
174 };
175
176 private static class FunctionName {
177 public String dataverse = null;
178 public String library = null;
179 public String function = null;
180 public String hint = null;
181 }
182
183 private static String getHint(Token t) {
184 if (t.specialToken == null) {
185 return null;
186 }
187 String s = t.specialToken.image;
188 int n = s.length();
189 if (n < 2) {
190 return null;
191 }
192 return s.substring(1).trim();
193 }
194
Yingyi Bucb5bf332017-01-02 22:19:50 -0800195 private static void checkBindingVariable(Expression returnExpression, VariableExpr var,
196 ILangExpression bodyExpression) throws ParseException {
197 if (returnExpression != null && var == null) {
198 try {
199 throw new ParseException("Need a binding variable for the enclosed expression: " +
200 AQLFormatPrintUtil.toString(bodyExpression));
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800201 } catch (CompilationException e){
Yingyi Bucb5bf332017-01-02 22:19:50 -0800202 throw new ParseException(e.getLocalizedMessage());
203 }
204 }
205 }
206
Yingyi Bu391f09e2015-10-29 13:49:39 -0700207 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
208 IRecordFieldDataGen rfdg = null;
209 String splits[] = hint.split(" +");
210 if (splits[0].equals(VAL_FILE_HINT)) {
211 File[] valFiles = new File[splits.length - 1];
212 for (int k=1; k<splits.length; k++) {
213 valFiles[k-1] = new File(splits[k]);
214 }
215 rfdg = new FieldValFileDataGen(valFiles);
216 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
217 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
218 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
219 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
220 } else if (splits[0].equals(LIST_HINT)) {
221 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
222 } else if (splits[0].equals(INTERVAL_HINT)) {
223 FieldIntervalDataGen.ValueType vt;
224 if (splits[1].equals("int")) {
225 vt = FieldIntervalDataGen.ValueType.INT;
226 } else if (splits[1].equals("long")) {
227 vt = FieldIntervalDataGen.ValueType.LONG;
228 } else if (splits[1].equals("float")) {
229 vt = FieldIntervalDataGen.ValueType.FLOAT;
230 } else if (splits[1].equals("double")) {
231 vt = FieldIntervalDataGen.ValueType.DOUBLE;
232 } else {
233 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
234 }
235 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
236 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
237 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
238 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
239 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
240 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
241 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
242 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
243 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
244 } else if (splits[0].equals(AUTO_HINT)) {
245 rfdg = new AutoDataGen(splits[1]);
246 }
247 return rfdg;
248 }
249
250 public AQLParser(String s){
251 this(new StringReader(s));
252 super.setInput(s);
253 }
254
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800255 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700256 File file = new File(args[0]);
257 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
258 AQLParser parser = new AQLParser(fis);
259 List<Statement> st = parser.parse();
260 //st.accept(new AQLPrintVisitor(), 0);
261 }
262
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800263 public List<Statement> parse() throws CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700264 try {
265 return Statement();
266 } catch (Error e) {
267 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
268 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800269 throw new CompilationException(new ParseException(e.getMessage()));
Yingyi Bu391f09e2015-10-29 13:49:39 -0700270 } catch (ParseException e){
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800271 throw new CompilationException(e.getMessage());
Yingyi Bu391f09e2015-10-29 13:49:39 -0700272 }
273 }
274}
275
276PARSER_END(AQLParser)
277
278
279List<Statement> Statement() throws ParseException:
280{
281 scopeStack.push(RootScopeFactory.createRootScope(this));
282 List<Statement> decls = new ArrayList<Statement>();
283 Statement stmt = null;
284}
285{
286 ( stmt = SingleStatement() (";") ?
287 {
288 decls.add(stmt);
289 }
290 )*
291 <EOF>
292 {
293 return decls;
294 }
295}
296
297Statement SingleStatement() throws ParseException:
298{
299 Statement stmt = null;
300}
301{
302 (
303 stmt = DataverseDeclaration()
304 | stmt = FunctionDeclaration()
305 | stmt = CreateStatement()
306 | stmt = LoadStatement()
307 | stmt = DropStatement()
308 | stmt = WriteStatement()
309 | stmt = SetStatement()
310 | stmt = InsertStatement()
Yingyi Bucb5bf332017-01-02 22:19:50 -0800311 | stmt = UpsertStatement()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700312 | stmt = DeleteStatement()
313 | stmt = UpdateStatement()
314 | stmt = FeedStatement()
315 | stmt = CompactStatement()
316 | stmt = Query()
317 | stmt = RefreshExternalDatasetStatement()
318 | stmt = RunStatement()
319 )
320 {
321 return stmt;
322 }
323}
324
325DataverseDecl DataverseDeclaration() throws ParseException:
326{
327 String dvName = null;
328}
329{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400330 <USE> <DATAVERSE> dvName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700331 {
332 defaultDataverse = dvName;
333 return new DataverseDecl(new Identifier(dvName));
334 }
335}
336
337Statement CreateStatement() throws ParseException:
338{
339 String hint = null;
340 boolean dgen = false;
341 Statement stmt = null;
342}
343{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400344 <CREATE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700345 (
346 {
347 hint = getHint(token);
348 if (hint != null && hint.startsWith(DGEN_HINT)) {
349 dgen = true;
350 }
351 }
352 stmt = TypeSpecification(hint, dgen)
353 | stmt = NodegroupSpecification()
354 | stmt = DatasetSpecification()
355 | stmt = IndexSpecification()
356 | stmt = DataverseSpecification()
357 | stmt = FunctionSpecification()
358 | stmt = FeedSpecification()
359 | stmt = FeedPolicySpecification()
360 )
361 {
362 return stmt;
363 }
364}
365
366TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
367{
368 Pair<Identifier,Identifier> nameComponents = null;
369 boolean ifNotExists = false;
370 TypeExpression typeExpr = null;
371}
372{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400373 <TYPE> nameComponents = TypeName() ifNotExists = IfNotExists()
374 <AS> typeExpr = TypeExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700375 {
376 long numValues = -1;
377 String filename = null;
378 if (dgen) {
379 String splits[] = hint.split(" +");
380 if (splits.length != 3) {
381 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
382 }
383 filename = splits[1];
384 numValues = Long.parseLong(splits[2]);
385 }
386 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
387 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
388 }
389}
390
391
392NodegroupDecl NodegroupSpecification() throws ParseException:
393{
394 String name = null;
395 String tmp = null;
396 boolean ifNotExists = false;
397 List<Identifier>ncNames = null;
398}
399{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400400 <NODEGROUP> name = Identifier()
401 ifNotExists = IfNotExists() <ON> tmp = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700402 {
403 ncNames = new ArrayList<Identifier>();
404 ncNames.add(new Identifier(tmp));
405 }
406 ( <COMMA> tmp = Identifier()
407 {
408 ncNames.add(new Identifier(tmp));
409 }
410 )*
411 {
412 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
413 }
414}
415
416DatasetDecl DatasetSpecification() throws ParseException:
417{
418 Pair<Identifier,Identifier> nameComponents = null;
419 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800420 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700421 String adapterName = null;
422 Map<String,String> properties = null;
423 Map<String,String> compactionPolicyProperties = null;
424 FunctionSignature appliedFunction = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800425 Pair<List<Integer>, List<List<String>>> primaryKeyFields = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700426 String nodeGroupName = null;
427 Map<String,String> hints = new HashMap<String,String>();
428 DatasetDecl dsetDecl = null;
429 boolean autogenerated = false;
430 String compactionPolicy = null;
431 boolean temp = false;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800432 Pair<Integer, List<String>> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800433 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700434}
435{
436 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400437 <EXTERNAL> <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800438 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700439 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400440 <USING> adapterName = AdapterName() properties = Configuration()
441 ( <ON> nodeGroupName = Identifier() )?
442 ( <HINTS> hints = Properties() )?
443 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700444 {
445 ExternalDetailsDecl edd = new ExternalDetailsDecl();
446 edd.setAdapter(adapterName);
447 edd.setProperties(properties);
448 dsetDecl = new DatasetDecl(nameComponents.first,
449 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800450 typeComponents.first,
451 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800452 metaTypeComponents.first,
453 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700454 nodeGroupName != null? new Identifier(nodeGroupName): null,
455 compactionPolicy,
456 compactionPolicyProperties,
457 hints,
458 DatasetType.EXTERNAL,
459 edd,
460 ifNotExists);
461 }
462
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400463 | (<INTERNAL> | <TEMPORARY> {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700464 temp = token.image.toLowerCase().equals("temporary");
465 }
Yingyi Bub9169b62016-02-26 21:21:49 -0800466 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700467 <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800468 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800469 (
470 { String name; }
471 <WITH>
472 name = Identifier()
473 {
474 if(!name.equals("meta")){
475 throw new ParseException("We can only support one additional associated field called \"meta\".");
476 }
477 }
478 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
479 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700480 ifNotExists = IfNotExists()
481 primaryKeyFields = PrimaryKey()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400482 ( <AUTOGENERATED> { autogenerated = true; } )?
483 ( <ON> nodeGroupName = Identifier() )?
484 ( <HINTS> hints = Properties() )?
485 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
486 ( <WITH> <FILTER> <ON> filterField = NestedField() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700487 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800488 if(filterField!=null && filterField.first!=0){
489 throw new ParseException("A filter field can only be a field in the main record of the dataset.");
490 }
491 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields.second,
492 primaryKeyFields.first,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700493 autogenerated,
Yingyi Buc9bfe252016-03-01 00:02:40 -0800494 filterField == null? null : filterField.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700495 temp);
496 dsetDecl = new DatasetDecl(nameComponents.first,
497 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800498 typeComponents.first,
499 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800500 metaTypeComponents.first,
501 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700502 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
503 compactionPolicy,
504 compactionPolicyProperties,
505 hints,
506 DatasetType.INTERNAL,
507 idd,
508 ifNotExists);
509 }
510 )
511 {
512 return dsetDecl;
513 }
514}
515
516RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
517{
518 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
519 Pair<Identifier,Identifier> nameComponents = null;
520 String datasetName = null;
521}
522{
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +0300523 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400524 <REFRESH> <EXTERNAL> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700525 {
526 redss.setDataverseName(nameComponents.first);
527 redss.setDatasetName(nameComponents.second);
528 return redss;
529 }
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +0300530 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700531}
532
533RunStatement RunStatement() throws ParseException:
534{
535 String system = null;
536 String tmp;
537 ArrayList<String> parameters = new ArrayList<String>();
538 Pair<Identifier,Identifier> nameComponentsFrom = null;
539 Pair<Identifier,Identifier> nameComponentsTo = null;
540}
541{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400542 <RUN> system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
Yingyi Bu391f09e2015-10-29 13:49:39 -0700543 {
544 parameters.add(tmp);
545 }
546 )*<RIGHTPAREN>
547 <FROM> <DATASET> nameComponentsFrom = QualifiedName()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400548 <TO> <DATASET> nameComponentsTo = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700549 {
550 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
551 }
552}
553
554CreateIndexStatement IndexSpecification() throws ParseException:
555{
556 CreateIndexStatement cis = new CreateIndexStatement();
557 String indexName = null;
558 boolean ifNotExists = false;
559 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800560 Pair<Integer, Pair<List<String>, TypeExpression>> fieldPair = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700561 IndexParams indexType = null;
562 boolean enforced = false;
563}
564{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400565 <INDEX> indexName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700566 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400567 <ON> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700568 <LEFTPAREN> ( fieldPair = OpenField()
569 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800570 cis.addFieldExprPair(fieldPair.second);
571 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700572 }
573 ) (<COMMA> fieldPair = OpenField()
574 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800575 cis.addFieldExprPair(fieldPair.second);
576 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700577 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400578 )* <RIGHTPAREN> ( <TYPE> indexType = IndexType() )? ( <ENFORCED> { enforced = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700579 {
580 cis.setIndexName(new Identifier(indexName));
581 cis.setIfNotExists(ifNotExists);
582 cis.setDataverseName(nameComponents.first);
583 cis.setDatasetName(nameComponents.second);
584 if (indexType != null) {
585 cis.setIndexType(indexType.type);
586 cis.setGramLength(indexType.gramLength);
587 }
588 cis.setEnforced(enforced);
589 return cis;
590 }
591}
592
593String CompactionPolicy() throws ParseException :
594{
595 String compactionPolicy = null;
596}
597{
598 compactionPolicy = Identifier()
599 {
600 return compactionPolicy;
601 }
602}
603
604String FilterField() throws ParseException :
605{
606 String filterField = null;
607}
608{
609 filterField = Identifier()
610 {
611 return filterField;
612 }
613}
614
615IndexParams IndexType() throws ParseException:
616{
617 IndexType type = null;
618 int gramLength = 0;
619}
620{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400621 (
622 <BTREE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700623 {
624 type = IndexType.BTREE;
625 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400626 |<RTREE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700627 {
628 type = IndexType.RTREE;
629 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400630 |<KEYWORD>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700631 {
632 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
633 }
Taewoo Kimc49405a2017-01-04 00:30:43 -0800634 |<FULLTEXT>
635 {
636 type = IndexType.SINGLE_PARTITION_WORD_INVIX;
637 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400638 |<NGRAM> <LEFTPAREN> <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700639 {
640 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
641 gramLength = Integer.valueOf(token.image);
642 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400643 <RIGHTPAREN>
644 )
645 {
646 return new IndexParams(type, gramLength);
647 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700648}
649
650CreateDataverseStatement DataverseSpecification() throws ParseException :
651{
652 String dvName = null;
653 boolean ifNotExists = false;
654 String format = null;
655}
656{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400657 <DATAVERSE> dvName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700658 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400659 ( <WITH> <FORMAT> format = StringLiteral() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700660 {
661 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
662 }
663}
664
665CreateFunctionStatement FunctionSpecification() throws ParseException:
666{
667 FunctionSignature signature;
668 boolean ifNotExists = false;
669 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
670 String functionBody;
671 VarIdentifier var = null;
672 Expression functionBodyExpr;
673 Token beginPos;
674 Token endPos;
675 FunctionName fctName = null;
676
677 createNewScope();
678}
679{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400680 <FUNCTION> fctName = FunctionName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700681 ifNotExists = IfNotExists()
682 paramList = ParameterList()
683 <LEFTBRACE>
684 {
685 beginPos = token;
686 }
687 functionBodyExpr = Expression() <RIGHTBRACE>
688 {
689 endPos = token;
690 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
691 // TODO use fctName.library
692 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
693 getCurrentScope().addFunctionDescriptor(signature, false);
694 removeCurrentScope();
695 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
696 }
697}
698
699CreateFeedStatement FeedSpecification() throws ParseException:
700{
701 Pair<Identifier,Identifier> nameComponents = null;
702 boolean ifNotExists = false;
703 String adapterName = null;
704 Map<String,String> properties = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700705 CreateFeedStatement cfs = null;
706 Pair<Identifier,Identifier> sourceNameComponents = null;
Michael Blowd6cf6412016-06-30 02:44:35 -0400707
Yingyi Bu391f09e2015-10-29 13:49:39 -0700708}
709{
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800710 <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
711 <USING> adapterName = AdapterName() properties = Configuration()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400712 {
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800713 cfs = new CreateFeedStatement(nameComponents, adapterName, properties, ifNotExists);
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400714 return cfs;
715 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700716}
717
718CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
719{
Michael Blowd6cf6412016-06-30 02:44:35 -0400720 String policyName = null;
721 String basePolicyName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700722 String sourcePolicyFile = null;
723 String definition = null;
724 boolean ifNotExists = false;
725 Map<String,String> properties = null;
726 CreateFeedPolicyStatement cfps = null;
727}
728{
729 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400730 <INGESTION> <POLICY> policyName = Identifier() ifNotExists = IfNotExists()
731 <FROM>
732 (
733 <POLICY> basePolicyName = Identifier() properties = Configuration() ( <DEFINITION> definition = StringLiteral())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700734 {
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400735 cfps = new CreateFeedPolicyStatement(policyName, basePolicyName, properties, definition, ifNotExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700736 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300737 |<PATH> sourcePolicyFile = StringLiteral() (<DEFINITION> definition = StringLiteral())?
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400738 {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700739 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400740 }
741 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700742 )
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400743 {
744 return cfps;
745 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700746}
747
748
749
750List<VarIdentifier> ParameterList() throws ParseException:
751{
752 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
753 VarIdentifier var = null;
754}
755{
756 <LEFTPAREN> (<VARIABLE>
757 {
758 var = new VarIdentifier();
759 var.setValue(token.image);
760 paramList.add(var);
761 getCurrentScope().addNewVarSymbolToScope(var);
762 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400763 ( <COMMA> <VARIABLE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700764 {
765 var = new VarIdentifier();
766 var.setValue(token.image);
767 paramList.add(var);
768 getCurrentScope().addNewVarSymbolToScope(var);
769 }
770 )*)? <RIGHTPAREN>
771 {
772 return paramList;
773 }
774}
775
776boolean IfNotExists() throws ParseException:
777{
778}
779{
Yingyi Budcd91122016-08-10 12:13:36 -0700780 (
781 LOOKAHEAD(1)
782 <IF>
783 <IDENTIFIER>
784 (
785 {
786 if(!token.image.equals("not")){
787 throw new ParseException("Expect word \"not\" at line " + token.beginLine + ", column "
788 + token.beginColumn +"!");
789 }
790 }
791 )
792 <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700793 {
794 return true;
795 }
796 )?
797 {
798 return false;
799 }
800}
801
802FunctionSignature ApplyFunction() throws ParseException:
803{
804 FunctionName functioName = null;
805 FunctionSignature funcSig = null;
806}
807{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400808 <APPLY> <FUNCTION> functioName = FunctionName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700809 {
810 String fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
811 return new FunctionSignature(functioName.dataverse, fqFunctionName, 1);
812 }
813}
814
815String GetPolicy() throws ParseException:
816{
817 String policy = null;
818}
819{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400820 <USING> <POLICY> policy = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700821 {
822 return policy;
823 }
824
825}
826
827FunctionSignature FunctionSignature() throws ParseException:
828{
829 FunctionName fctName = null;
830 int arity = 0;
831}
832{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400833 fctName = FunctionName() <SYMBOLAT> <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700834 {
835 arity = new Integer(token.image);
836 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
837 throw new ParseException(" invalid arity:" + arity);
838 }
839
840 // TODO use fctName.library
841 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
842 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
843 }
844}
845
Yingyi Buc9bfe252016-03-01 00:02:40 -0800846Pair<List<Integer>, List<List<String>>> PrimaryKey() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700847{
Yingyi Buc9bfe252016-03-01 00:02:40 -0800848 Pair<Integer, List<String>> tmp = null;
849 List<Integer> keyFieldSourceIndicators = new ArrayList<Integer>();
850 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
Yingyi Bu391f09e2015-10-29 13:49:39 -0700851}
852{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400853 <PRIMARY> <KEY> tmp = NestedField()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700854 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800855 keyFieldSourceIndicators.add(tmp.first);
856 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700857 }
858 ( <COMMA> tmp = NestedField()
859 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800860 keyFieldSourceIndicators.add(tmp.first);
861 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700862 }
863 )*
864 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800865 return new Pair<List<Integer>, List<List<String>>> (keyFieldSourceIndicators, primaryKeyFields);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700866 }
867}
868
869Statement DropStatement() throws ParseException:
870{
871 String id = null;
872 Pair<Identifier,Identifier> pairId = null;
873 Triple<Identifier,Identifier,Identifier> tripleId = null;
874 FunctionSignature funcSig = null;
875 boolean ifExists = false;
876 Statement stmt = null;
877}
878{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400879 <DROP>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700880 (
881 <DATASET> pairId = QualifiedName() ifExists = IfExists()
882 {
Yingyi Buab817482016-08-19 21:29:31 -0700883 stmt = new DropDatasetStatement(pairId.first, pairId.second, ifExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700884 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400885 | <INDEX> tripleId = DoubleQualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700886 {
887 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
888 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400889 | <NODEGROUP> id = Identifier() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700890 {
891 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
892 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400893 | <TYPE> pairId = TypeName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700894 {
895 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
896 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400897 | <DATAVERSE> id = Identifier() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700898 {
899 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
900 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400901 | <FUNCTION> funcSig = FunctionSignature() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700902 {
903 stmt = new FunctionDropStatement(funcSig, ifExists);
904 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400905 | <FEED> pairId = QualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700906 {
907 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
908 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300909 | <INGESTION> <POLICY> pairId = QualifiedName() ifExists = IfExists()
910 {
911 stmt = new FeedPolicyDropStatement(pairId.first, pairId.second, ifExists);
912 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700913 )
914 {
915 return stmt;
916 }
917}
918
919boolean IfExists() throws ParseException :
920{
921}
922{
Yingyi Budcd91122016-08-10 12:13:36 -0700923 (
924 LOOKAHEAD(1)
925 <IF> <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700926 {
927 return true;
928 }
929 )?
930 {
931 return false;
932 }
933}
934
935InsertStatement InsertStatement() throws ParseException:
936{
937 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800938 VariableExpr var = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700939 Query query;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800940 Expression returnExpression = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700941}
942{
Yingyi Bucb5bf332017-01-02 22:19:50 -0800943 <INSERT> <INTO> <DATASET> nameComponents = QualifiedName()
Steven Glenn Jacobsafa909a2016-10-16 10:35:30 -0700944 (<AS> var = Variable())?
945 {
946 if(var != null){
947 getCurrentScope().addNewVarSymbolToScope(var.getVar());
948 }
949 }
Yingyi Bucb5bf332017-01-02 22:19:50 -0800950 query = Query() ( <RETURNING> returnExpression = Expression())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700951 {
Yingyi Bucb5bf332017-01-02 22:19:50 -0800952 checkBindingVariable(returnExpression, var, query);
Yingyi Bucaea8f02015-11-16 15:12:15 -0800953 query.setTopLevel(true);
Yingyi Bucb5bf332017-01-02 22:19:50 -0800954 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
955 returnExpression);
956 }
957}
958
959UpsertStatement UpsertStatement() throws ParseException:
960{
961 Pair<Identifier,Identifier> nameComponents = null;
962 VariableExpr var = null;
963 Query query;
964 Expression returnExpression = null;
965}
966{
967 <UPSERT> <INTO> <DATASET> nameComponents = QualifiedName()
968 (<AS> var = Variable())?
969 {
970 if(var != null){
971 getCurrentScope().addNewVarSymbolToScope(var.getVar());
972 }
973 }
974 query = Query() ( <RETURNING> returnExpression = Expression())?
975 {
976 checkBindingVariable(returnExpression, var, query);
977 query.setTopLevel(true);
978 return new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
979 returnExpression);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700980 }
981}
982
983DeleteStatement DeleteStatement() throws ParseException:
984{
985 VariableExpr var = null;
986 Expression condition = null;
987 Pair<Identifier, Identifier> nameComponents;
988 // This is related to the new metadata lock management
989 setDataverses(new ArrayList<String>());
990 setDatasets(new ArrayList<String>());
991
992}
993{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400994 <DELETE> var = Variable()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700995 {
996 getCurrentScope().addNewVarSymbolToScope(var.getVar());
997 }
998 <FROM> <DATASET> nameComponents = QualifiedName()
999 (<WHERE> condition = Expression())?
1000 {
1001 // First we get the dataverses and datasets that we want to lock
1002 List<String> dataverses = getDataverses();
1003 List<String> datasets = getDatasets();
1004 // we remove the pointer to the dataverses and datasets
1005 setDataverses(null);
1006 setDatasets(null);
1007 return new DeleteStatement(var, nameComponents.first, nameComponents.second,
1008 condition, getVarCounter(), dataverses, datasets);
1009 }
1010}
1011
1012UpdateStatement UpdateStatement() throws ParseException:
1013{
1014 VariableExpr vars;
1015 Expression target;
1016 Expression condition;
1017 UpdateClause uc;
1018 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
1019}
1020{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001021 <UPDATE> vars = Variable() <IN> target = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001022 <WHERE> condition = Expression()
1023 <LEFTPAREN> (uc = UpdateClause()
1024 {
1025 ucs.add(uc);
1026 }
1027 (<COMMA> uc = UpdateClause()
1028 {
1029 ucs.add(uc);
1030 }
1031 )*) <RIGHTPAREN>
1032 {
1033 return new UpdateStatement(vars, target, condition, ucs);
1034 }
1035}
1036
1037UpdateClause UpdateClause() throws ParseException:
1038{
1039 Expression target = null;
1040 Expression value = null ;
1041 InsertStatement is = null;
1042 DeleteStatement ds = null;
1043 UpdateStatement us = null;
1044 Expression condition = null;
1045 UpdateClause ifbranch = null;
1046 UpdateClause elsebranch = null;
1047}
1048{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001049 (<SET> target = Expression() <ASSIGN> value = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001050 | is = InsertStatement()
1051 | ds = DeleteStatement()
1052 | us = UpdateStatement()
1053 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
1054 <THEN> ifbranch = UpdateClause()
1055 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
1056 {
1057 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
1058 }
1059 )
1060}
1061
1062Statement SetStatement() throws ParseException:
1063{
1064 String pn = null;
1065 String pv = null;
1066}
1067{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001068 <SET> pn = Identifier() pv = StringLiteral()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001069 {
1070 return new SetStatement(pn, pv);
1071 }
1072}
1073
1074Statement WriteStatement() throws ParseException:
1075{
1076 String nodeName = null;
1077 String fileName = null;
1078 Query query;
1079 String writerClass = null;
1080 Pair<Identifier,Identifier> nameComponents = null;
1081}
1082{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001083 <WRITE> <OUTPUT> <TO> nodeName = Identifier() <COLON> fileName = StringLiteral()
1084 ( <USING> writerClass = StringLiteral() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001085 {
1086 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
1087 }
1088}
1089
1090LoadStatement LoadStatement() throws ParseException:
1091{
1092 Identifier dataverseName = null;
1093 Identifier datasetName = null;
1094 boolean alreadySorted = false;
1095 String adapterName;
1096 Map<String,String> properties;
1097 Pair<Identifier,Identifier> nameComponents = null;
1098}
1099{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001100 <LOAD> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001101 {
1102 dataverseName = nameComponents.first;
1103 datasetName = nameComponents.second;
1104 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001105 <USING> adapterName = AdapterName() properties = Configuration()
1106 (<PRESORTED>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001107 {
1108 alreadySorted = true;
1109 }
1110 )?
1111 {
1112 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1113 }
1114}
1115
1116
1117String AdapterName() throws ParseException :
1118{
1119 String adapterName = null;
1120}
1121{
1122 adapterName = Identifier()
1123 {
1124 return adapterName;
1125 }
1126}
1127
1128Statement CompactStatement() throws ParseException:
1129{
1130 Pair<Identifier,Identifier> nameComponents = null;
1131 Statement stmt = null;
1132}
1133{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001134 <COMPACT> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001135 {
1136 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1137 }
1138 {
1139 return stmt;
1140 }
1141}
1142
1143Statement FeedStatement() throws ParseException:
1144{
1145 Pair<Identifier,Identifier> feedNameComponents = null;
1146 Pair<Identifier,Identifier> datasetNameComponents = null;
1147
1148 Map<String,String> configuration = null;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001149 FunctionSignature appliedFunction = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001150 Statement stmt = null;
1151 String policy = null;
1152}
1153{
1154 (
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001155 <CONNECT> <FEED> feedNameComponents = QualifiedName() <TO> <DATASET> datasetNameComponents = QualifiedName()
1156 (appliedFunction = ApplyFunction())? (policy = GetPolicy())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001157 {
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001158 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, appliedFunction, policy, getVarCounter());
Yingyi Bu391f09e2015-10-29 13:49:39 -07001159 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001160 | <DISCONNECT> <FEED> feedNameComponents = QualifiedName() <FROM> <DATASET> datasetNameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001161 {
1162 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1163 }
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001164 | <START> <FEED> feedNameComponents = QualifiedName()
1165 {
1166 stmt = new StartFeedStatement (feedNameComponents);
1167 }
1168 | <STOP> <FEED> feedNameComponents = QualifiedName()
1169 {
1170 stmt = new StopFeedStatement (feedNameComponents);
1171 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001172 )
1173 {
1174 return stmt;
1175 }
1176}
1177
1178Map<String,String> Configuration() throws ParseException :
1179{
1180 Map<String,String> configuration = new LinkedHashMap<String,String>();
1181 Pair<String, String> keyValuePair = null;
1182}
1183{
1184 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1185 {
1186 configuration.put(keyValuePair.first, keyValuePair.second);
1187 }
1188 ( <COMMA> keyValuePair = KeyValuePair()
1189 {
1190 configuration.put(keyValuePair.first, keyValuePair.second);
1191 }
1192 )* )? <RIGHTPAREN>
1193 {
1194 return configuration;
1195 }
1196}
1197
1198Pair<String, String> KeyValuePair() throws ParseException:
1199{
1200 String key;
1201 String value;
1202}
1203{
1204 <LEFTPAREN> key = StringLiteral() <EQ> value = StringLiteral() <RIGHTPAREN>
1205 {
1206 return new Pair<String, String>(key, value);
1207 }
1208}
1209
1210Map<String,String> Properties() throws ParseException:
1211{
1212 Map<String,String> properties = new HashMap<String,String>();
1213 Pair<String, String> property;
1214}
1215{
1216 ( <LEFTPAREN> property = Property()
1217 {
1218 properties.put(property.first, property.second);
1219 }
1220 ( <COMMA> property = Property()
1221 {
1222 properties.put(property.first, property.second);
1223 }
1224 )* <RIGHTPAREN> )?
1225 {
1226 return properties;
1227 }
1228}
1229
1230Pair<String, String> Property() throws ParseException:
1231{
1232 String key;
1233 String value;
1234}
1235{
1236 key = Identifier() <EQ> ( value = StringLiteral() | <INTEGER_LITERAL>
1237 {
1238 try {
1239 value = "" + Long.valueOf(token.image);
1240 } catch (NumberFormatException nfe) {
1241 throw new ParseException("inapproriate value: " + token.image);
1242 }
1243 }
1244 )
1245 {
1246 return new Pair<String, String>(key.toUpperCase(), value);
1247 }
1248}
1249
1250TypeExpression IndexedTypeExpr() throws ParseException:
1251{
1252 TypeExpression typeExpr = null;
1253}
1254{
1255 (
1256 typeExpr = TypeReference()
1257 | typeExpr = OrderedListTypeDef()
1258 | typeExpr = UnorderedListTypeDef()
1259 )
1260 {
1261 return typeExpr;
1262 }
1263}
1264
1265TypeExpression TypeExpr() throws ParseException:
1266{
1267 TypeExpression typeExpr = null;
1268}
1269{
1270 (
1271 typeExpr = RecordTypeDef()
1272 | typeExpr = TypeReference()
1273 | typeExpr = OrderedListTypeDef()
1274 | typeExpr = UnorderedListTypeDef()
1275 )
1276 {
1277 return typeExpr;
1278 }
1279}
1280
1281RecordTypeDefinition RecordTypeDef() throws ParseException:
1282{
1283 RecordTypeDefinition recType = new RecordTypeDefinition();
1284 RecordTypeDefinition.RecordKind recordKind = null;
1285}
1286{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001287 ( <CLOSED>{ recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1288 | <OPEN>{ recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001289 <LEFTBRACE>
1290 {
1291 String hint = getHint(token);
1292 if (hint != null) {
1293 String splits[] = hint.split(" +");
1294 if (splits[0].equals(GEN_FIELDS_HINT)) {
1295 if (splits.length != 5) {
1296 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1297 }
1298 if (!splits[1].equals("int")) {
1299 throw new ParseException("The only supported type for gen-fields is int.");
1300 }
1301 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1302 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1303 recType.setUndeclaredFieldsDataGen(ufdg);
1304 }
1305 }
1306
1307 }
1308 (
1309 RecordField(recType)
1310 ( <COMMA> RecordField(recType) )*
1311 )?
1312 <RIGHTBRACE>
1313 {
1314 if (recordKind == null) {
1315 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1316 }
1317 recType.setRecordKind(recordKind);
1318 return recType;
1319 }
1320}
1321
1322void RecordField(RecordTypeDefinition recType) throws ParseException:
1323{
1324 String fieldName;
1325 TypeExpression type = null;
1326 boolean nullable = false;
1327}
1328{
1329 fieldName = Identifier()
1330 {
1331 String hint = getHint(token);
1332 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1333 }
1334 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1335 {
1336 recType.addField(fieldName, type, nullable, rfdg);
1337 }
1338}
1339
1340TypeReferenceExpression TypeReference() throws ParseException:
1341{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001342 Pair<Identifier,Identifier> id = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001343}
1344{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001345 id = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001346 {
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001347 if (id.first == null && id.second.getValue().equalsIgnoreCase("int")) {
1348 id.second.setValue("int64");
Yingyi Bu391f09e2015-10-29 13:49:39 -07001349 }
1350
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001351 return new TypeReferenceExpression(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001352 }
1353}
1354
1355OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1356{
1357 TypeExpression type = null;
1358}
1359{
1360 <LEFTBRACKET>
1361 ( type = TypeExpr() )
1362 <RIGHTBRACKET>
1363 {
1364 return new OrderedListTypeDefinition(type);
1365 }
1366}
1367
1368
1369UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1370{
1371 TypeExpression type = null;
1372}
1373{
1374 <LEFTDBLBRACE>
1375 ( type = TypeExpr() )
1376 <RIGHTDBLBRACE>
1377 {
1378 return new UnorderedListTypeDefinition(type);
1379 }
1380}
1381
1382FunctionName FunctionName() throws ParseException:
1383{
1384 String first = null;
1385 String second = null;
1386 String third = null;
1387 boolean secondAfterDot = false;
1388}
1389{
Michael Blowd6cf6412016-06-30 02:44:35 -04001390 first = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001391 {
1392 FunctionName result = new FunctionName();
1393 result.hint = getHint(token);
Michael Blowd6cf6412016-06-30 02:44:35 -04001394 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001395 ( <DOT> second = Identifier()
1396 {
1397 secondAfterDot = true;
1398 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001399 ( <SYMBOLHASH> third = Identifier())? | <SYMBOLHASH> second = Identifier() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001400 {
1401 if (second == null) {
1402 result.dataverse = defaultDataverse;
1403 result.library = null;
1404 result.function = first;
1405 } else if (third == null) {
1406 if (secondAfterDot) {
1407 result.dataverse = first;
1408 result.library = null;
1409 result.function = second;
1410 } else {
1411 result.dataverse = defaultDataverse;
1412 result.library = first;
1413 result.function = second;
1414 }
1415 } else {
1416 result.dataverse = first;
1417 result.library = second;
1418 result.function = third;
1419 }
1420
1421 if (result.function.equalsIgnoreCase("int")) {
1422 result.function = "int64";
1423 }
1424 return result;
1425 }
1426}
1427
1428
1429Pair<Identifier,Identifier> TypeName() throws ParseException:
1430{
1431 Pair<Identifier,Identifier> name = null;
1432}
1433{
1434 name = QualifiedName()
1435 {
1436 if (name.first == null) {
1437 name.first = new Identifier(defaultDataverse);
1438 }
1439 return name;
1440 }
1441}
1442
1443String Identifier() throws ParseException:
1444{
1445 String lit = null;
1446}
1447{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001448 ((<IDENTIFIER>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001449 {
1450 return token.image;
1451 }
1452 | lit = StringLiteral()
1453 {
1454 return lit;
1455 }
1456 )
1457}
1458
Yingyi Buc9bfe252016-03-01 00:02:40 -08001459Pair<Integer, Pair<List<String>, TypeExpression>> OpenField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001460{
1461 TypeExpression fieldType = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001462 Pair<Integer, List<String>> fieldList = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001463}
1464{
1465 fieldList = NestedField()
Mike Careyaf8e19d2016-08-25 17:55:17 -07001466 ( <COLON> fieldType = IndexedTypeExpr() <QUES>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001467 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001468 return new Pair<Integer, Pair<List<String>, TypeExpression>>
1469 (fieldList.first, new Pair<List<String>, TypeExpression>(fieldList.second, fieldType));
Yingyi Bu391f09e2015-10-29 13:49:39 -07001470 }
1471}
1472
Yingyi Buc9bfe252016-03-01 00:02:40 -08001473Pair<Integer, List<String>> NestedField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001474{
1475 List<String> exprList = new ArrayList<String>();
1476 String lit = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001477 int source = 0;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001478}
1479{
1480 lit = Identifier()
1481 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001482 boolean meetParens = false;
1483 }
1484 (
Yingyi Buc9bfe252016-03-01 00:02:40 -08001485 LOOKAHEAD(1)
Yingyi Bub9169b62016-02-26 21:21:49 -08001486 <LEFTPAREN><RIGHTPAREN>
1487 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001488 if(!lit.equals("meta")){
Yingyi Bub9169b62016-02-26 21:21:49 -08001489 throw new ParseException("The string before () has to be \"meta\".");
1490 }
1491 meetParens = true;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001492 source = 1;
Yingyi Bub9169b62016-02-26 21:21:49 -08001493 }
1494 )?
1495 {
1496 if(!meetParens){
1497 exprList.add(lit);
1498 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001499 }
1500 (<DOT>
1501 lit = Identifier()
1502 {
1503 exprList.add(lit);
1504 }
1505 )*
1506 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001507 return new Pair<Integer, List<String>>(source, exprList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001508 }
1509}
1510
1511
1512
1513String StringLiteral() throws ParseException:
1514{
1515}
1516{
1517 <STRING_LITERAL>
1518 {
1519 return removeQuotesAndEscapes(token.image);
1520 }
1521}
1522
1523Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1524{
1525 String first = null;
1526 String second = null;
1527}
1528{
1529 first = Identifier() (<DOT> second = Identifier())?
1530 {
1531 Identifier id1 = null;
1532 Identifier id2 = null;
1533 if (second == null) {
1534 id2 = new Identifier(first);
1535 } else
1536 {
1537 id1 = new Identifier(first);
1538 id2 = new Identifier(second);
1539 }
1540 return new Pair<Identifier,Identifier>(id1, id2);
1541 }
1542}
1543
1544Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1545{
1546 String first = null;
1547 String second = null;
1548 String third = null;
1549}
1550{
1551 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1552 {
1553 Identifier id1 = null;
1554 Identifier id2 = null;
1555 Identifier id3 = null;
1556 if (third == null) {
1557 id2 = new Identifier(first);
1558 id3 = new Identifier(second);
1559 } else {
1560 id1 = new Identifier(first);
1561 id2 = new Identifier(second);
1562 id3 = new Identifier(third);
1563 }
1564 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1565 }
1566}
1567
1568FunctionDecl FunctionDeclaration() throws ParseException:
1569{
1570 FunctionDecl funcDecl;
1571 FunctionSignature signature;
1572 String functionName;
1573 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1574 Expression funcBody;
1575 createNewScope();
1576}
1577{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001578 <DECLARE> <FUNCTION> functionName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001579 paramList = ParameterList()
1580 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1581 {
1582 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1583 getCurrentScope().addFunctionDescriptor(signature, false);
1584 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1585 removeCurrentScope();
1586 return funcDecl;
1587 }
1588}
1589
1590
1591Query Query() throws ParseException:
1592{
Till Westmannef3f0272016-07-27 18:34:01 -07001593 Query query = new Query(false);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001594 // we set the pointers to the dataverses and datasets lists to fill them with entities to be locked
1595 setDataverses(query.getDataverses());
1596 setDatasets(query.getDatasets());
1597 Expression expr;
1598}
1599{
1600 expr = Expression()
1601 {
1602 query.setBody(expr);
1603 query.setVarCounter(getVarCounter());
1604 // we remove the pointers to the locked entities before we return the query object
1605 setDataverses(null);
1606 setDatasets(null);
1607 return query;
1608 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001609}
1610
1611
1612
1613Expression Expression():
1614{
1615 Expression expr = null;
1616 Expression exprP = null;
1617}
1618{
1619(
1620
1621//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1622 expr = OperatorExpr()
1623 | expr = IfThenElse()
1624 | expr = FLWOGR()
1625 | expr = QuantifiedExpression()
1626
1627
1628)
1629 {
1630 return (exprP==null) ? expr : exprP;
1631 }
1632}
1633
1634
1635
1636Expression OperatorExpr()throws ParseException:
1637{
1638 OperatorExpr op = null;
1639 Expression operand = null;
1640}
1641{
1642 operand = AndExpr()
1643 (
1644
1645 <OR>
1646 {
1647 if (op == null) {
1648 op = new OperatorExpr();
1649 op.addOperand(operand);
1650 op.setCurrentop(true);
1651 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001652 try{
1653 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001654 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001655 throw new ParseException(e.getMessage());
1656 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001657 }
1658
1659 operand = AndExpr()
1660 {
1661 op.addOperand(operand);
1662 }
1663
1664 )*
1665
1666 {
1667 return op==null? operand: op;
1668 }
1669}
1670
1671Expression AndExpr()throws ParseException:
1672{
1673 OperatorExpr op = null;
1674 Expression operand = null;
1675}
1676{
1677 operand = RelExpr()
1678 (
1679
1680 <AND>
1681 {
1682 if (op == null) {
1683 op = new OperatorExpr();
1684 op.addOperand(operand);
1685 op.setCurrentop(true);
1686 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001687 try{
1688 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001689 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001690 throw new ParseException(e.getMessage());
1691 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001692 }
1693
1694 operand = RelExpr()
1695 {
1696 op.addOperand(operand);
1697 }
1698
1699 )*
1700
1701 {
1702 return op==null? operand: op;
1703 }
1704}
1705
1706
1707
1708Expression RelExpr()throws ParseException:
1709{
1710 OperatorExpr op = null;
1711 Expression operand = null;
1712 boolean broadcast = false;
1713 IExpressionAnnotation annotation = null;
1714}
1715{
1716 operand = AddExpr()
1717 {
1718 if (operand instanceof VariableExpr) {
1719 String hint = getHint(token);
1720 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1721 broadcast = true;
1722 }
1723 }
1724 }
1725
1726 (
1727 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> |<SIMILAR>)
1728 {
1729 String mhint = getHint(token);
1730 if (mhint != null) {
1731 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1732 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1733 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1734 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1735 }
1736 }
1737 if (op == null) {
1738 op = new OperatorExpr();
1739 op.addOperand(operand, broadcast);
1740 op.setCurrentop(true);
1741 broadcast = false;
1742 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001743 try{
1744 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001745 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001746 throw new ParseException(e.getMessage());
1747 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001748 }
1749
1750 operand = AddExpr()
1751 {
1752 broadcast = false;
1753 if (operand instanceof VariableExpr) {
1754 String hint = getHint(token);
1755 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1756 broadcast = true;
1757 }
1758 }
1759 op.addOperand(operand, broadcast);
1760 }
1761 )?
1762
1763 {
1764 if (annotation != null) {
1765 op.addHint(annotation);
1766 }
1767 return op==null? operand: op;
1768 }
1769}
1770
1771Expression AddExpr()throws ParseException:
1772{
1773 OperatorExpr op = null;
1774 Expression operand = null;
1775}
1776{
1777 operand = MultExpr()
1778
1779 ( (<PLUS> | <MINUS>)
1780 {
1781 if (op == null) {
1782 op = new OperatorExpr();
1783 op.addOperand(operand);
1784 op.setCurrentop(true);
1785 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001786 try{
1787 ((OperatorExpr)op).addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001788 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001789 throw new ParseException(e.getMessage());
1790 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001791 }
1792
1793 operand = MultExpr()
1794 {
1795 op.addOperand(operand);
1796 }
1797 )*
1798
1799 {
1800 return op==null? operand: op;
1801 }
1802}
1803
1804Expression MultExpr()throws ParseException:
1805{
1806 OperatorExpr op = null;
1807 Expression operand = null;
1808}
1809{
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001810 operand = ExponentExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001811
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001812 (( <MUL> | <DIV> | <MOD> | <IDIV>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001813 {
1814 if (op == null) {
1815 op = new OperatorExpr();
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001816 op.addOperand(operand);
1817 op.setCurrentop(true);
1818 }
1819 try{
1820 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001821 } catch (CompilationException e){
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001822 throw new ParseException(e.getMessage());
1823 }
1824 }
1825 operand = ExponentExpr()
1826 {
1827 op.addOperand(operand);
1828 }
1829 )*
1830
1831 {
1832 return op==null?operand:op;
1833 }
1834}
1835
1836Expression ExponentExpr()throws ParseException:
1837{
1838 OperatorExpr op = null;
1839 Expression operand = null;
1840}
1841{
1842 operand = UnionExpr()
1843
1844 ( <CARET>
1845 {
1846 if (op == null) {
1847 op = new OperatorExpr();
1848 op.addOperand(operand);
1849 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001850 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001851 try{
1852 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001853 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001854 throw new ParseException(e.getMessage());
1855 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001856 }
1857 operand = UnionExpr()
1858 {
1859 op.addOperand(operand);
1860 }
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001861 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001862
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001863 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07001864 return op==null?operand:op;
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001865 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001866}
1867
1868Expression UnionExpr() throws ParseException:
1869{
1870 UnionExpr union = null;
1871 Expression operand1 = null;
1872 Expression operand2 = null;
1873}
1874{
1875 operand1 = UnaryExpr()
1876 (<UNION>
1877 (operand2 = UnaryExpr()) {
1878 if (union == null) {
1879 union = new UnionExpr();
1880 union.addExpr(operand1);
1881 }
1882 union.addExpr(operand2);
1883 } )*
1884 {
1885 return (union == null)? operand1: union;
1886 }
1887}
1888
1889Expression UnaryExpr() throws ParseException:
1890{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001891 UnaryExpr uexpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001892 Expression expr = null;
1893}
1894{
1895 ( (<PLUS> | <MINUS>)
1896 {
1897 uexpr = new UnaryExpr();
Yingyi Bu196db5d2016-07-15 19:07:20 -07001898 try{
1899 uexpr.setExprType(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001900 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001901 throw new ParseException(e.getMessage());
1902 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001903 }
1904 )?
1905
1906 expr = ValueExpr()
1907 {
1908 if(uexpr!=null){
1909 ((UnaryExpr)uexpr).setExpr(expr);
1910 return uexpr;
1911 }
1912 else{
1913 return expr;
1914 }
1915 }
1916}
1917
1918Expression ValueExpr()throws ParseException:
1919{
1920 Expression expr = null;
1921 Identifier ident = null;
1922 AbstractAccessor fa = null;
1923 Expression indexExpr = null;
1924}
1925{
1926 expr = PrimaryExpr() ( ident = Field()
1927 {
1928 fa = (fa == null ? new FieldAccessor(expr, ident)
1929 : new FieldAccessor(fa, ident));
1930 }
1931 | indexExpr = Index()
1932 {
1933 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
1934 : new IndexAccessor(fa, indexExpr));
1935 }
1936 )*
1937 {
1938 return fa == null ? expr : fa;
1939 }
1940}
1941
1942Identifier Field() throws ParseException:
1943{
1944 String ident = null;
1945}
1946{
1947 <DOT> ident = Identifier()
1948 {
1949 return new Identifier(ident);
1950 }
1951}
1952
1953Expression Index() throws ParseException:
1954{
1955 Expression expr = null;
1956}
1957{
1958 <LEFTBRACKET> ( expr = Expression()
1959 {
1960 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1961 {
1962 Literal lit = ((LiteralExpr)expr).getValue();
1963 if(lit.getLiteralType() != Literal.Type.INTEGER &&
1964 lit.getLiteralType() != Literal.Type.LONG) {
1965 throw new ParseException("Index should be an INTEGER");
1966 }
1967 }
1968 }
1969
1970 | <QUES> // ANY
1971
1972 )
1973
1974 <RIGHTBRACKET>
1975 {
1976 return expr;
1977 }
1978}
1979
1980
1981Expression PrimaryExpr()throws ParseException:
1982{
1983 Expression expr = null;
1984}
1985{
1986 ( LOOKAHEAD(2)
1987 expr = FunctionCallExpr()
1988 | expr = Literal()
1989 | expr = DatasetAccessExpression()
1990 | expr = VariableRef()
1991 {
1992 if(((VariableExpr)expr).getIsNewVar() == true)
1993 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
1994 }
1995 | expr = ListConstructor()
1996 | expr = RecordConstructor()
1997 | expr = ParenthesizedExpression()
1998 )
1999 {
2000 return expr;
2001 }
2002}
2003
2004Expression Literal() throws ParseException:
2005{
2006 LiteralExpr lit = new LiteralExpr();
2007 String str = null;
2008}
2009{
2010 ( str = StringLiteral()
2011 {
2012 lit.setValue(new StringLiteral(str));
2013 }
2014 | <INTEGER_LITERAL>
2015 {
2016 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
2017 }
2018 | <FLOAT_LITERAL>
2019 {
2020 lit.setValue(new FloatLiteral(new Float(token.image)));
2021 }
2022 | <DOUBLE_LITERAL>
2023 {
2024 lit.setValue(new DoubleLiteral(new Double(token.image)));
2025 }
Yingyi Bu535d86b2016-05-23 16:44:25 -07002026 | <MISSING>
2027 {
2028 lit.setValue(MissingLiteral.INSTANCE);
2029 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002030 | <NULL>
2031 {
2032 lit.setValue(NullLiteral.INSTANCE);
2033 }
2034 | <TRUE>
2035 {
2036 lit.setValue(TrueLiteral.INSTANCE);
2037 }
2038 | <FALSE>
2039 {
2040 lit.setValue(FalseLiteral.INSTANCE);
2041 }
2042 )
2043 {
2044 return lit;
2045 }
2046}
2047
2048
2049VariableExpr VariableRef() throws ParseException:
2050{
2051 VariableExpr varExp = new VariableExpr();
2052 VarIdentifier var = new VarIdentifier();
2053}
2054{
2055 <VARIABLE>
2056 {
2057 String varName = token.image;
2058 Identifier ident = lookupSymbol(varName);
2059 if (isInForbiddenScopes(varName)) {
2060 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.");
2061 }
2062 if(ident != null) { // exist such ident
2063 varExp.setIsNewVar(false);
2064 varExp.setVar((VarIdentifier)ident);
2065 } else {
2066 varExp.setVar(var);
2067 }
2068 var.setValue(varName);
2069 return varExp;
2070 }
2071}
2072
2073
2074VariableExpr Variable() throws ParseException:
2075{
2076 VariableExpr varExp = new VariableExpr();
2077 VarIdentifier var = new VarIdentifier();
2078}
2079{
2080 <VARIABLE>
2081 {
2082 Identifier ident = lookupSymbol(token.image);
2083 if(ident != null) { // exist such ident
2084 varExp.setIsNewVar(false);
2085 }
2086 varExp.setVar(var);
2087 var.setValue(token.image);
2088 return varExp;
2089 }
2090}
2091
2092Expression ListConstructor() throws ParseException:
2093{
2094 Expression expr = null;
2095}
2096{
2097 (
2098 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
2099 )
2100
2101 {
2102 return expr;
2103 }
2104}
2105
2106
2107ListConstructor OrderedListConstructor() throws ParseException:
2108{
2109 ListConstructor expr = new ListConstructor();
2110 List<Expression> exprList = null;
2111 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
2112}
2113{
2114 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
2115 {
2116 expr.setExprList(exprList);
2117 return expr;
2118 }
2119}
2120
2121ListConstructor UnorderedListConstructor() throws ParseException:
2122{
2123 ListConstructor expr = new ListConstructor();
2124 List<Expression> exprList = null;
2125 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
2126}
2127{
2128 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
2129 {
2130 expr.setExprList(exprList);
2131 return expr;
2132 }
2133}
2134
2135List<Expression> ExpressionList() throws ParseException:
2136{
2137 Expression expr = null;
2138 List<Expression> list = null;
2139 List<Expression> exprList = new ArrayList<Expression>();
2140}
2141{
2142 (
2143 expr = Expression() { exprList.add(expr); }
2144 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
2145 )?
2146 (LOOKAHEAD(1) Comma())?
2147 {
2148 return exprList;
2149 }
2150}
2151
2152void Comma():
2153{}
2154{
2155 <COMMA>
2156}
2157
2158RecordConstructor RecordConstructor() throws ParseException:
2159{
2160 RecordConstructor expr = new RecordConstructor();
2161 FieldBinding tmp = null;
2162 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2163}
2164{
2165 <LEFTBRACE> (tmp = FieldBinding()
2166 {
2167 fbList.add(tmp);
2168 }
2169 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2170 {
2171 expr.setFbList(fbList);
2172 return expr;
2173 }
2174}
2175
2176FieldBinding FieldBinding() throws ParseException:
2177{
2178 FieldBinding fb = new FieldBinding();
2179 Expression left, right;
2180}
2181{
2182 left = Expression() <COLON> right = Expression()
2183 {
2184 fb.setLeftExpr(left);
2185 fb.setRightExpr(right);
2186 return fb;
2187 }
2188}
2189
2190
2191Expression FunctionCallExpr() throws ParseException:
2192{
2193 CallExpr callExpr;
2194 List<Expression> argList = new ArrayList<Expression>();
2195 Expression tmp;
2196 int arity = 0;
2197 FunctionName funcName = null;
2198 String hint = null;
2199}
2200{
2201 funcName = FunctionName()
2202 {
2203 hint = funcName.hint;
2204 }
2205 <LEFTPAREN> (tmp = Expression()
2206 {
2207 argList.add(tmp);
2208 arity ++;
2209 }
2210 (<COMMA> tmp = Expression()
2211 {
2212 argList.add(tmp);
2213 arity++;
2214 }
2215 )*)? <RIGHTPAREN>
2216 {
2217 // TODO use funcName.library
2218 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2219 FunctionSignature signature
2220 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2221 if (signature == null) {
2222 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2223 }
2224 callExpr = new CallExpr(signature,argList);
2225 if (hint != null) {
2226 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2227 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2228 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2229 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2230 }
2231 }
2232 return callExpr;
2233 }
2234}
2235
2236
2237Expression DatasetAccessExpression() throws ParseException:
2238{
2239 String funcName;
2240 String arg1 = null;
2241 String arg2 = null;
2242 Expression nameArg;
2243}
2244{
2245 <DATASET>
2246 {
2247 funcName = token.image;
2248 }
2249 ( ( arg1 = Identifier() ( <DOT> arg2 = Identifier() )? )
2250 {
2251 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
2252 LiteralExpr ds = new LiteralExpr();
2253 ds.setValue( new StringLiteral(name) );
2254 nameArg = ds;
2255 if(arg2 != null){
2256 addDataverse(arg1.toString());
2257 addDataset(name);
2258 } else {
2259 addDataset(defaultDataverse + "." + name);
2260 }
2261 }
2262 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
2263 {
2264 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
2265 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
2266 if (signature == null) {
2267 signature = new FunctionSignature(dataverse, funcName, 1);
2268 }
2269 List<Expression> argList = new ArrayList<Expression>();
2270 argList.add(nameArg);
2271 return new CallExpr(signature, argList);
2272 }
2273}
2274
2275Expression ParenthesizedExpression() throws ParseException:
2276{
2277 Expression expr;
2278}
2279{
2280 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2281 {
2282 return expr;
2283 }
2284}
2285
2286Expression IfThenElse() throws ParseException:
2287{
2288 Expression condExpr;
2289 Expression thenExpr;
2290 Expression elseExpr;
2291 IfExpr ifExpr = new IfExpr();
2292}
2293{
2294 <IF> <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> <THEN> thenExpr = Expression() <ELSE> elseExpr = Expression()
2295
2296 {
2297 ifExpr.setCondExpr(condExpr);
2298 ifExpr.setThenExpr(thenExpr);
2299 ifExpr.setElseExpr(elseExpr);
2300 return ifExpr;
2301 }
2302}
2303
Taewoo Kim92c0bac2017-02-11 16:38:44 -08002304// Note: if you modify this part: "tmp = LetClause() {clauseList.add(tmp);}", please also apply the necessary
2305// change to the AQLPlusExtension.jj file since it refers to this string part and may behave incorrectly if this
2306// part is modified. For more details, please refer to AQLPlusExtension.jj file.
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +03002307Expression FLWOGR() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002308{
2309 FLWOGRExpression flworg = new FLWOGRExpression();
2310 List<Clause> clauseList = new ArrayList<Clause>();
2311 Expression returnExpr;
2312 Clause tmp;
2313 createNewScope();
2314}
2315{
2316 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2317 (tmp = Clause() {clauseList.add(tmp);})* (<RETURN>|<SELECT>) returnExpr = Expression()
2318
2319 {
2320 flworg.setClauseList(clauseList);
2321 flworg.setReturnExpr(returnExpr);
2322 removeCurrentScope();
2323 return flworg;
2324 }
2325}
2326
2327Clause Clause()throws ParseException :
2328{
2329 Clause clause;
2330}
2331{
2332 (
2333 clause = ForClause()
2334 | clause = LetClause()
2335 | clause = WhereClause()
2336 | clause = OrderbyClause()
2337 | clause = GroupClause()
2338 | clause = LimitClause()
2339 | clause = DistinctClause()
2340 )
2341 {
2342 return clause;
2343 }
2344}
2345
2346Clause ForClause()throws ParseException :
2347{
2348 ForClause fc = new ForClause();
2349 VariableExpr varExp;
2350 VariableExpr varPos = null;
2351 Expression inExp;
2352 extendCurrentScope();
2353}
2354{
2355 (<FOR>|<FROM>) varExp = Variable() (<AT> varPos = Variable())? <IN> ( inExp = Expression() )
2356 {
2357 fc.setVarExpr(varExp);
2358 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2359 fc.setInExpr(inExp);
2360 if (varPos != null) {
2361 fc.setPosExpr(varPos);
2362 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2363 }
2364 return fc;
2365 }
2366}
2367
2368Clause LetClause() throws ParseException:
2369{
2370 LetClause lc = new LetClause();
2371 VariableExpr varExp;
2372 Expression beExp;
2373 extendCurrentScope();
2374}
2375{
2376 (<LET>|<WITH>) varExp = Variable() <ASSIGN> beExp = Expression()
2377 {
2378 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2379 lc.setVarExpr(varExp);
2380 lc.setBindingExpr(beExp);
2381 return lc;
2382 }
2383}
2384
2385Clause WhereClause()throws ParseException :
2386{
2387 WhereClause wc = new WhereClause();
2388 Expression whereExpr;
2389}
2390{
2391 <WHERE> whereExpr = Expression()
2392 {
2393 wc.setWhereExpr(whereExpr);
2394 return wc;
2395 }
2396}
2397
2398Clause OrderbyClause()throws ParseException :
2399{
2400 OrderbyClause oc = new OrderbyClause();
2401 Expression orderbyExpr;
2402 List<Expression> orderbyList = new ArrayList<Expression>();
2403 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2404 int numOfOrderby = 0;
2405}
2406{
2407 (
2408 <ORDER>
2409 {
2410 String hint = getHint(token);
2411 if (hint != null) {
2412 if (hint.startsWith(INMEMORY_HINT)) {
2413 String splits[] = hint.split(" +");
2414 int numFrames = Integer.parseInt(splits[1]);
2415 int numTuples = Integer.parseInt(splits[2]);
2416 oc.setNumFrames(numFrames);
2417 oc.setNumTuples(numTuples);
2418 }
2419 if (hint.startsWith(RANGE_HINT)) {
2420 try{
2421 oc.setRangeMap(RangeMapBuilder.parseHint(hint.substring(RANGE_HINT.length())));
Taewoo Kime65e6ca2017-01-14 17:53:28 -08002422 } catch (CompilationException e) {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002423 throw new ParseException(e.getMessage());
2424 }
2425 }
2426 }
2427 }
2428 <BY> orderbyExpr = Expression()
2429 {
2430 orderbyList.add(orderbyExpr);
2431 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2432 }
2433 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2434 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2435 {
2436 modifierList.add(modif);
2437 }
2438
2439 (<COMMA> orderbyExpr = Expression()
2440 {
2441 orderbyList.add(orderbyExpr);
2442 modif = OrderbyClause.OrderModifier.ASC;
2443 }
2444 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2445 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2446 {
2447 modifierList.add(modif);
2448 }
2449 )*
2450)
2451 {
2452 oc.setModifierList(modifierList);
2453 oc.setOrderbyList(orderbyList);
2454 return oc;
2455 }
2456}
2457Clause GroupClause()throws ParseException :
2458{
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +03002459 GroupbyClause gbc = new GroupbyClause();
2460 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2461 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002462 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002463 Map<Expression, VariableExpr> withVarMap = new HashMap<Expression, VariableExpr>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002464 VariableExpr var = null;
2465 VariableExpr withVar = null;
2466 Expression expr = null;
2467 VariableExpr decorVar = null;
2468 Expression decorExpr = null;
2469}
2470{
2471 {
2472 Scope newScope = extendCurrentScopeNoPush(true);
2473 // extendCurrentScope(true);
2474 }
2475 <GROUP>
2476 {
2477 String hint = getHint(token);
2478 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2479 gbc.setHashGroupByHint(true);
2480 }
2481 }
2482 <BY> (LOOKAHEAD(2) var = Variable()
2483 {
2484 newScope.addNewVarSymbolToScope(var.getVar());
2485 } <ASSIGN>)?
2486 expr = Expression()
2487 {
2488 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2489 vePairList.add(pair1);
2490 }
2491 (<COMMA> ( LOOKAHEAD(2) var = Variable()
2492 {
2493 newScope.addNewVarSymbolToScope(var.getVar());
2494 } <ASSIGN>)?
2495 expr = Expression()
2496 {
2497 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2498 vePairList.add(pair2);
2499 }
2500 )*
2501 (<DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2502 {
2503 newScope.addNewVarSymbolToScope(decorVar.getVar());
2504 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2505 decorPairList.add(pair3);
2506 }
2507 (<COMMA> <DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2508 {
2509 newScope.addNewVarSymbolToScope(decorVar.getVar());
2510 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2511 decorPairList.add(pair4);
2512 }
2513 )*
2514 )?
2515 (<WITH>|<KEEPING>) withVar = VariableRef()
2516 {
2517 if(withVar.getIsNewVar()==true)
2518 throw new ParseException("can't find variable " + withVar.getVar());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002519 withVarMap.put(withVar, withVar);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002520 newScope.addNewVarSymbolToScope(withVar.getVar());
2521 }
2522 (<COMMA> withVar = VariableRef()
2523 {
2524 if(withVar.getIsNewVar()==true)
2525 throw new ParseException("can't find variable " + withVar.getVar());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002526 withVarMap.put(withVar, withVar);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002527 newScope.addNewVarSymbolToScope(withVar.getVar());
2528 })*
2529 {
2530 gbc.setGbyPairList(vePairList);
2531 gbc.setDecorPairList(decorPairList);
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002532 gbc.setWithVarMap(withVarMap);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002533 replaceCurrentScope(newScope);
2534 return gbc;
2535 }
2536}
2537
2538
2539LimitClause LimitClause() throws ParseException:
2540{
2541 LimitClause lc = new LimitClause();
2542 Expression expr;
2543 pushForbiddenScope(getCurrentScope());
2544}
2545{
2546 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
2547 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
2548
2549 {
2550 popForbiddenScope();
2551 return lc;
2552 }
2553}
2554
2555DistinctClause DistinctClause() throws ParseException:
2556{
2557 List<Expression> exprs = new ArrayList<Expression>();
2558 Expression expr;
2559}
2560{
2561 <DISTINCT> <BY> expr = Expression()
2562 {
2563 exprs.add(expr);
2564 }
2565 (<COMMA> expr = Expression()
2566 {
2567 exprs.add(expr);
2568 }
2569 )*
2570 {
2571 return new DistinctClause(exprs);
2572 }
2573}
2574
2575QuantifiedExpression QuantifiedExpression()throws ParseException:
2576{
2577 QuantifiedExpression qc = new QuantifiedExpression();
2578 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2579 Expression satisfiesExpr;
2580 VariableExpr var;
2581 Expression inExpr;
2582 QuantifiedPair pair;
2583}
2584{
2585 {
2586 createNewScope();
2587 }
2588
Michael Blowd6cf6412016-06-30 02:44:35 -04002589 ( (<SOME> { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2590 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
Yingyi Bu391f09e2015-10-29 13:49:39 -07002591 var = Variable() <IN> inExpr = Expression()
2592 {
2593 pair = new QuantifiedPair(var, inExpr);
2594 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2595 quantifiedList.add(pair);
2596 }
2597 (
2598 <COMMA> var = Variable() <IN> inExpr = Expression()
2599 {
2600 pair = new QuantifiedPair(var, inExpr);
2601 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2602 quantifiedList.add(pair);
2603 }
2604 )*
2605 <SATISFIES> satisfiesExpr = Expression()
2606 {
2607 qc.setSatisfiesExpr(satisfiesExpr);
2608 qc.setQuantifiedList(quantifiedList);
2609 removeCurrentScope();
2610 return qc;
2611 }
2612}
2613
2614TOKEN_MGR_DECLS:
2615{
2616 public int commentDepth = 0;
Till Westmanne9b2adf2016-10-15 12:39:01 -07002617 public ArrayDeque<Integer> lexerStateStack = new ArrayDeque<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002618
2619 public void pushState() {
2620 lexerStateStack.push( curLexState );
2621 }
2622
2623 public void popState(String token) {
2624 if (lexerStateStack.size() > 0) {
2625 SwitchTo( lexerStateStack.pop() );
2626 } else {
2627 int errorLine = input_stream.getEndLine();
2628 int errorColumn = input_stream.getEndColumn();
2629 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
2630 + "\" but state stack is empty.";
2631 throw new TokenMgrError(msg, -1);
2632 }
2633 }
2634}
2635
2636<DEFAULT,IN_DBL_BRACE>
2637TOKEN :
2638{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002639 <APPLY : "apply">
2640 | <AS : "as">
2641 | <ASC : "asc">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002642 | <AT : "at">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002643 | <AUTOGENERATED : "autogenerated">
2644 | <BTREE : "btree">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002645 | <BY : "by">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002646 | <CLOSED : "closed">
2647 | <COMPACT : "compact">
2648 | <COMPACTION : "compaction">
2649 | <CONNECT : "connect">
2650 | <CREATE : "create">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002651 | <DATASET : "dataset">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002652 | <DATAVERSE : "dataverse">
2653 | <DECLARE : "declare">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002654 | <DECOR : "decor">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002655 | <DEFINITION : "definition">
2656 | <DELETE : "delete">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002657 | <DESC : "desc">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002658 | <DISCONNECT : "disconnect">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002659 | <DISTINCT : "distinct">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002660 | <DROP : "drop">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002661 | <ELSE : "else">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002662 | <ENFORCED : "enforced">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002663 | <EVERY : "every">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002664 | <EXISTS : "exists">
2665 | <EXTERNAL : "external">
2666 | <FEED : "feed">
2667 | <FILTER : "filter">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002668 | <FOR : "for">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002669 | <FORMAT : "format">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002670 | <FROM : "from">
Taewoo Kimc49405a2017-01-04 00:30:43 -08002671 | <FULLTEXT : "fulltext">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002672 | <FUNCTION : "function">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002673 | <GROUP : "group">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002674 | <HINTS : "hints">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002675 | <IF : "if">
2676 | <IN : "in">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002677 | <INDEX : "index">
2678 | <INGESTION : "ingestion">
2679 | <INSERT : "insert">
2680 | <INTERNAL : "internal">
2681 | <INTO : "into">
2682 | <KEY : "key">
2683 | <KEYWORD : "keyword">
2684 | <KEEPING : "keeping">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002685 | <LET : "let">
2686 | <LIMIT : "limit">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002687 | <LOAD : "load">
2688 | <NGRAM : "ngram">
2689 | <NODEGROUP : "nodegroup">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002690 | <OFFSET : "offset">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002691 | <ON : "on">
2692 | <OPEN : "open">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002693 | <ORDER : "order">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002694 | <OUTPUT : "output">
2695 | <PATH : "path">
2696 | <POLICY : "policy">
2697 | <PRESORTED : "pre-sorted">
2698 | <PRIMARY : "primary">
2699 | <REFRESH : "refresh">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002700 | <RETURN : "return">
Steven Glenn Jacobsafa909a2016-10-16 10:35:30 -07002701 | <RETURNING : "returning">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002702 | <RTREE : "rtree">
2703 | <RUN : "run">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002704 | <SATISFIES : "satisfies">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002705 | <SECONDARY : "secondary">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002706 | <SELECT : "select">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002707 | <SET : "set">
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08002708 | <START: "start">
2709 | <STOP: "stop">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002710 | <SOME : "some">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002711 | <TEMPORARY : "temporary">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002712 | <THEN : "then">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002713 | <TO : "to">
2714 | <TYPE : "type">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002715 | <UNION : "union">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002716 | <UPDATE : "update">
2717 | <UPSERT : "upsert">
2718 | <USE : "use">
2719 | <USING : "using">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002720 | <WHERE : "where">
2721 | <WITH : "with">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002722 | <WRITE : "write">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002723}
2724
2725<DEFAULT,IN_DBL_BRACE>
2726TOKEN :
2727{
2728 <CARET : "^">
2729 | <DIV : "/">
2730 | <IDIV : "idiv">
2731 | <MINUS : "-">
2732 | <MOD : "%">
2733 | <MUL : "*">
2734 | <PLUS : "+">
2735
2736 | <LEFTPAREN : "(">
2737 | <RIGHTPAREN : ")">
2738 | <LEFTBRACKET : "[">
2739 | <RIGHTBRACKET : "]">
2740
2741 | <COLON : ":">
2742 | <COMMA : ",">
2743 | <DOT : ".">
2744 | <QUES : "?">
2745
2746 | <LT : "<">
2747 | <GT : ">">
2748 | <LE : "<=">
2749 | <GE : ">=">
2750 | <EQ : "=">
2751 | <NE : "!=">
2752 | <SIMILAR : "~=">
2753 | <ASSIGN : ":=">
2754
2755 | <AND : "and">
2756 | <OR : "or">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002757
2758 | <SYMBOLAT : "@">
2759 | <SYMBOLHASH : "#">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002760}
2761
2762<DEFAULT,IN_DBL_BRACE>
2763TOKEN :
2764{
2765 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
2766}
2767
2768<DEFAULT>
2769TOKEN :
2770{
2771 <RIGHTBRACE : "}"> { popState("}"); }
2772}
2773
2774<DEFAULT,IN_DBL_BRACE>
2775TOKEN :
2776{
2777 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
2778}
2779
2780<IN_DBL_BRACE>
2781TOKEN :
2782{
2783 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
2784}
2785
2786<DEFAULT,IN_DBL_BRACE>
2787TOKEN :
2788{
2789 <INTEGER_LITERAL : (<DIGIT>)+ >
2790}
2791
2792<DEFAULT,IN_DBL_BRACE>
2793TOKEN :
2794{
Yingyi Bu535d86b2016-05-23 16:44:25 -07002795 < MISSING : "missing">
2796 | <NULL : "null">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002797 | <TRUE : "true">
2798 | <FALSE : "false">
2799}
2800
2801<DEFAULT,IN_DBL_BRACE>
2802TOKEN :
2803{
2804 <#DIGIT : ["0" - "9"]>
2805}
2806
2807<DEFAULT,IN_DBL_BRACE>
2808TOKEN:
2809{
2810 < DOUBLE_LITERAL: <DIGITS>
2811 | <DIGITS> ( "." <DIGITS> )?
2812 | "." <DIGITS>
2813 >
2814 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
2815 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
2816 | "." <DIGITS> ( "f" | "F" )
2817 >
2818 | <DIGITS : (<DIGIT>)+ >
2819}
2820
2821<DEFAULT,IN_DBL_BRACE>
2822TOKEN :
2823{
2824 <#LETTER : ["A" - "Z", "a" - "z"]>
2825 | <SPECIALCHARS : ["$", "_", "-"]>
2826}
2827
2828<DEFAULT,IN_DBL_BRACE>
2829TOKEN :
2830{
2831 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
2832 <STRING_LITERAL : ("\"" (
2833 <EscapeQuot>
2834 | <EscapeBslash>
2835 | <EscapeSlash>
2836 | <EscapeBspace>
2837 | <EscapeFormf>
2838 | <EscapeNl>
2839 | <EscapeCr>
2840 | <EscapeTab>
2841 | ~["\"","\\"])* "\"")
2842 | ("\'"(
2843 <EscapeApos>
2844 | <EscapeBslash>
2845 | <EscapeSlash>
2846 | <EscapeBspace>
2847 | <EscapeFormf>
2848 | <EscapeNl>
2849 | <EscapeCr>
2850 | <EscapeTab>
2851 | ~["\'","\\"])* "\'")>
2852 | < #EscapeQuot: "\\\"" >
2853 | < #EscapeApos: "\\\'" >
2854 | < #EscapeBslash: "\\\\" >
2855 | < #EscapeSlash: "\\/" >
2856 | < #EscapeBspace: "\\b" >
2857 | < #EscapeFormf: "\\f" >
2858 | < #EscapeNl: "\\n" >
2859 | < #EscapeCr: "\\r" >
2860 | < #EscapeTab: "\\t" >
2861}
2862
2863<DEFAULT,IN_DBL_BRACE>
2864TOKEN :
2865{
2866 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2867}
2868
2869<DEFAULT,IN_DBL_BRACE>
2870TOKEN :
2871{
2872 <VARIABLE : "$" <LETTER> (<LETTER> | <DIGIT> | "_")*>
2873}
2874
2875<DEFAULT,IN_DBL_BRACE>
2876SKIP:
2877{
2878 " "
2879 | "\t"
2880 | "\r"
2881 | "\n"
2882}
2883
2884<DEFAULT,IN_DBL_BRACE>
2885SKIP:
2886{
2887 <"//" (~["\n"])* "\n">
2888}
2889
2890<DEFAULT,IN_DBL_BRACE>
2891SKIP:
2892{
2893 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2894}
2895
2896<DEFAULT,IN_DBL_BRACE>
2897SKIP:
2898{
2899 <"/*"> { pushState(); } : INSIDE_COMMENT
2900}
2901
2902<INSIDE_COMMENT>
2903SPECIAL_TOKEN:
2904{
2905 <"+"(" ")*(~["*"])*>
2906}
2907
2908<INSIDE_COMMENT>
2909SKIP:
2910{
2911 <"/*"> { pushState(); }
2912}
2913
2914<INSIDE_COMMENT>
2915SKIP:
2916{
2917 <"*/"> { popState("*/"); }
2918 | <~[]>
2919}