use of org.apache.calcite.sql.SqlNode in project storm by apache.
the class StormSqlImpl method execute.
@Override
public void execute(Iterable<String> statements, ChannelHandler result) throws Exception {
Map<String, DataSource> dataSources = new HashMap<>();
for (String sql : statements) {
StormParser parser = new StormParser(sql);
SqlNode node = parser.impl().parseSqlStmtEof();
if (node instanceof SqlCreateTable) {
handleCreateTable((SqlCreateTable) node, dataSources);
} else if (node instanceof SqlCreateFunction) {
handleCreateFunction((SqlCreateFunction) node);
} else {
FrameworkConfig config = buildFrameWorkConfig();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelNode tree = planner.convert(validate);
PlanCompiler compiler = new PlanCompiler(typeFactory);
AbstractValuesProcessor proc = compiler.compile(tree);
proc.initialize(dataSources, result);
}
}
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class TestSqlBracketlessSyntax method checkComplexExpressionParsing.
@Test
public void checkComplexExpressionParsing() throws Exception {
FrameworkConfig config = //
Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL).setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH).setParserFactory(DrillParserImpl.FACTORY).build()).defaultSchema(//
SimpleCalciteSchema.createRootSchema(false)).convertletTable(//
DrillConvertletTable.INSTANCE).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode node = planner.parse("" + "select a[4].c \n" + "from x.y.z \n" + "where a.c.b = 5 and x[2] = 7 \n" + "group by d \n" + "having a.c < 5 \n" + "order by x.a.a.a.a.a");
String expected = "SELECT `a`[4]['c']\n" + "FROM `x`.`y`.`z`\n" + "WHERE `a`.`c`['b'] = 5 AND `x`[2] = 7\n" + "GROUP BY `d`\n" + "HAVING `a`.`c` < 5\n" + "ORDER BY `x`.`a`['a']['a']['a']['a']";
SqlNode rewritten = node.accept(new CompoundIdentifierConverter());
String rewrittenQuery = rewritten.toString();
DrillAssert.assertMultiLineStringEquals(expected, rewrittenQuery);
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DescribeTableHandler method rewrite.
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws RelConversionException, ForemanSetupException {
SqlDescribeTable node = unwrap(sqlNode, SqlDescribeTable.class);
try {
List<SqlNode> selectList = ImmutableList.of((SqlNode) new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO), new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));
SqlNode fromClause = new SqlIdentifier(ImmutableList.of(IS_SCHEMA_NAME, TAB_COLUMNS), null, SqlParserPos.ZERO, null);
final SqlIdentifier table = node.getTable();
final SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
final List<String> schemaPathGivenInCmd = Util.skipLast(table.names);
final SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);
final String charset = Util.getDefaultCharset().name();
if (schema == null) {
SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.SCHEMA_PATH_JOINER.join(schemaPathGivenInCmd));
}
if (SchemaUtilites.isRootSchema(schema)) {
throw UserException.validationError().message("No schema selected.").build(logger);
}
final String tableName = Util.last(table.names);
// find resolved schema path
final String schemaPath = SchemaUtilites.unwrapAsDrillSchemaInstance(schema).getFullSchemaName();
if (schema.getTable(tableName) == null) {
throw UserException.validationError().message("Unknown table [%s] in schema [%s]", tableName, schemaPath).build(logger);
}
SqlNode schemaCondition = null;
if (!SchemaUtilites.isRootSchema(schema)) {
schemaCondition = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(schemaPath, charset, SqlParserPos.ZERO));
}
SqlNode where = DrillParserUtil.createCondition(new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(tableName, charset, SqlParserPos.ZERO));
where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);
SqlNode columnFilter = null;
if (node.getColumn() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.EQUALS, SqlLiteral.createCharString(node.getColumn().toString(), charset, SqlParserPos.ZERO));
} else if (node.getColumnQualifier() != null) {
columnFilter = DrillParserUtil.createCondition(new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO), SqlStdOperatorTable.LIKE, node.getColumnQualifier());
}
where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);
return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO), fromClause, where, null, null, null, null, null, null);
} catch (Exception ex) {
throw UserException.planError(ex).message("Error while rewriting DESCRIBE query: %d", ex.getMessage()).build(logger);
}
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
the class DefaultSqlHandler method validateAndConvert.
protected ConvertedRelNode validateAndConvert(SqlNode sqlNode) throws ForemanSetupException, RelConversionException, ValidationException {
final SqlNode rewrittenSqlNode = rewrite(sqlNode);
final TypedSqlNode validatedTypedSqlNode = validateNode(rewrittenSqlNode);
final SqlNode validated = validatedTypedSqlNode.getSqlNode();
RelNode rel = convertToRel(validated);
rel = preprocessNode(rel);
return new ConvertedRelNode(rel, validatedTypedSqlNode.getType());
}
use of org.apache.calcite.sql.SqlNode in project drill by apache.
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.OptionType type;
if (scope == null) {
// No scope mentioned assumed SESSION
type = OptionType.SESSION;
} else {
switch(scope.toLowerCase()) {
case "session":
type = OptionType.SESSION;
break;
case "system":
type = OptionType.SYSTEM;
break;
default:
throw UserException.validationError().message("Invalid OPTION scope %s. Scope must be SESSION or SYSTEM.", scope).build(logger);
}
}
final OptionManager options = context.getOptions();
if (type == OptionType.SYSTEM) {
// administrative privileges.
if (context.isUserAuthenticationEnabled() && !ImpersonationUtil.hasAdminPrivileges(context.getQueryUserName(), options.getOption(ExecConstants.ADMIN_USERS_VALIDATOR), options.getOption(ExecConstants.ADMIN_USER_GROUPS_VALIDATOR))) {
throw UserException.permissionError().message("Not authorized to change SYSTEM options.").build(logger);
}
}
// Currently, we convert multi-part identifier to a string.
final String name = option.getName().toString();
if (value != null) {
// SET option
final OptionValue optionValue = createOptionValue(name, type, (SqlLiteral) value);
options.setOption(optionValue);
} else {
// RESET option
if ("ALL".equalsIgnoreCase(name)) {
options.deleteAllOptions(type);
} else {
options.deleteOption(name, type);
}
}
return DirectPlan.createDirectPlan(context, true, String.format("%s updated.", name));
}
Aggregations