|
| 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.query.recent; |
| 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 IoTDBGroupByAllTableIT { |
| 40 | + protected static final String DATABASE_NAME = "test"; |
| 41 | + |
| 42 | + protected static final String[] createSqls = |
| 43 | + new String[] { |
| 44 | + "CREATE DATABASE " + DATABASE_NAME, |
| 45 | + "USE " + DATABASE_NAME, |
| 46 | + "CREATE TABLE table1(device_id STRING TAG, s1 DOUBLE FIELD, x INT32 FIELD, y INT32 FIELD)", |
| 47 | + "INSERT INTO table1(time, device_id, s1, x, y) VALUES (1, 'd1', 20.0, 1, 10)", |
| 48 | + "INSERT INTO table1(time, device_id, s1, x, y) VALUES (2, 'd1', 30.0, 2, 20)", |
| 49 | + "INSERT INTO table1(time, device_id, s1, x, y) VALUES (3, 'd2', 20.0, 1, 30)", |
| 50 | + "INSERT INTO table1(time, device_id, s1, x, y) VALUES (4, 'd2', 40.0, 3, 40)", |
| 51 | + "FLUSH" |
| 52 | + }; |
| 53 | + |
| 54 | + @BeforeClass |
| 55 | + public static void setUp() throws Exception { |
| 56 | + EnvFactory.getEnv().initClusterEnvironment(); |
| 57 | + prepareTableData(createSqls); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterClass |
| 61 | + public static void tearDown() throws Exception { |
| 62 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testBasicInference() { |
| 67 | + String[] expectedHeader = new String[] {"device_id", "_col1"}; |
| 68 | + String[] retArray = new String[] {"d1,25.0,", "d2,30.0,"}; |
| 69 | + |
| 70 | + tableResultSetEqualTest( |
| 71 | + "SELECT device_id, avg(s1) FROM table1 GROUP BY ALL ORDER BY device_id", |
| 72 | + expectedHeader, |
| 73 | + retArray, |
| 74 | + DATABASE_NAME); |
| 75 | + |
| 76 | + expectedHeader = new String[] {"d", "avg_s1"}; |
| 77 | + retArray = new String[] {"d1,25.0,", "d2,30.0,"}; |
| 78 | + tableResultSetEqualTest( |
| 79 | + "SELECT device_id AS d, avg(s1) AS avg_s1 FROM table1 GROUP BY ALL ORDER BY d", |
| 80 | + expectedHeader, |
| 81 | + retArray, |
| 82 | + DATABASE_NAME); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testExpressionAndConstantInference() { |
| 87 | + String[] expectedHeader = new String[] {"_col0", "_col1"}; |
| 88 | + String[] retArray = new String[] {"false,2,", "true,2,"}; |
| 89 | + |
| 90 | + tableResultSetEqualTest( |
| 91 | + "SELECT s1 > 25, count(*) FROM table1 GROUP BY ALL ORDER BY 1", |
| 92 | + expectedHeader, |
| 93 | + retArray, |
| 94 | + DATABASE_NAME); |
| 95 | + |
| 96 | + retArray = new String[] {"factory_01,4,"}; |
| 97 | + tableResultSetEqualTest( |
| 98 | + "SELECT 'factory_01', count(*) FROM table1 GROUP BY ALL", |
| 99 | + expectedHeader, |
| 100 | + retArray, |
| 101 | + DATABASE_NAME); |
| 102 | + |
| 103 | + retArray = new String[] {"100,4,"}; |
| 104 | + tableResultSetEqualTest( |
| 105 | + "SELECT 100, count(*) FROM table1 GROUP BY ALL", expectedHeader, retArray, DATABASE_NAME); |
| 106 | + |
| 107 | + expectedHeader = new String[] {"device_id", "_col1", "_col2"}; |
| 108 | + retArray = new String[] {"d1,1,25.0,", "d2,1,30.0,"}; |
| 109 | + tableResultSetEqualTest( |
| 110 | + "SELECT device_id, 1, avg(s1) FROM table1 GROUP BY ALL ORDER BY device_id", |
| 111 | + expectedHeader, |
| 112 | + retArray, |
| 113 | + DATABASE_NAME); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAggregateExpressionsAreSkipped() { |
| 118 | + String[] expectedHeader = new String[] {"_col0", "_col1"}; |
| 119 | + String[] retArray = new String[] {"7.0,7.0,"}; |
| 120 | + |
| 121 | + tableResultSetEqualTest( |
| 122 | + "SELECT sum(x), abs(sum(x)) FROM table1 GROUP BY ALL", |
| 123 | + expectedHeader, |
| 124 | + retArray, |
| 125 | + DATABASE_NAME); |
| 126 | + |
| 127 | + expectedHeader = new String[] {"_col0"}; |
| 128 | + retArray = new String[] {"27.5,"}; |
| 129 | + tableResultSetEqualTest( |
| 130 | + "SELECT avg(s1) FROM table1 GROUP BY ALL", expectedHeader, retArray, DATABASE_NAME); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testMixedAggregateExpressionValidation() { |
| 135 | + tableAssertTestFail( |
| 136 | + "SELECT s1 + avg(y) FROM table1 GROUP BY ALL", |
| 137 | + "must be an aggregate expression or appear in GROUP BY clause", |
| 138 | + DATABASE_NAME); |
| 139 | + |
| 140 | + String[] expectedHeader = new String[] {"s1", "_col1"}; |
| 141 | + String[] retArray = new String[] {"20.0,40.0,", "30.0,50.0,", "40.0,80.0,"}; |
| 142 | + |
| 143 | + tableResultSetEqualTest( |
| 144 | + "SELECT s1, s1 + avg(y) FROM table1 GROUP BY ALL ORDER BY s1", |
| 145 | + expectedHeader, |
| 146 | + retArray, |
| 147 | + DATABASE_NAME); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testSelectAllAfterExpansion() { |
| 152 | + String[] expectedHeader = new String[] {"time", "device_id", "s1", "x", "y", "_col5"}; |
| 153 | + String[] retArray = |
| 154 | + new String[] { |
| 155 | + "1970-01-01T00:00:00.001Z,d1,20.0,1,10,1,", |
| 156 | + "1970-01-01T00:00:00.002Z,d1,30.0,2,20,1,", |
| 157 | + "1970-01-01T00:00:00.003Z,d2,20.0,1,30,1,", |
| 158 | + "1970-01-01T00:00:00.004Z,d2,40.0,3,40,1," |
| 159 | + }; |
| 160 | + |
| 161 | + tableResultSetEqualTest( |
| 162 | + "SELECT *, count(*) FROM table1 GROUP BY ALL ORDER BY time", |
| 163 | + expectedHeader, |
| 164 | + retArray, |
| 165 | + DATABASE_NAME); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testWindowFunctionValidation() { |
| 170 | + String[] expectedHeader = new String[] {"device_id", "_col1", "_col2"}; |
| 171 | + String[] retArray = new String[] {"d1,1,25.0,", "d2,1,30.0,"}; |
| 172 | + |
| 173 | + tableResultSetEqualTest( |
| 174 | + "SELECT device_id, count(*) OVER (PARTITION BY device_id), avg(s1) FROM table1 GROUP BY ALL ORDER BY device_id", |
| 175 | + expectedHeader, |
| 176 | + retArray, |
| 177 | + DATABASE_NAME); |
| 178 | + |
| 179 | + tableAssertTestFail( |
| 180 | + "SELECT device_id, rank() OVER (ORDER BY s1), avg(s1) FROM table1 GROUP BY ALL", |
| 181 | + "must be an aggregate expression or appear in GROUP BY clause", |
| 182 | + DATABASE_NAME); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void testIllegalAutoGroupByCombination() { |
| 187 | + tableAssertTestFail( |
| 188 | + "SELECT device_id, count(*) FROM table1 GROUP BY ALL, device_id", |
| 189 | + "GROUP BY ALL cannot be combined with explicit grouping elements", |
| 190 | + DATABASE_NAME); |
| 191 | + |
| 192 | + tableAssertTestFail( |
| 193 | + "SELECT device_id, count(*) FROM table1 GROUP BY ALL, ROLLUP(device_id)", |
| 194 | + "GROUP BY ALL cannot be combined with explicit grouping elements", |
| 195 | + DATABASE_NAME); |
| 196 | + |
| 197 | + tableAssertTestFail( |
| 198 | + "SELECT device_id, count(*) FROM table1 GROUP BY CUBE(device_id), ALL", |
| 199 | + "GROUP BY ALL cannot be combined with explicit grouping elements", |
| 200 | + DATABASE_NAME); |
| 201 | + } |
| 202 | +} |
0 commit comments