qiaojialin commented on a change in pull request #366: [IOTDB-190] upgrade from antlr3 to antlr4 URL: https://github.com/apache/incubator-iotdb/pull/366#discussion_r320699784
########## File path: server/src/main/java/org/apache/iotdb/db/qp/strategy/ExecuteSqlVisitor.java ########## @@ -0,0 +1,1271 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.qp.strategy; + +import main.antlr4.org.apache.iotdb.db.sql.parse.TSParser; +import main.antlr4.org.apache.iotdb.db.sql.parse.TSParserBaseVisitor; +import org.antlr.v4.runtime.tree.TerminalNode; +import org.apache.iotdb.db.exception.qp.LogicalOperatorException; +import org.apache.iotdb.db.qp.constant.DatetimeUtils; +import org.apache.iotdb.db.qp.constant.SQLConstant; +import org.apache.iotdb.db.qp.logical.RootOperator; +import org.apache.iotdb.db.qp.logical.crud.*; +import org.apache.iotdb.db.qp.logical.sys.AuthorOperator; +import org.apache.iotdb.db.qp.logical.sys.LoadDataOperator; +import org.apache.iotdb.db.qp.logical.sys.MetadataOperator; +import org.apache.iotdb.db.qp.logical.sys.PropertyOperator; +import org.apache.iotdb.db.query.fill.IFill; +import org.apache.iotdb.db.query.fill.LinearFill; +import org.apache.iotdb.db.query.fill.PreviousFill; +import org.apache.iotdb.db.sql.parse.SqlParseException; +import org.apache.iotdb.tsfile.common.conf.TSFileConfig; +import org.apache.iotdb.tsfile.common.constant.TsFileConstant; +import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; +import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; +import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; +import org.apache.iotdb.tsfile.read.common.Path; +import org.apache.iotdb.tsfile.utils.Pair; +import org.apache.iotdb.tsfile.utils.StringContainer; + +import java.time.ZoneId; +import java.util.*; + +import static org.apache.iotdb.db.qp.constant.SQLConstant.*; + +public class ExecuteSqlVisitor extends TSParserBaseVisitor { + private static final String ERR_INCORRECT_AUTHOR_COMMAND = "illegal ast tree in grant author " + + "command, please check you SQL statement"; + + private RootOperator initializedOperator = null; + private SelectOperator selectOp; + private FilterOperator whereOp; + private ZoneId zoneId; + private Map<TSDataType, IFill> fillTypes; + private TSDataType fillClauseDataType; + private Path whereSeriesPath; + + private boolean isAndWhereClause = false; + private boolean isOrWhereClause = false; + private boolean isNotWhereClause = false; + + public ExecuteSqlVisitor(ZoneId zoneId) { + this.zoneId = zoneId; + } + + + @Override + public Object visitStatement(TSParser.StatementContext ctx) { + return visit(ctx.execStatement()); + } + + @Override + public RootOperator visitQueryStatement(TSParser.QueryStatementContext ctx) { + initializedOperator = new QueryOperator(SQLConstant.TOK_QUERY); + visit(ctx.selectClause()); + if (ctx.whereClause() != null) { + visit(ctx.whereClause()); + } + if (ctx.specialClause() != null) { + visit(ctx.specialClause()); + } + return initializedOperator; + } + + @Override + public Object visitX_positiveFloat(TSParser.X_positiveFloatContext ctx) { + return ctx.PositiveFloat().getText(); + } + + @Override + public Object visitX_negativeInteger(TSParser.X_negativeIntegerContext ctx) { + return ctx.NegativeFloat().getText(); + } + + @Override + public Object visitX_positiveIntegerDot(TSParser.X_positiveIntegerDotContext ctx) { + return ctx.PositiveInteger().getText(); + } + + @Override + public Object visitX_negativeIntegerDot(TSParser.X_negativeIntegerDotContext ctx) { + return ctx.NegativeInteger().getText(); + } + + @Override + public Object visitX_unsignedIntegerDotUnsignedInteger(TSParser.X_unsignedIntegerDotUnsignedIntegerContext ctx) { + return ctx.getText(); + } + + @Override + public Object visitX_unsignedIntegerDot(TSParser.X_unsignedIntegerDotContext ctx) { + return ctx.UnsignedInteger().getText(); + } + + @Override + public Object visitX_dotUnsignedInteger(TSParser.X_dotUnsignedIntegerContext ctx) { + return ctx.getText(); + } + + @Override + public Object visitX_unsignedIntegerDoubleInScientificNotationSuffix(TSParser.X_unsignedIntegerDoubleInScientificNotationSuffixContext ctx) { + return ctx.getText(); + } + + @Override + public Object visitX_doubleInScientificNotationSuffix(TSParser.X_doubleInScientificNotationSuffixContext ctx) { + return ctx.getText(); + } + + + @Override + public Object visitIntegerString(TSParser.IntegerStringContext ctx) { + return ctx.integer().getText(); + } + + @Override + public Object visitFloatString(TSParser.FloatStringContext ctx) { + return visit(ctx.floatValue()); + } + + @Override + public Object visitBooleanString(TSParser.BooleanStringContext ctx) { + return ctx.getText(); + } + + @Override + public RootOperator visitInsertStatement(TSParser.InsertStatementContext ctx) { + InsertOperator insertOp = new InsertOperator(SQLConstant.TOK_INSERT); + initializedOperator = insertOp; + + // set select path + Path selectPath = parsePrefixPath(ctx.prefixPath()); + selectOp = new SelectOperator(SQLConstant.TOK_SELECT); + selectOp.addSelectPath(selectPath); + ((SFWOperator) initializedOperator).setSelectOperator(selectOp); + + // set time + long timestamp = (long) visit(ctx.multiValue().time); + insertOp.setTime(timestamp); + + // set measurement list + String[] measurementList = new String[ctx.multidentifier().identifier().size()]; + for (int i = 0; i < measurementList.length; i++) { + measurementList[i] = ctx.multidentifier().identifier(i).getText(); + } + insertOp.setMeasurementList(measurementList); + + // set value list + String[] valueList = new String[ctx.multiValue().numberOrStringWidely().size()]; + for (int i = 0; i < valueList.length; i++) { + valueList[i] = ctx.multiValue().numberOrStringWidely(i).getText(); + } + insertOp.setValueList(valueList); + + return initializedOperator; + } + + public long parseTimeFormat(String timestampStr) { + if (timestampStr == null || timestampStr.trim().equals("")) { + throw new SqlParseException("input timestamp cannot be empty"); + } + if (timestampStr.equalsIgnoreCase(SQLConstant.NOW_FUNC)) { + return System.currentTimeMillis(); + } + try { + return DatetimeUtils.convertDatetimeStrToLong(timestampStr, zoneId); + } catch (Exception e) { + throw new SqlParseException(String + .format("Input time format %s error. " + + "Input like yyyy-MM-dd HH:mm:ss, yyyy-MM-ddTHH:mm:ss or " + + "refer to user document for more info.", timestampStr)); + } + } + + + @Override + public Object visitSelectIndex(TSParser.SelectIndexContext ctx) { + throw new SqlParseException("Not supported TSParser type SELECT_INDEX"); + } + + @Override + public Object visitSelectSimple(TSParser.SelectSimpleContext ctx) { + selectOp = new SelectOperator(SQLConstant.TOK_SELECT); + for (TSParser.ClusteredPathContext cp : ctx.clusteredPath()) { Review comment: ```suggestion for (TSParser.AggregationPathContext cp : ctx.aggregationPath()) { ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services