pjfanning commented on a change in pull request #2428:
URL: https://github.com/apache/drill/pull/2428#discussion_r785377804



##########
File path: 
contrib/format-excel/src/main/java/org/apache/drill/exec/store/excel/ExcelBatchReader.java
##########
@@ -364,6 +368,36 @@ private void getColumnHeaders(SchemaBuilder builder) {
     builder.buildSchema();
   }
 
+  /**
+   * This function verifies whether a given column name is already present in 
the projected schema.
+   * If so, it appends _n to the column name.  N will be incremented for every 
duplicate column
+   * @param columnName The original column
+   * @return The deconflicted column name
+   */
+  private String deconflictColumnNames(String columnName) {
+    Pattern pattern = Pattern.compile("_(\\d+)$");
+    Matcher matcher = pattern.matcher(columnName);
+    while (containsIgnoreCase(columnName, excelFieldNames)) {
+      if (matcher.find()) {
+        int index = Integer.parseInt(matcher.group(1));
+        index++;
+        columnName = matcher.replaceFirst("_" + index);
+      } else {
+        columnName = columnName + "_1";
+      }
+    }
+    return columnName;
+  }
+
+  private boolean containsIgnoreCase(String str, List<String> list) {

Review comment:
       this is a little slow - you could maintain a TreeSet which has the same 
elements as `excelFieldNames` - 
`TreeSet<String>(String.CASE_INSENSITIVE_ORDER)` -- it would be great if 
LinkedHashSet supported this ordering too but it seems it doesn't
   
   So the trade off is using a little extra memory to keep the TreeSet and 
being about to do lookups in order (log-base2-n) time instead of order (n). Not 
a big deal really but worth considering.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to