0

I am using Apache Calcite 1.38.0 along with the PostgreSQL JDBC driver version 42.7.2. When executing an SQL query through a JdbcSchema connection to my PostgreSQL database, I encountered an exception.

Code to initialize the connection:

BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(MY_PG_URL);
dataSource.setUsername(MY_PG_USERNAME);
dataSource.setPassword(MY_PG_PWD);

JdbcSchema.create(
    rootSchema, 
    "pgSchema", 
    dataSource, 
    null, 
    "public"
); 
// rootSchema is obtained from CalciteConnection.getRootSchema()

PostgreSQL table DDL:

CREATE TABLE test_table (
    value NUMERIC -- A NUMERIC column without specifying precision and scale
);

Query execution:

String sql = "SELECT * FROM pgSchema.test_table";
Statement statement = calciteConnection.createStatement();
statement.executeQuery(sql);

Exception encountered:

** org.apache.calcite.runtime.CalciteException: DECIMAL precision 0 must be between 1 and 19**

Investigation:

From my analysis, when PgDatabaseMetaData#getColumns retrieves column metadata, if the NUMERIC column does not explicitly specify precision and scale (i.e., its atttypmod is -1), TypeInfoCache#getPrecision and getScale return 0. This causes SqlTypeFactoryImpl#createSqlType in Calcite to fail validation for precision, leading to this exception.

Questions: 1. Is this a bug in Apache Calcite? 2. Is there any workaround to resolve this issue?

Any insights would be greatly appreciated!

4
  • It is not from Postgres: a) select 1.0::numeric; 1.0 b) From here Numeric type: numeric [...] up to 131072 digits before the decimal point; up to 16383 digits after the decimal point and The maximum precision that can be explicitly specified in a numeric type declaration is 1000. The ... between 1 and 19 seems to be coming from somewhere else, best bet Calcite. Commented Mar 24 at 4:29
  • Yes, this limitation comes from Calcite. I understand that Calcite should handle this case properly; otherwise, tables containing NUMERIC fields would become unusable.
    – Eric
    Commented Mar 24 at 5:41
  • It appears to be a known issue Commented Mar 24 at 11:19
  • 1) As @NickBarnes pointed out there is an Calcite issue you can comment on. 2) In meantime use a precision of 19 for the NUMERIC, given that is the maximum that Calcite supports. Commented Mar 24 at 15:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.