在MySql上进行方解,试图将id列转换为bigint

时间:2017-11-15 10:44:38

标签: apache-calcite

我尝试使用Calcite在同一查询中查询MySql和Vertica:

MySql表:CREATE TABLE tableA (id INT(11), name VARCHAR(5), PRIMARY KEY(id));

Vertica表:CREATE TABLE tableB (id INTEGER NOT NULL, name VARCHAR(20), PRIMARY KEY (id));

使用Calcite运行以下查询时:

statement.executeQuery(
            "SELECT a.\"name\", b.\"name\" " +
            "FROM \"mysqlschema\".\"tableA\" as a " +
            "INNER JOIN \"verticaschema\".\"tableB\" as b ON a.\"id\" = b.\"id\" " +
            "WHERE a.\"id\" = 1 "))

我得到了:

    Exception in thread "main" java.sql.SQLException: Error while executing SQL "SELECT a."name", b."name" FROM "mysqlschema"."tableA" as a INNER JOIN "verticaschema"."tableB" as b ON a."id" = b."id" WHERE a."id" = 1 ": while executing SQL [SELECT `id`, `name`, CAST(`id` AS BIGINT) AS `id0`
FROM `mysqlschema`.`tableA`
WHERE `id` = 1]
    at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
    at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
    at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:156)
    at org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:218)
    at com.test.TestCalcite.main(TestCalcite.java:73)
Caused by: java.lang.RuntimeException: while executing SQL [SELECT `id`, `name`, CAST(`id` AS BIGINT) AS `id0`
FROM `mysqlschema`.`tableA`
WHERE `id` = 1]
    at org.apache.calcite.runtime.ResultSetEnumerable.enumerator(ResultSetEnumerable.java:153)
    at org.apache.calcite.linq4j.EnumerableDefaults$10$1.<init>(EnumerableDefaults.java:1100)
    at org.apache.calcite.linq4j.EnumerableDefaults$10.enumerator(EnumerableDefaults.java:1099)
    at Baz$6$1.<init>(Unknown Source)
    at Baz$6.enumerator(Unknown Source)
    at org.apache.calcite.linq4j.AbstractEnumerable.iterator(AbstractEnumerable.java:33)
    at org.apache.calcite.avatica.MetaImpl.createCursor(MetaImpl.java:90)
    at org.apache.calcite.avatica.AvaticaResultSet.execute(AvaticaResultSet.java:206)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:67)
    at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:44)
    at org.apache.calcite.avatica.AvaticaConnection$1.execute(AvaticaConnection.java:630)
    at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:607)
    at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:638)
    at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:149)
    ... 2 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BIGINT) AS `id0`
FROM `mysqlschema`.`tableA`
WHERE `id` = 1' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at org.apache.calcite.runtime.ResultSetEnumerable.enumerator(ResultSetEnumerable.java:143)

转换MySql表中的id列可以解决这个问题,但我无法转换为我处理无法更改的现有表格。

关于如何解决这个问题的任何想法?

由于

1 个答案:

答案 0 :(得分:0)

通过在B中转换id来解决:

ON a."id" = CAST(b."b" AS INT)