Search in sources :

Example 11 with MycatView

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;
}
Also used : VertxExecuter(io.mycat.vertx.VertxExecuter) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) Getter(lombok.Getter) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) RelOptCost(org.apache.calcite.plan.RelOptCost) NonNull(io.reactivex.rxjava3.annotations.NonNull) Filter(org.apache.calcite.rel.core.Filter) RelMdUtil(org.apache.calcite.rel.metadata.RelMdUtil) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) RexUtil(org.apache.calcite.rex.RexUtil) CorrelationId(org.apache.calcite.rel.core.CorrelationId) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) NewMycatConnection(io.mycat.newquery.NewMycatConnection) io.mycat.calcite(io.mycat.calcite) org.apache.calcite.adapter.enumerable(org.apache.calcite.adapter.enumerable) MycatView(io.mycat.calcite.logical.MycatView) Future(io.vertx.core.Future) JoinInfo(org.apache.calcite.rel.core.JoinInfo) Collectors(java.util.stream.Collectors) org.apache.calcite.sql(org.apache.calcite.sql) List(java.util.List) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) MYCAT_SQL_LOOKUP_IN(io.mycat.calcite.MycatImplementor.MYCAT_SQL_LOOKUP_IN) org.apache.calcite.linq4j.tree(org.apache.calcite.linq4j.tree) JoinRelType(org.apache.calcite.rel.core.JoinRelType) org.apache.calcite.rel(org.apache.calcite.rel) SqlString(org.apache.calcite.sql.util.SqlString) RexShuttle(org.apache.calcite.rex.RexShuttle) Util(org.apache.calcite.util.Util) NotNull(org.jetbrains.annotations.NotNull) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Collections(java.util.Collections) RexCall(org.apache.calcite.rex.RexCall) RexShuttle(org.apache.calcite.rex.RexShuttle) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) SqlString(org.apache.calcite.sql.util.SqlString) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SqlString(org.apache.calcite.sql.util.SqlString) MycatView(io.mycat.calcite.logical.MycatView) Filter(org.apache.calcite.rel.core.Filter) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 12 with MycatView

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);
}
Also used : CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) MycatView(io.mycat.calcite.logical.MycatView) MycatRelDatasourceSourceInfo(io.mycat.calcite.MycatRelDatasourceSourceInfo) ShardingIndexTable(io.mycat.calcite.table.ShardingIndexTable) ShardingTable(io.mycat.calcite.table.ShardingTable) NameMap(io.mycat.util.NameMap)

Example 13 with MycatView

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();
}
Also used : MycatView(io.mycat.calcite.logical.MycatView)

Example 14 with MycatView

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;
    }
}
Also used : HepRelVertex(org.apache.calcite.plan.hep.HepRelVertex) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl)

Example 15 with MycatView

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;
}
Also used : MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode)

Aggregations

MycatView (io.mycat.calcite.logical.MycatView)29 RelNode (org.apache.calcite.rel.RelNode)21 RexNode (org.apache.calcite.rex.RexNode)9 RelBuilder (org.apache.calcite.tools.RelBuilder)8 RelOptCluster (org.apache.calcite.plan.RelOptCluster)7 RelHint (org.apache.calcite.rel.hint.RelHint)7 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)7 NotNull (org.jetbrains.annotations.NotNull)7 MycatSQLTableLookup (io.mycat.calcite.physical.MycatSQLTableLookup)6 Collectors (java.util.stream.Collectors)6 ImmutableList (com.google.common.collect.ImmutableList)5 RelCollation (org.apache.calcite.rel.RelCollation)5 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)5 RexBuilder (org.apache.calcite.rex.RexBuilder)5 MycatProject (io.mycat.calcite.physical.MycatProject)4 Join (org.apache.calcite.rel.core.Join)4 JoinRelType (org.apache.calcite.rel.core.JoinRelType)4 RelDataType (org.apache.calcite.rel.type.RelDataType)4 SqlString (org.apache.calcite.sql.util.SqlString)4 io.mycat (io.mycat)3