Search in sources :

Example 1 with StoragePlugin

use of org.apache.drill.exec.store.StoragePlugin in project drill by axbaretto.

the class DescribeSchemaHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) {
    SqlIdentifier schema = ((SqlDescribeSchema) sqlNode).getSchema();
    SchemaPlus drillSchema = SchemaUtilites.findSchema(config.getConverter().getDefaultSchema(), schema.names);
    if (drillSchema != null) {
        StoragePlugin storagePlugin;
        try {
            storagePlugin = context.getStorage().getPlugin(schema.names.get(0));
        } catch (ExecutionSetupException e) {
            throw new DrillRuntimeException("Failure while retrieving storage plugin", e);
        }
        String properties;
        try {
            final Map configMap = mapper.convertValue(storagePlugin.getConfig(), Map.class);
            if (storagePlugin instanceof FileSystemPlugin) {
                transformWorkspaces(schema.names, configMap);
            }
            properties = mapper.writeValueAsString(configMap);
        } catch (JsonProcessingException e) {
            throw new DrillRuntimeException("Error while trying to convert storage config to json string", e);
        }
        return DirectPlan.createDirectPlan(context, new DescribeSchemaResult(Joiner.on(".").join(schema.names), properties));
    }
    throw UserException.validationError().message(String.format("Invalid schema name [%s]", Joiner.on(".").join(schema.names))).build(logger);
}
Also used : ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException) FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SerializableString(com.fasterxml.jackson.core.SerializableString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SqlDescribeSchema(org.apache.calcite.sql.SqlDescribeSchema) StoragePlugin(org.apache.drill.exec.store.StoragePlugin)

Example 2 with StoragePlugin

use of org.apache.drill.exec.store.StoragePlugin in project drill by apache.

the class DynamicRootSchema method loadSchemaFactory.

/**
 * Loads schema factory(storage plugin) for specified {@code schemaName}
 * @param schemaName the name of the schema
 * @param caseSensitive whether matching for the schema name is case sensitive
 */
private void loadSchemaFactory(String schemaName, boolean caseSensitive) {
    try {
        SchemaPlus schemaPlus = this.plus();
        StoragePlugin plugin = storages.getPlugin(schemaName);
        if (plugin != null) {
            plugin.registerSchemas(schemaConfig, schemaPlus);
            return;
        }
        // Could not find the plugin of schemaName. The schemaName could be `dfs.tmp`, a 2nd level schema under 'dfs'
        List<String> paths = SchemaUtilites.getSchemaPathAsList(schemaName);
        if (paths.size() == 2) {
            plugin = storages.getPlugin(paths.get(0));
            if (plugin == null) {
                return;
            }
            // Looking for the SchemaPlus for the top level (e.g. 'dfs') of schemaName (e.g. 'dfs.tmp')
            SchemaPlus firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
            if (firstLevelSchema == null) {
                // register schema for this storage plugin to 'this'.
                plugin.registerSchemas(schemaConfig, schemaPlus);
                firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
            }
            // Load second level schemas for this storage plugin
            List<SchemaPlus> secondLevelSchemas = new ArrayList<>();
            for (String secondLevelSchemaName : firstLevelSchema.getSubSchemaNames()) {
                secondLevelSchemas.add(firstLevelSchema.getSubSchema(secondLevelSchemaName));
            }
            for (SchemaPlus schema : secondLevelSchemas) {
                org.apache.drill.exec.store.AbstractSchema drillSchema;
                try {
                    drillSchema = schema.unwrap(AbstractSchema.class);
                } catch (ClassCastException e) {
                    throw new RuntimeException(String.format("Schema '%s' is not expected under root schema", schema.getName()));
                }
                SubSchemaWrapper wrapper = new SubSchemaWrapper(drillSchema);
                schemaPlus.add(wrapper.getName(), wrapper);
            }
        }
    } catch (PluginException | IOException ex) {
        logger.warn("Failed to load schema for \"" + schemaName + "\"!", ex);
        // We can't proceed further without a schema, throw a runtime exception.
        UserException.Builder exceptBuilder = UserException.resourceError(ex).message("Failed to load schema for \"" + schemaName + "\"!").addContext(ex.getClass().getName() + ": " + ex.getMessage()).addContext(// Provide hint if it exists
        UserExceptionUtils.getUserHint(ex));
        throw exceptBuilder.build(logger);
    }
}
Also used : SubSchemaWrapper(org.apache.drill.exec.store.SubSchemaWrapper) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StoragePlugin(org.apache.drill.exec.store.StoragePlugin) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) AbstractSchema(org.apache.drill.exec.store.AbstractSchema)

Example 3 with StoragePlugin

use of org.apache.drill.exec.store.StoragePlugin in project drill by apache.

the class DrillStatsTable method readStatistics.

private TableStatistics readStatistics(DrillTable drillTable, Path path) throws IOException {
    final Object selection = drillTable.getSelection();
    if (selection instanceof FormatSelection) {
        StoragePlugin storagePlugin = drillTable.getPlugin();
        FormatSelection formatSelection = (FormatSelection) selection;
        FormatPluginConfig formatConfig = formatSelection.getFormat();
        if (storagePlugin instanceof FileSystemPlugin && (formatConfig instanceof ParquetFormatConfig)) {
            FormatPlugin fmtPlugin = storagePlugin.getFormatPlugin(formatConfig);
            if (fmtPlugin.supportsStatistics()) {
                return fmtPlugin.readStatistics(fs, path);
            }
        }
    }
    return null;
}
Also used : FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) FormatPluginConfig(org.apache.drill.common.logical.FormatPluginConfig) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) ParquetFormatConfig(org.apache.drill.exec.store.parquet.ParquetFormatConfig) StoragePlugin(org.apache.drill.exec.store.StoragePlugin) FormatPlugin(org.apache.drill.exec.store.dfs.FormatPlugin)

Example 4 with StoragePlugin

use of org.apache.drill.exec.store.StoragePlugin in project drill by axbaretto.

the class DynamicRootSchema method loadSchemaFactory.

/**
 * load schema factory(storage plugin) for schemaName
 * @param schemaName
 * @param caseSensitive
 */
public void loadSchemaFactory(String schemaName, boolean caseSensitive) {
    try {
        SchemaPlus thisPlus = this.plus();
        StoragePlugin plugin = getSchemaFactories().getPlugin(schemaName);
        if (plugin != null) {
            plugin.registerSchemas(schemaConfig, thisPlus);
            return;
        }
        // Could not find the plugin of schemaName. The schemaName could be `dfs.tmp`, a 2nd level schema under 'dfs'
        String[] paths = schemaName.split("\\.");
        if (paths.length == 2) {
            plugin = getSchemaFactories().getPlugin(paths[0]);
            if (plugin == null) {
                return;
            }
            // Found the storage plugin for first part(e.g. 'dfs') of schemaName (e.g. 'dfs.tmp')
            // register schema for this storage plugin to 'this'.
            plugin.registerSchemas(schemaConfig, thisPlus);
            // Load second level schemas for this storage plugin
            final SchemaPlus firstlevelSchema = thisPlus.getSubSchema(paths[0]);
            final List<SchemaPlus> secondLevelSchemas = Lists.newArrayList();
            for (String secondLevelSchemaName : firstlevelSchema.getSubSchemaNames()) {
                secondLevelSchemas.add(firstlevelSchema.getSubSchema(secondLevelSchemaName));
            }
            for (SchemaPlus schema : secondLevelSchemas) {
                org.apache.drill.exec.store.AbstractSchema drillSchema;
                try {
                    drillSchema = schema.unwrap(org.apache.drill.exec.store.AbstractSchema.class);
                } catch (ClassCastException e) {
                    throw new RuntimeException(String.format("Schema '%s' is not expected under root schema", schema.getName()));
                }
                SubSchemaWrapper wrapper = new SubSchemaWrapper(drillSchema);
                thisPlus.add(wrapper.getName(), wrapper);
            }
        }
    } catch (ExecutionSetupException | IOException ex) {
        logger.warn("Failed to load schema for \"" + schemaName + "\"!", ex);
    }
}
Also used : ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException) SubSchemaWrapper(org.apache.drill.exec.store.SubSchemaWrapper) SchemaPlus(org.apache.calcite.schema.SchemaPlus) IOException(java.io.IOException) StoragePlugin(org.apache.drill.exec.store.StoragePlugin) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema)

Example 5 with StoragePlugin

use of org.apache.drill.exec.store.StoragePlugin in project drill by axbaretto.

the class DynamicRootSchema method loadSchemaFactory.

/**
 * load schema factory(storage plugin) for schemaName
 * @param schemaName
 * @param caseSensitive
 */
public void loadSchemaFactory(String schemaName, boolean caseSensitive) {
    try {
        SchemaPlus thisPlus = this.plus();
        StoragePlugin plugin = getSchemaFactories().getPlugin(schemaName);
        if (plugin != null) {
            plugin.registerSchemas(schemaConfig, thisPlus);
            return;
        }
        // Could not find the plugin of schemaName. The schemaName could be `dfs.tmp`, a 2nd level schema under 'dfs'
        String[] paths = schemaName.split("\\.");
        if (paths.length == 2) {
            plugin = getSchemaFactories().getPlugin(paths[0]);
            if (plugin == null) {
                return;
            }
            // Found the storage plugin for first part(e.g. 'dfs') of schemaName (e.g. 'dfs.tmp')
            // register schema for this storage plugin to 'this'.
            plugin.registerSchemas(schemaConfig, thisPlus);
            // Load second level schemas for this storage plugin
            final SchemaPlus firstlevelSchema = thisPlus.getSubSchema(paths[0]);
            final List<SchemaPlus> secondLevelSchemas = Lists.newArrayList();
            for (String secondLevelSchemaName : firstlevelSchema.getSubSchemaNames()) {
                secondLevelSchemas.add(firstlevelSchema.getSubSchema(secondLevelSchemaName));
            }
            for (SchemaPlus schema : secondLevelSchemas) {
                org.apache.drill.exec.store.AbstractSchema drillSchema;
                try {
                    drillSchema = schema.unwrap(org.apache.drill.exec.store.AbstractSchema.class);
                } catch (ClassCastException e) {
                    throw new RuntimeException(String.format("Schema '%s' is not expected under root schema", schema.getName()));
                }
                SubSchemaWrapper wrapper = new SubSchemaWrapper(drillSchema);
                thisPlus.add(wrapper.getName(), wrapper);
            }
        }
    } catch (ExecutionSetupException | IOException ex) {
        logger.warn("Failed to load schema for \"" + schemaName + "\"!", ex);
    }
}
Also used : ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException) SubSchemaWrapper(org.apache.drill.exec.store.SubSchemaWrapper) SchemaPlus(org.apache.calcite.schema.SchemaPlus) IOException(java.io.IOException) StoragePlugin(org.apache.drill.exec.store.StoragePlugin) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema)

Aggregations

StoragePlugin (org.apache.drill.exec.store.StoragePlugin)6 SchemaPlus (org.apache.calcite.schema.SchemaPlus)5 IOException (java.io.IOException)3 ExecutionSetupException (org.apache.drill.common.exceptions.ExecutionSetupException)3 SubSchemaWrapper (org.apache.drill.exec.store.SubSchemaWrapper)3 FileSystemPlugin (org.apache.drill.exec.store.dfs.FileSystemPlugin)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SerializableString (com.fasterxml.jackson.core.SerializableString)2 Map (java.util.Map)2 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)2 SqlDescribeSchema (org.apache.calcite.sql.SqlDescribeSchema)2 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)2 PluginException (org.apache.drill.exec.store.StoragePluginRegistry.PluginException)2 ArrayList (java.util.ArrayList)1 FormatPluginConfig (org.apache.drill.common.logical.FormatPluginConfig)1 FormatPlugin (org.apache.drill.exec.store.dfs.FormatPlugin)1 FormatSelection (org.apache.drill.exec.store.dfs.FormatSelection)1 ParquetFormatConfig (org.apache.drill.exec.store.parquet.ParquetFormatConfig)1