This is an automated email from the ASF dual-hosted git repository.
JackieTien97 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 b702764f564 Reject UDFs in table-model UPDATE statements (#19196)
b702764f564 is described below
commit b702764f5644c0ff5a5e223e7cfea0e2df8edf4d
Author: shuwenwei <[email protected]>
AuthorDate: Tue Nov 10 19:30:52 2026 +0800
Reject UDFs in table-model UPDATE statements (#19196)
---
.../it/db/it/udf/IoTDBSQLFunctionManagementIT.java | 60 ++++++++++++++++++++++
.../iotdb/db/i18n/DataNodeQueryMessages.java | 3 ++
.../iotdb/db/i18n/DataNodeQueryMessages.java | 3 ++
.../db/queryengine/common/MPPQueryContext.java | 11 ++++
.../relational/analyzer/ExpressionAnalyzer.java | 11 ++++
.../relational/analyzer/StatementAnalyzer.java | 40 +++++++++++++++
6 files changed, 128 insertions(+)
diff --git
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBSQLFunctionManagementIT.java
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBSQLFunctionManagementIT.java
index f56da20b051..96571b39c6f 100644
---
a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBSQLFunctionManagementIT.java
+++
b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBSQLFunctionManagementIT.java
@@ -351,4 +351,64 @@ public class IoTDBSQLFunctionManagementIT {
}
}
}
+
+ @Test
+ public void testUdfRejectedInUpdateAndDeleteDevice() throws SQLException {
+ try (Connection connection = EnvFactory.getEnv().getTableConnection();
+ Statement statement = connection.createStatement()) {
+ statement.execute("create database udf_write_guard");
+ try {
+ statement.execute("use udf_write_guard");
+ statement.execute(
+ "create table t(device_id STRING TAG, attr1 STRING ATTRIBUTE, v
INT64 FIELD)");
+ statement.execute(
+ "insert into t(time, device_id, attr1, v) values(1, 'd1', 'a', 1),
(2, 'd2', 'b', 2)");
+ statement.execute(
+ "create function write_guard_udf as
'org.apache.iotdb.db.query.udf.example.relational.ContainNull'");
+
+ assertUdfRejected(
+ statement, "update t set attr1 = write_guard_udf(device_id) where
device_id = 'd1'");
+ assertUdfRejected(
+ statement, "update t set attr1 = 'x' where
write_guard_udf(device_id) = true");
+
+ // DELETE DEVICE currently rejects function expressions before the
UDF-specific check.
+ // Keep this assertion message-agnostic so future changes cannot allow
UDFs here.
+ assertRejected(statement, "delete devices from t where
write_guard_udf(device_id)");
+
+ try (ResultSet resultSet =
+ statement.executeQuery(
+ "select write_guard_udf(device_id) from t where device_id =
'd1'")) {
+ assertTrue(resultSet.next());
+ }
+
+ statement.execute("update t set attr1 = upper(device_id) where
device_id = 'd2'");
+ try (ResultSet resultSet =
+ statement.executeQuery("select attr1 from t where device_id =
'd2'")) {
+ assertTrue(resultSet.next());
+ assertEquals("D2", resultSet.getString(1));
+ }
+ } finally {
+ statement.execute("drop function write_guard_udf");
+ statement.execute("drop database udf_write_guard");
+ }
+ }
+ }
+
+ private static void assertUdfRejected(final Statement statement, final
String sql) {
+ try {
+ statement.execute(sql);
+ fail("Expected external UDF to be rejected in write statement: " + sql);
+ } catch (SQLException e) {
+ assertTrue(e.getMessage(), e.getMessage().contains("External
user-defined function"));
+ }
+ }
+
+ private static void assertRejected(final Statement statement, final String
sql) {
+ try {
+ statement.execute(sql);
+ fail("Expected statement to be rejected: " + sql);
+ } catch (SQLException ignored) {
+ // The current error may come from an earlier expression validation.
+ }
+ }
}
diff --git
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index b84bebec93d..355d5a00176 100644
---
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -901,6 +901,9 @@ public final class DataNodeQueryMessages {
public static final String
EXCEPTION_CANNOT_REFERENCE_ARG_COLUMN_ARG_IN_AN_UPDATE_VALUE_UPDATE_VALUES_CAN_ONLY_REFERENCE_ATTRIBUTE_OR_TAG_COLUMNS_C01BE71A
=
"Cannot reference %s column '%s' in an UPDATE value. UPDATE values
can only reference ATTRIBUTE or TAG columns.";
+ public static final String
+
EXCEPTION_EXTERNAL_USER_DEFINED_FUNCTION_ARG_IS_NOT_SUPPORTED_IN_THIS_WRITE_STATEMENT_8329DC8E
=
+ "External user-defined function %s is not supported in this write
statement";
public static final String DROP_FUNCTION_STATEMENT_IS_NOT_SUPPORTED_YET =
"Drop Function statement is not supported yet.";
public static final String SHOW_FUNCTION_STATEMENT_IS_NOT_SUPPORTED_YET =
diff --git
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index 1aef0814d92..bc8b004bbec 100644
---
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -881,6 +881,9 @@ public final class DataNodeQueryMessages {
public static final String
EXCEPTION_CANNOT_REFERENCE_ARG_COLUMN_ARG_IN_AN_UPDATE_VALUE_UPDATE_VALUES_CAN_ONLY_REFERENCE_ATTRIBUTE_OR_TAG_COLUMNS_C01BE71A
=
"无法在 UPDATE 值中引用 %s 列 '%s'。UPDATE 值只能引用 ATTRIBUTE 或 TAG 列。";
+ public static final String
+
EXCEPTION_EXTERNAL_USER_DEFINED_FUNCTION_ARG_IS_NOT_SUPPORTED_IN_THIS_WRITE_STATEMENT_8329DC8E
=
+ "外部用户自定义函数 %s 不支持在当前写语句中使用";
public static final String DROP_FUNCTION_STATEMENT_IS_NOT_SUPPORTED_YET =
"暂不支持 Drop Function 语句。";
public static final String SHOW_FUNCTION_STATEMENT_IS_NOT_SUPPORTED_YET =
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
index 3d982904dd8..728dd9e7df0 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
@@ -100,6 +100,9 @@ public class MPPQueryContext implements IAuditEntity {
private SessionInfo session;
private QueryType queryType = QueryType.READ;
+ // Set by analyzers for write statements that persist UDF expressions in
schema WAL.
+ private boolean udfForbidden;
+
/** the max executing time of query in ms. Unit: millisecond */
private long timeOut;
@@ -383,6 +386,14 @@ public class MPPQueryContext implements IAuditEntity {
this.queryType = queryType;
}
+ public boolean isUdfForbidden() {
+ return udfForbidden;
+ }
+
+ public void setUdfForbidden(boolean udfForbidden) {
+ this.udfForbidden = udfForbidden;
+ }
+
public ResultNodeContext getResultNodeContext() {
return resultNodeContext;
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
index b95d90e0a27..81b50d01018 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/ExpressionAnalyzer.java
@@ -20,6 +20,7 @@
package org.apache.iotdb.db.queryengine.plan.relational.analyzer;
import org.apache.iotdb.calc.plan.relational.metadata.CommonMetadataUtils;
+import org.apache.iotdb.common.rpc.thrift.Model;
import org.apache.iotdb.commons.exception.SemanticException;
import org.apache.iotdb.commons.queryengine.common.SessionInfo;
import org.apache.iotdb.commons.queryengine.plan.relational.analyzer.NodeRef;
@@ -86,6 +87,7 @@ import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Trim;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.WhenClause;
import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.WindowFrame;
import
org.apache.iotdb.commons.queryengine.plan.relational.type.TypeNotFoundException;
+import org.apache.iotdb.commons.queryengine.plan.udf.UDFManagementService;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
@@ -1054,6 +1056,15 @@ public class ExpressionAnalyzer {
@Override
public Type visitFunctionCall(FunctionCall node,
StackableAstVisitorContext<Context> context) {
String functionName = node.getName().getSuffix();
+ if (ExpressionAnalyzer.this.context.isUdfForbidden()
+ && UDFManagementService.getInstance().getUDFInformation(Model.TABLE,
functionName)
+ != null) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages
+
.EXCEPTION_EXTERNAL_USER_DEFINED_FUNCTION_ARG_IS_NOT_SUPPORTED_IN_THIS_WRITE_STATEMENT_8329DC8E,
+ functionName));
+ }
boolean isAggregation = metadata.isAggregationFunction(session,
functionName, accessControl);
boolean isRowPatternCount =
context.getContext().isPatternRecognition()
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
index 5fe0b18156b..9e73c44952d 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java
@@ -20,6 +20,7 @@
package org.apache.iotdb.db.queryengine.plan.relational.analyzer;
import org.apache.iotdb.calc.plan.relational.metadata.CommonMetadataUtils;
+import org.apache.iotdb.common.rpc.thrift.Model;
import org.apache.iotdb.commons.exception.IoTDBException;
import org.apache.iotdb.commons.exception.IoTDBRuntimeException;
import org.apache.iotdb.commons.exception.SemanticException;
@@ -118,6 +119,7 @@ import
org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.With;
import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.WithQuery;
import org.apache.iotdb.commons.queryengine.plan.relational.type.TypeManager;
import
org.apache.iotdb.commons.queryengine.plan.statement.component.FillPolicy;
+import org.apache.iotdb.commons.queryengine.plan.udf.UDFManagementService;
import org.apache.iotdb.commons.queryengine.utils.cte.CteDataStore;
import org.apache.iotdb.commons.schema.table.TsTable;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
@@ -979,6 +981,7 @@ public class StatementAnalyzer {
sessionContext.getUserName(),
new QualifiedObjectName(node.getDatabase(), node.getTableName()),
queryContext);
+ queryContext.setUdfForbidden(true);
final TranslationMap translationMap = analyzeTraverseDevice(node,
context, true);
final TsTable table =
DataNodeTableCache.getInstance().getTable(node.getDatabase(),
node.getTableName());
@@ -1029,6 +1032,7 @@ public class StatementAnalyzer {
})
.collect(Collectors.toList()));
} else {
+ validateNoUdfInPreAnalyzedUpdate(node);
final Set<String> attributeNames = new HashSet<>();
node.getAssignments()
.forEach(
@@ -1054,6 +1058,39 @@ public class StatementAnalyzer {
return null;
}
+ /**
+ * Validates an UPDATE whose expressions were analyzed by the sender and
are reused directly by
+ * the receiver. This happens for pipe-transferred UPDATE statements,
which have no source
+ * location and also store their predicate in {@link
Update#getTagFuzzyPredicate()} instead of
+ * the regular WHERE expression.
+ */
+ private void validateNoUdfInPreAnalyzedUpdate(final Update node) {
+ final List<Expression> expressions =
+ node.getAssignments().stream()
+ .map(UpdateAssignment::getValue)
+ .collect(Collectors.toList());
+ node.getWhere().ifPresent(expressions::add);
+ if (Objects.nonNull(node.getTagFuzzyPredicate())) {
+ expressions.add(node.getTagFuzzyPredicate());
+ }
+ validateNoUdfInExpressions(expressions);
+ }
+
+ private void validateNoUdfInExpressions(final Iterable<? extends
Expression> expressions) {
+ for (final FunctionCall functionCall :
+ ExpressionTreeUtils.extractExpressions(expressions,
FunctionCall.class)) {
+ final String functionName = functionCall.getName().getSuffix();
+ if (UDFManagementService.getInstance().getUDFInformation(Model.TABLE,
functionName)
+ != null) {
+ throw new SemanticException(
+ String.format(
+ DataNodeQueryMessages
+
.EXCEPTION_EXTERNAL_USER_DEFINED_FUNCTION_ARG_IS_NOT_SUPPORTED_IN_THIS_WRITE_STATEMENT_8329DC8E,
+ functionName));
+ }
+ }
+ }
+
private void validateUpdateAttributeColumn(
final Expression column, final TsTable table, final Set<String>
attributeNames) {
validateUpdateTargetColumn(column, table);
@@ -1164,6 +1201,9 @@ public class StatementAnalyzer {
final TsTable table =
DataNodeTableCache.getInstance().getTable(node.getDatabase(),
node.getTableName());
DataNodeTreeViewSchemaUtils.checkTableInWrite(node.getDatabase(), table);
+ // DELETE DEVICE currently rejects function expressions before
expression analysis. Keep the
+ // guard so future changes cannot silently allow external UDFs into this
schema-write path.
+ queryContext.setUdfForbidden(true);
node.parseModEntries(table, node.getDatabase(), queryContext);
analyzeTraverseDevice(node, context, node.getWhere().isPresent());
node.parseWhere(