2

I'm currently working on a project in which I want to parse an SQL-Query and create the tree of relational expressions for it. The main goal is to identify the join partners in the Queries. So I'd like to push the join-expressions down to the leafs of the tree. To achieve this, I decided to use Apache Calcite. My code looks roughly like this at the moment:

Planner planner = Frameworks.getPlanner(Frameworks.newConfigBuilder().defaultSchema(Frameworks.createRootSchema(false)).build());

SqlNode parsed = planner.parse("SELECT s.dnasamplename, e.Total_expression_level, e.Soluble_expression_level, s.id " +
            "FROM table1 e" +
            "JOIN table2 s on s.constructname = e.Clone_name" +
            "WHERE e.Total_expression_level like '0:%'");

planner.validate(parsed);

RelRoot relRoot = planner.rel(parsed);

If I try to execute the rel-Method of Planner directly, I get the error that I have to validate the query first. And that`s the point where I face troubles. I don't have a valid Schema. I just want to convert the query to a relational query plan. Is there a way to kind of skip the validation process?

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
hjk
  • 21
  • 3

1 Answers1

3

It's not possible to skip the validation process. The sql-to-relational converter relies on state that has previously been created by the validator.

People see that the validator throws an exception if the SQL is invalid and imagine that this is its only purpose; but building the state is equally important. The state includes of a map of the type of each SqlNode, expansions of "*" and "tableAlias.*" in SELECT clauses, and fully-qualified identifiers.

If your schema is missing or incomplete, you might try a similar approach to Apache Drill. If the validator asks for a particular column of a table, and the table does not have that column, the Calcite namespace for that table says "yes, I have that column!" and adds it. Thus, the validation process succeeds, and each namespace has a list of the extra columns needed by the query.

Julian Hyde
  • 1,239
  • 7
  • 10