- Notifications
You must be signed in to change notification settings - Fork 614
Closed
Description
Describe the bug
The ternary ? in "select true ? 1 : 0 as foo" confuses the PreparedStatement mechanism.
Working ternary in Statement
;; working example of the ternary against a Statement clj -Sdeps '{:deps {com.clickhouse/clickhouse-jdbc {:mvn/version "0.8.4"}}}' user=> (let [jdbc-url "jdbc:clickhouse://localhost:8123" sql "select true ? 1 : 0 as foo"] (with-open [connection (java.sql.DriverManager/getConnection jdbc-url "default" "password") statement (.createStatement connection)] (if (.execute statement sql) (let [rs (.getResultSet statement)] (while (.next rs) (println (.getInt rs "foo")))) (println "no results from sql: \n" sql)))) 1 nilFailing ternary in PreparedStatement with no inputs
user=> (let [jdbc-url "jdbc:clickhouse://localhost:8123" sql "select false ? 1 : 0 as foo"] (with-open [connection (java.sql.DriverManager/getConnection jdbc-url "default" "password") ps (.prepareStatement connection sql) rs (.executeQuery ps)] (while (.next rs) (println (.getInt rs "foo"))))) Execution error (ServerException) at com.clickhouse.client.api.internal.HttpAPIClientHelper/readError (HttpAPIClientHelper.java:371). Code: 62. DB::Exception: Syntax error: failed at position 19 ('1'): 1 : 0 as foo. Expected one of: token, Comma, FROM, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, QUALIFY, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, INTO OUTFILE, FORMAT, ParallelWithClause, PARALLEL WITH, end of query. (SYNTAX_ERROR) (version 25.1.3.23 (official build)) Failing ternary with one input in PreparedStatement
user=> (let [jdbc-url "jdbc:clickhouse://localhost:8123" sql "select false ? 1 : 0 as foo where 1 = ?"] (with-open [connection (java.sql.DriverManager/getConnection jdbc-url "default" "password") ps (doto (.prepareStatement connection sql) (.setInt 1 1)) rs (.executeQuery ps)] (while (.next rs) (println (.getInt rs "foo"))))) Execution error (ServerException) at com.clickhouse.client.api.internal.HttpAPIClientHelper/readError (HttpAPIClientHelper.java:371). Code: 62. DB::Exception: Syntax error: failed at position 14 ('1'): 1 1 : 0 as foo where 1 = null. Expected one of: OR, AND, IS NOT DISTINCT FROM, IS NULL, IS NOT NULL, BETWEEN, NOT BETWEEN, LIKE, ILIKE, NOT LIKE, NOT ILIKE, REGEXP, IN, NOT IN, GLOBAL IN, GLOBAL NOT IN, MOD, DIV, alias, AS, identifier, token, Comma, FROM, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, QUALIFY, ORDER BY, LIMIT, OFFSET, FETCH, SETTINGS, UNION, EXCEPT, INTERSECT, INTO OUTFILE, FORMAT, ParallelWithClause, PARALLEL WITH, end of query. (SYNTAX_ERROR) (version 25.1.3.23 (official build)) user=> Working example with clickhouse.jdbc.v1=true
❯ clj -Sdeps '{:deps {com.clickhouse/clickhouse-jdbc {:mvn/version "0.8.4"}}}' -J-Dclickhouse.jdbc.v1=true user=> (let [jdbc-url "jdbc:clickhouse://localhost:8123" sql "select false ? 1 : 0 as foo where 1 = ?"] (with-open [connection (java.sql.DriverManager/getConnection jdbc-url "default" "password") ps (doto (.prepareStatement connection sql) (.setInt 1 1)) rs (.executeQuery ps)] (while (.next rs) (println (.getInt rs "foo"))))) 0 nilExpected behaviour
These queries should all complete
Code example
// should be easily translatable from the raw sql strings above. "select true ? 1 : 0 as foo" "select false ? 1 : 0 as foo where 1 = ?"Error log
Configuration
Environment
- Client version: 0.8.4
- Language version: ?
- OS: osx
ClickHouse server
- ClickHouse Server version:
- ClickHouse Server non-default settings, if any:
docker run -p8123:8123 -p9000:9000 -e"CLICKHOUSE_PASSWORD=password" -it --rm clickhouse/clickhouse-server:latest chernser