|
| 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, and the third |
| 87 | + // argument accepts an explicit time column name. |
| 88 | + tableResultSetEqualTest( |
| 89 | + "SELECT device_id, integral(x, '1s') AS area, integral(x) AS area_default, " |
| 90 | + + "integral(x, '10s') AS area_with_unit, integral(x, '1s', time) AS area_timecol " |
| 91 | + + "FROM integral_table GROUP BY device_id ORDER BY device_id", |
| 92 | + expectedHeader, |
| 93 | + retArray, |
| 94 | + DATABASE); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testIntegralWithTimeWindow() { |
| 99 | + String[] expectedHeader = new String[] {"device_id", "window_start", "area"}; |
| 100 | + String[] retArray = |
| 101 | + new String[] { |
| 102 | + // window [0s, 2s) only accumulates (0s, 1.0) and (1s, 3.0) |
| 103 | + "d1,1970-01-01T00:00:00.000Z,2.0,", |
| 104 | + "d1,1970-01-01T00:00:02.000Z,3.0,", |
| 105 | + "d2,1970-01-01T00:00:00.000Z,11.0,", |
| 106 | + "d2,1970-01-01T00:00:02.000Z,12.0," |
| 107 | + }; |
| 108 | + // Each (device, window) group is integrated independently: no interpolation at the window |
| 109 | + // boundary and no accumulation across windows. |
| 110 | + tableResultSetEqualTest( |
| 111 | + "SELECT device_id, date_bin(2s, time) AS window_start, integral(x, '1s') AS area " |
| 112 | + + "FROM integral_table GROUP BY device_id, date_bin(2s, time) " |
| 113 | + + "ORDER BY device_id, window_start", |
| 114 | + expectedHeader, |
| 115 | + retArray, |
| 116 | + DATABASE); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testIntegralSkipsNullAndHandlesInsufficientPoints() { |
| 121 | + String[] expectedHeader = new String[] {"device_id", "area"}; |
| 122 | + String[] retArray = |
| 123 | + new String[] { |
| 124 | + // the null point is skipped, so the remaining points are (0s,1),(2s,2),(3s,4) |
| 125 | + "a,6.0,", |
| 126 | + // fewer than two effective points have no time span, so the area is 0 |
| 127 | + "b,0.0,", |
| 128 | + // negative values are allowed and the result may be negative |
| 129 | + "n,-2.0," |
| 130 | + }; |
| 131 | + tableResultSetEqualTest( |
| 132 | + "SELECT device_id, integral(v, '1s') AS area FROM integral_sparse " |
| 133 | + + "GROUP BY device_id ORDER BY device_id", |
| 134 | + expectedHeader, |
| 135 | + retArray, |
| 136 | + DATABASE); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testIntegralWithColumnRegex() { |
| 141 | + String[] expectedHeader = new String[] {"device_id", "_col1_x", "_col2_y"}; |
| 142 | + String[] retArray = new String[] {"d1,7.5,310.0,", "d2,34.5,610.0,"}; |
| 143 | + // COLUMNS('^[xy]$') is expanded into one integral per matched numeric column. |
| 144 | + tableResultSetEqualTest( |
| 145 | + "SELECT device_id, integral(COLUMNS('^[xy]$'), '1s') FROM integral_table " |
| 146 | + + "GROUP BY device_id ORDER BY device_id", |
| 147 | + expectedHeader, |
| 148 | + retArray, |
| 149 | + DATABASE); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testIntegralInvalidArguments() { |
| 154 | + tableAssertTestFail( |
| 155 | + "SELECT integral(s, '1s') FROM integral_invalid", |
| 156 | + "only support numeric data types", |
| 157 | + DATABASE); |
| 158 | + tableAssertTestFail( |
| 159 | + "SELECT integral(x, 'abc') FROM integral_table", |
| 160 | + "should be a positive duration string", |
| 161 | + DATABASE); |
| 162 | + tableAssertTestFail( |
| 163 | + "SELECT integral(x, '0s') FROM integral_table", |
| 164 | + "should be a positive duration string", |
| 165 | + DATABASE); |
| 166 | + tableAssertTestFail( |
| 167 | + "SELECT integral(x, '1s', device_id) FROM integral_table", |
| 168 | + "must be actual time name", |
| 169 | + DATABASE); |
| 170 | + } |
| 171 | +} |
0 commit comments