Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ 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, 'd0', 1), "
+ "(1, 'd1', 1), (2, 'd1', 2), (3, 'd1', 3), (4, 'd1', 2), (5, 'd1', 1), "
+ "(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')",
Expand Down Expand Up @@ -1071,6 +1078,43 @@ 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"};
// 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,",
"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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* 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");
}

@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);
for (double[] values : partitions) {
Column[] expected = processPartition(createProvider(pattern).getDataProcessor(), values);
Column[] actual = processPartition(provider.getDataProcessor(), values);
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);
}
}
}

@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;
}
}
Loading