Skip to content

DuckDBJ: DuckDBDatabaseMetaData.getPrimaryKeys always throws SQLFeatureNotSupportedException #6269

@rpbouman

Description

@rpbouman

What happens?

Calling getPrimaryKeys on DatabaseMetadata always throws a SQLFeatureNotSupportedException.

https://github.com/duckdb/duckdb/blob/master/tools/jdbc/src/main/java/org/duckdb/DuckDBDatabaseMetaData.java#L791

To Reproduce

Testcase:

  public void test_get_primary_keys() throws SQLException {
    Object[][] testData = new Object[12][6];
    int testDataIndex = 0;
    Connection conn = DriverManager.getConnection("jdbc:duckdb:");
    String catalog = conn.getCatalog();
    Statement stmt = conn.createStatement();
    for (int schemaNumber = 1; schemaNumber <= 2; schemaNumber++) {
      String schemaName = "schema" + schemaNumber;
      stmt.executeUpdate("CREATE SCHEMA " + schemaName);
      stmt.executeUpdate("SET SCHEMA = '" + schemaName + "'");
      for (int tableNumber = 1; tableNumber <= 3; tableNumber++) {
        String tableName = "table" + tableNumber;
        String columns = null;
        String pk = null;
        for (int columnNumber = 1; columnNumber <= tableNumber; columnNumber++) {
          String columnName = "column" + columnNumber;
          String columnDef = columnName + " int not null";
          columns = columns == null ? columnDef : columns + "," + columnDef;
          pk = pk == null ? columnName : pk + "," + columnName;
          testData[testDataIndex++] = new Object[] {
            catalog,
            schemaName,
            tableName,
            columnName,
            columnNumber,
            null
          };
        }
        stmt.executeUpdate(
          "CREATE TABLE " + tableName + "(" + columns + 
          ",PRIMARY KEY(" + pk + ") )"
        );
      }
    }
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    pw.println("WITH constraint_columns as (");
    pw.println("select");
    pw.println("  database_name as \"TABLE_CAT\"");
    pw.println(", schema_name as \"TABLE_SCHEM\"");
    pw.println(", table_name as \"TABLE_NAME\"");
    pw.println(", unnest(constraint_column_names) as \"COLUMN_NAME\"");
    pw.println(", cast(null as varchar) as \"PK_NAME\"");
    pw.println("from duckdb_constraints");
    pw.println("where constraint_type = 'PRIMARY KEY'");
    pw.println(")");
    pw.println("SELECT \"TABLE_CAT\"");
    pw.println(", \"TABLE_SCHEM\"");
    pw.println(", \"TABLE_NAME\"");
    pw.println(", \"COLUMN_NAME\"");
    pw.println(", cast(row_number() over ");
    pw.println("(partition by \"TABLE_CAT\", \"TABLE_SCHEM\", \"TABLE_NAME\") as int) as \"KEY_SEQ\"");
    pw.println(", \"PK_NAME\"");
    pw.println("FROM constraint_columns");
    pw.println("ORDER BY TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ");
    
    ResultSet resultSet = stmt.executeQuery(sw.toString());
    for (testDataIndex = 0; testDataIndex < testData.length; testDataIndex++) {
      assertTrue("Expected a row at position " + testDataIndex, resultSet.next());
      Object[] testDataRow = testData[testDataIndex];
      for (int columnIndex = 0; columnIndex < testDataRow.length; columnIndex++) {
        Object value = testDataRow[columnIndex];
        if (value == null || value instanceof String) {
          String columnValue = resultSet.getString(columnIndex + 1);
          assertTrue(
            "row value " + testDataIndex + ", " + columnIndex + 
            " " + value + " should equal column value " + columnValue, 
            value == null ? columnValue == null : value.equals(columnValue)
          );
        }
        else {
          int testValue = ((Integer)value).intValue();
          int columnValue = resultSet.getInt(columnIndex + 1);
          assertTrue(
            "row value " + testDataIndex + ", " + columnIndex + 
            " " + testValue + " should equal column value " + columnValue,
            testValue == columnValue
          );
        }
      }
    }    
  }

OS:

Windows 10 Pro

DuckDB Version:

v0.6.0 2213f9c

DuckDB Client:

DuckDBJ (jdbc)

Full Name:

Roland Bouman

Affiliation:

Just-Bi.nl (an EPAM company)

Have you tried this on the latest master branch?

  • I agree

Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?

  • I agree

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions