use of io.mycat.calcite.MycatRelDatasourceSourceInfo in project Mycat2 by MyCATApache.
the class ColocatedPlanner method checkColocatedPushDown.
public static Optional<PartitionGroup> checkColocatedPushDown(MycatDataContext context, Plan plan, DrdsSqlWithParams drdsSqlWithParams) {
CodeExecuterContext codeExecuterContext = plan.getCodeExecuterContext();
AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, codeExecuterContext, drdsSqlWithParams);
Map<String, MycatRelDatasourceSourceInfo> relContext = codeExecuterContext.getRelContext();
List<List<PartitionGroup>> lists = new ArrayList<>();
for (MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo : relContext.values()) {
if (mycatRelDatasourceSourceInfo.getRelNode() instanceof MycatView) {
MycatView mycatView = (MycatView) mycatRelDatasourceSourceInfo.getRelNode();
if (mycatView.getDistribution().type() == Distribution.Type.SHARDING) {
for (ShardingTable shardingTable : mycatView.getDistribution().getShardingTables()) {
if (shardingTable instanceof ShardingIndexTable) {
return Optional.empty();
}
}
}
lists.add(sqlMycatDataContext.getPartition(mycatView.getDigest()).orElse(Collections.emptyList()));
} else {
return Optional.empty();
}
}
// Colocated Push Down
PartitionGroup result = null;
for (List<PartitionGroup> list : lists) {
if (list.size() == 1) {
PartitionGroup each = list.get(0);
if (result == null) {
result = new PartitionGroup(each.getTargetName(), new HashMap<>(each.getMap()));
} else if (result.getTargetName().equals(each.getTargetName())) {
Map<String, Partition> eachMap = each.getMap();
Map<String, Partition> resMap = result.getMap();
for (Map.Entry<String, Partition> entry : eachMap.entrySet()) {
if (resMap.containsKey(entry.getKey())) {
// 已经存在的分区不一致,所以不匹配
Partition existed = resMap.get(entry.getKey());
if (!existed.getUniqueName().equals(entry.getValue().getUniqueName())) {
return Optional.empty();
}
} else {
resMap.put(entry.getKey(), entry.getValue());
}
}
} else {
return Optional.empty();
}
} else {
return Optional.empty();
}
}
return Optional.ofNullable(result);
}
use of io.mycat.calcite.MycatRelDatasourceSourceInfo 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