How can I insert Double.NaN into a PostgreSQL 'double precision' field with JOOQ?
I'm using PostgreSQL 9.5 with JOOQ 3.7.3. I'm inserting sensor values in batch into a table. Sometimes the value is a NaN, and when JOOQ tries to insert it, it throws an exception.
org.springframework.jdbc.BadSqlGrammarException: jOOQ; bad SQL grammar [];
nested exception is java.sql.BatchUpdateException: Batch entry 6
insert into "sensordata" ("measure_time", "location_id", "sensor_id", "value")
values (1462551000, 12, 15, NaN)
on conflict ("measure_time", "location_id", "sensor_id")
do update set "value" = NaN
was aborted.
Call getNextException to see the cause.
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
The value field is defined in SQL as such: "value" DOUBLE PRECISION NOT NULL DEFAULT 0.0
The relevant part of my Java code:
final List<InsertOnDuplicateSetMoreStep<SensordataRecord>> inserts =
seq(list)
.map(this::process)
.flatMap(t -> t)
.map(d ->
sql.insertInto(SENSORDATA, SENSORDATA.MEASURE_TIME, SENSORDATA.LOCATION_ID, SENSORDATA.SENSOR_ID, SENSORDATA.VALUE)
.values(d.getMeasureTime(), d.getLocationId(), d.getSensorId(), d.getValue())
.onDuplicateKeyUpdate()
.set(SENSORDATA.VALUE, d.getValue())
)
.toList();
sql.transaction(cfg -> DSL.using(cfg).batch(inserts).execute());
inserts.forEach(query -> DSL.using(cfg).execute(query));the transaction runs just fine. Maybe I should change the question to: "Why does batch insert fail when using the onDuplicateKeyUpdate clause?", but I'm not sure if that is the real problem with my query.