Search in sources :

Example 6 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project streamline by hortonworks.

the class RuleParser method parse.

public void parse() {
    try {
        SchemaPlus schema = Frameworks.createRootSchema(true);
        FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).build();
        Planner planner = Frameworks.getPlanner(config);
        SqlSelect sqlSelect = (SqlSelect) planner.parse(sql);
        // FROM
        streams = parseStreams(sqlSelect);
        // SELECT
        projection = parseProjection(sqlSelect);
        // WHERE
        condition = parseCondition(sqlSelect);
        // GROUP BY
        groupBy = parseGroupBy(sqlSelect);
        // HAVING
        having = parseHaving(sqlSelect);
    } catch (Exception ex) {
        LOG.error("Got Exception while parsing rule {}", sql);
        throw new RuntimeException(ex);
    }
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig)

Example 7 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner 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(// 
    CalciteSchema.createRootSchema(false, false).plus()).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(false));
    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) BaseTest(org.apache.drill.test.BaseTest) SqlTest(org.apache.drill.categories.SqlTest)

Example 8 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project samza by apache.

the class SamzaSqlQueryParser method parseQuery.

public static QueryInfo parseQuery(String sql) {
    Planner planner = createPlanner();
    SqlNode sqlNode;
    // Having semi-colons at the end of sql statement is a valid syntax in standard sql but not for Calcite parser.
    // Hence, removing trailing semi-colon before passing sql statement to Calcite parser.
    sql = sql.replaceAll(TRAILING_SEMI_COLON_REGEX, "");
    try {
        sqlNode = planner.parse(sql);
    } catch (SqlParseException e) {
        String errorMsg = SamzaSqlValidator.formatErrorString(sql, e);
        LOG.error(errorMsg, e);
        throw new SamzaException(errorMsg, e);
    }
    String sink;
    String selectQuery;
    ArrayList<String> sources;
    if (sqlNode instanceof SqlInsert) {
        SqlInsert sqlInsert = (SqlInsert) sqlNode;
        sink = sqlInsert.getTargetTable().toString();
        if (sqlInsert.getSource() instanceof SqlSelect) {
            SqlSelect sqlSelect = (SqlSelect) sqlInsert.getSource();
            selectQuery = sqlSelect.toString();
            LOG.info("Parsed select query {} from sql {}", selectQuery, sql);
            sources = getSourcesFromSelectQuery(sqlSelect);
        } else {
            String msg = String.format("Sql query is not of the expected format. Select node expected, found %s", sqlInsert.getSource().getClass().toString());
            LOG.error(msg);
            throw new SamzaException(msg);
        }
    } else {
        String msg = String.format("Sql query is not of the expected format. Insert node expected, found %s", sqlNode.getClass().toString());
        LOG.error(msg);
        throw new SamzaException(msg);
    }
    return new QueryInfo(selectQuery, sources, sink, sql);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Planner(org.apache.calcite.tools.Planner) SqlInsert(org.apache.calcite.sql.SqlInsert) SamzaException(org.apache.samza.SamzaException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 9 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project storm by apache.

the class StormSqlContext method explain.

public String explain(String query) throws SqlParseException, ValidationException, RelConversionException {
    FrameworkConfig config = buildFrameWorkConfig();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(query);
    SqlNode validate = planner.validate(parse);
    RelNode tree = planner.convert(validate);
    return StormRelUtils.explain(tree, SqlExplainLevel.ALL_ATTRIBUTES);
}
Also used : RelNode(org.apache.calcite.rel.RelNode) Planner(org.apache.calcite.tools.Planner) QueryPlanner(org.apache.storm.sql.planner.streams.QueryPlanner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 10 with Planner

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner in project beam by apache.

the class ThreeTablesSchema method transform.

private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final SchemaPlus defSchema = rootSchema.add("tt", new ThreeTablesSchema());
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules)).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelRoot planRoot = planner.rel(validate);
    RelNode planBefore = planRoot.rel;
    RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
    return planner.transform(0, desiredTraits, planBefore);
}
Also used : RelNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode) BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) Planner(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.Planner) RelRoot(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot) FrameworkConfig(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig) RelTraitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Aggregations

Planner (org.apache.calcite.tools.Planner)17 SqlNode (org.apache.calcite.sql.SqlNode)15 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)15 RelNode (org.apache.calcite.rel.RelNode)10 SchemaPlus (org.apache.calcite.schema.SchemaPlus)7 ArrayList (java.util.ArrayList)6 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)4 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)4 StreamableTable (org.apache.calcite.schema.StreamableTable)4 Table (org.apache.calcite.schema.Table)4 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)4 RelNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelNode)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 RelTraitSet (org.apache.calcite.plan.RelTraitSet)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)2