Search in sources :

Example 16 with Table

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

the class RefreshMetadataHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    final SqlRefreshMetadata refreshTable = unwrap(sqlNode, SqlRefreshMetadata.class);
    try {
        final SchemaPlus schema = findSchema(config.getConverter().getDefaultSchema(), refreshTable.getSchemaPath());
        if (schema == null) {
            return direct(false, "Storage plugin or workspace does not exist [%s]", SchemaUtilites.SCHEMA_PATH_JOINER.join(refreshTable.getSchemaPath()));
        }
        final String tableName = refreshTable.getName();
        if (tableName.contains("*") || tableName.contains("?")) {
            return direct(false, "Glob path %s not supported for metadata refresh", tableName);
        }
        final Table table = schema.getTable(tableName);
        if (table == null) {
            return direct(false, "Table %s does not exist.", tableName);
        }
        if (!(table instanceof DrillTable)) {
            return notSupported(tableName);
        }
        final DrillTable drillTable = (DrillTable) table;
        final Object selection = drillTable.getSelection();
        if (!(selection instanceof FormatSelection)) {
            return notSupported(tableName);
        }
        FormatSelection formatSelection = (FormatSelection) selection;
        FormatPluginConfig formatConfig = formatSelection.getFormat();
        if (!((formatConfig instanceof ParquetFormatConfig) || ((formatConfig instanceof NamedFormatPluginConfig) && ((NamedFormatPluginConfig) formatConfig).name.equals("parquet")))) {
            return notSupported(tableName);
        }
        FileSystemPlugin plugin = (FileSystemPlugin) drillTable.getPlugin();
        DrillFileSystem fs = new DrillFileSystem(plugin.getFormatPlugin(formatSelection.getFormat()).getFsConf());
        String selectionRoot = formatSelection.getSelection().selectionRoot;
        if (!fs.getFileStatus(new Path(selectionRoot)).isDirectory()) {
            return notSupported(tableName);
        }
        if (!(formatConfig instanceof ParquetFormatConfig)) {
            formatConfig = new ParquetFormatConfig();
        }
        Metadata.createMeta(fs, selectionRoot, (ParquetFormatConfig) formatConfig);
        return direct(true, "Successfully updated metadata for table %s.", tableName);
    } catch (Exception e) {
        logger.error("Failed to update metadata for table '{}'", refreshTable.getName(), e);
        return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) Table(org.apache.calcite.schema.Table) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) SqlRefreshMetadata(org.apache.drill.exec.planner.sql.parser.SqlRefreshMetadata) IOException(java.io.IOException) ValidationException(org.apache.calcite.tools.ValidationException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) DrillFileSystem(org.apache.drill.exec.store.dfs.DrillFileSystem) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) FormatPluginConfig(org.apache.drill.common.logical.FormatPluginConfig) ParquetFormatConfig(org.apache.drill.exec.store.parquet.ParquetFormatConfig)

Example 17 with Table

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

the class DropTableHandler method getPlan.

/**
   * Function resolves the schema and invokes the drop method
   * (while IF EXISTS statement is used function invokes the drop method only if table exists).
   * Raises an exception if the schema is immutable.
   *
   * @param sqlNode - SqlDropTable (SQL parse tree of drop table [if exists] query)
   * @return - Single row indicating drop succeeded or table is not found while IF EXISTS statement is used,
   * raise exception otherwise
   */
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException {
    SqlDropTable dropTableNode = ((SqlDropTable) sqlNode);
    String originalTableName = dropTableNode.getName();
    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    List<String> tableSchema = dropTableNode.getSchema();
    DrillConfig drillConfig = context.getConfig();
    UserSession session = context.getSession();
    AbstractSchema temporarySchema = resolveToTemporarySchema(tableSchema, defaultSchema, drillConfig);
    boolean isTemporaryTable = session.isTemporaryTable(temporarySchema, drillConfig, originalTableName);
    if (isTemporaryTable) {
        session.removeTemporaryTable(temporarySchema, originalTableName, drillConfig);
    } else {
        AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, tableSchema);
        Table tableToDrop = SqlHandlerUtil.getTableFromSchema(drillSchema, originalTableName);
        if (tableToDrop == null || tableToDrop.getJdbcTableType() != Schema.TableType.TABLE) {
            if (dropTableNode.checkTableExistence()) {
                return DirectPlan.createDirectPlan(context, false, String.format("Table [%s] not found", originalTableName));
            } else {
                throw UserException.validationError().message("Table [%s] not found", originalTableName).build(logger);
            }
        }
        SqlHandlerUtil.dropTableFromSchema(drillSchema, originalTableName);
    }
    String message = String.format("%s [%s] dropped", isTemporaryTable ? "Temporary table" : "Table", originalTableName);
    logger.info(message);
    return DirectPlan.createDirectPlan(context, true, message);
}
Also used : Table(org.apache.calcite.schema.Table) SqlDropTable(org.apache.drill.exec.planner.sql.parser.SqlDropTable) DrillConfig(org.apache.drill.common.config.DrillConfig) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) UserSession(org.apache.drill.exec.rpc.user.UserSession) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlDropTable(org.apache.drill.exec.planner.sql.parser.SqlDropTable)

Example 18 with Table

use of org.apache.calcite.schema.Table in project lucene-solr by apache.

the class SolrSchema method getTableMap.

@Override
protected Map<String, Table> getTableMap() {
    String zk = this.properties.getProperty("zk");
    try (CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder().withZkHost(zk).build()) {
        cloudSolrClient.connect();
        ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
        ClusterState clusterState = zkStateReader.getClusterState();
        final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();
        for (String collection : clusterState.getCollectionsMap().keySet()) {
            builder.put(collection, new SolrTable(this, collection));
        }
        Aliases aliases = zkStateReader.getAliases();
        if (aliases.collectionAliasSize() > 0) {
            for (Map.Entry<String, String> alias : aliases.getCollectionAliasMap().entrySet()) {
                builder.put(alias.getKey(), new SolrTable(this, alias.getValue()));
            }
        }
        return builder.build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) Table(org.apache.calcite.schema.Table) Aliases(org.apache.solr.common.cloud.Aliases) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

Table (org.apache.calcite.schema.Table)18 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)8 SchemaPlus (org.apache.calcite.schema.SchemaPlus)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 StreamableTable (org.apache.calcite.schema.StreamableTable)6 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)4 TridentRel (org.apache.storm.sql.planner.trident.rel.TridentRel)4 ArrayList (java.util.ArrayList)3 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelNode (org.apache.calcite.rel.RelNode)3 RelDataType (org.apache.calcite.rel.type.RelDataType)3 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)3 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)3 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)3 CompilerUtil (org.apache.storm.sql.compiler.CompilerUtil)3 DruidTable (io.druid.sql.calcite.table.DruidTable)2 IOException (java.io.IOException)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)2 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)2