Search in sources :

Example 1 with Partition

use of io.mycat.Partition in project Mycat2 by MyCATApache.

the class HBTQueryConvertor method filterFromTable.

public RelNode filterFromTable(FilterFromTableSchema input) {
    List<String> names = input.getNames();
    relBuilder.scan(names);
    TableScan tableScan = (TableScan) relBuilder.peek();
    RelOptTable table = tableScan.getTable();
    relBuilder.as(names.get(names.size() - 1));
    relBuilder.filter(toRex(input.getFilter()));
    Filter build = (Filter) relBuilder.build();
    relBuilder.clear();
    MycatLogicTable mycatTable = table.unwrap(MycatLogicTable.class);
    Distribution distribution = mycatTable.createDistribution();
    Iterable<Partition> dataNodes = distribution.getDataNodes().flatMap(i -> i.values().stream()).collect(Collectors.toList());
    return build.copy(build.getTraitSet(), ImmutableList.of(toPhyTable(mycatTable, dataNodes)));
}
Also used : MetadataManager(io.mycat.MetadataManager) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) MetaClusterCurrent(io.mycat.MetaClusterCurrent) MycatPhysicalTable(io.mycat.calcite.table.MycatPhysicalTable) SneakyThrows(lombok.SneakyThrows) org.apache.calcite.rex(org.apache.calcite.rex) LoggerFactory(org.slf4j.LoggerFactory) BigDecimal(java.math.BigDecimal) CorrelationId(org.apache.calcite.rel.core.CorrelationId) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) RelBuilder(org.apache.calcite.tools.RelBuilder) LocalTime(java.time.LocalTime) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) ByteString(org.apache.calcite.avatica.util.ByteString) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) ModifyFromSql(io.mycat.hbt.ast.modify.ModifyFromSql) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) LocalDate(java.time.LocalDate) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) RelOptTableImpl(org.apache.calcite.prepare.RelOptTableImpl) java.sql(java.sql) TableScan(org.apache.calcite.rel.core.TableScan) Distribution(io.mycat.calcite.rewriter.Distribution) java.util(java.util) io.mycat.hbt.ast.base(io.mycat.hbt.ast.base) LocalDateTime(java.time.LocalDateTime) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Filter(org.apache.calcite.rel.core.Filter) RelOptUtil(org.apache.calcite.plan.RelOptUtil) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) RelOptTable(org.apache.calcite.plan.RelOptTable) Partition(io.mycat.Partition) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.builder(com.google.common.collect.ImmutableList.builder) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) org.apache.calcite.util(org.apache.calcite.util) SqlOperator(org.apache.calcite.sql.SqlOperator) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) RelDataType(org.apache.calcite.rel.type.RelDataType) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) HBTOp(io.mycat.hbt.ast.HBTOp) MycatCalciteSupport(io.mycat.calcite.MycatCalciteSupport) RelNode(org.apache.calcite.rel.RelNode) SqlDialect(org.apache.calcite.sql.SqlDialect) JoinRelType(org.apache.calcite.rel.core.JoinRelType) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) io.mycat.hbt.ast.query(io.mycat.hbt.ast.query) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) TableScan(org.apache.calcite.rel.core.TableScan) Partition(io.mycat.Partition) Filter(org.apache.calcite.rel.core.Filter) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) Distribution(io.mycat.calcite.rewriter.Distribution) ByteString(org.apache.calcite.avatica.util.ByteString) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 2 with Partition

use of io.mycat.Partition in project Mycat2 by MyCATApache.

the class HBTQueryConvertor method toPhyTable.

private RelNode toPhyTable(MycatLogicTable unwrap, Iterable<Partition> dataNodes) {
    int count = 0;
    for (Partition partition : dataNodes) {
        MycatPhysicalTable mycatPhysicalTable = new MycatPhysicalTable(unwrap, partition);
        LogicalTableScan tableScan = LogicalTableScan.create(relBuilder.getCluster(), RelOptTableImpl.create(relBuilder.getRelOptSchema(), unwrap.getRowType(), mycatPhysicalTable, ImmutableList.of(partition.getSchema(), partition.getTable())), ImmutableList.of());
        count++;
        relBuilder.push(tableScan);
    }
    return relBuilder.union(true, count).build();
}
Also used : Partition(io.mycat.Partition) MycatPhysicalTable(io.mycat.calcite.table.MycatPhysicalTable) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan)

Example 3 with Partition

use of io.mycat.Partition in project Mycat2 by MyCATApache.

the class IndexShardingTableVO method toShardingTableConfig.

public ShardingTableConfig toShardingTableConfig() {
    shardingTableConfig.setShardingIndexTables(Collections.emptyMap());
    String sql = this.indexCreateTableSQL.getText();
    String shardingInfoText = this.indexShardingInfo.getText();
    List<List> partitions = new ArrayList<>();
    for (PartitionEntry item : indexPartitionsView.getItems()) {
        Partition partition = item.toPartition();
        String targetName = partition.getTargetName();
        String schema = partition.getSchema();
        String table = partition.getTable();
        Integer dbIndex = partition.getDbIndex();
        Integer tableIndex = partition.getTableIndex();
        Integer index = partition.getIndex();
        partitions.add(Arrays.asList(targetName, schema, table, dbIndex, tableIndex, index));
    }
    ShardingFunction shardingFuntion;
    if (!StringUtil.isEmpty(shardingInfoText)) {
        shardingFuntion = Json.decodeValue(shardingInfoText, ShardingFunction.class);
    } else {
        shardingFuntion = new ShardingFunction();
    }
    shardingTableConfig.setCreateTableSQL(sql);
    shardingTableConfig.setFunction(shardingFuntion);
    shardingTableConfig.setPartition(ShardingBackEndTableInfoConfig.builder().data(partitions).build());
    shardingTableConfig.setShardingIndexTables(Collections.emptyMap());
    return shardingTableConfig;
}
Also used : Partition(io.mycat.Partition) ShardingFunction(io.mycat.config.ShardingFunction)

Example 4 with Partition

use of io.mycat.Partition in project Mycat2 by MyCATApache.

the class CustomRuleFunction method calculateOne.

public Partition calculateOne(Map<String, RangeVariable> values) {
    List<Partition> partitions = calculate(values);
    if (partitions.isEmpty()) {
        throw new IllegalArgumentException("路由计算返回结果个数为0");
    }
    if (partitions.size() != 1) {
        List<Partition> dataNodes2 = calculate(values);
        dataNodes2 = calculate(values);
        throw new IllegalArgumentException("路由计算返回结果个数为" + partitions.size());
    }
    Partition partition = partitions.get(0);
    if (partition == null) {
        throw new IllegalArgumentException("路由计算返回结果为NULL");
    }
    return partitions.get(0);
}
Also used : Partition(io.mycat.Partition)

Example 5 with Partition

use of io.mycat.Partition in project Mycat2 by MyCATApache.

the class CustomRuleFunction method getPartition.

public Partition getPartition(int index) {
    List<Partition> partitions = calculate(Collections.emptyMap());
    Partition maybePartition = partitions.get(index);
    if (maybePartition.getIndex() != null) {
        if (maybePartition.getIndex() == index) {
            return maybePartition;
        } else {
            return partitions.stream().filter(p -> p.getIndex() == index).findFirst().orElse(null);
        }
    }
    return maybePartition;
}
Also used : Partition(io.mycat.Partition)

Aggregations

Partition (io.mycat.Partition)33 MetadataManager (io.mycat.MetadataManager)14 RangeVariable (io.mycat.RangeVariable)14 Test (org.junit.Test)14 ShardingTable (io.mycat.calcite.table.ShardingTable)12 CustomRuleFunction (io.mycat.router.CustomRuleFunction)11 ShardingTableConfig (io.mycat.config.ShardingTableConfig)10 Map (java.util.Map)9 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableList (com.google.common.collect.ImmutableList)3 LocalDate (java.time.LocalDate)3 Collectors (java.util.stream.Collectors)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 ToString (groovy.transform.ToString)2 MycatPhysicalTable (io.mycat.calcite.table.MycatPhysicalTable)2 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)2 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)2 LocalDateTime (java.time.LocalDateTime)2