use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class PlanImpl method explain.
public List<String> explain(MycatDataContext dataContext, DrdsSqlWithParams drdsSql, boolean code) {
ArrayList<String> list = new ArrayList<>();
ExplainWriter explainWriter = new ExplainWriter();
switch(this.type) {
case PHYSICAL:
Optional<ExplainDetail> singleViewOptional = ColocatedPlanner.executeQuery(dataContext, this, drdsSql);
if (singleViewOptional.isPresent()) {
ExplainDetail explainDetail = singleViewOptional.get();
list.add("ColocatedPushDown:");
list.add(explainDetail.toString());
}
list.add("\n");
list.add("Plan:");
String s = dumpPlan();
list.addAll(Arrays.asList(s.split("\n")));
List<SpecificSql> map = specificSql(drdsSql, dataContext);
for (SpecificSql specificSql : map) {
list.addAll(Arrays.asList(specificSql.toString().split("\n")));
}
if (code) {
list.add("Code:");
list.addAll(Arrays.asList(getCodeExecuterContext().getCodeContext().getCode().split("\n")));
}
break;
case UPDATE:
{
Collection<VertxExecuter.EachSQL> eachSQLS = VertxUpdateExecuter.explainUpdate(drdsSql, dataContext);
for (VertxExecuter.EachSQL eachSQL : eachSQLS) {
list.add(eachSQL.toString());
}
break;
}
case INSERT:
{
Iterable<VertxExecuter.EachSQL> eachSQLS = (VertxExecuter.explainInsert((SQLInsertStatement) drdsSql.getParameterizedStatement(), drdsSql.getParams()));
for (VertxExecuter.EachSQL eachSQL : eachSQLS) {
list.add(eachSQL.toString());
}
break;
}
default:
throw new IllegalStateException("Unexpected value: " + this.type);
}
for (String s1 : explainWriter.getText().split("\n")) {
list.add(s1);
}
return list.stream().filter(i -> !i.isEmpty()).collect(Collectors.toList());
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class MycatSQLTableLookup method dispatchRightObservable.
public static Observable<Object[]> dispatchRightObservable(NewMycatDataContext context, MycatSQLTableLookup tableLookup, Observable<Object[]> leftInput) {
MycatView rightView = (MycatView) tableLookup.getRight();
CopyMycatRowMetaData rightRowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(rightView.getRowType().getFieldList()));
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = (AsyncMycatDataContextImpl.SqlMycatDataContextImpl) context;
Observable<@NonNull List<Object[]>> buffer;
if (tableLookup.getType() == Type.BACK) {
// semi 可以分解
buffer = leftInput.buffer(300, 50);
} else {
// NONE 该运算不能分解
buffer = leftInput.toList().toObservable();
}
Observable<Object[]> rightObservable = buffer.flatMap(argsList -> {
if (argsList.isEmpty()) {
return Observable.empty();
}
RexShuttle rexShuttle = argSolver(argsList);
RelNode mycatInnerRelNode = rightView.getRelNode().accept(new RelShuttleImpl() {
@Override
public RelNode visit(RelNode other) {
if (other instanceof Filter) {
Filter filter = (Filter) other;
return filter.accept(rexShuttle);
} else if (other instanceof MycatTableLookupValues) {
MycatTableLookupValues mycatTableLookupValues = (MycatTableLookupValues) other;
return mycatTableLookupValues.apply(argsList);
}
return super.visit(other);
}
});
MycatView newRightView = new MycatView(rightView.getTraitSet(), mycatInnerRelNode, rightView.getDistribution(), rightView.getCondition().map(c -> c.accept(rexShuttle)).orElse(null));
DrdsSqlWithParams drdsSql = context.getDrdsSql();
SqlNode sqlTemplate = newRightView.getSQLTemplate(DrdsSqlWithParams.isForUpdate(drdsSql.getParameterizedSQL()));
ImmutableMultimap<String, SqlString> apply1 = newRightView.apply(context.getContext().getMergeUnionSize(), sqlTemplate, sqlMycatDataContext.getSqlMap(Collections.emptyMap(), newRightView, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
return Observable.merge(sqlMycatDataContext.getObservables(apply1, rightRowMetaData, (sessionConnection, sql, extractParams, calciteRowMetaData) -> VertxExecuter.runQuery(sessionConnection, sql, extractParams, calciteRowMetaData)));
});
return rightObservable;
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class MycatPreparedStatementUtil method outputToParameterizedProxySql.
public static DrdsSqlWithParams outputToParameterizedProxySql(MySqlInsertStatement sqlStatement) {
StringBuilder sb = new StringBuilder();
List<Object> outputParameters = new ArrayList<>();
MySqlExportParameterVisitor parameterVisitor = new MySqlExportParameterVisitor(outputParameters, sb, true) {
};
parameterVisitor.setShardingSupport(false);
parameterVisitor.setFeatures(VisitorFeature.OutputParameterizedQuesUnMergeInList.mask | VisitorFeature.OutputParameterizedQuesUnMergeAnd.mask | VisitorFeature.OutputParameterizedUnMergeShardingTable.mask | VisitorFeature.OutputParameterizedQuesUnMergeOr.mask | VisitorFeature.OutputParameterizedQuesUnMergeValuesList.mask | VisitorFeature.OutputParameterized.mask);
parameterVisitor.setInputParameters(Collections.emptyList());
sqlStatement.accept(parameterVisitor);
return new DrdsSqlWithParams(sb.toString(), outputParameters, false, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ExecutorProviderImpl method runAsObjectArray.
@Override
public RowBaseIterator runAsObjectArray(MycatDataContext context, String sqlStatement) {
DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(sqlStatement, context.getDefaultSchema());
Plan plan = DrdsRunnerHelper.getPlan(drdsSql);
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, plan.getCodeExecuterContext(), drdsSql);
PrepareExecutor prepareExecutor = prepare(plan);
RowBaseIterator baseIterator = prepareExecutor.asRowBaseIterator(sqlMycatDataContext, plan.getMetaData());
return baseIterator;
}
use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.
the class ExplainSQLHandler method onExecute.
@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<MySqlExplainStatement> request, MycatDataContext dataContext, Response response) {
MySqlExplainStatement explainAst = request.getAst();
explainAst.accept0(new MySqlASTVisitorAdapter() {
@Override
public boolean visit(SQLExprTableSource x) {
if (x.getSchema() == null) {
x.setSchema(dataContext.getDefaultSchema());
}
return super.visit(x);
}
});
if (explainAst.isDescribe()) {
SQLName tableName = explainAst.getTableName();
if (tableName instanceof SQLIdentifierExpr && dataContext.getDefaultSchema() != null) {
explainAst.setTableName(new SQLPropertyExpr(new SQLIdentifierExpr(dataContext.getDefaultSchema()), ((SQLIdentifierExpr) tableName).getName()));
}
return response.proxySelectToPrototype(explainAst.toUnformattedString(), Collections.emptyList());
}
SQLStatement statement = request.getAst().getStatement();
boolean forUpdate = false;
if (statement instanceof SQLSelectStatement) {
forUpdate = ((SQLSelectStatement) explainAst.getStatement()).getSelect().getFirstQueryBlock().isForUpdate();
}
ResultSetBuilder builder = ResultSetBuilder.create().addColumnInfo("plan", JDBCType.VARCHAR);
try {
HackRouter hackRouter = new HackRouter(statement, dataContext);
if (hackRouter.analyse()) {
Pair<String, String> plan = hackRouter.getPlan();
builder.addObjectRowPayload(Arrays.asList("targetName: " + plan.getKey() + " sql: " + plan.getValue()));
} else {
List<SQLCommentHint> hints = explainAst.getHints();
if (hints != null) {
statement.setHeadHints(hints);
}
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(statement, dataContext.getDefaultSchema());
Plan plan;
if (statement instanceof SQLInsertStatement || statement instanceof SQLUpdateStatement || statement instanceof SQLDeleteStatement) {
plan = UpdateSQLHandler.getPlan(drdsSqlWithParams);
} else {
plan = DrdsRunnerHelper.getPlan(drdsSqlWithParams);
}
List<String> explain = plan.explain(dataContext, drdsSqlWithParams, true);
for (String s1 : explain) {
builder.addObjectRowPayload(Arrays.asList(s1));
}
}
} catch (Throwable th) {
LOGGER.error("", th);
builder.addObjectRowPayload(Arrays.asList(th.toString()));
}
return response.sendResultSet(RowIterable.create(builder.build()));
}
Aggregations