public static RelRoot genRelRoot(Connection connection, String sql) throws Exception {
//从 conn 中获取相关的环境和配置,生成对应配置
CalciteServerStatement st = connection.createStatement().unwrap(CalciteServerStatement.class);
CalcitePrepare.Context prepareContext = st.createPrepareContext();
final FrameworkConfig config = Frameworks.newConfigBuilder()
.parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL).build())
.defaultSchema(prepareContext.getRootSchema().plus())
// .traitDefs(ConventionTraitDef.INSTANCE, RelDistributionTraitDef.INSTANCE)
.build();
Planner planner = Frameworks.getPlanner(config);
RelRoot root = null;
try {
// 解析
SqlNode parse1 = planner.parse(sql);
// 校验
SqlNode validate = planner.validate(parse1);
root = planner.rel(validate);
RelNode rel = root.rel;
} catch (Exception e) {
e.printStackTrace();
}
return root;
}