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 ;
@@ -47,16 +53,51 @@ protected List<TestData> createExistingDatabaseWithAllDataTypes() throws SQLExce
4753 return testUtils .createExistingDatabaseWithAllDataTypes (getNamespace ());
4854 }
4955
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+
5066 @ Override
5167 protected void dropNonImportableTable (String table ) throws SQLException {
5268 testUtils .dropTable (getNamespace (), table );
5369 }
5470
71+ @ SuppressWarnings ("unused" )
72+ private boolean isOracle () {
73+ return JdbcEnv .isOracle ();
74+ }
75+
76+ @ SuppressWarnings ("unused" )
77+ private boolean isSqlServer () {
78+ return JdbcEnv .isSqlServer ();
79+ }
80+
81+ @ SuppressWarnings ("unused" )
82+ private boolean isDb2 () {
83+ return JdbcEnv .isDb2 ();
84+ }
85+
5586 @ SuppressWarnings ("unused" )
5687 private boolean isSqlite () {
5788 return JdbcEnv .isSqlite ();
5889 }
5990
91+ @ SuppressWarnings ("unused" )
92+ private boolean isColumnTypeConversionToTextNotFullySupported () {
93+ return JdbcEnv .isDb2 () || JdbcEnv .isSqlServer () || JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
94+ }
95+
96+ @ SuppressWarnings ("unused" )
97+ private boolean isWideningColumnTypeConversionNotFullySupported () {
98+ return JdbcEnv .isOracle () || JdbcEnv .isSqlite ();
99+ }
100+
60101 @ Test
61102 @ Override
62103 @ DisabledIf ("isSqlite" )
@@ -71,4 +112,155 @@ public void importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationEx
71112 throws ExecutionException {
72113 super .importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationException ();
73114 }
115+
116+ @ Test
117+ @ Override
118+ @ DisabledIf ("isColumnTypeConversionToTextNotFullySupported" )
119+ public void
120+ alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
121+ throws Exception {
122+ super
123+ .alterColumnType_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ();
124+ }
125+
126+ @ Test
127+ @ EnabledIf ("isSqlServer" )
128+ public void
129+ alterColumnType_SqlServer_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
130+ throws Exception {
131+ // Arrange
132+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
133+ for (TestData testData : testDataList ) {
134+ if (testData .isImportableTable ()) {
135+ admin .importTable (
136+ getNamespace (),
137+ testData .getTableName (),
138+ Collections .emptyMap (),
139+ testData .getOverrideColumnsType ());
140+ }
141+ }
142+
143+ for (TestData testData : testDataList ) {
144+ if (testData .isImportableTable ()) {
145+ // Act
146+ TableMetadata metadata = testData .getTableMetadata ();
147+ for (String column : metadata .getColumnNames ()) {
148+ if (!metadata .getPartitionKeyNames ().contains (column )
149+ && !metadata .getClusteringKeyNames ().contains (column )) {
150+ if (Objects .equals (column , "col16" )) {
151+ // Conversion from IMAGE to VARCHAR(8000) is not supported in SQL Server engine
152+ continue ;
153+ }
154+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
155+ }
156+ }
157+
158+ // Assert
159+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
160+ assertThat (newMetadata ).isNotNull ();
161+ for (String column : metadata .getColumnNames ()) {
162+ if (!metadata .getPartitionKeyNames ().contains (column )
163+ && !metadata .getClusteringKeyNames ().contains (column )) {
164+ if (Objects .equals (column , "col16" )) {
165+ continue ;
166+ }
167+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
168+ }
169+ }
170+ }
171+ }
172+ }
173+
174+ @ Test
175+ @ EnabledIf ("isDb2" )
176+ public void
177+ alterColumnType_Db2_AlterColumnTypeFromEachExistingDataTypeToText_ForImportedTable_ShouldAlterColumnTypesCorrectly ()
178+ throws Exception {
179+ // Arrange
180+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
181+ for (TestData testData : testDataList ) {
182+ if (testData .isImportableTable ()) {
183+ admin .importTable (
184+ getNamespace (),
185+ testData .getTableName (),
186+ Collections .emptyMap (),
187+ testData .getOverrideColumnsType ());
188+ }
189+ }
190+
191+ for (TestData testData : testDataList ) {
192+ if (testData .isImportableTable ()) {
193+ // Act
194+ TableMetadata metadata = testData .getTableMetadata ();
195+ for (String column : metadata .getColumnNames ()) {
196+ if (!metadata .getPartitionKeyNames ().contains (column )
197+ && !metadata .getClusteringKeyNames ().contains (column )) {
198+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
199+ // Conversion from BLOB to TEXT is not supported in Db2 engine
200+ continue ;
201+ }
202+ admin .alterColumnType (getNamespace (), testData .getTableName (), column , DataType .TEXT );
203+ }
204+ }
205+
206+ // Assert
207+ TableMetadata newMetadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
208+ assertThat (newMetadata ).isNotNull ();
209+ for (String column : metadata .getColumnNames ()) {
210+ if (!metadata .getPartitionKeyNames ().contains (column )
211+ && !metadata .getClusteringKeyNames ().contains (column )) {
212+ if (metadata .getColumnDataType (column ).equals (DataType .BLOB )) {
213+ continue ;
214+ }
215+ assertThat (newMetadata .getColumnDataType (column )).isEqualTo (DataType .TEXT );
216+ }
217+ }
218+ }
219+ }
220+ }
221+
222+ @ Test
223+ @ Override
224+ @ DisabledIf ("isWideningColumnTypeConversionNotFullySupported" )
225+ public void alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ()
226+ throws Exception {
227+ super .alterColumnType_WideningConversion_ForImportedTable_ShouldAlterProperly ();
228+ }
229+
230+ @ Test
231+ @ EnabledIf ("isOracle" )
232+ public void alterColumnType_Oracle_WideningConversion_ForImportedTable_ShouldAlterProperly ()
233+ throws Exception {
234+ // Arrange
235+ testDataList .addAll (createExistingDatabaseWithAllDataTypes ());
236+ for (TestData testData : testDataList ) {
237+ if (testData .isImportableTable ()) {
238+ admin .importTable (
239+ getNamespace (),
240+ testData .getTableName (),
241+ Collections .emptyMap (),
242+ testData .getOverrideColumnsType ());
243+ }
244+ }
245+
246+ for (TestData testData : testDataList ) {
247+ if (testData .isImportableTable ()) {
248+ // Act
249+ for (String intCompatibleColumn :
250+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
251+ admin .alterColumnType (
252+ getNamespace (), testData .getTableName (), intCompatibleColumn , DataType .BIGINT );
253+ }
254+ // Conversion from FLOAT TO DOUBLE is not supported in Oracle engine
255+
256+ // Assert
257+ TableMetadata metadata = admin .getTableMetadata (getNamespace (), testData .getTableName ());
258+ assertThat (metadata ).isNotNull ();
259+ for (String intCompatibleColumn :
260+ getIntCompatibleColumnNamesOnExistingDatabase (testData .getTableName ())) {
261+ assertThat (metadata .getColumnDataType (intCompatibleColumn )).isEqualTo (DataType .BIGINT );
262+ }
263+ }
264+ }
265+ }
74266}
0 commit comments