Search in sources :

Example 1 with SqlOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable in project storm by apache.

the class TestCompilerUtils method sqlOverDummyTable.

public static CalciteState sqlOverDummyTable(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("NAME", typeFactory.createType(String.class)).field("ADDR", typeFactory.createType(String.class)).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 : 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 2 with SqlOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable in project storm by apache.

the class StormSqlImpl method buildFrameWorkConfig.

private FrameworkConfig buildFrameWorkConfig() {
    if (hasUdf) {
        List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
        sqlOperatorTables.add(SqlStdOperatorTable.instance());
        sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema), false, Collections.<String>emptyList(), typeFactory));
        return Frameworks.newConfigBuilder().defaultSchema(schema).operatorTable(new ChainedSqlOperatorTable(sqlOperatorTables)).build();
    } else {
        return Frameworks.newConfigBuilder().defaultSchema(schema).build();
    }
}
Also used : ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ArrayList(java.util.ArrayList)

Example 3 with SqlOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable in project calcite by apache.

the class DefaultSqlTestFactory method createOperatorTable.

public SqlOperatorTable createOperatorTable(SqlTestFactory factory) {
    final SqlOperatorTable opTab0 = (SqlOperatorTable) factory.get("operatorTable");
    MockSqlOperatorTable opTab = new MockSqlOperatorTable(opTab0);
    MockSqlOperatorTable.addRamp(opTab);
    return opTab;
}
Also used : MockSqlOperatorTable(org.apache.calcite.test.MockSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) MockSqlOperatorTable(org.apache.calcite.test.MockSqlOperatorTable)

Example 4 with SqlOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable in project calcite by apache.

the class CalcitePrepareImpl method createSqlValidator.

private SqlValidator createSqlValidator(Context context, CalciteCatalogReader catalogReader) {
    final SqlOperatorTable opTab0 = context.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
    final SqlOperatorTable opTab = ChainedSqlOperatorTable.of(opTab0, catalogReader);
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    final SqlConformance conformance = context.config().conformance();
    return new CalciteSqlValidator(opTab, catalogReader, typeFactory, conformance);
}
Also used : ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) SqlConformance(org.apache.calcite.sql.validate.SqlConformance)

Example 5 with SqlOperatorTable

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable in project calcite by apache.

the class PlannerTest method testValidateUserDefinedAggregate.

@Test
public void testValidateUserDefinedAggregate() throws Exception {
    final SqlStdOperatorTable stdOpTab = SqlStdOperatorTable.instance();
    SqlOperatorTable opTab = ChainedSqlOperatorTable.of(stdOpTab, new ListSqlOperatorTable(ImmutableList.<SqlOperator>of(new MyCountAggFunction())));
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR)).operatorTable(opTab).build();
    final Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse("select \"deptno\", my_count(\"empid\") from \"emps\"\n" + "group by \"deptno\"");
    assertThat(Util.toLinux(parse.toString()), equalTo("SELECT `deptno`, `MY_COUNT`(`empid`)\n" + "FROM `emps`\n" + "GROUP BY `deptno`"));
    // MY_COUNT is recognized as an aggregate function, and therefore it is OK
    // that its argument empid is not in the GROUP BY clause.
    SqlNode validate = planner.validate(parse);
    assertThat(validate, notNullValue());
    // The presence of an aggregate function in the SELECT clause causes it
    // to become an aggregate query. Non-aggregate expressions become illegal.
    planner.close();
    planner.reset();
    parse = planner.parse("select \"deptno\", count(1) from \"emps\"");
    try {
        validate = planner.validate(parse);
        fail("expected exception, got " + validate);
    } catch (ValidationException e) {
        assertThat(e.getCause().getCause().getMessage(), containsString("Expression 'deptno' is not being grouped"));
    }
}
Also used : ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) SqlOperator(org.apache.calcite.sql.SqlOperator) ListSqlOperatorTable(org.apache.calcite.sql.util.ListSqlOperatorTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)12 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)11 ArrayList (java.util.ArrayList)9 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)8 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)5 SqlNode (org.apache.calcite.sql.SqlNode)5 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)5 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)5 Planner (org.apache.calcite.tools.Planner)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 RelNode (org.apache.calcite.rel.RelNode)4 StreamableTable (org.apache.calcite.schema.StreamableTable)4 Table (org.apache.calcite.schema.Table)4 CalciteConnectionConfig (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig)2 RelTraitDef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef)2 CalciteCatalogReader (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader)2 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)2 SqlOperatorTable (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable)2 SqlParser (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser)2