Skip to content

Call the C API v2 with the Foreign Function and Memory API on Java 25+ - #1029

Merged
staticlibs merged 11 commits into
duckdb:mainfrom
staticlibs:java-ffm
Dec 6, 2026
Merged

staticlibs merged 11 commits into
duckdb:mainfrom
staticlibs:java-ffm

Conversation

@staticlibs

Copy link
Copy Markdown
Member

This is a continuation of the PR #1018

Robinho-MR and others added 11 commits November 29, 2026 01:55
The build uses two JDKs now. JAVA_JNI (JDK 8+) compiles the driver, the
JNI bindings and the tests, it is the JDK of JAVA_JNI_HOME, otherwise
the JDK that CMake detects. JAVA_FFM (JDK 22+) compiles the FFM
bindings, it is the JDK of JAVA_FFM_HOME, otherwise JAVA_JNI. The
configuration fails when JAVA_FFM is older than JDK 22, so the jars
without the FFM bindings are not built anymore. The JAVA22 names of the
CMake files and of gen_ffm_native.py are renamed to JAVA_FFM, the names
of the JDK 8+ part are renamed to JAVA_JNI.

The FFM classes are compiled for the release of JAVA_FFM and are packed
as META-INF/versions/<release>. Before this change javac used the
release of the build JDK and jar always used 22, so the build failed
with a JDK newer than 22. The FFM job of the CI checks the classes of
JDK 25.
duckdb_jdbc_nolib_mr.jar is removed, duckdb_jdbc_nolib.jar and
duckdb_jdbc.jar both have the FFM bindings now, as the classes of
META-INF/versions/<version of JAVA_FFM>. The jar that has the JNI
bindings only is an intermediate file of the build
(java_jni/duckdb_jdbc_jni.jar), the tests and the FFM bindings are
compiled against it. On a JDK that uses the FFM bindings the JNI ones
can be selected with -Djdk.util.jar.enableMultiRelease=false.

JAVA_FFM must be JDK 25 or later and must not be older than JAVA_JNI.
javac is called without --release, so the class files have the version
of the JDK that compiles them: a driver that is built with JDK 27 runs
on JDK 27 and later only. Release builds use JDK 8 and JDK 25, CMake
warns when JAVA_JNI is newer than 8 or JAVA_FFM is newer than 25.
JAVA_FFM_HOME is kept in the CMake cache, so it is not required when a
build directory is configured again.
The export lists were in conflict: the lists of main are taken and all
the functions of the C API v2 are exported again with
update_exports.py.

The FFM bindings follow the changes of the JNI bindings of main. The
new entry points are implemented: the value cast, the qualified names,
the table and the column descriptions, the object cache, the encoding
and the decoding of BIGNUM and the result sources of the Arrow export.
The DECIMAL values that are wider than 38 digits and the BIGNUM values
are read from their BIGNUM storage. The scan callbacks of the table
functions are set on a data source, the C API has no such callbacks on
a table function anymore. The Arrow export scans the chunks of the
result with the __jdbc_arrow_result_scan function and does not copy
them. DuckDBBindings and Capi of src/main/java22 are generated again,
gen_ffm_native.py finds the method references of the Capi functions
too.
The setup-jdk-corretto composite action downloads Amazon Corretto from
its permanent URL with curl (HTTPS only, the certificates are verified),
checks the SHA-256 checksum of the download against the checksum file
of Corretto, sets the environment variable that is specified in its
arguments (JAVA_HOME by default) to the home directory of the JDK and
prints the version of the JDK. It does not change PATH.

The jobs of Java.yml use this action instead of setup-java and of the
JDK packages of the Docker images. All the builds use JDK 8 as
JAVA_JNI_HOME and JDK 25 as JAVA_FFM_HOME, in the Docker builds the
JDKs are mounted into the container (the JDKs for Alpine Linux in the
musl builds). The tests run on JDK 8, the tests on the Linux hosts run
on JDK 27 (JAVA_LATEST_HOME), the FFM tests on JDK 25, Spark on JDK 17
and Jepsen on JDK 21. The steps that run Java set PATH to the JDK that
they use and print 'which java' and 'java -version'.
@staticlibs
staticlibs merged commit dc4c062 into duckdb:main Dec 6, 2026
33 of 34 checks passed
@staticlibs
staticlibs deleted the java-ffm branch December 6, 2026 09:46
@staticlibs

Copy link
Copy Markdown
Member Author

Just in case someone would like to try the FFM mode, snapshot binaries with this change included:

<dependency>
    <groupId>org.duckdb</groupId>
    <artifactId>duckdb_jdbc</artifactId>
    <version>2.1.0-dev-dc4c062</version>
</dependency>
<repository>
    <id>duckdb</id>
    <url>https://duckdb-staging.duckdb.org/duckdb/duckdb-java/maven/</url>
</repository>

FFM support classes are packages in the same JAR files (duckdb_jdbc-<version>.jar, duckdb_jdbc-<version>-nolib.jar) as before as a multi-release addition. These FFM classes are loaded automatically when the JAR is run on JDK 25 or newer. In this case JNI layer is not used at all (FFM classes are shadowing the JNI-loading ones). The JAR-specific native library is still required for them, because C API v2 is not yet stable in DuckDB. At some point in future, when C API v2 is stabilized - then JDK 25 will be able to run duckdb_jdbc-<version>-nolib.jar on top of non-JAR-specific libduckdb.so, but currently JAR-specific library is required.

When the JAR is run on JDK 8 to 24 - then FFM classes are not loaded and the JNI mode is used with the same logic as before. It is also possible to use JNI mode on JDK 25+ by passing -Djdk.util.jar.enableMultiRelease=false option to the JVM at startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants