Skip to content

Commit 3fed72a

Browse files
authored
Support SAMPLE, DERIVATIVE and INTEGRAL table functions
1 parent c7d85f2 commit 3fed72a

50 files changed

Lines changed: 2809 additions & 184 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iotdb.relational.it.db.it;
21+
22+
import org.apache.iotdb.it.env.EnvFactory;
23+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
24+
import org.apache.iotdb.itbase.category.TableClusterIT;
25+
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
26+
27+
import org.junit.AfterClass;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
31+
import org.junit.runner.RunWith;
32+
33+
import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
34+
import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
35+
import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
36+
37+
@RunWith(IoTDBTestRunner.class)
38+
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
39+
public class IoTDBIntegralTableIT {
40+
41+
private static final String DATABASE = "test";
42+
43+
private static final String[] SQLs =
44+
new String[] {
45+
"CREATE DATABASE " + DATABASE,
46+
"USE " + DATABASE,
47+
"CREATE TABLE integral_table(device_id STRING TAG, x DOUBLE FIELD, y DOUBLE FIELD)",
48+
"INSERT INTO integral_table(time, device_id, x, y) VALUES "
49+
+ "(0, 'd1', 1.0, 100.0), (1000, 'd1', 3.0, 104.0), "
50+
+ "(2000, 'd1', 2.0, 102.0), (3000, 'd1', 4.0, 108.0), "
51+
+ "(0, 'd2', 10.0, 200.0), (1000, 'd2', 12.0, 204.0), "
52+
+ "(2000, 'd2', 11.0, 202.0), (3000, 'd2', 13.0, 208.0)",
53+
"CREATE TABLE integral_sparse(device_id STRING TAG, v DOUBLE FIELD)",
54+
// device a skips the null point at time 1s, device b has a single point and device n only
55+
// contains negative values
56+
"INSERT INTO integral_sparse(time, device_id, v) VALUES "
57+
+ "(0, 'a', 1.0), (1000, 'a', null), (2000, 'a', 2.0), (3000, 'a', 4.0), "
58+
+ "(0, 'b', 5.0), "
59+
+ "(0, 'n', -1.0), (1000, 'n', -3.0)",
60+
"CREATE TABLE integral_invalid(device_id STRING TAG, s STRING FIELD)",
61+
"INSERT INTO integral_invalid(time, device_id, s) VALUES (0, 'd1', 'a'), (1000, 'd1', 'b')",
62+
// both tables have a ts column, but only integral_join_ts.ts is TIMESTAMP
63+
"CREATE TABLE integral_join_ts(device_id STRING TAG, v DOUBLE FIELD, ts TIMESTAMP FIELD)",
64+
"INSERT INTO integral_join_ts(time, device_id, v, ts) VALUES "
65+
+ "(0, 'd1', 1.0, 2021-01-01T00:00:00), (1000, 'd1', 3.0, 2021-01-01T00:00:01)",
66+
"CREATE TABLE integral_join_int(device_id STRING TAG, v DOUBLE FIELD, ts INT64 FIELD)",
67+
"INSERT INTO integral_join_int(time, device_id, v, ts) VALUES "
68+
+ "(0, 'd1', 1.0, 0), (1000, 'd1', 3.0, 1000)",
69+
};
70+
71+
@BeforeClass
72+
public static void setUp() throws Exception {
73+
EnvFactory.getEnv().initClusterEnvironment();
74+
prepareTableData(SQLs);
75+
}
76+
77+
@AfterClass
78+
public static void tearDown() throws Exception {
79+
EnvFactory.getEnv().cleanClusterEnvironment();
80+
}
81+
82+
@Test
83+
public void testIntegralBasicAndMultiGroup() {
84+
String[] expectedHeader =
85+
new String[] {"device_id", "area", "area_default", "area_with_unit", "area_timecol"};
86+
String[] retArray =
87+
new String[] {
88+
// d1.x: (1+3)/2 + (3+2)/2 + (2+4)/2 = 7.5; unit 10s makes the result 1/10 of unit 1s
89+
"d1,7.5,7.5,0.75,7.5,",
90+
// d2.x: (10+12)/2 + (12+11)/2 + (11+13)/2 = 34.5
91+
"d2,34.5,34.5,3.45,34.5,"
92+
};
93+
// Groups are integrated independently; the unit defaults to 1s when omitted, accepts a bare
94+
// duration (1s / 10s) or an integer number of ticks, and the third argument accepts an
95+
// explicit time column name.
96+
tableResultSetEqualTest(
97+
"SELECT device_id, integral(x, 1s) AS area, integral(x) AS area_default, "
98+
+ "integral(x, 10s) AS area_with_unit, integral(x, 1s, time) AS area_timecol "
99+
+ "FROM integral_table GROUP BY device_id ORDER BY device_id",
100+
expectedHeader,
101+
retArray,
102+
DATABASE);
103+
}
104+
105+
@Test
106+
public void testIntegralWithTimeWindow() {
107+
String[] expectedHeader = new String[] {"device_id", "window_start", "area"};
108+
String[] retArray =
109+
new String[] {
110+
// window [0s, 2s) only accumulates (0s, 1.0) and (1s, 3.0)
111+
"d1,1970-01-01T00:00:00.000Z,2.0,",
112+
"d1,1970-01-01T00:00:02.000Z,3.0,",
113+
"d2,1970-01-01T00:00:00.000Z,11.0,",
114+
"d2,1970-01-01T00:00:02.000Z,12.0,"
115+
};
116+
// Each (device, window) group is integrated independently: no interpolation at the window
117+
// boundary and no accumulation across windows.
118+
tableResultSetEqualTest(
119+
"SELECT device_id, date_bin(2s, time) AS window_start, integral(x, 1s) AS area "
120+
+ "FROM integral_table GROUP BY device_id, date_bin(2s, time) "
121+
+ "ORDER BY device_id, window_start",
122+
expectedHeader,
123+
retArray,
124+
DATABASE);
125+
}
126+
127+
@Test
128+
public void testIntegralSkipsNullAndHandlesInsufficientPoints() {
129+
String[] expectedHeader = new String[] {"device_id", "area"};
130+
String[] retArray =
131+
new String[] {
132+
// the null point is skipped, so the remaining points are (0s,1),(2s,2),(3s,4)
133+
"a,6.0,",
134+
// fewer than two effective points have no time span, so the area is 0
135+
"b,0.0,",
136+
// negative values are allowed and the result may be negative
137+
"n,-2.0,"
138+
};
139+
tableResultSetEqualTest(
140+
"SELECT device_id, integral(v, 1s) AS area FROM integral_sparse "
141+
+ "GROUP BY device_id ORDER BY device_id",
142+
expectedHeader,
143+
retArray,
144+
DATABASE);
145+
}
146+
147+
@Test
148+
public void testIntegralWithColumnRegex() {
149+
String[] expectedHeader = new String[] {"device_id", "_col1_x", "_col2_y"};
150+
String[] retArray = new String[] {"d1,7.5,310.0,", "d2,34.5,610.0,"};
151+
// COLUMNS('^[xy]$') is expanded into one integral per matched numeric column.
152+
tableResultSetEqualTest(
153+
"SELECT device_id, integral(COLUMNS('^[xy]$'), 1s) FROM integral_table "
154+
+ "GROUP BY device_id ORDER BY device_id",
155+
expectedHeader,
156+
retArray,
157+
DATABASE);
158+
}
159+
160+
@Test
161+
public void testIntegralTimeColumnOfJoin() {
162+
String[] expectedHeader = new String[] {"area"};
163+
String[] retArray = new String[] {"2.0,"};
164+
// a.ts is TIMESTAMP while b.ts is INT64: the third argument is validated by the column it
165+
// resolves to, so the same query is accepted whichever side of the join that column is on
166+
tableResultSetEqualTest(
167+
"SELECT integral(a.v, 1s, a.ts) AS area FROM integral_join_ts a "
168+
+ "JOIN integral_join_int b ON a.device_id = b.device_id AND a.time = b.time",
169+
expectedHeader,
170+
retArray,
171+
DATABASE);
172+
tableResultSetEqualTest(
173+
"SELECT integral(a.v, 1s, a.ts) AS area FROM integral_join_int b "
174+
+ "JOIN integral_join_ts a ON a.device_id = b.device_id AND a.time = b.time",
175+
expectedHeader,
176+
retArray,
177+
DATABASE);
178+
// the INT64 column is rejected in both join orders instead of silently producing a wrong
179+
// result when a TIMESTAMP column with the same name comes first in the visible fields
180+
tableAssertTestFail(
181+
"SELECT integral(a.v, 1s, b.ts) FROM integral_join_ts a "
182+
+ "JOIN integral_join_int b ON a.device_id = b.device_id AND a.time = b.time",
183+
"must be actual time name",
184+
DATABASE);
185+
tableAssertTestFail(
186+
"SELECT integral(a.v, 1s, b.ts) FROM integral_join_int b "
187+
+ "JOIN integral_join_ts a ON a.device_id = b.device_id AND a.time = b.time",
188+
"must be actual time name",
189+
DATABASE);
190+
}
191+
192+
@Test
193+
public void testIntegralInvalidArguments() {
194+
tableAssertTestFail(
195+
"SELECT integral(s, 1s) FROM integral_invalid",
196+
"only support numeric data types",
197+
DATABASE);
198+
tableAssertTestFail(
199+
"SELECT integral(x, 'abc') FROM integral_table",
200+
"should be a positive duration literal",
201+
DATABASE);
202+
tableAssertTestFail(
203+
"SELECT integral(x, '0s') FROM integral_table",
204+
"should be a positive duration literal",
205+
DATABASE);
206+
tableAssertTestFail(
207+
"SELECT integral(x, 0s) FROM integral_table",
208+
"should be a positive duration literal",
209+
DATABASE);
210+
tableAssertTestFail(
211+
"SELECT integral(x, 1s, device_id) FROM integral_table",
212+
"must be actual time name",
213+
DATABASE);
214+
}
215+
}

0 commit comments

Comments
 (0)