Search in sources :

Example 1 with MycatLogicTable

use of io.mycat.calcite.table.MycatLogicTable 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 MycatLogicTable

use of io.mycat.calcite.table.MycatLogicTable in project Mycat2 by MyCATApache.

the class VisualTablescanFactory method create.

@Override
public PhysicalPlan create(ComplierContext context) {
    RelOptTable table = tableScan.getRelNode().getTable();
    MycatLogicTable logicTable = table.unwrap(MycatLogicTable.class);
    TableHandler tableHandler = logicTable.getTable();
    Observable<Object[]> tableObservable = context.getTableObservable(tableHandler.getSchemaName(), tableHandler.getTableName());
    MycatRelDataType mycatRelDataTypeByCalcite = tableScan.getMycatRelDataTypeByCalcite();
    Schema schema = toArrowSchema(mycatRelDataTypeByCalcite);
    return ValuesPlan.create(schema, MycatRxJavaUtl.blockingIterable(tableObservable));
}
Also used : TableHandler(io.mycat.TableHandler) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) Schema(org.apache.arrow.vector.types.pojo.Schema) FactoryUtil.toArrowSchema(io.ordinate.engine.factory.FactoryUtil.toArrowSchema) RelOptTable(org.apache.calcite.plan.RelOptTable)

Example 3 with MycatLogicTable

use of io.mycat.calcite.table.MycatLogicTable in project Mycat2 by MyCATApache.

the class MycatImplementor method visit.

@Override
public Result visit(TableScan e) {
    try {
        MycatLogicTable logicTable = e.getTable().unwrap(MycatLogicTable.class);
        if (logicTable != null) {
            String hintText = "";
            if (dialect instanceof MysqlSqlDialect) {
                for (RelHint hint : e.getHints()) {
                    if ("INDEX".equalsIgnoreCase(hint.hintName)) {
                        hintText = "USE INDEX(" + String.join(",", hint.listOptions) + ")";
                    }
                }
            }
            TableHandler tableHandler = logicTable.logicTable();
            SqlNode tableParamSqlNode = new TableParamSqlNode(ImmutableList.of(tableHandler.getSchemaName(), tableHandler.getTableName()), tableHandler.getUniqueName(), hintText);
            return result(tableParamSqlNode, ImmutableList.of(Clause.FROM), e, null);
        }
        return super.visit(e);
    } catch (Throwable e1) {
        LOGGER.error("", e1);
        return null;
    }
}
Also used : TableHandler(io.mycat.TableHandler) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) RelHint(org.apache.calcite.rel.hint.RelHint)

Example 4 with MycatLogicTable

use of io.mycat.calcite.table.MycatLogicTable in project Mycat2 by MyCATApache.

the class DrdsRunnerHelper method convertRoSchemaPlus.

public static SchemaPlus convertRoSchemaPlus(NameMap<SchemaHandler> schemaHandlers) {
    SchemaPlus plus = CalciteSchema.createRootSchema(false).plus();
    List<MycatSchema> schemas = new ArrayList<>();
    for (Map.Entry<String, SchemaHandler> entry : schemaHandlers.entrySet()) {
        String schemaName = entry.getKey();
        SchemaHandler schemaHandler = entry.getValue();
        Map<String, Table> logicTableMap = new HashMap<>();
        for (TableHandler tableHandler : schemaHandler.logicTables().values()) {
            MycatLogicTable logicTable = new MycatLogicTable(tableHandler);
            logicTableMap.put(logicTable.getTable().getTableName(), logicTable);
        }
        MycatSchema schema = MycatSchema.create(schemaName, logicTableMap);
        plus.add(schemaName, schema);
        schemas.add(schema);
    }
    return plus;
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) Table(org.apache.calcite.schema.Table) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) NameMap(io.mycat.util.NameMap)

Example 5 with MycatLogicTable

use of io.mycat.calcite.table.MycatLogicTable in project Mycat2 by MyCATApache.

the class LocalTableScan method getMycatRelDataType.

@Override
public MycatRelDataType getMycatRelDataType() {
    MycatLogicTable mycatTable = getTable().unwrap(MycatLogicTable.class);
    TableHandler tableTable = mycatTable.getTable();
    List<MycatField> mycatFields = tableTable.getColumns().stream().map(c -> c.toMycatField()).collect(Collectors.toList());
    return MycatRelDataType.of(mycatFields);
}
Also used : RelInput(org.apache.calcite.rel.RelInput) TableScan(org.apache.calcite.rel.core.TableScan) TableHandler(io.mycat.TableHandler) RelFactories(org.apache.calcite.rel.core.RelFactories) MycatField(io.mycat.beans.mycat.MycatField) Collectors(java.util.stream.Collectors) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) RelWriter(org.apache.calcite.rel.RelWriter) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelHint(org.apache.calcite.rel.hint.RelHint) Map(java.util.Map) AbstractMycatTable(io.mycat.calcite.table.AbstractMycatTable) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) org.apache.calcite.plan(org.apache.calcite.plan) TableHandler(io.mycat.TableHandler) MycatLogicTable(io.mycat.calcite.table.MycatLogicTable) MycatField(io.mycat.beans.mycat.MycatField)

Aggregations

MycatLogicTable (io.mycat.calcite.table.MycatLogicTable)7 TableHandler (io.mycat.TableHandler)4 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)3 RelOptTable (org.apache.calcite.plan.RelOptTable)3 ImmutableList (com.google.common.collect.ImmutableList)2 MetadataManager (io.mycat.MetadataManager)2 FactoryUtil.toArrowSchema (io.ordinate.engine.factory.FactoryUtil.toArrowSchema)2 Collectors (java.util.stream.Collectors)2 TableScan (org.apache.calcite.rel.core.TableScan)2 RelHint (org.apache.calcite.rel.hint.RelHint)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 ImmutableList.builder (com.google.common.collect.ImmutableList.builder)1 MetaClusterCurrent (io.mycat.MetaClusterCurrent)1 Partition (io.mycat.Partition)1 JdbcRowMetaData (io.mycat.beans.mycat.JdbcRowMetaData)1