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/06/24 03:28:29 UTC
(iotdb) branch master updated: Add new parameter [ignoreNull] and supported data type for VARIATION Function (#15628)
This is an automated email from the ASF dual-hosted git repository.
jackietien 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 71ef45d9e1c Add new parameter [ignoreNull] and supported data type for VARIATION Function (#15628)
71ef45d9e1c is described below
commit 71ef45d9e1ceb452f4ab1045ab3648c6b5ba2bb2
Author: Chen YZ <43...@users.noreply.github.com>
AuthorDate: Tue Jun 24 11:28:24 2025 +0800
Add new parameter [ignoreNull] and supported data type for VARIATION Function (#15628)
---
.../relational/it/db/it/IoTDBWindowTVFIT.java | 175 ++++++++++++++++++
.../UDFColumnNotFoundException.java} | 18 +-
.../UDFTypeMismatchException.java} | 18 +-
.../table/argument/ScalarArgumentChecker.java | 8 +
.../java/org/apache/iotdb/udf/api/type/Type.java | 10 ++
.../relational/tvf/VariationTableFunction.java | 196 ++++++++++++++++++---
.../udf/builtin/relational/tvf/WindowTVFUtils.java | 6 +-
7 files changed, 382 insertions(+), 49 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 824d93c5fbe..b66f10e6262 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
@@ -32,6 +32,8 @@ import org.junit.runner.RunWith;
import java.sql.Connection;
import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
@@ -52,6 +54,13 @@ public class IoTDBWindowTVFIT {
"insert into bid values (2021-01-01T09:06:00, 'TESL', 200.0, 102)",
"insert into bid values (2021-01-01T09:07:00, 'TESL', 202.0, 202)",
"insert into bid values (2021-01-01T09:15:00, 'TESL', 195.0, 332)",
+ "create table multi_type (device_id string tag, int_val int32 field, long_val int64 field, float_val float field, double_val double field, bool_val boolean field, str_val string field, blob_val blob field, ts_val timestamp field, date_val date field)",
+ "insert into multi_type values (2021-01-01T09:00:00, 'device1', 1, null, 1.0, null, null, '1', null, 2021-01-01T09:00:00, null)",
+ "insert into multi_type values (2021-01-01T09:01:00, 'device1', 2, 2, 2.0, 2.0, true, '1', X'01', 2021-01-01T09:00:00, '2021-01-01')",
+ "insert into multi_type values (2021-01-01T09:02:00, 'device1', 3, 3, 3.0, 3.0, null, '2', X'02', 2021-01-01T10:00:00, '2021-01-02')",
+ "insert into multi_type values (2021-01-01T09:03:00, 'device1', null, null, null, null, false, null, null, null, null)",
+ "insert into multi_type values (2021-01-01T09:04:00, 'device1', null, null, null, null, null, null, null, null, null)",
+ "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')",
"FLUSH",
"CLEAR ATTRIBUTE CACHE",
};
@@ -227,6 +236,172 @@ public class IoTDBWindowTVFIT {
DATABASE_NAME);
}
+ @Test
+ public void testVariationWithMultiTypeAndNullValue() {
+ // Test case 1: int_val with delta=1.0, ignore_null=false
+ String[] expectedHeader = new String[] {"window_index", "time", "device_id", "int_val"};
+ String[] retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,1,",
+ "0,2021-01-01T09:01:00.000Z,device1,2,",
+ "1,2021-01-01T09:02:00.000Z,device1,3,",
+ "2,2021-01-01T09:03:00.000Z,device1,null,",
+ "2,2021-01-01T09:04:00.000Z,device1,null,",
+ "3,2021-01-01T09:05:00.000Z,device1,3,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, int_val FROM variation(multi_type, 'int_val', 1.0, false)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 2: long_val column with delta=1.0, ignore_null=false
+ expectedHeader = new String[] {"window_index", "time", "device_id", "long_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,null,",
+ "1,2021-01-01T09:01:00.000Z,device1,2,",
+ "1,2021-01-01T09:02:00.000Z,device1,3,",
+ "2,2021-01-01T09:03:00.000Z,device1,null,",
+ "2,2021-01-01T09:04:00.000Z,device1,null,",
+ "3,2021-01-01T09:05:00.000Z,device1,3,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, long_val FROM variation(multi_type, 'long_val', 1.0, false)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 3: float_val with delta=1.0, ignore_null=true
+ expectedHeader = new String[] {"window_index", "time", "device_id", "float_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,1.0,",
+ "0,2021-01-01T09:01:00.000Z,device1,2.0,",
+ "1,2021-01-01T09:02:00.000Z,device1,3.0,",
+ "1,2021-01-01T09:03:00.000Z,device1,null,",
+ "1,2021-01-01T09:04:00.000Z,device1,null,",
+ "1,2021-01-01T09:05:00.000Z,device1,3.0,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, float_val FROM variation(multi_type, 'float_val', 1.0, true)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 4: double_val with delta=1.0, ignore_null=true
+ expectedHeader = new String[] {"window_index", "time", "device_id", "double_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,null,",
+ "0,2021-01-01T09:01:00.000Z,device1,2.0,",
+ "0,2021-01-01T09:02:00.000Z,device1,3.0,",
+ "0,2021-01-01T09:03:00.000Z,device1,null,",
+ "0,2021-01-01T09:04:00.000Z,device1,null,",
+ "0,2021-01-01T09:05:00.000Z,device1,3.0,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, double_val FROM variation(multi_type, 'double_val', 1.0, true)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 5: bool_val with delta=0.0, ignore_null=true
+ expectedHeader = new String[] {"window_index", "time", "device_id", "bool_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,null,",
+ "0,2021-01-01T09:01:00.000Z,device1,true,",
+ "0,2021-01-01T09:02:00.000Z,device1,null,",
+ "1,2021-01-01T09:03:00.000Z,device1,false,",
+ "1,2021-01-01T09:04:00.000Z,device1,null,",
+ "1,2021-01-01T09:05:00.000Z,device1,false,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, bool_val FROM variation(multi_type, 'bool_val', 0.0, true)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 6: str_val with delta=0.0, ignore_null=true
+ expectedHeader = new String[] {"window_index", "time", "device_id", "str_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,1,",
+ "0,2021-01-01T09:01:00.000Z,device1,1,",
+ "1,2021-01-01T09:02:00.000Z,device1,2,",
+ "1,2021-01-01T09:03:00.000Z,device1,null,",
+ "1,2021-01-01T09:04:00.000Z,device1,null,",
+ "1,2021-01-01T09:05:00.000Z,device1,2,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, str_val FROM variation(multi_type, 'str_val', 0.0, true)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 7: blob_val with delta=0.0, ignore_null=false
+ expectedHeader = new String[] {"window_index", "time", "device_id", "blob_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,null,",
+ "1,2021-01-01T09:01:00.000Z,device1,0x01,",
+ "2,2021-01-01T09:02:00.000Z,device1,0x02,",
+ "3,2021-01-01T09:03:00.000Z,device1,null,",
+ "3,2021-01-01T09:04:00.000Z,device1,null,",
+ "4,2021-01-01T09:05:00.000Z,device1,0x02,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, blob_val FROM variation(multi_type, 'blob_val', 0.0, false)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 8: ts_val with delta=0.0, ignore_null=false
+ expectedHeader = new String[] {"window_index", "time", "device_id", "ts_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,2021-01-01T09:00:00.000Z,",
+ "0,2021-01-01T09:01:00.000Z,device1,2021-01-01T09:00:00.000Z,",
+ "1,2021-01-01T09:02:00.000Z,device1,2021-01-01T10:00:00.000Z,",
+ "2,2021-01-01T09:03:00.000Z,device1,null,",
+ "2,2021-01-01T09:04:00.000Z,device1,null,",
+ "3,2021-01-01T09:05:00.000Z,device1,2021-01-01T10:00:00.000Z,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, ts_val FROM variation(DATA=>multi_type, COL=>'ts_val', ignore_null=>false)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 9: date_val with delta=0.0, ignore_null=true (use default parameter)
+ expectedHeader = new String[] {"window_index", "time", "device_id", "date_val"};
+ retArray =
+ new String[] {
+ "0,2021-01-01T09:00:00.000Z,device1,null,",
+ "0,2021-01-01T09:01:00.000Z,device1,2021-01-01,",
+ "1,2021-01-01T09:02:00.000Z,device1,2021-01-02,",
+ "1,2021-01-01T09:03:00.000Z,device1,null,",
+ "1,2021-01-01T09:04:00.000Z,device1,null,",
+ "1,2021-01-01T09:05:00.000Z,device1,2021-01-02,"
+ };
+ tableResultSetEqualTest(
+ "SELECT window_index, time, device_id, date_val FROM variation(multi_type, 'date_val', 0.0)",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // Test case 10: check illegal type when delta!=0
+ List<String> illegalCol =
+ Arrays.asList("bool_val", "str_val", "blob_val", "ts_val", "date_val");
+ for (String col : illegalCol) {
+ tableAssertTestFail(
+ "SELECT * FROM variation(multi_type, '" + col + "', 1.0, true)",
+ "The column type must be numeric if DELTA is not 0",
+ DATABASE_NAME);
+ }
+ }
+
@Test
public void testCapacityFunction() {
String[] expectedHeader = new String[] {"window_index", "time", "stock_id", "price", "s1"};
diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFColumnNotFoundException.java
similarity index 69%
copy from iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
copy to iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFColumnNotFoundException.java
index 241ce5939bc..4912269d865 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFColumnNotFoundException.java
@@ -17,16 +17,14 @@
* under the License.
*/
-package org.apache.iotdb.udf.api.relational.table.argument;
+package org.apache.iotdb.udf.api.exception;
-import java.util.function.Function;
+public class UDFColumnNotFoundException extends UDFException {
+ public UDFColumnNotFoundException(String message) {
+ super(message);
+ }
-public class ScalarArgumentChecker {
- public static Function<Object, String> POSITIVE_LONG_CHECKER =
- (value) -> {
- if (value instanceof Long && (Long) value > 0) {
- return null;
- }
- return "should be a positive value";
- };
+ public UDFColumnNotFoundException(String message, Throwable cause) {
+ super(message, cause);
+ }
}
diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFTypeMismatchException.java
similarity index 69%
copy from iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
copy to iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFTypeMismatchException.java
index 241ce5939bc..571bc8c6c72 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/exception/UDFTypeMismatchException.java
@@ -17,16 +17,14 @@
* under the License.
*/
-package org.apache.iotdb.udf.api.relational.table.argument;
+package org.apache.iotdb.udf.api.exception;
-import java.util.function.Function;
+public class UDFTypeMismatchException extends UDFException {
+ public UDFTypeMismatchException(String message) {
+ super(message);
+ }
-public class ScalarArgumentChecker {
- public static Function<Object, String> POSITIVE_LONG_CHECKER =
- (value) -> {
- if (value instanceof Long && (Long) value > 0) {
- return null;
- }
- return "should be a positive value";
- };
+ public UDFTypeMismatchException(String message, Throwable cause) {
+ super(message, cause);
+ }
}
diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
index 241ce5939bc..125f6539101 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/relational/table/argument/ScalarArgumentChecker.java
@@ -29,4 +29,12 @@ public class ScalarArgumentChecker {
}
return "should be a positive value";
};
+
+ public static Function<Object, String> NON_NEGATIVE_DOUBLE_CHECKER =
+ (value) -> {
+ if (value instanceof Double && (Double) value >= 0) {
+ return null;
+ }
+ return "should be a non-negative value";
+ };
}
diff --git a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/type/Type.java b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/type/Type.java
index 8002d7b7ce5..a5c2852ac1b 100644
--- a/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/type/Type.java
+++ b/iotdb-api/udf-api/src/main/java/org/apache/iotdb/udf/api/type/Type.java
@@ -22,6 +22,8 @@ package org.apache.iotdb.udf.api.type;
import org.apache.tsfile.utils.Binary;
import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.List;
/** A substitution class for TsDataType in UDF APIs. */
public enum Type {
@@ -98,4 +100,12 @@ public enum Type {
return false;
}
}
+
+ public static List<Type> allTypes() {
+ return Arrays.asList(BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, DATE, BLOB, STRING);
+ }
+
+ public static List<Type> numericTypes() {
+ return Arrays.asList(INT32, INT64, FLOAT, DOUBLE);
+ }
}
diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/VariationTableFunction.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/VariationTableFunction.java
index 284f623f301..5df71f59638 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/VariationTableFunction.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/VariationTableFunction.java
@@ -20,6 +20,7 @@
package org.apache.iotdb.commons.udf.builtin.relational.tvf;
import org.apache.iotdb.udf.api.exception.UDFException;
+import org.apache.iotdb.udf.api.exception.UDFTypeMismatchException;
import org.apache.iotdb.udf.api.relational.TableFunction;
import org.apache.iotdb.udf.api.relational.access.Record;
import org.apache.iotdb.udf.api.relational.table.MapTableFunctionHandle;
@@ -45,11 +46,13 @@ import java.util.List;
import java.util.Map;
import static org.apache.iotdb.commons.udf.builtin.relational.tvf.WindowTVFUtils.findColumnIndex;
+import static org.apache.iotdb.udf.api.relational.table.argument.ScalarArgumentChecker.NON_NEGATIVE_DOUBLE_CHECKER;
public class VariationTableFunction implements TableFunction {
private static final String DATA_PARAMETER_NAME = "DATA";
private static final String COL_PARAMETER_NAME = "COL";
private static final String DELTA_PARAMETER_NAME = "DELTA";
+ private static final String IGNORE_NULL_PARAMETER_NAME = "IGNORE_NULL";
@Override
public List<ParameterSpecification> getArgumentsSpecifications() {
@@ -62,6 +65,13 @@ public class VariationTableFunction implements TableFunction {
ScalarParameterSpecification.builder()
.name(DELTA_PARAMETER_NAME)
.type(Type.DOUBLE)
+ .defaultValue(0.0)
+ .addChecker(NON_NEGATIVE_DOUBLE_CHECKER)
+ .build(),
+ ScalarParameterSpecification.builder()
+ .name(IGNORE_NULL_PARAMETER_NAME)
+ .type(Type.BOOLEAN)
+ .defaultValue(true)
.build());
}
@@ -70,19 +80,31 @@ public class VariationTableFunction implements TableFunction {
TableArgument tableArgument = (TableArgument) arguments.get(DATA_PARAMETER_NAME);
String expectedFieldName =
(String) ((ScalarArgument) arguments.get(COL_PARAMETER_NAME)).getValue();
- int requiredIndex =
- findColumnIndex(
- tableArgument,
- expectedFieldName,
- ImmutableSet.of(Type.INT32, Type.INT64, Type.FLOAT, Type.DOUBLE));
+ double delta = (double) ((ScalarArgument) arguments.get(DELTA_PARAMETER_NAME)).getValue();
+ int requiredIndex;
+ try {
+ requiredIndex =
+ findColumnIndex(
+ tableArgument,
+ expectedFieldName,
+ delta == 0
+ ? ImmutableSet.copyOf(Type.allTypes())
+ : ImmutableSet.copyOf(Type.numericTypes()));
+ } catch (UDFTypeMismatchException e) {
+ // print more information for the exception
+ throw new UDFTypeMismatchException(
+ e.getMessage() + " The column type must be numeric if DELTA is not 0.", e);
+ }
+
DescribedSchema properColumnSchema =
new DescribedSchema.Builder().addField("window_index", Type.INT64).build();
// outputColumnSchema
MapTableFunctionHandle handle =
new MapTableFunctionHandle.Builder()
+ .addProperty(DELTA_PARAMETER_NAME, delta)
.addProperty(
- DELTA_PARAMETER_NAME,
- ((ScalarArgument) arguments.get(DELTA_PARAMETER_NAME)).getValue())
+ IGNORE_NULL_PARAMETER_NAME,
+ ((ScalarArgument) arguments.get(IGNORE_NULL_PARAMETER_NAME)).getValue())
.build();
return TableFunctionAnalysis.builder()
.properColumnSchema(properColumnSchema)
@@ -102,41 +124,152 @@ public class VariationTableFunction implements TableFunction {
TableFunctionHandle tableFunctionHandle) {
double delta =
(double) ((MapTableFunctionHandle) tableFunctionHandle).getProperty(DELTA_PARAMETER_NAME);
+ boolean ignoreNull =
+ (boolean)
+ ((MapTableFunctionHandle) tableFunctionHandle).getProperty(IGNORE_NULL_PARAMETER_NAME);
return new TableFunctionProcessorProvider() {
@Override
public TableFunctionDataProcessor getDataProcessor() {
- return new VariationDataProcessor(delta);
+ return delta == 0
+ ? new EquivalentVariationDataProcessor(ignoreNull)
+ : new NonEquivalentVariationDataProcessor(delta, ignoreNull);
}
};
}
- private static class VariationDataProcessor implements TableFunctionDataProcessor {
+ private static class EquivalentVariationDataProcessor extends VariationDataProcessor {
+ protected Object baseValue = null;
+
+ public EquivalentVariationDataProcessor(boolean ignoreNull) {
+ super(ignoreNull);
+ }
+
+ @Override
+ void resetBaseValue(Record input) {
+ if (input.isNull(0)) {
+ baseValue = null;
+ } else {
+ baseValue = input.getObject(0);
+ }
+ }
+
+ @Override
+ boolean isCurrentBaseValueIsNull() {
+ return baseValue == null;
+ }
+
+ @Override
+ boolean outOfBound(Record input) {
+ if (isCurrentBaseValueIsNull()) {
+ throw new IllegalStateException("When comparing, base value should never be null");
+ }
+ return !input.getObject(0).equals(baseValue);
+ }
+ }
+ private static class NonEquivalentVariationDataProcessor extends VariationDataProcessor {
private final double gap;
- private long currentStartIndex = -1;
private double baseValue = 0;
- private long curIndex = 0;
- private long windowIndex = 0;
+ private boolean baseValueIsNull = true;
- public VariationDataProcessor(double delta) {
+ public NonEquivalentVariationDataProcessor(double delta, boolean ignoreNull) {
+ super(ignoreNull);
this.gap = delta;
}
+ @Override
+ void resetBaseValue(Record input) {
+ if (input.isNull(0)) {
+ baseValueIsNull = true;
+ } else {
+ baseValueIsNull = false;
+ baseValue = input.getDouble(0);
+ }
+ }
+
+ @Override
+ boolean isCurrentBaseValueIsNull() {
+ return baseValueIsNull;
+ }
+
+ @Override
+ boolean outOfBound(Record input) {
+ if (isCurrentBaseValueIsNull()) {
+ throw new IllegalStateException("When comparing, base value should never be null");
+ }
+ return Math.abs(input.getDouble(0) - baseValue) > gap;
+ }
+ }
+
+ private abstract static class VariationDataProcessor implements TableFunctionDataProcessor {
+
+ protected final boolean ignoreNull;
+
+ // first row index of current window
+ protected long currentStartIndex = 0;
+ // current row index
+ protected long curIndex = 0;
+ // current window number
+ private long windowIndex = 0;
+
+ public VariationDataProcessor(boolean ignoreNull) {
+ this.ignoreNull = ignoreNull;
+ }
+
+ // reset base value anyway
+ abstract void resetBaseValue(Record input);
+
+ abstract boolean isCurrentBaseValueIsNull();
+
+ // reset base value only if its current value is null
+ void resetBaseValueIfNull(Record input) {
+ if (isCurrentBaseValueIsNull()) {
+ resetBaseValue(input);
+ }
+ }
+
+ boolean isNewGroup(Record input) {
+ if (input.isNull(0)) { // current row is null
+ if (isCurrentBaseValueIsNull()) { // current base value of current group is also null
+ // in such case, whether ignoreNull is true or false, current row always belongs to
+ // current group
+ return false;
+ } else { // current base value is not null
+ // ignoreNull is true, current null row belongs to current group
+ // ignoreNull is false, current null row doesn't belong to current group
+ return !ignoreNull;
+ }
+ } else { // current row is not null
+ if (isCurrentBaseValueIsNull()) { // current base value of current group is null
+ // ignoreNull is true, current non-null row belongs to current group
+ // ignoreNull is false, current non-null row doesn't belong to current group
+ return !ignoreNull;
+ } else { // current base value is not null
+ // compare current row with the current non-null base value
+ // if the diff between current row and base value exceed threshold, current non-null row
+ // doesn't belong to current group
+ // if the diff within the threshold, current non-null row belongs to current group
+ return outOfBound(input);
+ }
+ }
+ }
+
+ abstract boolean outOfBound(Record input);
+
@Override
public void process(
Record input,
List<ColumnBuilder> properColumnBuilders,
ColumnBuilder passThroughIndexBuilder) {
- double value = input.getDouble(0);
- if (currentStartIndex == -1) {
- // init the first window
- currentStartIndex = curIndex;
- baseValue = value;
- } else if (Math.abs(value - baseValue) > gap) {
- outputWindow(properColumnBuilders, passThroughIndexBuilder);
- currentStartIndex = curIndex;
- // use the first value in the window as the base value
- baseValue = value;
+ if (curIndex == 0) { // first row, init state
+ resetBaseValue(input);
+ } else { // not first row
+ if (isNewGroup(input)) { // current row doesn't belong to current group
+ outputWindow(properColumnBuilders, passThroughIndexBuilder, input);
+ } else { // current row belongs to current group
+ // reset base value of current group if it is null
+ resetBaseValueIfNull(input);
+ }
}
curIndex++;
}
@@ -144,16 +277,25 @@ public class VariationTableFunction implements TableFunction {
@Override
public void finish(
List<ColumnBuilder> properColumnBuilders, ColumnBuilder passThroughIndexBuilder) {
- outputWindow(properColumnBuilders, passThroughIndexBuilder);
+ outputWindow(properColumnBuilders, passThroughIndexBuilder, null);
}
- private void outputWindow(
- List<ColumnBuilder> properColumnBuilders, ColumnBuilder passThroughIndexBuilder) {
+ protected void outputWindow(
+ List<ColumnBuilder> properColumnBuilders,
+ ColumnBuilder passThroughIndexBuilder,
+ Record input) {
for (long i = currentStartIndex; i < curIndex; i++) {
properColumnBuilders.get(0).writeLong(windowIndex);
passThroughIndexBuilder.writeLong(i);
}
- windowIndex++;
+ if (curIndex > currentStartIndex) {
+ // reset if not empty group
+ windowIndex++;
+ currentStartIndex = curIndex;
+ if (input != null) {
+ resetBaseValue(input);
+ }
+ }
}
}
}
diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/WindowTVFUtils.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/WindowTVFUtils.java
index 9c9be03fbba..f0f72858c00 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/WindowTVFUtils.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/WindowTVFUtils.java
@@ -19,7 +19,9 @@
package org.apache.iotdb.commons.udf.builtin.relational.tvf;
+import org.apache.iotdb.udf.api.exception.UDFColumnNotFoundException;
import org.apache.iotdb.udf.api.exception.UDFException;
+import org.apache.iotdb.udf.api.exception.UDFTypeMismatchException;
import org.apache.iotdb.udf.api.relational.table.argument.TableArgument;
import org.apache.iotdb.udf.api.type.Type;
@@ -42,13 +44,13 @@ public class WindowTVFUtils {
Optional<String> fieldName = tableArgument.getFieldNames().get(i);
if (fieldName.isPresent() && expectedFieldName.equalsIgnoreCase(fieldName.get())) {
if (!expectedTypes.contains(tableArgument.getFieldTypes().get(i))) {
- throw new UDFException(
+ throw new UDFTypeMismatchException(
String.format("The type of the column [%s] is not as expected.", expectedFieldName));
}
return i;
}
}
- throw new UDFException(
+ throw new UDFColumnNotFoundException(
String.format(
"Required column [%s] not found in the source table argument.", expectedFieldName));
}