Skip to content

Commit 3fba166

Browse files
committed
Support the agg function and tvf, including Sample, Derivative and integral
fix antlr fix04 add more IT fix01 fix01 Support the function including Sample, Derivative and integral
1 parent 536dd7f commit 3fba166

48 files changed

Lines changed: 2566 additions & 131 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: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
};
63+
64+
@BeforeClass
65+
public static void setUp() throws Exception {
66+
EnvFactory.getEnv().initClusterEnvironment();
67+
prepareTableData(SQLs);
68+
}
69+
70+
@AfterClass
71+
public static void tearDown() throws Exception {
72+
EnvFactory.getEnv().cleanClusterEnvironment();
73+
}
74+
75+
@Test
76+
public void testIntegralBasicAndMultiGroup() {
77+
String[] expectedHeader =
78+
new String[] {"device_id", "area", "area_default", "area_with_unit", "area_timecol"};
79+
String[] retArray =
80+
new String[] {
81+
// d1.x: (1+3)/2 + (3+2)/2 + (2+4)/2 = 7.5; unit 10s makes the result 1/10 of unit 1s
82+
"d1,7.5,7.5,0.75,7.5,",
83+
// d2.x: (10+12)/2 + (12+11)/2 + (11+13)/2 = 34.5
84+
"d2,34.5,34.5,3.45,34.5,"
85+
};
86+
// Groups are integrated independently; the unit defaults to 1s when omitted, accepts a bare
87+
// duration (1s / 10s) or an integer number of ticks, and the third argument accepts an
88+
// explicit time column name.
89+
tableResultSetEqualTest(
90+
"SELECT device_id, integral(x, 1s) AS area, integral(x) AS area_default, "
91+
+ "integral(x, 10s) AS area_with_unit, integral(x, 1s, time) AS area_timecol "
92+
+ "FROM integral_table GROUP BY device_id ORDER BY device_id",
93+
expectedHeader,
94+
retArray,
95+
DATABASE);
96+
}
97+
98+
@Test
99+
public void testIntegralWithTimeWindow() {
100+
String[] expectedHeader = new String[] {"device_id", "window_start", "area"};
101+
String[] retArray =
102+
new String[] {
103+
// window [0s, 2s) only accumulates (0s, 1.0) and (1s, 3.0)
104+
"d1,1970-01-01T00:00:00.000Z,2.0,",
105+
"d1,1970-01-01T00:00:02.000Z,3.0,",
106+
"d2,1970-01-01T00:00:00.000Z,11.0,",
107+
"d2,1970-01-01T00:00:02.000Z,12.0,"
108+
};
109+
// Each (device, window) group is integrated independently: no interpolation at the window
110+
// boundary and no accumulation across windows.
111+
tableResultSetEqualTest(
112+
"SELECT device_id, date_bin(2s, time) AS window_start, integral(x, 1s) AS area "
113+
+ "FROM integral_table GROUP BY device_id, date_bin(2s, time) "
114+
+ "ORDER BY device_id, window_start",
115+
expectedHeader,
116+
retArray,
117+
DATABASE);
118+
}
119+
120+
@Test
121+
public void testIntegralSkipsNullAndHandlesInsufficientPoints() {
122+
String[] expectedHeader = new String[] {"device_id", "area"};
123+
String[] retArray =
124+
new String[] {
125+
// the null point is skipped, so the remaining points are (0s,1),(2s,2),(3s,4)
126+
"a,6.0,",
127+
// fewer than two effective points have no time span, so the area is 0
128+
"b,0.0,",
129+
// negative values are allowed and the result may be negative
130+
"n,-2.0,"
131+
};
132+
tableResultSetEqualTest(
133+
"SELECT device_id, integral(v, 1s) AS area FROM integral_sparse "
134+
+ "GROUP BY device_id ORDER BY device_id",
135+
expectedHeader,
136+
retArray,
137+
DATABASE);
138+
}
139+
140+
@Test
141+
public void testIntegralWithColumnRegex() {
142+
String[] expectedHeader = new String[] {"device_id", "_col1_x", "_col2_y"};
143+
String[] retArray = new String[] {"d1,7.5,310.0,", "d2,34.5,610.0,"};
144+
// COLUMNS('^[xy]$') is expanded into one integral per matched numeric column.
145+
tableResultSetEqualTest(
146+
"SELECT device_id, integral(COLUMNS('^[xy]$'), 1s) FROM integral_table "
147+
+ "GROUP BY device_id ORDER BY device_id",
148+
expectedHeader,
149+
retArray,
150+
DATABASE);
151+
}
152+
153+
@Test
154+
public void testIntegralInvalidArguments() {
155+
tableAssertTestFail(
156+
"SELECT integral(s, 1s) FROM integral_invalid",
157+
"only support numeric data types",
158+
DATABASE);
159+
tableAssertTestFail(
160+
"SELECT integral(x, 'abc') FROM integral_table",
161+
"should be a positive duration literal",
162+
DATABASE);
163+
tableAssertTestFail(
164+
"SELECT integral(x, '0s') FROM integral_table",
165+
"should be a positive duration literal",
166+
DATABASE);
167+
tableAssertTestFail(
168+
"SELECT integral(x, 0s) FROM integral_table",
169+
"should be a positive duration literal",
170+
DATABASE);
171+
tableAssertTestFail(
172+
"SELECT integral(x, 1s, device_id) FROM integral_table",
173+
"must be actual time name",
174+
DATABASE);
175+
}
176+
}

0 commit comments

Comments
 (0)