Search in sources :

Example 6 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig in project drill by axbaretto.

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());
    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) SqlTest(org.apache.drill.categories.SqlTest)

Example 7 with FrameworkConfig

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

the class TestCompilerUtils method sqlOverNestedTable.

public static CalciteState sqlOverNestedTable(String sql) throws RelConversionException, ValidationException, SqlParseException {
    SchemaPlus schema = Frameworks.createRootSchema(true);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory).field("ID", SqlTypeName.INTEGER).field("MAPFIELD", typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)), true)).field("NESTEDMAPFIELD", typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createMapType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true), typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)), true)), true)).field("ARRAYFIELD", typeFactory.createTypeWithNullability(typeFactory.createArrayType(typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true), -1L), true)).build();
    Table table = streamableTable.stream();
    schema.add("FOO", table);
    schema.add("BAR", table);
    schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));
    List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
    sqlOperatorTables.add(SqlStdOperatorTable.instance());
    sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema), false, Collections.<String>emptyList(), typeFactory));
    SqlOperatorTable chainedSqlOperatorTable = new ChainedSqlOperatorTable(sqlOperatorTables);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).operatorTable(chainedSqlOperatorTable).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode tree = planner.convert(validate);
    System.out.println(RelOptUtil.toString(tree, SqlExplainLevel.ALL_ATTRIBUTES));
    return new CalciteState(schema, tree);
}
Also used : CompilerUtil(com.hortonworks.streamline.streams.sql.compiler.CompilerUtil) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Table(org.apache.calcite.schema.Table) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) StreamableTable(org.apache.calcite.schema.StreamableTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) StreamableTable(org.apache.calcite.schema.StreamableTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) RelNode(org.apache.calcite.rel.RelNode) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 8 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig 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 9 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig 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 10 with FrameworkConfig

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

the class SamzaSqlQueryParser method createPlanner.

private static Planner createPlanner() {
    Connection connection;
    SchemaPlus rootSchema;
    try {
        JavaTypeFactory typeFactory = new SamzaSqlJavaTypeFactoryImpl();
        SamzaSqlDriver driver = new SamzaSqlDriver(typeFactory);
        DriverManager.deregisterDriver(DriverManager.getDriver("jdbc:calcite:"));
        DriverManager.registerDriver(driver);
        connection = driver.connect("jdbc:calcite:", new Properties());
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        rootSchema = calciteConnection.getRootSchema();
    } catch (SQLException e) {
        throw new SamzaException(e);
    }
    final List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelCollationTraitDef.INSTANCE);
    FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).build()).defaultSchema(rootSchema).operatorTable(SqlStdOperatorTable.instance()).traitDefs(traitDefs).context(Contexts.EMPTY_CONTEXT).costFactory(null).build();
    return Frameworks.getPlanner(frameworkConfig);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaSqlDriver(org.apache.samza.sql.interfaces.SamzaSqlDriver) Properties(java.util.Properties) SamzaException(org.apache.samza.SamzaException) RelTraitDef(org.apache.calcite.plan.RelTraitDef) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)

Aggregations

FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)24 Planner (org.apache.calcite.tools.Planner)14 RelNode (org.apache.calcite.rel.RelNode)12 SqlNode (org.apache.calcite.sql.SqlNode)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)10 ArrayList (java.util.ArrayList)6 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)5 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 StreamableTable (org.apache.calcite.schema.StreamableTable)4 Table (org.apache.calcite.schema.Table)4 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)4 Test (org.junit.Test)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)3 RelBuilder (org.apache.calcite.tools.RelBuilder)3 HashMap (java.util.HashMap)2