Skip to content

Commit

Permalink
fix default ‘’ to null
Browse files Browse the repository at this point in the history
  • Loading branch information
JNSimba committed Aug 5, 2024
1 parent 27bf18e commit c50a7e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ public void fillOriginSchema(String tableName, JsonNode columns) {
}
}

private void buildFieldSchema(Map<String, FieldSchema> filedSchemaMap, JsonNode column) {
@VisibleForTesting
public void buildFieldSchema(Map<String, FieldSchema> filedSchemaMap, JsonNode column) {
String fieldName = column.get("name").asText();
String dorisTypeName = buildDorisTypeName(column);
String defaultValue = handleDefaultValue(extractJsonNode(column, "defaultValueExpression"));
Expand All @@ -315,7 +316,7 @@ public String buildDorisTypeName(JsonNode column) {
}

private String handleDefaultValue(String defaultValue) {
if (StringUtils.isNullOrWhitespaceOnly(defaultValue)) {
if (defaultValue == null) {
return null;
}
if (defaultValue.equals("1970-01-01 00:00:00")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,44 @@ private Map<String, FieldSchema> buildDatetimeFieldSchemaMap() {
return filedSchemaMap;
}

@Test
public void buildFieldSchemaTest() {
Map<String, FieldSchema> result = new HashMap<>();
String columnInfo =
"{\"name\":\"order_ts\",\"jdbcType\":2014,\"typeName\":\"TIMESTAMP\",\"typeExpression\":\"TIMESTAMP\",\"charsetName\":null,\"position\":4,\"optional\":true,\"autoIncremented\":false,\"generated\":false,\"comment\":\"中文注释\",\"hasDefaultValue\":true,\"defaultValueExpression\":\"1970-01-01 00:00:00\",\"enumValues\":[]}\n";
schemaChange.setSourceConnector("mysql");
JsonNode columns = null;
try {
columns = objectMapper.readTree(columnInfo);
} catch (IOException e) {
e.printStackTrace();
}
schemaChange.buildFieldSchema(result, columns);
Assert.assertTrue(result.containsKey("order_ts"));
FieldSchema fieldSchema = result.get("order_ts");
Assert.assertEquals(fieldSchema.getName().toLowerCase(), "order_ts");
Assert.assertEquals(fieldSchema.getTypeString().toLowerCase(), "datetimev2(0)");
Assert.assertEquals(fieldSchema.getDefaultValue().toLowerCase(), "current_timestamp");
Assert.assertEquals(fieldSchema.getComment(), "中文注释");

columnInfo =
"{\"name\":\"other_no\",\"jdbcType\":12,\"typeName\":\"VARCHAR\",\"typeExpression\":\"VARCHAR\",\"charsetName\":\"utf8mb4\",\"length\":50,\"position\":23,\"optional\":true,\"autoIncremented\":false,\"generated\":false,\"comment\":\"comment\",\"hasDefaultValue\":true,\"defaultValueExpression\":\"\",\"enumValues\":[]}\n";
schemaChange.setSourceConnector("mysql");
columns = null;
try {
columns = objectMapper.readTree(columnInfo);
} catch (IOException e) {
e.printStackTrace();
}
schemaChange.buildFieldSchema(result, columns);
Assert.assertTrue(result.containsKey("other_no"));
fieldSchema = result.get("other_no");
Assert.assertEquals(fieldSchema.getName().toLowerCase(), "other_no");
Assert.assertEquals(fieldSchema.getTypeString().toLowerCase(), "varchar(150)");
Assert.assertEquals(fieldSchema.getDefaultValue().toLowerCase(), "");
Assert.assertEquals(fieldSchema.getComment(), "comment");
}

@After
public void after() {
mockRestService.close();
Expand Down

0 comments on commit c50a7e0

Please sign in to comment.