You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2022/04/18 12:38:15 UTC

[iotdb] branch rel/0.13 updated: [To rel/0.13][IOTDB-2882] Fix unary expression display bug (#5577)

This is an automated email from the ASF dual-hosted git repository.

rong pushed a commit to branch rel/0.13
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/0.13 by this push:
     new cca1e424b0 [To rel/0.13][IOTDB-2882] Fix unary expression display bug (#5577)
cca1e424b0 is described below

commit cca1e424b017c801770471977f040f46bdfe0159
Author: flashzxi <39...@users.noreply.github.com>
AuthorDate: Mon Apr 18 20:38:10 2022 +0800

    [To rel/0.13][IOTDB-2882] Fix unary expression display bug (#5577)
---
 .../iotdb/db/integration/IoTDBSelectSchemaIT.java  | 107 +++++++++++++++++++++
 .../query/expression/unary/NegationExpression.java |   8 +-
 2 files changed, 114 insertions(+), 1 deletion(-)

diff --git a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBSelectSchemaIT.java b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBSelectSchemaIT.java
new file mode 100644
index 0000000000..91a7ce0c59
--- /dev/null
+++ b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBSelectSchemaIT.java
@@ -0,0 +1,107 @@
+/*
+ * 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.integration;
+
+import org.apache.iotdb.integration.env.EnvFactory;
+import org.apache.iotdb.itbase.category.ClusterTest;
+import org.apache.iotdb.itbase.category.LocalStandaloneTest;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+@Category({LocalStandaloneTest.class, ClusterTest.class})
+public class IoTDBSelectSchemaIT {
+  private static String INSERTION_SQLS =
+      "insert into root.sg.d1(time, s1, s2, s3) values (1, 1, 2, 3.0);";
+
+  private static void createTimeSeries() {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("SET STORAGE GROUP TO root.sg");
+      statement.execute("CREATE TIMESERIES root.sg.d1.s1 with datatype=INT32,encoding=PLAIN");
+      statement.execute("CREATE TIMESERIES root.sg.d1.s2 with datatype=INT64,encoding=PLAIN");
+      statement.execute("CREATE TIMESERIES root.sg.d1.s3 with datatype=DOUBLE,encoding=PLAIN");
+    } catch (SQLException throwable) {
+      fail(throwable.getMessage());
+    }
+  }
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    EnvFactory.getEnv().initBeforeClass();
+    createTimeSeries();
+    generateData();
+  }
+
+  private static void generateData() {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute(INSERTION_SQLS);
+    } catch (SQLException throwable) {
+      fail(throwable.getMessage());
+    }
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    EnvFactory.getEnv().cleanAfterClass();
+  }
+
+  @Test
+  public void testSchemaExpression() {
+    String[] expressions = {
+      "s1+s2", "-s1+s2", "-(s1+s3)", "-(-(s1))", "((s1+s2)*s3)",
+    };
+    String[] completeExpressions = {
+      "root.sg.d1.s1+root.sg.d1.s2",
+      "-root.sg.d1.s1+root.sg.d1.s2",
+      "-(root.sg.d1.s1+root.sg.d1.s3)",
+      "-(-root.sg.d1.s1)",
+      "(root.sg.d1.s1+root.sg.d1.s2)*root.sg.d1.s3"
+    };
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      ResultSet resultSet =
+          statement.executeQuery(
+              String.format(
+                  "select %s, %s, %s, %s, %s from root.sg.d1",
+                  expressions[0], expressions[1], expressions[2], expressions[3], expressions[4]));
+      int columnCount = resultSet.getMetaData().getColumnCount();
+      assertEquals(1 + expressions.length, columnCount);
+
+      for (int i = 0; i < expressions.length; ++i) {
+        assertEquals(
+            completeExpressions[i], resultSet.getMetaData().getColumnName(i + 2).replace(" ", ""));
+      }
+    } catch (SQLException throwable) {
+      fail(throwable.getMessage());
+    }
+  }
+}
diff --git a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NegationExpression.java b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NegationExpression.java
index 43c6dc5968..d48e6ae312 100644
--- a/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NegationExpression.java
+++ b/server/src/main/java/org/apache/iotdb/db/query/expression/unary/NegationExpression.java
@@ -153,6 +153,12 @@ public class NegationExpression extends Expression {
 
   @Override
   public String getExpressionStringInternal() {
-    return "-" + expression.toString();
+    if (expression instanceof FunctionExpression
+        || expression instanceof ConstantOperand
+        || expression instanceof TimeSeriesOperand) {
+      return "-" + expression.toString();
+    } else {
+      return "-(" + expression.toString() + ")";
+    }
   }
 }