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

caogaofei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 62e3ecf0c8c Fix select into bug while using alias
62e3ecf0c8c is described below

commit 62e3ecf0c8c37d5e87667a3330715a147ee1c614
Author: Jackie Tien <[email protected]>
AuthorDate: Thu Aug 15 14:40:55 2024 +0800

    Fix select into bug while using alias
---
 .../iotdb/db/it/selectinto/IoTDBSelectIntoIT.java   | 21 +++++++++++++++++++++
 .../db/queryengine/plan/analyze/AnalyzeVisitor.java | 17 ++++++++++++++---
 .../apache/iotdb/db/utils/constant/SqlConstant.java |  3 +++
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/selectinto/IoTDBSelectIntoIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/selectinto/IoTDBSelectIntoIT.java
index 87be3ade269..f6dde78db7f 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/selectinto/IoTDBSelectIntoIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/selectinto/IoTDBSelectIntoIT.java
@@ -570,6 +570,27 @@ public class IoTDBSelectIntoIT {
         queryRetArray);
   }
 
+  @Test
+  public void testAliasAlignByDevice() {
+    String[] intoRetArray =
+        new String[] {
+          "root.sg.d1,s1,root.sg_abd_alias.d1.k1,10,",
+        };
+    resultSetEqualTest(
+        "select s1 as k1 " + "into root.sg_abd_alias.d1(::) from root.sg.d1 
align by device;",
+        selectIntoAlignByDeviceHeader,
+        intoRetArray);
+
+    intoRetArray =
+        new String[] {
+          "k1,root.sg_abd_alias.d2.k1,10,",
+        };
+    resultSetEqualTest(
+        "select s1 as k1 " + "into root.sg_abd_alias.d2(::) from root.sg.d1;",
+        selectIntoHeader,
+        intoRetArray);
+  }
+
   // -------------------------------------- CHECK EXCEPTION 
-------------------------------------
 
   @Test
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
index 81c7ef4fd8a..226bd1ded87 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
@@ -207,6 +207,7 @@ import static 
org.apache.iotdb.db.queryengine.plan.optimization.LimitOffsetPushD
 import static 
org.apache.iotdb.db.queryengine.plan.optimization.LimitOffsetPushDown.pushDownLimitOffsetInGroupByTimeForDevice;
 import static 
org.apache.iotdb.db.schemaengine.schemaregion.view.visitor.GetSourcePathsVisitor.getSourcePaths;
 import static org.apache.iotdb.db.utils.constant.SqlConstant.COUNT_TIME_HEADER;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.ROOT_DOT;
 
 /** This visitor is used to analyze each type of Statement and returns the 
{@link Analysis}. */
 public class AnalyzeVisitor extends StatementVisitor<Analysis, 
MPPQueryContext> {
@@ -2170,13 +2171,15 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
       PartialPath targetDevice = constructTargetDevice(sourceDevice, 
deviceTemplate);
       
deviceViewIntoPathDescriptor.specifyDeviceAlignment(targetDevice.toString(), 
isAlignedDevice);
 
-      for (Expression sourceColumn : sourceColumns) {
+      for (Pair<Expression, String> pair : outputExpressions) {
+        Expression sourceColumn = pair.left;
         String measurementTemplate = 
intoDeviceMeasurementIterator.getMeasurementTemplate();
         String targetMeasurement;
         if (sourceColumn instanceof TimeSeriesOperand) {
           targetMeasurement =
               constructTargetMeasurement(
-                  
sourceDevice.concatAsMeasurementPath(sourceColumn.getExpressionString()),
+                  sourceDevice.concatAsMeasurementPath(
+                      pair.right == null ? sourceColumn.getExpressionString() 
: pair.right),
                   measurementTemplate);
         } else {
           targetMeasurement = measurementTemplate;
@@ -2228,6 +2231,8 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
     IntoComponent.IntoPathIterator intoPathIterator = 
intoComponent.getIntoPathIterator();
     for (Pair<Expression, String> pair : outputExpressions) {
       Expression sourceExpression = pair.left;
+      // if it's really view path, it should start with root.
+      // otherwise it should just be an alias
       String viewPath = pair.right;
       PartialPath deviceTemplate = intoPathIterator.getDeviceTemplate();
       String measurementTemplate = intoPathIterator.getMeasurementTemplate();
@@ -2239,7 +2244,13 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
       if (sourceExpression instanceof TimeSeriesOperand) {
         if (viewPath != null) {
           try {
-            sourcePath = new MeasurementPath(viewPath);
+            // if it's really view path, it should start with root.
+            if (viewPath.startsWith(ROOT_DOT)) {
+              sourcePath = new MeasurementPath(viewPath);
+            } else {
+              // otherwise it should just be an alias
+              sourcePath = new PartialPath(viewPath);
+            }
           } catch (IllegalPathException e) {
             throw new SemanticException(
                 String.format(
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/SqlConstant.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/SqlConstant.java
index 8521e3ad2d1..ba3a4d86d72 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/SqlConstant.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/constant/SqlConstant.java
@@ -35,6 +35,9 @@ public class SqlConstant {
   public static final String NOW_FUNC = "now()";
 
   public static final String ROOT = "root";
+
+  public static final String ROOT_DOT = "root.";
+
   public static final String QUOTE = "'";
   public static final String DQUOTE = "\"";
   public static final String BOOLEAN_TRUE = "true";

Reply via email to