use of io.mycat.calcite.logical.MycatView 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.calcite.logical.MycatView 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.logical.MycatView in project Mycat2 by MyCATApache.
the class RelMdCollation method collations.
/**
* Catch-all implementation for
* {@link BuiltInMetadata.Collation#collations()},
* invoked using reflection, for any relational expression not
* handled by a more specific method.
*
* <p>{@link org.apache.calcite.rel.core.Union},
* {@link org.apache.calcite.rel.core.Intersect},
* {@link org.apache.calcite.rel.core.Minus},
* {@link Join},
* {@link org.apache.calcite.rel.core.Correlate}
* do not in general return sorted results
* (but implementations using particular algorithms may).
*
* @param rel Relational expression
* @return Relational expression's collations
* @see RelMetadataQuery#collations(RelNode)
*/
public ImmutableList<RelCollation> collations(RelNode rel, RelMetadataQuery mq) {
if (rel instanceof MycatView) {
MycatView mycatView = (MycatView) rel;
RelCollation collation;
if (mycatView.isMergeSort()) {
Sort sort = (Sort) mycatView.getRelNode();
collation = sort.getCollation();
if (collation != null) {
return ImmutableList.of(collation);
}
}
collation = mycatView.getTraitSet().getCollation();
if (collation != null) {
return ImmutableList.of(collation);
}
}
return ImmutableList.of();
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatHepJoinClustering method getView.
MycatView getView(RelNode relNode) {
if (relNode instanceof HepRelVertex) {
relNode = ((HepRelVertex) relNode).getCurrentRel();
}
if (relNode instanceof MycatView) {
return (MycatView) relNode;
} else {
class Collector extends RelShuttleImpl {
public MycatView view;
public Collector() {
}
@Override
public RelNode visit(RelNode other) {
if (other instanceof MycatView) {
MycatView next = (MycatView) other;
if (this.view != null) {
// todo
} else {
this.view = next;
}
}
return super.visit(other);
}
}
Collector collector = new Collector();
relNode.accept(collector);
return collector.view;
}
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class RelJsonWriter method explain_.
// ~ Methods ------------------------------------------------------------------
protected void explain_(RelNode rel, List<Pair<String, Object>> values) {
final Map<String, Object> map = jsonBuilder.map();
// ensure that id is the first attribute
map.put("id", null);
map.put("relOp", relJson.classToTypeName(rel.getClass()));
for (Pair<String, Object> value : values) {
if (value.right instanceof RelNode) {
continue;
}
put(map, value.left, value.right);
}
// omit 'inputs: ["3"]' if "3" is the preceding rel
final List<Object> list = explainInputs(rel.getInputs());
if (list.size() != 1 || !list.get(0).equals(previousId)) {
map.put("inputs", list);
}
if (rel instanceof MycatView) {
MycatView mycatView = (MycatView) rel;
map.put("inputs", explainInputs(Collections.singletonList(mycatView.getRelNode())));
}
final String id = Integer.toString(relIdMap.size());
relIdMap.put(rel, id);
map.put("id", id);
relList.add(map);
previousId = id;
}
Aggregations