You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ca...@apache.org on 2024/10/18 02:46:53 UTC

(iotdb) branch add_max_min_support updated: perfect last_by

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

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


The following commit(s) were added to refs/heads/add_max_min_support by this push:
     new d65fd09c2ee perfect last_by
d65fd09c2ee is described below

commit d65fd09c2ee9c17fe135318cac5ddf8887bf0a30
Author: Beyyes <cg...@foxmail.com>
AuthorDate: Fri Oct 18 10:46:38 2024 +0800

    perfect last_by
---
 .../relational/aggregation/LastByAccumulator.java  | 22 +++++++++++----------
 .../plan/relational/sql/parser/AstBuilder.java     | 23 +++++++++++++++++++++-
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/LastByAccumulator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/LastByAccumulator.java
index e7386e8504b..480a1e5a1cf 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/LastByAccumulator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/source/relational/aggregation/LastByAccumulator.java
@@ -37,6 +37,7 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.LAST_BY;
 
 public class LastByAccumulator implements TableAccumulator {
 
@@ -115,7 +116,7 @@ public class LastByAccumulator implements TableAccumulator {
   public void addIntermediate(Column argument) {
     checkArgument(
         argument instanceof BinaryColumn || argument instanceof RunLengthEncodedColumn,
-        "intermediate input and output of Last should be BinaryColumn");
+        "intermediate input and output of LastBy should be BinaryColumn");
 
     for (int i = 0; i < argument.getPositionCount(); i++) {
       if (argument.isNull(i)) {
@@ -126,6 +127,8 @@ public class LastByAccumulator implements TableAccumulator {
       long time = BytesUtils.bytesToLongFromOffset(bytes, Long.BYTES, 0);
       int offset = Long.BYTES;
       boolean isXNull = BytesUtils.bytesToBool(bytes, offset);
+      offset += 1;
+
       if (isXNull) {
         if (!initResult || time > yLastTime) {
           initResult = true;
@@ -178,6 +181,7 @@ public class LastByAccumulator implements TableAccumulator {
     checkArgument(
         columnBuilder instanceof BinaryColumnBuilder,
         "intermediate input and output of LastBy should be BinaryColumn");
+
     if (!initResult) {
       columnBuilder.appendNull();
     } else {
@@ -187,12 +191,7 @@ public class LastByAccumulator implements TableAccumulator {
 
   @Override
   public void evaluateFinal(ColumnBuilder columnBuilder) {
-    if (!initResult) {
-      columnBuilder.appendNull();
-      return;
-    }
-
-    if (xIsNull) {
+    if (!initResult || xIsNull) {
       columnBuilder.appendNull();
       return;
     }
@@ -314,9 +313,10 @@ public class LastByAccumulator implements TableAccumulator {
           break;
         default:
           throw new UnSupportedDataTypeException(
-              String.format("Unsupported data type in Last Aggregation: %s", yDataType));
+              String.format("Unsupported data type: %s in Aggregation: %s", yDataType, LAST_BY));
       }
     } else {
+      // x is time column
       if (yStatistics != null) {
         if (!initResult || yStatistics.getEndTime() > yLastTime) {
           initResult = true;
@@ -369,12 +369,14 @@ public class LastByAccumulator implements TableAccumulator {
             break;
           default:
             throw new UnSupportedDataTypeException(
-                String.format("Unsupported data type in Extreme: %s", xDataType));
+                String.format("Unsupported data type: %s in aggregation %s", xDataType, LAST_BY));
         }
       }
     } catch (IOException e) {
       throw new UnsupportedOperationException(
-          "Failed to serialize intermediate result for AvgAccumulator.", e);
+          String.format(
+              "Failed to serialize intermediate result for Accumulator %s, errorMsg: %s.",
+              LAST_BY, e.getMessage()));
     }
     return byteArrayOutputStream.toByteArray();
   }
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
index 0530327bf8c..e5b74c1bae1 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/sql/parser/AstBuilder.java
@@ -208,6 +208,10 @@ import static org.apache.iotdb.db.queryengine.plan.relational.sql.ast.GroupingSe
 import static org.apache.iotdb.db.queryengine.plan.relational.sql.ast.GroupingSets.Type.ROLLUP;
 import static org.apache.iotdb.db.queryengine.plan.relational.sql.ast.QualifiedName.mapIdentifier;
 import static org.apache.iotdb.db.queryengine.transformation.dag.column.unary.scalar.TableBuiltinScalarFunction.DATE_BIN;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.FIRST;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.FIRST_BY;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.LAST;
+import static org.apache.iotdb.db.utils.constant.SqlConstant.LAST_BY;
 
 public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
 
@@ -1861,7 +1865,7 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
               new DereferenceExpression(getLocation(ctx.label), (Identifier) visit(ctx.label)));
     }
 
-    if (name.toString().equalsIgnoreCase("first") || name.toString().equalsIgnoreCase("last")) {
+    if (name.toString().equalsIgnoreCase(FIRST) || name.toString().equalsIgnoreCase(LAST)) {
       if (arguments.size() == 1) {
         arguments.add(
             new Identifier(
@@ -1877,6 +1881,23 @@ public class AstBuilder extends RelationalSqlBaseVisitor<Node> {
       } else {
         throw parseError("Invalid number of arguments for 'first' or 'last' function", ctx);
       }
+    } else if (name.toString().equalsIgnoreCase(FIRST_BY)
+        || name.toString().equalsIgnoreCase(LAST_BY)) {
+      if (arguments.size() == 2) {
+        arguments.add(
+            new Identifier(
+                TimestampOperand.TIMESTAMP_EXPRESSION_STRING.toLowerCase(Locale.ENGLISH)));
+      } else if (arguments.size() == 3) {
+        check(
+            arguments
+                .get(2)
+                .toString()
+                .equalsIgnoreCase(TimestampOperand.TIMESTAMP_EXPRESSION_STRING),
+            "The third argument of 'first_by' or 'last_by' function must be 'time'",
+            ctx);
+      } else {
+        throw parseError("Invalid number of arguments for 'first_by' or 'last_by' function", ctx);
+      }
     }
 
     return new FunctionCall(getLocation(ctx), name, distinct, arguments);