|
1 | 1 | package com.scalar.db.storage.jdbc; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
3 | 4 | import static org.assertj.core.api.Assertions.assertThatCode; |
| 5 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 6 | +import static org.assertj.core.api.Assertions.catchThrowable; |
4 | 7 |
|
| 8 | +import com.scalar.db.api.DistributedStorage; |
5 | 9 | import com.scalar.db.api.DistributedStorageAdminIntegrationTestBase; |
| 10 | +import com.scalar.db.api.Put; |
| 11 | +import com.scalar.db.api.PutBuilder; |
| 12 | +import com.scalar.db.api.Result; |
| 13 | +import com.scalar.db.api.Scan; |
| 14 | +import com.scalar.db.api.Scanner; |
6 | 15 | import com.scalar.db.api.TableMetadata; |
7 | 16 | import com.scalar.db.config.DatabaseConfig; |
8 | 17 | import com.scalar.db.exception.storage.ExecutionException; |
9 | 18 | import com.scalar.db.io.DataType; |
| 19 | +import com.scalar.db.io.Key; |
10 | 20 | import com.scalar.db.util.AdminTestUtils; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.time.Instant; |
| 24 | +import java.time.LocalDate; |
| 25 | +import java.time.LocalDateTime; |
| 26 | +import java.time.LocalTime; |
| 27 | +import java.time.ZoneId; |
| 28 | +import java.time.ZoneOffset; |
| 29 | +import java.util.List; |
11 | 30 | import java.util.Map; |
12 | 31 | import java.util.Properties; |
13 | 32 | import org.junit.jupiter.api.Test; |
@@ -37,11 +56,28 @@ protected boolean isCreateIndexOnTextColumnEnabled() { |
37 | 56 | return !JdbcTestUtils.isDb2(rdbEngine); |
38 | 57 | } |
39 | 58 |
|
| 59 | + @SuppressWarnings("unused") |
| 60 | + private boolean isOracle() { |
| 61 | + return JdbcEnv.isOracle(); |
| 62 | + } |
| 63 | + |
40 | 64 | @SuppressWarnings("unused") |
41 | 65 | private boolean isDb2() { |
42 | 66 | return JdbcEnv.isDb2(); |
43 | 67 | } |
44 | 68 |
|
| 69 | + @SuppressWarnings("unused") |
| 70 | + private boolean isSqlite() { |
| 71 | + return JdbcEnv.isSqlite(); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("unused") |
| 75 | + private boolean isColumnTypeConversionToTextNotFullySupported() { |
| 76 | + return JdbcTestUtils.isDb2(rdbEngine) |
| 77 | + || JdbcTestUtils.isOracle(rdbEngine) |
| 78 | + || JdbcTestUtils.isSqlite(rdbEngine); |
| 79 | + } |
| 80 | + |
45 | 81 | @Test |
46 | 82 | @Override |
47 | 83 | @DisabledIf("isDb2") |
@@ -97,6 +133,228 @@ public void renameColumn_Db2_ForPrimaryOrIndexKeyColumn_ShouldThrowUnsupportedOp |
97 | 133 | } |
98 | 134 | } |
99 | 135 |
|
| 136 | + @Test |
| 137 | + @Override |
| 138 | + @DisabledIf("isColumnTypeConversionToTextNotFullySupported") |
| 139 | + public void |
| 140 | + alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ShouldAlterColumnTypesCorrectly() |
| 141 | + throws ExecutionException, IOException { |
| 142 | + super |
| 143 | + .alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ShouldAlterColumnTypesCorrectly(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + @EnabledIf("isOracle") |
| 148 | + public void |
| 149 | + alterColumnType_Oracle_AlterColumnTypeFromEachExistingDataTypeToText_ShouldThrowUnsupportedOperationException() |
| 150 | + throws ExecutionException { |
| 151 | + try (DistributedStorage storage = storageFactory.getStorage()) { |
| 152 | + // Arrange |
| 153 | + Map<String, String> options = getCreationOptions(); |
| 154 | + TableMetadata.Builder currentTableMetadataBuilder = |
| 155 | + TableMetadata.newBuilder() |
| 156 | + .addColumn(getColumnName1(), DataType.INT) |
| 157 | + .addColumn(getColumnName2(), DataType.INT) |
| 158 | + .addColumn(getColumnName3(), DataType.INT) |
| 159 | + .addColumn(getColumnName4(), DataType.BIGINT) |
| 160 | + .addColumn(getColumnName5(), DataType.FLOAT) |
| 161 | + .addColumn(getColumnName6(), DataType.DOUBLE) |
| 162 | + .addColumn(getColumnName7(), DataType.TEXT) |
| 163 | + .addColumn(getColumnName8(), DataType.BLOB) |
| 164 | + .addColumn(getColumnName9(), DataType.DATE) |
| 165 | + .addColumn(getColumnName10(), DataType.TIME) |
| 166 | + .addPartitionKey(getColumnName1()) |
| 167 | + .addClusteringKey(getColumnName2(), Scan.Ordering.Order.ASC); |
| 168 | + if (isTimestampTypeSupported()) { |
| 169 | + currentTableMetadataBuilder |
| 170 | + .addColumn(getColumnName11(), DataType.TIMESTAMP) |
| 171 | + .addColumn(getColumnName12(), DataType.TIMESTAMPTZ); |
| 172 | + } |
| 173 | + TableMetadata currentTableMetadata = currentTableMetadataBuilder.build(); |
| 174 | + admin.createTable(getNamespace1(), getTable4(), currentTableMetadata, options); |
| 175 | + PutBuilder.Buildable put = |
| 176 | + Put.newBuilder() |
| 177 | + .namespace(getNamespace1()) |
| 178 | + .table(getTable4()) |
| 179 | + .partitionKey(Key.ofInt(getColumnName1(), 1)) |
| 180 | + .clusteringKey(Key.ofInt(getColumnName2(), 2)) |
| 181 | + .intValue(getColumnName3(), 1) |
| 182 | + .bigIntValue(getColumnName4(), 2L) |
| 183 | + .floatValue(getColumnName5(), 3.0f) |
| 184 | + .doubleValue(getColumnName6(), 4.0d) |
| 185 | + .textValue(getColumnName7(), "5") |
| 186 | + .blobValue(getColumnName8(), "6".getBytes(StandardCharsets.UTF_8)) |
| 187 | + .dateValue(getColumnName9(), LocalDate.now(ZoneId.of("UTC"))) |
| 188 | + .timeValue(getColumnName10(), LocalTime.now(ZoneId.of("UTC"))); |
| 189 | + if (isTimestampTypeSupported()) { |
| 190 | + put.timestampValue(getColumnName11(), LocalDateTime.now(ZoneOffset.UTC)); |
| 191 | + put.timestampTZValue(getColumnName12(), Instant.now()); |
| 192 | + } |
| 193 | + storage.put(put.build()); |
| 194 | + storage.close(); |
| 195 | + |
| 196 | + // Act Assert |
| 197 | + assertThatThrownBy( |
| 198 | + () -> |
| 199 | + admin.alterColumnType( |
| 200 | + getNamespace1(), getTable4(), getColumnName3(), DataType.TEXT)) |
| 201 | + .isInstanceOf(UnsupportedOperationException.class); |
| 202 | + assertThatThrownBy( |
| 203 | + () -> |
| 204 | + admin.alterColumnType( |
| 205 | + getNamespace1(), getTable4(), getColumnName4(), DataType.TEXT)) |
| 206 | + .isInstanceOf(UnsupportedOperationException.class); |
| 207 | + assertThatThrownBy( |
| 208 | + () -> |
| 209 | + admin.alterColumnType( |
| 210 | + getNamespace1(), getTable4(), getColumnName5(), DataType.TEXT)) |
| 211 | + .isInstanceOf(UnsupportedOperationException.class); |
| 212 | + assertThatThrownBy( |
| 213 | + () -> |
| 214 | + admin.alterColumnType( |
| 215 | + getNamespace1(), getTable4(), getColumnName6(), DataType.TEXT)) |
| 216 | + .isInstanceOf(UnsupportedOperationException.class); |
| 217 | + admin.alterColumnType(getNamespace1(), getTable4(), getColumnName7(), DataType.TEXT); |
| 218 | + assertThatThrownBy( |
| 219 | + () -> |
| 220 | + admin.alterColumnType( |
| 221 | + getNamespace1(), getTable4(), getColumnName8(), DataType.TEXT)) |
| 222 | + .isInstanceOf(UnsupportedOperationException.class); |
| 223 | + assertThatThrownBy( |
| 224 | + () -> |
| 225 | + admin.alterColumnType( |
| 226 | + getNamespace1(), getTable4(), getColumnName9(), DataType.TEXT)) |
| 227 | + .isInstanceOf(UnsupportedOperationException.class); |
| 228 | + assertThatThrownBy( |
| 229 | + () -> |
| 230 | + admin.alterColumnType( |
| 231 | + getNamespace1(), getTable4(), getColumnName10(), DataType.TEXT)) |
| 232 | + .isInstanceOf(UnsupportedOperationException.class); |
| 233 | + if (isTimestampTypeSupported()) { |
| 234 | + assertThatThrownBy( |
| 235 | + () -> |
| 236 | + admin.alterColumnType( |
| 237 | + getNamespace1(), getTable4(), getColumnName11(), DataType.TEXT)) |
| 238 | + .isInstanceOf(UnsupportedOperationException.class); |
| 239 | + assertThatThrownBy( |
| 240 | + () -> |
| 241 | + admin.alterColumnType( |
| 242 | + getNamespace1(), getTable4(), getColumnName12(), DataType.TEXT)) |
| 243 | + .isInstanceOf(UnsupportedOperationException.class); |
| 244 | + } |
| 245 | + } finally { |
| 246 | + admin.dropTable(getNamespace1(), getTable4(), true); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + @Override |
| 252 | + @DisabledIf("isOracle") |
| 253 | + public void alterColumnType_WideningConversion_ShouldAlterColumnTypesCorrectly() |
| 254 | + throws ExecutionException, IOException { |
| 255 | + super.alterColumnType_WideningConversion_ShouldAlterColumnTypesCorrectly(); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + @EnabledIf("isOracle") |
| 260 | + public void alterColumnType_Oracle_WideningConversion_ShouldAlterColumnTypesCorrectly() |
| 261 | + throws ExecutionException, IOException { |
| 262 | + try { |
| 263 | + // Arrange |
| 264 | + Map<String, String> options = getCreationOptions(); |
| 265 | + TableMetadata.Builder currentTableMetadataBuilder = |
| 266 | + TableMetadata.newBuilder() |
| 267 | + .addColumn(getColumnName1(), DataType.INT) |
| 268 | + .addColumn(getColumnName2(), DataType.INT) |
| 269 | + .addColumn(getColumnName3(), DataType.INT) |
| 270 | + .addColumn(getColumnName4(), DataType.FLOAT) |
| 271 | + .addPartitionKey(getColumnName1()) |
| 272 | + .addClusteringKey(getColumnName2(), Scan.Ordering.Order.ASC); |
| 273 | + TableMetadata currentTableMetadata = currentTableMetadataBuilder.build(); |
| 274 | + admin.createTable(getNamespace1(), getTable4(), currentTableMetadata, options); |
| 275 | + DistributedStorage storage = storageFactory.getStorage(); |
| 276 | + int expectedColumn3Value = 1; |
| 277 | + float expectedColumn4Value = 4.0f; |
| 278 | + |
| 279 | + PutBuilder.Buildable put = |
| 280 | + Put.newBuilder() |
| 281 | + .namespace(getNamespace1()) |
| 282 | + .table(getTable4()) |
| 283 | + .partitionKey(Key.ofInt(getColumnName1(), 1)) |
| 284 | + .clusteringKey(Key.ofInt(getColumnName2(), 2)) |
| 285 | + .intValue(getColumnName3(), expectedColumn3Value) |
| 286 | + .floatValue(getColumnName4(), expectedColumn4Value); |
| 287 | + storage.put(put.build()); |
| 288 | + storage.close(); |
| 289 | + |
| 290 | + // Act |
| 291 | + admin.alterColumnType(getNamespace1(), getTable4(), getColumnName3(), DataType.BIGINT); |
| 292 | + Throwable exception = |
| 293 | + catchThrowable( |
| 294 | + () -> |
| 295 | + admin.alterColumnType( |
| 296 | + getNamespace1(), getTable4(), getColumnName4(), DataType.DOUBLE)); |
| 297 | + |
| 298 | + // Assert |
| 299 | + assertThat(exception).isInstanceOf(UnsupportedOperationException.class); |
| 300 | + TableMetadata.Builder expectedTableMetadataBuilder = |
| 301 | + TableMetadata.newBuilder() |
| 302 | + .addColumn(getColumnName1(), DataType.INT) |
| 303 | + .addColumn(getColumnName2(), DataType.INT) |
| 304 | + .addColumn(getColumnName3(), DataType.BIGINT) |
| 305 | + .addColumn(getColumnName4(), DataType.FLOAT) |
| 306 | + .addPartitionKey(getColumnName1()) |
| 307 | + .addClusteringKey(getColumnName2(), Scan.Ordering.Order.ASC); |
| 308 | + TableMetadata expectedTableMetadata = expectedTableMetadataBuilder.build(); |
| 309 | + assertThat(admin.getTableMetadata(getNamespace1(), getTable4())) |
| 310 | + .isEqualTo(expectedTableMetadata); |
| 311 | + storage = storageFactory.getStorage(); |
| 312 | + Scan scan = |
| 313 | + Scan.newBuilder() |
| 314 | + .namespace(getNamespace1()) |
| 315 | + .table(getTable4()) |
| 316 | + .partitionKey(Key.ofInt(getColumnName1(), 1)) |
| 317 | + .build(); |
| 318 | + try (Scanner scanner = storage.scan(scan)) { |
| 319 | + List<Result> results = scanner.all(); |
| 320 | + assertThat(results).hasSize(1); |
| 321 | + Result result = results.get(0); |
| 322 | + assertThat(result.getBigInt(getColumnName3())).isEqualTo(expectedColumn3Value); |
| 323 | + } |
| 324 | + storage.close(); |
| 325 | + } finally { |
| 326 | + admin.dropTable(getNamespace1(), getTable4(), true); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + @EnabledIf("isSqlite") |
| 332 | + public void alterColumnType_Sqlite_AlterColumnType_ShouldThrowUnsupportedOperationException() |
| 333 | + throws ExecutionException { |
| 334 | + try { |
| 335 | + // Arrange |
| 336 | + Map<String, String> options = getCreationOptions(); |
| 337 | + TableMetadata.Builder currentTableMetadataBuilder = |
| 338 | + TableMetadata.newBuilder() |
| 339 | + .addColumn(getColumnName1(), DataType.INT) |
| 340 | + .addColumn(getColumnName2(), DataType.INT) |
| 341 | + .addColumn(getColumnName3(), DataType.INT) |
| 342 | + .addPartitionKey(getColumnName1()) |
| 343 | + .addClusteringKey(getColumnName2(), Scan.Ordering.Order.ASC); |
| 344 | + TableMetadata currentTableMetadata = currentTableMetadataBuilder.build(); |
| 345 | + admin.createTable(getNamespace1(), getTable4(), currentTableMetadata, options); |
| 346 | + |
| 347 | + // Act Assert |
| 348 | + assertThatThrownBy( |
| 349 | + () -> |
| 350 | + admin.alterColumnType( |
| 351 | + getNamespace1(), getTable4(), getColumnName3(), DataType.TEXT)) |
| 352 | + .isInstanceOf(UnsupportedOperationException.class); |
| 353 | + } finally { |
| 354 | + admin.dropTable(getNamespace1(), getTable4(), true); |
| 355 | + } |
| 356 | + } |
| 357 | + |
100 | 358 | @Override |
101 | 359 | protected boolean isIndexOnBlobColumnSupported() { |
102 | 360 | return !JdbcTestUtils.isDb2(rdbEngine); |
|
0 commit comments