java - Calcite SQL Join Query Fails with "Multiple entries with same key" Error - Stack Overflow

admin2025-04-18  3

I'm trying to execute a SQL join query across two different databases (CockroachDB and H2) using Apache Calcite. The query works fine when executed directly on the respective databases, but when I try to run it through Calcite, I encounter the following error:

Error executing query: Error while executing SQL "EXPLAIN PLAN FOR SELECT c.customer_name, o.order_id, o.order_date FROM CRDB.customers c JOIN H2DB.orders o ON c.customer_id = o.customer_id": Multiple entries with same key: primary=JdbcTable {primary} and primary=JdbcTable {primary}

Here is the relevant part of my Java code:

// Initialize Calcite connection with case-sensitive settings
Properties info = new Properties();
info.setProperty("lex", Lex.MYSQL.name());
info.setProperty("caseSensitive", "true");
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();

// Connect to CockroachDB
org.postgresql.ds.PGSimpleDataSource cockroachDS = new org.postgresql.ds.PGSimpleDataSource();
cockroachDS.setUrl("jdbc:postgresql://localhost:26257/sample");
cockroachDS.setUser("root");
cockroachDS.setPassword("");

// Connect to H2
org.h2.jdbcx.JdbcDataSource h2DS = new org.h2.jdbcx.JdbcDataSource();
h2DS.setURL("jdbc:h2:testdata;AUTO_SERVER=TRUE");
h2DS.setUser("");
h2DS.setPassword("");

// Add schemas
rootSchema.add("CRDB", 
    JdbcSchema.create(rootSchema, "CRDB", cockroachDS, 
        "sample",  // catalog
        "public")); // schema

rootSchema.add("H2DB", 
    JdbcSchema.create(rootSchema, "H2DB", h2DS,
        "DEFAULT",  // catalog
        "PUBLIC")); // schema

// Execute join query with simplified schema references
String sql = 
    "SELECT c.customer_name, o.order_id, o.order_date " +
    "FROM CRDB.customers c " +
    "JOIN H2DB.orders o ON c.customer_id = o.customer_id";

try (Statement statement = calciteConnection.createStatement()) {
    System.out.println("Executing query: " + sql);
    
    // Enable debug logging
    statement.execute("EXPLAIN PLAN FOR " + sql);
    ResultSet explainRs = statement.getResultSet();
    System.out.println("\nQuery Plan:");
    while (explainRs.next()) {
        System.out.println(explainRs.getString(1));
    }
    
    // Execute actual query
    ResultSet rs = statement.executeQuery(sql);

    // Print results
    System.out.println("\nQuery Results:");
    while (rs.next()) {
        System.out.printf("Customer: %s, Order ID: %s, Date: %s%n",
            rs.getString(1),
            rs.getString(2),
            rs.getString(3));
    }
} catch (SQLException e) {
    System.err.println("Error executing query: " + e.getMessage());
    e.printStackTrace();
}

// Clean up
connection.close();

Additional Information: Calcite Version: 1.38.0 CockroachDB Version: 24.* H2 Database Version: 2.3 Java Version: 21

What I've Tried:

  1. Verified that the tables customers and orders exist in their respective databases.
  2. Ensured that the connection details for both databases are correct.
  3. Tried running the query directly on both databases, which works fine.

Question:

What could be causing the "Multiple entries with same key" error in Calcite, and how can I resolve it to successfully execute the join query across the two databases? If possible please correct the code.

转载请注明原文地址:http://anycun.com/QandA/1744967028a90153.html