Search in sources :

Example 1 with MycatProject

use of io.mycat.calcite.physical.MycatProject in project Mycat2 by MyCATApache.

the class MycatView method createMycatProject.

public static MycatRel createMycatProject(RelNode indexTableScan, List<String> indexColumns, boolean nullable) {
    RelDataType rowType = indexTableScan.getRowType();
    ArrayList<Integer> ints = new ArrayList<>();
    for (String indexColumn : indexColumns) {
        ints.add(rowType.getField(indexColumn, false, false).getIndex());
    }
    RelNode project = RelOptUtil.createProject(indexTableScan, ints);
    if (project instanceof LogicalProject) {
        List<RexNode> projects = ((Project) project).getProjects();
        // if (nullable){
        // RexBuilder rexBuilder = MycatCalciteSupport.RexBuilder;
        // for (RexNode rexNode : projects) {
        // rexBuilder.makeNotNull(rexNode);
        // }
        // 
        // }
        project = MycatProject.create(project.getInput(0), projects, project.getRowType());
    }
    MycatProject mycatProject = (MycatProject) project;
    if (mycatProject.getInput() instanceof MycatView) {
        MycatView mycatProjectInput = (MycatView) mycatProject.getInput();
        return mycatProjectInput.changeTo(mycatProject.copy(mycatProject.getTraitSet(), ImmutableList.of(mycatProjectInput.getRelNode())));
    }
    return (MycatRel) project;
}
Also used : MycatProject(io.mycat.calcite.physical.MycatProject) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlString(org.apache.calcite.sql.util.SqlString) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) MycatProject(io.mycat.calcite.physical.MycatProject) LogicalProject(org.apache.calcite.rel.logical.LogicalProject)

Example 2 with MycatProject

use of io.mycat.calcite.physical.MycatProject in project Mycat2 by MyCATApache.

the class MycatView method produceIndexViews.

public static List<RelNode> produceIndexViews(TableScan tableScan, final RexNode wholeCondition, List<Integer> projects, RelDataType orginalRowType, String indexName) {
    DrdsSqlCompiler drdsSqlCompiler = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
    RelOptCluster cluster = tableScan.getCluster();
    RelBuilder relBuilder = MycatCalciteSupport.relBuilderFactory.create(cluster, drdsSqlCompiler.getCatalogReader());
    ShardingTable shardingTable = (ShardingTable) tableScan.getTable().unwrap(MycatLogicTable.class).getTable();
    List<ShardingIndexTable> indexTables = shardingTable.getIndexTables();
    String[] shardingKeys = shardingTable.getLogicTable().getShardingKeys().toArray(new String[] {});
    ArrayList<RelNode> tableArrayList = new ArrayList<>(indexTables.size());
    MycatView primaryTableScan;
    for (ShardingIndexTable indexTable : indexTables) {
        if (indexName != null && !indexName.equalsIgnoreCase(indexTable.getIndexName())) {
            continue;
        }
        ProjectIndexMapping indexMapping = project(indexTable, projects);
        boolean indexOnlyScan = !indexMapping.needFactTable();
        final IndexCondition indexCondition;
        if (wholeCondition != null) {
            PredicateAnalyzer predicateAnalyzer = new PredicateAnalyzer(indexTable.keyMetas(), shardingTable.getColumns().stream().map(i -> i.getColumnName()).collect(Collectors.toList()));
            Map<QueryType, List<IndexCondition>> queryTypeListMap = predicateAnalyzer.translateMatch(wholeCondition);
            if (queryTypeListMap.isEmpty())
                return Collections.emptyList();
            List<IndexCondition> next = queryTypeListMap.values().stream().filter(i -> i != null).iterator().next().stream().filter(i -> i != null).collect(Collectors.toList());
            indexCondition = next.get(0);
            primaryTableScan = MycatView.ofCondition(LocalFilter.create(wholeCondition, LocalTableScan.create((TableScan) relBuilder.scan(shardingTable.getSchemaName(), shardingTable.getTableName()).build())), Distribution.of(shardingTable), wholeCondition);
        } else {
            continue;
        }
        RelNode indexTableView = null;
        LocalFilter localFilter = null;
        RexNode pushdownCondition = null;
        LocalTableScan localTableScan = LocalTableScan.create((TableScan) relBuilder.scan(indexTable.getSchemaName(), indexTable.getTableName()).build());
        switch(indexCondition.getQueryType()) {
            case PK_POINT_QUERY:
                List<String> indexColumnNames = indexCondition.getIndexColumnNames();
                RexNode rexNode = indexCondition.getPushDownRexNodeList().get(0);
                relBuilder.push(localTableScan);
                List<RexNode> pushDownConditions = new ArrayList<>();
                for (String indexColumnName : indexColumnNames) {
                    pushDownConditions.add(relBuilder.equals(relBuilder.field(indexColumnName), rexNode));
                }
                pushdownCondition = RexUtil.composeConjunction(relBuilder.getRexBuilder(), pushDownConditions);
                RelNode build = relBuilder.build();
                localFilter = LocalFilter.create(LogicalFilter.create(localTableScan, pushdownCondition), localTableScan);
                indexTableView = localFilter;
                break;
            case PK_RANGE_QUERY:
            case PK_FULL_SCAN:
                continue;
        }
        if (indexOnlyScan) {
            List<Integer> newProject = getProjectIntList(projects, tableScan.deriveRowType(), indexTableView.getRowType());
            MycatView view = MycatView.ofCondition(LocalProject.create((Project) RelOptUtil.createProject(indexTableView, newProject), indexTableView), Distribution.of(indexTable), pushdownCondition);
            tableArrayList.add(view);
            continue;
        } else {
            indexTableView = MycatView.ofCondition(indexTableView, Distribution.of(indexTable), pushdownCondition);
            RelNode leftProject = createMycatProject(indexTableView, indexMapping.getIndexColumns());
            RelNode rightProject = createMycatProject(primaryTableScan, indexMapping.getFactColumns());
            Join relNode = (Join) relBuilder.push(leftProject).push(rightProject).join(JoinRelType.INNER, shardingKeys).build();
            relNode = MycatHashJoin.create(relNode.getTraitSet(), ImmutableList.of(), leftProject, rightProject, relNode.getCondition(), relNode.getJoinType());
            MycatRel mycatProject = createMycatProject(relNode, getProjectStringList(projects, tableScan.getRowType()));
            if (RelOptUtil.areRowTypesEqual(orginalRowType, mycatProject.getRowType(), false)) {
                tableArrayList.add(mycatProject);
            }
            continue;
        }
    }
    return (List) tableArrayList;
}
Also used : io.mycat(io.mycat) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) org.apache.calcite.rex(org.apache.calcite.rex) JavaRowFormat(org.apache.calcite.adapter.enumerable.JavaRowFormat) PhysTypeImpl(org.apache.calcite.adapter.enumerable.PhysTypeImpl) EnumerableDefaults(org.apache.calcite.linq4j.EnumerableDefaults) SqlNode(org.apache.calcite.sql.SqlNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RxBuiltInMethodImpl(org.apache.calcite.util.RxBuiltInMethodImpl) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) PhysType(org.apache.calcite.adapter.enumerable.PhysType) MycatMergeSort(io.mycat.calcite.physical.MycatMergeSort) Function1(org.apache.calcite.linq4j.function.Function1) SqlKind(org.apache.calcite.sql.SqlKind) DataContext(org.apache.calcite.DataContext) Collectors(java.util.stream.Collectors) QueryType(io.mycat.querycondition.QueryType) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) Type(java.lang.reflect.Type) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PredicateAnalyzer(io.mycat.calcite.rewriter.PredicateAnalyzer) SqlString(org.apache.calcite.sql.util.SqlString) NotNull(org.jetbrains.annotations.NotNull) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RelOptTableImpl(org.apache.calcite.prepare.RelOptTableImpl) Iterables(com.google.common.collect.Iterables) Distribution(io.mycat.calcite.rewriter.Distribution) java.util(java.util) io.mycat.calcite.localrel(io.mycat.calcite.localrel) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) Iterators(com.google.common.collect.Iterators) ParamHolder(io.mycat.calcite.spm.ParamHolder) ImmutableList(com.google.common.collect.ImmutableList) Observable(io.reactivex.rxjava3.core.Observable) Pair(org.apache.calcite.util.Pair) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) Queryable(org.apache.calcite.linq4j.Queryable) io.mycat.calcite.table(io.mycat.calcite.table) RelDataType(org.apache.calcite.rel.type.RelDataType) org.apache.calcite.rel.core(org.apache.calcite.rel.core) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) Linq4j(org.apache.calcite.linq4j.Linq4j) io.mycat.calcite(io.mycat.calcite) MycatProject(io.mycat.calcite.physical.MycatProject) Enumerable(org.apache.calcite.linq4j.Enumerable) IndexCondition(io.mycat.calcite.rewriter.IndexCondition) MycatHashJoin(io.mycat.calcite.physical.MycatHashJoin) SqlDialect(org.apache.calcite.sql.SqlDialect) org.apache.calcite.linq4j.tree(org.apache.calcite.linq4j.tree) org.apache.calcite.rel(org.apache.calcite.rel) org.apache.calcite.plan(org.apache.calcite.plan) SqlString(org.apache.calcite.sql.util.SqlString) ImmutableList(com.google.common.collect.ImmutableList) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RelBuilder(org.apache.calcite.tools.RelBuilder) MycatHashJoin(io.mycat.calcite.physical.MycatHashJoin) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) MycatProject(io.mycat.calcite.physical.MycatProject) PredicateAnalyzer(io.mycat.calcite.rewriter.PredicateAnalyzer) IndexCondition(io.mycat.calcite.rewriter.IndexCondition) QueryType(io.mycat.querycondition.QueryType)

Example 3 with MycatProject

use of io.mycat.calcite.physical.MycatProject in project Mycat2 by MyCATApache.

the class SQLRBORewriter method splitAggregate.

private static Optional<RelNode> splitAggregate(MycatView viewNode, Aggregate aggregate) {
    try {
        AggregatePushContext aggregateContext = AggregatePushContext.split(aggregate);
        MycatView newView = viewNode.changeTo(LogicalAggregate.create(viewNode.getRelNode(), aggregate.getHints(), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregateContext.getPartialAggregateCallList()));
        LogicalAggregate globalAggregateRelNode = LogicalAggregate.create(newView, aggregate.getHints(), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregateContext.getGlobalAggregateCallList());
        MycatProject projectRelNode = MycatProject.create(globalAggregateRelNode, aggregateContext.getProjectExprList(), aggregate.getRowType());
        return RexUtil.isIdentity(projectRelNode.getProjects(), projectRelNode.getInput().getRowType()) ? Optional.of(globalAggregateRelNode) : Optional.of(projectRelNode);
    } catch (Throwable throwable) {
        LOGGER.debug("", throwable);
    }
    return Optional.empty();
}
Also used : MycatView(io.mycat.calcite.logical.MycatView) MycatProject(io.mycat.calcite.physical.MycatProject)

Aggregations

MycatProject (io.mycat.calcite.physical.MycatProject)3 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)2 LogicalProject (org.apache.calcite.rel.logical.LogicalProject)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 SqlString (org.apache.calcite.sql.util.SqlString)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 io.mycat (io.mycat)1 io.mycat.calcite (io.mycat.calcite)1 io.mycat.calcite.localrel (io.mycat.calcite.localrel)1 MycatView (io.mycat.calcite.logical.MycatView)1 MycatHashJoin (io.mycat.calcite.physical.MycatHashJoin)1 MycatMergeSort (io.mycat.calcite.physical.MycatMergeSort)1 Distribution (io.mycat.calcite.rewriter.Distribution)1 IndexCondition (io.mycat.calcite.rewriter.IndexCondition)1 PredicateAnalyzer (io.mycat.calcite.rewriter.PredicateAnalyzer)1 ParamHolder (io.mycat.calcite.spm.ParamHolder)1 io.mycat.calcite.table (io.mycat.calcite.table)1