Querying DuckDB
DuckDB is an embedded analytical database. Querydsl supports it
through the regular querydsl-sql module, with dedicated templates and a query factory
in com.querydsl.sql.duckdb.
Maven Integration
<dependency>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>7.1</version>
</dependency>
<dependency>
<groupId>org.duckdb</groupId>
<artifactId>duckdb_jdbc</artifactId>
<version>2.0.0.1</version>
</dependency>
Code Generation
Query types are generated from the database schema with the export goal of the
Maven plugin, as described in Querying SQL. Point it
at a DuckDB file and use the main schema:
<plugin>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>7.1</version>
<executions>
<execution>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbcDriver>org.duckdb.DuckDBDriver</jdbcDriver>
<jdbcUrl>jdbc:duckdb:${project.basedir}/src/main/resources/schema.duckdb</jdbcUrl>
<schemaPattern>main</schemaPattern>
<packageName>com.myproject.domain</packageName>
<targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
<dependency>
<groupId>org.duckdb</groupId>
<artifactId>duckdb_jdbc</artifactId>
<version>2.0.0.1</version>
</dependency>
</dependencies>
</plugin>
The driver reports HUGEINT, UBIGINT, UHUGEINT, UUID, INTERVAL, JSON, lists,
structs and maps as java.sql.Types.OTHER, so these columns are generated as Object
paths. Map such columns explicitly with typeMappings (one entry per table and column)
inside the plugin <configuration>:
<typeMappings>
<!-- ID hugeint -->
<typeMapping>
<table>ACCOUNT</table>
<column>ID</column>
<type>java.math.BigInteger</type>
</typeMapping>
<!-- BALANCE ubigint -->
<typeMapping>
<table>ACCOUNT</table>
<column>BALANCE</column>
<type>java.math.BigInteger</type>
</typeMapping>
<!-- EXTERNAL_ID uuid -->
<typeMapping>
<table>ACCOUNT</table>
<column>EXTERNAL_ID</column>
<type>java.util.UUID</type>
</typeMapping>
</typeMappings>
DuckDBTemplates binds BigInteger values with full HUGEINT/UHUGEINT precision, so
these paths round-trip values beyond the 38 digits of DECIMAL.
Constraint names are generated by DuckDB, so primary and foreign key members are named
after them (for example employeePk) rather than after the names used in CREATE TABLE.
Querying
The registry picks DuckDBTemplates from the connection metadata. Use
DuckDBQueryFactory to get the DuckDB specific clauses:
Supplier<Connection> connections =
() -> {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new QueryException(e);
}
};
DuckDBQueryFactory queryFactory =
new DuckDBQueryFactory(new Configuration(new DuckDBTemplates()), connections);
List<String> topEarners =
queryFactory
.select(employee.firstname)
.from(employee)
.qualify(
SQLExpressions.rowNumber()
.over()
.partitionBy(employee.departmentId)
.orderBy(employee.salary.desc())
.eq(1L))
.fetch();
Insert, Update, Delete and Upsert
Inserts (including batches), updates, deletes, merge and MERGE ... USING work as with
any other database. Generated keys are returned for columns filled by a sequence default,
such as id integer default nextval('employee_seq').
The emulated merge runs a separate update and insert, so it is not atomic; prefer
insertOnConflictDoUpdate for upserts.
For upserts, the factory offers:
| Method | SQL |
|---|---|
insertOrIgnore(entity) |
insert or ignore into ... |
insertOrReplace(entity) |
insert or replace into ... |
insertOnConflictDoNothing(entity, keyColumns...) |
insert into ... on conflict (...) do nothing |
insertOnConflictDoUpdate(entity, List.of(keyColumns...), columns...) |
insert into ... on conflict (...) do update set col = excluded.col |
queryFactory
.insertOnConflictDoUpdate(employee, List.of(employee.id), employee.salary)
.set(employee.id, 1)
.set(employee.salary, 120000)
.execute();
Limitations
- DuckDB has no row locks, so
FOR UPDATEandFOR SHAREare not supported. - Only one process can write to a DuckDB file at a time. Inside one process, share a single database instance and open extra connections from it instead of opening the file twice.
- An explicit
NULLwritten to a column does not fall back to its sequence default. MathExpressions.random(seed)is not supported; DuckDB seeds throughsetseed().- Correlated
INsubqueries over several columns, such as(a, b) in (select ...), are not supported by DuckDB. - The
/operator always returns aDOUBLE, also for integer operands.