Search in sources :

Example 1 with TableHandler

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

the class ComplierContext method getTableObservable.

public Observable<Object[]> getTableObservable(String schemaName, String tableName) {
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    TableHandler tableHandler = metadataManager.getTable(schemaName, tableName);
    VisualTableHandler visualTableHandler = (VisualTableHandler) tableHandler;
    return visualTableHandler.scanAll();
}
Also used : MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) VisualTableHandler(io.mycat.prototypeserver.mysql.VisualTableHandler) VisualTableHandler(io.mycat.prototypeserver.mysql.VisualTableHandler)

Example 2 with TableHandler

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

the class PrototypeHandlerImpl method showColumns.

@Override
public List<Object[]> showColumns(SQLShowColumnsStatement statement) {
    ArrayList<Object[]> objects = new ArrayList<>();
    String schemaName = SQLUtils.normalize(statement.getDatabase().getSimpleName());
    String tableName = Optional.ofNullable((SQLExpr) statement.getTable()).map(i -> SQLUtils.normalize(i.toString())).orElse(null);
    if (tableName == null && statement.getLike() != null) {
        String pattern = SQLUtils.normalize(statement.getLike().toString());
        PatternMatcher mysqlPattern = PatternMatcher.createMysqlPattern(pattern, false);
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        SchemaHandler schemaHandler = metadataManager.getSchemaMap().get(schemaName);
        if (schemaHandler == null)
            return Collections.emptyList();
        List<String> tables = schemaHandler.logicTables().values().stream().map(i -> i.getTableName()).collect(Collectors.toList());
        tableName = tables.stream().filter(i -> mysqlPattern.match(i)).findFirst().orElse(null);
    }
    if (tableName == null) {
        return Collections.emptyList();
    }
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    TableHandler table = metadataManager.getTable(schemaName, tableName);
    if (table == null)
        return Collections.emptyList();
    String createTableSQL = table.getCreateTableSQL();
    MySqlCreateTableStatement sqlStatement = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(createTableSQL);
    List<SQLColumnDefinition> columns = new ArrayList<SQLColumnDefinition>();
    List<String> dataTypes = new ArrayList<String>();
    List<String> defaultValues = new ArrayList<String>();
    int name_len = -1, dataType_len = -1, defaultVal_len = 7, extra_len = 5;
    for (SQLTableElement element : sqlStatement.getTableElementList()) {
        if (element instanceof SQLColumnDefinition) {
            SQLColumnDefinition column = (SQLColumnDefinition) element;
            columns.add(column);
            String name = SQLUtils.normalize(column.getName().getSimpleName());
            if (name_len < name.length()) {
                name_len = name.length();
            }
            String dataType = column.getDataType().getName();
            if (column.getDataType().getArguments().size() > 0) {
                dataType += "(";
                for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
                    if (i != 0) {
                        dataType += ",";
                    }
                    SQLExpr arg = column.getDataType().getArguments().get(i);
                    dataType += arg.toString();
                }
                dataType += ")";
            }
            if (dataType_len < dataType.length()) {
                dataType_len = dataType.length();
            }
            dataTypes.add(dataType);
            if (column.getDefaultExpr() == null) {
                defaultValues.add(null);
            } else {
                String defaultVal = SQLUtils.toMySqlString(column.getDefaultExpr());
                if (defaultVal.length() > 2 && defaultVal.charAt(0) == '\'' && defaultVal.charAt(defaultVal.length() - 1) == '\'') {
                    defaultVal = defaultVal.substring(1, defaultVal.length() - 1);
                }
                defaultValues.add(defaultVal);
                if (defaultVal_len < defaultVal.length()) {
                    defaultVal_len = defaultVal.length();
                }
            }
            if (column.isAutoIncrement()) {
                extra_len = "auto_increment".length();
            } else if (column.getOnUpdate() != null) {
                extra_len = "on update CURRENT_TIMESTAMP".length();
            }
        }
    }
    for (int i = 0; i < columns.size(); i++) {
        SQLColumnDefinition column = columns.get(i);
        String name = SQLUtils.normalize(column.getName().getSimpleName());
        String Field = name;
        String Type = dataTypes.get(i);
        String Collation = Optional.ofNullable(column.getCollateExpr()).map(s -> SQLUtils.normalize(s.toString())).orElse(null);
        String Null = column.containsNotNullConstaint() ? "NO" : "YES";
        String Key = sqlStatement.isPrimaryColumn(name) ? "PRI" : sqlStatement.isUNI(name) ? "UNI" : sqlStatement.isMUL(name) ? "MUL" : "";
        String Default = Optional.ofNullable(defaultValues.get(i)).orElse("NULL");
        String Extra = "";
        String Privileges = "select,insert,update,references";
        String Comment = Optional.ofNullable(column.getComment()).map(s -> s.toString()).orElse("");
        if (statement.isFull()) {
            objects.add(new Object[] { Field, Type, Collation, Null, Key, Default, Extra, Privileges, Comment });
        } else {
            objects.add(new Object[] { Field, Type, Null, Key, Default, Extra });
        }
    }
    return objects;
}
Also used : MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) SchemaHandler(io.mycat.calcite.table.SchemaHandler) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) PatternMatcher(org.apache.doris.common.PatternMatcher)

Example 3 with TableHandler

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

the class PrototypeHandlerImpl method showTables.

@Override
public List<Object[]> showTables(SQLShowTablesStatement statement) {
    String schemaName = SQLUtils.normalize(statement.getDatabase().getSimpleName());
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    NameMap<SchemaHandler> schemaMap = metadataManager.getSchemaMap();
    SchemaHandler schemaHandler = schemaMap.get(schemaName);
    if (schemaHandler == null)
        return Collections.emptyList();
    Collection<String> strings = schemaHandler.logicTables().values().stream().map(i -> i.getTableName()).collect(Collectors.toList());
    ;
    SQLExpr like = statement.getLike();
    if (like == null) {
        NameMap<TableHandler> tables = schemaHandler.logicTables();
        strings = tables.keySet();
    } else if (like instanceof SQLTextLiteralExpr) {
        PatternMatcher matcher = PatternMatcher.createMysqlPattern(((SQLTextLiteralExpr) like).getText(), false);
        strings = strings.stream().filter(i -> matcher.match(i)).collect(Collectors.toList());
    }
    if (statement.isFull()) {
        return strings.stream().sorted().map(i -> new Object[] { i, "BASE TABLE" }).collect(Collectors.toList());
    } else {
        return strings.stream().sorted().map(i -> new Object[] { i }).collect(Collectors.toList());
    }
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) MetadataManager(io.mycat.MetadataManager) java.util(java.util) Connection(java.sql.Connection) TableHandler(io.mycat.TableHandler) MetaClusterCurrent(io.mycat.MetaClusterCurrent) LoggerFactory(org.slf4j.LoggerFactory) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Supplier(java.util.function.Supplier) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSet(java.sql.ResultSet) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) DatasourceConfig(io.mycat.config.DatasourceConfig) com.alibaba.druid.sql.dialect.mysql.ast.statement(com.alibaba.druid.sql.dialect.mysql.ast.statement) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) Collectors(java.util.stream.Collectors) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) Statement(java.sql.Statement) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) MysqlVariableService(io.mycat.MysqlVariableService) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) MetadataManager(io.mycat.MetadataManager) TableHandler(io.mycat.TableHandler) SQLTextLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr) PatternMatcher(org.apache.doris.common.PatternMatcher) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 4 with TableHandler

use of io.mycat.TableHandler 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 5 with TableHandler

use of io.mycat.TableHandler 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)

Aggregations

TableHandler (io.mycat.TableHandler)13 MetadataManager (io.mycat.MetadataManager)9 MycatLogicTable (io.mycat.calcite.table.MycatLogicTable)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)3 ShardingTableHandler (io.mycat.router.ShardingTableHandler)3 Collectors (java.util.stream.Collectors)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLLiteralExpr)2 SQLTextLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLTextLiteralExpr)2 com.alibaba.druid.sql.ast.statement (com.alibaba.druid.sql.ast.statement)2 com.alibaba.druid.sql.dialect.mysql.ast.statement (com.alibaba.druid.sql.dialect.mysql.ast.statement)2 MetaClusterCurrent (io.mycat.MetaClusterCurrent)2 MysqlVariableService (io.mycat.MysqlVariableService)2 PrepareExecutor (io.mycat.calcite.PrepareExecutor)2 GlobalTableHandler (io.mycat.calcite.table.GlobalTableHandler)2 NormalTableHandler (io.mycat.calcite.table.NormalTableHandler)2 SchemaHandler (io.mycat.calcite.table.SchemaHandler)2 DatasourceConfig (io.mycat.config.DatasourceConfig)2