Search in sources :

Example 6 with SqlNode

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) SqlCreateTable(org.apache.storm.sql.parser.SqlCreateTable) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) DataSource(org.apache.storm.sql.runtime.DataSource) PlanCompiler(org.apache.storm.sql.compiler.backends.standalone.PlanCompiler) RelNode(org.apache.calcite.rel.RelNode) SqlCreateFunction(org.apache.storm.sql.parser.SqlCreateFunction) Planner(org.apache.calcite.tools.Planner) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) StormParser(org.apache.storm.sql.parser.StormParser) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlNode

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);
}
Also used : CompoundIdentifierConverter(org.apache.drill.exec.planner.sql.parser.CompoundIdentifierConverter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 8 with SqlNode

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);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlDescribeTable(org.apache.drill.exec.planner.sql.parser.SqlDescribeTable) UserException(org.apache.drill.common.exceptions.UserException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with SqlNode

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());
}
Also used : RelNode(org.apache.calcite.rel.RelNode) SqlNode(org.apache.calcite.sql.SqlNode) TypedSqlNode(org.apache.calcite.sql.TypedSqlNode) TypedSqlNode(org.apache.calcite.sql.TypedSqlNode)

Example 10 with SqlNode

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));
}
Also used : 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) OptionType(org.apache.drill.exec.server.options.OptionValue.OptionType) OptionManager(org.apache.drill.exec.server.options.OptionManager) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)23 RelNode (org.apache.calcite.rel.RelNode)6 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)5 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)5 Planner (org.apache.calcite.tools.Planner)5 SchemaPlus (org.apache.calcite.schema.SchemaPlus)4 SqlSelect (org.apache.calcite.sql.SqlSelect)4 HashMap (java.util.HashMap)3 SqlNodeList (org.apache.calcite.sql.SqlNodeList)3 RelConversionException (org.apache.calcite.tools.RelConversionException)3 SqlCreateFunction (org.apache.storm.sql.parser.SqlCreateFunction)3 SqlCreateTable (org.apache.storm.sql.parser.SqlCreateTable)3 StormParser (org.apache.storm.sql.parser.StormParser)3 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)3 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)3 ArrayList (java.util.ArrayList)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)2 StreamableTable (org.apache.calcite.schema.StreamableTable)2