zhuyufeng created CALCITE-6934:
----------------------------------

             Summary: The examples for DDL extension on the official website 
cannot run
                 Key: CALCITE-6934
                 URL: https://issues.apache.org/jira/browse/CALCITE-6934
             Project: Calcite
          Issue Type: Bug
          Components: site
    Affects Versions: 1.39.0
            Reporter: zhuyufeng


When I was learning Calcite by browsing the official website, I found that the 
[examples|https://calcite.apache.org/docs/adapter.html#server] there couldn't 
run. I specifically wrote a demo to reproduce this issue.
{code:java}
// Some comments here
public class PlayCalciteDdl {
    public static void main(String[] args) throws Exception {
        Class.forName("org.apache.calcite.jdbc.Driver");
        Connection connection = 
DriverManager.getConnection("jdbc:calcite:parserFactory=org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl#FACTORY");
        CalciteConnection calciteConnection = 
connection.unwrap(CalciteConnection.class);

        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        Schema schema = new ReflectiveSchema(new HrSchema());
        rootSchema.add("hr", schema);

        Statement statement = calciteConnection.createStatement();
        statement.execute("CREATE TABLE t (i INTEGER, j VARCHAR(10))");
        statement.execute("INSERT INTO t VALUES (1, 'a'), (2, 'bc')");
        statement.execute("CREATE VIEW v AS SELECT * FROM t WHERE i > 1");

        ResultSet resultSet = statement.executeQuery("SELECT count(*) as num 
FROM v");
        while (resultSet.next()) {
            System.out.println(resultSet.getString("num"));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }

    public static class HrSchema {
        public final Employee[] emp = new Employee[]{
                new Employee("1" , "A", "Tom"),
                new Employee("1", "B", "Lisa")
        };
        public final Department[] dept = new Department[]{new Department("1")};
    }

    public static class Employee {
        public final String deptNo;
        public final String empId;
        public final String empName;

        public Employee(String deptNo, String empId, String empName) {
            this.deptNo = deptNo;
            this.empId = empId;
            this.empName = empName;
        }
    }

    public static class Department {
        public final String deptNo;

        public Department(String deptNo) {
            this.deptNo = deptNo;
        }
    }
}
{code}
it will throw an exception:
{code:java}
Caused by: java.lang.UnsupportedOperationException: DDL not supported: CREATE 
TABLE `T` (`I` INTEGER, `J` VARCHAR(10))
        at 
org.apache.calcite.server.DdlExecutor.lambda$static$0(DdlExecutor.java:28)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl.executeDdl(CalcitePrepareImpl.java:370)
        at 
org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:635){code}
after debugging, the parameter values in the JDBC connection link provided on 
the official website are incorrect
{code:java}
//wrong value
parserFactory=org.apache.calcite.sql.parser.ddl.SqlDdlParserImpl#FACTORY
{code}
The class SqlDdlParserImpl.FACTORY does not override the getDdlExecutor() 
method in the SqlParserImplFactory interface.

Therefore, the default logic defined in the interface is used during runtime, 
and the default logic will throw an exception.

The correct parameter value should be:
{code:java}
org.apache.calcite.server.ServerDdlExecutor#PARSER_FACTORY
{code}
After I replaced the parameter value in the JDBC connection with this one, it 
could run properly.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to