use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.
the class DrdsSqlCompiler method dispatch.
public MycatRel dispatch(OptimizationContext optimizationContext, DrdsSql drdsSql, SchemaPlus plus) {
SQLStatement sqlStatement = drdsSql.getParameterizedStatement();
if (sqlStatement instanceof SQLSelectStatement) {
return compileQuery(optimizationContext, plus, drdsSql);
}
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
if (sqlStatement instanceof MySqlInsertStatement) {
MySqlInsertStatement insertStatement = (MySqlInsertStatement) sqlStatement;
String schemaName = SQLUtils.normalize(insertStatement.getTableSource().getSchema());
String tableName = SQLUtils.normalize(insertStatement.getTableName().getSimpleName());
TableHandler logicTable = Objects.requireNonNull(metadataManager.getTable(schemaName, tableName));
switch(logicTable.getType()) {
case SHARDING:
MycatInsertRel mycatInsertRel = new MycatInsertRel(sqlStatement, false);
optimizationContext.saveAlways();
return mycatInsertRel;
case GLOBAL:
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
case NORMAL:
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof MySqlUpdateStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlUpdateStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(tableSource.getSchema());
String tableName = SQLUtils.normalize(((MySqlUpdateStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof SQLReplaceStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((SQLReplaceStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
String tableName = SQLUtils.normalize(((SQLReplaceStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
} else if (sqlStatement instanceof MySqlDeleteStatement) {
SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlDeleteStatement) sqlStatement).getTableSource();
String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
String tableName = SQLUtils.normalize(((MySqlDeleteStatement) sqlStatement).getTableName().getSimpleName());
TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
switch(logicTable.getType()) {
case SHARDING:
return compileDelete(logicTable, optimizationContext, drdsSql, plus);
case GLOBAL:
{
return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
}
case NORMAL:
{
return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
}
case CUSTOM:
throw new UnsupportedOperationException();
}
}
return null;
}
use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.
the class MemPlanCache method add.
public synchronized PlanResultSet add(boolean fix, DrdsSql drdsSql) {
Long baselineId = null;
Baseline baseline = this.getBaseline(drdsSql);
DrdsSqlCompiler drdsSqlCompiler = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel mycatRel = drdsSqlCompiler.dispatch(optimizationContext, drdsSql);
RelJsonWriter relJsonWriter = new RelJsonWriter();
mycatRel.explain(relJsonWriter);
long hash = planIds.nextPlanId();
BaselinePlan newBaselinePlan = new BaselinePlan(drdsSql.getParameterizedSQL(), relJsonWriter.asString(), hash, baselineId = baseline.getBaselineId(), null);
getCodeExecuterContext(baseline, newBaselinePlan, optimizationContext, mycatRel);
return saveBaselinePlan(fix, false, baseline, newBaselinePlan);
}
use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.
the class PartitionKeyJoinTest method parse.
public static Explain parse(DrdsConst drdsConst, String sql) {
DrdsSqlCompiler drds = getDrds(drdsConst);
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.
the class ShardingJoinRBOMegreTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.
the class UpdateSQLHandler method getPlan.
@Nullable
public static Plan getPlan(DrdsSqlWithParams drdsSqlWithParams) {
UpdatePlanCache updatePlanCache = MetaClusterCurrent.wrapper(UpdatePlanCache.class);
Plan plan = updatePlanCache.computeIfAbsent(drdsSqlWithParams.getParameterizedSQL(), s -> {
DrdsSqlCompiler drdsRunner = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drdsRunner.dispatch(optimizationContext, drdsSqlWithParams);
return DrdsExecutorCompiler.convertToExecuter(dispatch);
});
return plan;
}
Aggregations