Qihoo360 / Quicksql

A Flexible, Fast, Federated(3F) SQL Analysis Middleware for Multiple Data Sources
https://quicksql.readthedocs.io
MIT License
2.06k stars 583 forks source link

请问下用calcite parse 测试时获取SqlJoin时为什么报错 #246

Open 120003808 opened 3 years ago

120003808 commented 3 years ago

calcite 1.27.0 添加多个schema是获取sqlJoin node报错,但是sql验证通过并且可以正常执行返回结果,请问下要怎么弄 `import org.apache.calcite.adapter.jdbc.JdbcSchema; import org.apache.calcite.avatica.util.Casing; import org.apache.calcite.avatica.util.Quoting; import org.apache.calcite.jdbc.CalciteConnection; import org.apache.calcite.rel.type.RelDataType; import org.apache.calcite.schema.SchemaPlus; import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.SqlSelect; import org.apache.calcite.sql.parser.SqlParser; import org.apache.calcite.tools.FrameworkConfig; import org.apache.calcite.tools.Frameworks; import org.apache.calcite.tools.Planner; import org.apache.calcite.util.Pair;

import javax.sql.DataSource; import java.sql.*; import java.util.Properties;

public class MySqlTest { private static final String report = "report"; private static final String ds = "ds";

public static void main(String[] args) throws Exception {
    Properties properties = new Properties();
    properties.setProperty("caseSensitive", "false");
    Connection connection = DriverManager.getConnection("jdbc:calcite:", properties);
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    String reportUrl =   "jdbc:mysql://localhost:3306/report?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";
    String dataCubeUrl = "jdbc:mysql://localhost:3306/ds?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai";

    DataSource reportDs = JdbcSchema.dataSource(reportUrl, "com.mysql.jdbc.Driver", "root", "root");
    DataSource cubeDs = JdbcSchema.dataSource(dataCubeUrl, "com.mysql.jdbc.Driver", "root", "root");

    SqlParser.Config builder = SqlParser.config()
            .withQuotedCasing(Casing.TO_UPPER)
            .withUnquotedCasing(Casing.TO_UPPER)
            .withCaseSensitive(false)
            .withQuoting(Quoting.BACK_TICK)
            .withUnquotedCasing(Casing.UNCHANGED);

    final FrameworkConfig config = Frameworks.newConfigBuilder()
            .defaultSchema(rootSchema)
            .parserConfig(builder)
            .build();

    Planner planner = Frameworks.getPlanner(config);

    rootSchema.add(ds, JdbcSchema.create(rootSchema, ds, reportDs, ds, null));
    rootSchema.add(report, JdbcSchema.create(rootSchema, report, cubeDs, report, null));
    String sql = "select t1.* from report.sys_conn t1 left join ds.ds_conn t2 on t1.id = t2.id limit 10";

    SqlNode originSqlNode = planner.parse(sql);
    Pair<SqlNode, RelDataType> sqlNodeRelDataTypePair = planner.validateAndGetType(originSqlNode);
    SqlNode sqlNode = sqlNodeRelDataTypePair.getKey();
    SqlSelect sqlSelect = (SqlSelect) sqlNode;
    System.out.println(sqlSelect);

    Statement statement = calciteConnection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    ResultSetMetaData metaData = resultSet.getMetaData();
    while (resultSet.next()) {
        int columnCount = metaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            System.out.print(metaData.getColumnLabel(i) + ":\t" + resultSet.getObject(i) + "\t");
        }
        System.out.println();
    }
    SqlNode from = sqlSelect.getFrom();
    System.out.println(from);
}

}`