blob: 8d842e389c4a17c2c1a903e26cc0cba6c29d155d [file] [log] [blame]
Michael Blow82464fb2017-03-28 18:48:13 -04001//
2// Licensed to the Apache Software Foundation (ASF) under one
3// or more contributor license agreements. See the NOTICE file
4// distributed with this work for additional information
5// regarding copyright ownership. The ASF licenses this file
6// to you under the Apache License, Version 2.0 (the
7// "License"); you may not use this file except in compliance
8// with the License. You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing,
13// software distributed under the License is distributed on an
14// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15// KIND, either express or implied. See the License for the
16// specific language governing permissions and limitations
17// under the License.
18//
Yingyi Bu391f09e2015-10-29 13:49:39 -070019options {
20
21
22 STATIC = false;
23
24}
25
26
27PARSER_BEGIN(AQLParser)
28
29package org.apache.asterix.lang.aql.parser;
30
31// For AQLParserTokenManager
Till Westmanne9b2adf2016-10-15 12:39:01 -070032import java.util.ArrayDeque;
Yingyi Bu391f09e2015-10-29 13:49:39 -070033
34import java.io.BufferedReader;
35import java.io.File;
36import java.io.FileInputStream;
37import java.io.FileNotFoundException;
38import java.io.IOException;
39import java.io.InputStreamReader;
40import java.io.Reader;
41import java.io.StringReader;
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.LinkedHashMap;
45import java.util.List;
46import java.util.Map;
47
48import org.apache.asterix.common.annotations.AutoDataGen;
49import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
50import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
51import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
52import org.apache.asterix.common.annotations.FieldIntervalDataGen;
53import org.apache.asterix.common.annotations.FieldValFileDataGen;
54import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
55import org.apache.asterix.common.annotations.IRecordFieldDataGen;
56import org.apache.asterix.common.annotations.InsertRandIntDataGen;
57import org.apache.asterix.common.annotations.ListDataGen;
58import org.apache.asterix.common.annotations.ListValFileDataGen;
59import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
60import org.apache.asterix.common.annotations.TypeDataGen;
61import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
62import org.apache.asterix.common.config.DatasetConfig.DatasetType;
63import org.apache.asterix.common.config.DatasetConfig.IndexType;
Taewoo Kime65e6ca2017-01-14 17:53:28 -080064import org.apache.asterix.common.exceptions.CompilationException;
Yingyi Bu391f09e2015-10-29 13:49:39 -070065import org.apache.asterix.common.functions.FunctionSignature;
66import org.apache.asterix.lang.aql.clause.DistinctClause;
67import org.apache.asterix.lang.aql.clause.ForClause;
68import org.apache.asterix.lang.aql.expression.FLWOGRExpression;
69import org.apache.asterix.lang.aql.expression.UnionExpr;
70import org.apache.asterix.lang.aql.util.RangeMapBuilder;
Yingyi Bucb5bf332017-01-02 22:19:50 -080071import org.apache.asterix.lang.aql.util.AQLFormatPrintUtil;
Yingyi Bu391f09e2015-10-29 13:49:39 -070072import org.apache.asterix.lang.common.base.Clause;
73import org.apache.asterix.lang.common.base.Expression;
Yingyi Bucb5bf332017-01-02 22:19:50 -080074import org.apache.asterix.lang.common.base.ILangExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -070075import org.apache.asterix.lang.common.base.IParser;
76import org.apache.asterix.lang.common.base.Literal;
77import org.apache.asterix.lang.common.base.Statement;
78import org.apache.asterix.lang.common.clause.GroupbyClause;
79import org.apache.asterix.lang.common.clause.LetClause;
80import org.apache.asterix.lang.common.clause.LimitClause;
81import org.apache.asterix.lang.common.clause.OrderbyClause;
82import org.apache.asterix.lang.common.clause.UpdateClause;
83import org.apache.asterix.lang.common.clause.WhereClause;
84import org.apache.asterix.lang.common.context.RootScopeFactory;
85import org.apache.asterix.lang.common.context.Scope;
86import org.apache.asterix.lang.common.expression.AbstractAccessor;
87import org.apache.asterix.lang.common.expression.CallExpr;
88import org.apache.asterix.lang.common.expression.FieldAccessor;
89import org.apache.asterix.lang.common.expression.FieldBinding;
90import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
91import org.apache.asterix.lang.common.expression.IfExpr;
92import org.apache.asterix.lang.common.expression.IndexAccessor;
93import org.apache.asterix.lang.common.expression.ListConstructor;
94import org.apache.asterix.lang.common.expression.LiteralExpr;
95import org.apache.asterix.lang.common.expression.OperatorExpr;
96import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
97import org.apache.asterix.lang.common.expression.QuantifiedExpression;
98import org.apache.asterix.lang.common.expression.RecordConstructor;
99import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
100import org.apache.asterix.lang.common.expression.TypeExpression;
101import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
102import org.apache.asterix.lang.common.expression.UnaryExpr;
103import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
104import org.apache.asterix.lang.common.expression.VariableExpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700105import org.apache.asterix.lang.common.literal.DoubleLiteral;
106import org.apache.asterix.lang.common.literal.FalseLiteral;
107import org.apache.asterix.lang.common.literal.FloatLiteral;
108import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
Yingyi Bu535d86b2016-05-23 16:44:25 -0700109import org.apache.asterix.lang.common.literal.MissingLiteral;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700110import org.apache.asterix.lang.common.literal.NullLiteral;
111import org.apache.asterix.lang.common.literal.StringLiteral;
112import org.apache.asterix.lang.common.literal.TrueLiteral;
113import org.apache.asterix.lang.common.parser.ScopeChecker;
114import org.apache.asterix.lang.common.statement.CompactStatement;
115import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
116import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
117import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
118import org.apache.asterix.lang.common.statement.CreateFeedStatement;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800119import org.apache.asterix.lang.common.statement.StartFeedStatement;
120import org.apache.asterix.lang.common.statement.StopFeedStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700121import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
122import org.apache.asterix.lang.common.statement.CreateIndexStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700123import org.apache.asterix.lang.common.statement.DatasetDecl;
124import org.apache.asterix.lang.common.statement.DataverseDecl;
125import org.apache.asterix.lang.common.statement.DataverseDropStatement;
126import org.apache.asterix.lang.common.statement.DeleteStatement;
127import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
Yingyi Buab817482016-08-19 21:29:31 -0700128import org.apache.asterix.lang.common.statement.DropDatasetStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700129import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
130import org.apache.asterix.lang.common.statement.FeedDropStatement;
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300131import org.apache.asterix.lang.common.statement.FeedPolicyDropStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700132import org.apache.asterix.lang.common.statement.FunctionDecl;
133import org.apache.asterix.lang.common.statement.FunctionDropStatement;
134import org.apache.asterix.lang.common.statement.IndexDropStatement;
135import org.apache.asterix.lang.common.statement.InsertStatement;
136import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
137import org.apache.asterix.lang.common.statement.LoadStatement;
138import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
139import org.apache.asterix.lang.common.statement.NodegroupDecl;
140import org.apache.asterix.lang.common.statement.Query;
141import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
142import org.apache.asterix.lang.common.statement.RunStatement;
143import org.apache.asterix.lang.common.statement.SetStatement;
144import org.apache.asterix.lang.common.statement.TypeDecl;
145import org.apache.asterix.lang.common.statement.TypeDropStatement;
146import org.apache.asterix.lang.common.statement.UpdateStatement;
Abdullah Alamoudi5dc958c2016-01-30 11:15:23 +0300147import org.apache.asterix.lang.common.statement.UpsertStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700148import org.apache.asterix.lang.common.statement.WriteStatement;
149import org.apache.asterix.lang.common.struct.Identifier;
150import org.apache.asterix.lang.common.struct.QuantifiedPair;
151import org.apache.asterix.lang.common.struct.VarIdentifier;
Yingyi Buab817482016-08-19 21:29:31 -0700152import org.apache.asterix.metadata.utils.MetadataConstants;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700153import org.apache.hyracks.algebricks.common.utils.Pair;
154import org.apache.hyracks.algebricks.common.utils.Triple;
155import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
156import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
157import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
158
159
Yingyi Bucaea8f02015-11-16 15:12:15 -0800160class AQLParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700161
162 // optimizer hints
163 private static final String AUTO_HINT = "auto";
164 private static final String BROADCAST_JOIN_HINT = "bcast";
165 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
166 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
167 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
168 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
169 private static final String HASH_GROUP_BY_HINT = "hash";
170 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
171 private static final String INMEMORY_HINT = "inmem";
172 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
173 private static final String INTERVAL_HINT = "interval";
174 private static final String LIST_HINT = "list";
175 private static final String LIST_VAL_FILE_HINT = "list-val-file";
176 private static final String RANGE_HINT = "range";
177 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
178 private static final String VAL_FILE_HINT = "val-files";
179 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
Yingyi Bu391f09e2015-10-29 13:49:39 -0700180 private static final String GEN_FIELDS_HINT = "gen-fields";
Yingyi Bu391f09e2015-10-29 13:49:39 -0700181 // data generator hints
182 private static final String DGEN_HINT = "dgen";
183
184 private static class IndexParams {
185 public IndexType type;
186 public int gramLength;
187
188 public IndexParams(IndexType type, int gramLength) {
189 this.type = type;
190 this.gramLength = gramLength;
191 }
192 };
193
194 private static class FunctionName {
195 public String dataverse = null;
196 public String library = null;
197 public String function = null;
198 public String hint = null;
199 }
200
201 private static String getHint(Token t) {
202 if (t.specialToken == null) {
203 return null;
204 }
205 String s = t.specialToken.image;
206 int n = s.length();
207 if (n < 2) {
208 return null;
209 }
210 return s.substring(1).trim();
211 }
212
Yingyi Bucb5bf332017-01-02 22:19:50 -0800213 private static void checkBindingVariable(Expression returnExpression, VariableExpr var,
214 ILangExpression bodyExpression) throws ParseException {
215 if (returnExpression != null && var == null) {
216 try {
217 throw new ParseException("Need a binding variable for the enclosed expression: " +
218 AQLFormatPrintUtil.toString(bodyExpression));
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800219 } catch (CompilationException e){
Yingyi Bucb5bf332017-01-02 22:19:50 -0800220 throw new ParseException(e.getLocalizedMessage());
221 }
222 }
223 }
224
Yingyi Bu391f09e2015-10-29 13:49:39 -0700225 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
226 IRecordFieldDataGen rfdg = null;
227 String splits[] = hint.split(" +");
228 if (splits[0].equals(VAL_FILE_HINT)) {
229 File[] valFiles = new File[splits.length - 1];
230 for (int k=1; k<splits.length; k++) {
231 valFiles[k-1] = new File(splits[k]);
232 }
233 rfdg = new FieldValFileDataGen(valFiles);
234 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
235 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
236 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
237 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
238 } else if (splits[0].equals(LIST_HINT)) {
239 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
240 } else if (splits[0].equals(INTERVAL_HINT)) {
241 FieldIntervalDataGen.ValueType vt;
242 if (splits[1].equals("int")) {
243 vt = FieldIntervalDataGen.ValueType.INT;
244 } else if (splits[1].equals("long")) {
245 vt = FieldIntervalDataGen.ValueType.LONG;
246 } else if (splits[1].equals("float")) {
247 vt = FieldIntervalDataGen.ValueType.FLOAT;
248 } else if (splits[1].equals("double")) {
249 vt = FieldIntervalDataGen.ValueType.DOUBLE;
250 } else {
251 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
252 }
253 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
254 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
255 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
256 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
257 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
258 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
259 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
260 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
261 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
262 } else if (splits[0].equals(AUTO_HINT)) {
263 rfdg = new AutoDataGen(splits[1]);
264 }
265 return rfdg;
266 }
267
268 public AQLParser(String s){
269 this(new StringReader(s));
270 super.setInput(s);
271 }
272
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800273 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700274 File file = new File(args[0]);
275 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
276 AQLParser parser = new AQLParser(fis);
277 List<Statement> st = parser.parse();
278 //st.accept(new AQLPrintVisitor(), 0);
279 }
280
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800281 public List<Statement> parse() throws CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700282 try {
283 return Statement();
284 } catch (Error e) {
285 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
286 // 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 -0800287 throw new CompilationException(new ParseException(e.getMessage()));
Yingyi Bu391f09e2015-10-29 13:49:39 -0700288 } catch (ParseException e){
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800289 throw new CompilationException(e.getMessage());
Yingyi Bu391f09e2015-10-29 13:49:39 -0700290 }
291 }
292}
293
294PARSER_END(AQLParser)
295
296
297List<Statement> Statement() throws ParseException:
298{
299 scopeStack.push(RootScopeFactory.createRootScope(this));
300 List<Statement> decls = new ArrayList<Statement>();
301 Statement stmt = null;
302}
303{
304 ( stmt = SingleStatement() (";") ?
305 {
306 decls.add(stmt);
307 }
308 )*
Till Westmann81870d72017-03-16 18:11:23 -0700309 (";") *
Yingyi Bu391f09e2015-10-29 13:49:39 -0700310 <EOF>
311 {
312 return decls;
313 }
314}
315
316Statement SingleStatement() throws ParseException:
317{
318 Statement stmt = null;
319}
320{
321 (
322 stmt = DataverseDeclaration()
323 | stmt = FunctionDeclaration()
324 | stmt = CreateStatement()
325 | stmt = LoadStatement()
326 | stmt = DropStatement()
327 | stmt = WriteStatement()
328 | stmt = SetStatement()
329 | stmt = InsertStatement()
Yingyi Bucb5bf332017-01-02 22:19:50 -0800330 | stmt = UpsertStatement()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700331 | stmt = DeleteStatement()
332 | stmt = UpdateStatement()
333 | stmt = FeedStatement()
334 | stmt = CompactStatement()
335 | stmt = Query()
336 | stmt = RefreshExternalDatasetStatement()
337 | stmt = RunStatement()
338 )
339 {
340 return stmt;
341 }
342}
343
344DataverseDecl DataverseDeclaration() throws ParseException:
345{
346 String dvName = null;
347}
348{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400349 <USE> <DATAVERSE> dvName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700350 {
351 defaultDataverse = dvName;
352 return new DataverseDecl(new Identifier(dvName));
353 }
354}
355
356Statement CreateStatement() throws ParseException:
357{
358 String hint = null;
359 boolean dgen = false;
360 Statement stmt = null;
361}
362{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400363 <CREATE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700364 (
365 {
366 hint = getHint(token);
367 if (hint != null && hint.startsWith(DGEN_HINT)) {
368 dgen = true;
369 }
370 }
371 stmt = TypeSpecification(hint, dgen)
372 | stmt = NodegroupSpecification()
373 | stmt = DatasetSpecification()
374 | stmt = IndexSpecification()
375 | stmt = DataverseSpecification()
376 | stmt = FunctionSpecification()
377 | stmt = FeedSpecification()
378 | stmt = FeedPolicySpecification()
379 )
380 {
381 return stmt;
382 }
383}
384
385TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
386{
387 Pair<Identifier,Identifier> nameComponents = null;
388 boolean ifNotExists = false;
389 TypeExpression typeExpr = null;
390}
391{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400392 <TYPE> nameComponents = TypeName() ifNotExists = IfNotExists()
393 <AS> typeExpr = TypeExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700394 {
395 long numValues = -1;
396 String filename = null;
397 if (dgen) {
398 String splits[] = hint.split(" +");
399 if (splits.length != 3) {
400 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
401 }
402 filename = splits[1];
403 numValues = Long.parseLong(splits[2]);
404 }
405 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
406 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
407 }
408}
409
410
411NodegroupDecl NodegroupSpecification() throws ParseException:
412{
413 String name = null;
414 String tmp = null;
415 boolean ifNotExists = false;
416 List<Identifier>ncNames = null;
417}
418{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400419 <NODEGROUP> name = Identifier()
420 ifNotExists = IfNotExists() <ON> tmp = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700421 {
422 ncNames = new ArrayList<Identifier>();
423 ncNames.add(new Identifier(tmp));
424 }
425 ( <COMMA> tmp = Identifier()
426 {
427 ncNames.add(new Identifier(tmp));
428 }
429 )*
430 {
431 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
432 }
433}
434
435DatasetDecl DatasetSpecification() throws ParseException:
436{
437 Pair<Identifier,Identifier> nameComponents = null;
438 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800439 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700440 String adapterName = null;
441 Map<String,String> properties = null;
442 Map<String,String> compactionPolicyProperties = null;
443 FunctionSignature appliedFunction = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800444 Pair<List<Integer>, List<List<String>>> primaryKeyFields = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700445 String nodeGroupName = null;
446 Map<String,String> hints = new HashMap<String,String>();
447 DatasetDecl dsetDecl = null;
448 boolean autogenerated = false;
449 String compactionPolicy = null;
450 boolean temp = false;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800451 Pair<Integer, List<String>> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800452 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700453}
454{
455 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400456 <EXTERNAL> <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800457 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700458 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400459 <USING> adapterName = AdapterName() properties = Configuration()
460 ( <ON> nodeGroupName = Identifier() )?
461 ( <HINTS> hints = Properties() )?
462 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700463 {
464 ExternalDetailsDecl edd = new ExternalDetailsDecl();
465 edd.setAdapter(adapterName);
466 edd.setProperties(properties);
467 dsetDecl = new DatasetDecl(nameComponents.first,
468 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800469 typeComponents.first,
470 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800471 metaTypeComponents.first,
472 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700473 nodeGroupName != null? new Identifier(nodeGroupName): null,
474 compactionPolicy,
475 compactionPolicyProperties,
476 hints,
477 DatasetType.EXTERNAL,
478 edd,
479 ifNotExists);
480 }
481
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400482 | (<INTERNAL> | <TEMPORARY> {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700483 temp = token.image.toLowerCase().equals("temporary");
484 }
Yingyi Bub9169b62016-02-26 21:21:49 -0800485 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700486 <DATASET> nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800487 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800488 (
489 { String name; }
490 <WITH>
491 name = Identifier()
492 {
493 if(!name.equals("meta")){
494 throw new ParseException("We can only support one additional associated field called \"meta\".");
495 }
496 }
497 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
498 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700499 ifNotExists = IfNotExists()
500 primaryKeyFields = PrimaryKey()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400501 ( <AUTOGENERATED> { autogenerated = true; } )?
502 ( <ON> nodeGroupName = Identifier() )?
503 ( <HINTS> hints = Properties() )?
504 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (compactionPolicyProperties = Configuration())? )?
505 ( <WITH> <FILTER> <ON> filterField = NestedField() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700506 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800507 if(filterField!=null && filterField.first!=0){
508 throw new ParseException("A filter field can only be a field in the main record of the dataset.");
509 }
510 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields.second,
511 primaryKeyFields.first,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700512 autogenerated,
Yingyi Buc9bfe252016-03-01 00:02:40 -0800513 filterField == null? null : filterField.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700514 temp);
515 dsetDecl = new DatasetDecl(nameComponents.first,
516 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800517 typeComponents.first,
518 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800519 metaTypeComponents.first,
520 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700521 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
522 compactionPolicy,
523 compactionPolicyProperties,
524 hints,
525 DatasetType.INTERNAL,
526 idd,
527 ifNotExists);
528 }
529 )
530 {
531 return dsetDecl;
532 }
533}
534
535RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
536{
537 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
538 Pair<Identifier,Identifier> nameComponents = null;
539 String datasetName = null;
540}
541{
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +0300542 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400543 <REFRESH> <EXTERNAL> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700544 {
545 redss.setDataverseName(nameComponents.first);
546 redss.setDatasetName(nameComponents.second);
547 return redss;
548 }
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +0300549 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700550}
551
552RunStatement RunStatement() throws ParseException:
553{
554 String system = null;
555 String tmp;
556 ArrayList<String> parameters = new ArrayList<String>();
557 Pair<Identifier,Identifier> nameComponentsFrom = null;
558 Pair<Identifier,Identifier> nameComponentsTo = null;
559}
560{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400561 <RUN> system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
Yingyi Bu391f09e2015-10-29 13:49:39 -0700562 {
563 parameters.add(tmp);
564 }
565 )*<RIGHTPAREN>
566 <FROM> <DATASET> nameComponentsFrom = QualifiedName()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400567 <TO> <DATASET> nameComponentsTo = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700568 {
569 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
570 }
571}
572
573CreateIndexStatement IndexSpecification() throws ParseException:
574{
575 CreateIndexStatement cis = new CreateIndexStatement();
576 String indexName = null;
577 boolean ifNotExists = false;
578 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800579 Pair<Integer, Pair<List<String>, TypeExpression>> fieldPair = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700580 IndexParams indexType = null;
581 boolean enforced = false;
582}
583{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400584 <INDEX> indexName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700585 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400586 <ON> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700587 <LEFTPAREN> ( fieldPair = OpenField()
588 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800589 cis.addFieldExprPair(fieldPair.second);
590 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700591 }
592 ) (<COMMA> fieldPair = OpenField()
593 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800594 cis.addFieldExprPair(fieldPair.second);
595 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700596 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400597 )* <RIGHTPAREN> ( <TYPE> indexType = IndexType() )? ( <ENFORCED> { enforced = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700598 {
599 cis.setIndexName(new Identifier(indexName));
600 cis.setIfNotExists(ifNotExists);
601 cis.setDataverseName(nameComponents.first);
602 cis.setDatasetName(nameComponents.second);
603 if (indexType != null) {
604 cis.setIndexType(indexType.type);
605 cis.setGramLength(indexType.gramLength);
606 }
607 cis.setEnforced(enforced);
608 return cis;
609 }
610}
611
612String CompactionPolicy() throws ParseException :
613{
614 String compactionPolicy = null;
615}
616{
617 compactionPolicy = Identifier()
618 {
619 return compactionPolicy;
620 }
621}
622
623String FilterField() throws ParseException :
624{
625 String filterField = null;
626}
627{
628 filterField = Identifier()
629 {
630 return filterField;
631 }
632}
633
634IndexParams IndexType() throws ParseException:
635{
636 IndexType type = null;
637 int gramLength = 0;
638}
639{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400640 (
641 <BTREE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700642 {
643 type = IndexType.BTREE;
644 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400645 |<RTREE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700646 {
647 type = IndexType.RTREE;
648 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400649 |<KEYWORD>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700650 {
651 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
652 }
Taewoo Kimc49405a2017-01-04 00:30:43 -0800653 |<FULLTEXT>
654 {
655 type = IndexType.SINGLE_PARTITION_WORD_INVIX;
656 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400657 |<NGRAM> <LEFTPAREN> <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700658 {
659 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
660 gramLength = Integer.valueOf(token.image);
661 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400662 <RIGHTPAREN>
663 )
664 {
665 return new IndexParams(type, gramLength);
666 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700667}
668
669CreateDataverseStatement DataverseSpecification() throws ParseException :
670{
671 String dvName = null;
672 boolean ifNotExists = false;
673 String format = null;
674}
675{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400676 <DATAVERSE> dvName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700677 ifNotExists = IfNotExists()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400678 ( <WITH> <FORMAT> format = StringLiteral() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700679 {
680 return new CreateDataverseStatement(new Identifier(dvName), format, ifNotExists);
681 }
682}
683
684CreateFunctionStatement FunctionSpecification() throws ParseException:
685{
686 FunctionSignature signature;
687 boolean ifNotExists = false;
688 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
689 String functionBody;
690 VarIdentifier var = null;
691 Expression functionBodyExpr;
692 Token beginPos;
693 Token endPos;
694 FunctionName fctName = null;
695
696 createNewScope();
697}
698{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400699 <FUNCTION> fctName = FunctionName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700700 ifNotExists = IfNotExists()
701 paramList = ParameterList()
702 <LEFTBRACE>
703 {
704 beginPos = token;
705 }
706 functionBodyExpr = Expression() <RIGHTBRACE>
707 {
708 endPos = token;
709 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
710 // TODO use fctName.library
711 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
712 getCurrentScope().addFunctionDescriptor(signature, false);
713 removeCurrentScope();
714 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
715 }
716}
717
718CreateFeedStatement FeedSpecification() throws ParseException:
719{
720 Pair<Identifier,Identifier> nameComponents = null;
721 boolean ifNotExists = false;
722 String adapterName = null;
723 Map<String,String> properties = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700724 CreateFeedStatement cfs = null;
725 Pair<Identifier,Identifier> sourceNameComponents = null;
Michael Blowd6cf6412016-06-30 02:44:35 -0400726
Yingyi Bu391f09e2015-10-29 13:49:39 -0700727}
728{
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800729 <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
730 <USING> adapterName = AdapterName() properties = Configuration()
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400731 {
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800732 cfs = new CreateFeedStatement(nameComponents, adapterName, properties, ifNotExists);
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400733 return cfs;
734 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700735}
736
737CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
738{
Michael Blowd6cf6412016-06-30 02:44:35 -0400739 String policyName = null;
740 String basePolicyName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700741 String sourcePolicyFile = null;
742 String definition = null;
743 boolean ifNotExists = false;
744 Map<String,String> properties = null;
745 CreateFeedPolicyStatement cfps = null;
746}
747{
748 (
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400749 <INGESTION> <POLICY> policyName = Identifier() ifNotExists = IfNotExists()
750 <FROM>
751 (
752 <POLICY> basePolicyName = Identifier() properties = Configuration() ( <DEFINITION> definition = StringLiteral())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700753 {
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400754 cfps = new CreateFeedPolicyStatement(policyName, basePolicyName, properties, definition, ifNotExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700755 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300756 |<PATH> sourcePolicyFile = StringLiteral() (<DEFINITION> definition = StringLiteral())?
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400757 {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700758 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400759 }
760 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700761 )
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400762 {
763 return cfps;
764 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700765}
766
767
768
769List<VarIdentifier> ParameterList() throws ParseException:
770{
771 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
772 VarIdentifier var = null;
773}
774{
775 <LEFTPAREN> (<VARIABLE>
776 {
777 var = new VarIdentifier();
778 var.setValue(token.image);
779 paramList.add(var);
780 getCurrentScope().addNewVarSymbolToScope(var);
781 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400782 ( <COMMA> <VARIABLE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700783 {
784 var = new VarIdentifier();
785 var.setValue(token.image);
786 paramList.add(var);
787 getCurrentScope().addNewVarSymbolToScope(var);
788 }
789 )*)? <RIGHTPAREN>
790 {
791 return paramList;
792 }
793}
794
795boolean IfNotExists() throws ParseException:
796{
797}
798{
Yingyi Budcd91122016-08-10 12:13:36 -0700799 (
800 LOOKAHEAD(1)
801 <IF>
802 <IDENTIFIER>
803 (
804 {
805 if(!token.image.equals("not")){
806 throw new ParseException("Expect word \"not\" at line " + token.beginLine + ", column "
807 + token.beginColumn +"!");
808 }
809 }
810 )
811 <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700812 {
813 return true;
814 }
815 )?
816 {
817 return false;
818 }
819}
820
821FunctionSignature ApplyFunction() throws ParseException:
822{
823 FunctionName functioName = null;
824 FunctionSignature funcSig = null;
825}
826{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400827 <APPLY> <FUNCTION> functioName = FunctionName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700828 {
829 String fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
830 return new FunctionSignature(functioName.dataverse, fqFunctionName, 1);
831 }
832}
833
834String GetPolicy() throws ParseException:
835{
836 String policy = null;
837}
838{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400839 <USING> <POLICY> policy = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700840 {
841 return policy;
842 }
843
844}
845
846FunctionSignature FunctionSignature() throws ParseException:
847{
848 FunctionName fctName = null;
849 int arity = 0;
850}
851{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400852 fctName = FunctionName() <SYMBOLAT> <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700853 {
854 arity = new Integer(token.image);
855 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
856 throw new ParseException(" invalid arity:" + arity);
857 }
858
859 // TODO use fctName.library
860 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
861 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
862 }
863}
864
Yingyi Buc9bfe252016-03-01 00:02:40 -0800865Pair<List<Integer>, List<List<String>>> PrimaryKey() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700866{
Yingyi Buc9bfe252016-03-01 00:02:40 -0800867 Pair<Integer, List<String>> tmp = null;
868 List<Integer> keyFieldSourceIndicators = new ArrayList<Integer>();
869 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
Yingyi Bu391f09e2015-10-29 13:49:39 -0700870}
871{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400872 <PRIMARY> <KEY> tmp = NestedField()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700873 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800874 keyFieldSourceIndicators.add(tmp.first);
875 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700876 }
877 ( <COMMA> tmp = NestedField()
878 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800879 keyFieldSourceIndicators.add(tmp.first);
880 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700881 }
882 )*
883 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800884 return new Pair<List<Integer>, List<List<String>>> (keyFieldSourceIndicators, primaryKeyFields);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700885 }
886}
887
888Statement DropStatement() throws ParseException:
889{
890 String id = null;
891 Pair<Identifier,Identifier> pairId = null;
892 Triple<Identifier,Identifier,Identifier> tripleId = null;
893 FunctionSignature funcSig = null;
894 boolean ifExists = false;
895 Statement stmt = null;
896}
897{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400898 <DROP>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700899 (
900 <DATASET> pairId = QualifiedName() ifExists = IfExists()
901 {
Yingyi Buab817482016-08-19 21:29:31 -0700902 stmt = new DropDatasetStatement(pairId.first, pairId.second, ifExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700903 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400904 | <INDEX> tripleId = DoubleQualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700905 {
906 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
907 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400908 | <NODEGROUP> id = Identifier() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700909 {
910 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
911 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400912 | <TYPE> pairId = TypeName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700913 {
914 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
915 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400916 | <DATAVERSE> id = Identifier() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700917 {
918 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
919 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400920 | <FUNCTION> funcSig = FunctionSignature() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700921 {
922 stmt = new FunctionDropStatement(funcSig, ifExists);
923 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +0400924 | <FEED> pairId = QualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700925 {
926 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
927 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300928 | <INGESTION> <POLICY> pairId = QualifiedName() ifExists = IfExists()
929 {
930 stmt = new FeedPolicyDropStatement(pairId.first, pairId.second, ifExists);
931 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700932 )
933 {
934 return stmt;
935 }
936}
937
938boolean IfExists() throws ParseException :
939{
940}
941{
Yingyi Budcd91122016-08-10 12:13:36 -0700942 (
943 LOOKAHEAD(1)
944 <IF> <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700945 {
946 return true;
947 }
948 )?
949 {
950 return false;
951 }
952}
953
954InsertStatement InsertStatement() throws ParseException:
955{
956 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800957 VariableExpr var = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700958 Query query;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800959 Expression returnExpression = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700960}
961{
Yingyi Bucb5bf332017-01-02 22:19:50 -0800962 <INSERT> <INTO> <DATASET> nameComponents = QualifiedName()
Steven Glenn Jacobsafa909a2016-10-16 10:35:30 -0700963 (<AS> var = Variable())?
964 {
965 if(var != null){
966 getCurrentScope().addNewVarSymbolToScope(var.getVar());
967 }
968 }
Yingyi Bucb5bf332017-01-02 22:19:50 -0800969 query = Query() ( <RETURNING> returnExpression = Expression())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700970 {
Yingyi Bucb5bf332017-01-02 22:19:50 -0800971 checkBindingVariable(returnExpression, var, query);
Yingyi Bucaea8f02015-11-16 15:12:15 -0800972 query.setTopLevel(true);
Yingyi Bucb5bf332017-01-02 22:19:50 -0800973 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
974 returnExpression);
975 }
976}
977
978UpsertStatement UpsertStatement() throws ParseException:
979{
980 Pair<Identifier,Identifier> nameComponents = null;
981 VariableExpr var = null;
982 Query query;
983 Expression returnExpression = null;
984}
985{
986 <UPSERT> <INTO> <DATASET> nameComponents = QualifiedName()
987 (<AS> var = Variable())?
988 {
989 if(var != null){
990 getCurrentScope().addNewVarSymbolToScope(var.getVar());
991 }
992 }
993 query = Query() ( <RETURNING> returnExpression = Expression())?
994 {
995 checkBindingVariable(returnExpression, var, query);
996 query.setTopLevel(true);
997 return new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
998 returnExpression);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700999 }
1000}
1001
1002DeleteStatement DeleteStatement() throws ParseException:
1003{
1004 VariableExpr var = null;
1005 Expression condition = null;
1006 Pair<Identifier, Identifier> nameComponents;
1007 // This is related to the new metadata lock management
1008 setDataverses(new ArrayList<String>());
1009 setDatasets(new ArrayList<String>());
1010
1011}
1012{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001013 <DELETE> var = Variable()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001014 {
1015 getCurrentScope().addNewVarSymbolToScope(var.getVar());
1016 }
1017 <FROM> <DATASET> nameComponents = QualifiedName()
1018 (<WHERE> condition = Expression())?
1019 {
1020 // First we get the dataverses and datasets that we want to lock
1021 List<String> dataverses = getDataverses();
1022 List<String> datasets = getDatasets();
1023 // we remove the pointer to the dataverses and datasets
1024 setDataverses(null);
1025 setDatasets(null);
1026 return new DeleteStatement(var, nameComponents.first, nameComponents.second,
1027 condition, getVarCounter(), dataverses, datasets);
1028 }
1029}
1030
1031UpdateStatement UpdateStatement() throws ParseException:
1032{
1033 VariableExpr vars;
1034 Expression target;
1035 Expression condition;
1036 UpdateClause uc;
1037 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
1038}
1039{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001040 <UPDATE> vars = Variable() <IN> target = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001041 <WHERE> condition = Expression()
1042 <LEFTPAREN> (uc = UpdateClause()
1043 {
1044 ucs.add(uc);
1045 }
1046 (<COMMA> uc = UpdateClause()
1047 {
1048 ucs.add(uc);
1049 }
1050 )*) <RIGHTPAREN>
1051 {
1052 return new UpdateStatement(vars, target, condition, ucs);
1053 }
1054}
1055
1056UpdateClause UpdateClause() throws ParseException:
1057{
1058 Expression target = null;
1059 Expression value = null ;
1060 InsertStatement is = null;
1061 DeleteStatement ds = null;
1062 UpdateStatement us = null;
1063 Expression condition = null;
1064 UpdateClause ifbranch = null;
1065 UpdateClause elsebranch = null;
1066}
1067{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001068 (<SET> target = Expression() <ASSIGN> value = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001069 | is = InsertStatement()
1070 | ds = DeleteStatement()
1071 | us = UpdateStatement()
1072 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
1073 <THEN> ifbranch = UpdateClause()
1074 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
1075 {
1076 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
1077 }
1078 )
1079}
1080
1081Statement SetStatement() throws ParseException:
1082{
1083 String pn = null;
1084 String pv = null;
1085}
1086{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001087 <SET> pn = Identifier() pv = StringLiteral()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001088 {
1089 return new SetStatement(pn, pv);
1090 }
1091}
1092
1093Statement WriteStatement() throws ParseException:
1094{
1095 String nodeName = null;
1096 String fileName = null;
1097 Query query;
1098 String writerClass = null;
1099 Pair<Identifier,Identifier> nameComponents = null;
1100}
1101{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001102 <WRITE> <OUTPUT> <TO> nodeName = Identifier() <COLON> fileName = StringLiteral()
1103 ( <USING> writerClass = StringLiteral() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001104 {
1105 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
1106 }
1107}
1108
1109LoadStatement LoadStatement() throws ParseException:
1110{
1111 Identifier dataverseName = null;
1112 Identifier datasetName = null;
1113 boolean alreadySorted = false;
1114 String adapterName;
1115 Map<String,String> properties;
1116 Pair<Identifier,Identifier> nameComponents = null;
1117}
1118{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001119 <LOAD> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001120 {
1121 dataverseName = nameComponents.first;
1122 datasetName = nameComponents.second;
1123 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001124 <USING> adapterName = AdapterName() properties = Configuration()
1125 (<PRESORTED>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001126 {
1127 alreadySorted = true;
1128 }
1129 )?
1130 {
1131 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1132 }
1133}
1134
1135
1136String AdapterName() throws ParseException :
1137{
1138 String adapterName = null;
1139}
1140{
1141 adapterName = Identifier()
1142 {
1143 return adapterName;
1144 }
1145}
1146
1147Statement CompactStatement() throws ParseException:
1148{
1149 Pair<Identifier,Identifier> nameComponents = null;
1150 Statement stmt = null;
1151}
1152{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001153 <COMPACT> <DATASET> nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001154 {
1155 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1156 }
1157 {
1158 return stmt;
1159 }
1160}
1161
1162Statement FeedStatement() throws ParseException:
1163{
1164 Pair<Identifier,Identifier> feedNameComponents = null;
1165 Pair<Identifier,Identifier> datasetNameComponents = null;
1166
1167 Map<String,String> configuration = null;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001168 FunctionSignature appliedFunction = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001169 Statement stmt = null;
1170 String policy = null;
1171}
1172{
1173 (
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001174 <CONNECT> <FEED> feedNameComponents = QualifiedName() <TO> <DATASET> datasetNameComponents = QualifiedName()
1175 (appliedFunction = ApplyFunction())? (policy = GetPolicy())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001176 {
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001177 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, appliedFunction, policy, getVarCounter());
Yingyi Bu391f09e2015-10-29 13:49:39 -07001178 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001179 | <DISCONNECT> <FEED> feedNameComponents = QualifiedName() <FROM> <DATASET> datasetNameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001180 {
1181 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1182 }
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001183 | <START> <FEED> feedNameComponents = QualifiedName()
1184 {
1185 stmt = new StartFeedStatement (feedNameComponents);
1186 }
1187 | <STOP> <FEED> feedNameComponents = QualifiedName()
1188 {
1189 stmt = new StopFeedStatement (feedNameComponents);
1190 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001191 )
1192 {
1193 return stmt;
1194 }
1195}
1196
1197Map<String,String> Configuration() throws ParseException :
1198{
1199 Map<String,String> configuration = new LinkedHashMap<String,String>();
1200 Pair<String, String> keyValuePair = null;
1201}
1202{
1203 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1204 {
1205 configuration.put(keyValuePair.first, keyValuePair.second);
1206 }
1207 ( <COMMA> keyValuePair = KeyValuePair()
1208 {
1209 configuration.put(keyValuePair.first, keyValuePair.second);
1210 }
1211 )* )? <RIGHTPAREN>
1212 {
1213 return configuration;
1214 }
1215}
1216
1217Pair<String, String> KeyValuePair() throws ParseException:
1218{
1219 String key;
1220 String value;
1221}
1222{
1223 <LEFTPAREN> key = StringLiteral() <EQ> value = StringLiteral() <RIGHTPAREN>
1224 {
1225 return new Pair<String, String>(key, value);
1226 }
1227}
1228
1229Map<String,String> Properties() throws ParseException:
1230{
1231 Map<String,String> properties = new HashMap<String,String>();
1232 Pair<String, String> property;
1233}
1234{
1235 ( <LEFTPAREN> property = Property()
1236 {
1237 properties.put(property.first, property.second);
1238 }
1239 ( <COMMA> property = Property()
1240 {
1241 properties.put(property.first, property.second);
1242 }
1243 )* <RIGHTPAREN> )?
1244 {
1245 return properties;
1246 }
1247}
1248
1249Pair<String, String> Property() throws ParseException:
1250{
1251 String key;
1252 String value;
1253}
1254{
1255 key = Identifier() <EQ> ( value = StringLiteral() | <INTEGER_LITERAL>
1256 {
1257 try {
1258 value = "" + Long.valueOf(token.image);
1259 } catch (NumberFormatException nfe) {
1260 throw new ParseException("inapproriate value: " + token.image);
1261 }
1262 }
1263 )
1264 {
1265 return new Pair<String, String>(key.toUpperCase(), value);
1266 }
1267}
1268
1269TypeExpression IndexedTypeExpr() throws ParseException:
1270{
1271 TypeExpression typeExpr = null;
1272}
1273{
1274 (
1275 typeExpr = TypeReference()
1276 | typeExpr = OrderedListTypeDef()
1277 | typeExpr = UnorderedListTypeDef()
1278 )
1279 {
1280 return typeExpr;
1281 }
1282}
1283
1284TypeExpression TypeExpr() throws ParseException:
1285{
1286 TypeExpression typeExpr = null;
1287}
1288{
1289 (
1290 typeExpr = RecordTypeDef()
1291 | typeExpr = TypeReference()
1292 | typeExpr = OrderedListTypeDef()
1293 | typeExpr = UnorderedListTypeDef()
1294 )
1295 {
1296 return typeExpr;
1297 }
1298}
1299
1300RecordTypeDefinition RecordTypeDef() throws ParseException:
1301{
1302 RecordTypeDefinition recType = new RecordTypeDefinition();
1303 RecordTypeDefinition.RecordKind recordKind = null;
1304}
1305{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001306 ( <CLOSED>{ recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1307 | <OPEN>{ recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001308 <LEFTBRACE>
1309 {
1310 String hint = getHint(token);
1311 if (hint != null) {
1312 String splits[] = hint.split(" +");
1313 if (splits[0].equals(GEN_FIELDS_HINT)) {
1314 if (splits.length != 5) {
1315 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1316 }
1317 if (!splits[1].equals("int")) {
1318 throw new ParseException("The only supported type for gen-fields is int.");
1319 }
1320 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1321 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1322 recType.setUndeclaredFieldsDataGen(ufdg);
1323 }
1324 }
1325
1326 }
1327 (
1328 RecordField(recType)
1329 ( <COMMA> RecordField(recType) )*
1330 )?
1331 <RIGHTBRACE>
1332 {
1333 if (recordKind == null) {
1334 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1335 }
1336 recType.setRecordKind(recordKind);
1337 return recType;
1338 }
1339}
1340
1341void RecordField(RecordTypeDefinition recType) throws ParseException:
1342{
1343 String fieldName;
1344 TypeExpression type = null;
1345 boolean nullable = false;
1346}
1347{
1348 fieldName = Identifier()
1349 {
1350 String hint = getHint(token);
1351 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1352 }
1353 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1354 {
1355 recType.addField(fieldName, type, nullable, rfdg);
1356 }
1357}
1358
1359TypeReferenceExpression TypeReference() throws ParseException:
1360{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001361 Pair<Identifier,Identifier> id = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001362}
1363{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001364 id = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001365 {
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001366 if (id.first == null && id.second.getValue().equalsIgnoreCase("int")) {
1367 id.second.setValue("int64");
Yingyi Bu391f09e2015-10-29 13:49:39 -07001368 }
1369
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001370 return new TypeReferenceExpression(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001371 }
1372}
1373
1374OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1375{
1376 TypeExpression type = null;
1377}
1378{
1379 <LEFTBRACKET>
1380 ( type = TypeExpr() )
1381 <RIGHTBRACKET>
1382 {
1383 return new OrderedListTypeDefinition(type);
1384 }
1385}
1386
1387
1388UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1389{
1390 TypeExpression type = null;
1391}
1392{
1393 <LEFTDBLBRACE>
1394 ( type = TypeExpr() )
1395 <RIGHTDBLBRACE>
1396 {
1397 return new UnorderedListTypeDefinition(type);
1398 }
1399}
1400
1401FunctionName FunctionName() throws ParseException:
1402{
1403 String first = null;
1404 String second = null;
1405 String third = null;
1406 boolean secondAfterDot = false;
1407}
1408{
Michael Blowd6cf6412016-06-30 02:44:35 -04001409 first = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001410 {
1411 FunctionName result = new FunctionName();
1412 result.hint = getHint(token);
Michael Blowd6cf6412016-06-30 02:44:35 -04001413 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001414 ( <DOT> second = Identifier()
1415 {
1416 secondAfterDot = true;
1417 }
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001418 ( <SYMBOLHASH> third = Identifier())? | <SYMBOLHASH> second = Identifier() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001419 {
1420 if (second == null) {
1421 result.dataverse = defaultDataverse;
1422 result.library = null;
1423 result.function = first;
1424 } else if (third == null) {
1425 if (secondAfterDot) {
1426 result.dataverse = first;
1427 result.library = null;
1428 result.function = second;
1429 } else {
1430 result.dataverse = defaultDataverse;
1431 result.library = first;
1432 result.function = second;
1433 }
1434 } else {
1435 result.dataverse = first;
1436 result.library = second;
1437 result.function = third;
1438 }
1439
1440 if (result.function.equalsIgnoreCase("int")) {
1441 result.function = "int64";
1442 }
1443 return result;
1444 }
1445}
1446
1447
1448Pair<Identifier,Identifier> TypeName() throws ParseException:
1449{
1450 Pair<Identifier,Identifier> name = null;
1451}
1452{
1453 name = QualifiedName()
1454 {
1455 if (name.first == null) {
1456 name.first = new Identifier(defaultDataverse);
1457 }
1458 return name;
1459 }
1460}
1461
1462String Identifier() throws ParseException:
1463{
1464 String lit = null;
1465}
1466{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001467 ((<IDENTIFIER>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001468 {
1469 return token.image;
1470 }
1471 | lit = StringLiteral()
1472 {
1473 return lit;
1474 }
1475 )
1476}
1477
Yingyi Buc9bfe252016-03-01 00:02:40 -08001478Pair<Integer, Pair<List<String>, TypeExpression>> OpenField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001479{
1480 TypeExpression fieldType = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001481 Pair<Integer, List<String>> fieldList = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001482}
1483{
1484 fieldList = NestedField()
Mike Careyaf8e19d2016-08-25 17:55:17 -07001485 ( <COLON> fieldType = IndexedTypeExpr() <QUES>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001486 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001487 return new Pair<Integer, Pair<List<String>, TypeExpression>>
1488 (fieldList.first, new Pair<List<String>, TypeExpression>(fieldList.second, fieldType));
Yingyi Bu391f09e2015-10-29 13:49:39 -07001489 }
1490}
1491
Yingyi Buc9bfe252016-03-01 00:02:40 -08001492Pair<Integer, List<String>> NestedField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001493{
1494 List<String> exprList = new ArrayList<String>();
1495 String lit = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001496 int source = 0;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001497}
1498{
1499 lit = Identifier()
1500 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001501 boolean meetParens = false;
1502 }
1503 (
Yingyi Buc9bfe252016-03-01 00:02:40 -08001504 LOOKAHEAD(1)
Yingyi Bub9169b62016-02-26 21:21:49 -08001505 <LEFTPAREN><RIGHTPAREN>
1506 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001507 if(!lit.equals("meta")){
Yingyi Bub9169b62016-02-26 21:21:49 -08001508 throw new ParseException("The string before () has to be \"meta\".");
1509 }
1510 meetParens = true;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001511 source = 1;
Yingyi Bub9169b62016-02-26 21:21:49 -08001512 }
1513 )?
1514 {
1515 if(!meetParens){
1516 exprList.add(lit);
1517 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001518 }
1519 (<DOT>
1520 lit = Identifier()
1521 {
1522 exprList.add(lit);
1523 }
1524 )*
1525 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001526 return new Pair<Integer, List<String>>(source, exprList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001527 }
1528}
1529
1530
1531
1532String StringLiteral() throws ParseException:
1533{
1534}
1535{
1536 <STRING_LITERAL>
1537 {
1538 return removeQuotesAndEscapes(token.image);
1539 }
1540}
1541
1542Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1543{
1544 String first = null;
1545 String second = null;
1546}
1547{
1548 first = Identifier() (<DOT> second = Identifier())?
1549 {
1550 Identifier id1 = null;
1551 Identifier id2 = null;
1552 if (second == null) {
1553 id2 = new Identifier(first);
1554 } else
1555 {
1556 id1 = new Identifier(first);
1557 id2 = new Identifier(second);
1558 }
1559 return new Pair<Identifier,Identifier>(id1, id2);
1560 }
1561}
1562
1563Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1564{
1565 String first = null;
1566 String second = null;
1567 String third = null;
1568}
1569{
1570 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1571 {
1572 Identifier id1 = null;
1573 Identifier id2 = null;
1574 Identifier id3 = null;
1575 if (third == null) {
1576 id2 = new Identifier(first);
1577 id3 = new Identifier(second);
1578 } else {
1579 id1 = new Identifier(first);
1580 id2 = new Identifier(second);
1581 id3 = new Identifier(third);
1582 }
1583 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1584 }
1585}
1586
1587FunctionDecl FunctionDeclaration() throws ParseException:
1588{
1589 FunctionDecl funcDecl;
1590 FunctionSignature signature;
1591 String functionName;
1592 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1593 Expression funcBody;
1594 createNewScope();
1595}
1596{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04001597 <DECLARE> <FUNCTION> functionName = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001598 paramList = ParameterList()
1599 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1600 {
1601 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1602 getCurrentScope().addFunctionDescriptor(signature, false);
1603 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1604 removeCurrentScope();
1605 return funcDecl;
1606 }
1607}
1608
1609
1610Query Query() throws ParseException:
1611{
Till Westmannef3f0272016-07-27 18:34:01 -07001612 Query query = new Query(false);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001613 // we set the pointers to the dataverses and datasets lists to fill them with entities to be locked
1614 setDataverses(query.getDataverses());
1615 setDatasets(query.getDatasets());
1616 Expression expr;
1617}
1618{
1619 expr = Expression()
1620 {
1621 query.setBody(expr);
1622 query.setVarCounter(getVarCounter());
1623 // we remove the pointers to the locked entities before we return the query object
1624 setDataverses(null);
1625 setDatasets(null);
1626 return query;
1627 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001628}
1629
1630
1631
1632Expression Expression():
1633{
1634 Expression expr = null;
1635 Expression exprP = null;
1636}
1637{
1638(
1639
1640//OperatorExpr | IfThenElse | FLWOGRExpression | QuantifiedExpression
1641 expr = OperatorExpr()
1642 | expr = IfThenElse()
1643 | expr = FLWOGR()
1644 | expr = QuantifiedExpression()
1645
1646
1647)
1648 {
1649 return (exprP==null) ? expr : exprP;
1650 }
1651}
1652
1653
1654
1655Expression OperatorExpr()throws ParseException:
1656{
1657 OperatorExpr op = null;
1658 Expression operand = null;
1659}
1660{
1661 operand = AndExpr()
1662 (
1663
1664 <OR>
1665 {
1666 if (op == null) {
1667 op = new OperatorExpr();
1668 op.addOperand(operand);
1669 op.setCurrentop(true);
1670 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001671 try{
1672 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001673 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001674 throw new ParseException(e.getMessage());
1675 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001676 }
1677
1678 operand = AndExpr()
1679 {
1680 op.addOperand(operand);
1681 }
1682
1683 )*
1684
1685 {
1686 return op==null? operand: op;
1687 }
1688}
1689
1690Expression AndExpr()throws ParseException:
1691{
1692 OperatorExpr op = null;
1693 Expression operand = null;
1694}
1695{
1696 operand = RelExpr()
1697 (
1698
1699 <AND>
1700 {
1701 if (op == null) {
1702 op = new OperatorExpr();
1703 op.addOperand(operand);
1704 op.setCurrentop(true);
1705 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001706 try{
1707 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001708 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001709 throw new ParseException(e.getMessage());
1710 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001711 }
1712
1713 operand = RelExpr()
1714 {
1715 op.addOperand(operand);
1716 }
1717
1718 )*
1719
1720 {
1721 return op==null? operand: op;
1722 }
1723}
1724
1725
1726
1727Expression RelExpr()throws ParseException:
1728{
1729 OperatorExpr op = null;
1730 Expression operand = null;
1731 boolean broadcast = false;
1732 IExpressionAnnotation annotation = null;
1733}
1734{
1735 operand = AddExpr()
1736 {
1737 if (operand instanceof VariableExpr) {
1738 String hint = getHint(token);
1739 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1740 broadcast = true;
1741 }
1742 }
1743 }
1744
1745 (
1746 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> |<SIMILAR>)
1747 {
1748 String mhint = getHint(token);
1749 if (mhint != null) {
1750 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1751 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1752 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1753 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1754 }
1755 }
1756 if (op == null) {
1757 op = new OperatorExpr();
1758 op.addOperand(operand, broadcast);
1759 op.setCurrentop(true);
1760 broadcast = false;
1761 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001762 try{
1763 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001764 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001765 throw new ParseException(e.getMessage());
1766 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001767 }
1768
1769 operand = AddExpr()
1770 {
1771 broadcast = false;
1772 if (operand instanceof VariableExpr) {
1773 String hint = getHint(token);
1774 if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
1775 broadcast = true;
1776 }
1777 }
1778 op.addOperand(operand, broadcast);
1779 }
1780 )?
1781
1782 {
1783 if (annotation != null) {
1784 op.addHint(annotation);
1785 }
1786 return op==null? operand: op;
1787 }
1788}
1789
1790Expression AddExpr()throws ParseException:
1791{
1792 OperatorExpr op = null;
1793 Expression operand = null;
1794}
1795{
1796 operand = MultExpr()
1797
1798 ( (<PLUS> | <MINUS>)
1799 {
1800 if (op == null) {
1801 op = new OperatorExpr();
1802 op.addOperand(operand);
1803 op.setCurrentop(true);
1804 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001805 try{
1806 ((OperatorExpr)op).addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001807 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001808 throw new ParseException(e.getMessage());
1809 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001810 }
1811
1812 operand = MultExpr()
1813 {
1814 op.addOperand(operand);
1815 }
1816 )*
1817
1818 {
1819 return op==null? operand: op;
1820 }
1821}
1822
1823Expression MultExpr()throws ParseException:
1824{
1825 OperatorExpr op = null;
1826 Expression operand = null;
1827}
1828{
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001829 operand = ExponentExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001830
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001831 (( <MUL> | <DIV> | <MOD> | <IDIV>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001832 {
1833 if (op == null) {
1834 op = new OperatorExpr();
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001835 op.addOperand(operand);
1836 op.setCurrentop(true);
1837 }
1838 try{
1839 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001840 } catch (CompilationException e){
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001841 throw new ParseException(e.getMessage());
1842 }
1843 }
1844 operand = ExponentExpr()
1845 {
1846 op.addOperand(operand);
1847 }
1848 )*
1849
1850 {
1851 return op==null?operand:op;
1852 }
1853}
1854
1855Expression ExponentExpr()throws ParseException:
1856{
1857 OperatorExpr op = null;
1858 Expression operand = null;
1859}
1860{
1861 operand = UnionExpr()
1862
1863 ( <CARET>
1864 {
1865 if (op == null) {
1866 op = new OperatorExpr();
1867 op.addOperand(operand);
1868 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001869 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001870 try{
1871 op.addOperator(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001872 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001873 throw new ParseException(e.getMessage());
1874 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001875 }
1876 operand = UnionExpr()
1877 {
1878 op.addOperand(operand);
1879 }
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001880 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001881
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001882 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07001883 return op==null?operand:op;
Yingyi Bu79ccdac2016-07-26 23:49:24 -07001884 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001885}
1886
1887Expression UnionExpr() throws ParseException:
1888{
1889 UnionExpr union = null;
1890 Expression operand1 = null;
1891 Expression operand2 = null;
1892}
1893{
1894 operand1 = UnaryExpr()
1895 (<UNION>
1896 (operand2 = UnaryExpr()) {
1897 if (union == null) {
1898 union = new UnionExpr();
1899 union.addExpr(operand1);
1900 }
1901 union.addExpr(operand2);
1902 } )*
1903 {
1904 return (union == null)? operand1: union;
1905 }
1906}
1907
1908Expression UnaryExpr() throws ParseException:
1909{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001910 UnaryExpr uexpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001911 Expression expr = null;
1912}
1913{
1914 ( (<PLUS> | <MINUS>)
1915 {
1916 uexpr = new UnaryExpr();
Yingyi Bu196db5d2016-07-15 19:07:20 -07001917 try{
1918 uexpr.setExprType(token.image);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001919 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001920 throw new ParseException(e.getMessage());
1921 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001922 }
1923 )?
1924
1925 expr = ValueExpr()
1926 {
1927 if(uexpr!=null){
1928 ((UnaryExpr)uexpr).setExpr(expr);
1929 return uexpr;
1930 }
1931 else{
1932 return expr;
1933 }
1934 }
1935}
1936
1937Expression ValueExpr()throws ParseException:
1938{
1939 Expression expr = null;
1940 Identifier ident = null;
1941 AbstractAccessor fa = null;
1942 Expression indexExpr = null;
1943}
1944{
1945 expr = PrimaryExpr() ( ident = Field()
1946 {
1947 fa = (fa == null ? new FieldAccessor(expr, ident)
1948 : new FieldAccessor(fa, ident));
1949 }
1950 | indexExpr = Index()
1951 {
1952 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
1953 : new IndexAccessor(fa, indexExpr));
1954 }
1955 )*
1956 {
1957 return fa == null ? expr : fa;
1958 }
1959}
1960
1961Identifier Field() throws ParseException:
1962{
1963 String ident = null;
1964}
1965{
1966 <DOT> ident = Identifier()
1967 {
1968 return new Identifier(ident);
1969 }
1970}
1971
1972Expression Index() throws ParseException:
1973{
1974 Expression expr = null;
1975}
1976{
1977 <LEFTBRACKET> ( expr = Expression()
1978 {
1979 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
1980 {
1981 Literal lit = ((LiteralExpr)expr).getValue();
1982 if(lit.getLiteralType() != Literal.Type.INTEGER &&
1983 lit.getLiteralType() != Literal.Type.LONG) {
1984 throw new ParseException("Index should be an INTEGER");
1985 }
1986 }
1987 }
1988
1989 | <QUES> // ANY
1990
1991 )
1992
1993 <RIGHTBRACKET>
1994 {
1995 return expr;
1996 }
1997}
1998
1999
2000Expression PrimaryExpr()throws ParseException:
2001{
2002 Expression expr = null;
2003}
2004{
2005 ( LOOKAHEAD(2)
2006 expr = FunctionCallExpr()
2007 | expr = Literal()
2008 | expr = DatasetAccessExpression()
2009 | expr = VariableRef()
2010 {
2011 if(((VariableExpr)expr).getIsNewVar() == true)
2012 throw new ParseException("can't find variable " + ((VariableExpr)expr).getVar());
2013 }
2014 | expr = ListConstructor()
2015 | expr = RecordConstructor()
2016 | expr = ParenthesizedExpression()
2017 )
2018 {
2019 return expr;
2020 }
2021}
2022
2023Expression Literal() throws ParseException:
2024{
2025 LiteralExpr lit = new LiteralExpr();
2026 String str = null;
2027}
2028{
2029 ( str = StringLiteral()
2030 {
2031 lit.setValue(new StringLiteral(str));
2032 }
2033 | <INTEGER_LITERAL>
2034 {
2035 lit.setValue(new LongIntegerLiteral(new Long(token.image)));
2036 }
2037 | <FLOAT_LITERAL>
2038 {
2039 lit.setValue(new FloatLiteral(new Float(token.image)));
2040 }
2041 | <DOUBLE_LITERAL>
2042 {
2043 lit.setValue(new DoubleLiteral(new Double(token.image)));
2044 }
Yingyi Bu535d86b2016-05-23 16:44:25 -07002045 | <MISSING>
2046 {
2047 lit.setValue(MissingLiteral.INSTANCE);
2048 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002049 | <NULL>
2050 {
2051 lit.setValue(NullLiteral.INSTANCE);
2052 }
2053 | <TRUE>
2054 {
2055 lit.setValue(TrueLiteral.INSTANCE);
2056 }
2057 | <FALSE>
2058 {
2059 lit.setValue(FalseLiteral.INSTANCE);
2060 }
2061 )
2062 {
2063 return lit;
2064 }
2065}
2066
2067
2068VariableExpr VariableRef() throws ParseException:
2069{
2070 VariableExpr varExp = new VariableExpr();
2071 VarIdentifier var = new VarIdentifier();
2072}
2073{
2074 <VARIABLE>
2075 {
2076 String varName = token.image;
2077 Identifier ident = lookupSymbol(varName);
2078 if (isInForbiddenScopes(varName)) {
2079 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.");
2080 }
2081 if(ident != null) { // exist such ident
2082 varExp.setIsNewVar(false);
2083 varExp.setVar((VarIdentifier)ident);
2084 } else {
2085 varExp.setVar(var);
2086 }
2087 var.setValue(varName);
2088 return varExp;
2089 }
2090}
2091
2092
2093VariableExpr Variable() throws ParseException:
2094{
2095 VariableExpr varExp = new VariableExpr();
2096 VarIdentifier var = new VarIdentifier();
2097}
2098{
2099 <VARIABLE>
2100 {
2101 Identifier ident = lookupSymbol(token.image);
2102 if(ident != null) { // exist such ident
2103 varExp.setIsNewVar(false);
2104 }
2105 varExp.setVar(var);
2106 var.setValue(token.image);
2107 return varExp;
2108 }
2109}
2110
2111Expression ListConstructor() throws ParseException:
2112{
2113 Expression expr = null;
2114}
2115{
2116 (
2117 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
2118 )
2119
2120 {
2121 return expr;
2122 }
2123}
2124
2125
2126ListConstructor OrderedListConstructor() throws ParseException:
2127{
2128 ListConstructor expr = new ListConstructor();
2129 List<Expression> exprList = null;
2130 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
2131}
2132{
2133 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
2134 {
2135 expr.setExprList(exprList);
2136 return expr;
2137 }
2138}
2139
2140ListConstructor UnorderedListConstructor() throws ParseException:
2141{
2142 ListConstructor expr = new ListConstructor();
2143 List<Expression> exprList = null;
2144 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
2145}
2146{
2147 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
2148 {
2149 expr.setExprList(exprList);
2150 return expr;
2151 }
2152}
2153
2154List<Expression> ExpressionList() throws ParseException:
2155{
2156 Expression expr = null;
2157 List<Expression> list = null;
2158 List<Expression> exprList = new ArrayList<Expression>();
2159}
2160{
2161 (
2162 expr = Expression() { exprList.add(expr); }
2163 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
2164 )?
2165 (LOOKAHEAD(1) Comma())?
2166 {
2167 return exprList;
2168 }
2169}
2170
2171void Comma():
2172{}
2173{
2174 <COMMA>
2175}
2176
2177RecordConstructor RecordConstructor() throws ParseException:
2178{
2179 RecordConstructor expr = new RecordConstructor();
2180 FieldBinding tmp = null;
2181 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2182}
2183{
2184 <LEFTBRACE> (tmp = FieldBinding()
2185 {
2186 fbList.add(tmp);
2187 }
2188 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2189 {
2190 expr.setFbList(fbList);
2191 return expr;
2192 }
2193}
2194
2195FieldBinding FieldBinding() throws ParseException:
2196{
2197 FieldBinding fb = new FieldBinding();
2198 Expression left, right;
2199}
2200{
2201 left = Expression() <COLON> right = Expression()
2202 {
2203 fb.setLeftExpr(left);
2204 fb.setRightExpr(right);
2205 return fb;
2206 }
2207}
2208
2209
2210Expression FunctionCallExpr() throws ParseException:
2211{
2212 CallExpr callExpr;
2213 List<Expression> argList = new ArrayList<Expression>();
2214 Expression tmp;
2215 int arity = 0;
2216 FunctionName funcName = null;
2217 String hint = null;
2218}
2219{
2220 funcName = FunctionName()
2221 {
2222 hint = funcName.hint;
2223 }
2224 <LEFTPAREN> (tmp = Expression()
2225 {
2226 argList.add(tmp);
2227 arity ++;
2228 }
2229 (<COMMA> tmp = Expression()
2230 {
2231 argList.add(tmp);
2232 arity++;
2233 }
2234 )*)? <RIGHTPAREN>
2235 {
2236 // TODO use funcName.library
2237 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2238 FunctionSignature signature
2239 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2240 if (signature == null) {
2241 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2242 }
2243 callExpr = new CallExpr(signature,argList);
2244 if (hint != null) {
2245 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2246 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2247 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2248 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2249 }
2250 }
2251 return callExpr;
2252 }
2253}
2254
2255
2256Expression DatasetAccessExpression() throws ParseException:
2257{
2258 String funcName;
2259 String arg1 = null;
2260 String arg2 = null;
2261 Expression nameArg;
2262}
2263{
2264 <DATASET>
2265 {
2266 funcName = token.image;
2267 }
2268 ( ( arg1 = Identifier() ( <DOT> arg2 = Identifier() )? )
2269 {
2270 String name = arg2 == null ? arg1 : arg1 + "." + arg2;
2271 LiteralExpr ds = new LiteralExpr();
2272 ds.setValue( new StringLiteral(name) );
2273 nameArg = ds;
2274 if(arg2 != null){
2275 addDataverse(arg1.toString());
2276 addDataset(name);
2277 } else {
2278 addDataset(defaultDataverse + "." + name);
2279 }
2280 }
2281 | ( <LEFTPAREN> nameArg = Expression() <RIGHTPAREN> ) )
2282 {
2283 String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
2284 FunctionSignature signature = lookupFunctionSignature(dataverse, funcName, 1);
2285 if (signature == null) {
2286 signature = new FunctionSignature(dataverse, funcName, 1);
2287 }
2288 List<Expression> argList = new ArrayList<Expression>();
2289 argList.add(nameArg);
2290 return new CallExpr(signature, argList);
2291 }
2292}
2293
2294Expression ParenthesizedExpression() throws ParseException:
2295{
2296 Expression expr;
2297}
2298{
2299 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2300 {
2301 return expr;
2302 }
2303}
2304
2305Expression IfThenElse() throws ParseException:
2306{
2307 Expression condExpr;
2308 Expression thenExpr;
2309 Expression elseExpr;
2310 IfExpr ifExpr = new IfExpr();
2311}
2312{
2313 <IF> <LEFTPAREN> condExpr = Expression() <RIGHTPAREN> <THEN> thenExpr = Expression() <ELSE> elseExpr = Expression()
2314
2315 {
2316 ifExpr.setCondExpr(condExpr);
2317 ifExpr.setThenExpr(thenExpr);
2318 ifExpr.setElseExpr(elseExpr);
2319 return ifExpr;
2320 }
2321}
2322
Taewoo Kim92c0bac2017-02-11 16:38:44 -08002323// Note: if you modify this part: "tmp = LetClause() {clauseList.add(tmp);}", please also apply the necessary
2324// change to the AQLPlusExtension.jj file since it refers to this string part and may behave incorrectly if this
2325// part is modified. For more details, please refer to AQLPlusExtension.jj file.
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +03002326Expression FLWOGR() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002327{
2328 FLWOGRExpression flworg = new FLWOGRExpression();
2329 List<Clause> clauseList = new ArrayList<Clause>();
2330 Expression returnExpr;
2331 Clause tmp;
2332 createNewScope();
2333}
2334{
2335 (tmp = ForClause() {clauseList.add(tmp);} | tmp = LetClause() {clauseList.add(tmp);})
2336 (tmp = Clause() {clauseList.add(tmp);})* (<RETURN>|<SELECT>) returnExpr = Expression()
2337
2338 {
2339 flworg.setClauseList(clauseList);
2340 flworg.setReturnExpr(returnExpr);
2341 removeCurrentScope();
2342 return flworg;
2343 }
2344}
2345
2346Clause Clause()throws ParseException :
2347{
2348 Clause clause;
2349}
2350{
2351 (
2352 clause = ForClause()
2353 | clause = LetClause()
2354 | clause = WhereClause()
2355 | clause = OrderbyClause()
2356 | clause = GroupClause()
2357 | clause = LimitClause()
2358 | clause = DistinctClause()
2359 )
2360 {
2361 return clause;
2362 }
2363}
2364
2365Clause ForClause()throws ParseException :
2366{
2367 ForClause fc = new ForClause();
2368 VariableExpr varExp;
2369 VariableExpr varPos = null;
2370 Expression inExp;
2371 extendCurrentScope();
2372}
2373{
2374 (<FOR>|<FROM>) varExp = Variable() (<AT> varPos = Variable())? <IN> ( inExp = Expression() )
2375 {
2376 fc.setVarExpr(varExp);
2377 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2378 fc.setInExpr(inExp);
2379 if (varPos != null) {
2380 fc.setPosExpr(varPos);
2381 getCurrentScope().addNewVarSymbolToScope(varPos.getVar());
2382 }
2383 return fc;
2384 }
2385}
2386
2387Clause LetClause() throws ParseException:
2388{
2389 LetClause lc = new LetClause();
2390 VariableExpr varExp;
2391 Expression beExp;
2392 extendCurrentScope();
2393}
2394{
2395 (<LET>|<WITH>) varExp = Variable() <ASSIGN> beExp = Expression()
2396 {
2397 getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
2398 lc.setVarExpr(varExp);
2399 lc.setBindingExpr(beExp);
2400 return lc;
2401 }
2402}
2403
2404Clause WhereClause()throws ParseException :
2405{
2406 WhereClause wc = new WhereClause();
2407 Expression whereExpr;
2408}
2409{
2410 <WHERE> whereExpr = Expression()
2411 {
2412 wc.setWhereExpr(whereExpr);
2413 return wc;
2414 }
2415}
2416
2417Clause OrderbyClause()throws ParseException :
2418{
2419 OrderbyClause oc = new OrderbyClause();
2420 Expression orderbyExpr;
2421 List<Expression> orderbyList = new ArrayList<Expression>();
2422 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2423 int numOfOrderby = 0;
2424}
2425{
2426 (
2427 <ORDER>
2428 {
2429 String hint = getHint(token);
2430 if (hint != null) {
2431 if (hint.startsWith(INMEMORY_HINT)) {
2432 String splits[] = hint.split(" +");
2433 int numFrames = Integer.parseInt(splits[1]);
2434 int numTuples = Integer.parseInt(splits[2]);
2435 oc.setNumFrames(numFrames);
2436 oc.setNumTuples(numTuples);
2437 }
2438 if (hint.startsWith(RANGE_HINT)) {
2439 try{
2440 oc.setRangeMap(RangeMapBuilder.parseHint(hint.substring(RANGE_HINT.length())));
Taewoo Kime65e6ca2017-01-14 17:53:28 -08002441 } catch (CompilationException e) {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002442 throw new ParseException(e.getMessage());
2443 }
2444 }
2445 }
2446 }
2447 <BY> orderbyExpr = Expression()
2448 {
2449 orderbyList.add(orderbyExpr);
2450 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2451 }
2452 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2453 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2454 {
2455 modifierList.add(modif);
2456 }
2457
2458 (<COMMA> orderbyExpr = Expression()
2459 {
2460 orderbyList.add(orderbyExpr);
2461 modif = OrderbyClause.OrderModifier.ASC;
2462 }
2463 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2464 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2465 {
2466 modifierList.add(modif);
2467 }
2468 )*
2469)
2470 {
2471 oc.setModifierList(modifierList);
2472 oc.setOrderbyList(orderbyList);
2473 return oc;
2474 }
2475}
2476Clause GroupClause()throws ParseException :
2477{
Abdullah Alamoudi806f7d22016-07-23 18:02:55 +03002478 GroupbyClause gbc = new GroupbyClause();
2479 // GbyVariableExpressionPair pair = new GbyVariableExpressionPair();
2480 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002481 List<GbyVariableExpressionPair> decorPairList = new ArrayList<GbyVariableExpressionPair>();
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002482 Map<Expression, VariableExpr> withVarMap = new HashMap<Expression, VariableExpr>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002483 VariableExpr var = null;
2484 VariableExpr withVar = null;
2485 Expression expr = null;
2486 VariableExpr decorVar = null;
2487 Expression decorExpr = null;
2488}
2489{
2490 {
2491 Scope newScope = extendCurrentScopeNoPush(true);
2492 // extendCurrentScope(true);
2493 }
2494 <GROUP>
2495 {
2496 String hint = getHint(token);
2497 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2498 gbc.setHashGroupByHint(true);
2499 }
2500 }
2501 <BY> (LOOKAHEAD(2) var = Variable()
2502 {
2503 newScope.addNewVarSymbolToScope(var.getVar());
2504 } <ASSIGN>)?
2505 expr = Expression()
2506 {
2507 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2508 vePairList.add(pair1);
2509 }
2510 (<COMMA> ( LOOKAHEAD(2) var = Variable()
2511 {
2512 newScope.addNewVarSymbolToScope(var.getVar());
2513 } <ASSIGN>)?
2514 expr = Expression()
2515 {
2516 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2517 vePairList.add(pair2);
2518 }
2519 )*
2520 (<DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2521 {
2522 newScope.addNewVarSymbolToScope(decorVar.getVar());
2523 GbyVariableExpressionPair pair3 = new GbyVariableExpressionPair(decorVar, decorExpr);
2524 decorPairList.add(pair3);
2525 }
2526 (<COMMA> <DECOR> decorVar = Variable() <ASSIGN> decorExpr = Expression()
2527 {
2528 newScope.addNewVarSymbolToScope(decorVar.getVar());
2529 GbyVariableExpressionPair pair4 = new GbyVariableExpressionPair(decorVar, decorExpr);
2530 decorPairList.add(pair4);
2531 }
2532 )*
2533 )?
2534 (<WITH>|<KEEPING>) withVar = VariableRef()
2535 {
2536 if(withVar.getIsNewVar()==true)
2537 throw new ParseException("can't find variable " + withVar.getVar());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002538 withVarMap.put(withVar, withVar);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002539 newScope.addNewVarSymbolToScope(withVar.getVar());
2540 }
2541 (<COMMA> withVar = VariableRef()
2542 {
2543 if(withVar.getIsNewVar()==true)
2544 throw new ParseException("can't find variable " + withVar.getVar());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002545 withVarMap.put(withVar, withVar);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002546 newScope.addNewVarSymbolToScope(withVar.getVar());
2547 })*
2548 {
2549 gbc.setGbyPairList(vePairList);
2550 gbc.setDecorPairList(decorPairList);
Yingyi Bu8671ddf2016-08-14 23:58:43 -07002551 gbc.setWithVarMap(withVarMap);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002552 replaceCurrentScope(newScope);
2553 return gbc;
2554 }
2555}
2556
2557
2558LimitClause LimitClause() throws ParseException:
2559{
2560 LimitClause lc = new LimitClause();
2561 Expression expr;
2562 pushForbiddenScope(getCurrentScope());
2563}
2564{
2565 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
2566 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
2567
2568 {
2569 popForbiddenScope();
2570 return lc;
2571 }
2572}
2573
2574DistinctClause DistinctClause() throws ParseException:
2575{
2576 List<Expression> exprs = new ArrayList<Expression>();
2577 Expression expr;
2578}
2579{
2580 <DISTINCT> <BY> expr = Expression()
2581 {
2582 exprs.add(expr);
2583 }
2584 (<COMMA> expr = Expression()
2585 {
2586 exprs.add(expr);
2587 }
2588 )*
2589 {
2590 return new DistinctClause(exprs);
2591 }
2592}
2593
2594QuantifiedExpression QuantifiedExpression()throws ParseException:
2595{
2596 QuantifiedExpression qc = new QuantifiedExpression();
2597 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
2598 Expression satisfiesExpr;
2599 VariableExpr var;
2600 Expression inExpr;
2601 QuantifiedPair pair;
2602}
2603{
2604 {
2605 createNewScope();
2606 }
2607
Michael Blowd6cf6412016-06-30 02:44:35 -04002608 ( (<SOME> { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
2609 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
Yingyi Bu391f09e2015-10-29 13:49:39 -07002610 var = Variable() <IN> inExpr = Expression()
2611 {
2612 pair = new QuantifiedPair(var, inExpr);
2613 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2614 quantifiedList.add(pair);
2615 }
2616 (
2617 <COMMA> var = Variable() <IN> inExpr = Expression()
2618 {
2619 pair = new QuantifiedPair(var, inExpr);
2620 getCurrentScope().addNewVarSymbolToScope(var.getVar());
2621 quantifiedList.add(pair);
2622 }
2623 )*
2624 <SATISFIES> satisfiesExpr = Expression()
2625 {
2626 qc.setSatisfiesExpr(satisfiesExpr);
2627 qc.setQuantifiedList(quantifiedList);
2628 removeCurrentScope();
2629 return qc;
2630 }
2631}
2632
2633TOKEN_MGR_DECLS:
2634{
2635 public int commentDepth = 0;
Till Westmanne9b2adf2016-10-15 12:39:01 -07002636 public ArrayDeque<Integer> lexerStateStack = new ArrayDeque<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002637
2638 public void pushState() {
2639 lexerStateStack.push( curLexState );
2640 }
2641
2642 public void popState(String token) {
2643 if (lexerStateStack.size() > 0) {
2644 SwitchTo( lexerStateStack.pop() );
2645 } else {
2646 int errorLine = input_stream.getEndLine();
2647 int errorColumn = input_stream.getEndColumn();
2648 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
2649 + "\" but state stack is empty.";
2650 throw new TokenMgrError(msg, -1);
2651 }
2652 }
2653}
2654
2655<DEFAULT,IN_DBL_BRACE>
2656TOKEN :
2657{
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002658 <APPLY : "apply">
2659 | <AS : "as">
2660 | <ASC : "asc">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002661 | <AT : "at">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002662 | <AUTOGENERATED : "autogenerated">
2663 | <BTREE : "btree">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002664 | <BY : "by">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002665 | <CLOSED : "closed">
2666 | <COMPACT : "compact">
2667 | <COMPACTION : "compaction">
2668 | <CONNECT : "connect">
2669 | <CREATE : "create">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002670 | <DATASET : "dataset">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002671 | <DATAVERSE : "dataverse">
2672 | <DECLARE : "declare">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002673 | <DECOR : "decor">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002674 | <DEFINITION : "definition">
2675 | <DELETE : "delete">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002676 | <DESC : "desc">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002677 | <DISCONNECT : "disconnect">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002678 | <DISTINCT : "distinct">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002679 | <DROP : "drop">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002680 | <ELSE : "else">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002681 | <ENFORCED : "enforced">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002682 | <EVERY : "every">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002683 | <EXISTS : "exists">
2684 | <EXTERNAL : "external">
2685 | <FEED : "feed">
2686 | <FILTER : "filter">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002687 | <FOR : "for">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002688 | <FORMAT : "format">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002689 | <FROM : "from">
Taewoo Kimc49405a2017-01-04 00:30:43 -08002690 | <FULLTEXT : "fulltext">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002691 | <FUNCTION : "function">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002692 | <GROUP : "group">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002693 | <HINTS : "hints">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002694 | <IF : "if">
2695 | <IN : "in">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002696 | <INDEX : "index">
2697 | <INGESTION : "ingestion">
2698 | <INSERT : "insert">
2699 | <INTERNAL : "internal">
2700 | <INTO : "into">
2701 | <KEY : "key">
2702 | <KEYWORD : "keyword">
2703 | <KEEPING : "keeping">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002704 | <LET : "let">
2705 | <LIMIT : "limit">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002706 | <LOAD : "load">
2707 | <NGRAM : "ngram">
2708 | <NODEGROUP : "nodegroup">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002709 | <OFFSET : "offset">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002710 | <ON : "on">
2711 | <OPEN : "open">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002712 | <ORDER : "order">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002713 | <OUTPUT : "output">
2714 | <PATH : "path">
2715 | <POLICY : "policy">
2716 | <PRESORTED : "pre-sorted">
2717 | <PRIMARY : "primary">
2718 | <REFRESH : "refresh">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002719 | <RETURN : "return">
Steven Glenn Jacobsafa909a2016-10-16 10:35:30 -07002720 | <RETURNING : "returning">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002721 | <RTREE : "rtree">
2722 | <RUN : "run">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002723 | <SATISFIES : "satisfies">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002724 | <SECONDARY : "secondary">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002725 | <SELECT : "select">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002726 | <SET : "set">
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08002727 | <START: "start">
2728 | <STOP: "stop">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002729 | <SOME : "some">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002730 | <TEMPORARY : "temporary">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002731 | <THEN : "then">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002732 | <TO : "to">
2733 | <TYPE : "type">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002734 | <UNION : "union">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002735 | <UPDATE : "update">
2736 | <UPSERT : "upsert">
2737 | <USE : "use">
2738 | <USING : "using">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002739 | <WHERE : "where">
2740 | <WITH : "with">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002741 | <WRITE : "write">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002742}
2743
2744<DEFAULT,IN_DBL_BRACE>
2745TOKEN :
2746{
2747 <CARET : "^">
2748 | <DIV : "/">
2749 | <IDIV : "idiv">
2750 | <MINUS : "-">
2751 | <MOD : "%">
2752 | <MUL : "*">
2753 | <PLUS : "+">
2754
2755 | <LEFTPAREN : "(">
2756 | <RIGHTPAREN : ")">
2757 | <LEFTBRACKET : "[">
2758 | <RIGHTBRACKET : "]">
2759
2760 | <COLON : ":">
2761 | <COMMA : ",">
2762 | <DOT : ".">
2763 | <QUES : "?">
2764
2765 | <LT : "<">
2766 | <GT : ">">
2767 | <LE : "<=">
2768 | <GE : ">=">
2769 | <EQ : "=">
2770 | <NE : "!=">
2771 | <SIMILAR : "~=">
2772 | <ASSIGN : ":=">
2773
2774 | <AND : "and">
2775 | <OR : "or">
Abdullah Alamoudie6e54f32016-07-12 20:40:15 +04002776
2777 | <SYMBOLAT : "@">
2778 | <SYMBOLHASH : "#">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002779}
2780
2781<DEFAULT,IN_DBL_BRACE>
2782TOKEN :
2783{
2784 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
2785}
2786
2787<DEFAULT>
2788TOKEN :
2789{
2790 <RIGHTBRACE : "}"> { popState("}"); }
2791}
2792
2793<DEFAULT,IN_DBL_BRACE>
2794TOKEN :
2795{
2796 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
2797}
2798
2799<IN_DBL_BRACE>
2800TOKEN :
2801{
2802 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
2803}
2804
2805<DEFAULT,IN_DBL_BRACE>
2806TOKEN :
2807{
2808 <INTEGER_LITERAL : (<DIGIT>)+ >
2809}
2810
2811<DEFAULT,IN_DBL_BRACE>
2812TOKEN :
2813{
Yingyi Bu535d86b2016-05-23 16:44:25 -07002814 < MISSING : "missing">
2815 | <NULL : "null">
Yingyi Bu391f09e2015-10-29 13:49:39 -07002816 | <TRUE : "true">
2817 | <FALSE : "false">
2818}
2819
2820<DEFAULT,IN_DBL_BRACE>
2821TOKEN :
2822{
2823 <#DIGIT : ["0" - "9"]>
2824}
2825
2826<DEFAULT,IN_DBL_BRACE>
2827TOKEN:
2828{
2829 < DOUBLE_LITERAL: <DIGITS>
2830 | <DIGITS> ( "." <DIGITS> )?
2831 | "." <DIGITS>
2832 >
2833 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
2834 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
2835 | "." <DIGITS> ( "f" | "F" )
2836 >
2837 | <DIGITS : (<DIGIT>)+ >
2838}
2839
2840<DEFAULT,IN_DBL_BRACE>
2841TOKEN :
2842{
2843 <#LETTER : ["A" - "Z", "a" - "z"]>
2844 | <SPECIALCHARS : ["$", "_", "-"]>
2845}
2846
2847<DEFAULT,IN_DBL_BRACE>
2848TOKEN :
2849{
2850 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
2851 <STRING_LITERAL : ("\"" (
2852 <EscapeQuot>
2853 | <EscapeBslash>
2854 | <EscapeSlash>
2855 | <EscapeBspace>
2856 | <EscapeFormf>
2857 | <EscapeNl>
2858 | <EscapeCr>
2859 | <EscapeTab>
2860 | ~["\"","\\"])* "\"")
2861 | ("\'"(
2862 <EscapeApos>
2863 | <EscapeBslash>
2864 | <EscapeSlash>
2865 | <EscapeBspace>
2866 | <EscapeFormf>
2867 | <EscapeNl>
2868 | <EscapeCr>
2869 | <EscapeTab>
2870 | ~["\'","\\"])* "\'")>
2871 | < #EscapeQuot: "\\\"" >
2872 | < #EscapeApos: "\\\'" >
2873 | < #EscapeBslash: "\\\\" >
2874 | < #EscapeSlash: "\\/" >
2875 | < #EscapeBspace: "\\b" >
2876 | < #EscapeFormf: "\\f" >
2877 | < #EscapeNl: "\\n" >
2878 | < #EscapeCr: "\\r" >
2879 | < #EscapeTab: "\\t" >
2880}
2881
2882<DEFAULT,IN_DBL_BRACE>
2883TOKEN :
2884{
2885 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
2886}
2887
2888<DEFAULT,IN_DBL_BRACE>
2889TOKEN :
2890{
2891 <VARIABLE : "$" <LETTER> (<LETTER> | <DIGIT> | "_")*>
2892}
2893
2894<DEFAULT,IN_DBL_BRACE>
2895SKIP:
2896{
2897 " "
2898 | "\t"
2899 | "\r"
2900 | "\n"
2901}
2902
2903<DEFAULT,IN_DBL_BRACE>
2904SKIP:
2905{
2906 <"//" (~["\n"])* "\n">
2907}
2908
2909<DEFAULT,IN_DBL_BRACE>
2910SKIP:
2911{
2912 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
2913}
2914
2915<DEFAULT,IN_DBL_BRACE>
2916SKIP:
2917{
2918 <"/*"> { pushState(); } : INSIDE_COMMENT
2919}
2920
2921<INSIDE_COMMENT>
2922SPECIAL_TOKEN:
2923{
2924 <"+"(" ")*(~["*"])*>
2925}
2926
2927<INSIDE_COMMENT>
2928SKIP:
2929{
2930 <"/*"> { pushState(); }
2931}
2932
2933<INSIDE_COMMENT>
2934SKIP:
2935{
2936 <"*/"> { popState("*/"); }
2937 | <~[]>
2938}