Source:
public static String RToSqlUdf(List<String> RExps, List<String> selectedColumns, List<Column> existingColumns) { List<String> udfs = Lists.newArrayList(); Map<String, String> newColToDef = new HashMap<String, String>(); boolean updateOnConflict = (selectedColumns == null || selectedColumns.isEmpty()); String dupColExp = "%s duplicates another column name"; if (updateOnConflict) { if (existingColumns != null && !existingColumns.isEmpty()) { for (Column c : existingColumns) { udfs.add(c.getName()); } } } else { for (String c : selectedColumns) { udfs.add(c); } } Set<String> newColsInRExp = new HashSet<String>(); for (String str : RExps) { int index = Integer.max(str.indexOf("="), str.indexOf("~")); String[] udf = new String[2]; if (index == -1) { udf[0] = str; } else { udf[0] = str.substring(0,index); udf[1] = str.substring(index + 1); } String newCol = (index != -1) ? udf[0].trim().replaceAll("\\W", "") : udf[0].trim(); if (newColsInRExp.contains(newCol)) { throw new RuntimeException(String.format(dupColExp, newCol)); } String newDef = (index != -1) ? udf[1].trim() : null; if (!udfs.contains(newCol)) { udfs.add(newCol); } else if (!updateOnConflict) { throw new RuntimeException(String.format(dupColExp,newCol)); } if (newDef != null && !newDef.isEmpty()) { newColToDef.put(newCol.replaceAll("\\W", ""), newDef); } newColsInRExp.add(newCol); } String selectStr = ""; for (String udf : udfs) { String exp = newColToDef.containsKey(udf) ? String.format("%s as %s", newColToDef.get(udf), udf) : String.format("%s", udf); selectStr += (exp + ","); } return selectStr.substring(0, selectStr.length() - 1); }

Target:
public static String RToSqlUdf(List<String> RExps, List<String> selectedColumns, List<Column> existingColumns) { List<String> udfs = Lists.newArrayList(); Map<String, String> newColToDef = new HashMap<String, String>(); boolean updateOnConflict = (selectedColumns == null || selectedColumns.isEmpty()); String dupColExp = "%s duplicates another column name"; if (updateOnConflict) { if (existingColumns != null && !existingColumns.isEmpty()) { for (Column c : existingColumns) { udfs.add(c.getName()); } } } else { for (String c : selectedColumns) { udfs.add(c); } } Set<String> newColsInRExp = new HashSet<String>(); for (String str : RExps) { int index = str.indexOf("=") > str.indexOf("~") ? str.indexOf("=") : str.indexOf("~"); String[] udf = new String[2]; if (index == -1) { udf[0] = str; } else { udf[0] = str.substring(0,index); udf[1] = str.substring(index + 1); } String newCol = (index != -1) ? udf[0].trim().replaceAll("\\W", "") : udf[0].trim(); if (newColsInRExp.contains(newCol)) { throw new RuntimeException(String.format(dupColExp, newCol)); } String newDef = (index != -1) ? udf[1].trim() : null; if (!udfs.contains(newCol)) { udfs.add(newCol); } else if (!updateOnConflict) { throw new RuntimeException(String.format(dupColExp,newCol)); } if (newDef != null && !newDef.isEmpty()) { newColToDef.put(newCol.replaceAll("\\W", ""), newDef); } newColsInRExp.add(newCol); } String selectStr = ""; for (String udf : udfs) { String exp = newColToDef.containsKey(udf) ? String.format("%s as %s", newColToDef.get(udf), udf) : String.format("%s", udf); selectStr += (exp + ","); } return selectStr.substring(0, selectStr.length() - 1); }