blob: c3a9c0bff53a38eff742417da3ccf6b36ac1b7ce [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(SQLPPParser)
28
29package org.apache.asterix.lang.sqlpp.parser;
30
31// For SQL++ ParserTokenManager
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;
Yingyi Budaa549c2016-06-28 22:30:52 -070043import java.util.Collections;
Yingyi Bu391f09e2015-10-29 13:49:39 -070044import java.util.HashMap;
45import java.util.Iterator;
46import java.util.LinkedHashMap;
47import java.util.List;
48import java.util.Map;
49
50import org.apache.asterix.common.annotations.AutoDataGen;
51import org.apache.asterix.common.annotations.DateBetweenYearsDataGen;
52import org.apache.asterix.common.annotations.DatetimeAddRandHoursDataGen;
53import org.apache.asterix.common.annotations.DatetimeBetweenYearsDataGen;
54import org.apache.asterix.common.annotations.FieldIntervalDataGen;
55import org.apache.asterix.common.annotations.FieldValFileDataGen;
56import org.apache.asterix.common.annotations.FieldValFileSameIndexDataGen;
57import org.apache.asterix.common.annotations.IRecordFieldDataGen;
58import org.apache.asterix.common.annotations.InsertRandIntDataGen;
59import org.apache.asterix.common.annotations.ListDataGen;
60import org.apache.asterix.common.annotations.ListValFileDataGen;
61import org.apache.asterix.common.annotations.SkipSecondaryIndexSearchExpressionAnnotation;
62import org.apache.asterix.common.annotations.TypeDataGen;
63import org.apache.asterix.common.annotations.UndeclaredFieldsDataGen;
64import org.apache.asterix.common.config.DatasetConfig.DatasetType;
65import org.apache.asterix.common.config.DatasetConfig.IndexType;
Taewoo Kime65e6ca2017-01-14 17:53:28 -080066import org.apache.asterix.common.exceptions.CompilationException;
Dmitry Lychagin85142c02018-04-05 17:27:36 -070067import org.apache.asterix.common.exceptions.ErrorCode;
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -080068import org.apache.asterix.common.functions.FunctionConstants;
Yingyi Bu391f09e2015-10-29 13:49:39 -070069import org.apache.asterix.common.functions.FunctionSignature;
70import org.apache.asterix.lang.common.base.Expression;
71import org.apache.asterix.lang.common.base.Literal;
72import org.apache.asterix.lang.common.base.IParser;
73import org.apache.asterix.lang.common.base.Statement;
74import org.apache.asterix.lang.common.clause.GroupbyClause;
75import org.apache.asterix.lang.common.clause.LetClause;
76import org.apache.asterix.lang.common.clause.LimitClause;
77import org.apache.asterix.lang.common.clause.OrderbyClause;
78import org.apache.asterix.lang.common.clause.UpdateClause;
79import org.apache.asterix.lang.common.clause.WhereClause;
80import org.apache.asterix.lang.common.context.RootScopeFactory;
81import org.apache.asterix.lang.common.context.Scope;
82import org.apache.asterix.lang.common.expression.AbstractAccessor;
83import org.apache.asterix.lang.common.expression.CallExpr;
84import org.apache.asterix.lang.common.expression.FieldAccessor;
85import org.apache.asterix.lang.common.expression.FieldBinding;
86import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
87import org.apache.asterix.lang.common.expression.IfExpr;
88import org.apache.asterix.lang.common.expression.IndexAccessor;
Dmitry Lychagin8ba59442017-06-16 14:19:45 -070089import org.apache.asterix.lang.common.expression.IndexedTypeExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -070090import org.apache.asterix.lang.common.expression.ListConstructor;
91import org.apache.asterix.lang.common.expression.LiteralExpr;
92import org.apache.asterix.lang.common.expression.OperatorExpr;
93import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
94import org.apache.asterix.lang.common.expression.QuantifiedExpression;
95import org.apache.asterix.lang.common.expression.RecordConstructor;
96import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
97import org.apache.asterix.lang.common.expression.TypeExpression;
98import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
99import org.apache.asterix.lang.common.expression.UnaryExpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700100import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
101import org.apache.asterix.lang.common.expression.VariableExpr;
102import org.apache.asterix.lang.common.literal.DoubleLiteral;
103import org.apache.asterix.lang.common.literal.FalseLiteral;
104import org.apache.asterix.lang.common.literal.FloatLiteral;
105import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
Yingyi Bu535d86b2016-05-23 16:44:25 -0700106import org.apache.asterix.lang.common.literal.MissingLiteral;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700107import org.apache.asterix.lang.common.literal.NullLiteral;
108import org.apache.asterix.lang.common.literal.StringLiteral;
109import org.apache.asterix.lang.common.literal.TrueLiteral;
110import org.apache.asterix.lang.common.parser.ScopeChecker;
111import org.apache.asterix.lang.common.statement.CompactStatement;
112import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800113import org.apache.asterix.lang.common.statement.StartFeedStatement;
114import org.apache.asterix.lang.common.statement.StopFeedStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700115import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
116import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
117import org.apache.asterix.lang.common.statement.CreateFeedStatement;
118import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
119import org.apache.asterix.lang.common.statement.CreateIndexStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700120import org.apache.asterix.lang.common.statement.DatasetDecl;
121import org.apache.asterix.lang.common.statement.DataverseDecl;
122import org.apache.asterix.lang.common.statement.DataverseDropStatement;
123import org.apache.asterix.lang.common.statement.DeleteStatement;
124import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
Yingyi Buab817482016-08-19 21:29:31 -0700125import org.apache.asterix.lang.common.statement.DropDatasetStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700126import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
127import org.apache.asterix.lang.common.statement.FeedDropStatement;
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300128import org.apache.asterix.lang.common.statement.FeedPolicyDropStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700129import org.apache.asterix.lang.common.statement.FunctionDecl;
130import org.apache.asterix.lang.common.statement.FunctionDropStatement;
131import org.apache.asterix.lang.common.statement.IndexDropStatement;
132import org.apache.asterix.lang.common.statement.InsertStatement;
133import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
134import org.apache.asterix.lang.common.statement.LoadStatement;
135import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
136import org.apache.asterix.lang.common.statement.NodegroupDecl;
137import org.apache.asterix.lang.common.statement.Query;
138import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700139import org.apache.asterix.lang.common.statement.SetStatement;
140import org.apache.asterix.lang.common.statement.TypeDecl;
141import org.apache.asterix.lang.common.statement.TypeDropStatement;
142import org.apache.asterix.lang.common.statement.UpdateStatement;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800143import org.apache.asterix.lang.common.statement.UpsertStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700144import org.apache.asterix.lang.common.statement.WriteStatement;
145import org.apache.asterix.lang.common.struct.Identifier;
Dmitry Lychaginef1719e2017-12-15 08:33:07 -0800146import org.apache.asterix.lang.common.struct.OperatorType;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700147import org.apache.asterix.lang.common.struct.QuantifiedPair;
148import org.apache.asterix.lang.common.struct.VarIdentifier;
149import org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause;
150import org.apache.asterix.lang.sqlpp.clause.FromClause;
151import org.apache.asterix.lang.sqlpp.clause.FromTerm;
152import org.apache.asterix.lang.sqlpp.clause.HavingClause;
153import org.apache.asterix.lang.sqlpp.clause.JoinClause;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700154import org.apache.asterix.lang.sqlpp.clause.Projection;
155import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
156import org.apache.asterix.lang.sqlpp.clause.SelectClause;
157import org.apache.asterix.lang.sqlpp.clause.SelectElement;
158import org.apache.asterix.lang.sqlpp.clause.SelectRegular;
159import org.apache.asterix.lang.sqlpp.clause.SelectSetOperation;
160import org.apache.asterix.lang.sqlpp.clause.UnnestClause;
Xikui Wangf6741682018-02-22 19:17:17 -0800161import org.apache.asterix.lang.common.clause.WhereClause;
Yingyi Buc8c067c2016-07-25 23:37:19 -0700162import org.apache.asterix.lang.sqlpp.expression.CaseExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700163import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
164import org.apache.asterix.lang.sqlpp.optype.JoinType;
165import org.apache.asterix.lang.sqlpp.optype.SetOpType;
166import org.apache.asterix.lang.sqlpp.struct.SetOperationInput;
167import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
Yingyi Bu9e3f9be2016-07-01 10:07:37 -0700168import org.apache.asterix.lang.sqlpp.util.ExpressionToVariableUtil;
Xikui Wang96fd4022017-04-10 14:23:31 -0700169import org.apache.asterix.lang.sqlpp.util.FunctionMapUtil;
Yingyi Buacc12a92016-03-26 17:25:05 -0700170import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -0800171import org.apache.asterix.om.functions.BuiltinFunctions;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700172import org.apache.hyracks.algebricks.common.utils.Pair;
173import org.apache.hyracks.algebricks.common.utils.Triple;
Xikui Wang5a61b2a2018-01-02 14:14:03 -0800174import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700175import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
176import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
177import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
178
Yingyi Bucaea8f02015-11-16 15:12:15 -0800179class SQLPPParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700180
181 // optimizer hints
182 private static final String AUTO_HINT = "auto";
183 private static final String BROADCAST_JOIN_HINT = "bcast";
184 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
185 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
186 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
187 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
188 private static final String HASH_GROUP_BY_HINT = "hash";
189 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
190 private static final String INMEMORY_HINT = "inmem";
191 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
192 private static final String INTERVAL_HINT = "interval";
193 private static final String LIST_HINT = "list";
194 private static final String LIST_VAL_FILE_HINT = "list-val-file";
195 private static final String RANGE_HINT = "range";
196 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
197 private static final String VAL_FILE_HINT = "val-files";
198 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
199
200 private static final String GEN_FIELDS_HINT = "gen-fields";
201
202 // data generator hints
203 private static final String DGEN_HINT = "dgen";
204
Till Westmann7199a562016-09-17 16:07:32 -0700205 // error configuration
206 protected static final boolean REPORT_EXPECTED_TOKENS = false;
207
Yingyi Bu391f09e2015-10-29 13:49:39 -0700208 private static class IndexParams {
209 public IndexType type;
210 public int gramLength;
211
212 public IndexParams(IndexType type, int gramLength) {
213 this.type = type;
214 this.gramLength = gramLength;
215 }
216 };
217
218 private static class FunctionName {
219 public String dataverse = null;
220 public String library = null;
221 public String function = null;
222 public String hint = null;
223 }
224
225 private static String getHint(Token t) {
226 if (t.specialToken == null) {
227 return null;
228 }
229 String s = t.specialToken.image;
230 int n = s.length();
231 if (n < 2) {
232 return null;
233 }
234 return s.substring(1).trim();
235 }
236
237 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
238 IRecordFieldDataGen rfdg = null;
239 String splits[] = hint.split(" +");
240 if (splits[0].equals(VAL_FILE_HINT)) {
241 File[] valFiles = new File[splits.length - 1];
242 for (int k=1; k<splits.length; k++) {
243 valFiles[k-1] = new File(splits[k]);
244 }
245 rfdg = new FieldValFileDataGen(valFiles);
246 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
247 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
248 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
249 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
250 } else if (splits[0].equals(LIST_HINT)) {
251 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
252 } else if (splits[0].equals(INTERVAL_HINT)) {
253 FieldIntervalDataGen.ValueType vt;
254 if (splits[1].equals("int")) {
255 vt = FieldIntervalDataGen.ValueType.INT;
256 } else if (splits[1].equals("long")) {
257 vt = FieldIntervalDataGen.ValueType.LONG;
258 } else if (splits[1].equals("float")) {
259 vt = FieldIntervalDataGen.ValueType.FLOAT;
260 } else if (splits[1].equals("double")) {
261 vt = FieldIntervalDataGen.ValueType.DOUBLE;
262 } else {
263 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
264 }
265 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
266 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
267 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
268 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
269 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
270 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
271 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
272 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
273 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
274 } else if (splits[0].equals(AUTO_HINT)) {
275 rfdg = new AutoDataGen(splits[1]);
276 }
277 return rfdg;
278 }
279
Till Westmann7199a562016-09-17 16:07:32 -0700280 public SQLPPParser(String s) {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700281 this(new StringReader(s));
282 super.setInput(s);
283 }
284
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800285 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700286 File file = new File(args[0]);
287 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
288 SQLPPParser parser = new SQLPPParser(fis);
289 List<Statement> st = parser.parse();
290 //st.accept(new SQLPPPrintVisitor(), 0);
291 }
292
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800293 public List<Statement> parse() throws CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700294 try {
295 return Statement();
296 } catch (Error e) {
297 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
298 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
Till Westmann60f89982017-08-11 18:14:20 -0700299 final String msg = e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : "");
Dmitry Lychagin85142c02018-04-05 17:27:36 -0700300 throw new CompilationException(ErrorCode.PARSE_ERROR, msg);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700301 } catch (ParseException e) {
Dmitry Lychagin85142c02018-04-05 17:27:36 -0700302 throw new CompilationException(ErrorCode.PARSE_ERROR, getMessage(e));
Yingyi Bu391f09e2015-10-29 13:49:39 -0700303 }
304 }
Till Westmann7199a562016-09-17 16:07:32 -0700305
306 protected String getMessage(ParseException pe) {
307 Token currentToken = pe.currentToken;
308 if (currentToken == null) {
309 return pe.getMessage();
310 }
311 int[][] expectedTokenSequences = pe.expectedTokenSequences;
312 String[] tokenImage = pe.tokenImage;
313 String sep = REPORT_EXPECTED_TOKENS ? eol : " ";
314 StringBuilder expected = REPORT_EXPECTED_TOKENS ? new StringBuilder() : null;
315 int maxSize = appendExpected(expected, expectedTokenSequences, tokenImage);
316 Token tok = currentToken.next;
317 int line = tok.beginLine;
318 String message = "In line " + line + " >>" + getLine(line) + "<<" + sep + "Encountered ";
319 for (int i = 0; i < maxSize; i++) {
320 if (i != 0) {
321 message += " ";
322 }
323 if (tok.kind == 0) {
324 message += fixQuotes(tokenImage[0]);
325 break;
326 }
Till Westmanne3c9f272016-10-09 11:09:26 -0700327 final String fixedTokenImage = tokenImage[tok.kind];
328 if (! tok.image.equalsIgnoreCase(stripQuotes(fixedTokenImage))) {
329 message += fixQuotes(fixedTokenImage) + " ";
330 }
Till Westmann7199a562016-09-17 16:07:32 -0700331 message += quot + addEscapes(tok.image) + quot;
332 tok = tok.next;
333 }
334 message += " at column " + currentToken.next.beginColumn + "." + sep;
335 if (REPORT_EXPECTED_TOKENS) {
336 if (expectedTokenSequences.length == 1) {
337 message += "Was expecting:" + sep + " ";
338 } else {
339 message += "Was expecting one of:" + sep + " ";
340 }
341 message += expected.toString();
342 }
343 return message;
344 }
345
Yingyi Bu391f09e2015-10-29 13:49:39 -0700346}
347
348PARSER_END(SQLPPParser)
349
350
351List<Statement> Statement() throws ParseException:
352{
353 scopeStack.push(RootScopeFactory.createRootScope(this));
354 List<Statement> decls = new ArrayList<Statement>();
355 Statement stmt = null;
356}
357{
Murtadha Hubaildc775222017-08-21 15:22:45 +0300358 (
359 (stmt = SingleStatement()
360 {
361 decls.add(stmt);
362 }
363 )?
364 (<SEMICOLON>)+
Yingyi Bu391f09e2015-10-29 13:49:39 -0700365 )*
366 <EOF>
367 {
368 return decls;
369 }
370}
371
372Statement SingleStatement() throws ParseException:
373{
374 Statement stmt = null;
375}
376{
377 (
378 stmt = DataverseDeclaration()
379 | stmt = FunctionDeclaration()
380 | stmt = CreateStatement()
381 | stmt = LoadStatement()
382 | stmt = DropStatement()
383 | stmt = WriteStatement()
384 | stmt = SetStatement()
385 | stmt = InsertStatement()
386 | stmt = DeleteStatement()
387 | stmt = UpdateStatement()
Yingyi Bucb5bf332017-01-02 22:19:50 -0800388 | stmt = UpsertStatement()
Yingyi Buab817482016-08-19 21:29:31 -0700389 | stmt = ConnectionStatement()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700390 | stmt = CompactStatement()
Till Westmannef3f0272016-07-27 18:34:01 -0700391 | stmt = ExplainStatement()
Murtadha Hubaildc775222017-08-21 15:22:45 +0300392 | stmt = Query(false)
Yingyi Bu391f09e2015-10-29 13:49:39 -0700393 | stmt = RefreshExternalDatasetStatement()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700394 )
395 {
396 return stmt;
397 }
398}
399
400DataverseDecl DataverseDeclaration() throws ParseException:
401{
402 String dvName = null;
403}
404{
405 <USE> dvName = Identifier()
406 {
407 defaultDataverse = dvName;
408 return new DataverseDecl(new Identifier(dvName));
409 }
410}
411
412Statement CreateStatement() throws ParseException:
413{
414 String hint = null;
415 boolean dgen = false;
416 Statement stmt = null;
417}
418{
419 <CREATE>
420 (
421 {
422 hint = getHint(token);
423 if (hint != null && hint.startsWith(DGEN_HINT)) {
424 dgen = true;
425 }
426 }
427 stmt = TypeSpecification(hint, dgen)
428 | stmt = NodegroupSpecification()
429 | stmt = DatasetSpecification()
430 | stmt = IndexSpecification()
431 | stmt = DataverseSpecification()
432 | stmt = FunctionSpecification()
433 | stmt = FeedSpecification()
434 | stmt = FeedPolicySpecification()
435 )
436 {
437 return stmt;
438 }
439}
440
441TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
442{
443 Pair<Identifier,Identifier> nameComponents = null;
444 boolean ifNotExists = false;
445 TypeExpression typeExpr = null;
446}
447{
448 <TYPE> nameComponents = TypeName() ifNotExists = IfNotExists()
Till Westmannf6028272016-09-30 14:34:42 -0700449 <AS> typeExpr = RecordTypeDef()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700450 {
451 long numValues = -1;
452 String filename = null;
453 if (dgen) {
454 String splits[] = hint.split(" +");
455 if (splits.length != 3) {
456 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
457 }
458 filename = splits[1];
459 numValues = Long.parseLong(splits[2]);
460 }
461 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
462 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
463 }
464}
465
466
467NodegroupDecl NodegroupSpecification() throws ParseException:
468{
469 String name = null;
470 String tmp = null;
471 boolean ifNotExists = false;
472 List<Identifier>ncNames = null;
473}
474{
475 <NODEGROUP> name = Identifier()
476 ifNotExists = IfNotExists() <ON> tmp = Identifier()
477 {
478 ncNames = new ArrayList<Identifier>();
479 ncNames.add(new Identifier(tmp));
480 }
481 ( <COMMA> tmp = Identifier()
482 {
483 ncNames.add(new Identifier(tmp));
484 }
485 )*
486 {
487 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
488 }
489}
490
491DatasetDecl DatasetSpecification() throws ParseException:
492{
493 Pair<Identifier,Identifier> nameComponents = null;
494 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800495 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700496 String adapterName = null;
497 Map<String,String> properties = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700498 FunctionSignature appliedFunction = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800499 Pair<List<Integer>, List<List<String>>> primaryKeyFields = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700500 String nodeGroupName = null;
501 Map<String,String> hints = new HashMap<String,String>();
502 DatasetDecl dsetDecl = null;
503 boolean autogenerated = false;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800504 Pair<Integer, List<String>> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800505 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Till Westmannf3aa19f2017-12-01 17:42:35 -0800506 RecordConstructor withRecord = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700507}
508{
509 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700510 <EXTERNAL> Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800511 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700512 ifNotExists = IfNotExists()
513 <USING> adapterName = AdapterName() properties = Configuration()
Till Westmannf3aa19f2017-12-01 17:42:35 -0800514 ( <ON> nodeGroupName = Identifier() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700515 ( <HINTS> hints = Properties() )?
Till Westmannf3aa19f2017-12-01 17:42:35 -0800516 ( <WITH> withRecord = RecordConstructor() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700517 {
518 ExternalDetailsDecl edd = new ExternalDetailsDecl();
519 edd.setAdapter(adapterName);
520 edd.setProperties(properties);
Till Westmannf3aa19f2017-12-01 17:42:35 -0800521 try{
Yingyi Bu391f09e2015-10-29 13:49:39 -0700522 dsetDecl = new DatasetDecl(nameComponents.first,
523 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800524 typeComponents.first,
525 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800526 metaTypeComponents.first,
527 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700528 nodeGroupName != null? new Identifier(nodeGroupName): null,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700529 hints,
530 DatasetType.EXTERNAL,
531 edd,
Till Westmannf3aa19f2017-12-01 17:42:35 -0800532 withRecord,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700533 ifNotExists);
Till Westmannf3aa19f2017-12-01 17:42:35 -0800534 } catch (CompilationException e){
535 throw new ParseException(e.getMessage());
536 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700537 }
538
Murtadha Hubail2c04ae02017-11-21 15:58:01 +0300539 | ( <INTERNAL> )?
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700540 Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800541 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800542 (
543 { String name; }
544 <WITH>
545 name = Identifier()
546 {
547 if(!name.toLowerCase().equals("meta")){
548 throw new ParseException("We can only support one additional associated field called \"meta\".");
549 }
550 }
551 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
552 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700553 ifNotExists = IfNotExists()
554 primaryKeyFields = PrimaryKey()
555 (<AUTOGENERATED> { autogenerated = true; } )?
556 (<ON> nodeGroupName = Identifier() )?
557 ( <HINTS> hints = Properties() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700558 ( LOOKAHEAD(2) <WITH> <FILTER> <ON> filterField = NestedField() )?
Till Westmannf3aa19f2017-12-01 17:42:35 -0800559 ( <WITH> withRecord = RecordConstructor() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700560 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800561 if(filterField!=null && filterField.first!=0){
562 throw new ParseException("A filter field can only be a field in the main record of the dataset.");
563 }
564 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields.second,
565 primaryKeyFields.first,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700566 autogenerated,
Murtadha Hubail2c04ae02017-11-21 15:58:01 +0300567 filterField == null? null : filterField.second);
Till Westmannf3aa19f2017-12-01 17:42:35 -0800568 try{
Yingyi Bu391f09e2015-10-29 13:49:39 -0700569 dsetDecl = new DatasetDecl(nameComponents.first,
570 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800571 typeComponents.first,
572 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800573 metaTypeComponents.first,
574 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700575 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700576 hints,
577 DatasetType.INTERNAL,
578 idd,
Till Westmannf3aa19f2017-12-01 17:42:35 -0800579 withRecord,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700580 ifNotExists);
Till Westmannf3aa19f2017-12-01 17:42:35 -0800581 } catch (CompilationException e){
582 throw new ParseException(e.getMessage());
583 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700584 }
585 )
586 {
587 return dsetDecl;
588 }
589}
590
591RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
592{
593 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
594 Pair<Identifier,Identifier> nameComponents = null;
595 String datasetName = null;
596}
597{
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700598 <REFRESH> <EXTERNAL> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700599 {
600 redss.setDataverseName(nameComponents.first);
601 redss.setDatasetName(nameComponents.second);
602 return redss;
603 }
604}
605
Yingyi Bu391f09e2015-10-29 13:49:39 -0700606CreateIndexStatement IndexSpecification() throws ParseException:
607{
608 CreateIndexStatement cis = new CreateIndexStatement();
609 String indexName = null;
610 boolean ifNotExists = false;
611 Pair<Identifier,Identifier> nameComponents = null;
Dmitry Lychagin8ba59442017-06-16 14:19:45 -0700612 Pair<Integer, Pair<List<String>, IndexedTypeExpression>> fieldPair = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700613 IndexParams indexType = null;
614 boolean enforced = false;
Ali Alsuliman8351d252017-09-24 00:43:15 -0700615 boolean isPrimaryIdx = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700616}
617{
Ali Alsuliman8351d252017-09-24 00:43:15 -0700618 (
619 (<INDEX> indexName = Identifier()
620 ifNotExists = IfNotExists()
621 <ON> nameComponents = QualifiedName()
622 <LEFTPAREN> ( fieldPair = OpenField()
623 {
624 cis.addFieldExprPair(fieldPair.second);
625 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700626 }
Ali Alsuliman8351d252017-09-24 00:43:15 -0700627 ) (<COMMA> fieldPair = OpenField()
628 {
629 cis.addFieldExprPair(fieldPair.second);
630 cis.addFieldIndexIndicator(fieldPair.first);
631 }
632 )* <RIGHTPAREN> ( <TYPE> indexType = IndexType() )? ( <ENFORCED> { enforced = true; } )?)
633 |
634 (<PRIMARY> <INDEX> {isPrimaryIdx = true;}
635 (
636 (indexName = Identifier())? ifNotExists = IfNotExists()
637 )
638 <ON> nameComponents = QualifiedName() (<TYPE> <BTREE>)?
639 )
640 )
641 {
642 if (isPrimaryIdx && indexName == null) {
643 indexName = "primary_idx_" + nameComponents.second;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700644 }
Ali Alsuliman8351d252017-09-24 00:43:15 -0700645 cis.setIndexName(new Identifier(indexName));
646 cis.setIfNotExists(ifNotExists);
647 cis.setDataverseName(nameComponents.first);
648 cis.setDatasetName(nameComponents.second);
649 if (indexType != null) {
650 cis.setIndexType(indexType.type);
651 cis.setGramLength(indexType.gramLength);
652 }
653 cis.setEnforced(enforced);
654 return cis;
655 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700656}
657
658String CompactionPolicy() throws ParseException :
659{
660 String compactionPolicy = null;
661}
662{
663 compactionPolicy = Identifier()
664 {
665 return compactionPolicy;
666 }
667}
668
669String FilterField() throws ParseException :
670{
671 String filterField = null;
672}
673{
674 filterField = Identifier()
675 {
676 return filterField;
677 }
678}
679
680IndexParams IndexType() throws ParseException:
681{
682 IndexType type = null;
683 int gramLength = 0;
684}
685{
686 (<BTREE>
687 {
688 type = IndexType.BTREE;
689 }
690 | <RTREE>
691 {
692 type = IndexType.RTREE;
693 }
694 | <KEYWORD>
695 {
696 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
697 }
Taewoo Kimc49405a2017-01-04 00:30:43 -0800698 |<FULLTEXT>
699 {
700 type = IndexType.SINGLE_PARTITION_WORD_INVIX;
701 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700702 | <NGRAM> <LEFTPAREN> <INTEGER_LITERAL>
703 {
704 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
705 gramLength = Integer.valueOf(token.image);
706 }
707 <RIGHTPAREN>)
708 {
709 return new IndexParams(type, gramLength);
710 }
711}
712
713CreateDataverseStatement DataverseSpecification() throws ParseException :
714{
715 String dvName = null;
716 boolean ifNotExists = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700717}
718{
719 <DATAVERSE> dvName = Identifier()
720 ifNotExists = IfNotExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700721 {
Till Westmannf6028272016-09-30 14:34:42 -0700722 return new CreateDataverseStatement(new Identifier(dvName), null, ifNotExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700723 }
724}
725
726CreateFunctionStatement FunctionSpecification() throws ParseException:
727{
728 FunctionSignature signature;
729 boolean ifNotExists = false;
730 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
731 String functionBody;
732 VarIdentifier var = null;
733 Expression functionBodyExpr;
734 Token beginPos;
735 Token endPos;
736 FunctionName fctName = null;
Steven Glenn Jacobs665e9fe2017-12-27 10:30:39 -0800737 String currentDataverse = defaultDataverse;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700738
739 createNewScope();
740}
741{
742 <FUNCTION> fctName = FunctionName()
Steven Glenn Jacobs665e9fe2017-12-27 10:30:39 -0800743 {
744 defaultDataverse = fctName.dataverse;
745 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700746 ifNotExists = IfNotExists()
747 paramList = ParameterList()
748 <LEFTBRACE>
749 {
750 beginPos = token;
751 }
Xikui Wang3de700a2018-03-15 16:32:55 -0700752 (functionBodyExpr = SelectExpression(true) | functionBodyExpr = Expression())
753 <RIGHTBRACE>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700754 {
755 endPos = token;
756 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
757 // TODO use fctName.library
758 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
759 getCurrentScope().addFunctionDescriptor(signature, false);
760 removeCurrentScope();
Steven Glenn Jacobs665e9fe2017-12-27 10:30:39 -0800761 defaultDataverse = currentDataverse;
762 return new CreateFunctionStatement(signature, paramList, functionBody, functionBodyExpr, ifNotExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700763 }
764}
765
766CreateFeedStatement FeedSpecification() throws ParseException:
767{
768 Pair<Identifier,Identifier> nameComponents = null;
769 boolean ifNotExists = false;
770 String adapterName = null;
771 Map<String,String> properties = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700772 CreateFeedStatement cfs = null;
773 Pair<Identifier,Identifier> sourceNameComponents = null;
Xikui Wang5a61b2a2018-01-02 14:14:03 -0800774 RecordConstructor withRecord = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700775}
776{
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800777 <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
Xikui Wang5a61b2a2018-01-02 14:14:03 -0800778 <WITH> withRecord = RecordConstructor()
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800779 {
Xikui Wang5a61b2a2018-01-02 14:14:03 -0800780 try {
781 cfs = new CreateFeedStatement(nameComponents, withRecord, ifNotExists);
782 return cfs;
783 } catch (AlgebricksException e) {
784 throw new ParseException(e.getMessage());
785 }
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800786 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700787}
788
789CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
790{
Michael Blowd6cf6412016-06-30 02:44:35 -0400791 String policyName = null;
792 String basePolicyName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700793 String sourcePolicyFile = null;
794 String definition = null;
795 boolean ifNotExists = false;
796 Map<String,String> properties = null;
797 CreateFeedPolicyStatement cfps = null;
798}
799{
800 (
801 <INGESTION> <POLICY> policyName = Identifier() ifNotExists = IfNotExists()
Yingyi Bu6d57e492016-06-06 21:24:42 -0700802 <FROM>
803 (<POLICY> basePolicyName = Identifier() properties = Configuration() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700804 {
805 cfps = new CreateFeedPolicyStatement(policyName,
806 basePolicyName, properties, definition, ifNotExists);
807 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300808 | <PATH> sourcePolicyFile = ConstantString() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700809 {
810 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
811 }
Yingyi Bu6d57e492016-06-06 21:24:42 -0700812 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700813 )
814 {
815 return cfps;
816 }
817}
818
819
820
821List<VarIdentifier> ParameterList() throws ParseException:
822{
823 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
824 VarIdentifier var = null;
825}
826{
827 <LEFTPAREN> (<IDENTIFIER>
828 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700829 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700830 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700831 }
832 (<COMMA> <IDENTIFIER>
833 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700834 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700835 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700836 }
837 )*)? <RIGHTPAREN>
838 {
839 return paramList;
840 }
841}
842
843boolean IfNotExists() throws ParseException:
844{
845}
846{
Yingyi Budaa549c2016-06-28 22:30:52 -0700847 ( LOOKAHEAD(1) <IF> <NOT> <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700848 {
849 return true;
850 }
851 )?
852 {
853 return false;
854 }
855}
856
Xikui Wang9d63f622017-05-18 17:50:44 -0700857void ApplyFunction(List<FunctionSignature> funcSigs) throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700858{
859 FunctionName functioName = null;
Xikui Wang261dc6d2017-03-29 21:23:15 -0700860 String fqFunctionName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700861}
862{
863 <APPLY> <FUNCTION> functioName = FunctionName()
864 {
Xikui Wang261dc6d2017-03-29 21:23:15 -0700865 fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
866 funcSigs.add(new FunctionSignature(functioName.dataverse, fqFunctionName, 1));
867 }
868 (
869 <COMMA> functioName = FunctionName()
870 {
871 fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
872 funcSigs.add(new FunctionSignature(functioName.dataverse, fqFunctionName, 1));
873 }
874 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -0700875}
876
877String GetPolicy() throws ParseException:
878{
879 String policy = null;
880}
881{
882 <USING> <POLICY> policy = Identifier()
883 {
884 return policy;
885 }
886
887}
888
889FunctionSignature FunctionSignature() throws ParseException:
890{
891 FunctionName fctName = null;
892 int arity = 0;
893}
894{
895 fctName = FunctionName() <ATT> <INTEGER_LITERAL>
896 {
897 arity = new Integer(token.image);
898 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
899 throw new ParseException(" invalid arity:" + arity);
900 }
901
902 // TODO use fctName.library
903 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
904 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
905 }
906}
907
Yingyi Buc9bfe252016-03-01 00:02:40 -0800908Pair<List<Integer>, List<List<String>>> PrimaryKey() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700909{
Yingyi Buc9bfe252016-03-01 00:02:40 -0800910 Pair<Integer, List<String>> tmp = null;
911 List<Integer> keyFieldSourceIndicators = new ArrayList<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -0700912 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
913}
914{
915 <PRIMARY> <KEY> tmp = NestedField()
916 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800917 keyFieldSourceIndicators.add(tmp.first);
918 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700919 }
920 ( <COMMA> tmp = NestedField()
921 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800922 keyFieldSourceIndicators.add(tmp.first);
923 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700924 }
925 )*
926 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800927 return new Pair<List<Integer>, List<List<String>>> (keyFieldSourceIndicators, primaryKeyFields);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700928 }
929}
930
931Statement DropStatement() throws ParseException:
932{
933 String id = null;
934 Pair<Identifier,Identifier> pairId = null;
935 Triple<Identifier,Identifier,Identifier> tripleId = null;
936 FunctionSignature funcSig = null;
937 boolean ifExists = false;
938 Statement stmt = null;
939}
940{
941 <DROP>
942 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700943 Dataset() pairId = QualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700944 {
Yingyi Buab817482016-08-19 21:29:31 -0700945 stmt = new DropDatasetStatement(pairId.first, pairId.second, ifExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700946 }
947 | <INDEX> tripleId = DoubleQualifiedName() ifExists = IfExists()
948 {
949 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
950 }
951 | <NODEGROUP> id = Identifier() ifExists = IfExists()
952 {
953 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
954 }
955 | <TYPE> pairId = TypeName() ifExists = IfExists()
956 {
957 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
958 }
959 | <DATAVERSE> id = Identifier() ifExists = IfExists()
960 {
961 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
962 }
963 | <FUNCTION> funcSig = FunctionSignature() ifExists = IfExists()
964 {
965 stmt = new FunctionDropStatement(funcSig, ifExists);
966 }
967 | <FEED> pairId = QualifiedName() ifExists = IfExists()
968 {
969 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
970 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300971 | <INGESTION> <POLICY> pairId = QualifiedName() ifExists = IfExists()
972 {
973 stmt = new FeedPolicyDropStatement(pairId.first, pairId.second, ifExists);
974 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700975 )
976 {
977 return stmt;
978 }
979}
980
981boolean IfExists() throws ParseException :
982{
983}
984{
985 ( LOOKAHEAD(1) <IF> <EXISTS>
986 {
987 return true;
988 }
989 )?
990 {
991 return false;
992 }
993}
994
995InsertStatement InsertStatement() throws ParseException:
996{
997 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800998 VariableExpr var = null;
999 Query query = null;
1000 Expression returnExpression = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001001}
1002{
Yingyi Bucb5bf332017-01-02 22:19:50 -08001003 <INSERT> <INTO> nameComponents = QualifiedName() (<AS> var = Variable())?
1004 query = Query(false)
1005 ( <RETURNING> returnExpression = Expression())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001006 {
Yingyi Bucb5bf332017-01-02 22:19:50 -08001007 if (returnExpression != null && var == null) {
1008 var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
1009 }
Yingyi Bucaea8f02015-11-16 15:12:15 -08001010 query.setTopLevel(true);
Yingyi Bucb5bf332017-01-02 22:19:50 -08001011 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
1012 returnExpression);
1013 }
1014}
1015
1016UpsertStatement UpsertStatement() throws ParseException:
1017{
1018 Pair<Identifier,Identifier> nameComponents = null;
1019 VariableExpr var = null;
1020 Query query = null;
1021 Expression returnExpression = null;
1022}
1023{
1024 <UPSERT> <INTO> nameComponents = QualifiedName() (<AS> var = Variable())?
1025 query = Query(false)
1026 ( <RETURNING> returnExpression = Expression())?
1027 {
1028 if (returnExpression != null && var == null) {
1029 var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
1030 }
1031 query.setTopLevel(true);
1032 return new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
1033 returnExpression);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001034 }
1035}
1036
1037DeleteStatement DeleteStatement() throws ParseException:
1038{
Yingyi Bu20e085b2016-07-06 12:57:27 -07001039 VariableExpr varExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001040 Expression condition = null;
1041 Pair<Identifier, Identifier> nameComponents;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001042}
1043{
Yingyi Bu20e085b2016-07-06 12:57:27 -07001044 <DELETE>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001045 <FROM> nameComponents = QualifiedName()
Yingyi Bu20e085b2016-07-06 12:57:27 -07001046 ((<AS>)? varExpr = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001047 (<WHERE> condition = Expression())?
Yingyi Bu20e085b2016-07-06 12:57:27 -07001048 {
Yingyi Bu20e085b2016-07-06 12:57:27 -07001049 if(varExpr == null){
1050 varExpr = new VariableExpr();
1051 VarIdentifier var = SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue());
1052 varExpr.setVar(var);
1053 }
1054 return new DeleteStatement(varExpr, nameComponents.first, nameComponents.second,
Abdullah Alamoudi6eb01752017-04-01 21:51:19 -07001055 condition, getVarCounter());
Yingyi Bu20e085b2016-07-06 12:57:27 -07001056 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001057}
1058
1059UpdateStatement UpdateStatement() throws ParseException:
1060{
1061 VariableExpr vars;
1062 Expression target;
1063 Expression condition;
1064 UpdateClause uc;
1065 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
1066}
1067{
1068 <UPDATE> vars = Variable() <IN> target = Expression()
1069 <WHERE> condition = Expression()
1070 <LEFTPAREN> (uc = UpdateClause()
1071 {
1072 ucs.add(uc);
1073 }
1074 (<COMMA> uc = UpdateClause()
1075 {
1076 ucs.add(uc);
1077 }
1078 )*) <RIGHTPAREN>
1079 {
1080 return new UpdateStatement(vars, target, condition, ucs);
1081 }
1082}
1083
1084UpdateClause UpdateClause() throws ParseException:
1085{
1086 Expression target = null;
1087 Expression value = null ;
1088 InsertStatement is = null;
1089 DeleteStatement ds = null;
1090 UpdateStatement us = null;
1091 Expression condition = null;
1092 UpdateClause ifbranch = null;
1093 UpdateClause elsebranch = null;
1094}
1095{
1096 (<SET> target = Expression() <EQ> value = Expression()
1097 | is = InsertStatement()
1098 | ds = DeleteStatement()
1099 | us = UpdateStatement()
1100 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
1101 <THEN> ifbranch = UpdateClause()
1102 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
1103 {
1104 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
1105 }
1106 )
1107}
1108
1109Statement SetStatement() throws ParseException:
1110{
1111 String pn = null;
1112 String pv = null;
1113}
1114{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001115 <SET> pn = Identifier() pv = ConstantString()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001116 {
1117 return new SetStatement(pn, pv);
1118 }
1119}
1120
1121Statement WriteStatement() throws ParseException:
1122{
1123 String nodeName = null;
1124 String fileName = null;
1125 Query query;
1126 String writerClass = null;
1127 Pair<Identifier,Identifier> nameComponents = null;
1128}
1129{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001130 <WRITE> <OUTPUT> <TO> nodeName = Identifier() <COLON> fileName = ConstantString()
1131 ( <USING> writerClass = ConstantString() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001132 {
1133 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
1134 }
1135}
1136
1137LoadStatement LoadStatement() throws ParseException:
1138{
1139 Identifier dataverseName = null;
1140 Identifier datasetName = null;
1141 boolean alreadySorted = false;
1142 String adapterName;
1143 Map<String,String> properties;
1144 Pair<Identifier,Identifier> nameComponents = null;
1145}
1146{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001147 <LOAD> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001148 {
1149 dataverseName = nameComponents.first;
1150 datasetName = nameComponents.second;
1151 }
1152 <USING> adapterName = AdapterName() properties = Configuration()
1153 (<PRESORTED>
1154 {
1155 alreadySorted = true;
1156 }
1157 )?
1158 {
1159 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1160 }
1161}
1162
1163
1164String AdapterName() throws ParseException :
1165{
1166 String adapterName = null;
1167}
1168{
1169 adapterName = Identifier()
1170 {
1171 return adapterName;
1172 }
1173}
1174
1175Statement CompactStatement() throws ParseException:
1176{
1177 Pair<Identifier,Identifier> nameComponents = null;
1178 Statement stmt = null;
1179}
1180{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001181 <COMPACT> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001182 {
1183 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1184 }
1185 {
1186 return stmt;
1187 }
1188}
1189
Yingyi Buab817482016-08-19 21:29:31 -07001190Statement ConnectionStatement() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001191{
1192 Pair<Identifier,Identifier> feedNameComponents = null;
1193 Pair<Identifier,Identifier> datasetNameComponents = null;
1194
1195 Map<String,String> configuration = null;
1196 Statement stmt = null;
1197 String policy = null;
1198}
1199{
1200 (
Yingyi Buab817482016-08-19 21:29:31 -07001201 <CONNECT> stmt = ConnectStatement()
1202 | <DISCONNECT> stmt = DisconnectStatement()
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001203 | <START> stmt = StartStatement()
1204 | <STOP> stmt = StopStatement()
Yingyi Buab817482016-08-19 21:29:31 -07001205 )
1206 {
1207 return stmt;
1208 }
1209}
1210
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001211Statement StartStatement() throws ParseException:
1212{
1213 Pair<Identifier,Identifier> feedNameComponents = null;
1214
1215 Statement stmt = null;
1216}
1217{
1218 <FEED> feedNameComponents = QualifiedName()
1219 {
1220 stmt = new StartFeedStatement (feedNameComponents);
1221 return stmt;
1222 }
1223}
1224
1225Statement StopStatement () throws ParseException:
1226{
1227 Pair<Identifier,Identifier> feedNameComponents = null;
1228
1229 Statement stmt = null;
1230}
1231{
1232 <FEED> feedNameComponents = QualifiedName()
1233 {
1234 stmt = new StopFeedStatement (feedNameComponents);
1235 return stmt;
1236 }
1237}
1238
1239
Yingyi Buab817482016-08-19 21:29:31 -07001240Statement DisconnectStatement() throws ParseException:
1241{
1242 Pair<Identifier,Identifier> feedNameComponents = null;
1243 Pair<Identifier,Identifier> datasetNameComponents = null;
1244
1245 Map<String,String> configuration = null;
1246 Statement stmt = null;
1247 String policy = null;
1248}
1249{
1250 (
1251 <FEED> feedNameComponents = QualifiedName() <FROM> Dataset() datasetNameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001252 {
1253 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1254 }
1255 )
Yingyi Buab817482016-08-19 21:29:31 -07001256 {
1257 return stmt;
1258 }
1259}
1260
1261Statement ConnectStatement() throws ParseException:
1262{
1263 Pair<Identifier,Identifier> feedNameComponents = null;
1264 Pair<Identifier,Identifier> datasetNameComponents = null;
1265
1266 Map<String,String> configuration = null;
Xikui Wang9d63f622017-05-18 17:50:44 -07001267 List<FunctionSignature> appliedFunctions = new ArrayList<FunctionSignature>();
Yingyi Buab817482016-08-19 21:29:31 -07001268 Statement stmt = null;
1269 String policy = null;
Xikui Wangf6741682018-02-22 19:17:17 -08001270 String whereClauseBody = null;
1271 WhereClause whereClause = null;
1272 Token beginPos = null;
1273 Token endPos = null;
Yingyi Buab817482016-08-19 21:29:31 -07001274}
1275{
1276 (
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001277 <FEED> feedNameComponents = QualifiedName() <TO> Dataset() datasetNameComponents = QualifiedName()
Xikui Wangf6741682018-02-22 19:17:17 -08001278 (ApplyFunction(appliedFunctions))?
1279 (policy = GetPolicy())?
1280 (
1281 <WHERE>
1282 {
1283 beginPos = token;
1284 whereClause = new WhereClause();
1285 Expression whereExpr;
1286 }
1287 whereExpr = Expression()
1288 {
1289 whereClause.setWhereExpr(whereExpr);
1290 }
1291 )?
1292 {
1293 if (whereClause != null) {
1294 endPos = token;
1295 whereClauseBody = extractFragment(beginPos.endLine, beginPos.endColumn, endPos.endLine, endPos.endColumn + 1);
1296 }
1297 }
Yingyi Buab817482016-08-19 21:29:31 -07001298 {
Xikui Wang261dc6d2017-03-29 21:23:15 -07001299 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, appliedFunctions,
Xikui Wangf6741682018-02-22 19:17:17 -08001300 policy, whereClauseBody, getVarCounter());
Yingyi Buab817482016-08-19 21:29:31 -07001301 }
1302 )
1303 {
1304 return stmt;
1305 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001306}
1307
1308Map<String,String> Configuration() throws ParseException :
1309{
1310 Map<String,String> configuration = new LinkedHashMap<String,String>();
1311 Pair<String, String> keyValuePair = null;
1312}
1313{
1314 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1315 {
1316 configuration.put(keyValuePair.first, keyValuePair.second);
1317 }
1318 ( <COMMA> keyValuePair = KeyValuePair()
1319 {
1320 configuration.put(keyValuePair.first, keyValuePair.second);
1321 }
1322 )* )? <RIGHTPAREN>
1323 {
1324 return configuration;
1325 }
1326}
1327
1328Pair<String, String> KeyValuePair() throws ParseException:
1329{
1330 String key;
1331 String value;
1332}
1333{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001334 <LEFTPAREN> key = ConstantString() <EQ> value = ConstantString() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001335 {
1336 return new Pair<String, String>(key, value);
1337 }
1338}
1339
1340Map<String,String> Properties() throws ParseException:
1341{
1342 Map<String,String> properties = new HashMap<String,String>();
1343 Pair<String, String> property;
1344}
1345{
1346 (LOOKAHEAD(1) <LEFTPAREN> property = Property()
1347 {
1348 properties.put(property.first, property.second);
1349 }
1350 ( <COMMA> property = Property()
1351 {
1352 properties.put(property.first, property.second);
1353 }
1354 )* <RIGHTPAREN> )?
1355 {
1356 return properties;
1357 }
1358}
1359
1360Pair<String, String> Property() throws ParseException:
1361{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001362 String key = null;
1363 String value = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001364}
1365{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001366 (key = Identifier() | key = StringLiteral())
1367 <EQ>
1368 ( value = ConstantString() | <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001369 {
1370 try {
1371 value = "" + Long.valueOf(token.image);
1372 } catch (NumberFormatException nfe) {
1373 throw new ParseException("inapproriate value: " + token.image);
1374 }
1375 }
1376 )
1377 {
1378 return new Pair<String, String>(key.toUpperCase(), value);
1379 }
1380}
1381
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001382IndexedTypeExpression IndexedTypeExpr() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001383{
1384 TypeExpression typeExpr = null;
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001385 boolean isUnknownable = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001386}
1387{
1388 (
1389 typeExpr = TypeReference()
1390 | typeExpr = OrderedListTypeDef()
1391 | typeExpr = UnorderedListTypeDef()
1392 )
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001393 ( <QUES> { isUnknownable = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001394 {
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001395 return new IndexedTypeExpression(typeExpr, isUnknownable);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001396 }
1397}
1398
1399TypeExpression TypeExpr() throws ParseException:
1400{
1401 TypeExpression typeExpr = null;
1402}
1403{
1404 (
1405 typeExpr = RecordTypeDef()
1406 | typeExpr = TypeReference()
1407 | typeExpr = OrderedListTypeDef()
1408 | typeExpr = UnorderedListTypeDef()
1409 )
1410 {
1411 return typeExpr;
1412 }
1413}
1414
1415RecordTypeDefinition RecordTypeDef() throws ParseException:
1416{
1417 RecordTypeDefinition recType = new RecordTypeDefinition();
1418 RecordTypeDefinition.RecordKind recordKind = null;
1419}
1420{
1421 ( <CLOSED> { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1422 | <OPEN> { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1423 <LEFTBRACE>
1424 {
1425 String hint = getHint(token);
1426 if (hint != null) {
1427 String splits[] = hint.split(" +");
1428 if (splits[0].equals(GEN_FIELDS_HINT)) {
1429 if (splits.length != 5) {
1430 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1431 }
1432 if (!splits[1].equals("int")) {
1433 throw new ParseException("The only supported type for gen-fields is int.");
1434 }
1435 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1436 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1437 recType.setUndeclaredFieldsDataGen(ufdg);
1438 }
1439 }
1440
1441 }
1442 (
1443 RecordField(recType)
1444 ( <COMMA> RecordField(recType) )*
1445 )?
1446 <RIGHTBRACE>
1447 {
1448 if (recordKind == null) {
1449 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1450 }
1451 recType.setRecordKind(recordKind);
1452 return recType;
1453 }
1454}
1455
1456void RecordField(RecordTypeDefinition recType) throws ParseException:
1457{
1458 String fieldName;
1459 TypeExpression type = null;
1460 boolean nullable = false;
1461}
1462{
1463 fieldName = Identifier()
1464 {
1465 String hint = getHint(token);
1466 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1467 }
1468 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1469 {
1470 recType.addField(fieldName, type, nullable, rfdg);
1471 }
1472}
1473
1474TypeReferenceExpression TypeReference() throws ParseException:
1475{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001476 Pair<Identifier,Identifier> id = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001477}
1478{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001479 id = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001480 {
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001481 if (id.first == null && id.second.getValue().equalsIgnoreCase("int")) {
1482 id.second.setValue("int64");
Yingyi Bu391f09e2015-10-29 13:49:39 -07001483 }
1484
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001485 return new TypeReferenceExpression(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001486 }
1487}
1488
1489OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1490{
1491 TypeExpression type = null;
1492}
1493{
1494 <LEFTBRACKET>
1495 ( type = TypeExpr() )
1496 <RIGHTBRACKET>
1497 {
1498 return new OrderedListTypeDefinition(type);
1499 }
1500}
1501
1502
1503UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1504{
1505 TypeExpression type = null;
1506}
1507{
1508 <LEFTDBLBRACE>
1509 ( type = TypeExpr() )
1510 <RIGHTDBLBRACE>
1511 {
1512 return new UnorderedListTypeDefinition(type);
1513 }
1514}
1515
1516FunctionName FunctionName() throws ParseException:
1517{
1518 String first = null;
1519 String second = null;
1520 String third = null;
1521 boolean secondAfterDot = false;
1522}
1523{
Michael Blowd6cf6412016-06-30 02:44:35 -04001524 first = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001525 {
1526 FunctionName result = new FunctionName();
1527 result.hint = getHint(token);
Michael Blowd6cf6412016-06-30 02:44:35 -04001528 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001529 ( <DOT> second = Identifier()
1530 {
1531 secondAfterDot = true;
1532 }
1533 (<SHARP> third = Identifier())? | <SHARP> second = Identifier() )?
1534 {
1535 if (second == null) {
1536 result.dataverse = defaultDataverse;
1537 result.library = null;
1538 result.function = first;
1539 } else if (third == null) {
1540 if (secondAfterDot) {
1541 result.dataverse = first;
1542 result.library = null;
1543 result.function = second;
1544 } else {
1545 result.dataverse = defaultDataverse;
1546 result.library = first;
1547 result.function = second;
1548 }
1549 } else {
1550 result.dataverse = first;
1551 result.library = second;
1552 result.function = third;
1553 }
1554
1555 if (result.function.equalsIgnoreCase("int")) {
1556 result.function = "int64";
1557 }
1558 return result;
1559 }
1560}
1561
1562Pair<Identifier,Identifier> TypeName() throws ParseException:
1563{
1564 Pair<Identifier,Identifier> name = null;
1565}
1566{
1567 name = QualifiedName()
1568 {
1569 if (name.first == null) {
1570 name.first = new Identifier(defaultDataverse);
1571 }
1572 return name;
1573 }
1574}
1575
1576String Identifier() throws ParseException:
1577{
1578 String lit = null;
1579}
1580{
1581 (<IDENTIFIER>
1582 {
1583 return token.image;
1584 }
1585 | lit = QuotedString()
1586 {
1587 return lit;
1588 }
1589 )
1590}
1591
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001592void Dataset() throws ParseException:
1593{
1594}
1595{
1596 (<DATASET>|<COLLECTION>)
1597}
1598
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001599Pair<Integer, Pair<List<String>, IndexedTypeExpression>> OpenField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001600{
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001601 IndexedTypeExpression fieldType = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001602 Pair<Integer, List<String>> fieldList = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001603}
1604{
1605 fieldList = NestedField()
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001606 ( <COLON> fieldType = IndexedTypeExpr() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001607 {
Dmitry Lychagin8ba59442017-06-16 14:19:45 -07001608 return new Pair<Integer, Pair<List<String>, IndexedTypeExpression>>
1609 (fieldList.first, new Pair<List<String>, IndexedTypeExpression>(fieldList.second, fieldType));
Yingyi Bu391f09e2015-10-29 13:49:39 -07001610 }
1611}
1612
Yingyi Buc9bfe252016-03-01 00:02:40 -08001613Pair<Integer, List<String>> NestedField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001614{
1615 List<String> exprList = new ArrayList<String>();
1616 String lit = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001617 int source = 0;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001618}
1619{
1620 lit = Identifier()
1621 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001622 boolean meetParens = false;
1623 }
1624 (
Yingyi Buc9bfe252016-03-01 00:02:40 -08001625 LOOKAHEAD(1)
Yingyi Bub9169b62016-02-26 21:21:49 -08001626 <LEFTPAREN><RIGHTPAREN>
1627 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001628 if(!lit.toLowerCase().equals("meta")){
Yingyi Bub9169b62016-02-26 21:21:49 -08001629 throw new ParseException("The string before () has to be \"meta\".");
1630 }
1631 meetParens = true;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001632 source = 1;
Yingyi Bub9169b62016-02-26 21:21:49 -08001633 }
1634 )?
1635 {
1636 if(!meetParens){
1637 exprList.add(lit);
1638 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001639 }
1640 (<DOT>
1641 lit = Identifier()
1642 {
1643 exprList.add(lit);
1644 }
1645 )*
1646 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001647 return new Pair<Integer, List<String>>(source, exprList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001648 }
1649}
1650
Yingyi Bu6d57e492016-06-06 21:24:42 -07001651String ConstantString() throws ParseException:
1652{
1653 String value = null;
1654}
1655{
1656 (value = QuotedString() | value = StringLiteral())
1657 {
1658 return value;
1659 }
1660}
1661
Yingyi Bu391f09e2015-10-29 13:49:39 -07001662
1663String QuotedString() throws ParseException:
1664{
1665}
1666{
1667 <QUOTED_STRING>
1668 {
1669 return removeQuotesAndEscapes(token.image);
1670 }
1671}
1672
1673
1674String StringLiteral() throws ParseException:
1675{
1676}
1677{
1678 <STRING_LITERAL>
1679 {
1680 return removeQuotesAndEscapes(token.image);
1681 }
1682}
1683
1684Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1685{
1686 String first = null;
1687 String second = null;
1688}
1689{
1690 first = Identifier() (<DOT> second = Identifier())?
1691 {
1692 Identifier id1 = null;
1693 Identifier id2 = null;
1694 if (second == null) {
1695 id2 = new Identifier(first);
1696 } else
1697 {
1698 id1 = new Identifier(first);
1699 id2 = new Identifier(second);
1700 }
1701 return new Pair<Identifier,Identifier>(id1, id2);
1702 }
1703}
1704
1705Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1706{
1707 String first = null;
1708 String second = null;
1709 String third = null;
1710}
1711{
1712 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1713 {
1714 Identifier id1 = null;
1715 Identifier id2 = null;
1716 Identifier id3 = null;
1717 if (third == null) {
1718 id2 = new Identifier(first);
1719 id3 = new Identifier(second);
1720 } else {
1721 id1 = new Identifier(first);
1722 id2 = new Identifier(second);
1723 id3 = new Identifier(third);
1724 }
1725 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1726 }
1727}
1728
1729FunctionDecl FunctionDeclaration() throws ParseException:
1730{
1731 FunctionDecl funcDecl;
1732 FunctionSignature signature;
1733 String functionName;
1734 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1735 Expression funcBody;
1736 createNewScope();
1737}
1738{
Xikui Wang3de700a2018-03-15 16:32:55 -07001739 <DECLARE> <FUNCTION>
1740 functionName = Identifier()
1741 paramList = ParameterList()
1742 <LEFTBRACE>
1743 (funcBody = SelectExpression(true) | funcBody = Expression())
1744 <RIGHTBRACE>
1745 {
1746 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1747 getCurrentScope().addFunctionDescriptor(signature, false);
1748 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1749 removeCurrentScope();
1750 return funcDecl;
1751 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001752}
1753
Till Westmannef3f0272016-07-27 18:34:01 -07001754Query ExplainStatement() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001755{
Till Westmannef3f0272016-07-27 18:34:01 -07001756 Query query;
1757}
1758{
Till Westmann516d1a82016-08-02 14:45:53 -07001759 <EXPLAIN> query = Query(true)
Till Westmannef3f0272016-07-27 18:34:01 -07001760 {
1761 return query;
1762 }
1763}
1764
1765Query Query(boolean explain) throws ParseException:
1766{
1767 Query query = new Query(explain);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001768 Expression expr;
1769}
1770{
1771 (
1772 expr = Expression()
1773 |
1774 expr = SelectExpression(false)
1775 )
1776 {
1777 query.setBody(expr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001778 return query;
1779 }
1780}
1781
1782
1783
1784Expression Expression():
1785{
1786 Expression expr = null;
1787 Expression exprP = null;
1788}
1789{
1790(
1791 LOOKAHEAD(2)
1792 expr = OperatorExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001793 | expr = QuantifiedExpression()
1794)
1795 {
1796 return (exprP==null) ? expr : exprP;
1797 }
1798}
1799
1800
1801
1802Expression OperatorExpr()throws ParseException:
1803{
1804 OperatorExpr op = null;
1805 Expression operand = null;
1806}
1807{
1808 operand = AndExpr()
1809 (
1810
1811 <OR>
1812 {
1813 if (op == null) {
1814 op = new OperatorExpr();
1815 op.addOperand(operand);
Xikui Wang3de700a2018-03-15 16:32:55 -07001816 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001817 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001818 try{
1819 op.addOperator(token.image.toLowerCase());
1820 } catch (Exception e){
1821 throw new ParseException(e.getMessage());
1822 }
Xikui Wang3de700a2018-03-15 16:32:55 -07001823 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001824
Xikui Wang3de700a2018-03-15 16:32:55 -07001825 operand = AndExpr()
1826 {
1827 op.addOperand(operand);
1828 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001829
1830 )*
1831
1832 {
1833 return op==null? operand: op;
1834 }
1835}
1836
1837Expression AndExpr()throws ParseException:
1838{
1839 OperatorExpr op = null;
1840 Expression operand = null;
1841}
1842{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001843 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001844 (
1845
1846 <AND>
1847 {
1848 if (op == null) {
1849 op = new OperatorExpr();
1850 op.addOperand(operand);
Yingyi Bu196db5d2016-07-15 19:07:20 -07001851 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001852 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001853 try{
1854 op.addOperator(token.image.toLowerCase());
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001855 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001856 throw new ParseException(e.getMessage());
1857 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001858 }
1859
Yingyi Bu196db5d2016-07-15 19:07:20 -07001860 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001861 {
1862 op.addOperand(operand);
1863 }
1864
1865 )*
1866
1867 {
1868 return op==null? operand: op;
1869 }
1870}
1871
Yingyi Bu196db5d2016-07-15 19:07:20 -07001872Expression NotExpr()throws ParseException:
1873{
1874 Expression inputExpr;
1875 boolean not = false;
1876}
1877{
1878 (<NOT> { not = true; } )? inputExpr = RelExpr()
1879 {
1880 if(not){
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08001881 FunctionSignature signature = new FunctionSignature(BuiltinFunctions.NOT);
Yingyi Bu196db5d2016-07-15 19:07:20 -07001882 return new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(inputExpr)));
1883 } else {
1884 return inputExpr;
1885 }
1886 }
1887}
Yingyi Bu391f09e2015-10-29 13:49:39 -07001888
1889Expression RelExpr()throws ParseException:
1890{
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001891 boolean not = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001892 OperatorExpr op = null;
1893 Expression operand = null;
1894 boolean broadcast = false;
1895 IExpressionAnnotation annotation = null;
1896}
1897{
Yingyi Bu6c638342016-09-02 17:54:34 -07001898 operand = BetweenExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001899
1900 (
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001901 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> | <LG> |<SIMILAR> | (<NOT> { not = true; })? <IN>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001902 {
1903 String mhint = getHint(token);
1904 if (mhint != null) {
1905 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001906 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1907 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1908 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
Yingyi Buea4ec722016-11-04 01:26:16 -07001909 } else if (mhint.equals(BROADCAST_JOIN_HINT)) {
1910 broadcast = true;
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001911 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001912 }
Yingyi Buea4ec722016-11-04 01:26:16 -07001913
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001914 String operator = token.image.toLowerCase();
Yingyi Bu4a4b8962016-09-16 12:09:11 -07001915 if (operator.equals("<>")){
1916 operator = "!=";
1917 }
1918 if (not) {
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001919 operator = "not_" + operator;
1920 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001921 if (op == null) {
1922 op = new OperatorExpr();
Yingyi Buea4ec722016-11-04 01:26:16 -07001923 op.addOperand(operand, false); // broadcast is always for the right branch
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001924 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001925 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001926 try{
1927 op.addOperator(operator);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001928 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001929 throw new ParseException(e.getMessage());
1930 }
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001931 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001932
Yingyi Bu6c638342016-09-02 17:54:34 -07001933 operand = BetweenExpr()
Yingyi Buea4ec722016-11-04 01:26:16 -07001934 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07001935 op.addOperand(operand, broadcast);
Yingyi Buea4ec722016-11-04 01:26:16 -07001936 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001937 )?
1938
1939 {
1940 if (annotation != null) {
1941 op.addHint(annotation);
1942 }
1943 return op==null? operand: op;
1944 }
1945}
1946
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001947
Yingyi Bu6c638342016-09-02 17:54:34 -07001948Expression BetweenExpr()throws ParseException:
1949{
1950 boolean not = false;
1951 OperatorExpr op = null;
1952 Expression operand = null;
1953 IExpressionAnnotation annotation = null;
1954}
1955{
1956 operand = IsExpr()
1957 (
1958 LOOKAHEAD(2)
1959 (<NOT> { not = true; })? <BETWEEN>
1960 {
1961 String mhint = getHint(token);
1962 if (mhint != null) {
1963 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1964 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1965 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1966 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1967 }
1968 }
1969 String operator = token.image.toLowerCase();
1970 if(not){
1971 operator = "not_" + operator;
1972 }
1973 if (op == null) {
1974 op = new OperatorExpr();
1975 op.addOperand(operand);
1976 op.setCurrentop(true);
1977 }
1978 try{
1979 op.addOperator(operator);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001980 } catch (CompilationException e){
Yingyi Bu6c638342016-09-02 17:54:34 -07001981 throw new ParseException(e.getMessage());
1982 }
1983 }
1984
1985 operand = IsExpr()
1986 {
1987 op.addOperand(operand);
1988 }
1989
1990 <AND>
1991 operand = IsExpr()
1992 {
Dmitry Lychaginef1719e2017-12-15 08:33:07 -08001993 op.addOperator(OperatorType.AND);
Yingyi Bu6c638342016-09-02 17:54:34 -07001994 op.addOperand(operand);
1995 }
1996 )?
1997
1998 {
1999 if (annotation != null) {
2000 op.addHint(annotation);
2001 }
2002 return op==null? operand: op;
2003 }
2004}
2005
Yingyi Budaa549c2016-06-28 22:30:52 -07002006Expression IsExpr() throws ParseException:
2007{
2008 Expression expr = null;
2009 Expression operand = null;
2010 boolean not = false;
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08002011 FunctionIdentifier fn = null;
Yingyi Budaa549c2016-06-28 22:30:52 -07002012}
2013{
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08002014 operand = LikeExpr()
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08002015 ( <IS>
2016 (<NOT> { not = true; })?
2017 (
2018 <NULL> { fn = BuiltinFunctions.IS_NULL; } |
2019 <MISSING> { fn = BuiltinFunctions.IS_MISSING; } |
Dmitry Lychagin7d594a32018-01-15 14:31:03 -08002020 <UNKNOWN> { fn = BuiltinFunctions.IS_UNKNOWN; } |
Dmitry Lychagin1aa0dd42018-04-17 14:35:24 -07002021 (<KNOWN> | <VALUED>) { not = !not; fn = BuiltinFunctions.IS_UNKNOWN; }
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08002022 )
Yingyi Budaa549c2016-06-28 22:30:52 -07002023 {
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08002024 FunctionSignature signature = new FunctionSignature(fn);
Yingyi Budaa549c2016-06-28 22:30:52 -07002025 expr = new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(operand)));
2026 if(not) {
Dmitry Lychagin7c53fcf2017-11-07 12:07:41 -08002027 FunctionSignature notSignature = new FunctionSignature(BuiltinFunctions.NOT);
Yingyi Budaa549c2016-06-28 22:30:52 -07002028 expr = new CallExpr(notSignature, new ArrayList<Expression>(Collections.singletonList(expr)));
2029 }
2030 }
2031 )?
2032 {
2033 return expr = expr==null? operand : expr;
2034 }
2035}
2036
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08002037
2038Expression LikeExpr()throws ParseException:
2039{
2040 boolean not = false;
2041 OperatorExpr op = null;
2042 Expression operand = null;
2043}
2044{
2045 operand = ConcatExpr()
2046 (
2047 LOOKAHEAD(2)
2048 (<NOT> { not = true; })? <LIKE>
2049 {
2050 op = new OperatorExpr();
2051 op.addOperand(operand);
2052 op.setCurrentop(true);
2053
2054 String operator = token.image.toLowerCase();
2055 if (not) {
2056 operator = "not_" + operator;
2057 }
2058 try{
2059 op.addOperator(operator);
2060 } catch (CompilationException e){
2061 throw new ParseException(e.getMessage());
2062 }
2063 }
2064
2065 operand = ConcatExpr()
2066 {
2067 op.addOperand(operand);
2068 }
2069 )?
2070
2071 {
2072 return op == null ? operand : op;
2073 }
2074}
2075
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002076Expression ConcatExpr()throws ParseException:
2077{
2078 OperatorExpr op = null;
2079 Expression operand = null;
2080}
2081{
2082 operand = AddExpr()
2083 (
2084 LOOKAHEAD(1)
2085 (<CONCAT>)
2086 {
2087 if (op == null) {
2088 op = new OperatorExpr();
2089 op.addOperand(operand);
2090 op.setCurrentop(true);
2091 }
2092 try{
2093 ((OperatorExpr)op).addOperator(token.image);
2094 } catch (Exception e){
2095 throw new ParseException(e.getMessage());
2096 }
2097 }
2098 operand = AddExpr()
2099 {
2100 op.addOperand(operand);
2101 }
2102 )*
2103
2104 {
2105 return op==null? operand: op;
2106 }
2107}
Yingyi Budaa549c2016-06-28 22:30:52 -07002108
Yingyi Bu391f09e2015-10-29 13:49:39 -07002109Expression AddExpr()throws ParseException:
2110{
2111 OperatorExpr op = null;
2112 Expression operand = null;
2113}
2114{
2115 operand = MultExpr()
Yingyi Budaa549c2016-06-28 22:30:52 -07002116 (
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002117 LOOKAHEAD(1)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002118 (<PLUS> | <MINUS>)
2119 {
2120 if (op == null) {
2121 op = new OperatorExpr();
2122 op.addOperand(operand);
2123 op.setCurrentop(true);
2124 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07002125 try{
2126 ((OperatorExpr)op).addOperator(token.image);
2127 } catch (Exception e){
2128 throw new ParseException(e.getMessage());
2129 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002130 }
2131
2132 operand = MultExpr()
2133 {
2134 op.addOperand(operand);
2135 }
2136 )*
2137
2138 {
2139 return op==null? operand: op;
2140 }
2141}
2142
2143Expression MultExpr()throws ParseException:
2144{
2145 OperatorExpr op = null;
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07002146 OperatorType opType = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002147 Expression operand = null;
2148}
2149{
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002150 operand = ExponentExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002151
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07002152 ( (
2153 <MUL> { opType = OperatorType.MUL; } |
2154 <DIVIDE> { opType = OperatorType.DIVIDE; } |
2155 <DIV> { opType = OperatorType.DIV; } |
2156 ( <MOD> | <PERCENT> ) { opType = OperatorType.MOD; }
2157 )
Yingyi Bu391f09e2015-10-29 13:49:39 -07002158 {
2159 if (op == null) {
2160 op = new OperatorExpr();
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002161 op.addOperand(operand);
2162 op.setCurrentop(true);
2163 }
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07002164 op.addOperator(opType);
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002165 }
2166 operand = ExponentExpr()
2167 {
2168 op.addOperand(operand);
2169 }
2170 )*
2171
2172 {
2173 return op==null?operand:op;
2174 }
2175}
2176
2177Expression ExponentExpr()throws ParseException:
2178{
2179 OperatorExpr op = null;
2180 Expression operand = null;
2181}
2182{
2183 operand = UnaryExpr()
2184 (<CARET>
2185 {
2186 if (op == null) {
2187 op = new OperatorExpr();
2188 op.addOperand(operand);
2189 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002190 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07002191 try{
2192 op.addOperator(token.image);
2193 } catch (Exception e){
2194 throw new ParseException(e.getMessage());
2195 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002196 }
2197 operand = UnaryExpr()
2198 {
2199 op.addOperand(operand);
2200 }
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002201 )?
2202 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002203 return op==null?operand:op;
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002204 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002205}
2206
2207Expression UnaryExpr() throws ParseException:
2208{
Yingyi Bu196db5d2016-07-15 19:07:20 -07002209 boolean not = false;
2210 UnaryExpr uexpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002211 Expression expr = null;
2212}
Yingyi Budaa549c2016-06-28 22:30:52 -07002213{
Yingyi Bu196db5d2016-07-15 19:07:20 -07002214 ( (<PLUS> | <MINUS> | (<NOT> { not = true; } )? <EXISTS> )
Yingyi Bu391f09e2015-10-29 13:49:39 -07002215 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07002216 String exprType = token.image.toLowerCase();
2217 if(not){
2218 exprType = "not_" + exprType;
2219 }
2220 uexpr = new UnaryExpr();
2221 try{
2222 uexpr.setExprType(exprType);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08002223 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07002224 throw new ParseException(e.getMessage());
Yingyi Budaa549c2016-06-28 22:30:52 -07002225 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002226 }
2227 )?
2228
2229 expr = ValueExpr()
2230 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07002231 if(uexpr==null){
Yingyi Bu391f09e2015-10-29 13:49:39 -07002232 return expr;
Yingyi Bu196db5d2016-07-15 19:07:20 -07002233 }
2234 uexpr.setExpr(expr);
2235 return uexpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002236 }
2237}
2238
2239Expression ValueExpr()throws ParseException:
2240{
2241 Expression expr = null;
2242 Identifier ident = null;
2243 AbstractAccessor fa = null;
2244 Expression indexExpr = null;
2245}
2246{
2247 expr = PrimaryExpr() (
2248 ident = Field()
2249 {
2250 fa = (fa == null ? new FieldAccessor(expr, ident)
2251 : new FieldAccessor(fa, ident));
2252 }
2253 | indexExpr = Index()
2254 {
2255 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
2256 : new IndexAccessor(fa, indexExpr));
2257 }
2258 )*
2259 {
2260 return fa == null ? expr : fa;
2261 }
2262}
2263
2264Identifier Field() throws ParseException:
2265{
2266 String ident = null;
2267}
2268{
2269 <DOT> ident = Identifier()
2270 {
2271 return new Identifier(ident);
2272 }
2273}
2274
2275Expression Index() throws ParseException:
2276{
2277 Expression expr = null;
2278}
2279{
2280 <LEFTBRACKET> ( expr = Expression()
2281 {
2282 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
2283 {
2284 Literal lit = ((LiteralExpr)expr).getValue();
2285 if(lit.getLiteralType() != Literal.Type.INTEGER &&
2286 lit.getLiteralType() != Literal.Type.LONG) {
2287 throw new ParseException("Index should be an INTEGER");
2288 }
2289 }
2290 }
2291
2292 | <QUES> // ANY
2293
2294 )
2295
2296 <RIGHTBRACKET>
2297 {
2298 return expr;
2299 }
2300}
2301
2302
2303Expression PrimaryExpr()throws ParseException:
2304{
2305 Expression expr = null;
2306}
2307{
2308 ( LOOKAHEAD(4)
2309 expr = FunctionCallExpr()
Dmitry Lychagin5fbd04b2018-04-19 16:54:28 -07002310 | expr = CaseExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002311 | expr = Literal()
2312 | expr = VariableRef()
2313 | expr = ListConstructor()
2314 | expr = RecordConstructor()
2315 | expr = ParenthesizedExpression()
2316 )
2317 {
2318 return expr;
2319 }
2320}
2321
2322Expression Literal() throws ParseException:
2323{
2324 LiteralExpr lit = new LiteralExpr();
2325 String str = null;
2326}
2327{
2328 ( str = StringLiteral()
2329 {
2330 lit.setValue(new StringLiteral(str));
2331 }
2332 | <INTEGER_LITERAL>
2333 {
Till Westmann68c6a992016-09-30 00:19:12 -07002334 try {
2335 lit.setValue(new LongIntegerLiteral(Long.valueOf(token.image)));
2336 } catch (NumberFormatException e) {
2337 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2338 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002339 }
2340 | <FLOAT_LITERAL>
2341 {
Till Westmann68c6a992016-09-30 00:19:12 -07002342 try {
2343 lit.setValue(new FloatLiteral(Float.valueOf(token.image)));
2344 } catch (NumberFormatException e) {
2345 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2346 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002347 }
2348 | <DOUBLE_LITERAL>
2349 {
Till Westmann68c6a992016-09-30 00:19:12 -07002350 try {
2351 lit.setValue(new DoubleLiteral(Double.valueOf(token.image)));
2352 } catch (NumberFormatException e) {
2353 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2354 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002355 }
Yingyi Bu535d86b2016-05-23 16:44:25 -07002356 | <MISSING>
2357 {
2358 lit.setValue(MissingLiteral.INSTANCE);
2359 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002360 | <NULL>
2361 {
2362 lit.setValue(NullLiteral.INSTANCE);
2363 }
2364 | <TRUE>
2365 {
2366 lit.setValue(TrueLiteral.INSTANCE);
2367 }
2368 | <FALSE>
2369 {
2370 lit.setValue(FalseLiteral.INSTANCE);
2371 }
2372 )
2373 {
2374 return lit;
2375 }
2376}
2377
2378
2379VariableExpr VariableRef() throws ParseException:
2380{
2381 VariableExpr varExp = new VariableExpr();
2382 VarIdentifier var = new VarIdentifier();
2383}
2384{
2385 { String id = null; }
2386 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2387 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002388 id = SqlppVariableUtil.toInternalVariableName(id); // Prefix user-defined variables with "$"
Yingyi Bu391f09e2015-10-29 13:49:39 -07002389 Identifier ident = lookupSymbol(id);
2390 if (isInForbiddenScopes(id)) {
2391 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.");
2392 }
2393 if(ident != null) { // exist such ident
Yingyi Bu391f09e2015-10-29 13:49:39 -07002394 varExp.setVar((VarIdentifier)ident);
2395 } else {
2396 varExp.setVar(var);
Yingyi Buacc12a92016-03-26 17:25:05 -07002397 varExp.setIsNewVar(false);
2398 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002399 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002400 return varExp;
2401 }
2402}
2403
2404
2405VariableExpr Variable() throws ParseException:
2406{
2407 VariableExpr varExp = new VariableExpr();
2408 VarIdentifier var = new VarIdentifier();
2409}
2410{
2411 { String id = null; }
2412 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2413 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002414 id = SqlppVariableUtil.toInternalVariableName(id); // prefix user-defined variables with "$".
Yingyi Bu391f09e2015-10-29 13:49:39 -07002415 Identifier ident = lookupSymbol(id);
2416 if(ident != null) { // exist such ident
2417 varExp.setIsNewVar(false);
2418 }
2419 varExp.setVar(var);
Yingyi Bucaea8f02015-11-16 15:12:15 -08002420 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002421 return varExp;
2422 }
2423}
2424
2425Expression ListConstructor() throws ParseException:
2426{
2427 Expression expr = null;
2428}
2429{
2430 (
2431 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
2432 )
2433
2434 {
2435 return expr;
2436 }
2437}
2438
2439
2440ListConstructor OrderedListConstructor() throws ParseException:
2441{
2442 ListConstructor expr = new ListConstructor();
2443 List<Expression> exprList = null;
2444 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
2445}
2446{
2447 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
2448 {
2449 expr.setExprList(exprList);
2450 return expr;
2451 }
2452}
2453
2454ListConstructor UnorderedListConstructor() throws ParseException:
2455{
2456 ListConstructor expr = new ListConstructor();
2457 List<Expression> exprList = null;
2458 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
2459}
2460{
2461 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
2462 {
2463 expr.setExprList(exprList);
2464 return expr;
2465 }
2466}
2467
2468List<Expression> ExpressionList() throws ParseException:
2469{
2470 Expression expr = null;
2471 List<Expression> list = null;
2472 List<Expression> exprList = new ArrayList<Expression>();
2473}
2474{
2475 (
Till Westmann60f89982017-08-11 18:14:20 -07002476 expr = Expression()
2477 {
2478 exprList.add(expr);
2479 }
2480 ( <COMMA> expr = Expression()
2481 {
2482 exprList.add(expr);
2483 }
2484 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -07002485 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002486 {
Till Westmann60f89982017-08-11 18:14:20 -07002487 return exprList;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002488 }
2489}
2490
Yingyi Bu391f09e2015-10-29 13:49:39 -07002491RecordConstructor RecordConstructor() throws ParseException:
2492{
2493 RecordConstructor expr = new RecordConstructor();
2494 FieldBinding tmp = null;
2495 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2496}
2497{
2498 <LEFTBRACE> (tmp = FieldBinding()
2499 {
2500 fbList.add(tmp);
2501 }
2502 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2503 {
2504 expr.setFbList(fbList);
2505 return expr;
2506 }
2507}
2508
2509FieldBinding FieldBinding() throws ParseException:
2510{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002511 Expression left, right;
2512}
2513{
2514 left = Expression() <COLON> right = Expression()
2515 {
Dmitry Lychagin5cdaa5d2018-02-21 11:11:26 -08002516 return new FieldBinding(left, right);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002517 }
2518}
2519
2520
2521Expression FunctionCallExpr() throws ParseException:
2522{
2523 CallExpr callExpr;
2524 List<Expression> argList = new ArrayList<Expression>();
Yingyi Buf4d09842016-08-26 00:03:52 -07002525 Expression tmp = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002526 int arity = 0;
2527 FunctionName funcName = null;
2528 String hint = null;
Yingyi Buf4d09842016-08-26 00:03:52 -07002529 boolean star = false;
Dmitry Lychagin7a4b5682017-09-11 18:53:07 -07002530 boolean distinct = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002531}
2532{
2533 funcName = FunctionName()
2534 {
2535 hint = funcName.hint;
2536 }
Dmitry Lychagin7a4b5682017-09-11 18:53:07 -07002537 <LEFTPAREN> (
2538 ( <DISTINCT> { distinct = true; } )?
2539 ( tmp = Expression() | <MUL> { star = true; } )
Yingyi Bu391f09e2015-10-29 13:49:39 -07002540 {
Yingyi Buf4d09842016-08-26 00:03:52 -07002541 if(star){
2542 if(!funcName.function.toLowerCase().equals("count")){
2543 throw new ParseException("The parameter * can only be used in COUNT().");
2544 }
2545 argList.add(new LiteralExpr(new LongIntegerLiteral(1L)));
2546 } else {
2547 argList.add(tmp);
2548 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002549 arity ++;
2550 }
2551 (<COMMA> tmp = Expression()
2552 {
2553 argList.add(tmp);
2554 arity++;
2555 }
2556 )*)? <RIGHTPAREN>
2557 {
Dmitry Lychagin7a4b5682017-09-11 18:53:07 -07002558 String name = funcName.function;
2559 if (distinct) {
2560 name += "-distinct";
2561 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002562 // TODO use funcName.library
Dmitry Lychagin7a4b5682017-09-11 18:53:07 -07002563 String fqFunctionName = funcName.library == null ? name : funcName.library + "#" + name;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002564 FunctionSignature signature
2565 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2566 if (signature == null) {
2567 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2568 }
Xikui Wang96fd4022017-04-10 14:23:31 -07002569 callExpr = FunctionMapUtil.normalizedListInputFunctions(new CallExpr(signature,argList));
Yingyi Bu391f09e2015-10-29 13:49:39 -07002570 if (hint != null) {
2571 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2572 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2573 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2574 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2575 }
2576 }
2577 return callExpr;
2578 }
2579}
2580
2581Expression ParenthesizedExpression() throws ParseException:
2582{
2583 Expression expr;
2584}
2585{
2586 (
2587 LOOKAHEAD(2)
2588 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2589 |
2590 expr = Subquery()
2591 )
2592 {
2593 return expr;
2594 }
2595}
2596
Yingyi Buc8c067c2016-07-25 23:37:19 -07002597
2598Expression CaseExpr() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002599{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002600 Expression conditionExpr = new LiteralExpr(TrueLiteral.INSTANCE);
2601 List<Expression> whenExprs = new ArrayList<Expression>();
2602 List<Expression> thenExprs = new ArrayList<Expression>();
2603 Expression elseExpr = null;
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002604
Yingyi Buc8c067c2016-07-25 23:37:19 -07002605 Expression whenExpr = null;
2606 Expression thenExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002607}
2608{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002609 <CASE> ( conditionExpr = Expression() )?
2610 (
2611 <WHEN> whenExpr = Expression()
2612 {
2613 whenExprs.add(whenExpr);
2614 }
2615 <THEN> thenExpr = Expression()
2616 {
2617 thenExprs.add(thenExpr);
2618 }
2619 )*
2620 (<ELSE> elseExpr = Expression() )?
2621 <END>
2622 {
2623 return new CaseExpression(conditionExpr, whenExprs, thenExprs, elseExpr);
2624 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002625}
2626
Yingyi Buab817482016-08-19 21:29:31 -07002627SelectExpression SelectExpression(boolean subquery) throws ParseException:
2628{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002629 List<LetClause> letClauses = new ArrayList<LetClause>();
2630 SelectSetOperation selectSetOperation;
2631 OrderbyClause orderbyClause = null;
2632 LimitClause limitClause = null;
2633 createNewScope();
Yingyi Buab817482016-08-19 21:29:31 -07002634}
2635{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002636 ( letClauses = LetClause() )?
2637 selectSetOperation = SelectSetOperation()
2638 (orderbyClause = OrderbyClause() {})?
2639 (limitClause = LimitClause() {})?
2640 {
2641 return new SelectExpression(letClauses, selectSetOperation, orderbyClause, limitClause, subquery);
2642 }
2643}
2644
Yingyi Buab817482016-08-19 21:29:31 -07002645SelectSetOperation SelectSetOperation() throws ParseException:
2646{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002647 SetOperationInput setOperationInputLeft;
2648 List<SetOperationRight> setOperationRights = new ArrayList<SetOperationRight>();
2649}
2650{
2651 {
2652 SelectBlock selectBlockLeft = null;
2653 SelectExpression subqueryLeft = null;
2654 Expression expr = null;
2655 }
2656 selectBlockLeft = SelectBlock()
2657 {
2658 setOperationInputLeft = new SetOperationInput(selectBlockLeft, subqueryLeft);
2659 }
2660 (
2661 {
2662 SetOpType opType = SetOpType.UNION;
2663 boolean setSemantics = true;
2664 SelectBlock selectBlockRight = null;
2665 SelectExpression subqueryRight = null;
2666 }
2667 (<UNION> {opType = SetOpType.UNION;} |<INTERSECT> {opType = SetOpType.INTERSECT;} |<EXCEPT> {opType = SetOpType.EXCEPT;}) (<ALL> {setSemantics = false;} )?
2668 (selectBlockRight = SelectBlock()| subqueryRight = Subquery())
2669 {
2670 setOperationRights.add(new SetOperationRight(opType, setSemantics, new SetOperationInput(selectBlockRight, subqueryRight)));
2671 }
2672 )*
2673 {
2674 return new SelectSetOperation(setOperationInputLeft, setOperationRights);
2675 }
2676}
2677
Yingyi Buab817482016-08-19 21:29:31 -07002678SelectExpression Subquery() throws ParseException:
2679{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002680 SelectExpression selectExpr = null;
2681}
2682{
2683 <LEFTPAREN> selectExpr = SelectExpression(true) {} <RIGHTPAREN>
2684 {
2685 return selectExpr;
2686 }
2687}
2688
Yingyi Buab817482016-08-19 21:29:31 -07002689SelectBlock SelectBlock() throws ParseException:
2690{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002691 SelectClause selectClause = null;
2692 FromClause fromClause = null;
2693 List<LetClause> fromLetClauses = null;
2694 WhereClause whereClause = null;
2695 GroupbyClause groupbyClause = null;
2696 List<LetClause> gbyLetClauses = null;
2697 HavingClause havingClause = null;
2698}
2699{
2700 (
2701 selectClause = SelectClause()
2702 (
2703 LOOKAHEAD(1)
2704 fromClause = FromClause()
2705 (
2706 LOOKAHEAD(1)
2707 fromLetClauses = LetClause()
2708 )?
2709 )?
2710 (whereClause = WhereClause())?
2711 (
2712 groupbyClause = GroupbyClause()
2713 (
2714 LOOKAHEAD(1)
2715 gbyLetClauses = LetClause()
2716 )?
2717 (havingClause = HavingClause())?
2718 )?
2719 |
2720 fromClause = FromClause()
2721 (
2722 LOOKAHEAD(1)
2723 fromLetClauses = LetClause()
2724 )?
2725 (whereClause = WhereClause())?
2726 (
2727 groupbyClause = GroupbyClause()
2728 (
2729 gbyLetClauses = LetClause()
2730 )?
2731 (havingClause = HavingClause())?
2732 )?
2733 selectClause = SelectClause()
2734 )
2735 {
2736 return new SelectBlock(selectClause, fromClause, fromLetClauses, whereClause, groupbyClause, gbyLetClauses, havingClause);
2737 }
2738}
2739
Yingyi Buab817482016-08-19 21:29:31 -07002740SelectClause SelectClause() throws ParseException:
2741{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002742 SelectRegular selectRegular = null;
2743 SelectElement selectElement = null;
2744 boolean distinct = false;
2745}
2746{
Michael Blowd6cf6412016-06-30 02:44:35 -04002747 <SELECT> (<ALL>|<DISTINCT> {distinct = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002748 (
2749 selectRegular = SelectRegular()
Michael Blowd6cf6412016-06-30 02:44:35 -04002750 |
Yingyi Bu391f09e2015-10-29 13:49:39 -07002751 selectElement = SelectElement()
Yingyi Bua89fae62016-07-06 07:58:55 -07002752 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002753 {
Yingyi Bua89fae62016-07-06 07:58:55 -07002754 if(selectRegular == null && selectElement == null){
2755 Projection projection = new Projection(null, null, true, false);
2756 List<Projection> projections = new ArrayList<Projection>();
2757 projections.add(projection);
2758 selectRegular = new SelectRegular(projections);
2759 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002760 return new SelectClause(selectElement, selectRegular, distinct);
2761 }
2762}
2763
Yingyi Buab817482016-08-19 21:29:31 -07002764SelectRegular SelectRegular() throws ParseException:
2765{
Michael Blowd6cf6412016-06-30 02:44:35 -04002766 List<Projection> projections = new ArrayList<Projection>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002767}
2768{
2769 {
2770 Projection projection = null;
2771 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002772 projection = Projection() { projections.add(projection); }
Yingyi Bua89fae62016-07-06 07:58:55 -07002773 ( LOOKAHEAD(2) <COMMA>
2774 projection = Projection() {projections.add(projection);}
2775 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -07002776 {
2777 return new SelectRegular(projections);
2778 }
2779}
2780
Yingyi Buab817482016-08-19 21:29:31 -07002781SelectElement SelectElement() throws ParseException:
2782{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002783 Expression expr = null;
2784 String name = null;
2785}
2786{
2787 (<RAW>|<ELEMENT>|<VALUE>) expr = Expression()
2788 {
2789 return new SelectElement(expr);
2790 }
2791}
2792
Yingyi Buab817482016-08-19 21:29:31 -07002793Projection Projection() throws ParseException :
2794{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002795 Expression expr = null;
2796 Identifier identifier = null;
2797 String name = null;
2798 boolean star = false;
Dmitry Lychagin5cdaa5d2018-02-21 11:11:26 -08002799 boolean varStar = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002800}
2801{
2802 (
Dmitry Lychagin5cdaa5d2018-02-21 11:11:26 -08002803 <MUL> {star = true; }
2804 | LOOKAHEAD(3) expr = VariableRef() <DOT> <MUL> {varStar = true; }
2805 | expr = Expression() ((<AS>)? name = Identifier())?
2806 {
2807 if (name == null) {
2808 String generatedColumnIdentifier = ExpressionToVariableUtil.getGeneratedIdentifier(expr, false);
2809 if (generatedColumnIdentifier != null) {
2810 name = SqlppVariableUtil.toUserDefinedName(generatedColumnIdentifier);
2811 }
2812 }
2813 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002814 )
2815 {
Dmitry Lychagin5cdaa5d2018-02-21 11:11:26 -08002816 return new Projection(expr, name, star, varStar);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002817 }
2818}
2819
2820FromClause FromClause() throws ParseException :
2821{
2822 List<FromTerm> fromTerms = new ArrayList<FromTerm>();
2823 extendCurrentScope();
2824}
2825{
2826 {
2827 FromTerm fromTerm = null;
2828 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002829 <FROM> fromTerm = FromTerm() { fromTerms.add(fromTerm); }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002830 (LOOKAHEAD(2) <COMMA> fromTerm = FromTerm() { fromTerms.add(fromTerm); } )*
2831 {
2832 return new FromClause(fromTerms);
2833 }
2834}
2835
2836FromTerm FromTerm() throws ParseException :
2837{
2838 Expression leftExpr = null;
Michael Blowd6cf6412016-06-30 02:44:35 -04002839 VariableExpr leftVar = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002840 VariableExpr posVar = null;
2841 List<AbstractBinaryCorrelateClause> correlateClauses = new ArrayList<AbstractBinaryCorrelateClause>();
2842}
2843{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002844 leftExpr = Expression() ((<AS>)? leftVar = Variable())? (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002845 (
2846 {JoinType joinType = JoinType.INNER; }
2847 (joinType = JoinType())?
2848 {
2849 AbstractBinaryCorrelateClause correlateClause = null;
2850 }
2851 (correlateClause = JoinClause(joinType)
Yingyi Bu6d999ed2016-07-13 11:59:06 -07002852 | correlateClause = UnnestClause(joinType)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002853 )
2854 {
2855 correlateClauses.add(correlateClause);
2856 }
2857 )*
2858 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002859 if(leftVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002860 leftVar = ExpressionToVariableUtil.getGeneratedVariable(leftExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002861 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002862 return new FromTerm(leftExpr, leftVar, posVar, correlateClauses);
2863 }
2864}
2865
2866JoinClause JoinClause(JoinType joinType) throws ParseException :
2867{
2868 Expression rightExpr = null;
2869 VariableExpr rightVar = null;
2870 VariableExpr posVar = null;
2871 Expression conditionExpr = null;
2872}
2873{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002874 <JOIN> rightExpr = Expression() ((<AS>)? rightVar = Variable())? (<AT> posVar = Variable())? <ON> conditionExpr = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002875 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002876 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002877 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002878 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002879 return new JoinClause(joinType, rightExpr, rightVar, posVar, conditionExpr);
2880 }
2881}
2882
Yingyi Bu391f09e2015-10-29 13:49:39 -07002883UnnestClause UnnestClause(JoinType joinType) throws ParseException :
2884{
2885 Expression rightExpr;
2886 VariableExpr rightVar;
2887 VariableExpr posVar = null;
2888}
2889{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002890 (<UNNEST>|<CORRELATE>|<FLATTEN>) rightExpr = Expression() ((<AS>)? rightVar = Variable()) (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002891 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002892 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002893 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002894 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002895 return new UnnestClause(joinType, rightExpr, rightVar, posVar);
2896 }
2897}
2898
2899
2900JoinType JoinType() throws ParseException :
2901{
2902 JoinType joinType = JoinType.INNER;
2903}
2904{
2905 (<INNER>|<LEFT> (<OUTER>)? {joinType = JoinType.LEFTOUTER; })
2906 {
2907 return joinType;
2908 }
2909}
2910
2911List<LetClause> LetClause() throws ParseException:
2912{
2913 List<LetClause> letList = new ArrayList<LetClause>();
2914 LetClause letClause;
2915}
2916{
2917 (
2918 (<LET>|<LETTING>) letClause = LetElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = LetElement() { letList.add(letClause); })*
2919 |
2920 <WITH> letClause = WithElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = WithElement() { letList.add(letClause); })*
2921 )
2922 {
2923 return letList;
2924 }
2925}
2926
2927WhereClause WhereClause()throws ParseException :
2928{
2929 WhereClause wc = new WhereClause();
2930 Expression whereExpr;
2931}
2932{
2933 <WHERE> whereExpr = Expression()
2934 {
2935 wc.setWhereExpr(whereExpr);
2936 return wc;
2937 }
2938}
2939
2940OrderbyClause OrderbyClause()throws ParseException :
2941{
2942 OrderbyClause oc = new OrderbyClause();
2943 Expression orderbyExpr;
2944 List<Expression> orderbyList = new ArrayList<Expression>();
2945 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2946 int numOfOrderby = 0;
2947}
2948{
2949 <ORDER>
2950 {
2951 String hint = getHint(token);
2952 if (hint != null) {
2953 if (hint.startsWith(INMEMORY_HINT)) {
2954 String splits[] = hint.split(" +");
2955 int numFrames = Integer.parseInt(splits[1]);
2956 int numTuples = Integer.parseInt(splits[2]);
2957 oc.setNumFrames(numFrames);
2958 oc.setNumTuples(numTuples);
2959 }
2960 }
2961 }
2962 <BY> orderbyExpr = Expression()
2963 {
2964 orderbyList.add(orderbyExpr);
2965 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2966 }
2967 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2968 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2969 {
2970 modifierList.add(modif);
2971 }
2972
2973 (LOOKAHEAD(2) <COMMA> orderbyExpr = Expression()
2974 {
2975 orderbyList.add(orderbyExpr);
2976 modif = OrderbyClause.OrderModifier.ASC;
2977 }
2978 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2979 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2980 {
2981 modifierList.add(modif);
2982 }
2983 )*
2984
2985 {
2986 oc.setModifierList(modifierList);
2987 oc.setOrderbyList(orderbyList);
2988 return oc;
2989 }
2990}
2991
2992GroupbyClause GroupbyClause()throws ParseException :
2993{
2994 GroupbyClause gbc = new GroupbyClause();
2995 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2996 VariableExpr var = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002997 Expression expr = null;
2998 VariableExpr decorVar = null;
2999 Expression decorExpr = null;
Yingyi Buacc12a92016-03-26 17:25:05 -07003000
3001 VariableExpr groupVar = null;
3002 List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<Pair<Expression, Identifier>>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07003003}
3004{
3005 {
3006 Scope newScope = extendCurrentScopeNoPush(true);
3007 // extendCurrentScope(true);
3008 }
3009 <GROUP>
3010 {
3011 String hint = getHint(token);
3012 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
3013 gbc.setHashGroupByHint(true);
3014 }
3015 }
3016 <BY> (
3017 expr = Expression()
3018 (LOOKAHEAD(1) (<AS>)?
3019 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08003020 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003021 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07003022 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07003023 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07003024 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07003025 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
3026 vePairList.add(pair1);
3027 }
3028 ( LOOKAHEAD(1) <COMMA>
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07003029 {
3030 var = null;
3031 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07003032 expr = Expression()
3033 (LOOKAHEAD(1) (<AS>)?
3034 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08003035 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003036 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07003037 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07003038 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07003039 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07003040 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
3041 vePairList.add(pair2);
3042 }
3043 )*
3044 )
Yingyi Buacc12a92016-03-26 17:25:05 -07003045 (<GROUP> <AS> groupVar = Variable()
3046 ( LOOKAHEAD(1)
3047 {
3048 VariableExpr fieldVarExpr = null;
3049 String fieldIdentifierStr = null;
3050 }
3051 <LEFTPAREN>
3052 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
3053 {
3054 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
3055 }
3056 (<COMMA>
3057 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
3058 {
3059 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
3060 }
3061 )*
3062 <RIGHTPAREN>
3063 )?
3064 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003065 {
3066 gbc.setGbyPairList(vePairList);
3067 gbc.setDecorPairList(new ArrayList<GbyVariableExpressionPair>());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07003068 gbc.setWithVarMap(new HashMap<Expression, VariableExpr>());
Yingyi Buacc12a92016-03-26 17:25:05 -07003069 gbc.setGroupVar(groupVar);
3070 gbc.setGroupFieldList(groupFieldList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003071 replaceCurrentScope(newScope);
3072 return gbc;
3073 }
3074}
3075
3076HavingClause HavingClause() throws ParseException:
3077{
3078 Expression filterExpr = null;
3079}
3080{
3081 <HAVING> filterExpr = Expression()
3082 {
3083 return new HavingClause(filterExpr);
3084 }
3085}
3086
3087LimitClause LimitClause() throws ParseException:
3088{
3089 LimitClause lc = new LimitClause();
3090 Expression expr;
3091 pushForbiddenScope(getCurrentScope());
3092}
3093{
3094 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
3095 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
3096
3097 {
3098 popForbiddenScope();
3099 return lc;
3100 }
3101}
3102
3103QuantifiedExpression QuantifiedExpression()throws ParseException:
3104{
3105 QuantifiedExpression qc = new QuantifiedExpression();
3106 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
3107 Expression satisfiesExpr;
3108 VariableExpr var;
3109 Expression inExpr;
3110 QuantifiedPair pair;
3111}
3112{
3113 {
3114 createNewScope();
3115 }
3116
Yingyi Bu8aac7242016-09-13 23:14:09 -07003117 ( ((<ANY>|<SOME>) { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
Michael Blowd6cf6412016-06-30 02:44:35 -04003118 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
Yingyi Bu391f09e2015-10-29 13:49:39 -07003119 var = Variable() <IN> inExpr = Expression()
3120 {
3121 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003122 quantifiedList.add(pair);
3123 }
3124 (
3125 <COMMA> var = Variable() <IN> inExpr = Expression()
3126 {
3127 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003128 quantifiedList.add(pair);
3129 }
3130 )*
Yingyi Bu858efae2016-10-13 17:31:57 -07003131 <SATISFIES> satisfiesExpr = Expression() (<END>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003132 {
3133 qc.setSatisfiesExpr(satisfiesExpr);
3134 qc.setQuantifiedList(quantifiedList);
3135 removeCurrentScope();
3136 return qc;
3137 }
3138}
3139
3140LetClause LetElement() throws ParseException:
3141{
3142 LetClause lc = new LetClause();
3143 VariableExpr varExp;
3144 Expression beExp;
3145 extendCurrentScope();
3146}
3147{
3148 varExp = Variable() <EQ> beExp = Expression()
3149 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07003150 lc.setVarExpr(varExp);
3151 lc.setBindingExpr(beExp);
3152 return lc;
3153 }
3154}
3155
3156LetClause WithElement() throws ParseException:
3157{
3158 LetClause lc = new LetClause();
3159 VariableExpr varExp;
3160 Expression beExp;
3161 extendCurrentScope();
3162}
3163{
3164 varExp = Variable() <AS> beExp = Expression()
3165 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07003166 lc.setVarExpr(varExp);
3167 lc.setBindingExpr(beExp);
3168 return lc;
3169 }
3170}
3171
3172TOKEN_MGR_DECLS:
3173{
3174 public int commentDepth = 0;
Till Westmanne9b2adf2016-10-15 12:39:01 -07003175 public ArrayDeque<Integer> lexerStateStack = new ArrayDeque<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07003176
3177 public void pushState() {
3178 lexerStateStack.push( curLexState );
3179 }
3180
3181 public void popState(String token) {
3182 if (lexerStateStack.size() > 0) {
3183 SwitchTo( lexerStateStack.pop() );
3184 } else {
3185 int errorLine = input_stream.getEndLine();
3186 int errorColumn = input_stream.getEndColumn();
3187 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
3188 + "\" but state stack is empty.";
3189 throw new TokenMgrError(msg, -1);
3190 }
3191 }
3192}
3193
3194<DEFAULT,IN_DBL_BRACE>
3195TOKEN [IGNORE_CASE]:
3196{
3197 <ALL : "all">
3198 | <AND : "and">
Yingyi Bu8aac7242016-09-13 23:14:09 -07003199 | <ANY : "any">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003200 | <APPLY : "apply">
3201 | <AS : "as">
3202 | <ASC : "asc">
3203 | <AT : "at">
3204 | <AUTOGENERATED : "autogenerated">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07003205 | <BETWEEN : "between">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003206 | <BTREE : "btree">
3207 | <BY : "by">
3208 | <CASE : "case">
3209 | <CLOSED : "closed">
3210 | <CREATE : "create">
3211 | <COMPACTION : "compaction">
3212 | <COMPACT : "compact">
3213 | <CONNECT : "connect">
3214 | <CORRELATE : "correlate">
Yingyi Bud56ff032016-08-01 10:26:57 -07003215 | <DATASET : "dataset">
Yingyi Bu1c0fff52016-03-25 20:23:30 -07003216 | <COLLECTION : "collection">
Yingyi Bud56ff032016-08-01 10:26:57 -07003217 | <DATAVERSE : "dataverse">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003218 | <DECLARE : "declare">
3219 | <DEFINITION : "definition">
3220 | <DELETE : "delete">
3221 | <DESC : "desc">
3222 | <DISCONNECT : "disconnect">
3223 | <DISTINCT : "distinct">
3224 | <DROP : "drop">
3225 | <ELEMENT : "element">
Till Westmann516d1a82016-08-02 14:45:53 -07003226 | <EXPLAIN : "explain">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003227 | <ELSE : "else">
3228 | <ENFORCED : "enforced">
Yingyi Buc8c067c2016-07-25 23:37:19 -07003229 | <END : "end">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003230 | <EVERY : "every">
3231 | <EXCEPT : "except">
3232 | <EXISTS : "exists">
3233 | <EXTERNAL : "external">
3234 | <FEED : "feed">
3235 | <FILTER : "filter">
3236 | <FLATTEN : "flatten">
3237 | <FOR : "for">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003238 | <FROM : "from">
3239 | <FULL : "full">
Taewoo Kimc49405a2017-01-04 00:30:43 -08003240 | <FULLTEXT : "fulltext">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003241 | <FUNCTION : "function">
3242 | <GROUP : "group">
3243 | <HAVING : "having">
3244 | <HINTS : "hints">
3245 | <IF : "if">
3246 | <INTO : "into">
3247 | <IN : "in">
3248 | <INDEX : "index">
3249 | <INGESTION : "ingestion">
3250 | <INNER : "inner">
3251 | <INSERT : "insert">
3252 | <INTERNAL : "internal">
3253 | <INTERSECT : "intersect">
Yingyi Budaa549c2016-06-28 22:30:52 -07003254 | <IS : "is">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003255 | <JOIN : "join">
3256 | <KEYWORD : "keyword">
3257 | <KEY : "key">
Dmitry Lychagin1aa0dd42018-04-17 14:35:24 -07003258 | <KNOWN : "known">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003259 | <LEFT : "left">
3260 | <LETTING : "letting">
3261 | <LET : "let">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07003262 | <LIKE : "like">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003263 | <LIMIT : "limit">
3264 | <LOAD : "load">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003265 | <NODEGROUP : "nodegroup">
3266 | <NGRAM : "ngram">
Yingyi Budaa549c2016-06-28 22:30:52 -07003267 | <NOT : "not">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003268 | <OFFSET : "offset">
3269 | <ON : "on">
3270 | <OPEN : "open">
3271 | <OR : "or">
3272 | <ORDER : "order">
3273 | <OUTER : "outer">
3274 | <OUTPUT : "output">
3275 | <PATH : "path">
3276 | <POLICY : "policy">
3277 | <PRESORTED : "pre-sorted">
3278 | <PRIMARY : "primary">
3279 | <RAW : "raw">
3280 | <REFRESH : "refresh">
3281 | <RETURN : "return">
Yingyi Bucb5bf332017-01-02 22:19:50 -08003282 | <RETURNING : "returning">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003283 | <RTREE : "rtree">
3284 | <RUN : "run">
3285 | <SATISFIES : "satisfies">
3286 | <SECONDARY : "secondary">
3287 | <SELECT : "select">
3288 | <SET : "set">
3289 | <SOME : "some">
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08003290 | <START : "start">
3291 | <STOP : "stop">
Murtadha Hubail2c04ae02017-11-21 15:58:01 +03003292 | <TEMPORARY : "temporary"> // intentionally not used but reserved for future usage
Yingyi Bu391f09e2015-10-29 13:49:39 -07003293 | <THEN : "then">
3294 | <TYPE : "type">
3295 | <TO : "to">
3296 | <UNION : "union">
Dmitry Lychagin8b6578a2017-11-08 15:04:35 -08003297 | <UNKNOWN : "unknown">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003298 | <UNNEST : "unnest">
Yingyi Budaa549c2016-06-28 22:30:52 -07003299 | <UPDATE : "update">
Yingyi Bucb5bf332017-01-02 22:19:50 -08003300 | <UPSERT : "upsert">
Yingyi Budaa549c2016-06-28 22:30:52 -07003301 | <USE : "use">
3302 | <USING : "using">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003303 | <VALUE : "value">
Dmitry Lychagin7d594a32018-01-15 14:31:03 -08003304 | <VALUED : "valued">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003305 | <WHEN : "when">
3306 | <WHERE : "where">
3307 | <WITH : "with">
3308 | <WRITE : "write">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003309}
3310
3311<DEFAULT,IN_DBL_BRACE>
3312TOKEN :
3313{
3314 <CARET : "^">
Yingyi Bufdc71eb2016-08-24 22:41:57 -07003315 | <CONCAT : "||">
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07003316 | <DIVIDE : "/">
3317 | <DIV : "div">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003318 | <MINUS : "-">
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07003319 | <MOD : "mod">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003320 | <MUL : "*">
3321 | <PLUS : "+">
3322
3323 | <LEFTPAREN : "(">
3324 | <RIGHTPAREN : ")">
3325 | <LEFTBRACKET : "[">
3326 | <RIGHTBRACKET : "]">
3327
3328 | <ATT : "@">
3329 | <COLON : ":">
3330 | <COMMA : ",">
3331 | <DOT : ".">
Dmitry Lychaginbf48c492018-05-01 18:30:27 -07003332 | <PERCENT: "%">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003333 | <QUES : "?">
3334 | <SEMICOLON : ";">
3335 | <SHARP : "#">
3336
3337 | <LT : "<">
3338 | <GT : ">">
3339 | <LE : "<=">
3340 | <GE : ">=">
3341 | <EQ : "=">
3342 | <NE : "!=">
Yingyi Bu4a4b8962016-09-16 12:09:11 -07003343 | <LG : "<>">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003344 | <SIMILAR : "~=">
3345}
3346
3347<DEFAULT,IN_DBL_BRACE>
3348TOKEN :
3349{
3350 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
3351}
3352
3353<DEFAULT>
3354TOKEN :
3355{
3356 <RIGHTBRACE : "}"> { popState("}"); }
3357}
3358
3359<DEFAULT,IN_DBL_BRACE>
3360TOKEN :
3361{
3362 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
3363}
3364
3365<IN_DBL_BRACE>
3366TOKEN :
3367{
3368 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
3369}
3370
3371<DEFAULT,IN_DBL_BRACE>
3372TOKEN :
3373{
3374 <INTEGER_LITERAL : (<DIGIT>)+ >
3375}
3376
3377<DEFAULT,IN_DBL_BRACE>
Yingyi Bue311a632016-06-07 18:23:16 -07003378TOKEN [IGNORE_CASE]:
Yingyi Bu391f09e2015-10-29 13:49:39 -07003379{
Yingyi Bu535d86b2016-05-23 16:44:25 -07003380 <MISSING : "missing">
3381 | <NULL : "null">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003382 | <TRUE : "true">
3383 | <FALSE : "false">
3384}
3385
3386<DEFAULT,IN_DBL_BRACE>
3387TOKEN :
3388{
3389 <#DIGIT : ["0" - "9"]>
3390}
3391
3392<DEFAULT,IN_DBL_BRACE>
3393TOKEN:
3394{
Dmitry Lychaginee8526b2018-03-30 14:21:01 -07003395 < DOUBLE_LITERAL: <DIGITS> ( "." <DIGITS> ) (("e"|"E") ("+"|"-")? <DIGITS>)?
3396 | <DIGITS> (("e"|"E") ("+"|"-")? <DIGITS>)
3397 | "." <DIGITS> (("e"|"E") ("+"|"-")? <DIGITS>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003398 >
Yingyi Bue4d919e2016-10-30 10:47:03 -07003399 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
3400 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
3401 | "." <DIGITS> ( "f" | "F" )
Yingyi Bu391f09e2015-10-29 13:49:39 -07003402 >
3403 | <DIGITS : (<DIGIT>)+ >
3404}
3405
3406<DEFAULT,IN_DBL_BRACE>
3407TOKEN :
3408{
3409 <#LETTER : ["A" - "Z", "a" - "z"]>
Dmitry Lychagin9c83efb2018-01-16 14:31:58 -08003410 | <#IDENTIFIER_SPECIALCHARS_START : ["_"]>
3411 | <#IDENTIFIER_SPECIALCHARS_REST : ["$"]>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003412}
3413
3414<DEFAULT,IN_DBL_BRACE>
3415TOKEN :
3416{
3417 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
Yingyi Bu6d57e492016-06-06 21:24:42 -07003418 <QUOTED_STRING : "`" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003419 <EscapeQuot>
3420 | <EscapeBslash>
3421 | <EscapeSlash>
3422 | <EscapeBspace>
3423 | <EscapeFormf>
3424 | <EscapeNl>
3425 | <EscapeCr>
3426 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003427 | ~["`","\\"])* "`">
3428 | <STRING_LITERAL : ("\"" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003429 <EscapeQuot>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003430 | <EscapeBslash>
3431 | <EscapeSlash>
3432 | <EscapeBspace>
3433 | <EscapeFormf>
3434 | <EscapeNl>
3435 | <EscapeCr>
3436 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003437 | ~["\"","\\"])* "\"")
3438 | ("\'"(
3439 <EscapeApos>
3440 | <EscapeBslash>
3441 | <EscapeSlash>
3442 | <EscapeBspace>
3443 | <EscapeFormf>
3444 | <EscapeNl>
3445 | <EscapeCr>
3446 | <EscapeTab>
3447 | ~["\'","\\"])* "\'")>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003448 | < #EscapeQuot: "\\\"" >
3449 | < #EscapeApos: "\\\'" >
3450 | < #EscapeBslash: "\\\\" >
3451 | < #EscapeSlash: "\\/" >
3452 | < #EscapeBspace: "\\b" >
3453 | < #EscapeFormf: "\\f" >
3454 | < #EscapeNl: "\\n" >
3455 | < #EscapeCr: "\\r" >
3456 | < #EscapeTab: "\\t" >
3457}
3458
3459<DEFAULT,IN_DBL_BRACE>
3460TOKEN :
3461{
Dmitry Lychagin9c83efb2018-01-16 14:31:58 -08003462 <IDENTIFIER : ( <LETTER> | <IDENTIFIER_SPECIALCHARS_START> )
3463 ( <LETTER> | <DIGIT> | <IDENTIFIER_SPECIALCHARS_START> | <IDENTIFIER_SPECIALCHARS_REST> )*>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003464}
3465
3466<DEFAULT,IN_DBL_BRACE>
3467SKIP:
3468{
3469 " "
3470 | "\t"
3471 | "\r"
3472 | "\n"
3473}
3474
3475<DEFAULT,IN_DBL_BRACE>
3476SKIP:
3477{
3478 <"//" (~["\n"])* "\n">
3479}
3480
3481<DEFAULT,IN_DBL_BRACE>
3482SKIP:
3483{
3484 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3485}
3486
3487<DEFAULT,IN_DBL_BRACE>
3488SKIP:
3489{
Yingyi Bu93846a72016-09-13 16:30:39 -07003490 <"--" (~["\n"])* "\n">
3491}
3492
3493
3494<DEFAULT,IN_DBL_BRACE>
3495SKIP:
3496{
3497 <"--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3498}
3499
3500<DEFAULT,IN_DBL_BRACE>
3501SKIP:
3502{
Yingyi Bu391f09e2015-10-29 13:49:39 -07003503 <"/*"> { pushState(); } : INSIDE_COMMENT
3504}
3505
3506<INSIDE_COMMENT>
3507SPECIAL_TOKEN:
3508{
3509 <"+"(" ")*(~["*"])*>
3510}
3511
3512<INSIDE_COMMENT>
3513SKIP:
3514{
3515 <"/*"> { pushState(); }
3516}
3517
3518<INSIDE_COMMENT>
3519SKIP:
3520{
3521 <"*/"> { popState("*/"); }
3522 | <~[]>
3523}