From d1728308b781c817c2f0591e21e5d7591a5f9a5b Mon Sep 17 00:00:00 2001 From: JackieTien97 Date: Tue, 20 Oct 2026 11:30:55 +0800 Subject: [PATCH 1/2] Fix pattern_match state leaking across partitions --- .../relational/it/db/it/IoTDBWindowTVFIT.java | 40 +++++ .../tvf/PatternMatchTableFunction.java | 16 +- .../tvf/PatternMatchTableFunctionTest.java | 139 ++++++++++++++++++ 3 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java index 2c8a822fb09de..583f1f182e3b4 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java @@ -70,6 +70,10 @@ public class IoTDBWindowTVFIT { "insert into multi_type values (2021-01-01T09:05:00, 'device1', 3, 3, 3.0, 3.0, false, '2', X'02', 2021-01-01T10:00:00, '2021-01-02')", "create table t1 (value double field, value1 int32 field)", "insert into t1 values (1, 1, 0),(2, 2, 0),(3, 1, 0),(4, 1, 0),(5, 1, 0),(6, 1.2, 0),(7, 1, 0),(8, 1, 0),(9, 2, 0),(10, 3, 0),(11, 4, 0),(12, 3, 0),(13, 2, 0),(14, 3, 0),(15, 4, 0),(16, 2, 0),(17, 1, 0),(18, 1, 0),(19, 1, 0),(20, 1, 0),(21, 1, 0),(41, 1, 0),(42, 2, 0),(43, 3, 0),(44, 4, 0),(45, 3, 0),(46, 2, 0),(47, 2, 0),(48, 2, 0),(49, 2, 0),(50, 2, 0),(51, 2, 0),(52, 2, 0)", + "CREATE TABLE pattern_partitions(device_id STRING TAG, value DOUBLE FIELD)", + "INSERT INTO pattern_partitions(time, device_id, value) VALUES " + + "(1, 'd1', 1), (2, 'd1', 2), (3, 'd1', 3), (4, 'd1', 2), (5, 'd1', 1), " + + "(1, 'd2', 10), (2, 'd2', 20), (3, 'd2', 30), (4, 'd2', 20), (5, 'd2', 10)", // M4 table function test data "CREATE TABLE table1(device_id STRING TAG, s1 DOUBLE FIELD, s2 DOUBLE FIELD, s3 STRING FIELD)", "INSERT INTO table1(time, device_id, s1, s2, s3) VALUES (1970-01-01T08:00:00.001+08:00, 'device_1', 15.0, 12.0, 'OK')", @@ -1071,6 +1075,42 @@ public void testPatternMatchFunction() { DATABASE_NAME); } + @Test + public void testPatternMatchPartitions() { + assertPatternMatchPartitions("1,2,3,2,1"); + } + + @Test + public void testPatternMatchRegexPartitions() { + assertPatternMatchPartitions("1,{2,3,2}+,1"); + } + + private void assertPatternMatchPartitions(String pattern) { + String[] expectedHeader = {"match_index", "time", "device_id", "value"}; + String[] expectedRows = { + "0,1970-01-01T00:00:00.001Z,d1,1.0,", + "0,1970-01-01T00:00:00.002Z,d1,2.0,", + "0,1970-01-01T00:00:00.003Z,d1,3.0,", + "0,1970-01-01T00:00:00.004Z,d1,2.0,", + "0,1970-01-01T00:00:00.005Z,d1,1.0,", + "0,1970-01-01T00:00:00.001Z,d2,10.0,", + "0,1970-01-01T00:00:00.002Z,d2,20.0,", + "0,1970-01-01T00:00:00.003Z,d2,30.0,", + "0,1970-01-01T00:00:00.004Z,d2,20.0,", + "0,1970-01-01T00:00:00.005Z,d2,10.0," + }; + tableResultSetEqualTest( + "SELECT match_index, time, device_id, value FROM pattern_match(" + + "DATA => (SELECT time, device_id, value FROM pattern_partitions) " + + "PARTITION BY device_id ORDER BY time, DATA_COL => 'value', PATTERN => '" + + pattern + + "', SMOOTH => 0.0, THRESHOLD => 100.0, WIDTH => 1000.0, HEIGHT => 1000.0, " + + "SMOOTH_ON_PATTERN => false) ORDER BY match_index, device_id, time", + expectedHeader, + expectedRows, + DATABASE_NAME); + } + @Test public void testM4TimeWindowMode() { String[] expectedHeader = diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunction.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunction.java index 801728f8e3882..83bd76b4ab792 100644 --- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunction.java +++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunction.java @@ -175,17 +175,17 @@ public TableFunctionProcessorProvider getProcessorProvider( boolean isPatternFromOrigin = (Boolean) ((MapTableFunctionHandle) tableFunctionHandle).getProperty(SMOOTH_ON_PATTERN); - QetchAlgorithm qetchAlgorithm = new QetchAlgorithm(); - qetchAlgorithm.setThreshold(threshold); - qetchAlgorithm.setSmoothValue(smoothValue); - qetchAlgorithm.setHeightLimit(heightLimit); - qetchAlgorithm.setWidthLimit(widthLimit); - qetchAlgorithm.setIsPatternFromOrigin(isPatternFromOrigin); - qetchAlgorithm.parsePattern2Automaton(pattern); - return new TableFunctionProcessorProvider() { @Override public TableFunctionDataProcessor getDataProcessor() { + // Matching state and pass-through row indexes belong to one input partition. + QetchAlgorithm qetchAlgorithm = new QetchAlgorithm(); + qetchAlgorithm.setThreshold(threshold); + qetchAlgorithm.setSmoothValue(smoothValue); + qetchAlgorithm.setHeightLimit(heightLimit); + qetchAlgorithm.setWidthLimit(widthLimit); + qetchAlgorithm.setIsPatternFromOrigin(isPatternFromOrigin); + qetchAlgorithm.parsePattern2Automaton(pattern); return new ShapeMatchDataProcessor(qetchAlgorithm); } }; diff --git a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java new file mode 100644 index 0000000000000..dab716d093120 --- /dev/null +++ b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java @@ -0,0 +1,139 @@ +/* + * 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.commons.queryengine.plan.relational.function.tvf; + +import org.apache.iotdb.udf.api.relational.access.Record; +import org.apache.iotdb.udf.api.relational.table.MapTableFunctionHandle; +import org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider; +import org.apache.iotdb.udf.api.relational.table.processor.TableFunctionDataProcessor; + +import org.apache.tsfile.block.column.Column; +import org.apache.tsfile.block.column.ColumnBuilder; +import org.apache.tsfile.read.common.block.column.DoubleColumnBuilder; +import org.apache.tsfile.read.common.block.column.IntColumnBuilder; +import org.apache.tsfile.read.common.block.column.LongColumnBuilder; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class PatternMatchTableFunctionTest { + + @Test + public void testPartitionStateIsIsolated() { + assertPartitionStateIsIsolated("1,2,3,2,1"); + } + + @Test + public void testRegexPartitionStateIsIsolated() { + assertPartitionStateIsIsolated("1,{2,3,2}+,1"); + } + + private void assertPartitionStateIsIsolated(String pattern) { + TableFunctionProcessorProvider provider = createProvider(pattern); + double[][] partitions = {{1, 2, 3, 2, 1}, {10, 20, 30, 20, 10}, {2, 3, 2, 1}}; + for (double[] values : partitions) { + Column[] expected = processPartition(createProvider(pattern).getDataProcessor(), values); + Column[] actual = processPartition(provider.getDataProcessor(), values); + assertEquals(values.length, expected[2].getPositionCount()); + assertEquals(values.length, actual[2].getPositionCount()); + for (int i = 0; i < values.length; i++) { + assertEquals(i, actual[2].getLong(i)); + assertEquals(expected[0].getInt(i), actual[0].getInt(i)); + assertEquals(expected[1].getDouble(i), actual[1].getDouble(i), 1e-9); + } + } + } + + @Test + public void testCreatingProcessorDoesNotResetAnotherPartition() { + TableFunctionProcessorProvider provider = createProvider("1,2,3,2,1"); + TableFunctionDataProcessor first = provider.getDataProcessor(); + first.beforeStart(); + List properBuilders = createProperBuilders(); + ColumnBuilder indexes = new LongColumnBuilder(null, 5); + first.process(record(1, 1), properBuilders, indexes); + first.process(record(2, 2), properBuilders, indexes); + + Column[] second = + processPartition(provider.getDataProcessor(), new double[] {10, 20, 30, 20, 10}); + + first.process(record(3, 3), properBuilders, indexes); + first.process(record(4, 2), properBuilders, indexes); + first.process(record(5, 1), properBuilders, indexes); + first.finish(properBuilders, indexes); + first.beforeDestroy(); + + Column[] expected = + processPartition( + createProvider("1,2,3,2,1").getDataProcessor(), new double[] {1, 2, 3, 2, 1}); + Column firstIndexes = indexes.build(); + Column firstSimilarities = properBuilders.get(1).build(); + assertEquals(5, second[2].getPositionCount()); + assertEquals(5, firstIndexes.getPositionCount()); + for (int i = 0; i < 5; i++) { + assertEquals(i, second[2].getLong(i)); + assertEquals(i, firstIndexes.getLong(i)); + assertEquals(expected[1].getDouble(i), firstSimilarities.getDouble(i), 1e-9); + } + } + + private TableFunctionProcessorProvider createProvider(String pattern) { + MapTableFunctionHandle handle = + new MapTableFunctionHandle.Builder() + .addProperty("PATTERN", pattern) + .addProperty("SMOOTH", 0.0) + .addProperty("THRESHOLD", 100.0) + .addProperty("WIDTH", 1000.0) + .addProperty("HEIGHT", 1000.0) + .addProperty("SMOOTH_ON_PATTERN", false) + .build(); + return new PatternMatchTableFunction().getProcessorProvider(handle); + } + + private Column[] processPartition(TableFunctionDataProcessor processor, double[] values) { + List properBuilders = createProperBuilders(); + ColumnBuilder indexes = new LongColumnBuilder(null, values.length); + processor.beforeStart(); + for (int i = 0; i < values.length; i++) { + processor.process(record(i + 1, values[i]), properBuilders, indexes); + } + processor.finish(properBuilders, indexes); + processor.beforeDestroy(); + return new Column[] { + properBuilders.get(0).build(), properBuilders.get(1).build(), indexes.build() + }; + } + + private List createProperBuilders() { + return Arrays.asList(new IntColumnBuilder(null, 5), new DoubleColumnBuilder(null, 5)); + } + + private Record record(long time, double value) { + Record record = mock(Record.class); + when(record.getLong(0)).thenReturn(time); + when(record.getDouble(1)).thenReturn(value); + return record; + } +} From be43631bf2dd190d718d27195780514bc6a2cc67 Mon Sep 17 00:00:00 2001 From: JackieTien97 Date: Tue, 20 Oct 2026 12:13:47 +0800 Subject: [PATCH 2/2] Handle singleton partitions in pattern_match --- .../relational/it/db/it/IoTDBWindowTVFIT.java | 6 +++- .../function/tvf/match/QetchAlgorithm.java | 5 ++++ .../tvf/PatternMatchTableFunctionTest.java | 30 ++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java index 583f1f182e3b4..b2ed057689178 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBWindowTVFIT.java @@ -72,8 +72,11 @@ public class IoTDBWindowTVFIT { "insert into t1 values (1, 1, 0),(2, 2, 0),(3, 1, 0),(4, 1, 0),(5, 1, 0),(6, 1.2, 0),(7, 1, 0),(8, 1, 0),(9, 2, 0),(10, 3, 0),(11, 4, 0),(12, 3, 0),(13, 2, 0),(14, 3, 0),(15, 4, 0),(16, 2, 0),(17, 1, 0),(18, 1, 0),(19, 1, 0),(20, 1, 0),(21, 1, 0),(41, 1, 0),(42, 2, 0),(43, 3, 0),(44, 4, 0),(45, 3, 0),(46, 2, 0),(47, 2, 0),(48, 2, 0),(49, 2, 0),(50, 2, 0),(51, 2, 0),(52, 2, 0)", "CREATE TABLE pattern_partitions(device_id STRING TAG, value DOUBLE FIELD)", "INSERT INTO pattern_partitions(time, device_id, value) VALUES " + + "(1, 'd0', 1), " + "(1, 'd1', 1), (2, 'd1', 2), (3, 'd1', 3), (4, 'd1', 2), (5, 'd1', 1), " - + "(1, 'd2', 10), (2, 'd2', 20), (3, 'd2', 30), (4, 'd2', 20), (5, 'd2', 10)", + + "(1, 'd1_singleton', 10), " + + "(1, 'd2', 10), (2, 'd2', 20), (3, 'd2', 30), (4, 'd2', 20), (5, 'd2', 10), " + + "(1, 'd3', 1)", // M4 table function test data "CREATE TABLE table1(device_id STRING TAG, s1 DOUBLE FIELD, s2 DOUBLE FIELD, s3 STRING FIELD)", "INSERT INTO table1(time, device_id, s1, s2, s3) VALUES (1970-01-01T08:00:00.001+08:00, 'device_1', 15.0, 12.0, 'OK')", @@ -1087,6 +1090,7 @@ public void testPatternMatchRegexPartitions() { private void assertPatternMatchPartitions(String pattern) { String[] expectedHeader = {"match_index", "time", "device_id", "value"}; + // Singleton partitions before, between, and after the matching devices produce no rows. String[] expectedRows = { "0,1970-01-01T00:00:00.001Z,d1,1.0,", "0,1970-01-01T00:00:00.002Z,d1,2.0,", diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/match/QetchAlgorithm.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/match/QetchAlgorithm.java index ac286d53fa8d0..ac52240c0f767 100644 --- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/match/QetchAlgorithm.java +++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/match/QetchAlgorithm.java @@ -602,6 +602,11 @@ private void closeNowSection() { } public void closeNowDataSegment() { + if (nowSection == null) { + // An input with fewer than two points has no section to match. + isNewDataSegment = true; + return; + } // after close operator,either dataLastLastSection or dataLastSection is not null closeNowSection(); if (dataLastLastSection != null && dataLastSection != null) { diff --git a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java index dab716d093120..dcb7467c3706c 100644 --- a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java +++ b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/queryengine/plan/relational/function/tvf/PatternMatchTableFunctionTest.java @@ -50,15 +50,37 @@ public void testRegexPartitionStateIsIsolated() { assertPartitionStateIsIsolated("1,{2,3,2}+,1"); } + @Test + public void testSingletonPartitions() { + assertSingletonPartitions("1,2,3,2,1"); + } + + @Test + public void testRegexSingletonPartitions() { + assertSingletonPartitions("1,{2,3,2}+,1"); + } + + private void assertSingletonPartitions(String pattern) { + assertPartitionStateIsIsolated( + pattern, new double[][] {{1}, {1, 2, 3, 2, 1}, {10}, {10, 20, 30, 20, 10}, {1}}); + } + private void assertPartitionStateIsIsolated(String pattern) { + assertPartitionStateIsIsolated( + pattern, new double[][] {{1, 2, 3, 2, 1}, {10, 20, 30, 20, 10}, {2, 3, 2, 1}}); + } + + private void assertPartitionStateIsIsolated(String pattern, double[][] partitions) { TableFunctionProcessorProvider provider = createProvider(pattern); - double[][] partitions = {{1, 2, 3, 2, 1}, {10, 20, 30, 20, 10}, {2, 3, 2, 1}}; for (double[] values : partitions) { Column[] expected = processPartition(createProvider(pattern).getDataProcessor(), values); Column[] actual = processPartition(provider.getDataProcessor(), values); - assertEquals(values.length, expected[2].getPositionCount()); - assertEquals(values.length, actual[2].getPositionCount()); - for (int i = 0; i < values.length; i++) { + int expectedRows = values.length == 1 ? 0 : values.length; + for (int column = 0; column < actual.length; column++) { + assertEquals(expectedRows, expected[column].getPositionCount()); + assertEquals(expectedRows, actual[column].getPositionCount()); + } + for (int i = 0; i < expectedRows; i++) { assertEquals(i, actual[2].getLong(i)); assertEquals(expected[0].getInt(i), actual[0].getInt(i)); assertEquals(expected[1].getDouble(i), actual[1].getDouble(i), 1e-9);