use of org.apache.drill.exec.server.options.OptionValue.OptionScope 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));
}
use of org.apache.drill.exec.server.options.OptionValue.OptionScope in project drill by axbaretto.
the class ExtendedOptionIterator method sortOptions.
/* *
* Remove the redundant rows for the same option based on the scope and return
* the value that takes precedence over others. For example the option set in session
* scope takes precedence over system and boot and etc.,
*/
public Iterator<OptionValue> sortOptions(Iterator<OptionValue> options) {
List<OptionValue> optionslist = Lists.newArrayList(options);
HashMap<String, OptionValue> optionsmap = new HashMap<>();
final Map<OptionScope, Integer> preference = new HashMap<OptionScope, Integer>() {
{
put(OptionScope.SESSION, 0);
put(OptionScope.SYSTEM, 1);
put(OptionScope.BOOT, 2);
}
};
for (OptionValue option : optionslist) {
if (optionsmap.containsKey(option.getName())) {
if (preference.get(option.scope) < preference.get(optionsmap.get(option.getName()).scope)) {
optionsmap.put(option.getName(), option);
}
} else {
optionsmap.put(option.getName(), option);
}
}
optionslist.clear();
for (String name : optionsmap.keySet()) {
optionslist.add(optionsmap.get(name));
}
Collections.sort(optionslist, new Comparator<OptionValue>() {
@Override
public int compare(OptionValue v1, OptionValue v2) {
return v1.name.compareTo(v2.name);
}
});
return optionslist.iterator();
}
use of org.apache.drill.exec.server.options.OptionValue.OptionScope 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));
}
}
use of org.apache.drill.exec.server.options.OptionValue.OptionScope 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));
}
Aggregations