11package com .scalar .db .storage .jdbc ;
22
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
35import com .scalar .db .api .DistributedStorageAdminImportTableIntegrationTestBase ;
6+ import com .scalar .db .api .TableMetadata ;
47import com .scalar .db .exception .storage .ExecutionException ;
8+ import com .scalar .db .io .DataType ;
59import java .sql .SQLException ;
10+ import java .util .Collections ;
611import java .util .List ;
12+ import java .util .Objects ;
713import java .util .Properties ;
814import org .junit .jupiter .api .Test ;
915import org .junit .jupiter .api .condition .DisabledIf ;
@@ -39,16 +45,51 @@ protected List<TestData> createExistingDatabaseWithAllDataTypes() throws SQLExce
3945 return testUtils .createExistingDatabaseWithAllDataTypes (getNamespace ());
4046 }
4147
48+ @ Override
49+ protected List <String > getIntCompatibleColumnNamesOnExistingDatabase (String table ) {
50+ return testUtils .getIntCompatibleColumnNamesOnExistingDatabase (table );
51+ }
52+
53+ @ Override
54+ protected List <String > getFloatCompatibleColumnNamesOnExistingDatabase (String table ) {
55+ return testUtils .getFloatCompatibleColumnNamesOnExistingDatabase (table );
56+ }
57+
4258 @ Override
4359 protected void dropNonImportableTable (String table ) throws SQLException {
4460 testUtils .dropTable (getNamespace (), table );
4561 }
4662
63+ @ SuppressWarnings ("unused" )
64+ private boolean isOracle () {
65+ return JdbcEnv .isOracle ();
66+ }
67+
68+ @ SuppressWarnings ("unused" )
69+ private boolean isSqlServer () {
70+ return JdbcEnv .isSqlServer ();
71+ }
72+
73+ @ SuppressWarnings ("unused" )
74+ private boolean isDb2 () {
75+ return JdbcEnv .isDb2 ();
76+ }
77+
4778 @ SuppressWarnings ("unused" )
4879 private boolean isSqlite () {
4980 return JdbcEnv .isSqlite ();
5081 }
5182
83+ @ SuppressWarnings ("unused" )
84+ private boolean isColumnTypeConversionToTextNotFullySupported () {
85+ return JdbcEnv .isDb2 () || JdbcEnv .isSqlServer () || JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
86+ }
87+
88+ @ SuppressWarnings ("unused" )
89+ private boolean isWideningColumnTypeConversionNotFullySupported () {
90+ return JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
91+ }
92+
5293 @ Test
5394 @ Override
5495 @ DisabledIf ("isSqlite" )
@@ -63,4 +104,155 @@ public void importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationEx
63104 throws ExecutionException {
64105 super .importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationException ();
65106 }
107+
108+ @ Test
109+ @ Override
110+ @ DisabledIf ("isColumnTypeConversionToTextNotFullySupported" )
111+ public void
112+ alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
113+ throws Exception {
114+ super
115+ .alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ();
116+ }
117+
118+ @ Test
119+ @ EnabledIf ("isSqlServer" )
120+ public void
121+ alterColumnType_SqlServer_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
122+ throws Exception {
123+ // Arrange
124+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
125+ for (TestData testData : testDataList ) {
126+ if (testData .isImportableTable ()) {
127+ admin .importTable (
128+ getNamespace (),
129+ testData .getTableName (),
130+ Collections .emptyMap (),
131+ testData .getOverrideColumnsType ());
132+ }
133+ }
134+
135+ for (TestData testData : testDataList ) {
136+ if (testData .isImportableTable ()) {
137+ // Act
138+ TableMetadata metadata = testData .getTableMetadata ();
139+ for (String column : metadata .getColumnNames ()) {
140+ if (!metadata .getPartitionKeyNames ().contains (column )
141+ && !metadata .getClusteringKeyNames ().contains (column )) {
142+ if (Objects .equals (column , "col16" )) {
143+ // Conversion from IMAGE to VARCHAR(8000) is not supported in SQL Server engine
144+ continue ;
145+ }
146+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
147+ }
148+ }
149+
150+ // Assert
151+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
152+ assertThat (newMetadata ).isNotNull ();
153+ for (String column : metadata .getColumnNames ()) {
154+ if (!metadata .getPartitionKeyNames ().contains (column )
155+ && !metadata .getClusteringKeyNames ().contains (column )) {
156+ if (Objects .equals (column , "col16" )) {
157+ continue ;
158+ }
159+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
160+ }
161+ }
162+ }
163+ }
164+ }
165+
166+ @ Test
167+ @ EnabledIf ("isDb2" )
168+ public void
169+ alterColumnType_Db2_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
170+ throws Exception {
171+ // Arrange
172+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
173+ for (TestData testData : testDataList ) {
174+ if (testData .isImportableTable ()) {
175+ admin .importTable (
176+ getNamespace (),
177+ testData .getTableName (),
178+ Collections .emptyMap (),
179+ testData .getOverrideColumnsType ());
180+ }
181+ }
182+
183+ for (TestData testData : testDataList ) {
184+ if (testData .isImportableTable ()) {
185+ // Act
186+ TableMetadata metadata = testData .getTableMetadata ();
187+ for (String column : metadata .getColumnNames ()) {
188+ if (!metadata .getPartitionKeyNames ().contains (column )
189+ && !metadata .getClusteringKeyNames ().contains (column )) {
190+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
191+ // Conversion from BLOB to TEXT is not supported in Db2 engine
192+ continue ;
193+ }
194+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
195+ }
196+ }
197+
198+ // Assert
199+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
200+ assertThat (newMetadata ).isNotNull ();
201+ for (String column : metadata .getColumnNames ()) {
202+ if (!metadata .getPartitionKeyNames ().contains (column )
203+ && !metadata .getClusteringKeyNames ().contains (column )) {
204+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
205+ continue ;
206+ }
207+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
208+ }
209+ }
210+ }
211+ }
212+ }
213+
214+ @ Test
215+ @ Override
216+ @ DisabledIf ("isWideningColumnTypeConversionNotFullySupported" )
217+ public void alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ()
218+ throws Exception {
219+ super .alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ();
220+ }
221+
222+ @ Test
223+ @ EnabledIf ("isOracle" )
224+ public void alterColumnType_Oracle_WideningConversion_ForImportedTable_ShouldAlterProperly ()
225+ throws Exception {
226+ // Arrange
227+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
228+ for (TestData testData : testDataList ) {
229+ if (testData .isImportableTable ()) {
230+ admin .importTable (
231+ getNamespace (),
232+ testData .getTableName (),
233+ Collections .emptyMap (),
234+ testData .getOverrideColumnsType ());
235+ }
236+ }
237+
238+ for (TestData testData : testDataList ) {
239+ if (testData .isImportableTable ()) {
240+ // Act
241+ for (String intCompatibleColumn :
242+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
243+ admin .alterColumnType (
244+ getNamespace (), testData .getTableName (), intCompatibleColumn , DataType .BIGINT );
245+ }
246+ // Conversion from FLOAT TO DOUBLE is not supported in Oracle engine
247+
248+ // Assert
249+ TableMetadata metadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
250+ assertThat (metadata ).isNotNull ();
251+ for (String intCompatibleColumn :
252+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
253+ assertThat (metadata .getColumnDataType (intCompatibleColumn )).isEqualTo (DataType .BIGINT );
254+ }
255+ }
256+ }
257+ }
66258}
0 commit comments