最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
.postgresql.ds.PGSimpleDataSource cockroachDS = new .postgresql.ds.PGSimpleDataSource();
cockroachDS.setUrl("jdbc:postgresql://localhost:26257/sample");
cockroachDS.setUser("root");
cockroachDS.setPassword("");

// Connect to H2
.h2.jdbcx.JdbcDataSource h2DS = new .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.

发布评论

评论列表(0)

  1. 暂无评论