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,158 @@ 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+ admin .createNamespace (getNamespace (), getCreationOptions ());
125+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
126+ for (TestData testData : testDataList ) {
127+ if (testData .isImportableTable ()) {
128+ admin .importTable (
129+ getNamespace (),
130+ testData .getTableName (),
131+ Collections .emptyMap (),
132+ testData .getOverrideColumnsType ());
133+ }
134+ }
135+
136+ for (TestData testData : testDataList ) {
137+ if (testData .isImportableTable ()) {
138+ // Act
139+ TableMetadata metadata = testData .getTableMetadata ();
140+ for (String column : metadata .getColumnNames ()) {
141+ if (!metadata .getPartitionKeyNames ().contains (column )
142+ && !metadata .getClusteringKeyNames ().contains (column )) {
143+ if (Objects .equals (column , "col16" )) {
144+ // Conversion from IMAGE to VARCHAR(8000) is not supported in SQL Server engine
145+ continue ;
146+ }
147+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
148+ }
149+ }
150+
151+ // Assert
152+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
153+ assertThat (newMetadata ).isNotNull ();
154+ for (String column : metadata .getColumnNames ()) {
155+ if (!metadata .getPartitionKeyNames ().contains (column )
156+ && !metadata .getClusteringKeyNames ().contains (column )) {
157+ if (Objects .equals (column , "col16" )) {
158+ continue ;
159+ }
160+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
161+ }
162+ }
163+ }
164+ }
165+ }
166+
167+ @ Test
168+ @ EnabledIf ("isDb2" )
169+ public void
170+ alterColumnType_Db2_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
171+ throws Exception {
172+ // Arrange
173+ admin .createNamespace (getNamespace (), getCreationOptions ());
174+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
175+ for (TestData testData : testDataList ) {
176+ if (testData .isImportableTable ()) {
177+ admin .importTable (
178+ getNamespace (),
179+ testData .getTableName (),
180+ Collections .emptyMap (),
181+ testData .getOverrideColumnsType ());
182+ }
183+ }
184+
185+ for (TestData testData : testDataList ) {
186+ if (testData .isImportableTable ()) {
187+ // Act
188+ TableMetadata metadata = testData .getTableMetadata ();
189+ for (String column : metadata .getColumnNames ()) {
190+ if (!metadata .getPartitionKeyNames ().contains (column )
191+ && !metadata .getClusteringKeyNames ().contains (column )) {
192+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
193+ // Conversion from BLOB to TEXT is not supported in Db2 engine
194+ continue ;
195+ }
196+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
197+ }
198+ }
199+
200+ // Assert
201+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
202+ assertThat (newMetadata ).isNotNull ();
203+ for (String column : metadata .getColumnNames ()) {
204+ if (!metadata .getPartitionKeyNames ().contains (column )
205+ && !metadata .getClusteringKeyNames ().contains (column )) {
206+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
207+ continue ;
208+ }
209+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
210+ }
211+ }
212+ }
213+ }
214+ }
215+
216+ @ Test
217+ @ Override
218+ @ DisabledIf ("isWideningColumnTypeConversionNotFullySupported" )
219+ public void alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ()
220+ throws Exception {
221+ super .alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ();
222+ }
223+
224+ @ Test
225+ @ EnabledIf ("isOracle" )
226+ public void alterColumnType_Oracle_WideningConversion_ForImportedTable_ShouldAlterProperly ()
227+ throws Exception {
228+ // Arrange
229+ admin .createNamespace (getNamespace (), getCreationOptions ());
230+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
231+ for (TestData testData : testDataList ) {
232+ if (testData .isImportableTable ()) {
233+ admin .importTable (
234+ getNamespace (),
235+ testData .getTableName (),
236+ Collections .emptyMap (),
237+ testData .getOverrideColumnsType ());
238+ }
239+ }
240+
241+ for (TestData testData : testDataList ) {
242+ if (testData .isImportableTable ()) {
243+ // Act
244+ for (String intCompatibleColumn :
245+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
246+ admin .alterColumnType (
247+ getNamespace (), testData .getTableName (), intCompatibleColumn , DataType .BIGINT );
248+ }
249+ // Conversion from FLOAT TO DOUBLE is not supported in Oracle engine
250+
251+ // Assert
252+ TableMetadata metadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
253+ assertThat (metadata ).isNotNull ();
254+ for (String intCompatibleColumn :
255+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
256+ assertThat (metadata .getColumnDataType (intCompatibleColumn )).isEqualTo (DataType .BIGINT );
257+ }
258+ }
259+ }
260+ }
66261}
0 commit comments