@Test public void testWithSyntax2() throws SqlParseException { String origin = "with tmp as (select id from a),tmp2 as (select * from b) select * from tmp,tmp2 where tmp.id = tmp2.id"; String expected = "with tmp as (select id from ssb.a),tmp2 as (select * from ssb.b) select * from tmp,tmp2 where tmp.id = tmp2.id"; Assert.assertEquals(expected, PushDownUtil.schemaCompletion(origin, "ssb")); }
static String schemaCompletion(String inputSql, String schema) throws SqlParseException { if (inputSql == null || inputSql.equals("")) { return ""; } SqlNode node = CalciteParser.parse(inputSql); PushDownUtil.FromTablesVisitor ftv = new PushDownUtil.FromTablesVisitor(); node.accept(ftv); List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema(); if (tablesWithoutSchema.isEmpty()) { return inputSql; } List<Pair<Integer, Integer>> tablesPos = new ArrayList<>(); for (SqlNode tables : tablesWithoutSchema) { tablesPos.add(CalciteParser.getReplacePos(tables, inputSql)); } Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { int r = o2.getFirst() - o1.getFirst(); return r == 0 ? o2.getSecond() - o1.getSecond() : r; } }); StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); }
PushDownUtil { static String schemaCompletion(String inputSql, String schema) throws SqlParseException { if (inputSql == null || inputSql.equals("")) { return ""; } SqlNode node = CalciteParser.parse(inputSql); PushDownUtil.FromTablesVisitor ftv = new PushDownUtil.FromTablesVisitor(); node.accept(ftv); List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema(); if (tablesWithoutSchema.isEmpty()) { return inputSql; } List<Pair<Integer, Integer>> tablesPos = new ArrayList<>(); for (SqlNode tables : tablesWithoutSchema) { tablesPos.add(CalciteParser.getReplacePos(tables, inputSql)); } Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { int r = o2.getFirst() - o1.getFirst(); return r == 0 ? o2.getSecond() - o1.getSecond() : r; } }); StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); } }
PushDownUtil { static String schemaCompletion(String inputSql, String schema) throws SqlParseException { if (inputSql == null || inputSql.equals("")) { return ""; } SqlNode node = CalciteParser.parse(inputSql); PushDownUtil.FromTablesVisitor ftv = new PushDownUtil.FromTablesVisitor(); node.accept(ftv); List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema(); if (tablesWithoutSchema.isEmpty()) { return inputSql; } List<Pair<Integer, Integer>> tablesPos = new ArrayList<>(); for (SqlNode tables : tablesWithoutSchema) { tablesPos.add(CalciteParser.getReplacePos(tables, inputSql)); } Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { int r = o2.getFirst() - o1.getFirst(); return r == 0 ? o2.getSecond() - o1.getSecond() : r; } }); StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); } private  PushDownUtil(); }
PushDownUtil { static String schemaCompletion(String inputSql, String schema) throws SqlParseException { if (inputSql == null || inputSql.equals("")) { return ""; } SqlNode node = CalciteParser.parse(inputSql); PushDownUtil.FromTablesVisitor ftv = new PushDownUtil.FromTablesVisitor(); node.accept(ftv); List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema(); if (tablesWithoutSchema.isEmpty()) { return inputSql; } List<Pair<Integer, Integer>> tablesPos = new ArrayList<>(); for (SqlNode tables : tablesWithoutSchema) { tablesPos.add(CalciteParser.getReplacePos(tables, inputSql)); } Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { int r = o2.getFirst() - o1.getFirst(); return r == 0 ? o2.getSecond() - o1.getSecond() : r; } }); StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); } private  PushDownUtil(); static Pair<List<List<String>>, List<SelectedColumnMeta>> tryPushDownSelectQuery(String project, String sql,
            String defaultSchema, SQLException sqlException, boolean isPrepare); static Pair<List<List<String>>, List<SelectedColumnMeta>> tryPushDownNonSelectQuery(String project,
            String sql, String defaultSchema, boolean isPrepare); }
PushDownUtil { static String schemaCompletion(String inputSql, String schema) throws SqlParseException { if (inputSql == null || inputSql.equals("")) { return ""; } SqlNode node = CalciteParser.parse(inputSql); PushDownUtil.FromTablesVisitor ftv = new PushDownUtil.FromTablesVisitor(); node.accept(ftv); List<SqlNode> tablesWithoutSchema = ftv.getTablesWithoutSchema(); if (tablesWithoutSchema.isEmpty()) { return inputSql; } List<Pair<Integer, Integer>> tablesPos = new ArrayList<>(); for (SqlNode tables : tablesWithoutSchema) { tablesPos.add(CalciteParser.getReplacePos(tables, inputSql)); } Collections.sort(tablesPos, new Comparator<Pair<Integer, Integer>>() { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { int r = o2.getFirst() - o1.getFirst(); return r == 0 ? o2.getSecond() - o1.getSecond() : r; } }); StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); } private  PushDownUtil(); static Pair<List<List<String>>, List<SelectedColumnMeta>> tryPushDownSelectQuery(String project, String sql,
            String defaultSchema, SQLException sqlException, boolean isPrepare); static Pair<List<List<String>>, List<SelectedColumnMeta>> tryPushDownNonSelectQuery(String project,
            String sql, String defaultSchema, boolean isPrepare);  }