1

the test code is :

public class HrSchema2{
    public List<Employee> emps = new ArrayList<Employee>();
    
    
    public HrSchema2() {
        
    }
}
public final String sql = "select count(e.empid) from hr.emps as e";

Class.forName("org.apache.calcite.jdbc.Driver");
Properties info = new Properties();
info.setProperty("lex", "JAVA");
try {
    Connection connection =
        DriverManager.getConnection("jdbc:calcite:", info);
    CalciteConnection calciteConnection =
        connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    Schema schema = new ReflectiveSchema(new HrSchema2());
    rootSchema.add("hr", schema);
    Statement statement = calciteConnection.createStatement();
    ResultSet resultSet = statement.executeQuery(sql);
    //print(resultSet);
    resultSet.close();
    statement.close();
    connection.close();
}catch(Exception ex) {
    ex.printStackTrace();
}

we got the following error:

java.sql.SQLException: Error while executing SQL "select count(e.empid) from hr.emps as e": From line 1, column 16 to line 1, column 20: Column 'empid' not found in table 'e'
goodbye from test runner

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Xiaobo Gu
  • 199
  • 1
  • 10

1 Answers1

0

Use org.apache.calcite.adapter.java.Array annotation on List<Employee> emps member variable in HrSchema2

@Array(component = Employee.class)
public List<Employee> emps = new ArrayList<>();

Refer CALCITE-4708 to get your use-case working.

Abhishek Dasgupta
  • 578
  • 1
  • 8
  • 20
  • but we still got this error java.sql.SQLException: Error while executing SQL "select count(*) from hr.emps": class com.xsmartware.javatest.calcite.Employee cannot be cast to class org.apache.calcite.runtime.FlatLists$ComparableList (com.xsmartware.javatest.calcite.Employee and org.apache.calcite.runtime.FlatLists$ComparableList are in unnamed module of loader 'app') – Xiaobo Gu Feb 02 '22 at 15:54
  • 1
    You are getting this exception since you haven't made the necessary changes in `ReflectiveSchema#fieldRelation` and `ReflectiveSchema#getElementType` (mentioned in the jira) – Abhishek Dasgupta Feb 02 '22 at 16:42