table not found with apache calcite

2019-01-26 00:35发布

问题:

I am trying to do some basic things with calcite to understand the framework. I have setup a simple example that is supposed to read from 2 json files. My model looks like

{
  version: '1.0',
  defaultSchema: 'PEOPLE',
  schemas: [
    {
      name: 'PEOPLE',
      type: 'custom',
      factory: 'demo.JsonSchemaFactory',
      operand: {
        directory: '/..../calcite-json/src/test/resources/files'
      }
    }
  ]
}

In my test, it seems that the model is being loaded fine because when I pull the database metadata information, I can see that my file is being loaded as a table under PEOPLE schema. But then right after that statement I am trying to do a select * from that table and I get an error that table was not found.

> --
null
PEOPLE
a
TABLE
-->
Jun 29, 2015 8:53:30 AM org.apache.calcite.sql.validate.SqlValidatorException <init>
SEVERE: org.apache.calcite.sql.validate.SqlValidatorException: Table 'A' not found
Jun 29, 2015 8:53:30 AM org.apache.calcite.runtime.CalciteException <init>
SEVERE: org.apache.calcite.runtime.CalciteContextException: At line 1, column 26: Table 'A' not found

The first line in the output shows the tables from database metadata "-- null PEOPLE a TABLE -->". This shows that a table "a" is present under schema "people" and is of the type "table".

My test code looks like this

@Test
public void testModel() throws SQLException {
    Properties props = new Properties();
    props.put("model", getPath("/model.json"));
    System.out.println("model = " + props.get("model"));
    Connection conn = DriverManager.getConnection("jdbc:calcite:", props);

    DatabaseMetaData md = conn.getMetaData();
    ResultSet tables = md.getTables(null, "PEOPLE", "%", null);
    while (tables.next()) {
        System.out.println("--");
        System.out.println(tables.getString(1));
        System.out.println(tables.getString(2));
        System.out.println(tables.getString(3));
        System.out.println(tables.getString(4));
        System.out.println("-->");
    }

    Statement stat = conn.createStatement();
    stat.execute("select _MAP['name'] from a");

    stat.close();
    conn.close();
}

Any ideas why I am not able to do a select on the loaded table?

Another interesting thing I noticed is that for 1 file, Schema.getTableMap is being called 4 times.

The complete code for the project can be found on github

回答1:

The problem is case-sensitivity. Because you did not enclose the table name in double-quotes, Calcite's SQL parser converted it to upper case. Because the file is called 'a.json', the table is also called 'a', whereas your query is looking for a table called 'A'.

The solution is to write your query as follows:

select _MAP['name'] from "a"

This becomes:

stat.execute("select _MAP['name'] from \"a\"");

when you embed it in Java.