|
28 | 28 |
|
29 | 29 | import java.lang.reflect.InvocationTargetException; |
30 | 30 | import java.lang.reflect.Method; |
| 31 | +import java.util.Optional; |
31 | 32 |
|
32 | 33 | /** An enriched {@code StarRocksCatalog} with more schema evolution abilities. */ |
33 | 34 | public class StarRocksEnrichedCatalog extends StarRocksCatalog { |
@@ -113,7 +114,13 @@ public void alterColumnType(String databaseName, String tableName, StarRocksColu |
113 | 114 | "column name cannot be null or empty."); |
114 | 115 | String alterSql = |
115 | 116 | buildAlterColumnTypeSql( |
116 | | - databaseName, tableName, column.getColumnName(), column.getDataType()); |
| 117 | + databaseName, |
| 118 | + tableName, |
| 119 | + column.getColumnName(), |
| 120 | + getFullColumnType( |
| 121 | + column.getDataType(), |
| 122 | + column.getColumnSize(), |
| 123 | + column.getDecimalDigits())); |
117 | 124 | try { |
118 | 125 | long startTimeMillis = System.currentTimeMillis(); |
119 | 126 | executeUpdateStatement(alterSql); |
@@ -181,4 +188,24 @@ private void checkTableArgument(String databaseName, String tableName) { |
181 | 188 | !StringUtils.isNullOrWhitespaceOnly(tableName), |
182 | 189 | "Table name cannot be null or empty."); |
183 | 190 | } |
| 191 | + |
| 192 | + private String getFullColumnType( |
| 193 | + String type, Optional<Integer> columnSize, Optional<Integer> decimalDigits) { |
| 194 | + String dataType = type.toUpperCase(); |
| 195 | + switch (dataType) { |
| 196 | + case "DECIMAL": |
| 197 | + Preconditions.checkArgument( |
| 198 | + columnSize.isPresent(), "DECIMAL type must have column size"); |
| 199 | + Preconditions.checkArgument( |
| 200 | + decimalDigits.isPresent(), "DECIMAL type must have decimal digits"); |
| 201 | + return String.format("DECIMAL(%d, %s)", columnSize.get(), decimalDigits.get()); |
| 202 | + case "CHAR": |
| 203 | + case "VARCHAR": |
| 204 | + Preconditions.checkArgument( |
| 205 | + columnSize.isPresent(), type + " type must have column size"); |
| 206 | + return String.format("%s(%d)", dataType, columnSize.get()); |
| 207 | + default: |
| 208 | + return dataType; |
| 209 | + } |
| 210 | + } |
184 | 211 | } |
0 commit comments