This is an automated email from the ASF dual-hosted git repository.
chenyz 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 3f96ada4c8d Tumble & Cumulate Windows TVFs (#15354)
3f96ada4c8d is described below
commit 3f96ada4c8db7d3335b778998776ce6b65d0b068
Author: shizy <[email protected]>
AuthorDate: Mon Apr 21 13:54:24 2025 +0800
Tumble & Cumulate Windows TVFs (#15354)
Tumble & Cumulate Windows TVFs
---
.../relational/it/db/it/IoTDBWindowTVFIT.java | 105 +++++++++++++++
.../relational/TableBuiltinTableFunction.java | 8 ++
.../relational/tvf/CumulateTableFunction.java | 150 +++++++++++++++++++++
.../relational/tvf/TumbleTableFunction.java | 130 ++++++++++++++++++
4 files changed, 393 insertions(+)
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 cb3fafb9703..68cd390f094 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
@@ -33,6 +33,7 @@ import org.junit.runner.RunWith;
import java.sql.Connection;
import java.sql.Statement;
+import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
import static org.junit.Assert.fail;
@@ -226,4 +227,108 @@ public class IoTDBWindowTVFIT {
retArray,
DATABASE_NAME);
}
+
+ @Test
+ public void testTumbleFunction() {
+ // TUMBLE (10m)
+ String[] expectedHeader =
+ new String[] {"window_start", "window_end", "time", "stock_id",
"price", "s1"};
+ String[] retArray =
+ new String[] {
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,2021-01-01T09:05:00.000Z,AAPL,100.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,2021-01-01T09:07:00.000Z,AAPL,103.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,2021-01-01T09:09:00.000Z,AAPL,102.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,2021-01-01T09:06:00.000Z,TESL,200.0,102.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,2021-01-01T09:07:00.000Z,TESL,202.0,202.0,",
+
"2021-01-01T09:10:00.000Z,2021-01-01T09:20:00.000Z,2021-01-01T09:15:00.000Z,TESL,195.0,332.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT * FROM TUMBLE(DATA => bid, TIMECOL => 'time', SIZE => 10m)
ORDER BY stock_id, time",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // TUMBLE (10m) + GROUP BY
+ expectedHeader = new String[] {"window_start", "window_end", "stock_id",
"sum"};
+ retArray =
+ new String[] {
+ "2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,AAPL,305.0,",
+ "2021-01-01T09:00:00.000Z,2021-01-01T09:10:00.000Z,TESL,402.0,",
+ "2021-01-01T09:10:00.000Z,2021-01-01T09:20:00.000Z,TESL,195.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT window_start, window_end, stock_id, sum(price) as sum FROM
TUMBLE(DATA => bid, TIMECOL => 'time', SIZE => 10m) GROUP BY window_start,
window_end, stock_id ORDER BY stock_id, window_start",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // TUMBLE (1h) + GROUP BY
+ expectedHeader = new String[] {"window_start", "window_end", "stock_id",
"sum"};
+ retArray =
+ new String[] {
+ "2021-01-01T09:00:00.000Z,2021-01-01T10:00:00.000Z,AAPL,305.0,",
+ "2021-01-01T09:00:00.000Z,2021-01-01T10:00:00.000Z,TESL,597.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT window_start, window_end, stock_id, sum(price) as sum FROM
TUMBLE(DATA => bid, TIMECOL => 'time', SIZE => 1h) GROUP BY window_start,
window_end, stock_id ORDER BY stock_id, window_start",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+ }
+
+ @Test
+ public void testCumulateFunction() {
+ String[] expectedHeader =
+ new String[] {"window_start", "window_end", "time", "stock_id",
"price", "s1"};
+ String[] retArray =
+ new String[] {
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:06:00.000Z,2021-01-01T09:05:00.000Z,AAPL,100.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,2021-01-01T09:05:00.000Z,AAPL,100.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,2021-01-01T09:07:00.000Z,AAPL,103.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,2021-01-01T09:09:00.000Z,AAPL,102.0,101.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,2021-01-01T09:06:00.000Z,TESL,200.0,102.0,",
+
"2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,2021-01-01T09:07:00.000Z,TESL,202.0,202.0,",
+
"2021-01-01T09:12:00.000Z,2021-01-01T09:18:00.000Z,2021-01-01T09:15:00.000Z,TESL,195.0,332.0,",
+
"2021-01-01T09:12:00.000Z,2021-01-01T09:24:00.000Z,2021-01-01T09:15:00.000Z,TESL,195.0,332.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT * FROM CUMULATE(DATA => bid, TIMECOL => 'time', STEP => 6m,
SIZE => 12m) ORDER BY stock_id, time",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ expectedHeader = new String[] {"window_start", "window_end", "stock_id",
"sum"};
+ retArray =
+ new String[] {
+ "2021-01-01T09:00:00.000Z,2021-01-01T09:06:00.000Z,AAPL,100.0,",
+ "2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,AAPL,305.0,",
+ "2021-01-01T09:00:00.000Z,2021-01-01T09:12:00.000Z,TESL,402.0,",
+ "2021-01-01T09:12:00.000Z,2021-01-01T09:18:00.000Z,TESL,195.0,",
+ "2021-01-01T09:12:00.000Z,2021-01-01T09:24:00.000Z,TESL,195.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT window_start, window_end, stock_id, sum(price) as sum FROM
CUMULATE(DATA => bid, TIMECOL => 'time', STEP => 6m, SIZE => 12m) GROUP BY
window_start, window_end, stock_id ORDER BY stock_id, window_start",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ expectedHeader = new String[] {"window_start", "window_end", "stock_id",
"sum"};
+ retArray =
+ new String[] {
+ "2021-01-01T09:00:00.000Z,2021-01-01T10:00:00.000Z,AAPL,305.0,",
+ "2021-01-01T09:00:00.000Z,2021-01-01T10:00:00.000Z,TESL,597.0,",
+ };
+ tableResultSetEqualTest(
+ "SELECT window_start, window_end, stock_id, sum(price) as sum FROM
CUMULATE(DATA => bid, TIMECOL => 'time', STEP => 1h, SIZE => 1h) GROUP BY
window_start, window_end, stock_id ORDER BY stock_id, window_start",
+ expectedHeader,
+ retArray,
+ DATABASE_NAME);
+
+ // test UDFException
+ String errMsg = "Cumulative table function requires size must be an
integral multiple of step.";
+ tableAssertTestFail(
+ "SELECT window_start, window_end, stock_id, sum(price) as sum FROM
CUMULATE(DATA => bid, TIMECOL => 'time', STEP => 4m, SIZE => 10m) GROUP BY
window_start, window_end, stock_id ORDER BY stock_id, window_start",
+ errMsg,
+ DATABASE_NAME);
+ }
}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/TableBuiltinTableFunction.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/TableBuiltinTableFunction.java
index 82e407f9022..c5984bcd92b 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/TableBuiltinTableFunction.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/TableBuiltinTableFunction.java
@@ -20,8 +20,10 @@
package org.apache.iotdb.commons.udf.builtin.relational;
import
org.apache.iotdb.commons.udf.builtin.relational.tvf.CapacityTableFunction;
+import
org.apache.iotdb.commons.udf.builtin.relational.tvf.CumulateTableFunction;
import org.apache.iotdb.commons.udf.builtin.relational.tvf.HOPTableFunction;
import
org.apache.iotdb.commons.udf.builtin.relational.tvf.SessionTableFunction;
+import org.apache.iotdb.commons.udf.builtin.relational.tvf.TumbleTableFunction;
import
org.apache.iotdb.commons.udf.builtin.relational.tvf.VariationTableFunction;
import org.apache.iotdb.udf.api.relational.TableFunction;
@@ -31,7 +33,9 @@ import java.util.Set;
import java.util.stream.Collectors;
public enum TableBuiltinTableFunction {
+ TUMBLE("tumble"),
HOP("hop"),
+ CUMULATE("cumulate"),
SESSION("session"),
VARIATION("variation"),
CAPACITY("capacity");
@@ -62,8 +66,12 @@ public enum TableBuiltinTableFunction {
public static TableFunction getBuiltinTableFunction(String functionName) {
switch (functionName.toLowerCase()) {
+ case "tumble":
+ return new TumbleTableFunction();
case "hop":
return new HOPTableFunction();
+ case "cumulate":
+ return new CumulateTableFunction();
case "session":
return new SessionTableFunction();
case "variation":
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/CumulateTableFunction.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/CumulateTableFunction.java
new file mode 100644
index 00000000000..acb3e588f05
--- /dev/null
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/CumulateTableFunction.java
@@ -0,0 +1,150 @@
+/*
+ * 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.udf.builtin.relational.tvf;
+
+import org.apache.iotdb.udf.api.exception.UDFException;
+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.TableFunctionAnalysis;
+import
org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider;
+import org.apache.iotdb.udf.api.relational.table.argument.Argument;
+import org.apache.iotdb.udf.api.relational.table.argument.DescribedSchema;
+import org.apache.iotdb.udf.api.relational.table.argument.ScalarArgument;
+import org.apache.iotdb.udf.api.relational.table.argument.TableArgument;
+import
org.apache.iotdb.udf.api.relational.table.processor.TableFunctionDataProcessor;
+import
org.apache.iotdb.udf.api.relational.table.specification.ParameterSpecification;
+import
org.apache.iotdb.udf.api.relational.table.specification.ScalarParameterSpecification;
+import
org.apache.iotdb.udf.api.relational.table.specification.TableParameterSpecification;
+import org.apache.iotdb.udf.api.type.Type;
+
+import org.apache.tsfile.block.column.ColumnBuilder;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static
org.apache.iotdb.commons.udf.builtin.relational.tvf.WindowTVFUtils.findColumnIndex;
+
+public class CumulateTableFunction implements TableFunction {
+
+ private static final String DATA_PARAMETER_NAME = "DATA";
+ private static final String TIMECOL_PARAMETER_NAME = "TIMECOL";
+ private static final String SIZE_PARAMETER_NAME = "SIZE";
+ private static final String STEP_PARAMETER_NAME = "STEP";
+ private static final String ORIGIN_PARAMETER_NAME = "ORIGIN";
+
+ @Override
+ public List<ParameterSpecification> getArgumentsSpecifications() {
+ return Arrays.asList(
+ TableParameterSpecification.builder()
+ .name(DATA_PARAMETER_NAME)
+ .rowSemantics()
+ .passThroughColumns()
+ .build(),
+ ScalarParameterSpecification.builder()
+ .name(TIMECOL_PARAMETER_NAME)
+ .type(Type.STRING)
+ .defaultValue("time")
+ .build(),
+
ScalarParameterSpecification.builder().name(SIZE_PARAMETER_NAME).type(Type.INT64).build(),
+
ScalarParameterSpecification.builder().name(STEP_PARAMETER_NAME).type(Type.INT64).build(),
+ ScalarParameterSpecification.builder()
+ .name(ORIGIN_PARAMETER_NAME)
+ .type(Type.TIMESTAMP)
+ .defaultValue(0L)
+ .build());
+ }
+
+ @Override
+ public TableFunctionAnalysis analyze(Map<String, Argument> arguments) throws
UDFException {
+ // size must be an integral multiple of step.
+ long size = (long) ((ScalarArgument)
arguments.get(SIZE_PARAMETER_NAME)).getValue();
+ long step = (long) ((ScalarArgument)
arguments.get(STEP_PARAMETER_NAME)).getValue();
+
+ if (size % step != 0) {
+ throw new UDFException(
+ "Cumulative table function requires size must be an integral
multiple of step.");
+ }
+
+ TableArgument tableArgument = (TableArgument)
arguments.get(DATA_PARAMETER_NAME);
+ String expectedFieldName =
+ (String) ((ScalarArgument)
arguments.get(TIMECOL_PARAMETER_NAME)).getValue();
+ int requiredIndex =
+ findColumnIndex(tableArgument, expectedFieldName,
Collections.singleton(Type.TIMESTAMP));
+ DescribedSchema properColumnSchema =
+ new DescribedSchema.Builder()
+ .addField("window_start", Type.TIMESTAMP)
+ .addField("window_end", Type.TIMESTAMP)
+ .build();
+
+ // outputColumnSchema
+ return TableFunctionAnalysis.builder()
+ .properColumnSchema(properColumnSchema)
+ .requireRecordSnapshot(false)
+ .requiredColumns(DATA_PARAMETER_NAME,
Collections.singletonList(requiredIndex))
+ .build();
+ }
+
+ @Override
+ public TableFunctionProcessorProvider getProcessorProvider(Map<String,
Argument> arguments) {
+ return new TableFunctionProcessorProvider() {
+ @Override
+ public TableFunctionDataProcessor getDataProcessor() {
+ return new CumulateDataProcessor(
+ (Long) ((ScalarArgument)
arguments.get(ORIGIN_PARAMETER_NAME)).getValue(),
+ (Long) ((ScalarArgument)
arguments.get(STEP_PARAMETER_NAME)).getValue(),
+ (Long) ((ScalarArgument)
arguments.get(SIZE_PARAMETER_NAME)).getValue());
+ }
+ };
+ }
+
+ private static class CumulateDataProcessor implements
TableFunctionDataProcessor {
+
+ private final long step;
+ private final long size;
+ private final long start;
+ private long curIndex = 0;
+
+ public CumulateDataProcessor(long startTime, long step, long size) {
+ this.step = step;
+ this.size = size;
+ this.start = startTime;
+ }
+
+ @Override
+ public void process(
+ Record input,
+ List<ColumnBuilder> properColumnBuilders,
+ ColumnBuilder passThroughIndexBuilder) {
+ // find the first windows
+ long timeValue = input.getLong(0);
+ long window_start = (timeValue - start) / size * size;
+ for (long steps = (timeValue - window_start + step) / step * step;
+ steps <= size;
+ steps += step) {
+ properColumnBuilders.get(0).writeLong(window_start);
+ properColumnBuilders.get(1).writeLong(window_start + steps);
+ passThroughIndexBuilder.writeLong(curIndex);
+ }
+ curIndex++;
+ }
+ }
+}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/TumbleTableFunction.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/TumbleTableFunction.java
new file mode 100644
index 00000000000..a239c694129
--- /dev/null
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/udf/builtin/relational/tvf/TumbleTableFunction.java
@@ -0,0 +1,130 @@
+/*
+ * 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.udf.builtin.relational.tvf;
+
+import org.apache.iotdb.udf.api.exception.UDFException;
+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.TableFunctionAnalysis;
+import
org.apache.iotdb.udf.api.relational.table.TableFunctionProcessorProvider;
+import org.apache.iotdb.udf.api.relational.table.argument.Argument;
+import org.apache.iotdb.udf.api.relational.table.argument.DescribedSchema;
+import org.apache.iotdb.udf.api.relational.table.argument.ScalarArgument;
+import org.apache.iotdb.udf.api.relational.table.argument.TableArgument;
+import
org.apache.iotdb.udf.api.relational.table.processor.TableFunctionDataProcessor;
+import
org.apache.iotdb.udf.api.relational.table.specification.ParameterSpecification;
+import
org.apache.iotdb.udf.api.relational.table.specification.ScalarParameterSpecification;
+import
org.apache.iotdb.udf.api.relational.table.specification.TableParameterSpecification;
+import org.apache.iotdb.udf.api.type.Type;
+
+import org.apache.tsfile.block.column.ColumnBuilder;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static
org.apache.iotdb.commons.udf.builtin.relational.tvf.WindowTVFUtils.findColumnIndex;
+
+public class TumbleTableFunction implements TableFunction {
+ private static final String DATA_PARAMETER_NAME = "DATA";
+ private static final String TIMECOL_PARAMETER_NAME = "TIMECOL";
+ private static final String SIZE_PARAMETER_NAME = "SIZE";
+ private static final String ORIGIN_PARAMETER_NAME = "ORIGIN";
+
+ @Override
+ public List<ParameterSpecification> getArgumentsSpecifications() {
+ return Arrays.asList(
+ TableParameterSpecification.builder()
+ .name(DATA_PARAMETER_NAME)
+ .rowSemantics()
+ .passThroughColumns()
+ .build(),
+ ScalarParameterSpecification.builder()
+ .name(TIMECOL_PARAMETER_NAME)
+ .type(Type.STRING)
+ .defaultValue("time")
+ .build(),
+
ScalarParameterSpecification.builder().name(SIZE_PARAMETER_NAME).type(Type.INT64).build(),
+ ScalarParameterSpecification.builder()
+ .name(ORIGIN_PARAMETER_NAME)
+ .type(Type.TIMESTAMP)
+ .defaultValue(0L)
+ .build());
+ }
+
+ @Override
+ public TableFunctionAnalysis analyze(Map<String, Argument> arguments) throws
UDFException {
+ TableArgument tableArgument = (TableArgument)
arguments.get(DATA_PARAMETER_NAME);
+ String expectedFieldName =
+ (String) ((ScalarArgument)
arguments.get(TIMECOL_PARAMETER_NAME)).getValue();
+ int requiredIndex =
+ findColumnIndex(tableArgument, expectedFieldName,
Collections.singleton(Type.TIMESTAMP));
+ DescribedSchema properColumnSchema =
+ new DescribedSchema.Builder()
+ .addField("window_start", Type.TIMESTAMP)
+ .addField("window_end", Type.TIMESTAMP)
+ .build();
+
+ // outputColumnSchema
+ return TableFunctionAnalysis.builder()
+ .properColumnSchema(properColumnSchema)
+ .requireRecordSnapshot(false)
+ .requiredColumns(DATA_PARAMETER_NAME,
Collections.singletonList(requiredIndex))
+ .build();
+ }
+
+ @Override
+ public TableFunctionProcessorProvider getProcessorProvider(Map<String,
Argument> arguments) {
+ return new TableFunctionProcessorProvider() {
+ @Override
+ public TableFunctionDataProcessor getDataProcessor() {
+ return new TumbleDataProcessor(
+ (Long) ((ScalarArgument)
arguments.get(ORIGIN_PARAMETER_NAME)).getValue(),
+ (Long) ((ScalarArgument)
arguments.get(SIZE_PARAMETER_NAME)).getValue());
+ }
+ };
+ }
+
+ private static class TumbleDataProcessor implements
TableFunctionDataProcessor {
+ private final long size;
+ private final long start;
+ private long curIndex = 0;
+
+ public TumbleDataProcessor(long startTime, long size) {
+ this.size = size;
+ this.start = startTime;
+ }
+
+ @Override
+ public void process(
+ Record input,
+ List<ColumnBuilder> properColumnBuilders,
+ ColumnBuilder passThroughIndexBuilder) {
+ // find the proper window
+ long timeValue = input.getLong(0);
+ long window_start = (timeValue - start) / size * size;
+ properColumnBuilders.get(0).writeLong(window_start);
+ properColumnBuilders.get(1).writeLong(window_start + size);
+ passThroughIndexBuilder.writeLong(curIndex);
+ curIndex++;
+ }
+ }
+}