Repository navigation
Closed
Copy link
Labels
clientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsenhancement
Milestone
Description
Description
The API for query parameters with an Instant generates a query which can not be parsed by the server.
Steps to reproduce
See example below.
Error Log or Exception StackTrace
com.clickhouse.client.api.ServerException: Code: 457. DB::Exception: Value 2025-06-16T11:54:26.413588562Z cannot be parsed as DateTime64 for query parameter 'year_start' because it isn't parsed completely: only 29 of 30 bytes was parsed: 2025-06-16T11:54:26.413588562. (BAD_QUERY_PARAMETER) (version 25.3.3.42 (official build))
Expected Behaviour
It should work smoothly.
Code Example
@Test
void testParamsWithInstant() throws Exception {
final Client client = new Client.Builder()
.addEndpoint(Protocol.HTTP, "localhost", 8123, false)
.setUsername("default")
.setPassword("")
.build();
final Map<String, Object> params = Map.of("year_start", Instant.now());
// This query should not throw an exception
client.query("SELECT * FROM system.tables WHERE metadata_modification_time < {year_start:DateTime64}", params).get();
}Test is passing when serializing the parameter to a String, e.g. "2025-06-16 11:54:26.413588562".
A related query with an array also fails with the following parsing error:
com.clickhouse.client.api.ServerException: Code: 130. DB::Exception: Cannot read array from text, expected comma or end of array, found '-': value [2025-06-16T12:03:29.844112439Z] cannot be parsed as Array(DateTime64) for query parameter 'year_start'. (CANNOT_READ_ARRAY_FROM_TEXT) (version 25.3.3.42 (official build))
The error above was done with:
final Map<String, Object> params = Map.of("year_start", List.of(Instant.now()));
// This query should not throw an exception
client.query("SELECT * FROM system.tables WHERE metadata_modification_time in {year_start:Array(DateTime64)}", params).get();
Configuration
Client Configuration
See above.
Environment
- Cloud
- Client version:
- Language version:
- OS:
ClickHouse Server
- ClickHouse Server version: 25.3.3.42
- ClickHouse Server non-default settings, if any:
CREATE TABLEstatements for tables involved:- Sample data for all these tables, use clickhouse-obfuscator if necessary
Metadata
Metadata
Assignees
Labels
clientModule: the client (client-v2; with legacy: the v1 client)data-typesArea: type names, value encoding and decoding, row mapping, data formatsenhancement
Activity
enqueue commentedon Jul 10, 2025
Great bug report 👏
The reason for this can be found in HttpAPIClientHelper.java#L557 ff.:
This implementation simply invokes
toString()on any parameter value supplied, which leads to incompatible behavior.I would try to solve this issue, e.g. by using
SerializerUtilsmethods. It would be great if we knew the ClickHouse data type, but that would require parsing the parameterized query -> no. Would that be OK for you @chernser ? Any further hints?Bonus: Check that Unicode parameters work.
chernser commentedon Jul 10, 2025
@enqueue thank you for helping with this issue!
Yes, we should do NO query parsing on client side.
We used just
toStringbecause it should not be too complex: in most cases application should care about converting java objects to a string.Lets start with adding utility methods to
com.clickhouse.client.api.DataTypeUtils.I do want to keep main logic thin because it affects performance.
You may use a
SerializerUtilsbut this class might be refactored soon.enqueue commentedon Jul 10, 2025
@chernser OK, I will try and use / enhance
com.clickhouse.client.api.DataTypeUtils. This would allow us to test using simple JUnit tests.Perhaps we can re-use some code from JDBC
PreparedStatementImpl.encodeObject()or fromSerializerUtils. I will first have to understand howDateTime64works with different precision etc.enqueue commentedon Jul 12, 2025
It's not as easy as it looks. Without parsing statement we do not know if target of parameter is DateTime64 or not.
I tried different parameter values via curl, and I did not find a satisfactory solution.
If we assumed DateTime64, we could pass decimal parameter value. But this would not work for Date, DateTime, or DateTime32 column types.
Formatting Instant as some date / time string is not nice, either. Even if setting date_time_input_format setting to "best_effort", I was not able to find a String representation that would satisfy all requirements.
chernser commentedon Jul 12, 2025
@enqueue
If I'm understood the problem correctly it is: application doesn't know haw to format Instance/Date/Time into Date/Date64/DateTime64.
So we need to provide set of conversion methods for most common cases + give access to formatters we use in this methods.
As the result we would get something
Conversion and type guessing is mostly always application specific and client may not handle everything. But client should provide tools. like this methods.
In the future we may create a request builder what will be separate from the main code to not add extra logic. Another option is to use something like JOOQ that helps to build requests from application data.
enqueue commentedon Jul 21, 2025
Tried to simply use decimal everywhere, because we do not know targeted precision. However, I probably ran into some overflow / parsing issue on ClickHouse side. Will investigate further.