question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

is it possible to specify DEFAULT value for PreparedStatements

See original GitHub issue

Table DDL

CREATE TABLE EXAMPLE
(
    id     Int64,
    date   DateTime64,
    number Int64 DEFAULT -1
) ENGINE = MergeTree
      ORDER BY (id);

From ClickHouse’s documentation, looking to specify DEFAULT when inserting multiple values that should utilize the default value defined for the column.

INSERT INTO EXAMPLE (id, date, number)
VALUES (1, DEFAULT, DEFAULT) (2, DEFAULT, 2) (3, '2022-01-01 00:00:00.000', 3);

Is it possible to specify this using PreparedStatement?

...
preparedStatement.setObject(1, 1);
preparedStatement.setObject(2, DEFAULT);
preparedStatement.setObject(3, DEFAULT);
preparedStatement.addBatch();
...
preparedStatement.executeBatch();
...

Expected results

id date number
1 1970-01-01 00:00:00.000 -1
2 1970-01-01 00:00:00.000 2
3 2022-01-01 00:00:00.000 3

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
zhicwucommented, Apr 17, 2022

Turns out it’s not that simple 😦 First there’s no generic way to set null in RowBinary format. On the other hand, switching to TabSeparated works in some cases(see tests at here) but not all.

I think the best way as of now is to set default value by yourself, and you have to update application each time default value in DDL is changed… I’ll leave this issue open, and try again later by enhancing the parser to extract default value(constant only) from column/table definition.

1reaction
zhicwucommented, Mar 24, 2022

Sorry I didn’t make it clear. You don’t have to deal with too many details of the data format , you just need to change connection property format to TabSeparatedWithNamesAndTypes. This way, the driver will use TabSeparatedWithNamesAndTypes for select queries and TabSeparated(without headers) for insert queries. It does not work in latest release(0.3.2-patch7), but I’ll fix that in these two days and then release another patch in weekend.

As to the workaround you mentioned above, it’s better to offload to the driver, so that nobody has to suffer. I’m thinking of a new connection property like nullAsDefault to control this - when it’s set to true, the driver will convert null value to default value based on target column data type(not column definition). Let me give it a shot tonight when I get home to see if it’s feasible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PreparedStatement - how specify to use default value of column
so the 'date_current' column defaults to the current date and time. I have a prepared statement like this: PreparedStatement psInsert_ = conn.
Read more >
Changing column default value using PreparedStatement ...
I just noticed a strange behavior when trying to changing the default value of an existing column. If I execute this SQL: ALTER...
Read more >
Is there a way to setXXX a column to DEFAULT ... - CodeRanch
I'm looking to use the following query with a PreparedStatement: "INSERT INTO (col1, col2) (?, ?)" However, there is no PreparedStatement.
Read more >
prepard statement how to set default value to specific place ...
hi all i have a prepared statement in that, to second place holder (or dollar ) i want to assign a default value....
Read more >
Using DB2PreparedStatement methods or constants to ... - IBM
To assign the default value of the target column to the input parameter, call PreparedStatement.setObject with DB2PreparedStatement.DB_PARAMETER_DEFAULT as the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found