Search in sources :

Example 1 with AbstractSchema

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

the class SqlBenchmark method setup.

@Setup(Level.Trial)
public void setup() throws Exception {
    tmpDir = Files.createTempDir();
    log.info("Starting benchmark setup using tmpDir[%s], rows[%,d].", tmpDir, rowsPerSegment);
    if (ComplexMetrics.getSerdeForType("hyperUnique") == null) {
        ComplexMetrics.registerSerde("hyperUnique", new HyperUniquesSerde(HyperLogLogHash.getDefault()));
    }
    final BenchmarkSchemaInfo schemaInfo = BenchmarkSchemas.SCHEMA_MAP.get("basic");
    final BenchmarkDataGenerator dataGenerator = new BenchmarkDataGenerator(schemaInfo.getColumnSchemas(), RNG_SEED + 1, schemaInfo.getDataInterval(), rowsPerSegment);
    final List<InputRow> rows = Lists.newArrayList();
    for (int i = 0; i < rowsPerSegment; i++) {
        final InputRow row = dataGenerator.nextRow();
        if (i % 20000 == 0) {
            log.info("%,d/%,d rows generated.", i, rowsPerSegment);
        }
        rows.add(row);
    }
    log.info("%,d/%,d rows generated.", rows.size(), rowsPerSegment);
    final PlannerConfig plannerConfig = new PlannerConfig();
    final QueryRunnerFactoryConglomerate conglomerate = CalciteTests.queryRunnerFactoryConglomerate();
    final QueryableIndex index = IndexBuilder.create().tmpDir(new File(tmpDir, "1")).indexMerger(TestHelper.getTestIndexMergerV9()).rows(rows).buildMMappedIndex();
    this.walker = new SpecificSegmentsQuerySegmentWalker(conglomerate).add(DataSegment.builder().dataSource("foo").interval(index.getDataInterval()).version("1").shardSpec(new LinearShardSpec(0)).build(), index);
    final Map<String, Table> tableMap = ImmutableMap.<String, Table>of("foo", new DruidTable(new TableDataSource("foo"), RowSignature.builder().add("__time", ValueType.LONG).add("dimSequential", ValueType.STRING).add("dimZipf", ValueType.STRING).add("dimUniform", ValueType.STRING).build()));
    final Schema druidSchema = new AbstractSchema() {

        @Override
        protected Map<String, Table> getTableMap() {
            return tableMap;
        }
    };
    plannerFactory = new PlannerFactory(Calcites.createRootSchema(druidSchema), walker, CalciteTests.createOperatorTable(), plannerConfig);
    groupByQuery = GroupByQuery.builder().setDataSource("foo").setInterval(new Interval(JodaUtils.MIN_INSTANT, JodaUtils.MAX_INSTANT)).setDimensions(Arrays.<DimensionSpec>asList(new DefaultDimensionSpec("dimZipf", "d0"), new DefaultDimensionSpec("dimSequential", "d1"))).setAggregatorSpecs(Arrays.<AggregatorFactory>asList(new CountAggregatorFactory("c"))).setGranularity(Granularities.ALL).build();
    sqlQuery = "SELECT\n" + "  dimZipf AS d0," + "  dimSequential AS d1,\n" + "  COUNT(*) AS c\n" + "FROM druid.foo\n" + "GROUP BY dimZipf, dimSequential";
}
Also used : DruidTable(io.druid.sql.calcite.table.DruidTable) Table(org.apache.calcite.schema.Table) LinearShardSpec(io.druid.timeline.partition.LinearShardSpec) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) BenchmarkDataGenerator(io.druid.benchmark.datagen.BenchmarkDataGenerator) HyperUniquesSerde(io.druid.query.aggregation.hyperloglog.HyperUniquesSerde) DruidTable(io.druid.sql.calcite.table.DruidTable) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) QueryRunnerFactoryConglomerate(io.druid.query.QueryRunnerFactoryConglomerate) SpecificSegmentsQuerySegmentWalker(io.druid.sql.calcite.util.SpecificSegmentsQuerySegmentWalker) TableDataSource(io.druid.query.TableDataSource) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) QueryableIndex(io.druid.segment.QueryableIndex) BenchmarkSchemaInfo(io.druid.benchmark.datagen.BenchmarkSchemaInfo) PlannerConfig(io.druid.sql.calcite.planner.PlannerConfig) InputRow(io.druid.data.input.InputRow) PlannerFactory(io.druid.sql.calcite.planner.PlannerFactory) File(java.io.File) Interval(org.joda.time.Interval) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with AbstractSchema

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

the class CalcitePlanner method getRootSchema.

private SchemaPlus getRootSchema() throws MetadataStorageManagerException {
    if (rootSchema != null) {
        return rootSchema;
    }
    final SchemaPlus _rootSchema = Frameworks.createRootSchema(true);
    for (String tableSpace : manager.getLocalTableSpaces()) {
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        SchemaPlus schema = _rootSchema.add(tableSpace, new AbstractSchema());
        List<Table> tables = tableSpaceManager.getAllTablesForPlanner();
        for (Table table : tables) {
            AbstractTableManager tableManager = tableSpaceManager.getTableManager(table.name);
            TableImpl tableDef = new TableImpl(tableManager);
            schema.add(table.name, tableDef);
        }
    }
    rootSchema = _rootSchema;
    return _rootSchema;
}
Also used : Table(herddb.model.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) AbstractTableManager(herddb.core.AbstractTableManager) SchemaPlus(org.apache.calcite.schema.SchemaPlus) TableSpaceManager(herddb.core.TableSpaceManager)

Example 3 with AbstractSchema

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

the class ModelHandler method visit.

public void visit(JsonMapSchema jsonSchema) {
    checkRequiredAttributes(jsonSchema, "name");
    final SchemaPlus parentSchema = currentMutableSchema("schema");
    final SchemaPlus schema = parentSchema.add(jsonSchema.name, new AbstractSchema());
    if (jsonSchema.path != null) {
        schema.setPath(stringListList(jsonSchema.path));
    }
    populateSchema(jsonSchema, schema);
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 4 with AbstractSchema

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

the class JdbcTest method testSchemaCaching.

@Test
public void testSchemaCaching() throws Exception {
    final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
    final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    // create schema "/a"
    final Map<String, Schema> aSubSchemaMap = new HashMap<>();
    final SchemaPlus aSchema = rootSchema.add("a", new AbstractSchema() {

        @Override
        protected Map<String, Schema> getSubSchemaMap() {
            return aSubSchemaMap;
        }
    });
    aSchema.setCacheEnabled(true);
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    // first call, to populate the cache
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    // create schema "/a/b1". Appears only when we disable caching.
    aSubSchemaMap.put("b1", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    assertThat(aSchema.getSubSchema("b1"), nullValue());
    aSchema.setCacheEnabled(false);
    assertThat(aSchema.getSubSchemaNames().size(), is(1));
    assertThat(aSchema.getSubSchema("b1"), notNullValue());
    // create schema "/a/b2". Appears immediately, because caching is disabled.
    aSubSchemaMap.put("b2", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(2));
    // an explicit sub-schema appears immediately, even if caching is enabled
    aSchema.setCacheEnabled(true);
    assertThat(aSchema.getSubSchemaNames().size(), is(2));
    // explicit
    aSchema.add("b3", new AbstractSchema());
    // implicit
    aSubSchemaMap.put("b4", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(3));
    aSchema.setCacheEnabled(false);
    assertThat(aSchema.getSubSchemaNames().size(), is(4));
    for (String name : aSchema.getSubSchemaNames()) {
        assertThat(aSchema.getSubSchema(name), notNullValue());
    }
    // create schema "/a2"
    final Map<String, Schema> a2SubSchemaMap = new HashMap<>();
    final SchemaPlus a2Schema = rootSchema.add("a", new AbstractSchema() {

        @Override
        protected Map<String, Schema> getSubSchemaMap() {
            return a2SubSchemaMap;
        }
    });
    a2Schema.setCacheEnabled(true);
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    // create schema "/a2/b3". Change not visible since caching is enabled.
    a2SubSchemaMap.put("b3", new AbstractSchema());
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    Thread.sleep(1);
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    // Change visible after we turn off caching.
    a2Schema.setCacheEnabled(false);
    assertThat(a2Schema.getSubSchemaNames().size(), is(1));
    a2SubSchemaMap.put("b4", new AbstractSchema());
    assertThat(a2Schema.getSubSchemaNames().size(), is(2));
    for (String name : aSchema.getSubSchemaNames()) {
        assertThat(aSchema.getSubSchema(name), notNullValue());
    }
    // add tables and retrieve with various case sensitivities
    final TableInRootSchemaTest.SimpleTable table = new TableInRootSchemaTest.SimpleTable();
    a2Schema.add("table1", table);
    a2Schema.add("TABLE1", table);
    a2Schema.add("tabLe1", table);
    a2Schema.add("tabLe2", table);
    assertThat(a2Schema.getTableNames().size(), equalTo(4));
    final CalciteSchema a2CalciteSchema = CalciteSchema.from(a2Schema);
    assertThat(a2CalciteSchema.getTable("table1", true), notNullValue());
    assertThat(a2CalciteSchema.getTable("table1", false), notNullValue());
    assertThat(a2CalciteSchema.getTable("taBle1", true), nullValue());
    assertThat(a2CalciteSchema.getTable("taBle1", false), notNullValue());
    final TableMacro function = ViewTable.viewMacro(a2Schema, "values 1", null, null, null);
    Util.discard(function);
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) HashMap(java.util.HashMap) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) Map(java.util.Map) HashMap(java.util.HashMap) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 5 with AbstractSchema

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

the class JdbcTest method adviseSql.

private void adviseSql(String sql, Function<ResultSet, Void> checker) throws ClassNotFoundException, SQLException {
    Properties info = new Properties();
    info.put("lex", "JAVA");
    info.put("quoting", "DOUBLE_QUOTE");
    Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    calciteConnection.setSchema("hr");
    final TableFunction table = new SqlAdvisorGetHintsFunction();
    schema.add("get_hints", table);
    PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"get_hints\"(?, ?)) as t(id, names, type)");
    SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
    ps.setString(1, sap.sql);
    ps.setInt(2, sap.cursor);
    final ResultSet resultSet = ps.executeQuery();
    checker.apply(resultSet);
    resultSet.close();
    connection.close();
}
Also used : SqlAdvisorGetHintsFunction(org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

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