Search in sources :

Example 11 with SchemaPlus

use of org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class SchemaTreeProvider method createRootSchema.

/**
   * Create and return a SchemaTree with given <i>schemaConfig</i>.
   * @param schemaConfig
   * @return
   */
public SchemaPlus createRootSchema(SchemaConfig schemaConfig) {
    try {
        final SchemaPlus rootSchema = SimpleCalciteSchema.createRootSchema(false);
        dContext.getSchemaFactory().registerSchemas(schemaConfig, rootSchema);
        schemaTreesToClose.add(rootSchema);
        return rootSchema;
    } catch (IOException e) {
        // We can't proceed further without a schema, throw a runtime exception.
        throw UserException.resourceError(e).message("Failed to create schema tree.").build(logger);
    }
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) IOException(java.io.IOException)

Example 12 with SchemaPlus

use of org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class QueryContext method getNewDefaultSchema.

/**
   * Return reference to default schema instance in a schema tree. Each {@link org.apache.calcite.schema.SchemaPlus}
   * instance can refer to its parent and its children. From the returned reference to default schema instance,
   * clients can traverse the entire schema tree and know the default schema where to look up the tables first.
   *
   * @return Reference to default schema instance in a schema tree.
   */
public SchemaPlus getNewDefaultSchema() {
    final SchemaPlus rootSchema = getRootSchema();
    final SchemaPlus defaultSchema = session.getDefaultSchema(rootSchema);
    if (defaultSchema == null) {
        return rootSchema;
    }
    return defaultSchema;
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 13 with SchemaPlus

use of org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class UserSession method setDefaultSchemaPath.

/**
   * Update the schema path for the session.
   * @param newDefaultSchemaPath New default schema path to set. It could be relative to the current default schema or
   *                             absolute schema.
   * @param currentDefaultSchema Current default schema.
   * @throws ValidationException If the given default schema path is invalid in current schema tree.
   */
public void setDefaultSchemaPath(String newDefaultSchemaPath, SchemaPlus currentDefaultSchema) throws ValidationException {
    final List<String> newDefaultPathAsList = Lists.newArrayList(newDefaultSchemaPath.split("\\."));
    SchemaPlus newDefault;
    // First try to find the given schema relative to the current default schema.
    newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
    if (newDefault == null) {
        // If we fail to find the schema relative to current default schema, consider the given new default schema path as
        // absolute schema path.
        newDefault = SchemaUtilites.findSchema(currentDefaultSchema, newDefaultPathAsList);
    }
    if (newDefault == null) {
        SchemaUtilites.throwSchemaNotFoundException(currentDefaultSchema, newDefaultSchemaPath);
    }
    properties.setProperty(DrillProperties.SCHEMA, SchemaUtilites.getSchemaPath(newDefault));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 14 with SchemaPlus

use of org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class DescribeTableHandler method rewrite.

/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
    SqlDescribeTable node = unwrap(sqlNode, SqlDescribeTable.class);
    try {
        List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
        SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_COLUMNS), null, SqlParserPos.ZERO, null);
        final SqlIdentifier table = node.getTable();
        final SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
        final List<String> schemaPathGivenInCmd = Util.skipLast(table.names);
        final SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
        final String charset = Util.getDefaultCharset().name();
        if (schema == null) {
            SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.SCHEMA_PATH_JOINER.join(schemaPathGivenInCmd));
        }
        if (SchemaUtilites.isRootSchema(schema)) {
            throw UserException.validationError().message("No schema selected.").build(logger);
        }
        final String tableName = Util.last(table.names);
        // find resolved schema path
        final String schemaPath = SchemaUtilites.unwrapAsDrillSchemaInstance(schema).getFullSchemaName();
        if (schema.getTable(tableName) == null) {
            throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
        }
        SqlNode schemaCondition = null;
        if (!SchemaUtilites.isRootSchema(schema)) {
            schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, charset, SqlParserPos.ZERO));
        }
        SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, charset, SqlParserPos.ZERO));
        where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
        SqlNode columnFilter = null;
        if (node.getColumn() != null) {
            columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString(), charset, SqlParserPos.ZERO));
        } else if (node.getColumnQualifier() != null) {
            columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, node.getColumnQualifier());
        }
        where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
        return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
    } catch (Exception ex) {
        throw UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlDescribeTable(org.apache.drill.exec.planner.sql.parser.SqlDescribeTable) UserException(org.apache.drill.common.exceptions.UserException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 15 with SchemaPlus

use of org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class JdbcStoragePlugin method registerSchemas.

@Override
public void registerSchemas(SchemaConfig config, SchemaPlus parent) {
    JdbcCatalogSchema schema = new JdbcCatalogSchema(name);
    SchemaPlus holder = parent.add(name, schema);
    schema.setHolder(holder);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)33 Table (org.apache.calcite.schema.Table)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)6 DruidOperatorTable (io.druid.sql.calcite.planner.DruidOperatorTable)5 PlannerConfig (io.druid.sql.calcite.planner.PlannerConfig)5 PlannerFactory (io.druid.sql.calcite.planner.PlannerFactory)5 StreamableTable (org.apache.calcite.schema.StreamableTable)5 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 SqlNode (org.apache.calcite.sql.SqlNode)4 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)4 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)4 TridentRel (org.apache.storm.sql.planner.trident.rel.TridentRel)4 Before (org.junit.Before)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CompilerUtil (org.apache.storm.sql.compiler.CompilerUtil)3 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)2 UserSession (org.apache.drill.exec.rpc.user.UserSession)2 DrillFileSystem (org.apache.drill.exec.store.dfs.DrillFileSystem)2