Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-862

JdbcSchema gives NullPointerException on non-standard column type

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.5.0
    • Component/s: None
    • Labels:
      None
    • Environment:

      Connected to: MySQL (version 5.5.43-0ubuntu0.14.04.1-log)
      Driver: MySQL-AB JDBC Driver (version mysql-connector-java-5.1.13 ( Revision: $

      {bzr.revision-id}

      ))
      MySQL sample database "sakila"

      Description

      09:06 ~$ sqlline.calcite.mysql 
      issuing: !connect jdbc:calcite:model=inline:{"version":1.0,"defaultSchema":"mysql","schemas":[{"name":"mysql","type":"jdbc","jdbcUrl":"jdbc:mysql://XXX/sakila","jdbcUser":"XXX","jdbcPassword":"XXX"}]} '' '' org.apache.calcite.jdbc.Driver
      Connecting to jdbc:calcite:model=inline:{"version":1.0,"defaultSchema":"mysql","schemas":[{"name":"mysql","type":"jdbc","jdbcUrl":"jdbc:mysql://XXX/sakila","jdbcUser":"XXX","jdbcPassword":"XXX"}]}
      Connected to: Calcite (version 1.3.0-incubating-SNAPSHOT)
      Driver: Calcite JDBC Driver (version 1.3.0-incubating-SNAPSHOT)
      Autocommit status: true
      Transaction isolation: TRANSACTION_READ_UNCOMMITTED
      sqlline version 1.0.1 by Marc Prud'hommeaux
      0: jdbc:calcite:model=inline:{"version":1.0,"> !tables
      +------------+--------------+-------------------------+---------------+----------+-----------+-------------+------------+-------------+
      | TABLE_CAT  | TABLE_SCHEM  |       TABLE_NAME        |  TABLE_TYPE   | REMARKS  | TYPE_CAT  | TYPE_SCHEM  | TYPE_NAME  | SELF_REFERE |
      +------------+--------------+-------------------------+---------------+----------+-----------+-------------+------------+-------------+
      |            | metadata     | COLUMNS                 | SYSTEM_TABLE  |          |           |             |            |             |
      |            | metadata     | TABLES                  | SYSTEM_TABLE  |          |           |             |            |             |
      |            | mysql        | actor                   | TABLE         |          |           |             |            |             |
      |            | mysql        | actor_info              | VIEW          |          |           |             |            |             |
      |            | mysql        | address                 | TABLE         |          |           |             |            |             |
      |            | mysql        | category                | TABLE         |          |           |             |            |             |
      |            | mysql        | city                    | TABLE         |          |           |             |            |             |
      |            | mysql        | country                 | TABLE         |          |           |             |            |             |
      |            | mysql        | customer                | TABLE         |          |           |             |            |             |
      |            | mysql        | customer_list           | VIEW          |          |           |             |            |             |
      |            | mysql        | film                    | TABLE         |          |           |             |            |             |
      |            | mysql        | film_actor              | TABLE         |          |           |             |            |             |
      |            | mysql        | film_category           | TABLE         |          |           |             |            |             |
      |            | mysql        | film_list               | VIEW          |          |           |             |            |             |
      |            | mysql        | film_text               | TABLE         |          |           |             |            |             |
      |            | mysql        | inventory               | TABLE         |          |           |             |            |             |
      |            | mysql        | language                | TABLE         |          |           |             |            |             |
      |            | mysql        | payment                 | TABLE         |          |           |             |            |             |
      |            | mysql        | rental                  | TABLE         |          |           |             |            |             |
      |            | mysql        | sales_by_film_category  | VIEW          |          |           |             |            |             |
      |            | mysql        | sales_by_store          | VIEW          |          |           |             |            |             |
      |            | mysql        | staff                   | TABLE         |          |           |             |            |             |
      |            | mysql        | staff_list              | VIEW          |          |           |             |            |             |
      |            | mysql        | store                   | TABLE         |          |           |             |            |             |
      +------------+--------------+-------------------------+---------------+----------+-----------+-------------+------------+-------------+
      0: jdbc:calcite:model=inline:{"version":1.0,"> !columns country
      +------------+--------------+-------------+--------------+------------+---------------------------------------------------------------+
      | TABLE_CAT  | TABLE_SCHEM  | TABLE_NAME  | COLUMN_NAME  | DATA_TYPE  |                                      TYPE_NAME                |
      +------------+--------------+-------------+--------------+------------+---------------------------------------------------------------+
      |            | mysql        | country     | country_id   | 5          | SMALLINT NOT NULL                                             |
      |            | mysql        | country     | country      | 12         | VARCHAR(50) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en |
      |            | mysql        | country     | last_update  | 93         | TIMESTAMP(0) NOT NULL                                         |
      +------------+--------------+-------------+--------------+------------+---------------------------------------------------------------+
      0: jdbc:calcite:model=inline:{"version":1.0,"> !columns film   
      Error: exception while executing query: null (state=,code=0)
      java.sql.SQLException: exception while executing query: null
      	at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
      	at org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:432)
      	at org.apache.calcite.avatica.AvaticaConnection.createResultSet(AvaticaConnection.java:485)
      	at org.apache.calcite.avatica.AvaticaDatabaseMetaData.getColumns(AvaticaDatabaseMetaData.java:621)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:497)
      	at sqlline.SqlLine$Reflector.invoke(Unknown Source)
      	at sqlline.SqlLine$Commands.metadata(Unknown Source)
      	at sqlline.SqlLine$Commands.columns(Unknown Source)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:497)
      	at sqlline.SqlLine$ReflectiveCommandHandler.execute(Unknown Source)
      	at sqlline.SqlLine.dispatch(Unknown Source)
      	at sqlline.SqlLine.begin(Unknown Source)
      	at sqlline.SqlLine.mainWithInputRedirection(Unknown Source)
      	at sqlline.SqlLine.main(Unknown Source)
      Caused by: java.lang.NullPointerException
      	at org.apache.calcite.adapter.jdbc.JdbcSchema.sqlType(JdbcSchema.java:291)
      	at org.apache.calcite.adapter.jdbc.JdbcSchema.getRelDataType(JdbcSchema.java:280)
      	at org.apache.calcite.adapter.jdbc.JdbcSchema.getRelDataType(JdbcSchema.java:245)
      	at org.apache.calcite.adapter.jdbc.JdbcTable.getRowType(JdbcTable.java:98)
      	at org.apache.calcite.jdbc.CalciteMetaImpl.columns(CalciteMetaImpl.java:474)
      	at org.apache.calcite.jdbc.CalciteMetaImpl$6.apply(CalciteMetaImpl.java:323)
      	at org.apache.calcite.jdbc.CalciteMetaImpl$6.apply(CalciteMetaImpl.java:321)
      	at org.apache.calcite.linq4j.EnumerableDefaults$8$1.moveNext(EnumerableDefaults.java:1680)
      	at org.apache.calcite.linq4j.EnumerableDefaults$17$1.moveNext(EnumerableDefaults.java:2337)
      	at org.apache.calcite.linq4j.Linq4j$EnumeratorIterator.<init>(Linq4j.java:677)
      	at org.apache.calcite.linq4j.Linq4j.enumeratorIterator(Linq4j.java:103)
      	at org.apache.calcite.linq4j.AbstractEnumerable.iterator(AbstractEnumerable.java:33)
      	at org.apache.calcite.linq4j.Linq4j$IterableEnumerable.iterator(Linq4j.java:551)
      	at org.apache.calcite.avatica.MetaImpl.createCursor(MetaImpl.java:93)
      	at org.apache.calcite.avatica.AvaticaResultSet.execute(AvaticaResultSet.java:187)
      	at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:63)
      	at org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:42)
      	at org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:430)
      	... 18 more
      

        Activity

        Hide
        mprudhom Marc Prud'hommeaux added a comment -

        I'm guessing that this is because the "film" table has a column "release_year" that has DATA_TYPE 91 ("TYPE_NAME"="YEAR"), which is not a standard entry in java.sql.Types, and so org.apache.calcite.sql.type.SqlTypeName.getNameForJdbcType(int) is returning null for it, which is causing the NPE in the switch on JdbcSchema.sqlType(). A simple guard against null might help, although I'm not sure what other bad things will then happen with an unrecognized type.

        That being said, many databases use their own type constants, so they should be handled gracefully.

        Show
        mprudhom Marc Prud'hommeaux added a comment - I'm guessing that this is because the "film" table has a column "release_year" that has DATA_TYPE 91 ("TYPE_NAME"="YEAR"), which is not a standard entry in java.sql.Types, and so org.apache.calcite.sql.type.SqlTypeName.getNameForJdbcType(int) is returning null for it, which is causing the NPE in the switch on JdbcSchema.sqlType(). A simple guard against null might help, although I'm not sure what other bad things will then happen with an unrecognized type. That being said, many databases use their own type constants, so they should be handled gracefully.
        Hide
        mprudhom Marc Prud'hommeaux added a comment -

        Having SqlTypeName.getNameForJdbcType return ANY if the type isn't found fixes this problem so I've made a PR for it:

        https://github.com/apache/incubator-calcite/pull/125

        Show
        mprudhom Marc Prud'hommeaux added a comment - Having SqlTypeName.getNameForJdbcType return ANY if the type isn't found fixes this problem so I've made a PR for it: https://github.com/apache/incubator-calcite/pull/125
        Hide
        julianhyde Julian Hyde added a comment -

        Basically good. I think that getNameForJdbcType should return null and the code that consumes it - JdbcSchema.sqlType() - should convert the null to ANY. I've made that change, am rebasing & testing, and will commit shortly.

        Show
        julianhyde Julian Hyde added a comment - Basically good. I think that getNameForJdbcType should return null and the code that consumes it - JdbcSchema.sqlType() - should convert the null to ANY. I've made that change, am rebasing & testing, and will commit shortly.
        Show
        julianhyde Julian Hyde added a comment - Fixed in http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/f98ed96f . Thanks for the patch, Marc Prud'hommeaux !
        Hide
        jcamachorodriguez Jesus Camacho Rodriguez added a comment -

        Resolved in release 1.5.0 (2015-11-10)

        Show
        jcamachorodriguez Jesus Camacho Rodriguez added a comment - Resolved in release 1.5.0 (2015-11-10)

          People

          • Assignee:
            julianhyde Julian Hyde
            Reporter:
            mprudhom Marc Prud'hommeaux
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development