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!
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.NUMERIC
, given that is the maximum that Calcite supports.