@Test public void testTranslateCreateToWithInternal(){ String sql = " create table Identifier as select x as 'temp column' from fact_table;"; Pair<String, String> result = TempStatementUtil.translateCreateToWithInternal(sql); Assert.assertEquals("Identifier", result.getFirst()); Assert.assertEquals(" Identifier as (select x as 'temp column' from fact_table)", result.getSecond()); sql = " create temporary table Identifier as select x as 'temp column' \nfrom fact_table;"; result = TempStatementUtil.translateCreateToWithInternal(sql); Assert.assertEquals("Identifier", result.getFirst()); Assert.assertEquals(" Identifier as (select x as 'temp column' \nfrom fact_table)", result.getSecond()); }
static Pair<String, String> translateCreateToWithInternal(String sql) { String sql1 = sql.trim(); if (sql1.endsWith(";")) { sql1 = sql1.substring(0, sql1.length() - 1); } if (sql1.matches("(?i)^CREATE\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TABLE", WITH); } else if (sql1.matches("(?i)^CREATE\\s+TEMPORARY\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TEMPORARY\\s+TABLE", WITH); } else { throw new RuntimeException("Sql " + sql + " is not create table sql"); } String identifier = null; String[] splits = sql1.split("((?<=\\s)|(?=\\s))"); Pattern spacePattern = Pattern.compile("\\s"); ParserState state = ParserState.WAITING_WITH; for (int i = 0; i < splits.length; i++) { if (state == ParserState.IGNORE) { break; } if (spacePattern.matcher(splits[i]).matches()) { continue; } switch (state) { case WAITING_WITH: if (!splits[i].equals(WITH)) { throw new RuntimeException("the translated sql should start with \"WITH\", sql is " + sql1); } state = ParserState.WAITING_IDENTIFIER; break; case WAITING_IDENTIFIER: identifier = splits[i]; state = ParserState.WAITING_AS; break; case WAITING_AS: if (!splits[i].equalsIgnoreCase("AS")) { throw new RuntimeException("the translated sql should contains \"AS\", sql is " + sql1); } state = ParserState.WAITING_LEFT_PAREN; break; case WAITING_LEFT_PAREN: if (!splits[i].startsWith("(")) { splits[i] = "(" + splits[i]; splits[splits.length - 1] = splits[splits.length - 1] + ")"; } state = ParserState.IGNORE; break; default: break; } } sql1 = StringUtils.join(splits, "", 1, splits.length); return new Pair<>(identifier, sql1); }
TempStatementUtil { static Pair<String, String> translateCreateToWithInternal(String sql) { String sql1 = sql.trim(); if (sql1.endsWith(";")) { sql1 = sql1.substring(0, sql1.length() - 1); } if (sql1.matches("(?i)^CREATE\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TABLE", WITH); } else if (sql1.matches("(?i)^CREATE\\s+TEMPORARY\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TEMPORARY\\s+TABLE", WITH); } else { throw new RuntimeException("Sql " + sql + " is not create table sql"); } String identifier = null; String[] splits = sql1.split("((?<=\\s)|(?=\\s))"); Pattern spacePattern = Pattern.compile("\\s"); ParserState state = ParserState.WAITING_WITH; for (int i = 0; i < splits.length; i++) { if (state == ParserState.IGNORE) { break; } if (spacePattern.matcher(splits[i]).matches()) { continue; } switch (state) { case WAITING_WITH: if (!splits[i].equals(WITH)) { throw new RuntimeException("the translated sql should start with \"WITH\", sql is " + sql1); } state = ParserState.WAITING_IDENTIFIER; break; case WAITING_IDENTIFIER: identifier = splits[i]; state = ParserState.WAITING_AS; break; case WAITING_AS: if (!splits[i].equalsIgnoreCase("AS")) { throw new RuntimeException("the translated sql should contains \"AS\", sql is " + sql1); } state = ParserState.WAITING_LEFT_PAREN; break; case WAITING_LEFT_PAREN: if (!splits[i].startsWith("(")) { splits[i] = "(" + splits[i]; splits[splits.length - 1] = splits[splits.length - 1] + ")"; } state = ParserState.IGNORE; break; default: break; } } sql1 = StringUtils.join(splits, "", 1, splits.length); return new Pair<>(identifier, sql1); } }
TempStatementUtil { static Pair<String, String> translateCreateToWithInternal(String sql) { String sql1 = sql.trim(); if (sql1.endsWith(";")) { sql1 = sql1.substring(0, sql1.length() - 1); } if (sql1.matches("(?i)^CREATE\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TABLE", WITH); } else if (sql1.matches("(?i)^CREATE\\s+TEMPORARY\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TEMPORARY\\s+TABLE", WITH); } else { throw new RuntimeException("Sql " + sql + " is not create table sql"); } String identifier = null; String[] splits = sql1.split("((?<=\\s)|(?=\\s))"); Pattern spacePattern = Pattern.compile("\\s"); ParserState state = ParserState.WAITING_WITH; for (int i = 0; i < splits.length; i++) { if (state == ParserState.IGNORE) { break; } if (spacePattern.matcher(splits[i]).matches()) { continue; } switch (state) { case WAITING_WITH: if (!splits[i].equals(WITH)) { throw new RuntimeException("the translated sql should start with \"WITH\", sql is " + sql1); } state = ParserState.WAITING_IDENTIFIER; break; case WAITING_IDENTIFIER: identifier = splits[i]; state = ParserState.WAITING_AS; break; case WAITING_AS: if (!splits[i].equalsIgnoreCase("AS")) { throw new RuntimeException("the translated sql should contains \"AS\", sql is " + sql1); } state = ParserState.WAITING_LEFT_PAREN; break; case WAITING_LEFT_PAREN: if (!splits[i].startsWith("(")) { splits[i] = "(" + splits[i]; splits[splits.length - 1] = splits[splits.length - 1] + ")"; } state = ParserState.IGNORE; break; default: break; } } sql1 = StringUtils.join(splits, "", 1, splits.length); return new Pair<>(identifier, sql1); }  }
TempStatementUtil { static Pair<String, String> translateCreateToWithInternal(String sql) { String sql1 = sql.trim(); if (sql1.endsWith(";")) { sql1 = sql1.substring(0, sql1.length() - 1); } if (sql1.matches("(?i)^CREATE\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TABLE", WITH); } else if (sql1.matches("(?i)^CREATE\\s+TEMPORARY\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TEMPORARY\\s+TABLE", WITH); } else { throw new RuntimeException("Sql " + sql + " is not create table sql"); } String identifier = null; String[] splits = sql1.split("((?<=\\s)|(?=\\s))"); Pattern spacePattern = Pattern.compile("\\s"); ParserState state = ParserState.WAITING_WITH; for (int i = 0; i < splits.length; i++) { if (state == ParserState.IGNORE) { break; } if (spacePattern.matcher(splits[i]).matches()) { continue; } switch (state) { case WAITING_WITH: if (!splits[i].equals(WITH)) { throw new RuntimeException("the translated sql should start with \"WITH\", sql is " + sql1); } state = ParserState.WAITING_IDENTIFIER; break; case WAITING_IDENTIFIER: identifier = splits[i]; state = ParserState.WAITING_AS; break; case WAITING_AS: if (!splits[i].equalsIgnoreCase("AS")) { throw new RuntimeException("the translated sql should contains \"AS\", sql is " + sql1); } state = ParserState.WAITING_LEFT_PAREN; break; case WAITING_LEFT_PAREN: if (!splits[i].startsWith("(")) { splits[i] = "(" + splits[i]; splits[splits.length - 1] = splits[splits.length - 1] + ")"; } state = ParserState.IGNORE; break; default: break; } } sql1 = StringUtils.join(splits, "", 1, splits.length); return new Pair<>(identifier, sql1); }  static Pair<Boolean, String> handleTempStatement(String sql, KylinConfig config); }
TempStatementUtil { static Pair<String, String> translateCreateToWithInternal(String sql) { String sql1 = sql.trim(); if (sql1.endsWith(";")) { sql1 = sql1.substring(0, sql1.length() - 1); } if (sql1.matches("(?i)^CREATE\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TABLE", WITH); } else if (sql1.matches("(?i)^CREATE\\s+TEMPORARY\\s+TABLE\\p{all}*")) { sql1 = sql1.replaceAll("(?i)CREATE\\s+TEMPORARY\\s+TABLE", WITH); } else { throw new RuntimeException("Sql " + sql + " is not create table sql"); } String identifier = null; String[] splits = sql1.split("((?<=\\s)|(?=\\s))"); Pattern spacePattern = Pattern.compile("\\s"); ParserState state = ParserState.WAITING_WITH; for (int i = 0; i < splits.length; i++) { if (state == ParserState.IGNORE) { break; } if (spacePattern.matcher(splits[i]).matches()) { continue; } switch (state) { case WAITING_WITH: if (!splits[i].equals(WITH)) { throw new RuntimeException("the translated sql should start with \"WITH\", sql is " + sql1); } state = ParserState.WAITING_IDENTIFIER; break; case WAITING_IDENTIFIER: identifier = splits[i]; state = ParserState.WAITING_AS; break; case WAITING_AS: if (!splits[i].equalsIgnoreCase("AS")) { throw new RuntimeException("the translated sql should contains \"AS\", sql is " + sql1); } state = ParserState.WAITING_LEFT_PAREN; break; case WAITING_LEFT_PAREN: if (!splits[i].startsWith("(")) { splits[i] = "(" + splits[i]; splits[splits.length - 1] = splits[splits.length - 1] + ")"; } state = ParserState.IGNORE; break; default: break; } } sql1 = StringUtils.join(splits, "", 1, splits.length); return new Pair<>(identifier, sql1); }  static Pair<Boolean, String> handleTempStatement(String sql, KylinConfig config);  }