|
33 | 33 | import org.apache.iotdb.rpc.IoTDBConnectionException; |
34 | 34 | import org.apache.iotdb.rpc.StatementExecutionException; |
35 | 35 |
|
| 36 | +import org.apache.tsfile.enums.ColumnCategory; |
| 37 | +import org.apache.tsfile.enums.TSDataType; |
36 | 38 | import org.apache.tsfile.read.common.RowRecord; |
37 | 39 | import org.apache.tsfile.read.common.TimeRange; |
| 40 | +import org.apache.tsfile.write.record.Tablet; |
38 | 41 | import org.awaitility.Awaitility; |
39 | 42 | import org.junit.After; |
40 | 43 | import org.junit.AfterClass; |
|
60 | 63 | import java.sql.SQLException; |
61 | 64 | import java.sql.Statement; |
62 | 65 | import java.util.ArrayList; |
| 66 | +import java.util.Collections; |
63 | 67 | import java.util.List; |
64 | 68 | import java.util.Locale; |
65 | 69 | import java.util.Random; |
|
74 | 78 | import java.util.stream.Collectors; |
75 | 79 | import java.util.stream.Stream; |
76 | 80 |
|
| 81 | +import static org.apache.iotdb.relational.it.session.IoTDBSessionRelationalIT.genValue; |
77 | 82 | import static org.junit.Assert.assertEquals; |
78 | 83 | import static org.junit.Assert.assertFalse; |
79 | 84 | import static org.junit.Assert.assertTrue; |
@@ -2268,6 +2273,110 @@ public void testMultiDeviceCompletelyDeleteTable() throws SQLException { |
2268 | 2273 | cleanData(testNum); |
2269 | 2274 | } |
2270 | 2275 |
|
| 2276 | + @Test |
| 2277 | + public void testDropAndAlter() throws IoTDBConnectionException, StatementExecutionException { |
| 2278 | + try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) { |
| 2279 | + session.executeNonQueryStatement("CREATE TABLE IF NOT EXISTS drop_and_alter (s1 int32)"); |
| 2280 | + |
| 2281 | + // time=1 and time=2 are INT32 and deleted by drop column |
| 2282 | + Tablet tablet = |
| 2283 | + new Tablet( |
| 2284 | + "drop_and_alter", |
| 2285 | + Collections.singletonList("s1"), |
| 2286 | + Collections.singletonList(TSDataType.INT32), |
| 2287 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2288 | + tablet.addTimestamp(0, 1); |
| 2289 | + tablet.addValue("s1", 0, genValue(TSDataType.INT32, 1)); |
| 2290 | + session.insert(tablet); |
| 2291 | + tablet.reset(); |
| 2292 | + |
| 2293 | + session.executeNonQueryStatement("FLUSH"); |
| 2294 | + |
| 2295 | + tablet = |
| 2296 | + new Tablet( |
| 2297 | + "drop_and_alter", |
| 2298 | + Collections.singletonList("s1"), |
| 2299 | + Collections.singletonList(TSDataType.INT32), |
| 2300 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2301 | + tablet.addTimestamp(0, 2); |
| 2302 | + tablet.addValue("s1", 0, genValue(TSDataType.INT32, 2)); |
| 2303 | + session.insert(tablet); |
| 2304 | + tablet.reset(); |
| 2305 | + |
| 2306 | + session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1"); |
| 2307 | + |
| 2308 | + // time=3 and time=4 are STRING |
| 2309 | + tablet = |
| 2310 | + new Tablet( |
| 2311 | + "drop_and_alter", |
| 2312 | + Collections.singletonList("s1"), |
| 2313 | + Collections.singletonList(TSDataType.STRING), |
| 2314 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2315 | + tablet.addTimestamp(0, 3); |
| 2316 | + tablet.addValue("s1", 0, genValue(TSDataType.STRING, 3)); |
| 2317 | + session.insert(tablet); |
| 2318 | + tablet.reset(); |
| 2319 | + |
| 2320 | + session.executeNonQueryStatement("FLUSH"); |
| 2321 | + |
| 2322 | + tablet = |
| 2323 | + new Tablet( |
| 2324 | + "drop_and_alter", |
| 2325 | + Collections.singletonList("s1"), |
| 2326 | + Collections.singletonList(TSDataType.STRING), |
| 2327 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2328 | + tablet.addTimestamp(0, 4); |
| 2329 | + tablet.addValue("s1", 0, genValue(TSDataType.STRING, 4)); |
| 2330 | + session.insert(tablet); |
| 2331 | + tablet.reset(); |
| 2332 | + |
| 2333 | + session.executeNonQueryStatement("ALTER TABLE drop_and_alter DROP COLUMN s1"); |
| 2334 | + session.executeNonQueryStatement("ALTER TABLE drop_and_alter ADD COLUMN s1 TEXT"); |
| 2335 | + |
| 2336 | + // time=5 and time=6 are TEXT |
| 2337 | + tablet = |
| 2338 | + new Tablet( |
| 2339 | + "drop_and_alter", |
| 2340 | + Collections.singletonList("s1"), |
| 2341 | + Collections.singletonList(TSDataType.TEXT), |
| 2342 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2343 | + tablet.addTimestamp(0, 5); |
| 2344 | + tablet.addValue("s1", 0, genValue(TSDataType.STRING, 5)); |
| 2345 | + session.insert(tablet); |
| 2346 | + tablet.reset(); |
| 2347 | + |
| 2348 | + session.executeNonQueryStatement("FLUSH"); |
| 2349 | + |
| 2350 | + tablet = |
| 2351 | + new Tablet( |
| 2352 | + "drop_and_alter", |
| 2353 | + Collections.singletonList("s1"), |
| 2354 | + Collections.singletonList(TSDataType.TEXT), |
| 2355 | + Collections.singletonList(ColumnCategory.FIELD)); |
| 2356 | + tablet.addTimestamp(0, 6); |
| 2357 | + tablet.addValue("s1", 0, genValue(TSDataType.STRING, 6)); |
| 2358 | + session.insert(tablet); |
| 2359 | + tablet.reset(); |
| 2360 | + |
| 2361 | + SessionDataSet dataSet = |
| 2362 | + session.executeQueryStatement("select * from drop_and_alter order by time"); |
| 2363 | + // s1 is dropped but the time should remain |
| 2364 | + RowRecord rec; |
| 2365 | + int cnt = 0; |
| 2366 | + for (int i = 1; i < 7; i++) { |
| 2367 | + rec = dataSet.next(); |
| 2368 | + assertEquals(i, rec.getFields().get(0).getLongV()); |
| 2369 | + cnt++; |
| 2370 | + } |
| 2371 | + Assert.assertEquals(6, cnt); |
| 2372 | + assertFalse(dataSet.hasNext()); |
| 2373 | + } finally { |
| 2374 | + try (ITableSession session = EnvFactory.getEnv().getTableSessionConnectionWithDB("test")) { |
| 2375 | + session.executeNonQueryStatement("DROP TABLE IF EXISTS drop_and_alter"); |
| 2376 | + } |
| 2377 | + } |
| 2378 | + } |
| 2379 | + |
2271 | 2380 | private static void prepareDatabase() { |
2272 | 2381 | try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT); |
2273 | 2382 | Statement statement = connection.createStatement()) { |
|
0 commit comments