You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/12/24 09:10:13 UTC
[GitHub] [iotdb] ericpai commented on a change in pull request #4632: [IOTDB-1948] [IOTDB-1949] Group by fill supports vector
ericpai commented on a change in pull request #4632:
URL: https://github.com/apache/iotdb/pull/4632#discussion_r774901603
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,313 @@
+package org.apache.iotdb.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next value and time for each time series
+ private int[] nextIds;
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
Review comment:
This type inferencing logics are almost the same as `TypeInferenceUtils.getAggrDataType()`.Please use that instead of writing a duplicated one.
##########
File path: integration/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByFillMixPathsIT.java
##########
@@ -122,367 +127,361 @@ private void prepareData() {
}
}
- // @Test
- // public void singlePathMixTest() {
- // String[] retArray =
- // new String[] {
- // // "7,23.0,23,8"
- // "17,41.66666666666667,23,8",
- // "22,51.0,23,25",
- // "27,88.5,25,25",
- // "32,126.0,27,36",
- // "37,129.0,26,36",
- // "42,132.0,24,36",
- // "47,135.0,22,51",
- // "52,110.0,20,56",
- // "57,null,23,null",
- // "62,71.5,26,null"
- // // "72,33.0,33,72"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixTest() {
- // String[] retArray =
- // new String[] {
- // "17,41.66666666666667,23.0,10,23,23.5,true",
- // "22,51.0,null,10,23,null,true",
- // "27,88.5,35.0,null,23,36.0,true",
- // "32,126.0,43.36,36,22,48.2,false",
- // "37,129.0,37.0,36,22,38.2,true",
- // "42,132.0,null,45,22,null,false",
- // "47,135.0,35.900000000000006,45,22,39.35,true",
- // "52,110.0,null,56,13,null,false",
- // "57,null,34.800000000000004,null,18,40.5,true",
- // "62,71.5,38.800000000000004,null,23,42.6,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 23.0, 11.0, 10, 23, 11.0, true
- // 17, 41.67(null), 23.0(null), 10(null), 23(null), 23.5(null), true(null)
- // 22, 51.0, null, 10(null), 23, null, true(null)
- // 27, 88.5(null), 35.0, null, 23(null), 36.0, true(null)
- // 32, 126.0, 43.36, 36, 22, 48.2, false
- // 37, 129.0(null), 37.0, 36(null), 22(null), 38.2, true(null)
- // 42, 132.0(null), null, 45, 22(null), null, false
- // 47, 135.0, 35.9(null), 45(null), 22, 39.35(null), true(null)
- // 52, 110.0, null, 56, 13, null, false
- // 57, null, 34.8, null, 18(null), 40.5, true(null)
- // 62, 71.5(null), 38.8(null), null, 23(null), 42.6(null), true(null)
- // 72, 33.0, 46.8, 74, 33, 46.8, true
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void singlePathMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // // "112,33.5,33.5,114"
- // "117,null,null,114",
- // "122,39.05,39.05,114",
- // "127,null,null,114",
- // "132,44.6,44.6,133",
- // "137,47.93333333333334,47.93333333333334,133",
- // "142,51.266666666666666,51.266666666666666,133",
- // "147,54.6,54.6,148",
- // "152,47.4,47.4,148"
- // // "162,33.0,33.0,166"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // "117,null,null,114,26,null,true",
- // "122,27.0,39.05,114,27,39.05,true",
- // "127,null,null,114,null,null,true",
- // "132,29.0,44.6,133,29,44.6,false",
- // "137,28.666666666666668,47.93333333333334,133,29,47.93333333333334,true",
- // "142,28.333333333333332,51.266666666666666,133,29,51.266666666666666,true",
- // "147,28.0,54.6,148,28,54.6,false",
- // "152,32.0,47.4,null,32,47.4,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 25.0, 33.5, 114, 25, 33.5, false
- // 117, null, null, 114(null), 26(null), null, true(null)
- // 122, 27.0(null), 39.05(null), 114(null), 27(null), 39.05(null), true(null)
- // 127, null, null, 114(null), null, null, true(null)
- // 132, 29.0, 44.6, 133, 29, 44.6, false
- // 137, 28.67(null), 47.93(null), 133(null), 29(null), 47.93(null), true(null)
- // 142, 28.33(null), 51.27(null), 133(null), 29(null), 51.27(null), true(null)
- // 147, 28.0, 54.6, 148, 28, 54.6, false
- // 152, 32.0(null), 47.4(null), null, 32(null), 47.4(null), true(null)
- // 162, 40.0, 33.0, null, 40, 33.0, null
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
+ @Test
+ public void singlePathMixTest() {
+ String[] retArray =
+ new String[] {
+ // "7,23.0,23,8"
+ "17,23.0,23,8",
+ "22,51.0,23,25",
+ "27,51.0,23,25",
+ "32,126.0,27,36",
+ "37,126.0,27,36",
+ "42,126.0,27,36",
+ "47,135.0,22,51",
+ "52,110.0,20,56",
+ "57,110.0,20,56",
+ "62,110.0,20,56"
+ // "72,33.0,33,72"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void MultiPathsMixTest() {
+ String[] retArray =
+ new String[] {
+ "17,41.66666666666667,23.0,null,23,23.5,null",
+ "22,51.0,null,null,23,null,null",
+ "27,88.5,35.0,null,23,36.0,null",
+ "32,126.0,43.36,36,22,48.2,false",
+ "37,129.0,37.0,40,22,38.2,null",
+ "42,132.0,null,45,22,null,false",
+ "47,135.0,35.900000000000006,50,22,39.35,null",
+ "52,110.0,null,56,13,null,false",
+ "57,null,34.800000000000004,null,null,40.5,null",
+ "62,71.5,38.800000000000004,65,23,42.6,null"
+ };
+
+ /* Format result
+ linear, linear, linear, linear, linear, linear
+ 7, 23.0, 11.0, 10, 23, 11.0, true
+ 17, 41.67(null), 23.0(null), null, 23(null), 23.5(null), null
+ 22, 51.0, null, null, 23, null, null
+ 27, 88.5(null), 35.0, null, 23(null), 36.0, null
+ 32, 126.0, 43.36, 36, 22, 48.2, false
+ 37, 129.0(null), 37.0, 40(null), 22(null), 38.2, null
+ 42, 132.0(null), null, 45, 22(null), null, false
+ 47, 135.0, 35.9(null), 50(null), 22, 39.35(null), null
+ 52, 110.0, null, 56, 13, null, false
+ 57, null, 34.8, null, null, 40.5, null
+ 62, 71.5(null), 38.8(null), 65(null), 23(null), 42.6(null), null
+ 72, 33.0, 46.8, 74, 33, 46.8, true
+ */
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
Review comment:
use `Connection conn = EnvFactory.getEnv().getConnection()`
##########
File path: server/src/main/java/org/apache/iotdb/db/qp/physical/crud/GroupByTimeFillPlan.java
##########
@@ -49,4 +56,68 @@ public void setSingleFill(IFill singleFill) {
public void setFillType(Map<TSDataType, IFill> fillTypes) {
this.fillTypes = fillTypes;
}
+
+ public long getQueryStartTime() {
+ return queryStartTime;
+ }
+
+ public long getQueryEndTime() {
+ return queryEndTime;
+ }
+
+ /** union the query time range with the extra fill range */
+ public void initFillRange() {
+ long minQueryStartTime = startTime;
+ long maxQueryEndTime = endTime;
+ if (fillTypes != null) {
+ // old type fill logic
+ for (Map.Entry<TSDataType, IFill> IFillEntry : fillTypes.entrySet()) {
+ IFill fill = IFillEntry.getValue();
+ if (fill instanceof PreviousFill) {
+ fill.convertRange(startTime, endTime);
+ minQueryStartTime = Math.min(minQueryStartTime, fill.getQueryStartTime());
+ } else if (fill instanceof LinearFill) {
+ fill.convertRange(startTime, endTime);
+ minQueryStartTime = Math.min(minQueryStartTime, fill.getQueryStartTime());
+ maxQueryEndTime = Math.max(maxQueryEndTime, fill.getQueryEndTime());
+ }
+ }
+ } else {
+ IFill fill = singleFill;
+ if (fill instanceof PreviousFill) {
Review comment:
Same comment as above
##########
File path: server/src/main/java/org/apache/iotdb/db/qp/physical/crud/GroupByTimeFillPlan.java
##########
@@ -49,4 +56,68 @@ public void setSingleFill(IFill singleFill) {
public void setFillType(Map<TSDataType, IFill> fillTypes) {
this.fillTypes = fillTypes;
}
+
+ public long getQueryStartTime() {
+ return queryStartTime;
+ }
+
+ public long getQueryEndTime() {
+ return queryEndTime;
+ }
+
+ /** union the query time range with the extra fill range */
+ public void initFillRange() {
+ long minQueryStartTime = startTime;
+ long maxQueryEndTime = endTime;
+ if (fillTypes != null) {
+ // old type fill logic
+ for (Map.Entry<TSDataType, IFill> IFillEntry : fillTypes.entrySet()) {
+ IFill fill = IFillEntry.getValue();
+ if (fill instanceof PreviousFill) {
Review comment:
Maybe we can let `PreviousFill.getQueryEndTime() and ValueFill.getQueryEndTime()` equals the one passed to `fill.covertRange()`, so there's no need to check the instance type here, which is a formal OOP method.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
+ curTimestamp = curStartTime;
+ record = new RowRecord(curStartTime);
+ } else {
+ curTimestamp = curEndTime - 1;
+ record = new RowRecord(curEndTime - 1);
+ }
+
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ fillRecord(i, record);
+ continue;
+ }
+
+ long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+ if (cacheTime == curTimestamp) {
+ record.addField(getNextCacheValue(i), resultDataType[i]);
+ } else {
+ fillRecord(i, record);
+ }
+ }
+
+ try {
+ slideCache(record.getTimestamp());
+ } catch (QueryProcessException e) {
+ // ignored
+ }
+
+ return record;
+ }
+
+ private void fillRecord(int index, RowRecord record) throws IOException {
+ // Don't fill count aggregation
+ if (Objects.equals(aggregations.get(index), "count")) {
+ record.addField((long) 0, TSDataType.INT64);
+ return;
+ }
+
+ IFill fill;
+ if (fillTypes != null) {
+ // old type fill logic
+ fill = fillTypes.get(resultDataType[index]);
+ } else {
+ fill = singleFill;
+ }
+ if (fill == null) {
+ record.addField(null);
+ return;
+ }
+
+ Pair<Long, Object> beforePair, afterPair;
+ if (ascending) {
+ if (previousValues[index] != null) {
+ beforePair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ beforePair = null;
+ }
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ afterPair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ afterPair = null;
+ }
+ } else {
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ beforePair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ beforePair = null;
+ }
+ if (previousValues[index] != null) {
+ afterPair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ afterPair = null;
+ }
+ }
+
+ if (fill instanceof PreviousFill) {
+ if (beforePair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && ((!((PreviousFill) fill).isUntilLast())
+ || (afterPair != null && afterPair.left < endTime))) {
+ record.addField(beforePair.right, resultDataType[index]);
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof LinearFill) {
+ LinearFill linearFill = new LinearFill();
+ if (beforePair != null
+ && afterPair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && (fill.getAfterRange() == -1
+ || fill.insideAfterRange(afterPair.left, record.getTimestamp()))) {
+ try {
+ TimeValuePair filledPair =
+ linearFill.averageWithTimeAndDataType(
+ new TimeValuePair(
+ beforePair.left,
+ TsPrimitiveType.getByType(resultDataType[index], beforePair.right)),
+ new TimeValuePair(
+ afterPair.left,
+ TsPrimitiveType.getByType(resultDataType[index], afterPair.right)),
+ record.getTimestamp(),
+ resultDataType[index]);
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (UnSupportedFillTypeException e) {
+ // Don't fill and ignore exception
+ record.addField(null);
+ }
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof ValueFill) {
+ try {
+ TimeValuePair filledPair = fill.getFillResult();
+ if (filledPair == null) {
+ filledPair = ((ValueFill) fill).getSpecifiedFillResult(resultDataType[index]);
+ }
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (QueryProcessException | StorageEngineException e) {
+ throw new IOException(e);
+ }
+ }
+ }
+
+ private Object getNextCacheValue(int index) throws IOException {
+ switch (resultDataType[index]) {
+ case INT32:
+ return nextTVLists.get(index).getInt(nextIds[index]);
+ case INT64:
+ return nextTVLists.get(index).getLong(nextIds[index]);
+ case FLOAT:
+ return nextTVLists.get(index).getFloat(nextIds[index]);
+ case DOUBLE:
+ return nextTVLists.get(index).getDouble(nextIds[index]);
+ case BOOLEAN:
+ return nextTVLists.get(index).getBoolean(nextIds[index]);
+ case TEXT:
+ return nextTVLists.get(index).getString(nextIds[index]);
+ default:
+ throw new IOException("unknown data type!");
+ }
+ }
+
+ private void slideCache(long curTimestamp) throws IOException, QueryProcessException {
+ BitSet slideSet = new BitSet(aggregations.size());
+ slideSet.clear();
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ continue;
+ }
+
+ // slide cache when the current TV is used
+ if (nextTVLists.get(i).getTime(nextIds[i]) == curTimestamp) {
+ previousTimes[i] = curTimestamp;
+ previousValues[i] = getNextCacheValue(i);
+ nextIds[i] += 1;
Review comment:
nextIds[i]++
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,313 @@
+package org.apache.iotdb.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next value and time for each time series
+ private int[] nextIds;
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
Review comment:
`nextTVLists` can be initialized with the capacity of `aggregations.size()`.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
Review comment:
Why use `10MB` as the memory budget and `2` as the fetchSize?
##########
File path: integration/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByFillMixPathsIT.java
##########
@@ -122,367 +127,361 @@ private void prepareData() {
}
}
- // @Test
- // public void singlePathMixTest() {
- // String[] retArray =
- // new String[] {
- // // "7,23.0,23,8"
- // "17,41.66666666666667,23,8",
- // "22,51.0,23,25",
- // "27,88.5,25,25",
- // "32,126.0,27,36",
- // "37,129.0,26,36",
- // "42,132.0,24,36",
- // "47,135.0,22,51",
- // "52,110.0,20,56",
- // "57,null,23,null",
- // "62,71.5,26,null"
- // // "72,33.0,33,72"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixTest() {
- // String[] retArray =
- // new String[] {
- // "17,41.66666666666667,23.0,10,23,23.5,true",
- // "22,51.0,null,10,23,null,true",
- // "27,88.5,35.0,null,23,36.0,true",
- // "32,126.0,43.36,36,22,48.2,false",
- // "37,129.0,37.0,36,22,38.2,true",
- // "42,132.0,null,45,22,null,false",
- // "47,135.0,35.900000000000006,45,22,39.35,true",
- // "52,110.0,null,56,13,null,false",
- // "57,null,34.800000000000004,null,18,40.5,true",
- // "62,71.5,38.800000000000004,null,23,42.6,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 23.0, 11.0, 10, 23, 11.0, true
- // 17, 41.67(null), 23.0(null), 10(null), 23(null), 23.5(null), true(null)
- // 22, 51.0, null, 10(null), 23, null, true(null)
- // 27, 88.5(null), 35.0, null, 23(null), 36.0, true(null)
- // 32, 126.0, 43.36, 36, 22, 48.2, false
- // 37, 129.0(null), 37.0, 36(null), 22(null), 38.2, true(null)
- // 42, 132.0(null), null, 45, 22(null), null, false
- // 47, 135.0, 35.9(null), 45(null), 22, 39.35(null), true(null)
- // 52, 110.0, null, 56, 13, null, false
- // 57, null, 34.8, null, 18(null), 40.5, true(null)
- // 62, 71.5(null), 38.8(null), null, 23(null), 42.6(null), true(null)
- // 72, 33.0, 46.8, 74, 33, 46.8, true
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void singlePathMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // // "112,33.5,33.5,114"
- // "117,null,null,114",
- // "122,39.05,39.05,114",
- // "127,null,null,114",
- // "132,44.6,44.6,133",
- // "137,47.93333333333334,47.93333333333334,133",
- // "142,51.266666666666666,51.266666666666666,133",
- // "147,54.6,54.6,148",
- // "152,47.4,47.4,148"
- // // "162,33.0,33.0,166"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // "117,null,null,114,26,null,true",
- // "122,27.0,39.05,114,27,39.05,true",
- // "127,null,null,114,null,null,true",
- // "132,29.0,44.6,133,29,44.6,false",
- // "137,28.666666666666668,47.93333333333334,133,29,47.93333333333334,true",
- // "142,28.333333333333332,51.266666666666666,133,29,51.266666666666666,true",
- // "147,28.0,54.6,148,28,54.6,false",
- // "152,32.0,47.4,null,32,47.4,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 25.0, 33.5, 114, 25, 33.5, false
- // 117, null, null, 114(null), 26(null), null, true(null)
- // 122, 27.0(null), 39.05(null), 114(null), 27(null), 39.05(null), true(null)
- // 127, null, null, 114(null), null, null, true(null)
- // 132, 29.0, 44.6, 133, 29, 44.6, false
- // 137, 28.67(null), 47.93(null), 133(null), 29(null), 47.93(null), true(null)
- // 142, 28.33(null), 51.27(null), 133(null), 29(null), 51.27(null), true(null)
- // 147, 28.0, 54.6, 148, 28, 54.6, false
- // 152, 32.0(null), 47.4(null), null, 32(null), 47.4(null), true(null)
- // 162, 40.0, 33.0, null, 40, 33.0, null
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
+ @Test
+ public void singlePathMixTest() {
+ String[] retArray =
+ new String[] {
+ // "7,23.0,23,8"
+ "17,23.0,23,8",
+ "22,51.0,23,25",
+ "27,51.0,23,25",
+ "32,126.0,27,36",
+ "37,126.0,27,36",
+ "42,126.0,27,36",
+ "47,135.0,22,51",
+ "52,110.0,20,56",
+ "57,110.0,20,56",
+ "62,110.0,20,56"
+ // "72,33.0,33,72"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void MultiPathsMixTest() {
+ String[] retArray =
+ new String[] {
+ "17,41.66666666666667,23.0,null,23,23.5,null",
+ "22,51.0,null,null,23,null,null",
+ "27,88.5,35.0,null,23,36.0,null",
+ "32,126.0,43.36,36,22,48.2,false",
+ "37,129.0,37.0,40,22,38.2,null",
+ "42,132.0,null,45,22,null,false",
+ "47,135.0,35.900000000000006,50,22,39.35,null",
+ "52,110.0,null,56,13,null,false",
+ "57,null,34.800000000000004,null,null,40.5,null",
+ "62,71.5,38.800000000000004,65,23,42.6,null"
+ };
+
+ /* Format result
+ linear, linear, linear, linear, linear, linear
+ 7, 23.0, 11.0, 10, 23, 11.0, true
+ 17, 41.67(null), 23.0(null), null, 23(null), 23.5(null), null
+ 22, 51.0, null, null, 23, null, null
+ 27, 88.5(null), 35.0, null, 23(null), 36.0, null
+ 32, 126.0, 43.36, 36, 22, 48.2, false
+ 37, 129.0(null), 37.0, 40(null), 22(null), 38.2, null
+ 42, 132.0(null), null, 45, 22(null), null, false
+ 47, 135.0, 35.9(null), 50(null), 22, 39.35(null), null
+ 52, 110.0, null, 56, 13, null, false
+ 57, null, 34.8, null, null, 40.5, null
+ 62, 71.5(null), 38.8(null), 65(null), 23(null), 42.6(null), null
+ 72, 33.0, 46.8, 74, 33, 46.8, true
+ */
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), avg(hardware), max_time(status), "
+ + "min_value(temperature), max_value(hardware), first_value(status) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(linear, 12ms, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
+ + ","
+ + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), avg(hardware), max_time(status), "
+ + "min_value(temperature), max_value(hardware), first_value(status) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(linear, 12ms, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
+ + ","
+ + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void singlePathMixWithValueFilterTest() {
+ String[] retArray =
+ new String[] {
+ // "112,33.5,33.5,114"
+ "117,33.5,33.5,114",
+ "122,33.5,33.5,114",
+ "127,null,null,null",
+ "132,44.6,44.6,133",
+ "137,44.6,44.6,133",
+ "142,44.6,44.6,133",
+ "147,54.6,54.6,148",
+ "152,null,null,null"
+ // "162,33.0,33.0,166"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
Review comment:
use `Connection conn = EnvFactory.getEnv().getConnection()`
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
+ curTimestamp = curStartTime;
+ record = new RowRecord(curStartTime);
+ } else {
+ curTimestamp = curEndTime - 1;
+ record = new RowRecord(curEndTime - 1);
+ }
+
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ fillRecord(i, record);
+ continue;
+ }
+
+ long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+ if (cacheTime == curTimestamp) {
+ record.addField(getNextCacheValue(i), resultDataType[i]);
+ } else {
+ fillRecord(i, record);
+ }
+ }
+
+ try {
+ slideCache(record.getTimestamp());
+ } catch (QueryProcessException e) {
+ // ignored
+ }
+
+ return record;
+ }
+
+ private void fillRecord(int index, RowRecord record) throws IOException {
+ // Don't fill count aggregation
+ if (Objects.equals(aggregations.get(index), "count")) {
Review comment:
Why `count` is a special case here?
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
Review comment:
Is the `clear` operation redundant? Every bit is false after initializing.
##########
File path: integration/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByFillMixPathsIT.java
##########
@@ -122,367 +127,361 @@ private void prepareData() {
}
}
- // @Test
- // public void singlePathMixTest() {
- // String[] retArray =
- // new String[] {
- // // "7,23.0,23,8"
- // "17,41.66666666666667,23,8",
- // "22,51.0,23,25",
- // "27,88.5,25,25",
- // "32,126.0,27,36",
- // "37,129.0,26,36",
- // "42,132.0,24,36",
- // "47,135.0,22,51",
- // "52,110.0,20,56",
- // "57,null,23,null",
- // "62,71.5,26,null"
- // // "72,33.0,33,72"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixTest() {
- // String[] retArray =
- // new String[] {
- // "17,41.66666666666667,23.0,10,23,23.5,true",
- // "22,51.0,null,10,23,null,true",
- // "27,88.5,35.0,null,23,36.0,true",
- // "32,126.0,43.36,36,22,48.2,false",
- // "37,129.0,37.0,36,22,38.2,true",
- // "42,132.0,null,45,22,null,false",
- // "47,135.0,35.900000000000006,45,22,39.35,true",
- // "52,110.0,null,56,13,null,false",
- // "57,null,34.800000000000004,null,18,40.5,true",
- // "62,71.5,38.800000000000004,null,23,42.6,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 23.0, 11.0, 10, 23, 11.0, true
- // 17, 41.67(null), 23.0(null), 10(null), 23(null), 23.5(null), true(null)
- // 22, 51.0, null, 10(null), 23, null, true(null)
- // 27, 88.5(null), 35.0, null, 23(null), 36.0, true(null)
- // 32, 126.0, 43.36, 36, 22, 48.2, false
- // 37, 129.0(null), 37.0, 36(null), 22(null), 38.2, true(null)
- // 42, 132.0(null), null, 45, 22(null), null, false
- // 47, 135.0, 35.9(null), 45(null), 22, 39.35(null), true(null)
- // 52, 110.0, null, 56, 13, null, false
- // 57, null, 34.8, null, 18(null), 40.5, true(null)
- // 62, 71.5(null), 38.8(null), null, 23(null), 42.6(null), true(null)
- // 72, 33.0, 46.8, 74, 33, 46.8, true
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void singlePathMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // // "112,33.5,33.5,114"
- // "117,null,null,114",
- // "122,39.05,39.05,114",
- // "127,null,null,114",
- // "132,44.6,44.6,133",
- // "137,47.93333333333334,47.93333333333334,133",
- // "142,51.266666666666666,51.266666666666666,133",
- // "147,54.6,54.6,148",
- // "152,47.4,47.4,148"
- // // "162,33.0,33.0,166"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // "117,null,null,114,26,null,true",
- // "122,27.0,39.05,114,27,39.05,true",
- // "127,null,null,114,null,null,true",
- // "132,29.0,44.6,133,29,44.6,false",
- // "137,28.666666666666668,47.93333333333334,133,29,47.93333333333334,true",
- // "142,28.333333333333332,51.266666666666666,133,29,51.266666666666666,true",
- // "147,28.0,54.6,148,28,54.6,false",
- // "152,32.0,47.4,null,32,47.4,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 25.0, 33.5, 114, 25, 33.5, false
- // 117, null, null, 114(null), 26(null), null, true(null)
- // 122, 27.0(null), 39.05(null), 114(null), 27(null), 39.05(null), true(null)
- // 127, null, null, 114(null), null, null, true(null)
- // 132, 29.0, 44.6, 133, 29, 44.6, false
- // 137, 28.67(null), 47.93(null), 133(null), 29(null), 47.93(null), true(null)
- // 142, 28.33(null), 51.27(null), 133(null), 29(null), 51.27(null), true(null)
- // 147, 28.0, 54.6, 148, 28, 54.6, false
- // 152, 32.0(null), 47.4(null), null, 32(null), 47.4(null), true(null)
- // 162, 40.0, 33.0, null, 40, 33.0, null
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
+ @Test
+ public void singlePathMixTest() {
+ String[] retArray =
+ new String[] {
+ // "7,23.0,23,8"
+ "17,23.0,23,8",
+ "22,51.0,23,25",
+ "27,51.0,23,25",
+ "32,126.0,27,36",
+ "37,126.0,27,36",
+ "42,126.0,27,36",
+ "47,135.0,22,51",
+ "52,110.0,20,56",
+ "57,110.0,20,56",
+ "62,110.0,20,56"
+ // "72,33.0,33,72"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void MultiPathsMixTest() {
Review comment:
`MultiPathsMixTest` => `multiPathsMixTest`
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
Review comment:
Excellent work! Now the fill logics are decoupled from GroupByEngineDataSet indirectly. It seems that this class only calls `dataSet.hasNextWithoutConstraint()` and `dataSet.nextWithoutConstraint()`, which are just the abstract methods of `QueryDataSet` class. If we change the type from `GroupByEngineDataSet` to `QueryDataSet`, maybe we can support other types of implements in the furture.
##########
File path: integration/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByFillMixPathsIT.java
##########
@@ -122,367 +127,361 @@ private void prepareData() {
}
}
- // @Test
- // public void singlePathMixTest() {
- // String[] retArray =
- // new String[] {
- // // "7,23.0,23,8"
- // "17,41.66666666666667,23,8",
- // "22,51.0,23,25",
- // "27,88.5,25,25",
- // "32,126.0,27,36",
- // "37,129.0,26,36",
- // "42,132.0,24,36",
- // "47,135.0,22,51",
- // "52,110.0,20,56",
- // "57,null,23,null",
- // "62,71.5,26,null"
- // // "72,33.0,33,72"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixTest() {
- // String[] retArray =
- // new String[] {
- // "17,41.66666666666667,23.0,10,23,23.5,true",
- // "22,51.0,null,10,23,null,true",
- // "27,88.5,35.0,null,23,36.0,true",
- // "32,126.0,43.36,36,22,48.2,false",
- // "37,129.0,37.0,36,22,38.2,true",
- // "42,132.0,null,45,22,null,false",
- // "47,135.0,35.900000000000006,45,22,39.35,true",
- // "52,110.0,null,56,13,null,false",
- // "57,null,34.800000000000004,null,18,40.5,true",
- // "62,71.5,38.800000000000004,null,23,42.6,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 23.0, 11.0, 10, 23, 11.0, true
- // 17, 41.67(null), 23.0(null), 10(null), 23(null), 23.5(null), true(null)
- // 22, 51.0, null, 10(null), 23, null, true(null)
- // 27, 88.5(null), 35.0, null, 23(null), 36.0, true(null)
- // 32, 126.0, 43.36, 36, 22, 48.2, false
- // 37, 129.0(null), 37.0, 36(null), 22(null), 38.2, true(null)
- // 42, 132.0(null), null, 45, 22(null), null, false
- // 47, 135.0, 35.9(null), 45(null), 22, 39.35(null), true(null)
- // 52, 110.0, null, 56, 13, null, false
- // 57, null, 34.8, null, 18(null), 40.5, true(null)
- // 62, 71.5(null), 38.8(null), null, 23(null), 42.6(null), true(null)
- // 72, 33.0, 46.8, 74, 33, 46.8, true
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void singlePathMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // // "112,33.5,33.5,114"
- // "117,null,null,114",
- // "122,39.05,39.05,114",
- // "127,null,null,114",
- // "132,44.6,44.6,133",
- // "137,47.93333333333334,47.93333333333334,133",
- // "142,51.266666666666666,51.266666666666666,133",
- // "147,54.6,54.6,148",
- // "152,47.4,47.4,148"
- // // "162,33.0,33.0,166"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // "117,null,null,114,26,null,true",
- // "122,27.0,39.05,114,27,39.05,true",
- // "127,null,null,114,null,null,true",
- // "132,29.0,44.6,133,29,44.6,false",
- // "137,28.666666666666668,47.93333333333334,133,29,47.93333333333334,true",
- // "142,28.333333333333332,51.266666666666666,133,29,51.266666666666666,true",
- // "147,28.0,54.6,148,28,54.6,false",
- // "152,32.0,47.4,null,32,47.4,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 25.0, 33.5, 114, 25, 33.5, false
- // 117, null, null, 114(null), 26(null), null, true(null)
- // 122, 27.0(null), 39.05(null), 114(null), 27(null), 39.05(null), true(null)
- // 127, null, null, 114(null), null, null, true(null)
- // 132, 29.0, 44.6, 133, 29, 44.6, false
- // 137, 28.67(null), 47.93(null), 133(null), 29(null), 47.93(null), true(null)
- // 142, 28.33(null), 51.27(null), 133(null), 29(null), 51.27(null), true(null)
- // 147, 28.0, 54.6, 148, 28, 54.6, false
- // 152, 32.0(null), 47.4(null), null, 32(null), 47.4(null), true(null)
- // 162, 40.0, 33.0, null, 40, 33.0, null
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
+ @Test
+ public void singlePathMixTest() {
+ String[] retArray =
+ new String[] {
+ // "7,23.0,23,8"
+ "17,23.0,23,8",
+ "22,51.0,23,25",
+ "27,51.0,23,25",
+ "32,126.0,27,36",
+ "37,126.0,27,36",
+ "42,126.0,27,36",
+ "47,135.0,22,51",
+ "52,110.0,20,56",
+ "57,110.0,20,56",
+ "62,110.0,20,56"
+ // "72,33.0,33,72"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), last_value(temperature), max_time(temperature) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(previous, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void MultiPathsMixTest() {
+ String[] retArray =
+ new String[] {
+ "17,41.66666666666667,23.0,null,23,23.5,null",
+ "22,51.0,null,null,23,null,null",
+ "27,88.5,35.0,null,23,36.0,null",
+ "32,126.0,43.36,36,22,48.2,false",
+ "37,129.0,37.0,40,22,38.2,null",
+ "42,132.0,null,45,22,null,false",
+ "47,135.0,35.900000000000006,50,22,39.35,null",
+ "52,110.0,null,56,13,null,false",
+ "57,null,34.800000000000004,null,null,40.5,null",
+ "62,71.5,38.800000000000004,65,23,42.6,null"
+ };
+
+ /* Format result
+ linear, linear, linear, linear, linear, linear
+ 7, 23.0, 11.0, 10, 23, 11.0, true
+ 17, 41.67(null), 23.0(null), null, 23(null), 23.5(null), null
+ 22, 51.0, null, null, 23, null, null
+ 27, 88.5(null), 35.0, null, 23(null), 36.0, null
+ 32, 126.0, 43.36, 36, 22, 48.2, false
+ 37, 129.0(null), 37.0, 40(null), 22(null), 38.2, null
+ 42, 132.0(null), null, 45, 22(null), null, false
+ 47, 135.0, 35.9(null), 50(null), 22, 39.35(null), null
+ 52, 110.0, null, 56, 13, null, false
+ 57, null, 34.8, null, null, 40.5, null
+ 62, 71.5(null), 38.8(null), 65(null), 23(null), 42.6(null), null
+ 72, 33.0, 46.8, 74, 33, 46.8, true
+ */
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(temperature), avg(hardware), max_time(status), "
+ + "min_value(temperature), max_value(hardware), first_value(status) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(linear, 12ms, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
+ + ","
+ + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(temperature), avg(hardware), max_time(status), "
+ + "min_value(temperature), max_value(hardware), first_value(status) "
+ + "from root.ln.wf01.wt01 "
+ + "GROUP BY ([17, 65), 5ms) "
+ + "FILL(linear, 12ms, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
+ + ","
+ + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
+ + ","
+ + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void singlePathMixWithValueFilterTest() {
+ String[] retArray =
+ new String[] {
+ // "112,33.5,33.5,114"
+ "117,33.5,33.5,114",
+ "122,33.5,33.5,114",
+ "127,null,null,null",
+ "132,44.6,44.6,133",
+ "137,44.6,44.6,133",
+ "142,44.6,44.6,133",
+ "147,54.6,54.6,148",
+ "152,null,null,null"
+ // "162,33.0,33.0,166"
+ };
+
+ try (Connection connection =
+ DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
+ Statement statement = connection.createStatement()) {
+ boolean hasResultSet =
+ statement.execute(
+ "select sum(hardware), last_value(hardware), max_time(hardware) "
+ + "from root.ln.wf01.wt01 "
+ + "WHERE temperature >= 25 and status = false "
+ + "GROUP BY ([117, 155), 5ms) "
+ + "FILL(previousUntilLast, 12ms)");
+ assertTrue(hasResultSet);
+ int cnt;
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
+ assertEquals(retArray[cnt], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+
+ hasResultSet =
+ statement.execute(
+ "select sum(hardware), last_value(hardware), max_time(hardware) "
+ + "from root.ln.wf01.wt01 "
+ + "WHERE temperature >= 25 and status = false "
+ + "GROUP BY ([117, 155), 5ms) "
+ + "FILL(previousUntilLast, 12ms) "
+ + "order by time desc");
+ assertTrue(hasResultSet);
+ try (ResultSet resultSet = statement.getResultSet()) {
+ cnt = 0;
+ while (resultSet.next()) {
+ String ans =
+ resultSet.getString(TIMESTAMP_STR)
+ + ","
+ + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
+ + ","
+ + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
+ assertEquals(retArray[retArray.length - cnt - 1], ans);
+ cnt++;
+ }
+ assertEquals(retArray.length, cnt);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ @Test
+ public void MultiPathsMixWithValueFilterTest() {
+ String[] retArray =
+ new String[] {
+ "117,null,null,null,null,null,null",
+ "122,27.0,39.05,123,27,39.05,null",
+ "127,null,null,null,null,null,null",
+ "132,29.0,44.6,133,29,44.6,false",
+ "137,28.666666666666668,47.93333333333334,138,29,47.93333333333334,null",
+ "142,28.333333333333332,51.266666666666666,143,29,51.266666666666666,null",
+ "147,28.0,54.6,148,28,54.6,false",
+ "152,32.0,47.4,154,32,47.4,null"
+ };
+
+ /* Format result
+ linear, linear, linear, linear, linear, linear
+ 112, 25.0, 33.5, 114, 25, 33.5, false
+ 117, null, null, null, null, null, null
+ 122, 27.0(null), 39.05(null), 123(null), 27(null), 39.05(null), null
+ 127, null, null, null, null, null, null
+ 132, 29.0, 44.6, 133, 29, 44.6, false
+ 137, 28.67(null), 47.93(null), 138(null), 29(null), 47.93(null), null
+ 142, 28.33(null), 51.27(null), 143(null), 29(null), 51.27(null), null
+ 147, 28.0, 54.6, 148, 28, 54.6, false
+ 152, 32.0(null), 47.4(null), 154, 32(null), 47.4(null), null
+ 162, 40.0, 33.0, 166, 40, 33.0, null
+ */
+
+ try (Connection connection =
Review comment:
use `Connection conn = EnvFactory.getEnv().getConnection()`
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
Review comment:
`each time series` or `each aggregations` ? And maybe `nextIndices` is more sensitive than `nextIds`?
##########
File path: integration/src/test/java/org/apache/iotdb/db/integration/IoTDBGroupByFillMixPathsIT.java
##########
@@ -122,367 +127,361 @@ private void prepareData() {
}
}
- // @Test
- // public void singlePathMixTest() {
- // String[] retArray =
- // new String[] {
- // // "7,23.0,23,8"
- // "17,41.66666666666667,23,8",
- // "22,51.0,23,25",
- // "27,88.5,25,25",
- // "32,126.0,27,36",
- // "37,129.0,26,36",
- // "42,132.0,24,36",
- // "47,135.0,22,51",
- // "52,110.0,20,56",
- // "57,null,23,null",
- // "62,71.5,26,null"
- // // "72,33.0,33,72"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), last_value(temperature), max_time(temperature) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 10ms, 18ms],
- // int64[previousUntilLast, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.temperature"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixTest() {
- // String[] retArray =
- // new String[] {
- // "17,41.66666666666667,23.0,10,23,23.5,true",
- // "22,51.0,null,10,23,null,true",
- // "27,88.5,35.0,null,23,36.0,true",
- // "32,126.0,43.36,36,22,48.2,false",
- // "37,129.0,37.0,36,22,38.2,true",
- // "42,132.0,null,45,22,null,false",
- // "47,135.0,35.900000000000006,45,22,39.35,true",
- // "52,110.0,null,56,13,null,false",
- // "57,null,34.800000000000004,null,18,40.5,true",
- // "62,71.5,38.800000000000004,null,23,42.6,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 23.0, 11.0, 10, 23, 11.0, true
- // 17, 41.67(null), 23.0(null), 10(null), 23(null), 23.5(null), true(null)
- // 22, 51.0, null, 10(null), 23, null, true(null)
- // 27, 88.5(null), 35.0, null, 23(null), 36.0, true(null)
- // 32, 126.0, 43.36, 36, 22, 48.2, false
- // 37, 129.0(null), 37.0, 36(null), 22(null), 38.2, true(null)
- // 42, 132.0(null), null, 45, 22(null), null, false
- // 47, 135.0, 35.9(null), 45(null), 22, 39.35(null), true(null)
- // 52, 110.0, null, 56, 13, null, false
- // 57, null, 34.8, null, 18(null), 40.5, true(null)
- // 62, 71.5(null), 38.8(null), null, 23(null), 42.6(null), true(null)
- // 72, 33.0, 46.8, 74, 33, 46.8, true
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "GROUP BY ([17, 65), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void singlePathMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // // "112,33.5,33.5,114"
- // "117,null,null,114",
- // "122,39.05,39.05,114",
- // "127,null,null,114",
- // "132,44.6,44.6,133",
- // "137,47.93333333333334,47.93333333333334,133",
- // "142,51.266666666666666,51.266666666666666,133",
- // "147,54.6,54.6,148",
- // "152,47.4,47.4,148"
- // // "162,33.0,33.0,166"
- // };
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(hardware), last_value(hardware), max_time(hardware) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int64[previous, 17ms]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.lastValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.hardware"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
- //
- // @Test
- // public void MultiPathsMixWithValueFilterTest() {
- // String[] retArray =
- // new String[] {
- // "117,null,null,114,26,null,true",
- // "122,27.0,39.05,114,27,39.05,true",
- // "127,null,null,114,null,null,true",
- // "132,29.0,44.6,133,29,44.6,false",
- // "137,28.666666666666668,47.93333333333334,133,29,47.93333333333334,true",
- // "142,28.333333333333332,51.266666666666666,133,29,51.266666666666666,true",
- // "147,28.0,54.6,148,28,54.6,false",
- // "152,32.0,47.4,null,32,47.4,true"
- // };
- //
- // /* Format result
- // linear, linear, preUntil, linear, linear, value
- // 7, 25.0, 33.5, 114, 25, 33.5, false
- // 117, null, null, 114(null), 26(null), null, true(null)
- // 122, 27.0(null), 39.05(null), 114(null), 27(null), 39.05(null), true(null)
- // 127, null, null, 114(null), null, null, true(null)
- // 132, 29.0, 44.6, 133, 29, 44.6, false
- // 137, 28.67(null), 47.93(null), 133(null), 29(null), 47.93(null), true(null)
- // 142, 28.33(null), 51.27(null), 133(null), 29(null), 51.27(null), true(null)
- // 147, 28.0, 54.6, 148, 28, 54.6, false
- // 152, 32.0(null), 47.4(null), null, 32(null), 47.4(null), true(null)
- // 162, 40.0, 33.0, null, 40, 33.0, null
- // */
- //
- // try (Connection connection =
- // DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667/", "root", "root");
- // Statement statement = connection.createStatement()) {
- // boolean hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true])");
- // assertTrue(hasResultSet);
- // int cnt;
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[cnt], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- //
- // hasResultSet =
- // statement.execute(
- // "select sum(temperature), avg(hardware), max_time(status), "
- // + "min_value(temperature), max_value(hardware), first_value(status) "
- // + "from root.ln.wf01.wt01 "
- // + "WHERE temperature >= 25 and status = false "
- // + "GROUP BY ([117, 155), 5ms) "
- // + "FILL(double[linear, 12ms, 12ms], int32[linear, 12ms, 18ms], "
- // + "int64[previousUntilLast, 17ms], boolean[true]) "
- // + "order by time desc");
- // assertTrue(hasResultSet);
- // try (ResultSet resultSet = statement.getResultSet()) {
- // cnt = 0;
- // while (resultSet.next()) {
- // String ans =
- // resultSet.getString(TIMESTAMP_STR)
- // + ","
- // + resultSet.getString(sum("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(avg("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.maxTime("root.ln.wf01.wt01.status"))
- // + ","
- // + resultSet.getString(TestConstant.minValue("root.ln.wf01.wt01.temperature"))
- // + ","
- // + resultSet.getString(TestConstant.maxValue("root.ln.wf01.wt01.hardware"))
- // + ","
- // + resultSet.getString(TestConstant.firstValue("root.ln.wf01.wt01.status"));
- // assertEquals(retArray[retArray.length - cnt - 1], ans);
- // cnt++;
- // }
- // assertEquals(retArray.length, cnt);
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // fail(e.getMessage());
- // }
- // }
+ @Test
+ public void singlePathMixTest() {
+ String[] retArray =
+ new String[] {
+ // "7,23.0,23,8"
+ "17,23.0,23,8",
+ "22,51.0,23,25",
+ "27,51.0,23,25",
+ "32,126.0,27,36",
+ "37,126.0,27,36",
+ "42,126.0,27,36",
+ "47,135.0,22,51",
+ "52,110.0,20,56",
+ "57,110.0,20,56",
+ "62,110.0,20,56"
+ // "72,33.0,33,72"
+ };
+
+ try (Connection connection =
Review comment:
1. As new IT framework has been merged to master. Please use `Connection conn = EnvFactory.getEnv().getConnection();` to replace the original usage.
2. As every test case contains only read operations, we can use @BeforeClass and @AfterClass instead of @Before and @After to accelerate tests. And use
```java
ConfigFactory.getConfig().setPartitionInterval(1000);
EnvFactory.getEnv().initBeforeClass();
```
to replace
```java
EnvironmentUtils.closeStatMonitor();
EnvironmentUtils.envSetUp();
IoTDBDescriptor.getInstance().getConfig().setPartitionInterval(1000);
Class.forName(Config.JDBC_DRIVER_NAME);
```
You can reference `IoTDBGroupByIT`.
BTW, does it support quering with fill in cluster mode? This test class only has `LocalStandaloneTest` category.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
+ curTimestamp = curStartTime;
+ record = new RowRecord(curStartTime);
+ } else {
+ curTimestamp = curEndTime - 1;
+ record = new RowRecord(curEndTime - 1);
+ }
+
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ fillRecord(i, record);
+ continue;
+ }
+
+ long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+ if (cacheTime == curTimestamp) {
+ record.addField(getNextCacheValue(i), resultDataType[i]);
+ } else {
+ fillRecord(i, record);
+ }
+ }
+
+ try {
+ slideCache(record.getTimestamp());
+ } catch (QueryProcessException e) {
+ // ignored
Review comment:
It's not a good practise to ignore any exceptions. If we don't handle it, just adding a `WARN` or `ERROR` log is OK.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
+ curTimestamp = curStartTime;
+ record = new RowRecord(curStartTime);
+ } else {
+ curTimestamp = curEndTime - 1;
+ record = new RowRecord(curEndTime - 1);
+ }
+
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ fillRecord(i, record);
+ continue;
+ }
+
+ long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+ if (cacheTime == curTimestamp) {
+ record.addField(getNextCacheValue(i), resultDataType[i]);
+ } else {
+ fillRecord(i, record);
+ }
+ }
+
+ try {
+ slideCache(record.getTimestamp());
+ } catch (QueryProcessException e) {
+ // ignored
+ }
+
+ return record;
+ }
+
+ private void fillRecord(int index, RowRecord record) throws IOException {
+ // Don't fill count aggregation
+ if (Objects.equals(aggregations.get(index), "count")) {
+ record.addField((long) 0, TSDataType.INT64);
+ return;
+ }
+
+ IFill fill;
+ if (fillTypes != null) {
+ // old type fill logic
+ fill = fillTypes.get(resultDataType[index]);
+ } else {
+ fill = singleFill;
+ }
+ if (fill == null) {
+ record.addField(null);
+ return;
+ }
+
+ Pair<Long, Object> beforePair, afterPair;
+ if (ascending) {
+ if (previousValues[index] != null) {
+ beforePair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ beforePair = null;
+ }
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ afterPair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ afterPair = null;
+ }
+ } else {
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ beforePair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ beforePair = null;
+ }
+ if (previousValues[index] != null) {
+ afterPair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ afterPair = null;
+ }
+ }
+
+ if (fill instanceof PreviousFill) {
+ if (beforePair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && ((!((PreviousFill) fill).isUntilLast())
+ || (afterPair != null && afterPair.left < endTime))) {
+ record.addField(beforePair.right, resultDataType[index]);
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof LinearFill) {
+ LinearFill linearFill = new LinearFill();
+ if (beforePair != null
+ && afterPair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && (fill.getAfterRange() == -1
+ || fill.insideAfterRange(afterPair.left, record.getTimestamp()))) {
+ try {
+ TimeValuePair filledPair =
+ linearFill.averageWithTimeAndDataType(
+ new TimeValuePair(
+ beforePair.left,
+ TsPrimitiveType.getByType(resultDataType[index], beforePair.right)),
+ new TimeValuePair(
+ afterPair.left,
+ TsPrimitiveType.getByType(resultDataType[index], afterPair.right)),
+ record.getTimestamp(),
+ resultDataType[index]);
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (UnSupportedFillTypeException e) {
+ // Don't fill and ignore exception
+ record.addField(null);
+ }
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof ValueFill) {
+ try {
+ TimeValuePair filledPair = fill.getFillResult();
+ if (filledPair == null) {
+ filledPair = ((ValueFill) fill).getSpecifiedFillResult(resultDataType[index]);
+ }
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (QueryProcessException | StorageEngineException e) {
+ throw new IOException(e);
+ }
+ }
+ }
+
+ private Object getNextCacheValue(int index) throws IOException {
+ switch (resultDataType[index]) {
+ case INT32:
+ return nextTVLists.get(index).getInt(nextIds[index]);
+ case INT64:
+ return nextTVLists.get(index).getLong(nextIds[index]);
+ case FLOAT:
+ return nextTVLists.get(index).getFloat(nextIds[index]);
+ case DOUBLE:
+ return nextTVLists.get(index).getDouble(nextIds[index]);
+ case BOOLEAN:
+ return nextTVLists.get(index).getBoolean(nextIds[index]);
+ case TEXT:
+ return nextTVLists.get(index).getString(nextIds[index]);
+ default:
+ throw new IOException("unknown data type!");
+ }
+ }
+
+ private void slideCache(long curTimestamp) throws IOException, QueryProcessException {
+ BitSet slideSet = new BitSet(aggregations.size());
+ slideSet.clear();
Review comment:
Redundant `clear()`.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,313 @@
+package org.apache.iotdb.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next value and time for each time series
+ private int[] nextIds;
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, fetchSize));
Review comment:
Why use `10MB` as the memory budget, and what's the value of `fetchSize`?
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
Review comment:
Please add another one IT case to cover `leftCRight0 == false`.
##########
File path: server/src/main/java/org/apache/iotdb/db/query/dataset/groupby/GroupByFillDataSet.java
##########
@@ -0,0 +1,336 @@
+/*
+ * 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.db.query.dataset.groupby;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.exception.query.UnSupportedFillTypeException;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimeFillPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.executor.fill.IFill;
+import org.apache.iotdb.db.query.executor.fill.LinearFill;
+import org.apache.iotdb.db.query.executor.fill.PreviousFill;
+import org.apache.iotdb.db.query.executor.fill.ValueFill;
+import org.apache.iotdb.db.query.udf.datastructure.tv.ElasticSerializableTVList;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.utils.Pair;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupByFillDataSet extends GroupByEngineDataSet {
+
+ private GroupByEngineDataSet dataSet;
+
+ private final Map<TSDataType, IFill> fillTypes;
+ private final IFill singleFill;
+ private final List<String> aggregations;
+
+ // the result datatype for each time series
+ private final TSDataType[] resultDataType;
+
+ // the last value and time for each time series
+ private long[] previousTimes;
+ private Object[] previousValues;
+
+ // the next not null and unused rowId for each time series
+ private int[] nextIds;
+ // the next value and time for each time series
+ private List<ElasticSerializableTVList> nextTVLists;
+
+ public GroupByFillDataSet(QueryContext context, GroupByTimeFillPlan groupByTimeFillPlan)
+ throws QueryProcessException {
+ super(context, groupByTimeFillPlan);
+ this.aggregations = groupByTimeFillPlan.getDeduplicatedAggregations();
+ this.fillTypes = groupByTimeFillPlan.getFillType();
+ this.singleFill = groupByTimeFillPlan.getSingleFill();
+
+ this.resultDataType = new TSDataType[aggregations.size()];
+ initArrays(context);
+ }
+
+ private void initArrays(QueryContext context) throws QueryProcessException {
+ for (int i = 0; i < aggregations.size(); i++) {
+ switch (aggregations.get(i)) {
+ case "avg":
+ case "sum":
+ resultDataType[i] = TSDataType.DOUBLE;
+ break;
+ case "count":
+ case "max_time":
+ case "min_time":
+ resultDataType[i] = TSDataType.INT64;
+ break;
+ case "first_value":
+ case "last_value":
+ case "max_value":
+ case "min_value":
+ resultDataType[i] = dataTypes.get(i);
+ break;
+ default:
+ throw new QueryProcessException("unknown aggregation type, please update this code!");
+ }
+ }
+
+ previousTimes = new long[aggregations.size()];
+ previousValues = new Object[aggregations.size()];
+ nextIds = new int[aggregations.size()];
+ Arrays.fill(previousTimes, Long.MAX_VALUE);
+ Arrays.fill(previousValues, null);
+ Arrays.fill(nextIds, 0);
+
+ nextTVLists = new ArrayList<>();
+ for (int i = 0; i < aggregations.size(); i++) {
+ nextTVLists.add(
+ ElasticSerializableTVList.newElasticSerializableTVList(
+ resultDataType[i], context.getQueryId(), 10, 2));
+ }
+ }
+
+ public void setDataSet(GroupByEngineDataSet dataSet) {
+ this.dataSet = dataSet;
+ }
+
+ public void initCache() throws IOException, QueryProcessException {
+ BitSet cacheSet = new BitSet(aggregations.size());
+ cacheSet.clear();
+ while (cacheSet.cardinality() < aggregations.size() && dataSet.hasNextWithoutConstraint()) {
+ RowRecord record = dataSet.nextWithoutConstraint();
+ long timestamp = record.getTimestamp();
+ List<Field> fields = record.getFields();
+ for (int i = 0; i < fields.size(); i++) {
+ Field field = fields.get(i);
+ if (field == null) {
+ continue;
+ }
+
+ if (ascending && timestamp < startTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else if (!ascending && timestamp >= endTime) {
+ previousTimes[i] = timestamp;
+ previousValues[i] = field.getObjectValue(resultDataType[i]);
+ } else {
+ nextTVLists.get(i).put(timestamp, field.getObjectValue(resultDataType[i]));
+ cacheSet.set(i);
+ }
+ }
+ }
+ }
+
+ @Override
+ public RowRecord nextWithoutConstraint() throws IOException {
+ if (!hasCachedTimeInterval) {
+ throw new IOException(
+ "need to call hasNext() before calling next() " + "in GroupByFillDataSet.");
+ }
+
+ hasCachedTimeInterval = false;
+ RowRecord record;
+ long curTimestamp;
+ if (leftCRightO) {
+ curTimestamp = curStartTime;
+ record = new RowRecord(curStartTime);
+ } else {
+ curTimestamp = curEndTime - 1;
+ record = new RowRecord(curEndTime - 1);
+ }
+
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ fillRecord(i, record);
+ continue;
+ }
+
+ long cacheTime = nextTVLists.get(i).getTime(nextIds[i]);
+ if (cacheTime == curTimestamp) {
+ record.addField(getNextCacheValue(i), resultDataType[i]);
+ } else {
+ fillRecord(i, record);
+ }
+ }
+
+ try {
+ slideCache(record.getTimestamp());
+ } catch (QueryProcessException e) {
+ // ignored
+ }
+
+ return record;
+ }
+
+ private void fillRecord(int index, RowRecord record) throws IOException {
+ // Don't fill count aggregation
+ if (Objects.equals(aggregations.get(index), "count")) {
+ record.addField((long) 0, TSDataType.INT64);
+ return;
+ }
+
+ IFill fill;
+ if (fillTypes != null) {
+ // old type fill logic
+ fill = fillTypes.get(resultDataType[index]);
+ } else {
+ fill = singleFill;
+ }
+ if (fill == null) {
+ record.addField(null);
+ return;
+ }
+
+ Pair<Long, Object> beforePair, afterPair;
+ if (ascending) {
+ if (previousValues[index] != null) {
+ beforePair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ beforePair = null;
+ }
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ afterPair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ afterPair = null;
+ }
+ } else {
+ if (nextIds[index] < nextTVLists.get(index).size()) {
+ beforePair =
+ new Pair<>(nextTVLists.get(index).getTime(nextIds[index]), getNextCacheValue(index));
+ } else {
+ beforePair = null;
+ }
+ if (previousValues[index] != null) {
+ afterPair = new Pair<>(previousTimes[index], previousValues[index]);
+ } else {
+ afterPair = null;
+ }
+ }
+
+ if (fill instanceof PreviousFill) {
+ if (beforePair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && ((!((PreviousFill) fill).isUntilLast())
+ || (afterPair != null && afterPair.left < endTime))) {
+ record.addField(beforePair.right, resultDataType[index]);
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof LinearFill) {
+ LinearFill linearFill = new LinearFill();
+ if (beforePair != null
+ && afterPair != null
+ && (fill.getBeforeRange() == -1
+ || fill.insideBeforeRange(beforePair.left, record.getTimestamp()))
+ && (fill.getAfterRange() == -1
+ || fill.insideAfterRange(afterPair.left, record.getTimestamp()))) {
+ try {
+ TimeValuePair filledPair =
+ linearFill.averageWithTimeAndDataType(
+ new TimeValuePair(
+ beforePair.left,
+ TsPrimitiveType.getByType(resultDataType[index], beforePair.right)),
+ new TimeValuePair(
+ afterPair.left,
+ TsPrimitiveType.getByType(resultDataType[index], afterPair.right)),
+ record.getTimestamp(),
+ resultDataType[index]);
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (UnSupportedFillTypeException e) {
+ // Don't fill and ignore unsupported type exception
+ record.addField(null);
+ }
+ } else {
+ record.addField(null);
+ }
+ } else if (fill instanceof ValueFill) {
+ try {
+ TimeValuePair filledPair = fill.getFillResult();
+ if (filledPair == null) {
+ filledPair = ((ValueFill) fill).getSpecifiedFillResult(resultDataType[index]);
+ }
+ record.addField(filledPair.getValue().getValue(), resultDataType[index]);
+ } catch (NumberFormatException ne) {
+ // Don't fill and ignore type convert exception
+ record.addField(null);
+ } catch (QueryProcessException | StorageEngineException e) {
+ throw new IOException(e);
+ }
+ }
+ }
+
+ private Object getNextCacheValue(int index) throws IOException {
+ switch (resultDataType[index]) {
+ case INT32:
+ return nextTVLists.get(index).getInt(nextIds[index]);
+ case INT64:
+ return nextTVLists.get(index).getLong(nextIds[index]);
+ case FLOAT:
+ return nextTVLists.get(index).getFloat(nextIds[index]);
+ case DOUBLE:
+ return nextTVLists.get(index).getDouble(nextIds[index]);
+ case BOOLEAN:
+ return nextTVLists.get(index).getBoolean(nextIds[index]);
+ case TEXT:
+ return nextTVLists.get(index).getBinary(nextIds[index]);
+ default:
+ throw new IOException("unknown data type!");
+ }
+ }
+
+ private void slideCache(long curTimestamp) throws IOException, QueryProcessException {
+ BitSet slideSet = new BitSet(aggregations.size());
+ slideSet.clear();
+ for (int i = 0; i < aggregations.size(); i++) {
+ if (nextTVLists.get(i).size() == nextIds[i]) {
+ continue;
+ }
+
+ // slide cache when the current TV is used
+ if (nextTVLists.get(i).getTime(nextIds[i]) == curTimestamp) {
+ previousTimes[i] = curTimestamp;
+ previousValues[i] = getNextCacheValue(i);
+ nextIds[i] += 1;
+ nextTVLists.get(i).setEvictionUpperBound(nextIds[i]);
+ slideSet.set(i);
+ }
+ }
+
+ while (slideSet.cardinality() > 0 && dataSet.hasNextWithoutConstraint()) {
Review comment:
In this loop if `slideSet.cardinality() == true`, it will iterate until `dataSet.hasNextWithoutConstraint() == false`, which means `slideCache` will tranverse all the data. Is it as our expected ? Maybe some additional condition or mutation of `slideSet` is missing ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org