Search in sources :

Example 1 with DrillSqlSetOption

use of org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption in project drill by apache.

the class SetOptionHandler method getPlan.

/**
 * Handles {@link DrillSqlSetOption} query
 */
@Override
public final PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    // sqlNode could contain DrillSqlResetOption or DrillSqlSetOption, depends on parsed statement
    SqlSetOption statement = unwrap(sqlNode, SqlSetOption.class);
    OptionScope optionScope = getScope(statement, context.getOptions());
    OptionManager optionManager = context.getOptions().getOptionManager(optionScope);
    String optionName = statement.getName().toString();
    SqlNode optionValue = statement.getValue();
    if (optionValue == null) {
        // OptionManager.getOptionDefinition() call ensures that the specified option name is valid
        OptionDefinition optionDefinition = optionManager.getOptionDefinition(optionName);
        String value = String.valueOf(optionManager.getOption(optionName).getValue());
        // obtains option name from OptionDefinition to use the name as defined in the option, rather than what the user provided
        return DirectPlan.createDirectPlan(context, new SetOptionViewResult(optionDefinition.getValidator().getOptionName(), value));
    } else {
        if (optionScope == OptionValue.OptionScope.SYSTEM) {
            checkAdminPrivileges(context.getOptions());
        }
        if (!(optionValue instanceof SqlLiteral)) {
            throw UserException.validationError().message("Drill does not support assigning non-literal values in SET statements.").build(logger);
        }
        optionManager.setLocalOption(optionName, sqlLiteralToObject((SqlLiteral) optionValue));
        return DirectPlan.createDirectPlan(context, true, String.format("%s updated.", optionName));
    }
}
Also used : OptionScope(org.apache.drill.exec.server.options.OptionValue.OptionScope) DrillSqlSetOption(org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption) SqlSetOption(org.apache.calcite.sql.SqlSetOption) NlsString(org.apache.calcite.util.NlsString) OptionDefinition(org.apache.drill.exec.server.options.OptionDefinition) SqlLiteral(org.apache.calcite.sql.SqlLiteral) OptionManager(org.apache.drill.exec.server.options.OptionManager) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with DrillSqlSetOption

use of org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption 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

SqlNode (org.apache.calcite.sql.SqlNode)2 DrillSqlSetOption (org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption)2 SqlDescribeSchema (org.apache.calcite.sql.SqlDescribeSchema)1 SqlLiteral (org.apache.calcite.sql.SqlLiteral)1 SqlSetOption (org.apache.calcite.sql.SqlSetOption)1 NlsString (org.apache.calcite.util.NlsString)1 SqlConverter (org.apache.drill.exec.planner.sql.conversion.SqlConverter)1 AbstractSqlHandler (org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler)1 AnalyzeTableHandler (org.apache.drill.exec.planner.sql.handlers.AnalyzeTableHandler)1 DefaultSqlHandler (org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler)1 DescribeSchemaHandler (org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler)1 DescribeTableHandler (org.apache.drill.exec.planner.sql.handlers.DescribeTableHandler)1 ExplainHandler (org.apache.drill.exec.planner.sql.handlers.ExplainHandler)1 MetastoreAnalyzeTableHandler (org.apache.drill.exec.planner.sql.handlers.MetastoreAnalyzeTableHandler)1 RefreshMetadataHandler (org.apache.drill.exec.planner.sql.handlers.RefreshMetadataHandler)1 ResetOptionHandler (org.apache.drill.exec.planner.sql.handlers.ResetOptionHandler)1 SchemaHandler (org.apache.drill.exec.planner.sql.handlers.SchemaHandler)1 SetOptionHandler (org.apache.drill.exec.planner.sql.handlers.SetOptionHandler)1 SqlHandlerConfig (org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig)1 DrillSqlCall (org.apache.drill.exec.planner.sql.parser.DrillSqlCall)1