0

I am trying to generate SQL query from relational algebra in Calcite without connecting to the database

I saw an example at Calcite website where a JDBC adapter uses the DB connection for the first time and all subsequent calls get data from cache. What I am looking for is not connecting to the DB at all, just do a translation from relational to SQL.

Any hint is much appreciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

3

An example available in this notebook. I've included the relevant code below. You will need to create a schema instance for this to work. In this case, I've just used one of Calcite's test schemas.

import org.apache.calcite.jdbc.CalciteSchema;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.RelWriter;
import org.apache.calcite.rel.externalize.RelWriterImpl;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.test.CalciteAssert;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.RelBuilder;

SchemaPlus rootSchema = CalciteSchema.createRootSchema(true).plus();
FrameworkConfig config = Frameworks.newConfigBuilder()
    .defaultSchema(
        CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.HR))
    .build();
RelBuilder builder = RelBuilder.create(config);

RelNode opTree = builder.scan("emps")
    .scan("depts")
    .join(JoinRelType.INNER, "deptno")
    .filter(builder.equals(builder.field("empid"), builder.literal(100)))
    .build();

The other step of converting to SQL is fairly straightforward by constructing an instance of RelToSqlConverter and calling it's visit method on the RelNode object.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
0

You can use the new Quidem tests to run which would be much easier

Eminem347
  • 387
  • 4
  • 11