ClickHouse / clickhouse-java

ClickHouse Java Clients & JDBC Driver
https://clickhouse.com
Apache License 2.0
1.45k stars 535 forks source link

[client-v2] InsertSettings::SetDatabase not working #1868

Closed Am-phi closed 3 weeks ago

Am-phi commented 1 month ago

Describe the bug

According to the javadoc in the InsertSettings the option addDatabase allows to query another database than the one defined in the client. Which is not the case

Steps to reproduce

  1. Create a client with any database
  2. Create a table in another database
  3. Create an insertSetting with the option setDatabase with the database where you created your table
  4. Insert data

Expected behaviour

It's expected that the data would be inserted in the correct table

Code example

Client client = new Client.Builder()
            .addEndpoint(Protocol.HTTP, "localhost", 8123, false)
            .setUsername("default")
            .setPassword("")
            .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "true").equals("true"))
            .build();
        final String tableName = "insert_settings_database_test";
        final String new_database = "new_database";
        client.query("CREATE DATABASE " + new_database);
        client.query("CREATE TABLE " + new_database + "." + tableName + " (Id UInt32, event_ts Timestamp, name String, p1 Int64, p2 String) ENGINE = MergeTree() ORDER BY ()");
        InsertSettings insertSettings = settings.setDatabase(new_database); ByteArrayOutputStream data = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(data);
        for (int i = 0; i < 1000; i++) {
            writer.printf("%d\t%s\t%s\t%d\t%s\n", i, "2021-01-01 00:00:00", "name" + i, i, "p2");
        }
        writer.flush();
        InsertResponse response = client.insert(tableName, new ByteArrayInputStream(data.toByteArray()),
            ClickHouseFormat.TSV, insertSettings).get(30, TimeUnit.SECONDS);

Error log

com.clickhouse.client.api.ServerException: Code: 60. DB::Exception: Table default.insert_settings_database_test does not exist. Maybe you meant new_database.insert_settings_database_test?. (UNKNOWN_TABLE) (version 24.9.2.42 (official build))

at com.clickhouse.client.api.internal.HttpAPIClientHelper.readError(HttpAPIClientHelper.java:314)
at com.clickhouse.client.api.internal.HttpAPIClientHelper.executeRequest(HttpAPIClientHelper.java:357)
at com.clickhouse.client.api.Client.lambda$insert$7(Client.java:1331)
at com.clickhouse.client.api.Client.runAsyncOperation(Client.java:1864)
at com.clickhouse.client.api.Client.insert(Client.java:1418

Configuration

Environment

Am-phi commented 1 month ago

Proposed PR -> [client-v2] Fix usage of setDatabase in InsertSettings