Skip to content

Move the driver logic to Java on top of C API v2 entry points - #1023

Merged
staticlibs merged 22 commits into
duckdb:mainfrom
staticlibs:java-v2-port
Dec 2, 2026
Merged

staticlibs merged 22 commits into
duckdb:mainfrom
staticlibs:java-v2-port

Conversation

@staticlibs

Copy link
Copy Markdown
Member

This PR is a continuation of #1017.

Robinho-MR and others added 21 commits November 27, 2026 13:41
Conflicts:

- src/duckdb: the vendored sources are taken from main, they include
  duckdb/duckdb#29425 that was applied to them on this branch.
- src/jni/config.cpp, types.cpp, duckdb_java.cpp: the native code that is
  changed on main is implemented in Java on this branch. The wide DECIMAL
  changes of main are ported to DuckDBCore: the DECIMALBIGNUM type detail,
  reading the BIGNUM storage of the result vectors, and binding BigDecimal
  parameters that are wider than 38 digits with the max_decimal_width
  check. The values that do not fit in 128 bits are cast from their text
  through the new duckdb_v2_value_cast entry point.
- src/jni/arrow_export.cpp: the scan callbacks of the Arrow stream table
  function are moved to a data source, following the C API v2 change.
- TestDuckDBJDBC: test_query_progress is taken from main.
The appender was kept for compatibility and is no longer needed, the
rows are appended with DuckDBAppender. The class is removed with its
tests, DuckDBConnection#createSingleValueAppender and its error code.
The wide DECIMAL test that appended through it uses DuckDBAppender now.

DuckDBCore#toValue is only used for the bound parameters now, so the
values are always checked against max_decimal_width.
Every function that implements a JNI entry point of the C API v2
bindings and of the Arrow export gets the comment with its class, method
and signature, the same as the javac-generated header has and as the
other entry points have.
… options

'allowed_directories' and 'allowed_paths' can only be set on a running
instance, and DuckDB refuses to change them when the external access is
disabled. The usual sandboxing setup, enable_external_access=false with
allowed_directories, failed to connect: enable_external_access was a
startup option and the allowed directories were set after it.

When these options are given together, enable_external_access=false is
applied after them when the instance is created. The same is done for
lock_configuration, that refused all the options that are set after the
startup (the allowed paths, the logging options, jdbc_stream_results).

The options that are applied this way are not startup options, so they
are not compared when a cached instance is shared. A connection that asks
for these restrictions is refused, as before, when the existing instance
does not have them.
The appender took its columns from the binding of SELECT * FROM table,
that includes the generated columns. The INSERT with all the columns is
rejected by DuckDB for a table with a generated column, and the appender
asked for the values of the generated columns.

The columns are read from the description of the table now
(duckdb_v2_connection_describe_table), the generated columns are skipped
like the appender of the engine did. The INSERT uses the resolved name
of the table, that does not depend on the search path.

The entry points for the qualified names and the table descriptions are
added to the bindings.
The default values were read from duckdb_columns() with an exact match
of the table name as it was given to the appender, and without a schema
or a catalog when they were not given. A table that was created as "Foo"
and appended to as "foo" got NULL instead of its defaults, and the
defaults of the tables with the same name in the other schemas and
catalogs were mixed up.

The lookup uses the catalog, the schema and the name that the table
description resolved the name to.
…RT once

Every appendDefault call ran a SELECT of the default expression on the
connection, and every flush planned the INSERT from scratch. Appending
1M rows with two defaulted columns took 2M queries (about two minutes),
it takes less than a second now.

The appender only marks the rows that have the default value. A column
that has it in all the rows of a chunk is left out of the INSERT, so
that the engine fills it, also with the volatile defaults like nextval.
When a chunk has both the default and the other values in a column, the
defaults are evaluated by one query for the chunk. The INSERT statements
are prepared once and are reused by the next flushes.
The value of a VARIANT is read through a temporary vector of one row,
that is destroyed when the value is read. Only the data of the top-level
vector was copied, the vectors of the elements of a LIST, an ARRAY or a
STRUCT value kept pointing to the released memory: reading them later,
e.g. with Array#getResultSet, was a use-after-free.

The data of all the vectors that are created for the value is copied
now.
getSchema and getCatalog ran SELECT current_schema() and SELECT
current_database() on the connection of the user. With
jdbc_stream_results=true that failed while a result was streamed, and in
an aborted transaction it failed with 'Current transaction is aborted'.

The schema and the catalog are read from the 'schema' and 'search_path'
settings of the session now, that does not run anything on the
connection. When the search path has no catalog, the default database of
the instance is used, it is read once with a connection of its own.

setSchema and setCatalog still run a statement: the catalog entries are
looked up in a transaction when the search path is set. They set the
same search path entries as before the port: the schema name is not
split at the dots, and the catalog is set with its default schema.
The dbAddress of a connection to a cached instance was the 32-bit hash
code of the path of the database, so two databases could get the same
address. The JFR memory monitor, that is keyed by the address, then
reported the memory of one of them for both, and was stopped when the
connections to one of them were closed.

The address of a cached instance is an id that is stored in the object
cache of the instance by its first handle, the other handles to the
instance read it from there. The ids are unique in the process.
… them

The Arrow export copied all the rows of the result into a column data
collection and ran a query over the collection to get the Arrow batches.
A result that is streamed was read into the memory before the first
batch was returned, the rows were copied twice, and the batches were
filled by the threads of that query, so their sizes were not exact.

The rows that are not read yet are scanned by a table function now: the
chunks that are fetched already are passed to the scan as they are, and
a result that is streamed is read when the Arrow stream is read. The
scan is done by one thread in the order of the result, so the batches
have the requested size again: the assertions of TestArrow that were
relaxed are restored.
A list value of a connection property, e.g. of 'allowed_directories', is
passed to DuckDB as text that is cast to the type of the option. The
backslash is the escape character of the quoted entries of that text,
and the entries were only quoted: the backslashes of a Windows path were
dropped ('C:\Users\dir' was read as 'C:Usersdir'), so the allowed
directories did not match any file, and a quote in an entry was dropped
too. On Windows the tests of the allowed directories failed with
'file system operations are disabled by configuration'.

The backslashes and the quotes of the entries are escaped with a
backslash.
@staticlibs
staticlibs merged commit a9d5782 into duckdb:main Dec 2, 2026
17 checks passed
@staticlibs
staticlibs deleted the java-v2-port branch December 2, 2026 15:40
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