Skip to content

Commit f99d8b4

Browse files
committed
Add integration tests for correlation, covariance, regression, and moments functions
1 parent 9b20bfd commit f99d8b4

7 files changed

Lines changed: 1043 additions & 8 deletions

File tree

integration-test/src/main/java/org/apache/iotdb/itbase/constant/TestConstant.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ public static String varSamp(String path) {
145145
return String.format("var_samp(%s)", path);
146146
}
147147

148+
public static String corr(String path) {
149+
return String.format("corr(%s)", path);
150+
}
151+
152+
public static String covarPop(String path) {
153+
return String.format("covar_pop(%s)", path);
154+
}
155+
156+
public static String covarSamp(String path) {
157+
return String.format("covar_samp(%s)", path);
158+
}
159+
160+
public static String regrSlope(String path) {
161+
return String.format("regr_slope(%s)", path);
162+
}
163+
164+
public static String regrIntercept(String path) {
165+
return String.format("regr_intercept(%s)", path);
166+
}
167+
168+
public static String kurtosis(String path) {
169+
return String.format("kurtosis(%s)", path);
170+
}
171+
172+
public static String skewness(String path) {
173+
return String.format("skewness(%s)", path);
174+
}
175+
148176
public static String countUDAF(String path) {
149177
return String.format("count_udaf(%s)", path);
150178
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
package org.apache.iotdb.db.it.aggregation;
20+
21+
import org.apache.iotdb.it.env.EnvFactory;
22+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
23+
import org.apache.iotdb.itbase.category.ClusterIT;
24+
import org.apache.iotdb.itbase.category.LocalStandaloneIT;
25+
26+
import org.junit.AfterClass;
27+
import org.junit.Assert;
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 java.sql.Connection;
34+
import java.sql.ResultSet;
35+
import java.sql.Statement;
36+
37+
import static org.apache.iotdb.db.it.utils.TestUtils.prepareData;
38+
import static org.apache.iotdb.db.it.utils.TestUtils.resultSetEqualTest;
39+
import static org.apache.iotdb.itbase.constant.TestConstant.DEVICE;
40+
import static org.apache.iotdb.itbase.constant.TestConstant.TIMESTAMP_STR;
41+
import static org.apache.iotdb.itbase.constant.TestConstant.corr;
42+
import static org.junit.Assert.fail;
43+
44+
@RunWith(IoTDBTestRunner.class)
45+
@Category({LocalStandaloneIT.class, ClusterIT.class})
46+
public class IoTDBCorrelationIT {
47+
48+
protected static final String[] SQLs =
49+
new String[] {
50+
"CREATE DATABASE root.db",
51+
"CREATE TIMESERIES root.db.d1.s1 WITH DATATYPE=INT32, ENCODING=PLAIN",
52+
"CREATE TIMESERIES root.db.d1.s2 WITH DATATYPE=INT64, ENCODING=PLAIN",
53+
"CREATE TIMESERIES root.db.d1.s3 WITH DATATYPE=BOOLEAN, ENCODING=PLAIN",
54+
"CREATE TIMESERIES root.db.d1.s4 WITH DATATYPE=FLOAT, ENCODING=PLAIN",
55+
"CREATE TIMESERIES root.db.d1.s5 WITH DATATYPE=DOUBLE, ENCODING=PLAIN",
56+
"CREATE TIMESERIES root.db.d1.s6 WITH DATATYPE=TEXT, ENCODING=PLAIN",
57+
"CREATE TIMESERIES root.db.d2.s1 WITH DATATYPE=INT32, ENCODING=PLAIN",
58+
"CREATE TIMESERIES root.db.d2.s2 WITH DATATYPE=INT64, ENCODING=PLAIN",
59+
"CREATE TIMESERIES root.db.d2.s4 WITH DATATYPE=FLOAT, ENCODING=PLAIN",
60+
"CREATE TIMESERIES root.db.d2.s5 WITH DATATYPE=DOUBLE, ENCODING=PLAIN",
61+
"INSERT INTO root.db.d1(timestamp,s1,s2,s3,s4,s5,s6) values(1, 1, 1, true, 1, 1, \"1\")",
62+
"INSERT INTO root.db.d1(timestamp,s1,s2,s3,s4,s5,s6) values(2, 2, 2, false, 2, 2, \"2\")",
63+
"INSERT INTO root.db.d1(timestamp,s1,s2,s3,s4,s5,s6) values(3, 3, 2, false, 3, 2, \"2\")",
64+
"INSERT INTO root.db.d1(timestamp,s1,s2,s3,s4,s5,s6) values(10000000000, 4, 1, true, 4, 1, \"1\")",
65+
"INSERT INTO root.db.d1(timestamp,s1,s2,s3,s4,s5,s6) values(10000000001, 5, 1, true, 5, 1, \"1\")",
66+
"INSERT INTO root.db.d2(timestamp,s1,s2,s4,s5) values(1, 1, 2, 3, 4)",
67+
"INSERT INTO root.db.d2(timestamp,s1,s2,s4,s5) values(2, 1, 2, 3, 4)",
68+
"INSERT INTO root.db.d2(timestamp,s1,s2,s4,s5) values(10000000000, 1, 2, 3, 4)",
69+
"INSERT INTO root.db.d2(timestamp,s1,s2,s4,s5) values(10000000001, 1, 2, 3, 4)",
70+
"INSERT INTO root.db.d2(timestamp,s1,s2,s4,s5) values(10000000002, 1, 2, 3, 4)",
71+
"flush"
72+
};
73+
74+
@BeforeClass
75+
public static void setUp() throws Exception {
76+
EnvFactory.getEnv().getConfig().getCommonConfig().setPartitionInterval(1000);
77+
EnvFactory.getEnv().initClusterEnvironment();
78+
prepareData(SQLs);
79+
}
80+
81+
@AfterClass
82+
public static void tearDown() throws Exception {
83+
EnvFactory.getEnv().cleanClusterEnvironment();
84+
}
85+
86+
@Test
87+
public void testCorrWithUnsupportedTypesAndWrongArity() {
88+
String typeError =
89+
"Aggregate functions [CORR, COVAR_POP, COVAR_SAMP, REGR_SLOPE, REGR_INTERCEPT] only support "
90+
+ "numeric data types [INT32, INT64, FLOAT, DOUBLE, TIMESTAMP]";
91+
String argError = "Error size of input expressions";
92+
try (Connection connection = EnvFactory.getEnv().getConnection();
93+
Statement statement = connection.createStatement()) {
94+
try {
95+
try (ResultSet resultSet = statement.executeQuery("SELECT corr(s3, s1) FROM root.db.d1")) {
96+
resultSet.next();
97+
fail();
98+
}
99+
} catch (Exception e) {
100+
Assert.assertTrue(e.getMessage(), e.getMessage().contains(typeError));
101+
}
102+
103+
try {
104+
try (ResultSet resultSet = statement.executeQuery("SELECT corr(s6, s1) FROM root.db.d1")) {
105+
resultSet.next();
106+
fail();
107+
}
108+
} catch (Exception e) {
109+
Assert.assertTrue(e.getMessage(), e.getMessage().contains(typeError));
110+
}
111+
112+
try {
113+
statement.executeQuery("SELECT corr(s1) FROM root.db.d1");
114+
fail();
115+
} catch (Exception e) {
116+
Assert.assertTrue(e.getMessage(), e.getMessage().contains(argError));
117+
}
118+
} catch (Exception e) {
119+
e.printStackTrace();
120+
fail(e.getMessage());
121+
}
122+
}
123+
124+
@Test
125+
public void testCorrWithDifferentTypes() {
126+
String[] expectedHeader =
127+
new String[] {corr("root.db.d1.s1, root.db.d1.s2"), corr("root.db.d1.s4, root.db.d1.s5")};
128+
String[] retArray = new String[] {"-0.28867513459481287,-0.28867513459481287,"};
129+
resultSetEqualTest("select corr(s1,s2),corr(s4,s5) from root.db.d1", expectedHeader, retArray);
130+
131+
retArray = new String[] {"0.8660254037844386,0.8660254037844386,"};
132+
resultSetEqualTest(
133+
"select corr(s1,s2),corr(s4,s5) from root.db.d1 where time < 10", expectedHeader, retArray);
134+
}
135+
136+
@Test
137+
public void testCorrAlignByDevice() {
138+
String[] expectedHeader = new String[] {DEVICE, corr("s1, s2"), corr("s4, s5")};
139+
String[] retArray = new String[] {"root.db.d1,-0.28867513459481287,-0.28867513459481287,"};
140+
resultSetEqualTest(
141+
"select corr(s1,s2),corr(s4,s5) from root.db.d1 align by device", expectedHeader, retArray);
142+
143+
retArray = new String[] {"root.db.d1,0.8660254037844386,0.8660254037844386,"};
144+
resultSetEqualTest(
145+
"select corr(s1,s2),corr(s4,s5) from root.db.d1 where time < 10 align by device",
146+
expectedHeader,
147+
retArray);
148+
}
149+
150+
@Test
151+
public void testCorrInHaving() {
152+
String[] expectedHeader = new String[] {corr("root.db.d1.s1, root.db.d1.s2")};
153+
String[] retArray = new String[] {"-0.28867513459481287,"};
154+
resultSetEqualTest(
155+
"select corr(s1,s2) from root.db.d1 having corr(s1,s2) < 0", expectedHeader, retArray);
156+
}
157+
158+
@Test
159+
public void testCorrMultiDeviceWithoutGroupByLevel() {
160+
String[] expectedHeader =
161+
new String[] {
162+
corr("root.db.d1.s1, root.db.d1.s2"),
163+
corr("root.db.d1.s1, root.db.d2.s2"),
164+
corr("root.db.d2.s1, root.db.d1.s2"),
165+
corr("root.db.d2.s1, root.db.d2.s2")
166+
};
167+
String[] retArray = new String[] {"-0.28867513459481287,null,null,null,"};
168+
resultSetEqualTest("select corr(s1,s2) from root.db.d1,root.db.d2", expectedHeader, retArray);
169+
}
170+
171+
@Test
172+
public void testCorrMultiDeviceWithGroupByLevel() {
173+
String[] expectedHeader = new String[] {corr("root.*.*.s1, root.*.*.s2")};
174+
String[] retArray = new String[] {"-0.08111071056538134,"};
175+
resultSetEqualTest(
176+
"select corr(s1,s2) from root.db.* group by level = 0", expectedHeader, retArray);
177+
}
178+
179+
@Test
180+
public void testCorrWithSlidingWindow() {
181+
String[] expectedHeader = new String[] {TIMESTAMP_STR, corr("root.db.d1.s1, root.db.d1.s2")};
182+
String[] retArray = new String[] {"1,0.8660254037844387,", "3,null,"};
183+
resultSetEqualTest(
184+
"select corr(s1,s2) from root.db.d1 group by time([1,4),3ms,2ms)",
185+
expectedHeader,
186+
retArray);
187+
}
188+
}

0 commit comments

Comments
 (0)