Skip to content

Commit ea0c953

Browse files
authored
DM-51713: Fix upload field type conversion (#154)
1 parent c9db488 commit ea0c953

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

src/main/java/org/opencadc/tap/impl/RubinUploadManagerImpl.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
import javax.sql.DataSource;
4040
import org.apache.log4j.Logger;
41-
4241
import com.csvreader.CsvWriter;
4342
import com.google.cloud.storage.HttpMethod;
4443

@@ -234,7 +233,34 @@ private void writeSchemaFile(List<VOTableField> fields, String schemaFilename)
234233

235234
writer.write("\n { ");
236235
writer.write("\"name\": \"" + escapeJsonString(field.getName()) + "\", ");
237-
writer.write("\"type\": \"" + escapeJsonString(field.getDatatype()) + "\"");
236+
String datatype = field.getDatatype().toLowerCase();
237+
String typeString = convertVOTableTypeToMySQL(datatype);
238+
239+
if (field.getArraysize() != null && !field.getArraysize().trim().isEmpty()) {
240+
String arraysize = field.getArraysize().trim();
241+
if ("*".equals(arraysize)) {
242+
if ("char".equals(datatype) || "unicodechar".equals(datatype)) {
243+
typeString = "VARCHAR(255)";
244+
} else if ("bit".equals(datatype)) {
245+
typeString = "BIT(64)";
246+
} else {
247+
log.warn("Array type detected for " + datatype + " with arraysize=*, converting to TEXT");
248+
typeString = "TEXT";
249+
}
250+
} else {
251+
if ("char".equals(datatype) || "unicodechar".equals(datatype)) {
252+
typeString += "(" + arraysize + ")";
253+
} else if ("bit".equals(datatype)) {
254+
typeString += "(" + arraysize + ")";
255+
} else {
256+
log.warn("Array type detected for " + datatype + " with arraysize=" + arraysize
257+
+ ", converting to TEXT");
258+
typeString = "TEXT";
259+
}
260+
}
261+
}
262+
writer.write("\"type\": \"" + escapeJsonString(typeString) + "\"");
263+
238264
writer.write(" }");
239265
}
240266

@@ -243,6 +269,40 @@ private void writeSchemaFile(List<VOTableField> fields, String schemaFilename)
243269
writer.close();
244270
}
245271

272+
/**
273+
* Convert VOTable data types to MySQL equivalents
274+
*/
275+
private String convertVOTableTypeToMySQL(String voTableType) {
276+
switch (voTableType) {
277+
case "boolean":
278+
return "BOOLEAN";
279+
case "unsignedbyte":
280+
return "TINYINT UNSIGNED";
281+
case "short":
282+
return "SMALLINT";
283+
case "int":
284+
return "INT";
285+
case "long":
286+
return "BIGINT";
287+
case "float":
288+
return "FLOAT";
289+
case "double":
290+
return "DOUBLE";
291+
case "char":
292+
case "unicodechar":
293+
return "CHAR";
294+
case "bit":
295+
return "BIT";
296+
case "floatcomplex":
297+
case "doublecomplex":
298+
log.warn("Complex type " + voTableType + " not supported in MySQL, using VARCHAR");
299+
return "VARCHAR(255)";
300+
default:
301+
log.warn("Unknown VOTable type: " + voTableType + ", keeping as-is");
302+
return voTableType.toUpperCase();
303+
}
304+
}
305+
246306
/**
247307
* Helper method to escape special characters in JSON strings
248308
*/
@@ -344,7 +404,7 @@ private void writeDataWithoutHeaders(List<VOTableField> fields, TableData tableD
344404
public String getDatabaseTableName(UploadTable uploadTable) {
345405
StringBuilder sb = new StringBuilder();
346406
String username = getUsername();
347-
if (username != null){
407+
if (username != null) {
348408
sb.append("user_").append(username).append(".");
349409
}
350410
sb.append(uploadTable.tableName);
@@ -359,7 +419,7 @@ public String getDatabaseTableName(UploadTable uploadTable) {
359419
* @return the username of the caller
360420
*/
361421
protected static String getUsername() {
362-
422+
363423
AccessControlContext acContext = AccessController.getContext();
364424
Subject caller = Subject.getSubject(acContext);
365425
AuthMethod authMethod = AuthenticationUtil.getAuthMethod(caller);
@@ -376,5 +436,4 @@ protected static String getUsername() {
376436
return username;
377437
}
378438

379-
380439
}

0 commit comments

Comments
 (0)