11package com .scalar .db .storage .jdbc ;
22
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
35import com .scalar .db .api .DistributedStorageAdminImportTableIntegrationTestBase .TestData ;
6+ import com .scalar .db .api .TableMetadata ;
47import com .scalar .db .exception .storage .ExecutionException ;
8+ import com .scalar .db .io .DataType ;
59import com .scalar .db .transaction .consensuscommit .ConsensusCommitAdminImportTableIntegrationTestBase ;
610import java .sql .SQLException ;
11+ import java .util .Collections ;
712import java .util .List ;
813import java .util .Properties ;
914import org .junit .jupiter .api .Test ;
@@ -48,16 +53,46 @@ protected List<TestData> createExistingDatabaseWithAllDataTypes() throws SQLExce
4853 return testUtils .createExistingDatabaseWithAllDataTypes (getNamespace ());
4954 }
5055
56+ @ Override
57+ protected List <String > getIntCompatibleColumnNamesOnExistingDatabase (String table ) {
58+ return testUtils .getIntCompatibleColumnNamesOnExistingDatabase (table );
59+ }
60+
61+ @ Override
62+ protected List <String > getFloatCompatibleColumnNamesOnExistingDatabase (String table ) {
63+ return testUtils .getFloatCompatibleColumnNamesOnExistingDatabase (table );
64+ }
65+
5166 @ Override
5267 protected void dropNonImportableTable (String table ) throws SQLException {
5368 testUtils .dropTable (getNamespace (), table );
5469 }
5570
71+ @ SuppressWarnings ("unused" )
72+ private boolean isOracle () {
73+ return JdbcEnv .isOracle ();
74+ }
75+
76+ @ SuppressWarnings ("unused" )
77+ private boolean isDb2 () {
78+ return JdbcEnv .isDb2 ();
79+ }
80+
5681 @ SuppressWarnings ("unused" )
5782 private boolean isSqlite () {
5883 return JdbcEnv .isSqlite ();
5984 }
6085
86+ @ SuppressWarnings ("unused" )
87+ private boolean isColumnTypeConversionToTextNotFullySupported () {
88+ return JdbcEnv .isDb2 () || JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
89+ }
90+
91+ @ SuppressWarnings ("unused" )
92+ private boolean isWideningColumnTypeConversionNotFullySupported () {
93+ return JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
94+ }
95+
6196 @ Test
6297 @ Override
6398 @ DisabledIf ("isSqlite" )
@@ -72,4 +107,107 @@ public void importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationEx
72107 throws ExecutionException {
73108 super .importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationException ();
74109 }
110+
111+ @ Test
112+ @ Override
113+ @ DisabledIf ("isColumnTypeConversionToTextNotFullySupported" )
114+ public void
115+ alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
116+ throws Exception {
117+ super
118+ .alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ();
119+ }
120+
121+ @ Test
122+ @ EnabledIf ("isDb2" )
123+ public void
124+ alterColumnType_Db2_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
125+ throws Exception {
126+ // Arrange
127+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
128+ for (TestData testData : testDataList ) {
129+ if (testData .isImportableTable ()) {
130+ admin .importTable (
131+ getNamespace (),
132+ testData .getTableName (),
133+ Collections .emptyMap (),
134+ testData .getOverrideColumnsType ());
135+ }
136+ }
137+
138+ for (TestData testData : testDataList ) {
139+ if (testData .isImportableTable ()) {
140+ // Act
141+ TableMetadata metadata = testData .getTableMetadata ();
142+ for (String column : metadata .getColumnNames ()) {
143+ if (!metadata .getPartitionKeyNames ().contains (column )
144+ && !metadata .getClusteringKeyNames ().contains (column )) {
145+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
146+ // Conversion from BLOB TO TEXT is not supported in Oracle engine
147+ continue ;
148+ }
149+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
150+ }
151+ }
152+
153+ // Assert
154+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
155+ assertThat (newMetadata ).isNotNull ();
156+ for (String column : metadata .getColumnNames ()) {
157+ if (!metadata .getPartitionKeyNames ().contains (column )
158+ && !metadata .getClusteringKeyNames ().contains (column )) {
159+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
160+ continue ;
161+ }
162+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
163+ }
164+ }
165+ }
166+ }
167+ }
168+
169+ @ Test
170+ @ Override
171+ @ DisabledIf ("isWideningColumnTypeConversionNotFullySupported" )
172+ public void alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ()
173+ throws Exception {
174+ super .alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ();
175+ }
176+
177+ @ Test
178+ @ EnabledIf ("isOracle" )
179+ public void alterColumnType_Oracle_WideningConversion_ForImportedTable_ShouldAlterProperly ()
180+ throws Exception {
181+ // Arrange
182+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
183+ for (TestData testData : testDataList ) {
184+ if (testData .isImportableTable ()) {
185+ admin .importTable (
186+ getNamespace (),
187+ testData .getTableName (),
188+ Collections .emptyMap (),
189+ testData .getOverrideColumnsType ());
190+ }
191+ }
192+
193+ for (TestData testData : testDataList ) {
194+ if (testData .isImportableTable ()) {
195+ // Act
196+ for (String intCompatibleColumn :
197+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
198+ admin .alterColumnType (
199+ getNamespace (), testData .getTableName (), intCompatibleColumn , DataType .BIGINT );
200+ }
201+ // Conversion from FLOAT TO DOUBLE is not supported in Oracle engine
202+
203+ // Assert
204+ TableMetadata metadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
205+ assertThat (metadata ).isNotNull ();
206+ for (String intCompatibleColumn :
207+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
208+ assertThat (metadata .getColumnDataType (intCompatibleColumn )).isEqualTo (DataType .BIGINT );
209+ }
210+ }
211+ }
212+ }
75213}
0 commit comments