use of io.mycat.DrdsSql in project Mycat2 by MyCATApache.
the class QueryPlanner method innerComputeMinCostCodeExecuterContext.
public CodeExecuterContext innerComputeMinCostCodeExecuterContext(DrdsSql sqlSelectStatement) {
RelOptCluster relOptCluster = DrdsSqlCompiler.newCluster();
List<CodeExecuterContext> codeExecuterContexts = getAcceptedMycatRelList(sqlSelectStatement);
int size = codeExecuterContexts.size();
switch(size) {
case 0:
{
throw new IllegalArgumentException();
}
case 1:
{
return codeExecuterContexts.get(0);
}
default:
class SortObject implements Comparable<SortObject> {
final CodeExecuterContext context;
final RelOptCost cost;
public SortObject(CodeExecuterContext context, RelOptCost cost) {
this.context = context;
this.cost = cost;
}
@Override
public int compareTo(@NotNull SortObject o) {
return (this.cost.isLt(o.cost) ? 0 : 1);
}
}
return codeExecuterContexts.stream().filter(i -> i != null).map(i -> new SortObject(i, i.getMycatRel().computeSelfCost(relOptCluster.getPlanner(), getRelMetadataQuery()))).min(SortObject::compareTo).map(i -> i.context).orElse(null);
}
}
use of io.mycat.DrdsSql in project Mycat2 by MyCATApache.
the class MycatViewPlan method execute.
@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
RootAllocator rootAllocator = new RootAllocator();
NewMycatDataContext context = (NewMycatDataContext) rootContext.getContext();
DrdsSqlWithParams drdsSql = context.getDrdsSql();
MycatView view = (MycatView) mycatView;
CopyMycatRowMetaData rowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(view.getRowType().getFieldList()));
MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo = new MycatRelDatasourceSourceInfo(rowMetaData, view.getSQLTemplate(DrdsSql.isForUpdate(drdsSql.getParameterizedStatement())), view);
SqlNode sqlTemplate = mycatRelDatasourceSourceInfo.getSqlTemplate();
List<PartitionGroup> partitionGroups = AsyncMycatDataContextImpl.getSqlMap(Collections.emptyMap(), view, drdsSql, drdsSql.getHintDataNodeFilter());
ImmutableMultimap<String, SqlString> stringSqlStringImmutableMultimap = view.apply(-1, sqlTemplate, partitionGroups, drdsSql.getParams());
List<Observable<VectorSchemaRoot>> observableList = new ArrayList<>();
for (Map.Entry<String, SqlString> entry : stringSqlStringImmutableMultimap.entries()) {
String key = entry.getKey();
SqlString sqlString = entry.getValue();
observableList.add(Observable.create(emitter -> {
Future<NewMycatConnection> connectionFuture = context.getConnection(context.getContext().resolveDatasourceTargetName(key));
Future<Observable<VectorSchemaRoot>> observableFuture = connectionFuture.map(connection -> {
Observable<VectorSchemaRoot> observable = connection.prepareQuery(sqlString.getSql(), MycatPreparedStatementUtil.extractParams(drdsSql.getParams(), sqlString.getDynamicParameters()), MycatRelDataType.getMycatRelType(MycatViewPlan.this.schema()), rootAllocator);
return observable.doOnComplete(() -> context.recycleConnection(context.getContext().resolveDatasourceTargetName(key), Future.succeededFuture(connection)));
});
observableFuture.onFailure(event -> emitter.tryOnError(event));
observableFuture.onSuccess(event -> {
event = event.doOnComplete(() -> emitter.onComplete());
event = event.doOnError(throwable -> emitter.tryOnError(throwable));
event.forEach(vectorSchemaRoot -> emitter.onNext(vectorSchemaRoot));
});
}));
}
return Observable.fromIterable(observableList).flatMap(i -> i);
}
Aggregations