blob: 69bfbc5242aa0c5e15f93e3429b32e556f34cdcc [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;
Yingyi Bu391f09e2015-10-29 13:49:39 -070067import org.apache.asterix.common.functions.FunctionSignature;
68import org.apache.asterix.lang.common.base.Expression;
69import org.apache.asterix.lang.common.base.Literal;
70import org.apache.asterix.lang.common.base.IParser;
71import org.apache.asterix.lang.common.base.Statement;
72import org.apache.asterix.lang.common.clause.GroupbyClause;
73import org.apache.asterix.lang.common.clause.LetClause;
74import org.apache.asterix.lang.common.clause.LimitClause;
75import org.apache.asterix.lang.common.clause.OrderbyClause;
76import org.apache.asterix.lang.common.clause.UpdateClause;
77import org.apache.asterix.lang.common.clause.WhereClause;
78import org.apache.asterix.lang.common.context.RootScopeFactory;
79import org.apache.asterix.lang.common.context.Scope;
80import org.apache.asterix.lang.common.expression.AbstractAccessor;
81import org.apache.asterix.lang.common.expression.CallExpr;
82import org.apache.asterix.lang.common.expression.FieldAccessor;
83import org.apache.asterix.lang.common.expression.FieldBinding;
84import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
85import org.apache.asterix.lang.common.expression.IfExpr;
86import org.apache.asterix.lang.common.expression.IndexAccessor;
87import org.apache.asterix.lang.common.expression.ListConstructor;
88import org.apache.asterix.lang.common.expression.LiteralExpr;
89import org.apache.asterix.lang.common.expression.OperatorExpr;
90import org.apache.asterix.lang.common.expression.OrderedListTypeDefinition;
91import org.apache.asterix.lang.common.expression.QuantifiedExpression;
92import org.apache.asterix.lang.common.expression.RecordConstructor;
93import org.apache.asterix.lang.common.expression.RecordTypeDefinition;
94import org.apache.asterix.lang.common.expression.TypeExpression;
95import org.apache.asterix.lang.common.expression.TypeReferenceExpression;
96import org.apache.asterix.lang.common.expression.UnaryExpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -070097import org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition;
98import org.apache.asterix.lang.common.expression.VariableExpr;
99import org.apache.asterix.lang.common.literal.DoubleLiteral;
100import org.apache.asterix.lang.common.literal.FalseLiteral;
101import org.apache.asterix.lang.common.literal.FloatLiteral;
102import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
Yingyi Bu535d86b2016-05-23 16:44:25 -0700103import org.apache.asterix.lang.common.literal.MissingLiteral;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700104import org.apache.asterix.lang.common.literal.NullLiteral;
105import org.apache.asterix.lang.common.literal.StringLiteral;
106import org.apache.asterix.lang.common.literal.TrueLiteral;
107import org.apache.asterix.lang.common.parser.ScopeChecker;
108import org.apache.asterix.lang.common.statement.CompactStatement;
109import org.apache.asterix.lang.common.statement.ConnectFeedStatement;
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800110import org.apache.asterix.lang.common.statement.StartFeedStatement;
111import org.apache.asterix.lang.common.statement.StopFeedStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700112import org.apache.asterix.lang.common.statement.CreateDataverseStatement;
113import org.apache.asterix.lang.common.statement.CreateFeedPolicyStatement;
114import org.apache.asterix.lang.common.statement.CreateFeedStatement;
115import org.apache.asterix.lang.common.statement.CreateFunctionStatement;
116import org.apache.asterix.lang.common.statement.CreateIndexStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700117import org.apache.asterix.lang.common.statement.DatasetDecl;
118import org.apache.asterix.lang.common.statement.DataverseDecl;
119import org.apache.asterix.lang.common.statement.DataverseDropStatement;
120import org.apache.asterix.lang.common.statement.DeleteStatement;
121import org.apache.asterix.lang.common.statement.DisconnectFeedStatement;
Yingyi Buab817482016-08-19 21:29:31 -0700122import org.apache.asterix.lang.common.statement.DropDatasetStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700123import org.apache.asterix.lang.common.statement.ExternalDetailsDecl;
124import org.apache.asterix.lang.common.statement.FeedDropStatement;
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300125import org.apache.asterix.lang.common.statement.FeedPolicyDropStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700126import org.apache.asterix.lang.common.statement.FunctionDecl;
127import org.apache.asterix.lang.common.statement.FunctionDropStatement;
128import org.apache.asterix.lang.common.statement.IndexDropStatement;
129import org.apache.asterix.lang.common.statement.InsertStatement;
130import org.apache.asterix.lang.common.statement.InternalDetailsDecl;
131import org.apache.asterix.lang.common.statement.LoadStatement;
132import org.apache.asterix.lang.common.statement.NodeGroupDropStatement;
133import org.apache.asterix.lang.common.statement.NodegroupDecl;
134import org.apache.asterix.lang.common.statement.Query;
135import org.apache.asterix.lang.common.statement.RefreshExternalDatasetStatement;
136import org.apache.asterix.lang.common.statement.RunStatement;
137import org.apache.asterix.lang.common.statement.SetStatement;
138import org.apache.asterix.lang.common.statement.TypeDecl;
139import org.apache.asterix.lang.common.statement.TypeDropStatement;
140import org.apache.asterix.lang.common.statement.UpdateStatement;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800141import org.apache.asterix.lang.common.statement.UpsertStatement;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700142import org.apache.asterix.lang.common.statement.WriteStatement;
143import org.apache.asterix.lang.common.struct.Identifier;
144import org.apache.asterix.lang.common.struct.QuantifiedPair;
145import org.apache.asterix.lang.common.struct.VarIdentifier;
146import org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause;
147import org.apache.asterix.lang.sqlpp.clause.FromClause;
148import org.apache.asterix.lang.sqlpp.clause.FromTerm;
149import org.apache.asterix.lang.sqlpp.clause.HavingClause;
150import org.apache.asterix.lang.sqlpp.clause.JoinClause;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700151import org.apache.asterix.lang.sqlpp.clause.Projection;
152import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
153import org.apache.asterix.lang.sqlpp.clause.SelectClause;
154import org.apache.asterix.lang.sqlpp.clause.SelectElement;
155import org.apache.asterix.lang.sqlpp.clause.SelectRegular;
156import org.apache.asterix.lang.sqlpp.clause.SelectSetOperation;
157import org.apache.asterix.lang.sqlpp.clause.UnnestClause;
Yingyi Buc8c067c2016-07-25 23:37:19 -0700158import org.apache.asterix.lang.sqlpp.expression.CaseExpression;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700159import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
160import org.apache.asterix.lang.sqlpp.optype.JoinType;
161import org.apache.asterix.lang.sqlpp.optype.SetOpType;
162import org.apache.asterix.lang.sqlpp.struct.SetOperationInput;
163import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
Yingyi Bu9e3f9be2016-07-01 10:07:37 -0700164import org.apache.asterix.lang.sqlpp.util.ExpressionToVariableUtil;
Xikui Wang96fd4022017-04-10 14:23:31 -0700165import org.apache.asterix.lang.sqlpp.util.FunctionMapUtil;
Yingyi Buacc12a92016-03-26 17:25:05 -0700166import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700167import org.apache.hyracks.algebricks.common.utils.Pair;
168import org.apache.hyracks.algebricks.common.utils.Triple;
169import org.apache.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
170import org.apache.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
171import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
172
Yingyi Bucaea8f02015-11-16 15:12:15 -0800173class SQLPPParser extends ScopeChecker implements IParser {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700174
175 // optimizer hints
176 private static final String AUTO_HINT = "auto";
177 private static final String BROADCAST_JOIN_HINT = "bcast";
178 private static final String COMPOSE_VAL_FILES_HINT = "compose-val-files";
179 private static final String DATE_BETWEEN_YEARS_HINT = "date-between-years";
180 private static final String DATETIME_ADD_RAND_HOURS_HINT = "datetime-add-rand-hours";
181 private static final String DATETIME_BETWEEN_YEARS_HINT = "datetime-between-years";
182 private static final String HASH_GROUP_BY_HINT = "hash";
183 private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
184 private static final String INMEMORY_HINT = "inmem";
185 private static final String INSERT_RAND_INT_HINT = "insert-rand-int";
186 private static final String INTERVAL_HINT = "interval";
187 private static final String LIST_HINT = "list";
188 private static final String LIST_VAL_FILE_HINT = "list-val-file";
189 private static final String RANGE_HINT = "range";
190 private static final String SKIP_SECONDARY_INDEX_SEARCH_HINT = "skip-index";
191 private static final String VAL_FILE_HINT = "val-files";
192 private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
193
194 private static final String GEN_FIELDS_HINT = "gen-fields";
195
196 // data generator hints
197 private static final String DGEN_HINT = "dgen";
198
Till Westmann7199a562016-09-17 16:07:32 -0700199 // error configuration
200 protected static final boolean REPORT_EXPECTED_TOKENS = false;
201
Yingyi Bu391f09e2015-10-29 13:49:39 -0700202 private static class IndexParams {
203 public IndexType type;
204 public int gramLength;
205
206 public IndexParams(IndexType type, int gramLength) {
207 this.type = type;
208 this.gramLength = gramLength;
209 }
210 };
211
212 private static class FunctionName {
213 public String dataverse = null;
214 public String library = null;
215 public String function = null;
216 public String hint = null;
217 }
218
219 private static String getHint(Token t) {
220 if (t.specialToken == null) {
221 return null;
222 }
223 String s = t.specialToken.image;
224 int n = s.length();
225 if (n < 2) {
226 return null;
227 }
228 return s.substring(1).trim();
229 }
230
231 private static IRecordFieldDataGen parseFieldDataGen(String hint) throws ParseException {
232 IRecordFieldDataGen rfdg = null;
233 String splits[] = hint.split(" +");
234 if (splits[0].equals(VAL_FILE_HINT)) {
235 File[] valFiles = new File[splits.length - 1];
236 for (int k=1; k<splits.length; k++) {
237 valFiles[k-1] = new File(splits[k]);
238 }
239 rfdg = new FieldValFileDataGen(valFiles);
240 } else if (splits[0].equals(VAL_FILE_SAME_INDEX_HINT)) {
241 rfdg = new FieldValFileSameIndexDataGen(new File(splits[1]), splits[2]);
242 } else if (splits[0].equals(LIST_VAL_FILE_HINT)) {
243 rfdg = new ListValFileDataGen(new File(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3]));
244 } else if (splits[0].equals(LIST_HINT)) {
245 rfdg = new ListDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
246 } else if (splits[0].equals(INTERVAL_HINT)) {
247 FieldIntervalDataGen.ValueType vt;
248 if (splits[1].equals("int")) {
249 vt = FieldIntervalDataGen.ValueType.INT;
250 } else if (splits[1].equals("long")) {
251 vt = FieldIntervalDataGen.ValueType.LONG;
252 } else if (splits[1].equals("float")) {
253 vt = FieldIntervalDataGen.ValueType.FLOAT;
254 } else if (splits[1].equals("double")) {
255 vt = FieldIntervalDataGen.ValueType.DOUBLE;
256 } else {
257 throw new ParseException("Unknown type for interval data gen: " + splits[1]);
258 }
259 rfdg = new FieldIntervalDataGen(vt, splits[2], splits[3]);
260 } else if (splits[0].equals(INSERT_RAND_INT_HINT)) {
261 rfdg = new InsertRandIntDataGen(splits[1], splits[2]);
262 } else if (splits[0].equals(DATE_BETWEEN_YEARS_HINT)) {
263 rfdg = new DateBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
264 } else if (splits[0].equals(DATETIME_BETWEEN_YEARS_HINT)) {
265 rfdg = new DatetimeBetweenYearsDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]));
266 } else if (splits[0].equals(DATETIME_ADD_RAND_HOURS_HINT)) {
267 rfdg = new DatetimeAddRandHoursDataGen(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), splits[3]);
268 } else if (splits[0].equals(AUTO_HINT)) {
269 rfdg = new AutoDataGen(splits[1]);
270 }
271 return rfdg;
272 }
273
Till Westmann7199a562016-09-17 16:07:32 -0700274 public SQLPPParser(String s) {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700275 this(new StringReader(s));
276 super.setInput(s);
277 }
278
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800279 public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700280 File file = new File(args[0]);
281 Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
282 SQLPPParser parser = new SQLPPParser(fis);
283 List<Statement> st = parser.parse();
284 //st.accept(new SQLPPPrintVisitor(), 0);
285 }
286
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800287 public List<Statement> parse() throws CompilationException {
Yingyi Bu391f09e2015-10-29 13:49:39 -0700288 try {
289 return Statement();
290 } catch (Error e) {
291 // this is here as the JavaCharStream that's below the lexer somtimes throws Errors that are not handled
292 // by the ANTLR-generated lexer or parser (e.g it does this for invalid backslash u + 4 hex digits escapes)
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800293 throw new CompilationException(new ParseException(e.getMessage()));
Yingyi Bu391f09e2015-10-29 13:49:39 -0700294 } catch (ParseException e) {
Taewoo Kime65e6ca2017-01-14 17:53:28 -0800295 throw new CompilationException("Syntax error: " + getMessage(e));
Yingyi Bu391f09e2015-10-29 13:49:39 -0700296 }
297 }
Till Westmann7199a562016-09-17 16:07:32 -0700298
299 protected String getMessage(ParseException pe) {
300 Token currentToken = pe.currentToken;
301 if (currentToken == null) {
302 return pe.getMessage();
303 }
304 int[][] expectedTokenSequences = pe.expectedTokenSequences;
305 String[] tokenImage = pe.tokenImage;
306 String sep = REPORT_EXPECTED_TOKENS ? eol : " ";
307 StringBuilder expected = REPORT_EXPECTED_TOKENS ? new StringBuilder() : null;
308 int maxSize = appendExpected(expected, expectedTokenSequences, tokenImage);
309 Token tok = currentToken.next;
310 int line = tok.beginLine;
311 String message = "In line " + line + " >>" + getLine(line) + "<<" + sep + "Encountered ";
312 for (int i = 0; i < maxSize; i++) {
313 if (i != 0) {
314 message += " ";
315 }
316 if (tok.kind == 0) {
317 message += fixQuotes(tokenImage[0]);
318 break;
319 }
Till Westmanne3c9f272016-10-09 11:09:26 -0700320 final String fixedTokenImage = tokenImage[tok.kind];
321 if (! tok.image.equalsIgnoreCase(stripQuotes(fixedTokenImage))) {
322 message += fixQuotes(fixedTokenImage) + " ";
323 }
Till Westmann7199a562016-09-17 16:07:32 -0700324 message += quot + addEscapes(tok.image) + quot;
325 tok = tok.next;
326 }
327 message += " at column " + currentToken.next.beginColumn + "." + sep;
328 if (REPORT_EXPECTED_TOKENS) {
329 if (expectedTokenSequences.length == 1) {
330 message += "Was expecting:" + sep + " ";
331 } else {
332 message += "Was expecting one of:" + sep + " ";
333 }
334 message += expected.toString();
335 }
336 return message;
337 }
338
Yingyi Bu391f09e2015-10-29 13:49:39 -0700339}
340
341PARSER_END(SQLPPParser)
342
343
344List<Statement> Statement() throws ParseException:
345{
346 scopeStack.push(RootScopeFactory.createRootScope(this));
347 List<Statement> decls = new ArrayList<Statement>();
348 Statement stmt = null;
349}
350{
351 ( stmt = SingleStatement() (<SEMICOLON>)*
352 {
353 decls.add(stmt);
354 }
355 )*
Till Westmann81870d72017-03-16 18:11:23 -0700356 (<SEMICOLON>)*
Yingyi Bu391f09e2015-10-29 13:49:39 -0700357 <EOF>
358 {
359 return decls;
360 }
361}
362
363Statement SingleStatement() throws ParseException:
364{
365 Statement stmt = null;
366}
367{
368 (
369 stmt = DataverseDeclaration()
370 | stmt = FunctionDeclaration()
371 | stmt = CreateStatement()
372 | stmt = LoadStatement()
373 | stmt = DropStatement()
374 | stmt = WriteStatement()
375 | stmt = SetStatement()
376 | stmt = InsertStatement()
377 | stmt = DeleteStatement()
378 | stmt = UpdateStatement()
Yingyi Bucb5bf332017-01-02 22:19:50 -0800379 | stmt = UpsertStatement()
Yingyi Buab817482016-08-19 21:29:31 -0700380 | stmt = ConnectionStatement()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700381 | stmt = CompactStatement()
Till Westmannef3f0272016-07-27 18:34:01 -0700382 | stmt = ExplainStatement()
383 | stmt = Query(false) <SEMICOLON>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700384 | stmt = RefreshExternalDatasetStatement()
385 | stmt = RunStatement()
386 )
387 {
388 return stmt;
389 }
390}
391
392DataverseDecl DataverseDeclaration() throws ParseException:
393{
394 String dvName = null;
395}
396{
397 <USE> dvName = Identifier()
398 {
399 defaultDataverse = dvName;
400 return new DataverseDecl(new Identifier(dvName));
401 }
402}
403
404Statement CreateStatement() throws ParseException:
405{
406 String hint = null;
407 boolean dgen = false;
408 Statement stmt = null;
409}
410{
411 <CREATE>
412 (
413 {
414 hint = getHint(token);
415 if (hint != null && hint.startsWith(DGEN_HINT)) {
416 dgen = true;
417 }
418 }
419 stmt = TypeSpecification(hint, dgen)
420 | stmt = NodegroupSpecification()
421 | stmt = DatasetSpecification()
422 | stmt = IndexSpecification()
423 | stmt = DataverseSpecification()
424 | stmt = FunctionSpecification()
425 | stmt = FeedSpecification()
426 | stmt = FeedPolicySpecification()
427 )
428 {
429 return stmt;
430 }
431}
432
433TypeDecl TypeSpecification(String hint, boolean dgen) throws ParseException:
434{
435 Pair<Identifier,Identifier> nameComponents = null;
436 boolean ifNotExists = false;
437 TypeExpression typeExpr = null;
438}
439{
440 <TYPE> nameComponents = TypeName() ifNotExists = IfNotExists()
Till Westmannf6028272016-09-30 14:34:42 -0700441 <AS> typeExpr = RecordTypeDef()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700442 {
443 long numValues = -1;
444 String filename = null;
445 if (dgen) {
446 String splits[] = hint.split(" +");
447 if (splits.length != 3) {
448 throw new ParseException("Expecting /*+ dgen <filename> <numberOfItems> */");
449 }
450 filename = splits[1];
451 numValues = Long.parseLong(splits[2]);
452 }
453 TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
454 return new TypeDecl(nameComponents.first, nameComponents.second, typeExpr, tddg, ifNotExists);
455 }
456}
457
458
459NodegroupDecl NodegroupSpecification() throws ParseException:
460{
461 String name = null;
462 String tmp = null;
463 boolean ifNotExists = false;
464 List<Identifier>ncNames = null;
465}
466{
467 <NODEGROUP> name = Identifier()
468 ifNotExists = IfNotExists() <ON> tmp = Identifier()
469 {
470 ncNames = new ArrayList<Identifier>();
471 ncNames.add(new Identifier(tmp));
472 }
473 ( <COMMA> tmp = Identifier()
474 {
475 ncNames.add(new Identifier(tmp));
476 }
477 )*
478 {
479 return new NodegroupDecl(new Identifier(name), ncNames, ifNotExists);
480 }
481}
482
483DatasetDecl DatasetSpecification() throws ParseException:
484{
485 Pair<Identifier,Identifier> nameComponents = null;
486 boolean ifNotExists = false;
Your Namedace5f22016-01-12 14:02:48 -0800487 Pair<Identifier,Identifier> typeComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700488 String adapterName = null;
489 Map<String,String> properties = null;
490 Map<String,String> compactionPolicyProperties = null;
491 FunctionSignature appliedFunction = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800492 Pair<List<Integer>, List<List<String>>> primaryKeyFields = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700493 String nodeGroupName = null;
494 Map<String,String> hints = new HashMap<String,String>();
495 DatasetDecl dsetDecl = null;
496 boolean autogenerated = false;
497 String compactionPolicy = null;
498 boolean temp = false;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800499 Pair<Integer, List<String>> filterField = null;
Yingyi Bub9169b62016-02-26 21:21:49 -0800500 Pair<Identifier,Identifier> metaTypeComponents = new Pair<Identifier, Identifier>(null, null);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700501}
502{
503 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700504 <EXTERNAL> Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800505 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700506 ifNotExists = IfNotExists()
507 <USING> adapterName = AdapterName() properties = Configuration()
508 (<ON> nodeGroupName = Identifier() )?
509 ( <HINTS> hints = Properties() )?
510 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (LOOKAHEAD(1) compactionPolicyProperties = Configuration())? )?
511 {
512 ExternalDetailsDecl edd = new ExternalDetailsDecl();
513 edd.setAdapter(adapterName);
514 edd.setProperties(properties);
515 dsetDecl = new DatasetDecl(nameComponents.first,
516 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800517 typeComponents.first,
518 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800519 metaTypeComponents.first,
520 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700521 nodeGroupName != null? new Identifier(nodeGroupName): null,
522 compactionPolicy,
523 compactionPolicyProperties,
524 hints,
525 DatasetType.EXTERNAL,
526 edd,
527 ifNotExists);
528 }
529
Yingyi Bub9169b62016-02-26 21:21:49 -0800530 | (<INTERNAL> | <TEMPORARY> { temp = true; })?
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700531 Dataset() nameComponents = QualifiedName()
Your Namedace5f22016-01-12 14:02:48 -0800532 <LEFTPAREN> typeComponents = TypeName() <RIGHTPAREN>
Yingyi Bub9169b62016-02-26 21:21:49 -0800533 (
534 { String name; }
535 <WITH>
536 name = Identifier()
537 {
538 if(!name.toLowerCase().equals("meta")){
539 throw new ParseException("We can only support one additional associated field called \"meta\".");
540 }
541 }
542 <LEFTPAREN> metaTypeComponents = TypeName() <RIGHTPAREN>
543 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700544 ifNotExists = IfNotExists()
545 primaryKeyFields = PrimaryKey()
546 (<AUTOGENERATED> { autogenerated = true; } )?
547 (<ON> nodeGroupName = Identifier() )?
548 ( <HINTS> hints = Properties() )?
549 ( <USING> <COMPACTION> <POLICY> compactionPolicy = CompactionPolicy() (LOOKAHEAD(1) compactionPolicyProperties = Configuration())? )?
550 ( LOOKAHEAD(2) <WITH> <FILTER> <ON> filterField = NestedField() )?
551 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800552 if(filterField!=null && filterField.first!=0){
553 throw new ParseException("A filter field can only be a field in the main record of the dataset.");
554 }
555 InternalDetailsDecl idd = new InternalDetailsDecl(primaryKeyFields.second,
556 primaryKeyFields.first,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700557 autogenerated,
Yingyi Buc9bfe252016-03-01 00:02:40 -0800558 filterField == null? null : filterField.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700559 temp);
560 dsetDecl = new DatasetDecl(nameComponents.first,
561 nameComponents.second,
Your Namedace5f22016-01-12 14:02:48 -0800562 typeComponents.first,
563 typeComponents.second,
Yingyi Bub9169b62016-02-26 21:21:49 -0800564 metaTypeComponents.first,
565 metaTypeComponents.second,
Yingyi Bu391f09e2015-10-29 13:49:39 -0700566 nodeGroupName != null ? new Identifier(nodeGroupName) : null,
567 compactionPolicy,
568 compactionPolicyProperties,
569 hints,
570 DatasetType.INTERNAL,
571 idd,
572 ifNotExists);
573 }
574 )
575 {
576 return dsetDecl;
577 }
578}
579
580RefreshExternalDatasetStatement RefreshExternalDatasetStatement() throws ParseException:
581{
582 RefreshExternalDatasetStatement redss = new RefreshExternalDatasetStatement();
583 Pair<Identifier,Identifier> nameComponents = null;
584 String datasetName = null;
585}
586{
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700587 <REFRESH> <EXTERNAL> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700588 {
589 redss.setDataverseName(nameComponents.first);
590 redss.setDatasetName(nameComponents.second);
591 return redss;
592 }
593}
594
595RunStatement RunStatement() throws ParseException:
596{
597 String system = null;
598 String tmp;
599 ArrayList<String> parameters = new ArrayList<String>();
600 Pair<Identifier,Identifier> nameComponentsFrom = null;
601 Pair<Identifier,Identifier> nameComponentsTo = null;
602}
603{
604 <RUN> system = Identifier()<LEFTPAREN> ( tmp = Identifier() [<COMMA>]
605 {
606 parameters.add(tmp);
607 }
608 )*<RIGHTPAREN>
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700609 <FROM> Dataset() nameComponentsFrom = QualifiedName()
610 <TO> Dataset() nameComponentsTo = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700611 {
612 return new RunStatement(system, parameters, nameComponentsFrom.first, nameComponentsFrom.second, nameComponentsTo.first, nameComponentsTo.second);
613 }
614}
615
616CreateIndexStatement IndexSpecification() throws ParseException:
617{
618 CreateIndexStatement cis = new CreateIndexStatement();
619 String indexName = null;
620 boolean ifNotExists = false;
621 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -0800622 Pair<Integer, Pair<List<String>, TypeExpression>> fieldPair = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700623 IndexParams indexType = null;
624 boolean enforced = false;
625}
626{
627 <INDEX> indexName = Identifier()
628 ifNotExists = IfNotExists()
629 <ON> nameComponents = QualifiedName()
630 <LEFTPAREN> ( fieldPair = OpenField()
631 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800632 cis.addFieldExprPair(fieldPair.second);
633 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700634 }
635 ) (<COMMA> fieldPair = OpenField()
636 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800637 cis.addFieldExprPair(fieldPair.second);
638 cis.addFieldIndexIndicator(fieldPair.first);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700639 }
640 )* <RIGHTPAREN> ( <TYPE> indexType = IndexType() )? ( <ENFORCED> { enforced = true; } )?
641 {
642 cis.setIndexName(new Identifier(indexName));
643 cis.setIfNotExists(ifNotExists);
644 cis.setDataverseName(nameComponents.first);
645 cis.setDatasetName(nameComponents.second);
646 if (indexType != null) {
647 cis.setIndexType(indexType.type);
648 cis.setGramLength(indexType.gramLength);
649 }
650 cis.setEnforced(enforced);
651 return cis;
652 }
653}
654
655String CompactionPolicy() throws ParseException :
656{
657 String compactionPolicy = null;
658}
659{
660 compactionPolicy = Identifier()
661 {
662 return compactionPolicy;
663 }
664}
665
666String FilterField() throws ParseException :
667{
668 String filterField = null;
669}
670{
671 filterField = Identifier()
672 {
673 return filterField;
674 }
675}
676
677IndexParams IndexType() throws ParseException:
678{
679 IndexType type = null;
680 int gramLength = 0;
681}
682{
683 (<BTREE>
684 {
685 type = IndexType.BTREE;
686 }
687 | <RTREE>
688 {
689 type = IndexType.RTREE;
690 }
691 | <KEYWORD>
692 {
693 type = IndexType.LENGTH_PARTITIONED_WORD_INVIX;
694 }
Taewoo Kimc49405a2017-01-04 00:30:43 -0800695 |<FULLTEXT>
696 {
697 type = IndexType.SINGLE_PARTITION_WORD_INVIX;
698 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700699 | <NGRAM> <LEFTPAREN> <INTEGER_LITERAL>
700 {
701 type = IndexType.LENGTH_PARTITIONED_NGRAM_INVIX;
702 gramLength = Integer.valueOf(token.image);
703 }
704 <RIGHTPAREN>)
705 {
706 return new IndexParams(type, gramLength);
707 }
708}
709
710CreateDataverseStatement DataverseSpecification() throws ParseException :
711{
712 String dvName = null;
713 boolean ifNotExists = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700714}
715{
716 <DATAVERSE> dvName = Identifier()
717 ifNotExists = IfNotExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700718 {
Till Westmannf6028272016-09-30 14:34:42 -0700719 return new CreateDataverseStatement(new Identifier(dvName), null, ifNotExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700720 }
721}
722
723CreateFunctionStatement FunctionSpecification() throws ParseException:
724{
725 FunctionSignature signature;
726 boolean ifNotExists = false;
727 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
728 String functionBody;
729 VarIdentifier var = null;
730 Expression functionBodyExpr;
731 Token beginPos;
732 Token endPos;
733 FunctionName fctName = null;
734
735 createNewScope();
736}
737{
738 <FUNCTION> fctName = FunctionName()
739 ifNotExists = IfNotExists()
740 paramList = ParameterList()
741 <LEFTBRACE>
742 {
743 beginPos = token;
744 }
745 functionBodyExpr = Expression() <RIGHTBRACE>
746 {
747 endPos = token;
748 functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
749 // TODO use fctName.library
750 signature = new FunctionSignature(fctName.dataverse, fctName.function, paramList.size());
751 getCurrentScope().addFunctionDescriptor(signature, false);
752 removeCurrentScope();
753 return new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
754 }
755}
756
757CreateFeedStatement FeedSpecification() throws ParseException:
758{
759 Pair<Identifier,Identifier> nameComponents = null;
760 boolean ifNotExists = false;
761 String adapterName = null;
762 Map<String,String> properties = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700763 CreateFeedStatement cfs = null;
764 Pair<Identifier,Identifier> sourceNameComponents = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700765}
766{
Abdullah Alamoudifff200c2017-02-18 20:32:14 -0800767 <FEED> nameComponents = QualifiedName() ifNotExists = IfNotExists()
768 <USING> adapterName = AdapterName() properties = Configuration()
769 {
770 cfs = new CreateFeedStatement(nameComponents, adapterName, properties, ifNotExists);
771 return cfs;
772 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700773}
774
775CreateFeedPolicyStatement FeedPolicySpecification() throws ParseException:
776{
Michael Blowd6cf6412016-06-30 02:44:35 -0400777 String policyName = null;
778 String basePolicyName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700779 String sourcePolicyFile = null;
780 String definition = null;
781 boolean ifNotExists = false;
782 Map<String,String> properties = null;
783 CreateFeedPolicyStatement cfps = null;
784}
785{
786 (
787 <INGESTION> <POLICY> policyName = Identifier() ifNotExists = IfNotExists()
Yingyi Bu6d57e492016-06-06 21:24:42 -0700788 <FROM>
789 (<POLICY> basePolicyName = Identifier() properties = Configuration() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700790 {
791 cfps = new CreateFeedPolicyStatement(policyName,
792 basePolicyName, properties, definition, ifNotExists);
793 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300794 | <PATH> sourcePolicyFile = ConstantString() (<DEFINITION> definition = ConstantString())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700795 {
796 cfps = new CreateFeedPolicyStatement(policyName, sourcePolicyFile, definition, ifNotExists);
797 }
Yingyi Bu6d57e492016-06-06 21:24:42 -0700798 )
Yingyi Bu391f09e2015-10-29 13:49:39 -0700799 )
800 {
801 return cfps;
802 }
803}
804
805
806
807List<VarIdentifier> ParameterList() throws ParseException:
808{
809 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
810 VarIdentifier var = null;
811}
812{
813 <LEFTPAREN> (<IDENTIFIER>
814 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700815 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700816 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700817 }
818 (<COMMA> <IDENTIFIER>
819 {
Yingyi Buacc12a92016-03-26 17:25:05 -0700820 var = SqlppVariableUtil.toInternalVariableIdentifier(token.image);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700821 paramList.add(var);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700822 }
823 )*)? <RIGHTPAREN>
824 {
825 return paramList;
826 }
827}
828
829boolean IfNotExists() throws ParseException:
830{
831}
832{
Yingyi Budaa549c2016-06-28 22:30:52 -0700833 ( LOOKAHEAD(1) <IF> <NOT> <EXISTS>
Yingyi Bu391f09e2015-10-29 13:49:39 -0700834 {
835 return true;
836 }
837 )?
838 {
839 return false;
840 }
841}
842
Xikui Wang9d63f622017-05-18 17:50:44 -0700843void ApplyFunction(List<FunctionSignature> funcSigs) throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700844{
845 FunctionName functioName = null;
Xikui Wang261dc6d2017-03-29 21:23:15 -0700846 String fqFunctionName = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700847}
848{
849 <APPLY> <FUNCTION> functioName = FunctionName()
850 {
Xikui Wang261dc6d2017-03-29 21:23:15 -0700851 fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
852 funcSigs.add(new FunctionSignature(functioName.dataverse, fqFunctionName, 1));
853 }
854 (
855 <COMMA> functioName = FunctionName()
856 {
857 fqFunctionName = functioName.library == null ? functioName.function : functioName.library + "#" + functioName.function;
858 funcSigs.add(new FunctionSignature(functioName.dataverse, fqFunctionName, 1));
859 }
860 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -0700861}
862
863String GetPolicy() throws ParseException:
864{
865 String policy = null;
866}
867{
868 <USING> <POLICY> policy = Identifier()
869 {
870 return policy;
871 }
872
873}
874
875FunctionSignature FunctionSignature() throws ParseException:
876{
877 FunctionName fctName = null;
878 int arity = 0;
879}
880{
881 fctName = FunctionName() <ATT> <INTEGER_LITERAL>
882 {
883 arity = new Integer(token.image);
884 if (arity < 0 && arity != FunctionIdentifier.VARARGS) {
885 throw new ParseException(" invalid arity:" + arity);
886 }
887
888 // TODO use fctName.library
889 String fqFunctionName = fctName.library == null ? fctName.function : fctName.library + "#" + fctName.function;
890 return new FunctionSignature(fctName.dataverse, fqFunctionName, arity);
891 }
892}
893
Yingyi Buc9bfe252016-03-01 00:02:40 -0800894Pair<List<Integer>, List<List<String>>> PrimaryKey() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -0700895{
Yingyi Buc9bfe252016-03-01 00:02:40 -0800896 Pair<Integer, List<String>> tmp = null;
897 List<Integer> keyFieldSourceIndicators = new ArrayList<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -0700898 List<List<String>> primaryKeyFields = new ArrayList<List<String>>();
899}
900{
901 <PRIMARY> <KEY> tmp = NestedField()
902 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800903 keyFieldSourceIndicators.add(tmp.first);
904 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700905 }
906 ( <COMMA> tmp = NestedField()
907 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800908 keyFieldSourceIndicators.add(tmp.first);
909 primaryKeyFields.add(tmp.second);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700910 }
911 )*
912 {
Yingyi Buc9bfe252016-03-01 00:02:40 -0800913 return new Pair<List<Integer>, List<List<String>>> (keyFieldSourceIndicators, primaryKeyFields);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700914 }
915}
916
917Statement DropStatement() throws ParseException:
918{
919 String id = null;
920 Pair<Identifier,Identifier> pairId = null;
921 Triple<Identifier,Identifier,Identifier> tripleId = null;
922 FunctionSignature funcSig = null;
923 boolean ifExists = false;
924 Statement stmt = null;
925}
926{
927 <DROP>
928 (
Yingyi Bu1c0fff52016-03-25 20:23:30 -0700929 Dataset() pairId = QualifiedName() ifExists = IfExists()
Yingyi Bu391f09e2015-10-29 13:49:39 -0700930 {
Yingyi Buab817482016-08-19 21:29:31 -0700931 stmt = new DropDatasetStatement(pairId.first, pairId.second, ifExists);
Yingyi Bu391f09e2015-10-29 13:49:39 -0700932 }
933 | <INDEX> tripleId = DoubleQualifiedName() ifExists = IfExists()
934 {
935 stmt = new IndexDropStatement(tripleId.first, tripleId.second, tripleId.third, ifExists);
936 }
937 | <NODEGROUP> id = Identifier() ifExists = IfExists()
938 {
939 stmt = new NodeGroupDropStatement(new Identifier(id), ifExists);
940 }
941 | <TYPE> pairId = TypeName() ifExists = IfExists()
942 {
943 stmt = new TypeDropStatement(pairId.first, pairId.second, ifExists);
944 }
945 | <DATAVERSE> id = Identifier() ifExists = IfExists()
946 {
947 stmt = new DataverseDropStatement(new Identifier(id), ifExists);
948 }
949 | <FUNCTION> funcSig = FunctionSignature() ifExists = IfExists()
950 {
951 stmt = new FunctionDropStatement(funcSig, ifExists);
952 }
953 | <FEED> pairId = QualifiedName() ifExists = IfExists()
954 {
955 stmt = new FeedDropStatement(pairId.first, pairId.second, ifExists);
956 }
Abdullah Alamoudi5dc73ed2016-07-28 05:03:13 +0300957 | <INGESTION> <POLICY> pairId = QualifiedName() ifExists = IfExists()
958 {
959 stmt = new FeedPolicyDropStatement(pairId.first, pairId.second, ifExists);
960 }
Yingyi Bu391f09e2015-10-29 13:49:39 -0700961 )
962 {
963 return stmt;
964 }
965}
966
967boolean IfExists() throws ParseException :
968{
969}
970{
971 ( LOOKAHEAD(1) <IF> <EXISTS>
972 {
973 return true;
974 }
975 )?
976 {
977 return false;
978 }
979}
980
981InsertStatement InsertStatement() throws ParseException:
982{
983 Pair<Identifier,Identifier> nameComponents = null;
Yingyi Bucb5bf332017-01-02 22:19:50 -0800984 VariableExpr var = null;
985 Query query = null;
986 Expression returnExpression = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -0700987}
988{
Yingyi Bucb5bf332017-01-02 22:19:50 -0800989 <INSERT> <INTO> nameComponents = QualifiedName() (<AS> var = Variable())?
990 query = Query(false)
991 ( <RETURNING> returnExpression = Expression())?
Yingyi Bu391f09e2015-10-29 13:49:39 -0700992 {
Yingyi Bucb5bf332017-01-02 22:19:50 -0800993 if (returnExpression != null && var == null) {
994 var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
995 }
Yingyi Bucaea8f02015-11-16 15:12:15 -0800996 query.setTopLevel(true);
Yingyi Bucb5bf332017-01-02 22:19:50 -0800997 return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
998 returnExpression);
999 }
1000}
1001
1002UpsertStatement UpsertStatement() throws ParseException:
1003{
1004 Pair<Identifier,Identifier> nameComponents = null;
1005 VariableExpr var = null;
1006 Query query = null;
1007 Expression returnExpression = null;
1008}
1009{
1010 <UPSERT> <INTO> nameComponents = QualifiedName() (<AS> var = Variable())?
1011 query = Query(false)
1012 ( <RETURNING> returnExpression = Expression())?
1013 {
1014 if (returnExpression != null && var == null) {
1015 var = ExpressionToVariableUtil.getGeneratedVariable(query.getBody(), true);
1016 }
1017 query.setTopLevel(true);
1018 return new UpsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter(), var,
1019 returnExpression);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001020 }
1021}
1022
1023DeleteStatement DeleteStatement() throws ParseException:
1024{
Yingyi Bu20e085b2016-07-06 12:57:27 -07001025 VariableExpr varExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001026 Expression condition = null;
1027 Pair<Identifier, Identifier> nameComponents;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001028}
1029{
Yingyi Bu20e085b2016-07-06 12:57:27 -07001030 <DELETE>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001031 <FROM> nameComponents = QualifiedName()
Yingyi Bu20e085b2016-07-06 12:57:27 -07001032 ((<AS>)? varExpr = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001033 (<WHERE> condition = Expression())?
Yingyi Bu20e085b2016-07-06 12:57:27 -07001034 {
Yingyi Bu20e085b2016-07-06 12:57:27 -07001035 if(varExpr == null){
1036 varExpr = new VariableExpr();
1037 VarIdentifier var = SqlppVariableUtil.toInternalVariableIdentifier(nameComponents.second.getValue());
1038 varExpr.setVar(var);
1039 }
1040 return new DeleteStatement(varExpr, nameComponents.first, nameComponents.second,
Abdullah Alamoudi6eb01752017-04-01 21:51:19 -07001041 condition, getVarCounter());
Yingyi Bu20e085b2016-07-06 12:57:27 -07001042 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001043}
1044
1045UpdateStatement UpdateStatement() throws ParseException:
1046{
1047 VariableExpr vars;
1048 Expression target;
1049 Expression condition;
1050 UpdateClause uc;
1051 List<UpdateClause> ucs = new ArrayList<UpdateClause>();
1052}
1053{
1054 <UPDATE> vars = Variable() <IN> target = Expression()
1055 <WHERE> condition = Expression()
1056 <LEFTPAREN> (uc = UpdateClause()
1057 {
1058 ucs.add(uc);
1059 }
1060 (<COMMA> uc = UpdateClause()
1061 {
1062 ucs.add(uc);
1063 }
1064 )*) <RIGHTPAREN>
1065 {
1066 return new UpdateStatement(vars, target, condition, ucs);
1067 }
1068}
1069
1070UpdateClause UpdateClause() throws ParseException:
1071{
1072 Expression target = null;
1073 Expression value = null ;
1074 InsertStatement is = null;
1075 DeleteStatement ds = null;
1076 UpdateStatement us = null;
1077 Expression condition = null;
1078 UpdateClause ifbranch = null;
1079 UpdateClause elsebranch = null;
1080}
1081{
1082 (<SET> target = Expression() <EQ> value = Expression()
1083 | is = InsertStatement()
1084 | ds = DeleteStatement()
1085 | us = UpdateStatement()
1086 | <IF> <LEFTPAREN> condition = Expression() <RIGHTPAREN>
1087 <THEN> ifbranch = UpdateClause()
1088 [LOOKAHEAD(1) <ELSE> elsebranch = UpdateClause()]
1089 {
1090 return new UpdateClause(target, value, is, ds, us, condition, ifbranch, elsebranch);
1091 }
1092 )
1093}
1094
1095Statement SetStatement() throws ParseException:
1096{
1097 String pn = null;
1098 String pv = null;
1099}
1100{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001101 <SET> pn = Identifier() pv = ConstantString()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001102 {
1103 return new SetStatement(pn, pv);
1104 }
1105}
1106
1107Statement WriteStatement() throws ParseException:
1108{
1109 String nodeName = null;
1110 String fileName = null;
1111 Query query;
1112 String writerClass = null;
1113 Pair<Identifier,Identifier> nameComponents = null;
1114}
1115{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001116 <WRITE> <OUTPUT> <TO> nodeName = Identifier() <COLON> fileName = ConstantString()
1117 ( <USING> writerClass = ConstantString() )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001118 {
1119 return new WriteStatement(new Identifier(nodeName), fileName, writerClass);
1120 }
1121}
1122
1123LoadStatement LoadStatement() throws ParseException:
1124{
1125 Identifier dataverseName = null;
1126 Identifier datasetName = null;
1127 boolean alreadySorted = false;
1128 String adapterName;
1129 Map<String,String> properties;
1130 Pair<Identifier,Identifier> nameComponents = null;
1131}
1132{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001133 <LOAD> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001134 {
1135 dataverseName = nameComponents.first;
1136 datasetName = nameComponents.second;
1137 }
1138 <USING> adapterName = AdapterName() properties = Configuration()
1139 (<PRESORTED>
1140 {
1141 alreadySorted = true;
1142 }
1143 )?
1144 {
1145 return new LoadStatement(dataverseName, datasetName, adapterName, properties, alreadySorted);
1146 }
1147}
1148
1149
1150String AdapterName() throws ParseException :
1151{
1152 String adapterName = null;
1153}
1154{
1155 adapterName = Identifier()
1156 {
1157 return adapterName;
1158 }
1159}
1160
1161Statement CompactStatement() throws ParseException:
1162{
1163 Pair<Identifier,Identifier> nameComponents = null;
1164 Statement stmt = null;
1165}
1166{
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001167 <COMPACT> Dataset() nameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001168 {
1169 stmt = new CompactStatement(nameComponents.first, nameComponents.second);
1170 }
1171 {
1172 return stmt;
1173 }
1174}
1175
Yingyi Buab817482016-08-19 21:29:31 -07001176Statement ConnectionStatement() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001177{
1178 Pair<Identifier,Identifier> feedNameComponents = null;
1179 Pair<Identifier,Identifier> datasetNameComponents = null;
1180
1181 Map<String,String> configuration = null;
1182 Statement stmt = null;
1183 String policy = null;
1184}
1185{
1186 (
Yingyi Buab817482016-08-19 21:29:31 -07001187 <CONNECT> stmt = ConnectStatement()
1188 | <DISCONNECT> stmt = DisconnectStatement()
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001189 | <START> stmt = StartStatement()
1190 | <STOP> stmt = StopStatement()
Yingyi Buab817482016-08-19 21:29:31 -07001191 )
1192 {
1193 return stmt;
1194 }
1195}
1196
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001197Statement StartStatement() throws ParseException:
1198{
1199 Pair<Identifier,Identifier> feedNameComponents = null;
1200
1201 Statement stmt = null;
1202}
1203{
1204 <FEED> feedNameComponents = QualifiedName()
1205 {
1206 stmt = new StartFeedStatement (feedNameComponents);
1207 return stmt;
1208 }
1209}
1210
1211Statement StopStatement () throws ParseException:
1212{
1213 Pair<Identifier,Identifier> feedNameComponents = null;
1214
1215 Statement stmt = null;
1216}
1217{
1218 <FEED> feedNameComponents = QualifiedName()
1219 {
1220 stmt = new StopFeedStatement (feedNameComponents);
1221 return stmt;
1222 }
1223}
1224
1225
Yingyi Buab817482016-08-19 21:29:31 -07001226Statement DisconnectStatement() throws ParseException:
1227{
1228 Pair<Identifier,Identifier> feedNameComponents = null;
1229 Pair<Identifier,Identifier> datasetNameComponents = null;
1230
1231 Map<String,String> configuration = null;
1232 Statement stmt = null;
1233 String policy = null;
1234}
1235{
1236 (
1237 <FEED> feedNameComponents = QualifiedName() <FROM> Dataset() datasetNameComponents = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001238 {
1239 stmt = new DisconnectFeedStatement(feedNameComponents, datasetNameComponents);
1240 }
1241 )
Yingyi Buab817482016-08-19 21:29:31 -07001242 {
1243 return stmt;
1244 }
1245}
1246
1247Statement ConnectStatement() throws ParseException:
1248{
1249 Pair<Identifier,Identifier> feedNameComponents = null;
1250 Pair<Identifier,Identifier> datasetNameComponents = null;
1251
1252 Map<String,String> configuration = null;
Xikui Wang9d63f622017-05-18 17:50:44 -07001253 List<FunctionSignature> appliedFunctions = new ArrayList<FunctionSignature>();
Yingyi Buab817482016-08-19 21:29:31 -07001254 Statement stmt = null;
1255 String policy = null;
1256}
1257{
1258 (
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001259 <FEED> feedNameComponents = QualifiedName() <TO> Dataset() datasetNameComponents = QualifiedName()
Xikui Wang9d63f622017-05-18 17:50:44 -07001260 (ApplyFunction(appliedFunctions))? (policy = GetPolicy())?
Yingyi Buab817482016-08-19 21:29:31 -07001261 {
Xikui Wang261dc6d2017-03-29 21:23:15 -07001262 stmt = new ConnectFeedStatement(feedNameComponents, datasetNameComponents, appliedFunctions,
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08001263 policy, getVarCounter());
Yingyi Buab817482016-08-19 21:29:31 -07001264 }
1265 )
1266 {
1267 return stmt;
1268 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001269}
1270
1271Map<String,String> Configuration() throws ParseException :
1272{
1273 Map<String,String> configuration = new LinkedHashMap<String,String>();
1274 Pair<String, String> keyValuePair = null;
1275}
1276{
1277 <LEFTPAREN> ( keyValuePair = KeyValuePair()
1278 {
1279 configuration.put(keyValuePair.first, keyValuePair.second);
1280 }
1281 ( <COMMA> keyValuePair = KeyValuePair()
1282 {
1283 configuration.put(keyValuePair.first, keyValuePair.second);
1284 }
1285 )* )? <RIGHTPAREN>
1286 {
1287 return configuration;
1288 }
1289}
1290
1291Pair<String, String> KeyValuePair() throws ParseException:
1292{
1293 String key;
1294 String value;
1295}
1296{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001297 <LEFTPAREN> key = ConstantString() <EQ> value = ConstantString() <RIGHTPAREN>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001298 {
1299 return new Pair<String, String>(key, value);
1300 }
1301}
1302
1303Map<String,String> Properties() throws ParseException:
1304{
1305 Map<String,String> properties = new HashMap<String,String>();
1306 Pair<String, String> property;
1307}
1308{
1309 (LOOKAHEAD(1) <LEFTPAREN> property = Property()
1310 {
1311 properties.put(property.first, property.second);
1312 }
1313 ( <COMMA> property = Property()
1314 {
1315 properties.put(property.first, property.second);
1316 }
1317 )* <RIGHTPAREN> )?
1318 {
1319 return properties;
1320 }
1321}
1322
1323Pair<String, String> Property() throws ParseException:
1324{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001325 String key = null;
1326 String value = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001327}
1328{
Yingyi Bu6d57e492016-06-06 21:24:42 -07001329 (key = Identifier() | key = StringLiteral())
1330 <EQ>
1331 ( value = ConstantString() | <INTEGER_LITERAL>
Yingyi Bu391f09e2015-10-29 13:49:39 -07001332 {
1333 try {
1334 value = "" + Long.valueOf(token.image);
1335 } catch (NumberFormatException nfe) {
1336 throw new ParseException("inapproriate value: " + token.image);
1337 }
1338 }
1339 )
1340 {
1341 return new Pair<String, String>(key.toUpperCase(), value);
1342 }
1343}
1344
1345TypeExpression IndexedTypeExpr() throws ParseException:
1346{
1347 TypeExpression typeExpr = null;
1348}
1349{
1350 (
1351 typeExpr = TypeReference()
1352 | typeExpr = OrderedListTypeDef()
1353 | typeExpr = UnorderedListTypeDef()
1354 )
1355 {
1356 return typeExpr;
1357 }
1358}
1359
1360TypeExpression TypeExpr() throws ParseException:
1361{
1362 TypeExpression typeExpr = null;
1363}
1364{
1365 (
1366 typeExpr = RecordTypeDef()
1367 | typeExpr = TypeReference()
1368 | typeExpr = OrderedListTypeDef()
1369 | typeExpr = UnorderedListTypeDef()
1370 )
1371 {
1372 return typeExpr;
1373 }
1374}
1375
1376RecordTypeDefinition RecordTypeDef() throws ParseException:
1377{
1378 RecordTypeDefinition recType = new RecordTypeDefinition();
1379 RecordTypeDefinition.RecordKind recordKind = null;
1380}
1381{
1382 ( <CLOSED> { recordKind = RecordTypeDefinition.RecordKind.CLOSED; }
1383 | <OPEN> { recordKind = RecordTypeDefinition.RecordKind.OPEN; } )?
1384 <LEFTBRACE>
1385 {
1386 String hint = getHint(token);
1387 if (hint != null) {
1388 String splits[] = hint.split(" +");
1389 if (splits[0].equals(GEN_FIELDS_HINT)) {
1390 if (splits.length != 5) {
1391 throw new ParseException("Expecting: /*+ gen-fields <type> <min> <max> <prefix>*/");
1392 }
1393 if (!splits[1].equals("int")) {
1394 throw new ParseException("The only supported type for gen-fields is int.");
1395 }
1396 UndeclaredFieldsDataGen ufdg = new UndeclaredFieldsDataGen(UndeclaredFieldsDataGen.Type.INT,
1397 Integer.parseInt(splits[2]), Integer.parseInt(splits[3]), splits[4]);
1398 recType.setUndeclaredFieldsDataGen(ufdg);
1399 }
1400 }
1401
1402 }
1403 (
1404 RecordField(recType)
1405 ( <COMMA> RecordField(recType) )*
1406 )?
1407 <RIGHTBRACE>
1408 {
1409 if (recordKind == null) {
1410 recordKind = RecordTypeDefinition.RecordKind.OPEN;
1411 }
1412 recType.setRecordKind(recordKind);
1413 return recType;
1414 }
1415}
1416
1417void RecordField(RecordTypeDefinition recType) throws ParseException:
1418{
1419 String fieldName;
1420 TypeExpression type = null;
1421 boolean nullable = false;
1422}
1423{
1424 fieldName = Identifier()
1425 {
1426 String hint = getHint(token);
1427 IRecordFieldDataGen rfdg = hint != null ? parseFieldDataGen(hint) : null;
1428 }
1429 <COLON> type = TypeExpr() (<QUES> { nullable = true; } )?
1430 {
1431 recType.addField(fieldName, type, nullable, rfdg);
1432 }
1433}
1434
1435TypeReferenceExpression TypeReference() throws ParseException:
1436{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001437 Pair<Identifier,Identifier> id = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001438}
1439{
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001440 id = QualifiedName()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001441 {
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001442 if (id.first == null && id.second.getValue().equalsIgnoreCase("int")) {
1443 id.second.setValue("int64");
Yingyi Bu391f09e2015-10-29 13:49:39 -07001444 }
1445
Abdullah Alamoudie4b318f2016-09-19 13:31:25 +03001446 return new TypeReferenceExpression(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001447 }
1448}
1449
1450OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
1451{
1452 TypeExpression type = null;
1453}
1454{
1455 <LEFTBRACKET>
1456 ( type = TypeExpr() )
1457 <RIGHTBRACKET>
1458 {
1459 return new OrderedListTypeDefinition(type);
1460 }
1461}
1462
1463
1464UnorderedListTypeDefinition UnorderedListTypeDef() throws ParseException:
1465{
1466 TypeExpression type = null;
1467}
1468{
1469 <LEFTDBLBRACE>
1470 ( type = TypeExpr() )
1471 <RIGHTDBLBRACE>
1472 {
1473 return new UnorderedListTypeDefinition(type);
1474 }
1475}
1476
1477FunctionName FunctionName() throws ParseException:
1478{
1479 String first = null;
1480 String second = null;
1481 String third = null;
1482 boolean secondAfterDot = false;
1483}
1484{
Michael Blowd6cf6412016-06-30 02:44:35 -04001485 first = Identifier()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001486 {
1487 FunctionName result = new FunctionName();
1488 result.hint = getHint(token);
Michael Blowd6cf6412016-06-30 02:44:35 -04001489 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001490 ( <DOT> second = Identifier()
1491 {
1492 secondAfterDot = true;
1493 }
1494 (<SHARP> third = Identifier())? | <SHARP> second = Identifier() )?
1495 {
1496 if (second == null) {
1497 result.dataverse = defaultDataverse;
1498 result.library = null;
1499 result.function = first;
1500 } else if (third == null) {
1501 if (secondAfterDot) {
1502 result.dataverse = first;
1503 result.library = null;
1504 result.function = second;
1505 } else {
1506 result.dataverse = defaultDataverse;
1507 result.library = first;
1508 result.function = second;
1509 }
1510 } else {
1511 result.dataverse = first;
1512 result.library = second;
1513 result.function = third;
1514 }
1515
1516 if (result.function.equalsIgnoreCase("int")) {
1517 result.function = "int64";
1518 }
1519 return result;
1520 }
1521}
1522
1523Pair<Identifier,Identifier> TypeName() throws ParseException:
1524{
1525 Pair<Identifier,Identifier> name = null;
1526}
1527{
1528 name = QualifiedName()
1529 {
1530 if (name.first == null) {
1531 name.first = new Identifier(defaultDataverse);
1532 }
1533 return name;
1534 }
1535}
1536
1537String Identifier() throws ParseException:
1538{
1539 String lit = null;
1540}
1541{
1542 (<IDENTIFIER>
1543 {
1544 return token.image;
1545 }
1546 | lit = QuotedString()
1547 {
1548 return lit;
1549 }
1550 )
1551}
1552
Yingyi Bu1c0fff52016-03-25 20:23:30 -07001553void Dataset() throws ParseException:
1554{
1555}
1556{
1557 (<DATASET>|<COLLECTION>)
1558}
1559
Yingyi Buc9bfe252016-03-01 00:02:40 -08001560Pair<Integer, Pair<List<String>, TypeExpression>> OpenField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001561{
1562 TypeExpression fieldType = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001563 Pair<Integer, List<String>> fieldList = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001564}
1565{
1566 fieldList = NestedField()
Mike Careyaf8e19d2016-08-25 17:55:17 -07001567 ( <COLON> fieldType = IndexedTypeExpr() <QUES>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07001568 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001569 return new Pair<Integer, Pair<List<String>, TypeExpression>>
1570 (fieldList.first, new Pair<List<String>, TypeExpression>(fieldList.second, fieldType));
Yingyi Bu391f09e2015-10-29 13:49:39 -07001571 }
1572}
1573
Yingyi Buc9bfe252016-03-01 00:02:40 -08001574Pair<Integer, List<String>> NestedField() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001575{
1576 List<String> exprList = new ArrayList<String>();
1577 String lit = null;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001578 int source = 0;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001579}
1580{
1581 lit = Identifier()
1582 {
Yingyi Bub9169b62016-02-26 21:21:49 -08001583 boolean meetParens = false;
1584 }
1585 (
Yingyi Buc9bfe252016-03-01 00:02:40 -08001586 LOOKAHEAD(1)
Yingyi Bub9169b62016-02-26 21:21:49 -08001587 <LEFTPAREN><RIGHTPAREN>
1588 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001589 if(!lit.toLowerCase().equals("meta")){
Yingyi Bub9169b62016-02-26 21:21:49 -08001590 throw new ParseException("The string before () has to be \"meta\".");
1591 }
1592 meetParens = true;
Yingyi Buc9bfe252016-03-01 00:02:40 -08001593 source = 1;
Yingyi Bub9169b62016-02-26 21:21:49 -08001594 }
1595 )?
1596 {
1597 if(!meetParens){
1598 exprList.add(lit);
1599 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001600 }
1601 (<DOT>
1602 lit = Identifier()
1603 {
1604 exprList.add(lit);
1605 }
1606 )*
1607 {
Yingyi Buc9bfe252016-03-01 00:02:40 -08001608 return new Pair<Integer, List<String>>(source, exprList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001609 }
1610}
1611
Yingyi Bu6d57e492016-06-06 21:24:42 -07001612String ConstantString() throws ParseException:
1613{
1614 String value = null;
1615}
1616{
1617 (value = QuotedString() | value = StringLiteral())
1618 {
1619 return value;
1620 }
1621}
1622
Yingyi Bu391f09e2015-10-29 13:49:39 -07001623
1624String QuotedString() throws ParseException:
1625{
1626}
1627{
1628 <QUOTED_STRING>
1629 {
1630 return removeQuotesAndEscapes(token.image);
1631 }
1632}
1633
1634
1635String StringLiteral() throws ParseException:
1636{
1637}
1638{
1639 <STRING_LITERAL>
1640 {
1641 return removeQuotesAndEscapes(token.image);
1642 }
1643}
1644
1645Pair<Identifier,Identifier> QualifiedName() throws ParseException:
1646{
1647 String first = null;
1648 String second = null;
1649}
1650{
1651 first = Identifier() (<DOT> second = Identifier())?
1652 {
1653 Identifier id1 = null;
1654 Identifier id2 = null;
1655 if (second == null) {
1656 id2 = new Identifier(first);
1657 } else
1658 {
1659 id1 = new Identifier(first);
1660 id2 = new Identifier(second);
1661 }
1662 return new Pair<Identifier,Identifier>(id1, id2);
1663 }
1664}
1665
1666Triple<Identifier,Identifier,Identifier> DoubleQualifiedName() throws ParseException:
1667{
1668 String first = null;
1669 String second = null;
1670 String third = null;
1671}
1672{
1673 first = Identifier() <DOT> second = Identifier() (<DOT> third = Identifier())?
1674 {
1675 Identifier id1 = null;
1676 Identifier id2 = null;
1677 Identifier id3 = null;
1678 if (third == null) {
1679 id2 = new Identifier(first);
1680 id3 = new Identifier(second);
1681 } else {
1682 id1 = new Identifier(first);
1683 id2 = new Identifier(second);
1684 id3 = new Identifier(third);
1685 }
1686 return new Triple<Identifier,Identifier,Identifier>(id1, id2, id3);
1687 }
1688}
1689
1690FunctionDecl FunctionDeclaration() throws ParseException:
1691{
1692 FunctionDecl funcDecl;
1693 FunctionSignature signature;
1694 String functionName;
1695 List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
1696 Expression funcBody;
1697 createNewScope();
1698}
1699{
1700 <DECLARE> <FUNCTION> functionName = Identifier()
1701 paramList = ParameterList()
1702 <LEFTBRACE> funcBody = Expression() <RIGHTBRACE>
1703 {
1704 signature = new FunctionSignature(defaultDataverse, functionName, paramList.size());
1705 getCurrentScope().addFunctionDescriptor(signature, false);
1706 funcDecl = new FunctionDecl(signature, paramList, funcBody);
1707 removeCurrentScope();
1708 return funcDecl;
1709 }
1710}
1711
Till Westmannef3f0272016-07-27 18:34:01 -07001712Query ExplainStatement() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07001713{
Till Westmannef3f0272016-07-27 18:34:01 -07001714 Query query;
1715}
1716{
Till Westmann516d1a82016-08-02 14:45:53 -07001717 <EXPLAIN> query = Query(true)
Till Westmannef3f0272016-07-27 18:34:01 -07001718 {
1719 return query;
1720 }
1721}
1722
1723Query Query(boolean explain) throws ParseException:
1724{
1725 Query query = new Query(explain);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001726 Expression expr;
1727}
1728{
1729 (
1730 expr = Expression()
1731 |
1732 expr = SelectExpression(false)
1733 )
1734 {
1735 query.setBody(expr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001736 return query;
1737 }
1738}
1739
1740
1741
1742Expression Expression():
1743{
1744 Expression expr = null;
1745 Expression exprP = null;
1746}
1747{
1748(
1749 LOOKAHEAD(2)
1750 expr = OperatorExpr()
Yingyi Buc8c067c2016-07-25 23:37:19 -07001751 | expr = CaseExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001752 | expr = QuantifiedExpression()
1753)
1754 {
1755 return (exprP==null) ? expr : exprP;
1756 }
1757}
1758
1759
1760
1761Expression OperatorExpr()throws ParseException:
1762{
1763 OperatorExpr op = null;
1764 Expression operand = null;
1765}
1766{
1767 operand = AndExpr()
1768 (
1769
1770 <OR>
1771 {
1772 if (op == null) {
1773 op = new OperatorExpr();
1774 op.addOperand(operand);
1775 op.setCurrentop(true);
1776 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001777 try{
1778 op.addOperator(token.image.toLowerCase());
1779 } catch (Exception e){
1780 throw new ParseException(e.getMessage());
1781 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001782 }
1783
1784 operand = AndExpr()
1785 {
1786 op.addOperand(operand);
1787 }
1788
1789 )*
1790
1791 {
1792 return op==null? operand: op;
1793 }
1794}
1795
1796Expression AndExpr()throws ParseException:
1797{
1798 OperatorExpr op = null;
1799 Expression operand = null;
1800}
1801{
Yingyi Bu196db5d2016-07-15 19:07:20 -07001802 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001803 (
1804
1805 <AND>
1806 {
1807 if (op == null) {
1808 op = new OperatorExpr();
1809 op.addOperand(operand);
Yingyi Bu196db5d2016-07-15 19:07:20 -07001810 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001811 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001812 try{
1813 op.addOperator(token.image.toLowerCase());
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001814 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001815 throw new ParseException(e.getMessage());
1816 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001817 }
1818
Yingyi Bu196db5d2016-07-15 19:07:20 -07001819 operand = NotExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001820 {
1821 op.addOperand(operand);
1822 }
1823
1824 )*
1825
1826 {
1827 return op==null? operand: op;
1828 }
1829}
1830
Yingyi Bu196db5d2016-07-15 19:07:20 -07001831Expression NotExpr()throws ParseException:
1832{
1833 Expression inputExpr;
1834 boolean not = false;
1835}
1836{
1837 (<NOT> { not = true; } )? inputExpr = RelExpr()
1838 {
1839 if(not){
1840 FunctionSignature signature = new FunctionSignature(null, "not", 1);
1841 return new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(inputExpr)));
1842 } else {
1843 return inputExpr;
1844 }
1845 }
1846}
Yingyi Bu391f09e2015-10-29 13:49:39 -07001847
1848Expression RelExpr()throws ParseException:
1849{
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001850 boolean not = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07001851 OperatorExpr op = null;
1852 Expression operand = null;
1853 boolean broadcast = false;
1854 IExpressionAnnotation annotation = null;
1855}
1856{
Yingyi Bu6c638342016-09-02 17:54:34 -07001857 operand = BetweenExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07001858
1859 (
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001860 LOOKAHEAD(2)( <LT> | <GT> | <LE> | <GE> | <EQ> | <NE> | <LG> |<SIMILAR> | (<NOT> { not = true; })? <IN>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07001861 {
1862 String mhint = getHint(token);
1863 if (mhint != null) {
1864 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001865 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1866 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1867 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
Yingyi Buea4ec722016-11-04 01:26:16 -07001868 } else if (mhint.equals(BROADCAST_JOIN_HINT)) {
1869 broadcast = true;
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001870 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001871 }
Yingyi Buea4ec722016-11-04 01:26:16 -07001872
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001873 String operator = token.image.toLowerCase();
Yingyi Bu4a4b8962016-09-16 12:09:11 -07001874 if (operator.equals("<>")){
1875 operator = "!=";
1876 }
1877 if (not) {
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001878 operator = "not_" + operator;
1879 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001880 if (op == null) {
1881 op = new OperatorExpr();
Yingyi Buea4ec722016-11-04 01:26:16 -07001882 op.addOperand(operand, false); // broadcast is always for the right branch
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001883 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07001884 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07001885 try{
1886 op.addOperator(operator);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001887 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07001888 throw new ParseException(e.getMessage());
1889 }
Yingyi Bua8baf6d2016-07-05 21:40:44 -07001890 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001891
Yingyi Bu6c638342016-09-02 17:54:34 -07001892 operand = BetweenExpr()
Yingyi Buea4ec722016-11-04 01:26:16 -07001893 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07001894 op.addOperand(operand, broadcast);
Yingyi Buea4ec722016-11-04 01:26:16 -07001895 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07001896 )?
1897
1898 {
1899 if (annotation != null) {
1900 op.addHint(annotation);
1901 }
1902 return op==null? operand: op;
1903 }
1904}
1905
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001906
Yingyi Bu6c638342016-09-02 17:54:34 -07001907Expression BetweenExpr()throws ParseException:
1908{
1909 boolean not = false;
1910 OperatorExpr op = null;
1911 Expression operand = null;
1912 IExpressionAnnotation annotation = null;
1913}
1914{
1915 operand = IsExpr()
1916 (
1917 LOOKAHEAD(2)
1918 (<NOT> { not = true; })? <BETWEEN>
1919 {
1920 String mhint = getHint(token);
1921 if (mhint != null) {
1922 if (mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
1923 annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
1924 } else if (mhint.equals(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
1925 annotation = SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE;
1926 }
1927 }
1928 String operator = token.image.toLowerCase();
1929 if(not){
1930 operator = "not_" + operator;
1931 }
1932 if (op == null) {
1933 op = new OperatorExpr();
1934 op.addOperand(operand);
1935 op.setCurrentop(true);
1936 }
1937 try{
1938 op.addOperator(operator);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08001939 } catch (CompilationException e){
Yingyi Bu6c638342016-09-02 17:54:34 -07001940 throw new ParseException(e.getMessage());
1941 }
1942 }
1943
1944 operand = IsExpr()
1945 {
1946 op.addOperand(operand);
1947 }
1948
1949 <AND>
1950 operand = IsExpr()
1951 {
1952 op.addOperand(operand);
1953 }
1954 )?
1955
1956 {
1957 if (annotation != null) {
1958 op.addHint(annotation);
1959 }
1960 return op==null? operand: op;
1961 }
1962}
1963
Yingyi Budaa549c2016-06-28 22:30:52 -07001964Expression IsExpr() throws ParseException:
1965{
1966 Expression expr = null;
1967 Expression operand = null;
1968 boolean not = false;
1969}
1970{
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001971 operand = LikeExpr()
Yingyi Budaa549c2016-06-28 22:30:52 -07001972 ( <IS> (<NOT> { not = true; })? (<NULL> | <MISSING> | <UNKOWN>)
1973 {
1974 String functionName = "is-" + token.image.toLowerCase();
1975 FunctionSignature signature = new FunctionSignature(null, functionName, 1);
1976 expr = new CallExpr(signature, new ArrayList<Expression>(Collections.singletonList(operand)));
1977 if(not) {
1978 FunctionSignature notSignature = new FunctionSignature(null, "not", 1);
1979 expr = new CallExpr(notSignature, new ArrayList<Expression>(Collections.singletonList(expr)));
1980 }
1981 }
1982 )?
1983 {
1984 return expr = expr==null? operand : expr;
1985 }
1986}
1987
Yingyi Bu6e6a80c2017-01-21 20:18:49 -08001988
1989Expression LikeExpr()throws ParseException:
1990{
1991 boolean not = false;
1992 OperatorExpr op = null;
1993 Expression operand = null;
1994}
1995{
1996 operand = ConcatExpr()
1997 (
1998 LOOKAHEAD(2)
1999 (<NOT> { not = true; })? <LIKE>
2000 {
2001 op = new OperatorExpr();
2002 op.addOperand(operand);
2003 op.setCurrentop(true);
2004
2005 String operator = token.image.toLowerCase();
2006 if (not) {
2007 operator = "not_" + operator;
2008 }
2009 try{
2010 op.addOperator(operator);
2011 } catch (CompilationException e){
2012 throw new ParseException(e.getMessage());
2013 }
2014 }
2015
2016 operand = ConcatExpr()
2017 {
2018 op.addOperand(operand);
2019 }
2020 )?
2021
2022 {
2023 return op == null ? operand : op;
2024 }
2025}
2026
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002027Expression ConcatExpr()throws ParseException:
2028{
2029 OperatorExpr op = null;
2030 Expression operand = null;
2031}
2032{
2033 operand = AddExpr()
2034 (
2035 LOOKAHEAD(1)
2036 (<CONCAT>)
2037 {
2038 if (op == null) {
2039 op = new OperatorExpr();
2040 op.addOperand(operand);
2041 op.setCurrentop(true);
2042 }
2043 try{
2044 ((OperatorExpr)op).addOperator(token.image);
2045 } catch (Exception e){
2046 throw new ParseException(e.getMessage());
2047 }
2048 }
2049 operand = AddExpr()
2050 {
2051 op.addOperand(operand);
2052 }
2053 )*
2054
2055 {
2056 return op==null? operand: op;
2057 }
2058}
Yingyi Budaa549c2016-06-28 22:30:52 -07002059
Yingyi Bu391f09e2015-10-29 13:49:39 -07002060Expression AddExpr()throws ParseException:
2061{
2062 OperatorExpr op = null;
2063 Expression operand = null;
2064}
2065{
2066 operand = MultExpr()
Yingyi Budaa549c2016-06-28 22:30:52 -07002067 (
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002068 LOOKAHEAD(1)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002069 (<PLUS> | <MINUS>)
2070 {
2071 if (op == null) {
2072 op = new OperatorExpr();
2073 op.addOperand(operand);
2074 op.setCurrentop(true);
2075 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07002076 try{
2077 ((OperatorExpr)op).addOperator(token.image);
2078 } catch (Exception e){
2079 throw new ParseException(e.getMessage());
2080 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002081 }
2082
2083 operand = MultExpr()
2084 {
2085 op.addOperand(operand);
2086 }
2087 )*
2088
2089 {
2090 return op==null? operand: op;
2091 }
2092}
2093
2094Expression MultExpr()throws ParseException:
2095{
2096 OperatorExpr op = null;
2097 Expression operand = null;
2098}
2099{
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002100 operand = ExponentExpr()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002101
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002102 (( <MUL> | <DIV> | <MOD> | <IDIV>)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002103 {
2104 if (op == null) {
2105 op = new OperatorExpr();
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002106 op.addOperand(operand);
2107 op.setCurrentop(true);
2108 }
2109 try{
2110 op.addOperator(token.image);
2111 } catch (Exception e){
2112 throw new ParseException(e.getMessage());
2113 }
2114 }
2115 operand = ExponentExpr()
2116 {
2117 op.addOperand(operand);
2118 }
2119 )*
2120
2121 {
2122 return op==null?operand:op;
2123 }
2124}
2125
2126Expression ExponentExpr()throws ParseException:
2127{
2128 OperatorExpr op = null;
2129 Expression operand = null;
2130}
2131{
2132 operand = UnaryExpr()
2133 (<CARET>
2134 {
2135 if (op == null) {
2136 op = new OperatorExpr();
2137 op.addOperand(operand);
2138 op.setCurrentop(true);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002139 }
Yingyi Bu196db5d2016-07-15 19:07:20 -07002140 try{
2141 op.addOperator(token.image);
2142 } catch (Exception e){
2143 throw new ParseException(e.getMessage());
2144 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002145 }
2146 operand = UnaryExpr()
2147 {
2148 op.addOperand(operand);
2149 }
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002150 )?
2151 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07002152 return op==null?operand:op;
Yingyi Bu79ccdac2016-07-26 23:49:24 -07002153 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002154}
2155
2156Expression UnaryExpr() throws ParseException:
2157{
Yingyi Bu196db5d2016-07-15 19:07:20 -07002158 boolean not = false;
2159 UnaryExpr uexpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002160 Expression expr = null;
2161}
Yingyi Budaa549c2016-06-28 22:30:52 -07002162{
Yingyi Bu196db5d2016-07-15 19:07:20 -07002163 ( (<PLUS> | <MINUS> | (<NOT> { not = true; } )? <EXISTS> )
Yingyi Bu391f09e2015-10-29 13:49:39 -07002164 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07002165 String exprType = token.image.toLowerCase();
2166 if(not){
2167 exprType = "not_" + exprType;
2168 }
2169 uexpr = new UnaryExpr();
2170 try{
2171 uexpr.setExprType(exprType);
Taewoo Kime65e6ca2017-01-14 17:53:28 -08002172 } catch (CompilationException e){
Yingyi Bu196db5d2016-07-15 19:07:20 -07002173 throw new ParseException(e.getMessage());
Yingyi Budaa549c2016-06-28 22:30:52 -07002174 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002175 }
2176 )?
2177
2178 expr = ValueExpr()
2179 {
Yingyi Bu196db5d2016-07-15 19:07:20 -07002180 if(uexpr==null){
Yingyi Bu391f09e2015-10-29 13:49:39 -07002181 return expr;
Yingyi Bu196db5d2016-07-15 19:07:20 -07002182 }
2183 uexpr.setExpr(expr);
2184 return uexpr;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002185 }
2186}
2187
2188Expression ValueExpr()throws ParseException:
2189{
2190 Expression expr = null;
2191 Identifier ident = null;
2192 AbstractAccessor fa = null;
2193 Expression indexExpr = null;
2194}
2195{
2196 expr = PrimaryExpr() (
2197 ident = Field()
2198 {
2199 fa = (fa == null ? new FieldAccessor(expr, ident)
2200 : new FieldAccessor(fa, ident));
2201 }
2202 | indexExpr = Index()
2203 {
2204 fa = (fa == null ? new IndexAccessor(expr, indexExpr)
2205 : new IndexAccessor(fa, indexExpr));
2206 }
2207 )*
2208 {
2209 return fa == null ? expr : fa;
2210 }
2211}
2212
2213Identifier Field() throws ParseException:
2214{
2215 String ident = null;
2216}
2217{
2218 <DOT> ident = Identifier()
2219 {
2220 return new Identifier(ident);
2221 }
2222}
2223
2224Expression Index() throws ParseException:
2225{
2226 Expression expr = null;
2227}
2228{
2229 <LEFTBRACKET> ( expr = Expression()
2230 {
2231 if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
2232 {
2233 Literal lit = ((LiteralExpr)expr).getValue();
2234 if(lit.getLiteralType() != Literal.Type.INTEGER &&
2235 lit.getLiteralType() != Literal.Type.LONG) {
2236 throw new ParseException("Index should be an INTEGER");
2237 }
2238 }
2239 }
2240
2241 | <QUES> // ANY
2242
2243 )
2244
2245 <RIGHTBRACKET>
2246 {
2247 return expr;
2248 }
2249}
2250
2251
2252Expression PrimaryExpr()throws ParseException:
2253{
2254 Expression expr = null;
2255}
2256{
2257 ( LOOKAHEAD(4)
2258 expr = FunctionCallExpr()
2259 | expr = Literal()
2260 | expr = VariableRef()
2261 | expr = ListConstructor()
2262 | expr = RecordConstructor()
2263 | expr = ParenthesizedExpression()
2264 )
2265 {
2266 return expr;
2267 }
2268}
2269
2270Expression Literal() throws ParseException:
2271{
2272 LiteralExpr lit = new LiteralExpr();
2273 String str = null;
2274}
2275{
2276 ( str = StringLiteral()
2277 {
2278 lit.setValue(new StringLiteral(str));
2279 }
2280 | <INTEGER_LITERAL>
2281 {
Till Westmann68c6a992016-09-30 00:19:12 -07002282 try {
2283 lit.setValue(new LongIntegerLiteral(Long.valueOf(token.image)));
2284 } catch (NumberFormatException e) {
2285 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2286 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002287 }
2288 | <FLOAT_LITERAL>
2289 {
Till Westmann68c6a992016-09-30 00:19:12 -07002290 try {
2291 lit.setValue(new FloatLiteral(Float.valueOf(token.image)));
2292 } catch (NumberFormatException e) {
2293 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2294 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002295 }
2296 | <DOUBLE_LITERAL>
2297 {
Till Westmann68c6a992016-09-30 00:19:12 -07002298 try {
2299 lit.setValue(new DoubleLiteral(Double.valueOf(token.image)));
2300 } catch (NumberFormatException e) {
2301 throw new ParseException("Could not parse numeric literal \"" + token.image +'"');
2302 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002303 }
Yingyi Bu535d86b2016-05-23 16:44:25 -07002304 | <MISSING>
2305 {
2306 lit.setValue(MissingLiteral.INSTANCE);
2307 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002308 | <NULL>
2309 {
2310 lit.setValue(NullLiteral.INSTANCE);
2311 }
2312 | <TRUE>
2313 {
2314 lit.setValue(TrueLiteral.INSTANCE);
2315 }
2316 | <FALSE>
2317 {
2318 lit.setValue(FalseLiteral.INSTANCE);
2319 }
2320 )
2321 {
2322 return lit;
2323 }
2324}
2325
2326
2327VariableExpr VariableRef() throws ParseException:
2328{
2329 VariableExpr varExp = new VariableExpr();
2330 VarIdentifier var = new VarIdentifier();
2331}
2332{
2333 { String id = null; }
2334 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2335 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002336 id = SqlppVariableUtil.toInternalVariableName(id); // Prefix user-defined variables with "$"
Yingyi Bu391f09e2015-10-29 13:49:39 -07002337 Identifier ident = lookupSymbol(id);
2338 if (isInForbiddenScopes(id)) {
2339 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.");
2340 }
2341 if(ident != null) { // exist such ident
Yingyi Bu391f09e2015-10-29 13:49:39 -07002342 varExp.setVar((VarIdentifier)ident);
2343 } else {
2344 varExp.setVar(var);
Yingyi Buacc12a92016-03-26 17:25:05 -07002345 varExp.setIsNewVar(false);
2346 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002347 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002348 return varExp;
2349 }
2350}
2351
2352
2353VariableExpr Variable() throws ParseException:
2354{
2355 VariableExpr varExp = new VariableExpr();
2356 VarIdentifier var = new VarIdentifier();
2357}
2358{
2359 { String id = null; }
2360 (<IDENTIFIER> { id = token.image; } | id = QuotedString())
2361 {
Yingyi Buacc12a92016-03-26 17:25:05 -07002362 id = SqlppVariableUtil.toInternalVariableName(id); // prefix user-defined variables with "$".
Yingyi Bu391f09e2015-10-29 13:49:39 -07002363 Identifier ident = lookupSymbol(id);
2364 if(ident != null) { // exist such ident
2365 varExp.setIsNewVar(false);
2366 }
2367 varExp.setVar(var);
Yingyi Bucaea8f02015-11-16 15:12:15 -08002368 var.setValue(id);
Yingyi Bu391f09e2015-10-29 13:49:39 -07002369 return varExp;
2370 }
2371}
2372
2373Expression ListConstructor() throws ParseException:
2374{
2375 Expression expr = null;
2376}
2377{
2378 (
2379 expr = OrderedListConstructor() | expr = UnorderedListConstructor()
2380 )
2381
2382 {
2383 return expr;
2384 }
2385}
2386
2387
2388ListConstructor OrderedListConstructor() throws ParseException:
2389{
2390 ListConstructor expr = new ListConstructor();
2391 List<Expression> exprList = null;
2392 expr.setType(ListConstructor.Type.ORDERED_LIST_CONSTRUCTOR);
2393}
2394{
2395 <LEFTBRACKET> exprList = ExpressionList() <RIGHTBRACKET>
2396 {
2397 expr.setExprList(exprList);
2398 return expr;
2399 }
2400}
2401
2402ListConstructor UnorderedListConstructor() throws ParseException:
2403{
2404 ListConstructor expr = new ListConstructor();
2405 List<Expression> exprList = null;
2406 expr.setType(ListConstructor.Type.UNORDERED_LIST_CONSTRUCTOR);
2407}
2408{
2409 <LEFTDBLBRACE> exprList = ExpressionList() <RIGHTDBLBRACE>
2410 {
2411 expr.setExprList(exprList);
2412 return expr;
2413 }
2414}
2415
2416List<Expression> ExpressionList() throws ParseException:
2417{
2418 Expression expr = null;
2419 List<Expression> list = null;
2420 List<Expression> exprList = new ArrayList<Expression>();
2421}
2422{
2423 (
2424 expr = Expression() { exprList.add(expr); }
2425 (LOOKAHEAD(1) <COMMA> list = ExpressionList() { exprList.addAll(list); })?
2426 )?
2427 (LOOKAHEAD(1) Comma())?
2428 {
2429 return exprList;
2430 }
2431}
2432
2433void Comma():
2434{}
2435{
2436 <COMMA>
2437}
2438
2439RecordConstructor RecordConstructor() throws ParseException:
2440{
2441 RecordConstructor expr = new RecordConstructor();
2442 FieldBinding tmp = null;
2443 List<FieldBinding> fbList = new ArrayList<FieldBinding>();
2444}
2445{
2446 <LEFTBRACE> (tmp = FieldBinding()
2447 {
2448 fbList.add(tmp);
2449 }
2450 (<COMMA> tmp = FieldBinding() { fbList.add(tmp); })*)? <RIGHTBRACE>
2451 {
2452 expr.setFbList(fbList);
2453 return expr;
2454 }
2455}
2456
2457FieldBinding FieldBinding() throws ParseException:
2458{
2459 FieldBinding fb = new FieldBinding();
2460 Expression left, right;
2461}
2462{
2463 left = Expression() <COLON> right = Expression()
2464 {
2465 fb.setLeftExpr(left);
2466 fb.setRightExpr(right);
2467 return fb;
2468 }
2469}
2470
2471
2472Expression FunctionCallExpr() throws ParseException:
2473{
2474 CallExpr callExpr;
2475 List<Expression> argList = new ArrayList<Expression>();
Yingyi Buf4d09842016-08-26 00:03:52 -07002476 Expression tmp = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002477 int arity = 0;
2478 FunctionName funcName = null;
2479 String hint = null;
Yingyi Buf4d09842016-08-26 00:03:52 -07002480 boolean star = false;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002481}
2482{
2483 funcName = FunctionName()
2484 {
2485 hint = funcName.hint;
2486 }
Yingyi Buf4d09842016-08-26 00:03:52 -07002487 <LEFTPAREN> ( ( tmp = Expression() | <MUL> { star = true; } )
Yingyi Bu391f09e2015-10-29 13:49:39 -07002488 {
Yingyi Buf4d09842016-08-26 00:03:52 -07002489 if(star){
2490 if(!funcName.function.toLowerCase().equals("count")){
2491 throw new ParseException("The parameter * can only be used in COUNT().");
2492 }
2493 argList.add(new LiteralExpr(new LongIntegerLiteral(1L)));
2494 } else {
2495 argList.add(tmp);
2496 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002497 arity ++;
2498 }
2499 (<COMMA> tmp = Expression()
2500 {
2501 argList.add(tmp);
2502 arity++;
2503 }
2504 )*)? <RIGHTPAREN>
2505 {
2506 // TODO use funcName.library
2507 String fqFunctionName = funcName.library == null ? funcName.function : funcName.library + "#" + funcName.function;
2508 FunctionSignature signature
2509 = lookupFunctionSignature(funcName.dataverse, fqFunctionName, arity);
2510 if (signature == null) {
2511 signature = new FunctionSignature(funcName.dataverse, fqFunctionName, arity);
2512 }
Xikui Wang96fd4022017-04-10 14:23:31 -07002513 callExpr = FunctionMapUtil.normalizedListInputFunctions(new CallExpr(signature,argList));
Yingyi Bu391f09e2015-10-29 13:49:39 -07002514 if (hint != null) {
2515 if (hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
2516 callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
2517 } else if (hint.startsWith(SKIP_SECONDARY_INDEX_SEARCH_HINT)) {
2518 callExpr.addHint(SkipSecondaryIndexSearchExpressionAnnotation.INSTANCE);
2519 }
2520 }
2521 return callExpr;
2522 }
2523}
2524
2525Expression ParenthesizedExpression() throws ParseException:
2526{
2527 Expression expr;
2528}
2529{
2530 (
2531 LOOKAHEAD(2)
2532 <LEFTPAREN> expr = Expression() <RIGHTPAREN>
2533 |
2534 expr = Subquery()
2535 )
2536 {
2537 return expr;
2538 }
2539}
2540
Yingyi Buc8c067c2016-07-25 23:37:19 -07002541
2542Expression CaseExpr() throws ParseException:
Yingyi Bu391f09e2015-10-29 13:49:39 -07002543{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002544 Expression conditionExpr = new LiteralExpr(TrueLiteral.INSTANCE);
2545 List<Expression> whenExprs = new ArrayList<Expression>();
2546 List<Expression> thenExprs = new ArrayList<Expression>();
2547 Expression elseExpr = null;
Yingyi Bufdc71eb2016-08-24 22:41:57 -07002548
Yingyi Buc8c067c2016-07-25 23:37:19 -07002549 Expression whenExpr = null;
2550 Expression thenExpr = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002551}
2552{
Yingyi Buc8c067c2016-07-25 23:37:19 -07002553 <CASE> ( conditionExpr = Expression() )?
2554 (
2555 <WHEN> whenExpr = Expression()
2556 {
2557 whenExprs.add(whenExpr);
2558 }
2559 <THEN> thenExpr = Expression()
2560 {
2561 thenExprs.add(thenExpr);
2562 }
2563 )*
2564 (<ELSE> elseExpr = Expression() )?
2565 <END>
2566 {
2567 return new CaseExpression(conditionExpr, whenExprs, thenExprs, elseExpr);
2568 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002569}
2570
Yingyi Buab817482016-08-19 21:29:31 -07002571SelectExpression SelectExpression(boolean subquery) throws ParseException:
2572{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002573 List<LetClause> letClauses = new ArrayList<LetClause>();
2574 SelectSetOperation selectSetOperation;
2575 OrderbyClause orderbyClause = null;
2576 LimitClause limitClause = null;
2577 createNewScope();
Yingyi Buab817482016-08-19 21:29:31 -07002578}
2579{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002580 ( letClauses = LetClause() )?
2581 selectSetOperation = SelectSetOperation()
2582 (orderbyClause = OrderbyClause() {})?
2583 (limitClause = LimitClause() {})?
2584 {
2585 return new SelectExpression(letClauses, selectSetOperation, orderbyClause, limitClause, subquery);
2586 }
2587}
2588
Yingyi Buab817482016-08-19 21:29:31 -07002589SelectSetOperation SelectSetOperation() throws ParseException:
2590{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002591 SetOperationInput setOperationInputLeft;
2592 List<SetOperationRight> setOperationRights = new ArrayList<SetOperationRight>();
2593}
2594{
2595 {
2596 SelectBlock selectBlockLeft = null;
2597 SelectExpression subqueryLeft = null;
2598 Expression expr = null;
2599 }
2600 selectBlockLeft = SelectBlock()
2601 {
2602 setOperationInputLeft = new SetOperationInput(selectBlockLeft, subqueryLeft);
2603 }
2604 (
2605 {
2606 SetOpType opType = SetOpType.UNION;
2607 boolean setSemantics = true;
2608 SelectBlock selectBlockRight = null;
2609 SelectExpression subqueryRight = null;
2610 }
2611 (<UNION> {opType = SetOpType.UNION;} |<INTERSECT> {opType = SetOpType.INTERSECT;} |<EXCEPT> {opType = SetOpType.EXCEPT;}) (<ALL> {setSemantics = false;} )?
2612 (selectBlockRight = SelectBlock()| subqueryRight = Subquery())
2613 {
2614 setOperationRights.add(new SetOperationRight(opType, setSemantics, new SetOperationInput(selectBlockRight, subqueryRight)));
2615 }
2616 )*
2617 {
2618 return new SelectSetOperation(setOperationInputLeft, setOperationRights);
2619 }
2620}
2621
Yingyi Buab817482016-08-19 21:29:31 -07002622SelectExpression Subquery() throws ParseException:
2623{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002624 SelectExpression selectExpr = null;
2625}
2626{
2627 <LEFTPAREN> selectExpr = SelectExpression(true) {} <RIGHTPAREN>
2628 {
2629 return selectExpr;
2630 }
2631}
2632
Yingyi Buab817482016-08-19 21:29:31 -07002633SelectBlock SelectBlock() throws ParseException:
2634{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002635 SelectClause selectClause = null;
2636 FromClause fromClause = null;
2637 List<LetClause> fromLetClauses = null;
2638 WhereClause whereClause = null;
2639 GroupbyClause groupbyClause = null;
2640 List<LetClause> gbyLetClauses = null;
2641 HavingClause havingClause = null;
2642}
2643{
2644 (
2645 selectClause = SelectClause()
2646 (
2647 LOOKAHEAD(1)
2648 fromClause = FromClause()
2649 (
2650 LOOKAHEAD(1)
2651 fromLetClauses = LetClause()
2652 )?
2653 )?
2654 (whereClause = WhereClause())?
2655 (
2656 groupbyClause = GroupbyClause()
2657 (
2658 LOOKAHEAD(1)
2659 gbyLetClauses = LetClause()
2660 )?
2661 (havingClause = HavingClause())?
2662 )?
2663 |
2664 fromClause = FromClause()
2665 (
2666 LOOKAHEAD(1)
2667 fromLetClauses = LetClause()
2668 )?
2669 (whereClause = WhereClause())?
2670 (
2671 groupbyClause = GroupbyClause()
2672 (
2673 gbyLetClauses = LetClause()
2674 )?
2675 (havingClause = HavingClause())?
2676 )?
2677 selectClause = SelectClause()
2678 )
2679 {
2680 return new SelectBlock(selectClause, fromClause, fromLetClauses, whereClause, groupbyClause, gbyLetClauses, havingClause);
2681 }
2682}
2683
Yingyi Buab817482016-08-19 21:29:31 -07002684SelectClause SelectClause() throws ParseException:
2685{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002686 SelectRegular selectRegular = null;
2687 SelectElement selectElement = null;
2688 boolean distinct = false;
2689}
2690{
Michael Blowd6cf6412016-06-30 02:44:35 -04002691 <SELECT> (<ALL>|<DISTINCT> {distinct = true; } )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002692 (
2693 selectRegular = SelectRegular()
Michael Blowd6cf6412016-06-30 02:44:35 -04002694 |
Yingyi Bu391f09e2015-10-29 13:49:39 -07002695 selectElement = SelectElement()
Yingyi Bua89fae62016-07-06 07:58:55 -07002696 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002697 {
Yingyi Bua89fae62016-07-06 07:58:55 -07002698 if(selectRegular == null && selectElement == null){
2699 Projection projection = new Projection(null, null, true, false);
2700 List<Projection> projections = new ArrayList<Projection>();
2701 projections.add(projection);
2702 selectRegular = new SelectRegular(projections);
2703 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002704 return new SelectClause(selectElement, selectRegular, distinct);
2705 }
2706}
2707
Yingyi Buab817482016-08-19 21:29:31 -07002708SelectRegular SelectRegular() throws ParseException:
2709{
Michael Blowd6cf6412016-06-30 02:44:35 -04002710 List<Projection> projections = new ArrayList<Projection>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002711}
2712{
2713 {
2714 Projection projection = null;
2715 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002716 projection = Projection() { projections.add(projection); }
Yingyi Bua89fae62016-07-06 07:58:55 -07002717 ( LOOKAHEAD(2) <COMMA>
2718 projection = Projection() {projections.add(projection);}
2719 )*
Yingyi Bu391f09e2015-10-29 13:49:39 -07002720 {
2721 return new SelectRegular(projections);
2722 }
2723}
2724
Yingyi Buab817482016-08-19 21:29:31 -07002725SelectElement SelectElement() throws ParseException:
2726{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002727 Expression expr = null;
2728 String name = null;
2729}
2730{
2731 (<RAW>|<ELEMENT>|<VALUE>) expr = Expression()
2732 {
2733 return new SelectElement(expr);
2734 }
2735}
2736
Yingyi Buab817482016-08-19 21:29:31 -07002737Projection Projection() throws ParseException :
2738{
Yingyi Bu391f09e2015-10-29 13:49:39 -07002739 Expression expr = null;
2740 Identifier identifier = null;
2741 String name = null;
2742 boolean star = false;
2743 boolean exprStar = false;
2744}
2745{
2746 (
2747 LOOKAHEAD(2)
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002748 expr = Expression() ((<AS>)? name = Identifier())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002749 | expr = Expression() <DOT> <MUL> {exprStar = true; }
2750 | <MUL> {star = true; }
2751 )
2752 {
Yingyi Bua89fae62016-07-06 07:58:55 -07002753 if(!star && name == null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002754 String generatedColumnIdentifier = ExpressionToVariableUtil.getGeneratedIdentifier(expr, false);
2755 if(generatedColumnIdentifier != null){
2756 name = SqlppVariableUtil.toUserDefinedName(generatedColumnIdentifier);
2757 }
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002758 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002759 return new Projection(expr, name, star, exprStar);
2760 }
2761}
2762
2763FromClause FromClause() throws ParseException :
2764{
2765 List<FromTerm> fromTerms = new ArrayList<FromTerm>();
2766 extendCurrentScope();
2767}
2768{
2769 {
2770 FromTerm fromTerm = null;
2771 }
Michael Blowd6cf6412016-06-30 02:44:35 -04002772 <FROM> fromTerm = FromTerm() { fromTerms.add(fromTerm); }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002773 (LOOKAHEAD(2) <COMMA> fromTerm = FromTerm() { fromTerms.add(fromTerm); } )*
2774 {
2775 return new FromClause(fromTerms);
2776 }
2777}
2778
2779FromTerm FromTerm() throws ParseException :
2780{
2781 Expression leftExpr = null;
Michael Blowd6cf6412016-06-30 02:44:35 -04002782 VariableExpr leftVar = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002783 VariableExpr posVar = null;
2784 List<AbstractBinaryCorrelateClause> correlateClauses = new ArrayList<AbstractBinaryCorrelateClause>();
2785}
2786{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002787 leftExpr = Expression() ((<AS>)? leftVar = Variable())? (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002788 (
2789 {JoinType joinType = JoinType.INNER; }
2790 (joinType = JoinType())?
2791 {
2792 AbstractBinaryCorrelateClause correlateClause = null;
2793 }
2794 (correlateClause = JoinClause(joinType)
Yingyi Bu6d999ed2016-07-13 11:59:06 -07002795 | correlateClause = UnnestClause(joinType)
Yingyi Bu391f09e2015-10-29 13:49:39 -07002796 )
2797 {
2798 correlateClauses.add(correlateClause);
2799 }
2800 )*
2801 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002802 if(leftVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002803 leftVar = ExpressionToVariableUtil.getGeneratedVariable(leftExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002804 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002805 return new FromTerm(leftExpr, leftVar, posVar, correlateClauses);
2806 }
2807}
2808
2809JoinClause JoinClause(JoinType joinType) throws ParseException :
2810{
2811 Expression rightExpr = null;
2812 VariableExpr rightVar = null;
2813 VariableExpr posVar = null;
2814 Expression conditionExpr = null;
2815}
2816{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002817 <JOIN> rightExpr = Expression() ((<AS>)? rightVar = Variable())? (<AT> posVar = Variable())? <ON> conditionExpr = Expression()
Yingyi Bu391f09e2015-10-29 13:49:39 -07002818 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002819 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002820 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002821 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002822 return new JoinClause(joinType, rightExpr, rightVar, posVar, conditionExpr);
2823 }
2824}
2825
Yingyi Bu391f09e2015-10-29 13:49:39 -07002826UnnestClause UnnestClause(JoinType joinType) throws ParseException :
2827{
2828 Expression rightExpr;
2829 VariableExpr rightVar;
2830 VariableExpr posVar = null;
2831}
2832{
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002833 (<UNNEST>|<CORRELATE>|<FLATTEN>) rightExpr = Expression() ((<AS>)? rightVar = Variable()) (<AT> posVar = Variable())?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002834 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002835 if(rightVar==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002836 rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002837 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002838 return new UnnestClause(joinType, rightExpr, rightVar, posVar);
2839 }
2840}
2841
2842
2843JoinType JoinType() throws ParseException :
2844{
2845 JoinType joinType = JoinType.INNER;
2846}
2847{
2848 (<INNER>|<LEFT> (<OUTER>)? {joinType = JoinType.LEFTOUTER; })
2849 {
2850 return joinType;
2851 }
2852}
2853
2854List<LetClause> LetClause() throws ParseException:
2855{
2856 List<LetClause> letList = new ArrayList<LetClause>();
2857 LetClause letClause;
2858}
2859{
2860 (
2861 (<LET>|<LETTING>) letClause = LetElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = LetElement() { letList.add(letClause); })*
2862 |
2863 <WITH> letClause = WithElement() { letList.add(letClause); } (LOOKAHEAD(1) <COMMA> letClause = WithElement() { letList.add(letClause); })*
2864 )
2865 {
2866 return letList;
2867 }
2868}
2869
2870WhereClause WhereClause()throws ParseException :
2871{
2872 WhereClause wc = new WhereClause();
2873 Expression whereExpr;
2874}
2875{
2876 <WHERE> whereExpr = Expression()
2877 {
2878 wc.setWhereExpr(whereExpr);
2879 return wc;
2880 }
2881}
2882
2883OrderbyClause OrderbyClause()throws ParseException :
2884{
2885 OrderbyClause oc = new OrderbyClause();
2886 Expression orderbyExpr;
2887 List<Expression> orderbyList = new ArrayList<Expression>();
2888 List<OrderbyClause.OrderModifier> modifierList = new ArrayList<OrderbyClause.OrderModifier >();
2889 int numOfOrderby = 0;
2890}
2891{
2892 <ORDER>
2893 {
2894 String hint = getHint(token);
2895 if (hint != null) {
2896 if (hint.startsWith(INMEMORY_HINT)) {
2897 String splits[] = hint.split(" +");
2898 int numFrames = Integer.parseInt(splits[1]);
2899 int numTuples = Integer.parseInt(splits[2]);
2900 oc.setNumFrames(numFrames);
2901 oc.setNumTuples(numTuples);
2902 }
2903 }
2904 }
2905 <BY> orderbyExpr = Expression()
2906 {
2907 orderbyList.add(orderbyExpr);
2908 OrderbyClause.OrderModifier modif = OrderbyClause.OrderModifier.ASC;
2909 }
2910 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2911 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2912 {
2913 modifierList.add(modif);
2914 }
2915
2916 (LOOKAHEAD(2) <COMMA> orderbyExpr = Expression()
2917 {
2918 orderbyList.add(orderbyExpr);
2919 modif = OrderbyClause.OrderModifier.ASC;
2920 }
2921 ( (<ASC> { modif = OrderbyClause.OrderModifier.ASC; })
2922 | (<DESC> { modif = OrderbyClause.OrderModifier.DESC; }))?
2923 {
2924 modifierList.add(modif);
2925 }
2926 )*
2927
2928 {
2929 oc.setModifierList(modifierList);
2930 oc.setOrderbyList(orderbyList);
2931 return oc;
2932 }
2933}
2934
2935GroupbyClause GroupbyClause()throws ParseException :
2936{
2937 GroupbyClause gbc = new GroupbyClause();
2938 List<GbyVariableExpressionPair> vePairList = new ArrayList<GbyVariableExpressionPair>();
2939 VariableExpr var = null;
Yingyi Bu391f09e2015-10-29 13:49:39 -07002940 Expression expr = null;
2941 VariableExpr decorVar = null;
2942 Expression decorExpr = null;
Yingyi Buacc12a92016-03-26 17:25:05 -07002943
2944 VariableExpr groupVar = null;
2945 List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<Pair<Expression, Identifier>>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07002946}
2947{
2948 {
2949 Scope newScope = extendCurrentScopeNoPush(true);
2950 // extendCurrentScope(true);
2951 }
2952 <GROUP>
2953 {
2954 String hint = getHint(token);
2955 if (hint != null && hint.equals(HASH_GROUP_BY_HINT)) {
2956 gbc.setHashGroupByHint(true);
2957 }
2958 }
2959 <BY> (
2960 expr = Expression()
2961 (LOOKAHEAD(1) (<AS>)?
2962 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08002963 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002964 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002965 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002966 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002967 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002968 GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
2969 vePairList.add(pair1);
2970 }
2971 ( LOOKAHEAD(1) <COMMA>
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002972 {
2973 var = null;
2974 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002975 expr = Expression()
2976 (LOOKAHEAD(1) (<AS>)?
2977 var = Variable()
Yingyi Bucaea8f02015-11-16 15:12:15 -08002978 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07002979 {
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002980 if(var==null){
Yingyi Bu5b2d4c82016-07-13 17:56:48 -07002981 var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
Yingyi Bu9e3f9be2016-07-01 10:07:37 -07002982 }
Yingyi Bu391f09e2015-10-29 13:49:39 -07002983 GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
2984 vePairList.add(pair2);
2985 }
2986 )*
2987 )
Yingyi Buacc12a92016-03-26 17:25:05 -07002988 (<GROUP> <AS> groupVar = Variable()
2989 ( LOOKAHEAD(1)
2990 {
2991 VariableExpr fieldVarExpr = null;
2992 String fieldIdentifierStr = null;
2993 }
2994 <LEFTPAREN>
2995 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
2996 {
2997 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
2998 }
2999 (<COMMA>
3000 fieldVarExpr = VariableRef() <AS> fieldIdentifierStr = Identifier()
3001 {
3002 groupFieldList.add(new Pair<Expression, Identifier>(fieldVarExpr, new Identifier(fieldIdentifierStr)));
3003 }
3004 )*
3005 <RIGHTPAREN>
3006 )?
3007 )?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003008 {
3009 gbc.setGbyPairList(vePairList);
3010 gbc.setDecorPairList(new ArrayList<GbyVariableExpressionPair>());
Yingyi Bu8671ddf2016-08-14 23:58:43 -07003011 gbc.setWithVarMap(new HashMap<Expression, VariableExpr>());
Yingyi Buacc12a92016-03-26 17:25:05 -07003012 gbc.setGroupVar(groupVar);
3013 gbc.setGroupFieldList(groupFieldList);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003014 replaceCurrentScope(newScope);
3015 return gbc;
3016 }
3017}
3018
3019HavingClause HavingClause() throws ParseException:
3020{
3021 Expression filterExpr = null;
3022}
3023{
3024 <HAVING> filterExpr = Expression()
3025 {
3026 return new HavingClause(filterExpr);
3027 }
3028}
3029
3030LimitClause LimitClause() throws ParseException:
3031{
3032 LimitClause lc = new LimitClause();
3033 Expression expr;
3034 pushForbiddenScope(getCurrentScope());
3035}
3036{
3037 <LIMIT> expr = Expression() { lc.setLimitExpr(expr); }
3038 (<OFFSET> expr = Expression() { lc.setOffset(expr); })?
3039
3040 {
3041 popForbiddenScope();
3042 return lc;
3043 }
3044}
3045
3046QuantifiedExpression QuantifiedExpression()throws ParseException:
3047{
3048 QuantifiedExpression qc = new QuantifiedExpression();
3049 List<QuantifiedPair> quantifiedList = new ArrayList<QuantifiedPair>();
3050 Expression satisfiesExpr;
3051 VariableExpr var;
3052 Expression inExpr;
3053 QuantifiedPair pair;
3054}
3055{
3056 {
3057 createNewScope();
3058 }
3059
Yingyi Bu8aac7242016-09-13 23:14:09 -07003060 ( ((<ANY>|<SOME>) { qc.setQuantifier(QuantifiedExpression.Quantifier.SOME); })
Michael Blowd6cf6412016-06-30 02:44:35 -04003061 | (<EVERY> { qc.setQuantifier(QuantifiedExpression.Quantifier.EVERY); }))
Yingyi Bu391f09e2015-10-29 13:49:39 -07003062 var = Variable() <IN> inExpr = Expression()
3063 {
3064 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003065 quantifiedList.add(pair);
3066 }
3067 (
3068 <COMMA> var = Variable() <IN> inExpr = Expression()
3069 {
3070 pair = new QuantifiedPair(var, inExpr);
Yingyi Bu391f09e2015-10-29 13:49:39 -07003071 quantifiedList.add(pair);
3072 }
3073 )*
Yingyi Bu858efae2016-10-13 17:31:57 -07003074 <SATISFIES> satisfiesExpr = Expression() (<END>)?
Yingyi Bu391f09e2015-10-29 13:49:39 -07003075 {
3076 qc.setSatisfiesExpr(satisfiesExpr);
3077 qc.setQuantifiedList(quantifiedList);
3078 removeCurrentScope();
3079 return qc;
3080 }
3081}
3082
3083LetClause LetElement() throws ParseException:
3084{
3085 LetClause lc = new LetClause();
3086 VariableExpr varExp;
3087 Expression beExp;
3088 extendCurrentScope();
3089}
3090{
3091 varExp = Variable() <EQ> beExp = Expression()
3092 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07003093 lc.setVarExpr(varExp);
3094 lc.setBindingExpr(beExp);
3095 return lc;
3096 }
3097}
3098
3099LetClause WithElement() throws ParseException:
3100{
3101 LetClause lc = new LetClause();
3102 VariableExpr varExp;
3103 Expression beExp;
3104 extendCurrentScope();
3105}
3106{
3107 varExp = Variable() <AS> beExp = Expression()
3108 {
Yingyi Bu391f09e2015-10-29 13:49:39 -07003109 lc.setVarExpr(varExp);
3110 lc.setBindingExpr(beExp);
3111 return lc;
3112 }
3113}
3114
3115TOKEN_MGR_DECLS:
3116{
3117 public int commentDepth = 0;
Till Westmanne9b2adf2016-10-15 12:39:01 -07003118 public ArrayDeque<Integer> lexerStateStack = new ArrayDeque<Integer>();
Yingyi Bu391f09e2015-10-29 13:49:39 -07003119
3120 public void pushState() {
3121 lexerStateStack.push( curLexState );
3122 }
3123
3124 public void popState(String token) {
3125 if (lexerStateStack.size() > 0) {
3126 SwitchTo( lexerStateStack.pop() );
3127 } else {
3128 int errorLine = input_stream.getEndLine();
3129 int errorColumn = input_stream.getEndColumn();
3130 String msg = "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered \"" + token
3131 + "\" but state stack is empty.";
3132 throw new TokenMgrError(msg, -1);
3133 }
3134 }
3135}
3136
3137<DEFAULT,IN_DBL_BRACE>
3138TOKEN [IGNORE_CASE]:
3139{
3140 <ALL : "all">
3141 | <AND : "and">
Yingyi Bu8aac7242016-09-13 23:14:09 -07003142 | <ANY : "any">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003143 | <APPLY : "apply">
3144 | <AS : "as">
3145 | <ASC : "asc">
3146 | <AT : "at">
3147 | <AUTOGENERATED : "autogenerated">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07003148 | <BETWEEN : "between">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003149 | <BTREE : "btree">
3150 | <BY : "by">
3151 | <CASE : "case">
3152 | <CLOSED : "closed">
3153 | <CREATE : "create">
3154 | <COMPACTION : "compaction">
3155 | <COMPACT : "compact">
3156 | <CONNECT : "connect">
3157 | <CORRELATE : "correlate">
Yingyi Bud56ff032016-08-01 10:26:57 -07003158 | <DATASET : "dataset">
Yingyi Bu1c0fff52016-03-25 20:23:30 -07003159 | <COLLECTION : "collection">
Yingyi Bud56ff032016-08-01 10:26:57 -07003160 | <DATAVERSE : "dataverse">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003161 | <DECLARE : "declare">
3162 | <DEFINITION : "definition">
3163 | <DELETE : "delete">
3164 | <DESC : "desc">
3165 | <DISCONNECT : "disconnect">
3166 | <DISTINCT : "distinct">
3167 | <DROP : "drop">
3168 | <ELEMENT : "element">
Till Westmann516d1a82016-08-02 14:45:53 -07003169 | <EXPLAIN : "explain">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003170 | <ELSE : "else">
3171 | <ENFORCED : "enforced">
Yingyi Buc8c067c2016-07-25 23:37:19 -07003172 | <END : "end">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003173 | <EVERY : "every">
3174 | <EXCEPT : "except">
3175 | <EXISTS : "exists">
3176 | <EXTERNAL : "external">
3177 | <FEED : "feed">
3178 | <FILTER : "filter">
3179 | <FLATTEN : "flatten">
3180 | <FOR : "for">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003181 | <FROM : "from">
3182 | <FULL : "full">
Taewoo Kimc49405a2017-01-04 00:30:43 -08003183 | <FULLTEXT : "fulltext">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003184 | <FUNCTION : "function">
3185 | <GROUP : "group">
3186 | <HAVING : "having">
3187 | <HINTS : "hints">
3188 | <IF : "if">
3189 | <INTO : "into">
3190 | <IN : "in">
3191 | <INDEX : "index">
3192 | <INGESTION : "ingestion">
3193 | <INNER : "inner">
3194 | <INSERT : "insert">
3195 | <INTERNAL : "internal">
3196 | <INTERSECT : "intersect">
Yingyi Budaa549c2016-06-28 22:30:52 -07003197 | <IS : "is">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003198 | <JOIN : "join">
3199 | <KEYWORD : "keyword">
3200 | <KEY : "key">
3201 | <LEFT : "left">
3202 | <LETTING : "letting">
3203 | <LET : "let">
Yingyi Bua8baf6d2016-07-05 21:40:44 -07003204 | <LIKE : "like">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003205 | <LIMIT : "limit">
3206 | <LOAD : "load">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003207 | <NODEGROUP : "nodegroup">
3208 | <NGRAM : "ngram">
Yingyi Budaa549c2016-06-28 22:30:52 -07003209 | <NOT : "not">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003210 | <OFFSET : "offset">
3211 | <ON : "on">
3212 | <OPEN : "open">
3213 | <OR : "or">
3214 | <ORDER : "order">
3215 | <OUTER : "outer">
3216 | <OUTPUT : "output">
3217 | <PATH : "path">
3218 | <POLICY : "policy">
3219 | <PRESORTED : "pre-sorted">
3220 | <PRIMARY : "primary">
3221 | <RAW : "raw">
3222 | <REFRESH : "refresh">
3223 | <RETURN : "return">
Yingyi Bucb5bf332017-01-02 22:19:50 -08003224 | <RETURNING : "returning">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003225 | <RTREE : "rtree">
3226 | <RUN : "run">
3227 | <SATISFIES : "satisfies">
3228 | <SECONDARY : "secondary">
3229 | <SELECT : "select">
3230 | <SET : "set">
3231 | <SOME : "some">
Abdullah Alamoudifff200c2017-02-18 20:32:14 -08003232 | <START : "start">
3233 | <STOP : "stop">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003234 | <TEMPORARY : "temporary">
3235 | <THEN : "then">
3236 | <TYPE : "type">
3237 | <TO : "to">
3238 | <UNION : "union">
Yingyi Budaa549c2016-06-28 22:30:52 -07003239 | <UNKOWN : "unknown">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003240 | <UNNEST : "unnest">
Yingyi Budaa549c2016-06-28 22:30:52 -07003241 | <UPDATE : "update">
Yingyi Bucb5bf332017-01-02 22:19:50 -08003242 | <UPSERT : "upsert">
Yingyi Budaa549c2016-06-28 22:30:52 -07003243 | <USE : "use">
3244 | <USING : "using">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003245 | <VALUE : "value">
3246 | <WHEN : "when">
3247 | <WHERE : "where">
3248 | <WITH : "with">
3249 | <WRITE : "write">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003250}
3251
3252<DEFAULT,IN_DBL_BRACE>
3253TOKEN :
3254{
3255 <CARET : "^">
Yingyi Bufdc71eb2016-08-24 22:41:57 -07003256 | <CONCAT : "||">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003257 | <DIV : "/">
3258 | <IDIV : "idiv">
3259 | <MINUS : "-">
3260 | <MOD : "%">
3261 | <MUL : "*">
3262 | <PLUS : "+">
3263
3264 | <LEFTPAREN : "(">
3265 | <RIGHTPAREN : ")">
3266 | <LEFTBRACKET : "[">
3267 | <RIGHTBRACKET : "]">
3268
3269 | <ATT : "@">
3270 | <COLON : ":">
3271 | <COMMA : ",">
3272 | <DOT : ".">
3273 | <QUES : "?">
3274 | <SEMICOLON : ";">
3275 | <SHARP : "#">
3276
3277 | <LT : "<">
3278 | <GT : ">">
3279 | <LE : "<=">
3280 | <GE : ">=">
3281 | <EQ : "=">
3282 | <NE : "!=">
Yingyi Bu4a4b8962016-09-16 12:09:11 -07003283 | <LG : "<>">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003284 | <SIMILAR : "~=">
3285}
3286
3287<DEFAULT,IN_DBL_BRACE>
3288TOKEN :
3289{
3290 <LEFTBRACE : "{"> { pushState(); } : DEFAULT
3291}
3292
3293<DEFAULT>
3294TOKEN :
3295{
3296 <RIGHTBRACE : "}"> { popState("}"); }
3297}
3298
3299<DEFAULT,IN_DBL_BRACE>
3300TOKEN :
3301{
3302 <LEFTDBLBRACE : "{{"> { pushState(); } : IN_DBL_BRACE
3303}
3304
3305<IN_DBL_BRACE>
3306TOKEN :
3307{
3308 <RIGHTDBLBRACE : "}}"> { popState("}}"); }
3309}
3310
3311<DEFAULT,IN_DBL_BRACE>
3312TOKEN :
3313{
3314 <INTEGER_LITERAL : (<DIGIT>)+ >
3315}
3316
3317<DEFAULT,IN_DBL_BRACE>
Yingyi Bue311a632016-06-07 18:23:16 -07003318TOKEN [IGNORE_CASE]:
Yingyi Bu391f09e2015-10-29 13:49:39 -07003319{
Yingyi Bu535d86b2016-05-23 16:44:25 -07003320 <MISSING : "missing">
3321 | <NULL : "null">
Yingyi Bu391f09e2015-10-29 13:49:39 -07003322 | <TRUE : "true">
3323 | <FALSE : "false">
3324}
3325
3326<DEFAULT,IN_DBL_BRACE>
3327TOKEN :
3328{
3329 <#DIGIT : ["0" - "9"]>
3330}
3331
3332<DEFAULT,IN_DBL_BRACE>
3333TOKEN:
3334{
Yingyi Bue4d919e2016-10-30 10:47:03 -07003335 < DOUBLE_LITERAL: <DIGITS> ( "." <DIGITS> )
3336 | "." <DIGITS>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003337 >
Yingyi Bue4d919e2016-10-30 10:47:03 -07003338 | < FLOAT_LITERAL: <DIGITS> ( "f" | "F" )
3339 | <DIGITS> ( "." <DIGITS> ( "f" | "F" ) )?
3340 | "." <DIGITS> ( "f" | "F" )
Yingyi Bu391f09e2015-10-29 13:49:39 -07003341 >
3342 | <DIGITS : (<DIGIT>)+ >
3343}
3344
3345<DEFAULT,IN_DBL_BRACE>
3346TOKEN :
3347{
3348 <#LETTER : ["A" - "Z", "a" - "z"]>
3349 | <SPECIALCHARS : ["$", "_"]>
3350}
3351
3352<DEFAULT,IN_DBL_BRACE>
3353TOKEN :
3354{
3355 // backslash u + 4 hex digits escapes are handled in the underlying JavaCharStream
Yingyi Bu6d57e492016-06-06 21:24:42 -07003356 <QUOTED_STRING : "`" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003357 <EscapeQuot>
3358 | <EscapeBslash>
3359 | <EscapeSlash>
3360 | <EscapeBspace>
3361 | <EscapeFormf>
3362 | <EscapeNl>
3363 | <EscapeCr>
3364 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003365 | ~["`","\\"])* "`">
3366 | <STRING_LITERAL : ("\"" (
Yingyi Bu391f09e2015-10-29 13:49:39 -07003367 <EscapeQuot>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003368 | <EscapeBslash>
3369 | <EscapeSlash>
3370 | <EscapeBspace>
3371 | <EscapeFormf>
3372 | <EscapeNl>
3373 | <EscapeCr>
3374 | <EscapeTab>
Yingyi Bu6d57e492016-06-06 21:24:42 -07003375 | ~["\"","\\"])* "\"")
3376 | ("\'"(
3377 <EscapeApos>
3378 | <EscapeBslash>
3379 | <EscapeSlash>
3380 | <EscapeBspace>
3381 | <EscapeFormf>
3382 | <EscapeNl>
3383 | <EscapeCr>
3384 | <EscapeTab>
3385 | ~["\'","\\"])* "\'")>
Yingyi Bu391f09e2015-10-29 13:49:39 -07003386 | < #EscapeQuot: "\\\"" >
3387 | < #EscapeApos: "\\\'" >
3388 | < #EscapeBslash: "\\\\" >
3389 | < #EscapeSlash: "\\/" >
3390 | < #EscapeBspace: "\\b" >
3391 | < #EscapeFormf: "\\f" >
3392 | < #EscapeNl: "\\n" >
3393 | < #EscapeCr: "\\r" >
3394 | < #EscapeTab: "\\t" >
3395}
3396
3397<DEFAULT,IN_DBL_BRACE>
3398TOKEN :
3399{
3400 <IDENTIFIER : <LETTER> (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
3401}
3402
3403<DEFAULT,IN_DBL_BRACE>
3404SKIP:
3405{
3406 " "
3407 | "\t"
3408 | "\r"
3409 | "\n"
3410}
3411
3412<DEFAULT,IN_DBL_BRACE>
3413SKIP:
3414{
3415 <"//" (~["\n"])* "\n">
3416}
3417
3418<DEFAULT,IN_DBL_BRACE>
3419SKIP:
3420{
3421 <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3422}
3423
3424<DEFAULT,IN_DBL_BRACE>
3425SKIP:
3426{
Yingyi Bu93846a72016-09-13 16:30:39 -07003427 <"--" (~["\n"])* "\n">
3428}
3429
3430
3431<DEFAULT,IN_DBL_BRACE>
3432SKIP:
3433{
3434 <"--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3435}
3436
3437<DEFAULT,IN_DBL_BRACE>
3438SKIP:
3439{
3440 <"#" (~["\n"])* "\n">
3441}
3442
3443
3444<DEFAULT,IN_DBL_BRACE>
3445SKIP:
3446{
3447 <"#" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?>
3448}
3449
3450<DEFAULT,IN_DBL_BRACE>
3451SKIP:
3452{
Yingyi Bu391f09e2015-10-29 13:49:39 -07003453 <"/*"> { pushState(); } : INSIDE_COMMENT
3454}
3455
3456<INSIDE_COMMENT>
3457SPECIAL_TOKEN:
3458{
3459 <"+"(" ")*(~["*"])*>
3460}
3461
3462<INSIDE_COMMENT>
3463SKIP:
3464{
3465 <"/*"> { pushState(); }
3466}
3467
3468<INSIDE_COMMENT>
3469SKIP:
3470{
3471 <"*/"> { popState("*/"); }
3472 | <~[]>
3473}