JNSimba commented on code in PR #422: URL: https://github.com/apache/doris-flink-connector/pull/422#discussion_r1680368588
########## flink-doris-connector/src/main/java/org/apache/doris/flink/sink/schema/SQLParserSchemaManager.java: ########## @@ -161,49 +158,56 @@ private String processRenameColumnOperation( } private String extractDefaultValue(List<String> columnSpecs) { - String defaultValue = null; - if (columnSpecs.contains("default")) { - int defaultIndex = columnSpecs.indexOf("default"); - defaultValue = extractColumnSpecsValue(columnSpecs, defaultIndex); - } else if (columnSpecs.contains("DEFAULT")) { - int defaultIndex = columnSpecs.indexOf("DEFAULT"); - defaultValue = extractColumnSpecsValue(columnSpecs, defaultIndex); - } - return defaultValue; + return extractAdjacentString(columnSpecs, DEFAULT); } - private String extractColumnSpecsValue(List<String> columnSpecs, int keyIndex) { + private String extractAdjacentString(List<String> columnSpecs, String key) { int columnSpecsSize = columnSpecs.size(); - int valueIndex = keyIndex + 1; - if (valueIndex < columnSpecsSize - && !COLUMN_SPECS_KEYS.contains(columnSpecs.get(valueIndex))) { - return removeQuotes(columnSpecs.get(valueIndex)); + for (int i = 0; i < columnSpecsSize; i++) { + String columnSpec = columnSpecs.get(i); + if (key.equalsIgnoreCase(columnSpec) && i < columnSpecsSize - 1) { + String adjacentString = columnSpecs.get(i + 1); + if (!adjacentString.equalsIgnoreCase(DEFAULT) + && !adjacentString.equalsIgnoreCase(COMMENT)) { + return removeQuotes(adjacentString); + } + LOG.warn( + "Failed to extract adjacent string value. columnSpecs={}, key={}", + String.join(",", columnSpecs), + key); + } Review Comment: add ut ########## flink-doris-connector/src/main/java/org/apache/doris/flink/sink/schema/SQLParserSchemaManager.java: ########## @@ -161,49 +158,56 @@ private String processRenameColumnOperation( } private String extractDefaultValue(List<String> columnSpecs) { - String defaultValue = null; - if (columnSpecs.contains("default")) { - int defaultIndex = columnSpecs.indexOf("default"); - defaultValue = extractColumnSpecsValue(columnSpecs, defaultIndex); - } else if (columnSpecs.contains("DEFAULT")) { - int defaultIndex = columnSpecs.indexOf("DEFAULT"); - defaultValue = extractColumnSpecsValue(columnSpecs, defaultIndex); - } - return defaultValue; + return extractAdjacentString(columnSpecs, DEFAULT); } - private String extractColumnSpecsValue(List<String> columnSpecs, int keyIndex) { + private String extractAdjacentString(List<String> columnSpecs, String key) { int columnSpecsSize = columnSpecs.size(); - int valueIndex = keyIndex + 1; - if (valueIndex < columnSpecsSize - && !COLUMN_SPECS_KEYS.contains(columnSpecs.get(valueIndex))) { - return removeQuotes(columnSpecs.get(valueIndex)); + for (int i = 0; i < columnSpecsSize; i++) { + String columnSpec = columnSpecs.get(i); + if (key.equalsIgnoreCase(columnSpec) && i < columnSpecsSize - 1) { + String adjacentString = columnSpecs.get(i + 1); + if (!adjacentString.equalsIgnoreCase(DEFAULT) + && !adjacentString.equalsIgnoreCase(COMMENT)) { + return removeQuotes(adjacentString); + } + LOG.warn( + "Failed to extract adjacent string value. columnSpecs={}, key={}", + String.join(",", columnSpecs), + key); + } } - LOG.warn( - "Failed to extract column specs value. columnSpecs={}", - String.join(",", columnSpecs)); return null; } private String extractComment(List<String> columnSpecs) { - String comment = null; - if (columnSpecs.contains("comment")) { - int commentIndex = columnSpecs.indexOf("comment"); - comment = extractColumnSpecsValue(columnSpecs, commentIndex); - } - if (columnSpecs.contains("COMMENT")) { - int commentIndex = columnSpecs.indexOf("COMMENT"); - comment = extractColumnSpecsValue(columnSpecs, commentIndex); - } - return comment; + return extractAdjacentString(columnSpecs, COMMENT); } private String removeQuotes(String content) { - if (content.startsWith("'") && content.endsWith("'") && content.length() > 1) { - return content.substring(1, content.length() - 1); - } else if (content.startsWith("\"") && content.endsWith("\"") && content.length() > 1) { - return content.substring(1, content.length() - 1); - } + content = removeContinuousChar(content, '\''); + content = removeContinuousChar(content, '\"'); return content; } + + /** + * remove the continuous char in the string from both sides. + * + * @param str the input string, target the char to be removed + * @return the string without continuous chars from both sides + */ + private static String removeContinuousChar(String str, char target) { + if (str == null || str.length() < 2) { + return str; + } + int start = 0; + int end = str.length() - 1; + while (start <= end && str.charAt(start) == target) { + start++; + } + while (end >= start && str.charAt(end) == target) { + end--; + } + return str.substring(start, end + 1); + } Review Comment: add ut -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org