Search in sources :

Example 1 with SqlDescribeSchema

use of org.apache.calcite.sql.SqlDescribeSchema 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 SqlDescribeSchema

use of org.apache.calcite.sql.SqlDescribeSchema in project drill by axbaretto.

the class DrillSqlWorker method getQueryPlan.

/**
 * Converts sql query string into query physical plan.
 *
 * @param context query context
 * @param sql sql query
 * @param textPlan text plan
 * @return query physical plan
 */
private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Pointer<String> textPlan) throws ForemanSetupException {
    final SqlConverter parser = new SqlConverter(context);
    injector.injectChecked(context.getExecutionControls(), "sql-parsing", ForemanSetupException.class);
    final SqlNode sqlNode = parser.parse(sql);
    final AbstractSqlHandler handler;
    final SqlHandlerConfig config = new SqlHandlerConfig(context, parser);
    switch(sqlNode.getKind()) {
        case EXPLAIN:
            handler = new ExplainHandler(config, textPlan);
            break;
        case SET_OPTION:
            handler = new SetOptionHandler(context);
            break;
        case DESCRIBE_TABLE:
            if (sqlNode instanceof DrillSqlDescribeTable) {
                handler = new DescribeTableHandler(config);
                break;
            }
        case DESCRIBE_SCHEMA:
            if (sqlNode instanceof SqlDescribeSchema) {
                handler = new DescribeSchemaHandler(config);
                break;
            }
        case OTHER:
            if (sqlNode instanceof SqlCreateTable) {
                handler = ((DrillSqlCall) sqlNode).getSqlHandler(config, textPlan);
                break;
            }
            if (sqlNode instanceof DrillSqlCall) {
                handler = ((DrillSqlCall) sqlNode).getSqlHandler(config);
                break;
            }
        // fallthrough
        default:
            handler = new DefaultSqlHandler(config, textPlan);
    }
    try {
        return handler.getPlan(sqlNode);
    } catch (ValidationException e) {
        String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        throw UserException.validationError(e).message(errorMessage).build(logger);
    } catch (AccessControlException e) {
        throw UserException.permissionError(e).build(logger);
    } catch (SqlUnsupportedException e) {
        throw UserException.unsupportedError(e).build(logger);
    } catch (IOException | RelConversionException e) {
        throw new QueryInputException("Failure handling SQL.", e);
    }
}
Also used : DefaultSqlHandler(org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler) ExplainHandler(org.apache.drill.exec.planner.sql.handlers.ExplainHandler) ValidationException(org.apache.calcite.tools.ValidationException) DescribeTableHandler(org.apache.drill.exec.planner.sql.handlers.DescribeTableHandler) SetOptionHandler(org.apache.drill.exec.planner.sql.handlers.SetOptionHandler) AccessControlException(org.apache.hadoop.security.AccessControlException) SqlHandlerConfig(org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig) IOException(java.io.IOException) SqlDescribeSchema(org.apache.calcite.sql.SqlDescribeSchema) DescribeSchemaHandler(org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler) SqlCreateTable(org.apache.drill.exec.planner.sql.parser.SqlCreateTable) RelConversionException(org.apache.calcite.tools.RelConversionException) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) AbstractSqlHandler(org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler) SqlUnsupportedException(org.apache.drill.exec.work.foreman.SqlUnsupportedException) DrillSqlCall(org.apache.drill.exec.planner.sql.parser.DrillSqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlDescribeSchema

use of org.apache.calcite.sql.SqlDescribeSchema in project drill by apache.

the class DrillSqlWorker method getQueryPlan.

/**
 * Converts sql query string into query physical plan.
 *
 * @param context query context
 * @param sql sql query
 * @param textPlan text plan
 * @return query physical plan
 */
private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Pointer<String> textPlan) throws ForemanSetupException, RelConversionException, IOException, ValidationException {
    final SqlConverter parser = new SqlConverter(context);
    injector.injectChecked(context.getExecutionControls(), "sql-parsing", ForemanSetupException.class);
    final SqlNode sqlNode = checkAndApplyAutoLimit(parser, context, sql);
    final AbstractSqlHandler handler;
    final SqlHandlerConfig config = new SqlHandlerConfig(context, parser);
    switch(sqlNode.getKind()) {
        case EXPLAIN:
            handler = new ExplainHandler(config, textPlan);
            context.setSQLStatementType(SqlStatementType.EXPLAIN);
            break;
        case SET_OPTION:
            if (sqlNode instanceof DrillSqlSetOption) {
                handler = new SetOptionHandler(context);
                context.setSQLStatementType(SqlStatementType.SETOPTION);
                break;
            }
            if (sqlNode instanceof DrillSqlResetOption) {
                handler = new ResetOptionHandler(context);
                context.setSQLStatementType(SqlStatementType.SETOPTION);
                break;
            }
        case DESCRIBE_TABLE:
            if (sqlNode instanceof DrillSqlDescribeTable) {
                handler = new DescribeTableHandler(config);
                context.setSQLStatementType(SqlStatementType.DESCRIBE_TABLE);
                break;
            }
        case DESCRIBE_SCHEMA:
            if (sqlNode instanceof SqlDescribeSchema) {
                handler = new DescribeSchemaHandler(config);
                context.setSQLStatementType(SqlStatementType.DESCRIBE_SCHEMA);
                break;
            }
            if (sqlNode instanceof SqlSchema.Describe) {
                handler = new SchemaHandler.Describe(config);
                context.setSQLStatementType(SqlStatementType.DESCRIBE_SCHEMA);
                break;
            }
        case CREATE_TABLE:
            handler = ((DrillSqlCall) sqlNode).getSqlHandler(config, textPlan);
            context.setSQLStatementType(SqlStatementType.CTAS);
            break;
        case SELECT:
            handler = new DefaultSqlHandler(config, textPlan);
            context.setSQLStatementType(SqlStatementType.SELECT);
            break;
        case DROP_TABLE:
        case CREATE_VIEW:
        case DROP_VIEW:
        case OTHER_DDL:
        case OTHER:
            if (sqlNode instanceof DrillSqlCall) {
                handler = ((DrillSqlCall) sqlNode).getSqlHandler(config);
                if (handler instanceof AnalyzeTableHandler || handler instanceof MetastoreAnalyzeTableHandler) {
                    context.setSQLStatementType(SqlStatementType.ANALYZE);
                } else if (handler instanceof RefreshMetadataHandler) {
                    context.setSQLStatementType(SqlStatementType.REFRESH);
                } else {
                    context.setSQLStatementType(SqlStatementType.OTHER);
                }
                break;
            }
        // fallthrough
        default:
            handler = new DefaultSqlHandler(config, textPlan);
            context.setSQLStatementType(SqlStatementType.OTHER);
    }
    // Determines whether result set should be returned for the query based on return result set option and sql node kind.
    // Overrides the option on a query level if it differs from the current value.
    boolean currentReturnResultValue = context.getOptions().getBoolean(ExecConstants.RETURN_RESULT_SET_FOR_DDL);
    boolean newReturnResultSetValue = currentReturnResultValue || !SqlKind.DDL.contains(sqlNode.getKind());
    if (newReturnResultSetValue != currentReturnResultValue) {
        context.getOptions().setLocalOption(ExecConstants.RETURN_RESULT_SET_FOR_DDL, true);
    }
    return handler.getPlan(sqlNode);
}
Also used : SchemaHandler(org.apache.drill.exec.planner.sql.handlers.SchemaHandler) DescribeSchemaHandler(org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler) DefaultSqlHandler(org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler) ExplainHandler(org.apache.drill.exec.planner.sql.handlers.ExplainHandler) DescribeTableHandler(org.apache.drill.exec.planner.sql.handlers.DescribeTableHandler) SetOptionHandler(org.apache.drill.exec.planner.sql.handlers.SetOptionHandler) ResetOptionHandler(org.apache.drill.exec.planner.sql.handlers.ResetOptionHandler) SqlHandlerConfig(org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig) SqlDescribeSchema(org.apache.calcite.sql.SqlDescribeSchema) DescribeSchemaHandler(org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler) DrillSqlResetOption(org.apache.drill.exec.planner.sql.parser.DrillSqlResetOption) SqlConverter(org.apache.drill.exec.planner.sql.conversion.SqlConverter) MetastoreAnalyzeTableHandler(org.apache.drill.exec.planner.sql.handlers.MetastoreAnalyzeTableHandler) AnalyzeTableHandler(org.apache.drill.exec.planner.sql.handlers.AnalyzeTableHandler) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) AbstractSqlHandler(org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler) MetastoreAnalyzeTableHandler(org.apache.drill.exec.planner.sql.handlers.MetastoreAnalyzeTableHandler) RefreshMetadataHandler(org.apache.drill.exec.planner.sql.handlers.RefreshMetadataHandler) DrillSqlSetOption(org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption) DrillSqlCall(org.apache.drill.exec.planner.sql.parser.DrillSqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlDescribeSchema (org.apache.calcite.sql.SqlDescribeSchema)3 SqlNode (org.apache.calcite.sql.SqlNode)2 AbstractSqlHandler (org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler)2 DefaultSqlHandler (org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler)2 DescribeSchemaHandler (org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler)2 DescribeTableHandler (org.apache.drill.exec.planner.sql.handlers.DescribeTableHandler)2 ExplainHandler (org.apache.drill.exec.planner.sql.handlers.ExplainHandler)2 SetOptionHandler (org.apache.drill.exec.planner.sql.handlers.SetOptionHandler)2 SqlHandlerConfig (org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig)2 DrillSqlCall (org.apache.drill.exec.planner.sql.parser.DrillSqlCall)2 DrillSqlDescribeTable (org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SerializableString (com.fasterxml.jackson.core.SerializableString)1 IOException (java.io.IOException)1 Map (java.util.Map)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 RelConversionException (org.apache.calcite.tools.RelConversionException)1 ValidationException (org.apache.calcite.tools.ValidationException)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1