Search in sources :

Example 6 with MycatView

use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.

the class MycatJoinTableLookupTransposeRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    Join join = call.rel(0);
    RelDataType originalRowType = join.getRowType();
    RelOptCluster cluster = join.getCluster();
    RelNode outerLeft = call.rel(1);
    RelNode outerRight = call.rel(2);
    if (outerLeft instanceof MycatSQLTableLookup) {
        MycatSQLTableLookup mycatSQLTableLookup = (MycatSQLTableLookup) outerLeft;
        if (mycatSQLTableLookup.getType() == MycatSQLTableLookup.Type.BACK) {
            int fieldCount = mycatSQLTableLookup.getInput().getRowType().getFieldCount();
            if (join.analyzeCondition().leftSet().asList().stream().allMatch(i -> i < fieldCount)) {
                // 与最左表无关
                RelNode bottomInput = mycatSQLTableLookup.getInput();
                MycatView indexRightView = (MycatView) mycatSQLTableLookup.getRight();
                RelNode newInputJoin = join.copy(bottomInput.getTraitSet(), ImmutableList.of(bottomInput, outerRight));
                MycatSQLTableLookup newMycatSQLTableLookup = new MycatSQLTableLookup(join.getCluster(), join.getTraitSet(), newInputJoin, indexRightView, mycatSQLTableLookup.getJoinType(), mycatSQLTableLookup.getCondition(), mycatSQLTableLookup.getCorrelationIds(), mycatSQLTableLookup.getType());
                fixProject(originalRowType, newMycatSQLTableLookup, call.builder()).ifPresent(res -> {
                    call.transformTo(res);
                });
                return;
            }
        }
    }
    if (outerRight instanceof MycatSQLTableLookup) {
        MycatSQLTableLookup mycatSQLTableLookup = (MycatSQLTableLookup) outerRight;
        MycatView indexRightView = (MycatView) mycatSQLTableLookup.getRight();
        RelNode newInputJoin = join.copy(outerLeft.getTraitSet(), ImmutableList.of(mycatSQLTableLookup.getInput(), indexRightView));
        if (mycatSQLTableLookup.getType() == MycatSQLTableLookup.Type.BACK) {
            int fieldCount = mycatSQLTableLookup.getInput().getRowType().getFieldCount();
            if (join.analyzeCondition().rightSet().asList().stream().allMatch(i -> i > fieldCount)) {
                // 最右回表无关
                MycatSQLTableLookup newMycatSQLTableLookup = new MycatSQLTableLookup(join.getCluster(), join.getTraitSet(), newInputJoin, indexRightView, mycatSQLTableLookup.getJoinType(), mycatSQLTableLookup.getCondition(), mycatSQLTableLookup.getCorrelationIds(), mycatSQLTableLookup.getType());
                fixProject(originalRowType, newMycatSQLTableLookup, call.builder()).ifPresent(res -> {
                    call.transformTo(res);
                });
                return;
            }
        }
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) Join(org.apache.calcite.rel.core.Join) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 7 with MycatView

use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.

the class MycatMergeJoinRule method convert.

public RelNode convert(RelNode e, RelTraitSet targetTraits, boolean rbo) {
    if (rbo) {
        RelCollation targetCollation = targetTraits.getCollation();
        if (targetCollation != null && !targetCollation.getFieldCollations().isEmpty() && e instanceof MycatView) {
            RelTraitSet orginalTraitSet = e.getTraitSet();
            RelCollation orginalCollation = orginalTraitSet.getCollation();
            MycatView mycatView = (MycatView) e;
            RelNode relNode = mycatView.getRelNode();
            if (orginalCollation == null || orginalCollation.getFieldCollations().isEmpty()) {
                return mycatView.changeTo(LogicalSort.create(relNode, targetCollation, null, null));
            } else {
                if (orginalCollation.equals(targetTraits.getCollation())) {
                    return e;
                }
                if (relNode instanceof Sort) {
                    // todo check it right
                    Sort sort = (Sort) relNode;
                    return mycatView.changeTo(LogicalSort.create(relNode, targetCollation, sort.offset, sort.fetch));
                }
                return mycatView.changeTo(LogicalSort.create(relNode, targetCollation, null, null));
            }
        }
    } else {
        return convert(e, targetTraits);
    }
    return null;
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) LogicalSort(org.apache.calcite.rel.logical.LogicalSort) Sort(org.apache.calcite.rel.core.Sort) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 8 with MycatView

use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.

the class MycatRelFieldTrimmer method trimFields.

/**
 * TrimResult.class,
 * this,
 * "trimFields",
 * RelNode.class,
 * ImmutableBitSet.class,
 * Set.class
 * final ImmutableBitSet fieldsUsed,
 * Set<RelDataTypeField> extraFields
 *
 * @param
 * @return
 */
public TrimResult trimFields(MycatView rel, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    MycatView view = (MycatView) rel;
    RelNode oldRelNode = view.getRelNode();
    RelNode newRelNode = RelOptUtil.createProject(oldRelNode, fieldsUsed.asList());
    if (newRelNode == oldRelNode) {
        return super.trimFields(rel, fieldsUsed, extraFields);
    }
    LogicalProject project = (LogicalProject) newRelNode;
    Mappings.TargetMapping mapping = project.getMapping();
    if (mapping == null || project.getProjects().isEmpty()) {
        return super.trimFields(rel, fieldsUsed, extraFields);
    }
    MycatView newMycatView = new MycatView(view.getTraitSet(), newRelNode, view.getDistribution());
    return super.result(newMycatView, Mappings.target(mapping, rel.getRowType().getFieldCount(), newMycatView.getRowType().getFieldCount()));
}
Also used : MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) Mappings(org.apache.calcite.util.mapping.Mappings) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 9 with MycatView

use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.

the class MycatValuesJoinRule method onMatch.

@Override
public void onMatch(RelOptRuleCall call) {
    Join join = call.rel(0);
    RelHint lastJoinHint = HintTools.getLastJoinHint(join.getHints());
    if (lastJoinHint != null) {
        if (!"use_values_join".equalsIgnoreCase(lastJoinHint.hintName)) {
            return;
        }
    } else {
        return;
    }
    RelOptCluster cluster = join.getCluster();
    RelNode left = call.rel(1);
    RelNode right = call.rel(2);
    JoinInfo joinInfo = join.analyzeCondition();
    if (!joinInfo.isEqui()) {
        return;
    }
    if (!(right instanceof MycatView)) {
        return;
    }
    MycatView mycatView = (MycatView) right;
    if (mycatView.banPushdown()) {
        return;
    }
    JoinRelType joinType = join.getJoinType();
    switch(joinType) {
        case INNER:
        case SEMI:
        case LEFT:
        case RIGHT:
            break;
        default:
            return;
    }
    if (RelOptUtil.countJoins(mycatView.getRelNode()) > 1) {
        return;
    }
    RexBuilder rexBuilder = MycatCalciteSupport.RexBuilder;
    RelDataTypeFactory typeFactory = cluster.getTypeFactory();
    List<RexNode> leftExprs = new ArrayList<>();
    List<CorrelationId> correlationIds = new ArrayList<>();
    {
        for (Integer leftKey : join.analyzeCondition().leftSet()) {
            CorrelationId correl = cluster.createCorrel();
            correlationIds.add(correl);
            RelDataType type = left.getRowType().getFieldList().get(leftKey).getType();
            RexNode rexNode = rexBuilder.makeCorrel(typeFactory.createSqlType(SqlTypeName.VARCHAR), correl);
            leftExprs.add(rexBuilder.makeCast(type, rexNode));
        }
    }
    switch(mycatView.getDistribution().type()) {
        case PHY:
        case BROADCAST:
            RelBuilder builder = call.builder();
            builder.push(MycatTableLookupValues.create(cluster, left.getRowType(), leftExprs, left.getTraitSet())).push(mycatView.getRelNode()).join(joinType, join.getCondition());
            MycatView view = mycatView.changeTo(builder.build());
            MycatSQLTableLookup mycatSQLTableLookup = new MycatSQLTableLookup(cluster, join.getTraitSet(), left, view, joinType, join.getCondition(), correlationIds, MycatSQLTableLookup.Type.NONE);
            call.transformTo(mycatSQLTableLookup);
            break;
        default:
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelBuilder(org.apache.calcite.tools.RelBuilder) ArrayList(java.util.ArrayList) Join(org.apache.calcite.rel.core.Join) RelDataType(org.apache.calcite.rel.type.RelDataType) RelHint(org.apache.calcite.rel.hint.RelHint) JoinInfo(org.apache.calcite.rel.core.JoinInfo) JoinRelType(org.apache.calcite.rel.core.JoinRelType) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode)

Example 10 with MycatView

use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.

the class PlanImpl method specificSql.

@NotNull
public List<SpecificSql> specificSql(DrdsSqlWithParams drdsSql, int mergeUnionSize) {
    List<SpecificSql> res = new ArrayList<>();
    getMycatRel().accept(new RelShuttleImpl() {

        @Override
        protected RelNode visitChildren(RelNode relNode) {
            List<Each> sqls = new ArrayList<>();
            String parameterizedSql = "";
            if (relNode instanceof MycatView) {
                MycatView view = (MycatView) relNode;
                SqlNode sqlTemplate = view.getSQLTemplate(false);
                ImmutableMultimap<String, SqlString> apply = view.apply(mergeUnionSize, sqlTemplate, AsyncMycatDataContextImpl.getSqlMap(executerContext.getConstantMap(), view, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
                ImmutableMultimap<String, SqlString> stringImmutableMultimap = apply;
                for (Map.Entry<String, SqlString> entry : (stringImmutableMultimap.entries())) {
                    SqlString sqlString = new SqlString(entry.getValue().getDialect(), (Util.toLinux(entry.getValue().getSql())), entry.getValue().getDynamicParameters());
                    sqls.add(new Each(entry.getKey(), sqlString.getSql()));
                }
                if (relNode instanceof MycatView) {
                    parameterizedSql = ((MycatView) relNode).getSql();
                }
                if (relNode instanceof MycatTransientSQLTableScan) {
                    parameterizedSql = ((MycatTransientSQLTableScan) relNode).getSql();
                }
                res.add(new SpecificSql(relNode.getDigest(), parameterizedSql, sqls));
            } else if (relNode instanceof MycatSQLTableLookup) {
                MycatView right = ((MycatSQLTableLookup) relNode).getRight();
                right.accept(this);
            }
            return super.visitChildren(relNode);
        }
    });
    return res;
}
Also used : MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatView(io.mycat.calcite.logical.MycatView) RelNode(org.apache.calcite.rel.RelNode) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) SqlNode(org.apache.calcite.sql.SqlNode) NotNull(org.jetbrains.annotations.NotNull)

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