clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

Dash in database name is not supported by V2 client

Open k-popov opened this issue 5 months ago • 1 comments

It looks like parsing of JDBC URL does not support dash (hyphen) in database name. This results in Code: 81. DB::Exception: Database test does not exist. when trying to connect to database named test-dash.

Found in 0.8.4 but this part of code in master is the same.

RegEx at https://github.com/ClickHouse/clickhouse-java/blob/b2692a32f8de5f96b4a4ec2319057df0514c0c58/jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java#L129 shows the following when testing:

>>> import re
>>> re.match('(https?:)?\\/\\/([\\w\\.\\-]+|\\[[0-9a-fA-F:]+\\]):?([\\d]*)(?:\\/([\\w]+))?\\/?\\??(.*)$', 'https://host-name:8123/test-dash?param=pampam').groups()
('https:', 'host-name', '8123', 'test', '-dash?param=pampam')

(Tested in python as it was a quick test but I assume \w is the same) Note the 4th group only has test and -dash is already in 5th group where params should reside.

Adding - to characters set seems to fix parsing:

> re.match('(https?:)?\\/\\/([\\w\\.\\-]+|\\[[0-9a-fA-F:]+\\]):?([\\d]*)(?:\\/([\\w-]+))?\\/?\\??(.*)$', 'https://host-name:8123/test-dash?param=pampam').groups()
('https:', 'host-name', '8123', 'test-dash', 'param=pampam')

Here the 4th group contains full DB name test-dash

There was already a similar issue long ago: #251 but that was for client V1.

k-popov avatar Jun 19 '25 13:06 k-popov

Repro with 0.9.0:

$ clj -Sdeps '{:deps {com.clickhouse/clickhouse-jdbc {:mvn/version "0.9.0"}}}'
user=> (let [conn (java.sql.DriverManager/getConnection "jdbc:clickhouse://localhost:8123/my-db" "default" "")
             stmt (.prepareStatement conn "SELECT * FROM `my-table`;")]
         (let [rs (.executeQuery stmt)]
           (while (.next rs)
             (println (.getString rs 1)))))
Execution error (ServerException) at com.clickhouse.client.api.internal.HttpAPIClientHelper/readError (HttpAPIClientHelper.java:371).
Code: 81. DB::Exception: Database my does not exist. (UNKNOWN_DATABASE) (version 25.2.2.39 (official build)) 

rileythomp avatar Jun 19 '25 17:06 rileythomp

Relying on own regular expressions is not so nice. I would vote for using Java API for parsing URIs or URLs.

enqueue avatar Jul 02 '25 14:07 enqueue

@chernser Wow, that was fast 🚀 Please run integration tests. I am still seeing some errors locally.

enqueue avatar Jul 07 '25 14:07 enqueue

Will do!

chernser avatar Jul 07 '25 14:07 chernser