Search in sources :

Example 1 with SqlSetOption

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

the class SetOptionHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, ForemanSetupException {
    final SqlSetOption option = unwrap(sqlNode, SqlSetOption.class);
    final SqlNode value = option.getValue();
    if (value != null && !(value instanceof SqlLiteral)) {
        throw UserException.validationError().message("Drill does not support assigning non-literal values in SET statements.").build(logger);
    }
    final String scope = option.getScope();
    final OptionValue.OptionScope optionScope;
    if (scope == null) {
        // No scope mentioned assumed SESSION
        optionScope = OptionScope.SESSION;
    } else {
        switch(scope.toLowerCase()) {
            case "session":
                optionScope = OptionScope.SESSION;
                break;
            case "system":
                optionScope = OptionScope.SYSTEM;
                break;
            default:
                throw UserException.validationError().message("Invalid OPTION scope %s. Scope must be SESSION or SYSTEM.", scope).build(logger);
        }
    }
    final QueryOptionManager options = context.getOptions();
    if (optionScope == OptionScope.SYSTEM) {
        // administrative privileges.
        if (context.isUserAuthenticationEnabled() && !ImpersonationUtil.hasAdminPrivileges(context.getQueryUserName(), ExecConstants.ADMIN_USERS_VALIDATOR.getAdminUsers(options), ExecConstants.ADMIN_USER_GROUPS_VALIDATOR.getAdminUserGroups(options))) {
            throw UserException.permissionError().message("Not authorized to change SYSTEM options.").build(logger);
        }
    }
    final String optionName = option.getName().toString();
    // Currently, we convert multi-part identifier to a string.
    final OptionManager chosenOptions = options.getOptionManager(optionScope);
    if (value != null) {
        // SET option
        final Object literalObj = sqlLiteralToObject((SqlLiteral) value);
        chosenOptions.setLocalOption(optionName, literalObj);
    } else {
        // RESET option
        if ("ALL".equalsIgnoreCase(optionName)) {
            chosenOptions.deleteAllLocalOptions();
        } else {
            chosenOptions.deleteLocalOption(optionName);
        }
    }
    return DirectPlan.createDirectPlan(context, true, String.format("%s updated.", optionName));
}
Also used : OptionScope(org.apache.drill.exec.server.options.OptionValue.OptionScope) QueryOptionManager(org.apache.drill.exec.server.options.QueryOptionManager) OptionValue(org.apache.drill.exec.server.options.OptionValue) SqlSetOption(org.apache.calcite.sql.SqlSetOption) NlsString(org.apache.calcite.util.NlsString) SqlLiteral(org.apache.calcite.sql.SqlLiteral) OptionManager(org.apache.drill.exec.server.options.OptionManager) QueryOptionManager(org.apache.drill.exec.server.options.QueryOptionManager) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with SqlSetOption

use of org.apache.calcite.sql.SqlSetOption 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 3 with SqlSetOption

use of org.apache.calcite.sql.SqlSetOption in project calcite by apache.

the class SqlParserTest method testSqlOptions.

@Test
public void testSqlOptions() throws SqlParseException {
    SqlNode node = getSqlParser("alter system set schema = true").parseStmt();
    SqlSetOption opt = (SqlSetOption) node;
    assertThat(opt.getScope(), equalTo("SYSTEM"));
    SqlPrettyWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
    assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
    writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
    assertThat(writer.format(opt.getValue()), equalTo("TRUE"));
    writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
    assertThat(writer.format(opt), equalTo("ALTER SYSTEM SET \"SCHEMA\" = TRUE"));
    sql("alter system set \"a number\" = 1").ok("ALTER SYSTEM SET `a number` = 1").node(isDdl());
    check("alter system set flag = false", "ALTER SYSTEM SET `FLAG` = FALSE");
    check("alter system set approx = -12.3450", "ALTER SYSTEM SET `APPROX` = -12.3450");
    check("alter system set onOff = on", "ALTER SYSTEM SET `ONOFF` = `ON`");
    check("alter system set onOff = off", "ALTER SYSTEM SET `ONOFF` = `OFF`");
    check("alter system set baz = foo", "ALTER SYSTEM SET `BAZ` = `FOO`");
    check("alter system set \"a\".\"number\" = 1", "ALTER SYSTEM SET `a`.`number` = 1");
    sql("set approx = -12.3450").ok("SET `APPROX` = -12.3450").node(isDdl());
    node = getSqlParser("reset schema").parseStmt();
    opt = (SqlSetOption) node;
    assertThat(opt.getScope(), equalTo(null));
    writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
    assertThat(writer.format(opt.getName()), equalTo("\"SCHEMA\""));
    assertThat(opt.getValue(), equalTo(null));
    writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
    assertThat(writer.format(opt), equalTo("RESET \"SCHEMA\""));
    check("alter system RESET flag", "ALTER SYSTEM RESET `FLAG`");
    sql("reset onOff").ok("RESET `ONOFF`").node(isDdl());
    check("reset \"this\".\"is\".\"sparta\"", "RESET `this`.`is`.`sparta`");
    check("alter system reset all", "ALTER SYSTEM RESET `ALL`");
    check("reset all", "RESET `ALL`");
    // expressions not allowed
    checkFails("alter system set aString = 'abc' ^||^ 'def' ", "(?s)Encountered \"\\|\\|\" at line 1, column 34\\..*");
    // multiple assignments not allowed
    checkFails("alter system set x = 1^,^ y = 2", "(?s)Encountered \",\" at line 1, column 23\\..*");
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlSetOption(org.apache.calcite.sql.SqlSetOption) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 4 with SqlSetOption

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

the class ResetOptionHandler method getPlan.

/**
 * Handles {@link DrillSqlResetOption} query
 */
@Override
public final PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    QueryOptionManager options = context.getOptions();
    SqlSetOption statement = unwrap(sqlNode, SqlSetOption.class);
    OptionScope optionScope = getScope(statement, context.getOptions());
    if (optionScope == OptionValue.OptionScope.SYSTEM) {
        checkAdminPrivileges(options);
    }
    OptionManager optionManager = options.getOptionManager(optionScope);
    String optionName = statement.getName().toString();
    if ("ALL".equalsIgnoreCase(optionName)) {
        optionManager.deleteAllLocalOptions();
    } else {
        optionManager.deleteLocalOption(optionName);
    }
    return DirectPlan.createDirectPlan(context, true, String.format("%s updated.", optionName));
}
Also used : OptionScope(org.apache.drill.exec.server.options.OptionValue.OptionScope) QueryOptionManager(org.apache.drill.exec.server.options.QueryOptionManager) SqlSetOption(org.apache.calcite.sql.SqlSetOption) OptionManager(org.apache.drill.exec.server.options.OptionManager) QueryOptionManager(org.apache.drill.exec.server.options.QueryOptionManager)

Aggregations

SqlSetOption (org.apache.calcite.sql.SqlSetOption)4 SqlNode (org.apache.calcite.sql.SqlNode)3 OptionManager (org.apache.drill.exec.server.options.OptionManager)3 OptionScope (org.apache.drill.exec.server.options.OptionValue.OptionScope)3 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 NlsString (org.apache.calcite.util.NlsString)2 QueryOptionManager (org.apache.drill.exec.server.options.QueryOptionManager)2 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)1 DrillSqlSetOption (org.apache.drill.exec.planner.sql.parser.DrillSqlSetOption)1 OptionDefinition (org.apache.drill.exec.server.options.OptionDefinition)1 OptionValue (org.apache.drill.exec.server.options.OptionValue)1 Test (org.junit.Test)1