is it possible to specify DEFAULT value for PreparedStatements
See original GitHub issueTable 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:
- Created 2 years ago
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Turns out it’s not that simple 😦 First there’s no generic way to set
null
in RowBinary format. On the other hand, switching toTabSeparated
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.
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 useTabSeparatedWithNamesAndTypes
for select queries andTabSeparated
(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.