This is an automated email from the ASF dual-hosted git repository. JackieTien97 pushed a commit to branch codex/fix-pattern-match-partition-state in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit d1728308b781c817c2f0591e21e5d7591a5f9a5b Author: JackieTien97 <[email protected]> AuthorDate: Tue Oct 20 11:30:55 2026 +0800 Fix pattern_match state leaking across partitions --- .../relational/it/db/it/IoTDBWindowTVFIT.java | 40 ++++++ .../function/tvf/PatternMatchTableFunction.java | 16 +-- .../tvf/PatternMatchTableFunctionTest.java | 139 +++++++++++++++++++++ 3 files changed, 187 insertions(+), 8 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 2c8a822fb09..583f1f182e3 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 class IoTDBWindowTVFIT { 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 801728f8e38..83bd76b4ab7 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 class PatternMatchTableFunction implements TableFunction { 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 00000000000..dab716d0931 --- /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<ColumnBuilder> 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<ColumnBuilder> 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<ColumnBuilder> 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; + } +}
