From a410f0cb75b0c58a1baa7f721c13db67a9c1e755 Mon Sep 17 00:00:00 2001 From: Lars Date: Sun, 22 Sep 2024 17:48:42 -0700 Subject: [PATCH] Fix metadata reflection for DECIMAL columns --- .../org/duckdb/DuckDBDatabaseMetaData.java | 4 +- src/test/java/org/duckdb/TestDuckDBJDBC.java | 54 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/duckdb/DuckDBDatabaseMetaData.java b/src/main/java/org/duckdb/DuckDBDatabaseMetaData.java index 7074b0ca..12bfe16a 100644 --- a/src/main/java/org/duckdb/DuckDBDatabaseMetaData.java +++ b/src/main/java/org/duckdb/DuckDBDatabaseMetaData.java @@ -878,8 +878,8 @@ public ResultSet getColumns(String catalogPattern, String schemaPattern, String + "column_name as 'COLUMN_NAME', " + makeDataMap("regexp_replace(c.data_type, '\\(.*\\)', '')", "DATA_TYPE") + ", " + "c.data_type AS 'TYPE_NAME', " - + "NULL AS 'COLUMN_SIZE', NULL AS 'BUFFER_LENGTH', " - + "numeric_precision AS 'DECIMAL_DIGITS', " + + "numeric_precision AS 'COLUMN_SIZE', NULL AS 'BUFFER_LENGTH', " + + "numeric_scale AS 'DECIMAL_DIGITS', " + "10 AS 'NUM_PREC_RADIX', " + "CASE WHEN is_nullable = 'YES' THEN 1 else 0 END AS 'NULLABLE', " + "COLUMN_COMMENT as 'REMARKS', " diff --git a/src/test/java/org/duckdb/TestDuckDBJDBC.java b/src/test/java/org/duckdb/TestDuckDBJDBC.java index 6fc1907a..b0cbcfe1 100644 --- a/src/test/java/org/duckdb/TestDuckDBJDBC.java +++ b/src/test/java/org/duckdb/TestDuckDBJDBC.java @@ -1866,8 +1866,8 @@ public static void test_schema_reflection() throws Exception { assertEquals(rs.getInt(5), Types.INTEGER); assertEquals(rs.getString("TYPE_NAME"), "INTEGER"); assertEquals(rs.getString(6), "INTEGER"); - assertNull(rs.getObject("COLUMN_SIZE")); - assertNull(rs.getObject(7)); + assertEquals(rs.getInt("COLUMN_SIZE"), 32); // this should 10 for INTEGER + assertEquals(rs.getInt(7), 32); assertNull(rs.getObject("BUFFER_LENGTH")); assertNull(rs.getObject(8)); @@ -1889,8 +1889,8 @@ public static void test_schema_reflection() throws Exception { assertEquals(rs.getInt(5), Types.INTEGER); assertEquals(rs.getString("TYPE_NAME"), "INTEGER"); assertEquals(rs.getString(6), "INTEGER"); - assertNull(rs.getObject("COLUMN_SIZE")); - assertNull(rs.getObject(7)); + assertEquals(rs.getInt("COLUMN_SIZE"), 32); + assertEquals(rs.getInt(7), 32); assertNull(rs.getObject("BUFFER_LENGTH")); assertNull(rs.getObject(8)); assertEquals(rs.getString("REMARKS"), "a column"); @@ -1911,8 +1911,8 @@ public static void test_schema_reflection() throws Exception { assertEquals(rs.getInt(5), Types.INTEGER); assertEquals(rs.getString("TYPE_NAME"), "INTEGER"); assertEquals(rs.getString(6), "INTEGER"); - assertNull(rs.getObject("COLUMN_SIZE")); - assertNull(rs.getObject(7)); + assertEquals(rs.getInt("COLUMN_SIZE"), 32); + assertEquals(rs.getInt(7), 32); assertNull(rs.getObject("BUFFER_LENGTH")); assertNull(rs.getObject(8)); @@ -1933,6 +1933,48 @@ public static void test_schema_reflection() throws Exception { conn.close(); } + public static void test_column_reflection() throws Exception { + Connection conn = DriverManager.getConnection(JDBC_URL); + Statement stmt = conn.createStatement(); + stmt.execute("CREATE TABLE a (a DECIMAL(20,5), b CHAR(10), c VARCHAR(30), d LONG)"); + + DatabaseMetaData md = conn.getMetaData(); + ResultSet rs; + rs = md.getColumns(null, null, "a", null); + assertTrue(rs.next()); + assertEquals(rs.getString("TABLE_NAME"), "a"); + assertEquals(rs.getString("COLUMN_NAME"), "a"); + assertEquals(rs.getInt("DATA_TYPE"), Types.DECIMAL); + assertEquals(rs.getString("TYPE_NAME"), "DECIMAL(20,5)"); + assertEquals(rs.getString(6), "DECIMAL(20,5)"); + assertEquals(rs.getInt("COLUMN_SIZE"), 20); + assertEquals(rs.getInt("DECIMAL_DIGITS"), 5); + + assertTrue(rs.next()); + assertEquals(rs.getString("COLUMN_NAME"), "b"); + assertEquals(rs.getInt("DATA_TYPE"), Types.VARCHAR); + assertEquals(rs.getString("TYPE_NAME"), "VARCHAR"); + assertNull(rs.getObject("COLUMN_SIZE")); + assertNull(rs.getObject("DECIMAL_DIGITS")); + + assertTrue(rs.next()); + assertEquals(rs.getString("COLUMN_NAME"), "c"); + assertEquals(rs.getInt("DATA_TYPE"), Types.VARCHAR); + assertEquals(rs.getString("TYPE_NAME"), "VARCHAR"); + assertNull(rs.getObject("COLUMN_SIZE")); + assertNull(rs.getObject("DECIMAL_DIGITS")); + + assertTrue(rs.next()); + assertEquals(rs.getString("COLUMN_NAME"), "d"); + assertEquals(rs.getInt("DATA_TYPE"), Types.BIGINT); + assertEquals(rs.getString("TYPE_NAME"), "BIGINT"); + assertEquals(rs.getInt("COLUMN_SIZE"), 64); // should be 19 + assertEquals(rs.getInt("DECIMAL_DIGITS"), 0); + + rs.close(); + conn.close(); + } + public static void test_time_tz() throws Exception { try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement s = conn.createStatement()) { s.execute("set timezone = 'UTC'");