You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2025/07/17 01:38:45 UTC

(iotdb) 01/01: Add beforeDestroy in TableFunctionLeafProcessor and TableFunctionDataProcessor

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

jackietien pushed a commit to branch ty/udtf-change
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit ea3cfe717c7ebdc4f9cce04b4cc929dcadcd9a52
Author: JackieTien97 <ja...@gmail.com>
AuthorDate: Wed Jul 16 14:59:08 2025 +0800

    Add beforeDestroy in TableFunctionLeafProcessor and TableFunctionDataProcessor
---
 .../table/processor/TableFunctionDataProcessor.java     |  5 +++++
 .../table/processor/TableFunctionLeafProcessor.java     |  5 +++++
 .../main/java/org/apache/iotdb/rpc/TSStatusCode.java    |  1 +
 .../execution/fragment/FragmentInstanceManager.java     | 10 ++++++++++
 .../process/function/TableFunctionLeafOperator.java     | 17 ++++++++++++++++-
 .../process/function/TableFunctionOperator.java         |  4 ++++
 6 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionDataProcessor.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionDataProcessor.java
index aa78e50f4d8..0fe40fc894b 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionDataProcessor.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionDataProcessor.java
@@ -65,4 +65,9 @@ public interface TableFunctionDataProcessor {
       List<ColumnBuilder> properColumnBuilders, ColumnBuilder passThroughIndexBuilder) {
     // do nothing
   }
+
+  /** This method is mainly used to release the resources used in the UDF. */
+  default void beforeDestroy() {
+    // do nothing
+  }
 }
diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionLeafProcessor.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionLeafProcessor.java
index 41750fc3b53..ab8763f3c4a 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionLeafProcessor.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/processor/TableFunctionLeafProcessor.java
@@ -39,4 +39,9 @@ public interface TableFunctionLeafProcessor {
 
   /** This method is called to determine if the processor has finished processing all data. */
   boolean isFinish();
+
+  /** This method is mainly used to release the resources used in the UDF. */
+  default void beforeDestroy() {
+    // do nothing
+  }
 }
diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
index d64d761631c..7249df56230 100644
--- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
+++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TSStatusCode.java
@@ -222,6 +222,7 @@ public enum TSStatusCode {
   DROP_UDF_ON_DATANODE_ERROR(1203),
   CREATE_UDF_ERROR(1204),
   DROP_UDF_ERROR(1205),
+  EXECUTE_UDF_ERROR(1206),
 
   // Trigger
   CREATE_TRIGGER_ERROR(1300),
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceManager.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceManager.java
index c8fded6e2a8..89d50ce0264 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceManager.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceManager.java
@@ -27,6 +27,7 @@ import org.apache.iotdb.commons.exception.IoTDBException;
 import org.apache.iotdb.commons.exception.IoTDBRuntimeException;
 import org.apache.iotdb.commons.exception.QueryTimeoutException;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.SemanticException;
 import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
 import org.apache.iotdb.db.queryengine.common.QueryId;
 import org.apache.iotdb.db.queryengine.execution.driver.IDriver;
@@ -44,6 +45,9 @@ import org.apache.iotdb.db.schemaengine.schemaregion.ISchemaRegion;
 import org.apache.iotdb.db.storageengine.dataregion.IDataRegionForQuery;
 import org.apache.iotdb.db.utils.SetThreadName;
 import org.apache.iotdb.mpp.rpc.thrift.TFetchFragmentInstanceStatisticsResp;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.udf.api.exception.UDFException;
+import org.apache.iotdb.udf.api.exception.UDFTypeMismatchException;
 
 import io.airlift.units.Duration;
 import org.slf4j.Logger;
@@ -198,6 +202,12 @@ public class FragmentInstanceManager {
                             TOO_MANY_CONCURRENT_QUERIES_ERROR.getStatusCode()));
                   } else if (t instanceof IoTDBRuntimeException) {
                     stateMachine.failed(t);
+                  } else if (t instanceof UDFTypeMismatchException) {
+                    stateMachine.failed(new SemanticException(t.getMessage()));
+                  } else if (t instanceof UDFException) {
+                    stateMachine.failed(
+                        new IoTDBRuntimeException(
+                            t.getMessage(), TSStatusCode.EXECUTE_UDF_ERROR.getStatusCode(), true));
                   } else {
                     logger.warn("error when create FragmentInstanceExecution.", t);
                     stateMachine.failed(t);
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionLeafOperator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionLeafOperator.java
index 47f2ec9de35..476d47f6530 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionLeafOperator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionLeafOperator.java
@@ -19,8 +19,10 @@
 
 package org.apache.iotdb.db.queryengine.execution.operator.process.function;
 
+import org.apache.iotdb.commons.exception.IoTDBRuntimeException;
 import org.apache.iotdb.db.queryengine.execution.operator.OperatorContext;
 import org.apache.iotdb.db.queryengine.execution.operator.process.ProcessOperator;
+import org.apache.iotdb.rpc.TSStatusCode;
 import org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider;
 import org.apache.iotdb.udf.api.relational.table.processor.TableFunctionLeafProcessor;
 
@@ -61,6 +63,12 @@ public class TableFunctionLeafOperator implements ProcessOperator {
   @Override
   public TsBlock next() throws Exception {
     List<ColumnBuilder> columnBuilders = getOutputColumnBuilders();
+    try {
+      processor.process(columnBuilders);
+    } catch (Exception e) {
+      throw new IoTDBRuntimeException(
+          e.getMessage(), TSStatusCode.EXECUTE_UDF_ERROR.getStatusCode(), true);
+    }
     processor.process(columnBuilders);
     return buildTsBlock(columnBuilders);
   }
@@ -82,7 +90,14 @@ public class TableFunctionLeafOperator implements ProcessOperator {
   }
 
   @Override
-  public void close() throws Exception {}
+  public void close() throws Exception {
+    try {
+      processor.beforeDestroy();
+    } catch (Exception e) {
+      throw new IoTDBRuntimeException(
+          e.getMessage(), TSStatusCode.EXECUTE_UDF_ERROR.getStatusCode(), true);
+    }
+  }
 
   @Override
   public boolean isFinished() throws Exception {
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionOperator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionOperator.java
index 4e9f7e436a5..c00e05ec17f 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionOperator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/operator/process/function/TableFunctionOperator.java
@@ -174,6 +174,7 @@ public class TableFunctionOperator implements ProcessOperator {
           processor.finish(properColumnBuilders, passThroughIndexBuilder);
           resultTsBlocks.addAll(buildTsBlock(properColumnBuilders, passThroughIndexBuilder));
           partitionCache.clear();
+          processor.beforeDestroy();
           processor = null;
           return resultTsBlocks.poll();
         } else {
@@ -259,6 +260,9 @@ public class TableFunctionOperator implements ProcessOperator {
   public void close() throws Exception {
     partitionCache.close();
     inputOperator.close();
+    if (processor != null) {
+      processor.beforeDestroy();
+    }
   }
 
   @Override