Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata reflection for DECIMAL columns - with test #88

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/duckdb/DuckDBDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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', "
Expand Down
54 changes: 48 additions & 6 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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");
Expand All @@ -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));

Expand All @@ -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'");
Expand Down
Loading