Search in sources :

Example 16 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class FrameworksTest method testUpdate.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>
 * using UPDATE via {@link Frameworks}.
 */
@Test
public void testUpdate() throws Exception {
    Table table = new TableImpl();
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
    schema.add("MYTABLE", table);
    List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelDistributionTraitDef.INSTANCE);
    SqlParser.Config parserConfig = SqlParser.configBuilder(SqlParser.Config.DEFAULT).setCaseSensitive(false).build();
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(schema).traitDefs(traitDefs).ruleSets(RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE)).programs(Programs.ofRules(Programs.RULE_SET)).build();
    executeQuery(config, " UPDATE MYTABLE set id=7 where id=1", CalcitePrepareImpl.DEBUG);
}
Also used : RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) Test(org.junit.Test)

Example 17 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class LookupOperatorOverloadsTest method test.

@Test
public void test() throws SQLException {
    final String schemaName = "MySchema";
    final String funcName = "MyFUNC";
    final String anotherName = "AnotherFunc";
    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
        final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
        schema.add(funcName, table);
        schema.add(anotherName, table);
        final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
        schema.add(funcName, table2);
        final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
        final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
        final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
        CalciteCatalogReader reader = new CalciteCatalogReader(prepareContext.getRootSchema(), ImmutableList.<String>of(), typeFactory, prepareContext.config());
        final List<SqlOperator> operatorList = new ArrayList<>();
        SqlIdentifier myFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, funcName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(2, funcName, operatorList);
        operatorList.clear();
        reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(0, null, operatorList);
        operatorList.clear();
        SqlIdentifier anotherFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, anotherName), null, SqlParserPos.ZERO, null);
        reader.lookupOperatorOverloads(anotherFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
        checkFunctionType(1, anotherName, operatorList);
    }
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 18 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class DruidAdapterIT method testTableMapReused.

/**
 * Test to make sure that the mapping from a Table name to a Table returned from
 * {@link org.apache.calcite.adapter.druid.DruidSchema} is always the same Java object.
 */
@Test
public void testTableMapReused() {
    AbstractSchema schema = new DruidSchema("http://localhost:8082", "http://localhost:8081", true);
    assertSame(schema.getTable("wikiticker"), schema.getTable("wikiticker"));
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) DruidSchema(org.apache.calcite.adapter.druid.DruidSchema) Test(org.junit.Test)

Example 19 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.

the class ExampleFunctionTest method checkMazeTableFunction.

public void checkMazeTableFunction(Boolean solution, String maze) throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableFunction table = TableFunctionImpl.create(MAZE_METHOD);
    schema.add("Maze", table);
    final TableFunction table2 = TableFunctionImpl.create(SOLVE_METHOD);
    schema.add("Solve", table2);
    final String sql;
    if (solution) {
        sql = "select *\n" + "from table(\"s\".\"Solve\"(5, 3, 1)) as t(s)";
    } else {
        sql = "select *\n" + "from table(\"s\".\"Maze\"(5, 3, 1)) as t(s)";
    }
    ResultSet resultSet = connection.createStatement().executeQuery(sql);
    final StringBuilder b = new StringBuilder();
    while (resultSet.next()) {
        b.append(resultSet.getString(1)).append("\n");
    }
    assertThat(b.toString(), is(maze));
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 20 with AbstractSchema

use of org.apache.calcite.schema.impl.AbstractSchema in project samza by apache.

the class QueryPlanner method registerSourceSchemas.

private void registerSourceSchemas(SchemaPlus rootSchema) {
    RelSchemaConverter relSchemaConverter = new RelSchemaConverter();
    for (SqlIOConfig ssc : systemStreamConfigBySource.values()) {
        SchemaPlus previousLevelSchema = rootSchema;
        List<String> sourceParts = ssc.getSourceParts();
        RelSchemaProvider relSchemaProvider = relSchemaProviders.get(ssc.getSource());
        for (int sourcePartIndex = 0; sourcePartIndex < sourceParts.size(); sourcePartIndex++) {
            String sourcePart = sourceParts.get(sourcePartIndex);
            if (sourcePartIndex < sourceParts.size() - 1) {
                SchemaPlus sourcePartSchema = previousLevelSchema.getSubSchema(sourcePart);
                if (sourcePartSchema == null) {
                    sourcePartSchema = previousLevelSchema.add(sourcePart, new AbstractSchema());
                }
                previousLevelSchema = sourcePartSchema;
            } else {
                // If the source part is the last one, then fetch the schema corresponding to the stream and register.
                RelDataType relationalSchema = getSourceRelSchema(relSchemaProvider, relSchemaConverter);
                previousLevelSchema.add(sourcePart, createTableFromRelSchema(relationalSchema));
                break;
            }
        }
    }
}
Also used : SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelSchemaProvider(org.apache.samza.sql.interfaces.RelSchemaProvider) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 SchemaPlus (org.apache.calcite.schema.SchemaPlus)30 Connection (java.sql.Connection)22 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)22 Test (org.junit.Test)20 ResultSet (java.sql.ResultSet)15 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)10 TableFunction (org.apache.calcite.schema.TableFunction)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Schema (org.apache.calcite.schema.Schema)6 PreparedStatement (java.sql.PreparedStatement)5 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)5 Statement (java.sql.Statement)4 CloneSchema (org.apache.calcite.adapter.clone.CloneSchema)4 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)3 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)3 Table (org.apache.calcite.schema.Table)3