Search in sources :

Example 11 with Schema

use of org.apache.calcite.schema.Schema in project calcite by apache.

the class SqlCreateForeignSchema method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
    if (subSchema0 != null) {
        if (!getReplace() && !ifNotExists) {
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
        }
    }
    final Schema subSchema;
    final String libraryName;
    if (type != null) {
        Preconditions.checkArgument(library == null);
        final String typeName = (String) value(this.type);
        final JsonSchema.Type type = Util.enumVal(JsonSchema.Type.class, typeName.toUpperCase(Locale.ROOT));
        if (type != null) {
            switch(type) {
                case JDBC:
                    libraryName = JdbcSchema.Factory.class.getName();
                    break;
                default:
                    libraryName = null;
            }
        } else {
            libraryName = null;
        }
        if (libraryName == null) {
            throw SqlUtil.newContextException(this.type.getParserPosition(), RESOURCE.schemaInvalidType(typeName, Arrays.toString(JsonSchema.Type.values())));
        }
    } else {
        Preconditions.checkArgument(library != null);
        libraryName = (String) value(library);
    }
    final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, libraryName);
    final Map<String, Object> operandMap = new LinkedHashMap<>();
    for (Pair<SqlIdentifier, SqlNode> option : options(optionList)) {
        operandMap.put(option.left.getSimple(), value(option.right));
    }
    subSchema = schemaFactory.create(pair.left.plus(), pair.right, operandMap);
    pair.left.add(pair.right, subSchema);
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JsonSchema(org.apache.calcite.model.JsonSchema) JsonSchema(org.apache.calcite.model.JsonSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SchemaFactory(org.apache.calcite.schema.SchemaFactory) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashMap(java.util.LinkedHashMap) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with Schema

use of org.apache.calcite.schema.Schema in project calcite by apache.

the class ModelHandler method visit.

public void visit(JsonCustomSchema jsonSchema) {
    try {
        final SchemaPlus parentSchema = currentMutableSchema("sub-schema");
        checkRequiredAttributes(jsonSchema, "name", "factory");
        final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, jsonSchema.factory);
        final Schema schema = schemaFactory.create(parentSchema, jsonSchema.name, operandMap(jsonSchema, jsonSchema.operand));
        final SchemaPlus schemaPlus = parentSchema.add(jsonSchema.name, schema);
        populateSchema(jsonSchema, schemaPlus);
    } catch (Exception e) {
        throw new RuntimeException("Error instantiating " + jsonSchema, e);
    }
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 13 with Schema

use of org.apache.calcite.schema.Schema in project calcite by apache.

the class CsvTest method testPrepared.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1031">[CALCITE-1031]
 * In prepared statement, CsvScannableTable.scan is called twice</a>. To see
 * the bug, place a breakpoint in CsvScannableTable.scan, and note that it is
 * called twice. It should only be called once.
 */
@Test
public void testPrepared() throws SQLException {
    final Properties properties = new Properties();
    properties.setProperty("caseSensitive", "true");
    try (final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties)) {
        final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        final Schema schema = CsvSchemaFactory.INSTANCE.create(calciteConnection.getRootSchema(), null, ImmutableMap.<String, Object>of("directory", resourcePath("sales"), "flavor", "scannable"));
        calciteConnection.getRootSchema().add("TEST", schema);
        final String sql = "select * from \"TEST\".\"DEPTS\" where \"NAME\" = ?";
        final PreparedStatement statement2 = calciteConnection.prepareStatement(sql);
        statement2.setString(1, "Sales");
        final ResultSet resultSet1 = statement2.executeQuery();
        Function<ResultSet, Void> expect = expect("DEPTNO=10; NAME=Sales");
        expect.apply(resultSet1);
    }
}
Also used : Schema(org.apache.calcite.schema.Schema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 14 with Schema

use of org.apache.calcite.schema.Schema in project drill by apache.

the class CassandraRootDrillSchemaFactory method registerSchemas.

@Override
public void registerSchemas(SchemaConfig schemaConfig, SchemaPlus parent) {
    Schema schema = new CassandraRootDrillSchema(getName(), plugin, calciteSchemaFactory, parent, getName(), plugin.getConfig().toConfigMap());
    parent.add(getName(), schema);
}
Also used : Schema(org.apache.calcite.schema.Schema)

Example 15 with Schema

use of org.apache.calcite.schema.Schema in project drill by apache.

the class CassandraRootDrillSchema method createSubSchema.

private Schema createSubSchema(String schemaName) {
    Map<String, Object> configs = new HashMap<>(configMap);
    configs.put("keyspace", schemaName);
    SchemaPlus parentSchema = parent.getSubSchema(parentName);
    Schema schema = new CassandraDrillSchema(schemaName, plugin, (CassandraSchema) schemaFactory.create(parentSchema, schemaName, configs));
    parentSchema.add(schemaName, schema);
    return schema;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CassandraSchema(org.apache.calcite.adapter.cassandra.CassandraSchema) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) Schema(org.apache.calcite.schema.Schema) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Aggregations

Schema (org.apache.calcite.schema.Schema)16 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)7 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)7 HashMap (java.util.HashMap)5 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)5 Test (org.junit.Test)5 Map (java.util.Map)4 Connection (java.sql.Connection)3 CloneSchema (org.apache.calcite.adapter.clone.CloneSchema)3 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)3 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Properties (java.util.Properties)2 SchemaFactory (org.apache.calcite.schema.SchemaFactory)2 Table (org.apache.calcite.schema.Table)2 Table (com.hazelcast.sql.impl.schema.Table)1 BenchmarkDataGenerator (io.druid.benchmark.datagen.BenchmarkDataGenerator)1