You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by hu...@apache.org on 2023/06/20 09:19:36 UTC
[iotdb] 01/05: fix sonar (parser)
This is an automated email from the ASF dual-hosted git repository.
hui pushed a commit to branch lmh/fixSonar
in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 0b74365951ec1607781c24537e99c4409d023154
Author: Minghui Liu <li...@foxmail.com>
AuthorDate: Tue Jun 20 10:58:20 2023 +0800
fix sonar (parser)
---
.../plan/expression/multi/FunctionExpression.java | 2 +-
.../iotdb/db/mpp/plan/parser/ASTVisitor.java | 62 +++++++++-------------
.../db/mpp/plan/parser/StatementGenerator.java | 62 +++++++++++++---------
3 files changed, 62 insertions(+), 64 deletions(-)
diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/multi/FunctionExpression.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/multi/FunctionExpression.java
index af515d13179..daa8aad6b9c 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/multi/FunctionExpression.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/expression/multi/FunctionExpression.java
@@ -118,7 +118,7 @@ public class FunctionExpression extends Expression {
return functionType == FunctionType.AGGREGATION_FUNCTION;
}
- public Boolean isBuiltInScalarFunction() {
+ public boolean isBuiltInScalarFunction() {
if (functionType == null) {
initializeFunctionType();
}
diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
index f84065183e8..0a5b71bfeb5 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/ASTVisitor.java
@@ -26,7 +26,6 @@ import org.apache.iotdb.commons.auth.entity.PrivilegeType;
import org.apache.iotdb.commons.cluster.NodeStatus;
import org.apache.iotdb.commons.conf.IoTDBConstant;
import org.apache.iotdb.commons.cq.TimeoutPolicy;
-import org.apache.iotdb.commons.exception.IllegalPathException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.schema.filter.SchemaFilter;
import org.apache.iotdb.commons.schema.filter.impl.DataTypeFilter;
@@ -257,6 +256,11 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
private static final String GROUP_BY_COMMON_ONLY_ONE_MSG =
"Only one of group by time or group by variation/series/session can be supported at a time";
+ private static final String NEGATIVE_TIMESTAMP_ERROR_MSG =
+ "Please set the time >=0 or after 1970-01-01 00:00:00";
+ private static final String LIMIT_CONFIGURATION_ENABLED_ERROR_MSG =
+ "Limit configuration is not enabled, please enable it first.";
+
private static final String IGNORENULL = "IgnoreNull";
private ZoneId zoneId;
@@ -1129,7 +1133,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
BiConsumer<PartialPath, List<PartialPath>> setTargetPathsGroup,
Consumer<IntoItem> setTargetIntoItem) {
// full paths
- if (ctx.fullPath() != null && ctx.fullPath().size() > 0) {
+ if (ctx.fullPath() != null && !ctx.fullPath().isEmpty()) {
List<IoTDBSqlParser.FullPathContext> fullPathContextList = ctx.fullPath();
List<PartialPath> pathList = new ArrayList<>();
for (IoTDBSqlParser.FullPathContext pathContext : fullPathContextList) {
@@ -1140,7 +1144,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
// prefix path and suffix paths
if (ctx.prefixPath() != null
&& ctx.viewSuffixPaths() != null
- && ctx.viewSuffixPaths().size() > 0) {
+ && !ctx.viewSuffixPaths().isEmpty()) {
IoTDBSqlParser.PrefixPathContext prefixPathContext = ctx.prefixPath();
List<IoTDBSqlParser.ViewSuffixPathsContext> suffixPathContextList = ctx.viewSuffixPaths();
List<PartialPath> suffixPathList = new ArrayList<>();
@@ -1180,7 +1184,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
BiConsumer<PartialPath, List<PartialPath>> setSourcePathsGroup,
Consumer<QueryStatement> setSourceQueryStatement) {
// full paths
- if (ctx.fullPath() != null && ctx.fullPath().size() > 0) {
+ if (ctx.fullPath() != null && !ctx.fullPath().isEmpty()) {
List<IoTDBSqlParser.FullPathContext> fullPathContextList = ctx.fullPath();
List<PartialPath> pathList = new ArrayList<>();
for (IoTDBSqlParser.FullPathContext pathContext : fullPathContextList) {
@@ -1191,7 +1195,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
// prefix path and suffix paths
if (ctx.prefixPath() != null
&& ctx.viewSuffixPaths() != null
- && ctx.viewSuffixPaths().size() > 0) {
+ && !ctx.viewSuffixPaths().isEmpty()) {
IoTDBSqlParser.PrefixPathContext prefixPathContext = ctx.prefixPath();
PartialPath prefixPath = parsePrefixPath(prefixPathContext);
List<IoTDBSqlParser.ViewSuffixPathsContext> suffixPathContextList = ctx.viewSuffixPaths();
@@ -1549,10 +1553,10 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
GroupByAttributeClauseContext ctx, WindowType windowType) {
boolean ignoringNull = true;
- if (ctx.attributePair() != null && !ctx.attributePair().isEmpty()) {
- if (ctx.attributePair().key.getText().equalsIgnoreCase(IGNORENULL)) {
- ignoringNull = Boolean.parseBoolean(ctx.attributePair().value.getText());
- }
+ if (ctx.attributePair() != null
+ && !ctx.attributePair().isEmpty()
+ && ctx.attributePair().key.getText().equalsIgnoreCase(IGNORENULL)) {
+ ignoringNull = Boolean.parseBoolean(ctx.attributePair().value.getText());
}
List<ExpressionContext> expressions = ctx.expression();
if (windowType == WindowType.VARIATION_WINDOW) {
@@ -1953,10 +1957,6 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
return new PartialPath(path);
}
- private PartialPath convertConstantToPath(String src) throws IllegalPathException {
- return new PartialPath(src);
- }
-
private String parseNodeName(IoTDBSqlParser.NodeNameContext ctx) {
if (!useWildcard && !ctx.wildcard().isEmpty()) {
useWildcard = true;
@@ -2085,11 +2085,10 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
}
private String parseStringLiteralInInsertValue(String src) {
- if (2 <= src.length()) {
- if ((src.charAt(0) == '\"' && src.charAt(src.length() - 1) == '\"')
- || (src.charAt(0) == '\'' && src.charAt(src.length() - 1) == '\'')) {
- return "'" + parseStringLiteral(src) + "'";
- }
+ if (2 <= src.length()
+ && ((src.charAt(0) == '\"' && src.charAt(src.length() - 1) == '\"')
+ || (src.charAt(0) == '\'' && src.charAt(src.length() - 1) == '\''))) {
+ return "'" + parseStringLiteral(src) + "'";
}
return src;
}
@@ -3127,7 +3126,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
} else if (ctx.READONLY() != null) {
setSystemStatusStatement.setStatus(NodeStatus.ReadOnly);
} else {
- throw new RuntimeException("Unknown system status in set system command.");
+ throw new SemanticException("Unknown system status in set system command.");
}
return setSystemStatusStatement;
}
@@ -3495,17 +3494,6 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
return attributes;
}
- private PartialPath parsePathFromExpression(Expression expression) {
- if (expression instanceof TimeSeriesOperand) {
- return ((TimeSeriesOperand) expression).getPath();
- } else if (expression instanceof TimestampOperand) {
- return SqlConstant.TIME_PATH;
- } else {
- throw new IllegalArgumentException(
- "Unsupported expression type: " + expression.getExpressionType());
- }
- }
-
// PIPE
@Override
@@ -3636,7 +3624,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
if (ctx.time != null) {
long timestamp = parseTimeValue(ctx.time, DateTimeUtils.currentTime());
if (timestamp < 0) {
- throw new SemanticException("Please set the time >=0 or after 1970-01-01 00:00:00");
+ throw new SemanticException(NEGATIVE_TIMESTAMP_ERROR_MSG);
} else {
getRegionIdStatement.setTimeStamp(timestamp);
}
@@ -3664,7 +3652,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
if (ctx.startTime != null) {
long timestamp = parseTimeValue(ctx.startTime, DateTimeUtils.currentTime());
if (timestamp < 0) {
- throw new SemanticException("Please set the time >=0 or after 1970-01-01 00:00:00");
+ throw new SemanticException(NEGATIVE_TIMESTAMP_ERROR_MSG);
} else {
getTimeSlotListStatement.setStartTime(timestamp);
}
@@ -3672,7 +3660,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
if (ctx.endTime != null) {
long timestamp = parseTimeValue(ctx.endTime, DateTimeUtils.currentTime());
if (timestamp < 0) {
- throw new SemanticException("Please set the time >=0 or after 1970-01-01 00:00:00");
+ throw new SemanticException(NEGATIVE_TIMESTAMP_ERROR_MSG);
} else {
getTimeSlotListStatement.setEndTime(timestamp);
}
@@ -3711,7 +3699,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
@Override
public Statement visitSetSpaceQuota(IoTDBSqlParser.SetSpaceQuotaContext ctx) {
if (!IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
- throw new SemanticException("Limit configuration is not enabled, please enable it first.");
+ throw new SemanticException(LIMIT_CONFIGURATION_ENABLED_ERROR_MSG);
}
SetSpaceQuotaStatement setSpaceQuotaStatement = new SetSpaceQuotaStatement();
List<IoTDBSqlParser.PrefixPathContext> prefixPathContexts = ctx.prefixPath();
@@ -3778,7 +3766,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
@Override
public Statement visitSetThrottleQuota(IoTDBSqlParser.SetThrottleQuotaContext ctx) {
if (!IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
- throw new SemanticException("Limit configuration is not enabled, please enable it first.");
+ throw new SemanticException(LIMIT_CONFIGURATION_ENABLED_ERROR_MSG);
}
if (parseIdentifier(ctx.userName.getText()).equals(IoTDBConstant.PATH_ROOT)) {
throw new SemanticException("Cannot set throttle quota for user root.");
@@ -3878,7 +3866,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
@Override
public Statement visitShowThrottleQuota(IoTDBSqlParser.ShowThrottleQuotaContext ctx) {
if (!IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
- throw new SemanticException("Limit configuration is not enabled, please enable it first.");
+ throw new SemanticException(LIMIT_CONFIGURATION_ENABLED_ERROR_MSG);
}
ShowThrottleQuotaStatement showThrottleQuotaStatement = new ShowThrottleQuotaStatement();
if (ctx.userName != null) {
@@ -3952,7 +3940,7 @@ public class ASTVisitor extends IoTDBSqlParserBaseVisitor<Statement> {
@Override
public Statement visitShowSpaceQuota(IoTDBSqlParser.ShowSpaceQuotaContext ctx) {
if (!IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
- throw new SemanticException("Limit configuration is not enabled, please enable it first.");
+ throw new SemanticException(LIMIT_CONFIGURATION_ENABLED_ERROR_MSG);
}
ShowSpaceQuotaStatement showSpaceQuotaStatement = new ShowSpaceQuotaStatement();
if (ctx.prefixPath() != null) {
diff --git a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/StatementGenerator.java b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/StatementGenerator.java
index 5fc4f3a2348..5cf6c5aad6b 100644
--- a/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/StatementGenerator.java
+++ b/server/src/main/java/org/apache/iotdb/db/mpp/plan/parser/StatementGenerator.java
@@ -124,6 +124,10 @@ public class StatementGenerator {
private static final DataNodeDevicePathCache DEVICE_PATH_CACHE =
DataNodeDevicePathCache.getInstance();
+ private StatementGenerator() {
+ // forbidding instantiation
+ }
+
public static Statement createStatement(String sql, ZoneId zoneId) {
return invokeParser(sql, zoneId);
}
@@ -285,7 +289,7 @@ public class StatementGenerator {
}
public static InsertRowStatement createStatement(TSInsertStringRecordReq insertRecordReq)
- throws IllegalPathException, QueryProcessException {
+ throws IllegalPathException {
final long startTime = System.nanoTime();
// construct insert statement
InsertRowStatement insertStatement = new InsertRowStatement();
@@ -395,7 +399,7 @@ public class StatementGenerator {
}
public static InsertRowsStatement createStatement(TSInsertStringRecordsReq req)
- throws IllegalPathException, QueryProcessException {
+ throws IllegalPathException {
final long startTime = System.nanoTime();
// construct insert statement
InsertRowsStatement insertStatement = new InsertRowsStatement();
@@ -507,16 +511,16 @@ public class StatementGenerator {
CreateAlignedTimeSeriesStatement statement = new CreateAlignedTimeSeriesStatement();
statement.setDevicePath(new PartialPath(req.prefixPath));
List<TSDataType> dataTypes = new ArrayList<>();
- for (int dataType : req.dataTypes) {
- dataTypes.add(TSDataType.deserialize((byte) dataType));
+ for (Integer dataType : req.dataTypes) {
+ dataTypes.add(TSDataType.deserialize(dataType.byteValue()));
}
List<TSEncoding> encodings = new ArrayList<>();
- for (int encoding : req.encodings) {
- encodings.add(TSEncoding.deserialize((byte) encoding));
+ for (Integer encoding : req.encodings) {
+ encodings.add(TSEncoding.deserialize(encoding.byteValue()));
}
List<CompressionType> compressors = new ArrayList<>();
- for (int compressor : req.compressors) {
- compressors.add(CompressionType.deserialize((byte) compressor));
+ for (Integer compressor : req.compressors) {
+ compressors.add(CompressionType.deserialize(compressor.byteValue()));
}
statement.setMeasurements(req.measurements);
statement.setDataTypes(dataTypes);
@@ -538,16 +542,16 @@ public class StatementGenerator {
paths.add(new PartialPath(path));
}
List<TSDataType> dataTypes = new ArrayList<>();
- for (int dataType : req.dataTypes) {
- dataTypes.add(TSDataType.deserialize((byte) dataType));
+ for (Integer dataType : req.dataTypes) {
+ dataTypes.add(TSDataType.deserialize(dataType.byteValue()));
}
List<TSEncoding> encodings = new ArrayList<>();
- for (int encoding : req.encodings) {
- encodings.add(TSEncoding.deserialize((byte) encoding));
+ for (Integer encoding : req.encodings) {
+ encodings.add(TSEncoding.deserialize(encoding.byteValue()));
}
List<CompressionType> compressors = new ArrayList<>();
- for (int compressor : req.compressors) {
- compressors.add(CompressionType.deserialize((byte) compressor));
+ for (Integer compressor : req.compressors) {
+ compressors.add(CompressionType.deserialize(compressor.byteValue()));
}
CreateMultiTimeSeriesStatement statement = new CreateMultiTimeSeriesStatement();
statement.setPaths(paths);
@@ -603,7 +607,7 @@ public class StatementGenerator {
List<List<TSEncoding>> encodings = new ArrayList<>();
List<List<CompressionType>> compressors = new ArrayList<>();
- String templateName = ReadWriteIOUtils.readString(buffer);
+ ReadWriteIOUtils.readString(buffer); // skip template name
boolean isAlign = ReadWriteIOUtils.readBool(buffer);
if (isAlign) {
alignedPrefix.put("", new ArrayList<>());
@@ -650,18 +654,20 @@ public class StatementGenerator {
}
}
- for (String prefix : alignedPrefix.keySet()) {
+ for (Map.Entry<String, List<String>> alignedPrefixEntry : alignedPrefix.entrySet()) {
+ String prefix = alignedPrefixEntry.getKey();
+ List<String> alignedMeasurements = alignedPrefixEntry.getValue();
+
List<String> thisMeasurements = new ArrayList<>();
List<TSDataType> thisDataTypes = new ArrayList<>();
List<TSEncoding> thisEncodings = new ArrayList<>();
List<CompressionType> thisCompressors = new ArrayList<>();
- for (int i = 0; i < alignedPrefix.get(prefix).size(); i++) {
+ for (int i = 0; i < alignedMeasurements.size(); i++) {
if ("".equals(prefix)) {
- thisMeasurements.add(alignedPrefix.get(prefix).get(i));
+ thisMeasurements.add(alignedMeasurements.get(i));
} else {
- thisMeasurements.add(
- prefix + TsFileConstant.PATH_SEPARATOR + alignedPrefix.get(prefix).get(i));
+ thisMeasurements.add(prefix + TsFileConstant.PATH_SEPARATOR + alignedMeasurements.get(i));
}
thisDataTypes.add(alignedDataTypes.get(prefix).get(i));
thisEncodings.add(alignedEncodings.get(prefix).get(i));
@@ -673,6 +679,7 @@ public class StatementGenerator {
encodings.add(thisEncodings);
compressors.add(thisCompressors);
}
+
CreateSchemaTemplateStatement statement =
new CreateSchemaTemplateStatement(
req.getName(), measurements, dataTypes, encodings, compressors, isAlign);
@@ -778,8 +785,8 @@ public class StatementGenerator {
try {
// STAGE 1: try with simpler/faster SLL(*)
tree = parser1.singleStatement();
- // if we get here, there was no syntax error and SLL(*) was enough;
- // there is no need to try full LL(*)
+ // if we get here, there was no syntax error and SLL(*) was enough; there is no need to try
+ // full LL(*)
} catch (Exception ex) {
CharStream charStream2 = CharStreams.fromString(sql);
@@ -889,11 +896,14 @@ public class StatementGenerator {
new TimestampOperand(), new ConstantOperand(TSDataType.INT64, times[1]));
predictNum += 2;
}
- whereCondition.setPredicate(
- predictNum == 3
- ? new LogicAndExpression(leftPredicate, rightPredicate)
- : (predictNum == 1 ? leftPredicate : rightPredicate));
+ if (predictNum == 3) {
+ whereCondition.setPredicate(new LogicAndExpression(leftPredicate, rightPredicate));
+ } else if (predictNum == 1) {
+ whereCondition.setPredicate(leftPredicate);
+ } else {
+ whereCondition.setPredicate(rightPredicate);
+ }
queryStatement.setWhereCondition(whereCondition);
}
return queryStatement;