docs/src/main/sphinx/connector/duckdb.md
The DuckDB connector allows querying and creating tables in an external DuckDB instance. This can be used to join data between different systems like DuckDB and Hive, or between two different DuckDB instances.
libstdc++ as required by the DuckDB JDBC
driver.The connector can query a DuckDB database. Create a catalog properties file that
specifies the DuckDb connector by setting the connector.name to duckdb.
For example, to access a database as the example catalog, create the file
etc/catalog/example.properties. Replace the connection properties as
appropriate for your setup:
connector.name=duckdb
connection-url=jdbc:duckdb:<path>
The connection-url defines the connection information and parameters to pass
to the DuckDB JDBC driver. The parameters for the URL are available in the
DuckDB JDBC driver documentation.
The <path> must point to an existing, persistent DuckDB database file. For
example, use jdbc:duckdb:/opt/duckdb/trino.duckdb for a database created with
the command duckdb /opt/duckdb/trino.duckdb. The database automatically
contains the main schema and the information_schema schema. Use the main
schema for your new tables or create a new schema.
When using the connector on a Trino cluster the path must be consistent on all nodes and point to a shared storage to ensure that all nodes operate on the same database.
Using an in-memory DuckDB database jdbc:duckdb: is not supported.
Refer to the DuckDB documentation for tips on securing DuckDB. Note that Trino connects to the database using the JDBC driver and does not use the DuckDB CLI.
The DuckDB connector can only access a single database within a DuckDB instance. Thus, if you have multiple DuckDB servers, or want to connect to multiple DuckDB servers, you must configure multiple instances of the DuckDB connector.
(duckdb-type-mapping)=
Because Trino and DuckDB each support types that the other does not, this
connector {ref}modifies some types <type-mapping-overview> when reading or
writing data. Data types may not map the same way in both directions between
Trino and the data source. Refer to the following sections for type mapping in
each direction.
List of DuckDB data types.
The connector maps DuckDB types to the corresponding Trino types following this table:
:::{list-table} DuckDB type to Trino type mapping :widths: 30, 30, 40 :header-rows: 1
BOOLEANBOOLEANTINYINTTINYINTSMALLINTSMALLINTINTEGERINTEGERBIGINTBIGINTFLOATREALDOUBLEDOUBLEDECIMALDECIMALVARCHARVARCHARDATEDATE:::
No other types are supported.
The connector maps Trino types to the corresponding DuckDB types following this table:
:::{list-table} Trino type to DuckDB type mapping :widths: 30, 30, 40 :header-rows: 1
BOOLEANBOOLEANTINYINTTINYINTSMALLINTSMALLINTINTEGERINTEGERBIGINTBIGINTREALREALDOUBLEDOUBLEDECIMALDECIMALCHARVARCHARVARCHARVARCHARDATEDATE:::
No other types are supported.
(duckdb-sql-support)=
The connector provides read access and write access to data and metadata in
a DuckDB database. In addition to the {ref}globally available <sql-globally-available> and {ref}read operation <sql-read-operations>
statements, the connector supports the following features:
/sql/insert/sql/delete/sql/truncate/sql/create-table/sql/create-table-as/sql/drop-table/sql/alter-table/sql/create-schema/sql/drop-schemaThe connector provides specific table functions to access DuckDB.
(duckdb-query-function)=
query(varchar) -> tableThe query function allows you to query the underlying database directly. It
requires syntax native to DuckDB, because the full query is pushed down and
processed in DuckDB. This can be useful for accessing native features which
are not available in Trino or for improving query performance in situations
where running a query natively may be faster.
Find details about the SQL support of DuckDB that you can use in the query in the DuckDB SQL Command Reference and other statements and functions.
As a simple example, query the example catalog and select an entire table:
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
*
FROM
tpch.nation'
)
);