Duckdb Docs
Duckdb Docs
Contents i
Summary 1
Connect 5
Connect 7
Connect or Create a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Persistent Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
In‑Memory Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Concurrency 9
Handling Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Concurrency within a Single Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Writing to DuckDB from Multiple Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Optimistic Concurrency Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Data Import 11
Importing Data 13
INSERT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
CSV Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Parquet Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
JSON Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Appender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Data Sources 15
CSV Files 17
CSV Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
CSV Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
auto_type_candidates Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CSV Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Writing Using the COPY Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Reading Faulty CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Order Preservation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
CSV Auto Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
sniff_csv Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Detection Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Dialect Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
i
DuckDB Documentation
Type Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Header Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Dates and Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Reading Faulty CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Structural Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Anatomy of a CSV Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Using the ignore_errors Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Retrieving Faulty CSV Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Reject Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Reject Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
CSV Import Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Override the Header Flag if the Header Is Not Correctly Detected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Provide Names if the File Does Not Contain a Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Override the Types of Specific Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Use COPY When Loading Data into a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Use union_by_name When Loading Files with Different Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
JSON Files 33
JSON Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
About JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Loading JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Writing JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
JSON Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Retrieving JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Creating JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
JSON Creation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Loading JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The read_json Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Functions for Reading JSON Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Functions for Reading JSON as a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Loading with the COPY Statement Using FORMAT JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Writing JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
JSON Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
JSON Processing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
JSON Extraction Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
JSON Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
JSON Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Transforming JSON to Nested Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
JSON Format Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Records Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Installing and Loading the JSON extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
SQL to/from JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Equality Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Multiple Files 53
Reading Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
ii
DuckDB Documentation
CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Parquet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Multi‑File Reads and Globs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
List Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Glob Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
List of Globs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Filename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Glob Function to Find Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Combining Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Combining Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Union by Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Union by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Parquet Files 59
Reading and Writing Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
read_parquet Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Partial Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Inserts and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Writing to Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Installing and Loading the Parquet Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Querying Parquet Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Parquet Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Parquet Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Parquet File Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Parquet Key‑Value Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Parquet Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Reading and Writing Encrypted Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Writing Encrypted Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Reading Encrypted Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Performance Implications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Parquet Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Tips for Reading Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Use union_by_name When Loading Files with Different Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Tips for Writing Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Enabling PER_THREAD_OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Selecting a ROW_GROUP_SIZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The ROW_GROUPS_PER_FILE Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Partitioning 69
Hive Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Hive Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Filter Pushdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Autodetection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Hive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Writing Partitioned Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Partitioned Writes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
iii
DuckDB Documentation
Partitioned Writes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Overwriting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Filename Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Appender 73
Date, Time and Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Commit Frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Handling Constraint Violations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Appender Support in Other Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
INSERT Statements 75
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Client APIs 77
C 81
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Startup & Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Value Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
duckdb_fetch_chunk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
duckdb_value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Data Chunks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Vector Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Decimals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Example: Reading an int64 Vector with NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Example: Reading a String Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Example: Reading a Struct Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Example: Reading a List Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Validity Mask Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
iv
DuckDB Documentation
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
duckdb_value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
duckdb_fetch_chunk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Date Time Timestamp Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Hugeint Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Decimal Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Logical Type Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Appender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Table Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Table Function Bind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Table Function Init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Table Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Replacement Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Complete API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
API Reference Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Open Connect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Query Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Result Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Safe Fetch Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Date Time Timestamp Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Hugeint Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Unsigned Hugeint Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Decimal Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Bind Values To Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Execute Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Extract Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Pending Result Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Value Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Logical Type Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Data Chunk Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Vector Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Validity Mask Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Table Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Table Function Bind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Table Function Init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Table Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Replacement Scans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Profiling Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Appender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
v
DuckDB Documentation
CLI 305
CLI API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
In‑Memory vs. Persistent Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Running SQL Statements in the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Editor Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Exiting the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Dot Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Configuring the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Setting a Custom Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Non‑Interactive Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Loading Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Reading from stdin and Writing to stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Reading Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Restrictions for Reading Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Dot Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Dot Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Using the .help Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
.output: Writing Results to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Querying the Database Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Configuring the Syntax Highlighter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Importing Data from CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Moving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Changing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Completing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Using Read‑Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Autocomplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Error Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
vi
DuckDB Documentation
Go 325
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Appender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
More Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
vii
DuckDB Documentation
Node.js 347
Node.js API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Initializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Running a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Inserting Data via Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Loading Unsigned Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Node.js API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Typedefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
duckdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
duckdb~Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
duckdb~Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
duckdb~QueryResult . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
duckdb~Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
duckdb~TokenType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
duckdb~ERROR : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
duckdb~OPEN_READONLY : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
duckdb~OPEN_READWRITE : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
duckdb~OPEN_CREATE : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
duckdb~OPEN_FULLMUTEX : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
duckdb~OPEN_SHAREDCACHE : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
duckdb~OPEN_PRIVATECACHE : number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
ColumnInfo : object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
TypeInfo : object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
DuckDbError : object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
HTTPError : object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Python 367
Python API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Basic API Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Data Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
DataFrames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Result Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Writing Data to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Connection Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Using an In‑Memory Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Persistent Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Connection Object and Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Using Connections in Parallel Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Loading and Installing Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Community Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Unsigned Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Data Ingestion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
JSON Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Directly Accessing DataFrames and Arrow Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Pandas DataFrames – object Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
viii
DuckDB Documentation
ix
DuckDB Documentation
R API 397
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
duckdb: R API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
duckplyr: dplyr API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Reference Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Basic API Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Startup & Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Efficient Transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
dbplyr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Memory Limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Warning When Installing on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
x
DuckDB Documentation
Wasm 407
DuckDB Wasm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Getting Started with DuckDB‑Wasm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
cdn(jsdelivr) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
webpack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
vite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Statically Served . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Data Ingestion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Data Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Open & Close Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Apache Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Parquet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
httpfs (Wasm‑flavored) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Insert Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Query Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Arrow Table to JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Export Parquet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
INSTALL and LOAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Autoloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
List of Officially Available Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
HTTPFS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Extension Signing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Fetching DuckDB‑Wasm Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Serving Extensions from a Third‑Party Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Tooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
ODBC 427
ODBC API Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
DuckDB ODBC Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
ODBC API on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Driver Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Setting Up the Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
ODBC API on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
DSN Windows Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Default DuckDB DSN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Changing DuckDB DSN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
xi
DuckDB Documentation
SQL 435
Statements 445
Statements Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
ANALYZE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
ALTER TABLE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
RENAME TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
RENAME COLUMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
ADD COLUMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
DROP COLUMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
ALTER TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
SET / DROP DEFAULT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
ADD PRIMARY KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
ADD / DROP CONSTRAINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
ALTER VIEW Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
ATTACH and DETACH Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Attach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Attach Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Detach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Detach Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Name Qualification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Default Database and Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Changing the Default Database and Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Changing the Catalog Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Transactional Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
CALL Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
xii
DuckDB Documentation
xiii
DuckDB Documentation
xiv
DuckDB Documentation
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
PIVOT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Simplified PIVOT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Example Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
PIVOT ON and USING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
PIVOT ON, USING, and GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
IN Filter for ON Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Multiple Expressions per Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Using PIVOT within a SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Multiple PIVOT Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Simplified PIVOT Full Syntax Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
SQL Standard PIVOT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
SQL Standard PIVOT Full Syntax Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Profiling Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
EXPLAIN ANALYZE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
SAMPLE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
GROUP BY and HAVING Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
WINDOW Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
QUALIFY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
ORDER BY, LIMIT and OFFSET Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
VALUES List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Row IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Common Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Full Syntax Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
SET and RESET Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
RESET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
SET VARIABLE and RESET VARIABLE Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
SET VARIABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
RESET VARIABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
SUMMARIZE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Starting a Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Committing a Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Rolling Back a Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
xv
DuckDB Documentation
xvi
DuckDB Documentation
xvii
DuckDB Documentation
xviii
DuckDB Documentation
xix
DuckDB Documentation
Expressions 605
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
CASE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Explicit Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Casting Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
TRY_CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Using Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Default Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
ICU Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Combination Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
BETWEEN and IS [NOT] NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
IN Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
IN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
NOT IN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Binary Operators: AND and OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
xx
DuckDB Documentation
Functions 621
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Function Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Function Chaining via the Dot Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Query Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
DISTINCT Clause in Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
ORDER BY Clause in Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Handling NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
General Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Approximate Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Statistical Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Ordered Set Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
Miscellaneous Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Array Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Array‑Native Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Bitstring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Bitstring Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Bitstring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
Bitstring Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Blob Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Date Format Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
strftime Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
strptime Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
CSV Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
Date Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
xxi
DuckDB Documentation
xxii
DuckDB Documentation
Constraints 775
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Check Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
xxiii
DuckDB Documentation
Indexes 777
Index Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Min‑Max Index (Zonemap) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Adaptive Radix Tree (ART) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Indexes Defined by Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
CREATE INDEX and DROP INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Limitations of ART Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Updates Become Deletes and Inserts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Over‑Eager Unique Constraint Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
xxiv
DuckDB Documentation
Samples 809
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
reservoir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
bernoulli . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Table Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
xxv
DuckDB Documentation
Configuration 813
Configuration 815
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Secrets Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Configuration Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Global Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
Local Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Pragmas 823
Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Resource Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Default Ordering for NULLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Ordering by Non‑Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Implicit Casting to VARCHAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Python: Scan All Dataframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Information on DuckDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
Progress Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
EXPLAIN Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
Enable Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
Profiling Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Profiling Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Custom Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Disable Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Query Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Full‑Text Search Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Object Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Checkpointing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Temp Directory for Spilling Data to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Returning Errors as JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
IEEE Floating‑Point Operation Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Query Verification (for Development) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Block Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
Extensions 837
Extensions 839
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
Listing Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
Built‑In Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
xxvi
DuckDB Documentation
xxvii
DuckDB Documentation
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
xxviii
DuckDB Documentation
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Creating a Local Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
Versioning and Revisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
CONFIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
CREDENTIAL_CHAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
S3 API Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Configuration and Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
CONFIG Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
CREDENTIAL_CHAIN Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884
Overview of S3 Secret Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Platform‑Specific Secret Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
Partial Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
Reading Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
Globbing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
Hive Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 887
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Legacy Authentication Scheme for S3 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Legacy Authentication Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Per‑Request Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889
xxix
DuckDB Documentation
Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Background Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
xxx
DuckDB Documentation
Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
Schema Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Spatial 915
Spatial Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
Installing and Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
The GEOMETRY Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
Spatial Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
Function Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
ST_Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
ST_Area_Spheroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
ST_AsGeoJSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
ST_AsHEXWKB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
ST_AsSVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
ST_AsText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
ST_AsWKB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
ST_Boundary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
ST_Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
ST_Centroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
ST_Collect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923
ST_CollectionExtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
ST_Contains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
ST_ContainsProperly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
ST_ConvexHull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
ST_CoveredBy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
ST_Covers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
ST_Crosses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
ST_DWithin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926
ST_DWithin_Spheroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
ST_Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
ST_Dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
ST_Disjoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
ST_Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
ST_Distance_Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
ST_Distance_Spheroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
ST_Dump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
ST_EndPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
ST_Envelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
ST_Equals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
ST_Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
ST_ExteriorRing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
ST_FlipCoordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
ST_Force2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
ST_Force3DM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
ST_Force3DZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
ST_Force4D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
ST_GeomFromGeoJSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
ST_GeomFromHEXEWKB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
ST_GeomFromHEXWKB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
ST_GeomFromText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
ST_GeomFromWKB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
ST_GeometryType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
ST_HasM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
xxxi
DuckDB Documentation
ST_HasZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
ST_Hilbert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
ST_Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
ST_Intersects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
ST_Intersects_Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
ST_IsClosed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
ST_IsEmpty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
ST_IsRing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
ST_IsSimple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
ST_IsValid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
ST_Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
ST_Length_Spheroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
ST_LineMerge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
ST_M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
ST_MMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
ST_MMin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
ST_MakeEnvelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
ST_MakeLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
ST_MakePolygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
ST_MakeValid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
ST_Multi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
ST_NGeometries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
ST_NInteriorRings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
ST_NPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
ST_Normalize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
ST_NumGeometries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
ST_NumInteriorRings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
ST_NumPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
ST_Overlaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
ST_Perimeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
ST_Perimeter_Spheroid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
ST_Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
ST_Point2D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
ST_Point3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
ST_Point4D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
ST_PointN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
ST_PointOnSurface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
ST_Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
ST_QuadKey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
ST_ReducePrecision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
ST_RemoveRepeatedPoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
ST_Reverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
ST_ShortestLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
ST_Simplify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
ST_SimplifyPreserveTopology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
ST_StartPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
ST_Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
ST_Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
ST_Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
ST_Within . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
ST_X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
ST_XMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
ST_XMin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
xxxii
DuckDB Documentation
ST_Y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
ST_YMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
ST_YMin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
ST_Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
ST_ZMFlag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
ST_ZMax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
ST_ZMin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
ST_Envelope_Agg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953
ST_Extent_Agg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
ST_Intersection_Agg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
ST_Union_Agg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
Table Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
ST_Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
ST_Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
ST_ReadOSM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
ST_Read_Meta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
R‑Tree Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
Why Should I Use an R‑Tree Index? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
How Do R‑Tree Indexes Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
What Are the Limitations of R‑Tree Indexes in DuckDB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
How To Use R‑Tree Indexes in DuckDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
Performance Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Bulk Loading & Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Memory Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
R‑Tree Table Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
GDAL Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
GDAL Based COPY Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
xxxiii
DuckDB Documentation
SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
BLOB Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
JSON Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
BLOB Consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
BLOB Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
JSON Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970
BLOB Consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
BLOB Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
JSON Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
BLOB Consumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
Guides 983
Guides 985
Data Import and Export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
HTTP(S), S3 and GCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
JSON Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
Excel Files with the Spatial Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985
Querying Other Database Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
Directly Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
Meta Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986
xxxiv
DuckDB Documentation
xxxv
DuckDB Documentation
xxxvi
DuckDB Documentation
ODBC 1023
ODBC 101: A Duck Themed Guide to ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
What is ODBC? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
General Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
Connecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
Error Handling and Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
Buffers and Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
Setting up an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
1. Include the SQL Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
2. Define the ODBC Handles and Connect to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026
3. Adding a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
4. Fetching Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
5. Go Wild . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027
6. Free the Handles and Disconnecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028
Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028
Sample .cpp file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028
Sample CMakelists.txt file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029
Performance 1031
Performance Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Hardware Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
Local Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Network‑Attached Disks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Memory Allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Data Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Recommended Import Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032
Methods to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Microbenchmark: Using Timestamps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Microbenchmark: Joining on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
Microbenchmark: The Effect of Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
Zonemaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1035
The Effect of Ordering on Zonemaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1035
xxxvii
DuckDB Documentation
Python 1045
Installing the Python Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
Installing via Pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
Installing from Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
Executing SQL in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
Jupyter Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
Library Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
Library Import and Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
Connecting to DuckDB Natively . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046
Connecting to DuckDB via SQLAlchemy Using duckdb_engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
Querying DuckDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
Querying Pandas Dataframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
Visualizing DuckDB Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
Install and Load DuckDB httpfs Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
Boxplot & Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
xxxviii
DuckDB Documentation
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
SQL on Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
Import from Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
Export to Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
Import from Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
Export to Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
SQL on Apache Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
Apache Arrow Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051
Apache Arrow Datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Apache Arrow Scanners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Apache Arrow RecordBatchReaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
Import from Apache Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
Export to Apache Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
Export to an Arrow Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
Export as a RecordBatchReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
Export from Relational API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
Relational API on Pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
Multiple Python Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
Reader and Writer Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
Create Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
Run Threads and Show Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
Integration with Ibis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
Create a Database File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
Interactive Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
Common Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1060
mutate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
order_by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
group_by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
Chaining It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
Learn More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
Integration with Polars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
Polars to DuckDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1067
DuckDB to Polars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
Using fsspec Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
xxxix
DuckDB Documentation
Snippets 1089
Create Synthetic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089
Sharing Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1090
Overview 1099
Limits 1101
xl
DuckDB Documentation
Development 1113
Testing 1117
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117
How is DuckDB Tested? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117
sqllogictest Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117
Query Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1118
Editors & Syntax Highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1118
Temporary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1118
Require & Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1118
Writing Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Development and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Client Connector Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
Functions for Generating Test Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
test_all_types Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119
test_vector_types Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120
Triggering Which Tests to Run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121
Result Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121
NULL Values and Empty Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
Error Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122
Row‑Wise vs. Value‑Wise Result Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
Hashes and Outputting Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123
Result Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
Query Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
Persistent Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
xli
DuckDB Documentation
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1125
Data Generation without Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126
Multiple Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126
Concurrent Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127
Catch C/C++ Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1127
Profiling 1129
EXPLAIN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129
EXPLAIN ANALYZE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129
Pragmas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129
Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130
Cumulative Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1130
Detailed Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
Optimizer Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
Planner Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
Physical Planner Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
Custom Metrics Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131
Query Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133
Notation in Query Plans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133
xlii
DuckDB Documentation
Internals 1149
xliii
DuckDB Documentation
Binder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Logical Planner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Column Binding Resolver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Physical Plan Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152
xliv
DuckDB Documentation
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
Ungrouped Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
Grouped Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177
Grouped Aggregate with a Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178
Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179
Takeaway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1181
Appendix A: There and Back Again: Transferring Data from Pandas to a SQL Engine and Back . . . . . . . . . . . . . . . . . . . . 1181
Appendix B: Comparison to PandaSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182
Appendix C: Query on Parquet Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182
Ungrouped Aggregate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182
Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1183
xlv
DuckDB Documentation
DuckDB – Lord of the Enums: The Fellowship of the Categorical and Factors 1231
SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1232
Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1233
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234
Benchmark Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234
Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234
Grouped Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234
Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1235
Enum – Enum Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1235
Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236
What about the Sequels? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236
Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237
DuckDB Quacks Arrow: A Zero‑copy Data Integration between Apache Arrow and DuckDB 1239
Quick Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1239
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1239
Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1240
DuckDB and Arrow: The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1240
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1240
Streaming Data from/to Arrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1242
Benchmark Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
Projection Pushdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1243
Filter Pushdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244
Streaming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244
Conclusion and Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246
xlvi
DuckDB Documentation
xlvii
DuckDB Documentation
xlviii
DuckDB Documentation
xlix
DuckDB Documentation
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1361
Questions about Certain Results? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1362
Maintenance Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1362
From Waddle to Flying: Quickly Expanding DuckDB's Functionality with Scalar Python UDFs 1385
Python UDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1386
Quick‑Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1386
Generating Fake Data with Faker (Built‑In Type UDF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387
Swap String Case (PyArrow Type UDF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387
Predicting Taxi Fare Costs (Ibis + PyArrow UDF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1388
Benchmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1389
Built‑In Python vs. PyArrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1389
Python UDFs vs. External Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1390
Conclusions and Further Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1391
DuckDB ADBC – Zero‑Copy Data Transfer via Arrow Database Connectivity 1393
Quick Tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1394
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1395
Insert Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1395
Read Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1395
l
DuckDB Documentation
li
DuckDB Documentation
lii
DuckDB Documentation
liii
DuckDB Documentation
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1504
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1505
liv
DuckDB Documentation
Group By . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1543
Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1543
Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1543
Export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1543
Scan Other Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1544
Scale tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1544
Hardware Capabilities over Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1544
Analyzing the Results Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1545
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1546
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1546
Benchmark Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1546
Window Functions Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1548
lv
DuckDB Documentation
DuckDB in Python in the Browser with Pyodide, PyScript, and JupyterLite 1597
Time to “Hello World” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1597
Difficulties of Server‑Side Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1597
Enter Pyodide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1597
Use Cases for Pyodide DuckDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1598
Pyodide Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1598
PyScript Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1598
JupyterLite Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1599
lvi
DuckDB Documentation
Driving CSV Performance: Benchmarking DuckDB with the NYC Taxi Dataset 1619
The Taxi Data Set as CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1619
Reproducing the Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1619
Preparing the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1620
Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1620
Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1620
Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1620
Loading Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1620
Under the Hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1621
Query Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1622
How This Dataset Was Generated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1622
How Does This Dataset Differ from the Original One? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1623
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1623
lvii
DuckDB Documentation
CSV Files: Dethroning Parquet as the Ultimate Storage File Format — or Not? 1663
File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1663
CSV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1663
Parquet Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1663
lviii
DuckDB Documentation
Vertical Stacking as the Relational Model Intended: UNION ALL BY NAME 1691
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1691
Making Vertical Stacking Groovy Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1691
UNION vs. UNION ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1692
Reading Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1692
Data Lakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1693
Inserting Data by Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1693
The Inspirations for UNION ALL BY NAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1694
Improved Performance in DuckDB 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1694
Micro‑Benchmark . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1694
Closing Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1696
lix
DuckDB Documentation
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1697
Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1698
Aggregated Runtimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699
Individual Query Runtimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699
Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699
Acknowledgments 1705
lx
DuckDB Documentation
Summary
1
DuckDB Documentation
This document contains DuckDB's official documentation and guides in a single‑file easy‑to‑search form. If you find any issues, please
report them as a GitHub issue. Contributions are very welcome in the form of pull requests. If you are considering submitting a contribution
to the documentation, please consult our contributor guide.
Code repositories:
3
DuckDB Documentation
4
DuckDB Documentation
Connect
5
Connect
To use DuckDB, you must first create a connection to a database. The exact syntax varies between the client APIs but it typically involves
passing an argument to configure persistence.
Persistence
DuckDB can operate in both persistent mode, where the data is saved to disk, and in in‑memory mode, where the entire data set is stored
in the main memory.
Tip. Both persistent and in‑memory databases use spilling to disk to facilitate larger‑than‑memory workloads (i.e., out‑of‑core‑
processing).
Persistent Database
To create or open a persistent database, set the path of the database file, e.g., my_database.duckdb, when creating the connection.
This path can point to an existing database or to a file that does not yet exist and DuckDB will open or create a database at that location as
needed. The file may have an arbitrary extension, but .db or .duckdb are two common choices with .ddb also used sometimes.
Starting with v0.10, DuckDB's storage format is backwards‑compatible, i.e., DuckDB is able to read database files produced by an older
versions of DuckDB. For example, DuckDB v0.10 can read and operate on files created by the previous DuckDB version, v0.9. For more
details on DuckDB's storage format, see the storage page.
In‑Memory Database
DuckDB can operate in in‑memory mode. In most clients, this can be activated by passing the special value :memory: as the database
file or omitting the database file argument. In in‑memory mode, no data is persisted to disk, therefore, all data is lost when the process
finishes.
7
DuckDB Documentation
8
Concurrency
Handling Concurrency
When using option 1, DuckDB supports multiple writer threads using a combination of MVCC (Multi‑Version Concurrency Control) and
optimistic concurrency control (see Concurrency within a Single Process), but all within that single writer process. The reason for this con‑
currency model is to allow for the caching of data in RAM for faster analytical queries, rather than going back and forth to disk during each
query. It also allows the caching of functions pointers, the database catalog, and other items so that subsequent queries on the same
connection are faster.
DuckDB is optimized for bulk operations, so executing many small transactions is not a primary design goal.
DuckDB supports concurrency within a single process according to the following rules. As long as there are no write conflicts, multiple
concurrent writes will succeed. Appends will never conflict, even on the same table. Multiple threads can also simultaneously update
separate tables or separate subsets of the same table. Optimistic concurrency control comes into play when two threads attempt to edit
(update or delete) the same row at the same time. In that situation, the second thread to attempt the edit will fail with a conflict error.
Writing to DuckDB from multiple processes is not supported automatically and is not a primary design goal (see Handling Concurrency).
If multiple processes must write to the same file, several design patterns are possible, but would need to be implemented in application
logic. For example, each process could acquire a cross‑process mutex lock, then open the database in read/write mode and close it when the
query is complete. Instead of using a mutex lock, each process could instead retry the connection if another process is already connected to
the database (being sure to close the connection upon query completion). Another alternative would be to do multi‑process transactions
on a MySQL, PostgreSQL, or SQLite database, and use DuckDB's MySQL, PostgreSQL, or SQLite extensions to execute analytical queries on
that data periodically.
Additional options include writing data to Parquet files and using DuckDB's ability to read multiple Parquet files, taking a similar approach
with CSV files, or creating a web server to receive requests and manage reads and writes to DuckDB.
DuckDB uses optimistic concurrency control, an approach generally considered to be the best fit for read‑intensive analytical database
systems as it speeds up read query processing. As a result any transactions that modify the same rows at the same time will cause a
transaction conflict error:
9
DuckDB Documentation
Tip. A common workaround when a transaction conflict is encountered is to rerun the transaction.
10
DuckDB Documentation
Data Import
11
Importing Data
The first step to using a database system is to insert data into that system. DuckDB provides can directly connect to many popular data
sources and offers several data ingestion methods that allow you to easily and efficiently fill up the database. On this page, we provide an
overview of these methods so you can select which one is best suited for your use case.
INSERT Statements
INSERT statements are the standard way of loading data into a database system. They are suitable for quick prototyping, but should be
avoided for bulk loading as they have significant per‑row overhead.
INSERT INTO people VALUES (1, 'Mark');
For a more detailed description, see the page on the INSERT statement.
CSV Loading
Data can be efficiently loaded from CSV files using several methods. The simplest is to use the CSV file's name:
SELECT * FROM 'test.csv';
It is also possible to read data directly from compressed CSV files (e.g., compressed with gzip):
SELECT * FROM 'test.csv.gz';
DuckDB can create a table from the loaded data using the CREATE TABLE ... AS SELECT statement:
CREATE TABLE test AS
SELECT * FROM 'test.csv';
Parquet Loading
Parquet files can be efficiently loaded and queried using their filename:
SELECT * FROM 'test.parquet';
13
DuckDB Documentation
JSON Loading
JSON files can be efficiently loaded and queried using their filename:
Appender
In several APIs (C, C++, Go, Java, and Rust), the Appender can be used as an alternative for bulk data loading. This class can be used to
efficiently add rows to the database system without using SQL statements.
14
Data Sources
DuckDB sources several data sources, including file formats, network protocols, and database systems:
15
DuckDB Documentation
16
CSV Files
CSV Import
Examples
SELECT *
FROM read_csv('flights.csv',
delim = '|',
header = true,
columns = {
'FlightDate': 'DATE',
'UniqueCarrier': 'VARCHAR',
'OriginCityName': 'VARCHAR',
'DestCityName': 'VARCHAR'
});
Alternatively, create a table without specifying the schema manually using a CREATE TABLE .. AS SELECT statement:
If we serialize the entire table, we can simply refer to it with its name.
17
DuckDB Documentation
CSV Loading
CSV loading, i.e., importing CSV files to the database, is a very common, and yet surprisingly tricky, task. While CSVs seem simple on the
surface, there are a lot of inconsistencies found within CSV files that can make loading them a challenge. CSV files come in many different
varieties, are often corrupt, and do not have a schema. The CSV reader needs to cope with all of these different situations.
The DuckDB CSV reader can automatically infer which configuration flags to use by analyzing the CSV file using the CSV sniffer. This will
work correctly in most situations, and should be the first option attempted. In rare situations where the CSV reader cannot figure out the
correct configuration it is possible to manually configure the CSV reader to correctly parse the CSV file. See the auto detection page for
more information.
Parameters
Below are parameters that can be passed to the read_csv function. Where meaningfully applicable, these parameters can also be passed
to the COPY statement.
all_varchar Skip type detection and assume all columns are of type BOOL false
VARCHAR. This option is only supported by the read_
csv function.
allow_quoted_nulls Allow the conversion of quoted values to NULL values BOOL true
auto_detect Auto detect CSV parameters. BOOL true
auto_type_candidates Types that the sniffer uses when detecting column types. TYPE[] default types
The VARCHAR type is always included as a fallback option.
See example.
buffer_size Size of the buffers used to read files, in bytes. Must be large BIGINT 16 * max_
enough to hold four lines and can significantly impact line_size
performance.
columns Column names and types, as a struct (e.g., {'col1': STRUCT (empty)
'INTEGER', 'col2': 'VARCHAR'}). Using this
option disables auto detection.
18
DuckDB Documentation
comment Character used to initiate comments. Lines starting with a VARCHAR ‘| |compression|
comment character (optionally preceded by space Method used
characters) are completely ignored; other lines containing to compress
a comment character are parsed only up to that point. CSV files. By
default this
is detected
automatically
from the file
extension
(e.g.,t.csv.gzwill
use
gzip,t.csvwill
usenone).
Options
arenone,gzip,zstd.
|VARCHAR|auto|
|dateformat|
[Date for-
mat](#docs:sql:functio
used when
parsing and
writing
dates.
|VARCHAR|
(empty) |
|date_format|
Alias
fordateformat;
only
available in
theCOPYstatement.
|VARCHAR|
(empty) |
|decimal_
separator|
Decimal
separator for
numbers.
|VARCHAR|.|
|delim|
Delimiter
character
used to
separate
columns
within each
line. Alias
forsep.
|VARCHAR|,|
|delimiter|
Alias
fordelim; only
available in
theCOPYstatement.
19
|VARCHAR|,|
|escape|
String
DuckDB Documentation
Tip. We recommend the iconv command‑line tool to convert files with encodings not supported by read_csv to UTF‑8. For
example: bash iconv -f ISO-8859-2 -t UTF-8 input.csv > input-utf-8.csv
auto_type_candidates Details
The auto_type_candidates option lets you specify the data types that should be considered by the CSV reader for column data type
detection. Usage example:
The default value for the auto_type_candidates option is ['SQLNULL', 'BOOLEAN', 'BIGINT', 'DOUBLE', 'TIME',
'DATE', 'TIMESTAMP', 'VARCHAR'].
CSV Functions
The read_csv automatically attempts to figure out the correct configuration of the CSV reader using the CSV sniffer. It also automatically
deduces types of columns. If the CSV file has a header, it will use the names found in that header to name the columns. Otherwise, the
columns will be named column0, column1, column2, .... An example with the flights.csv file:
The path can either be a relative path (relative to the current working directory) or an absolute path.
If we set delim / sep, quote, escape, or header explicitly, we can bypass the automatic detection of this particular parameter:
Multiple files can be read at once by providing a glob or a list of files. Refer to the multiple files section for more information.
20
DuckDB Documentation
The COPY statement can be used to load data from a CSV file into a table. This statement has the same syntax as the one used in PostgreSQL.
To load the data using the COPY statement, we must first create a table with the correct schema (which matches the order of the columns
in the CSV file and uses types that fit the values in the CSV file). COPY detects the CSV's configuration options automatically.
If we want to manually specify the CSV format, we can do so using the configuration options of COPY.
CREATE TABLE ontime (flightdate DATE, uniquecarrier VARCHAR, origincityname VARCHAR, destcityname
VARCHAR);
COPY ontime FROM 'flights.csv' (DELIMITER '|', HEADER);
SELECT * FROM ontime;
DuckDB supports reading erroneous CSV files. For details, see the Reading Faulty CSV Files page.
Order Preservation
The CSV reader respects the preserve_insertion_order configuration option to preserve insertion order. When true (the default),
the order of the rows in the result set returned by the CSV reader is the same as the order of the corresponding lines read from the file(s).
When false, there is no guarantee that the order is preserved.
When using read_csv, the system tries to automatically infer how to read the CSV file using the CSV sniffer. This step is necessary because
CSV files are not self‑describing and come in many different dialects. The auto‑detection works roughly as follows:
• Detect the dialect of the CSV file (delimiter, quoting rule, escape)
• Detect the types of each of the columns
• Detect whether or not the file has a header row
By default the system will try to auto‑detect all options. However, options can be individually overridden by the user. This can be useful in
case the system makes a mistake. For example, if the delimiter is chosen incorrectly, we can override it by calling the read_csv with an
explicit delimiter (e.g., read_csv('file.csv', delim = '|')).
21
DuckDB Documentation
The detection works by operating on a sample of the file. The size of the sample can be modified by setting the sample_size parameter.
The default sample size is 20480 rows. Setting the sample_size parameter to -1 means the entire file is read for sampling. The way
sampling is performed depends on the type of file. If we are reading from a regular file on disk, we will jump into the file and try to sample
from different locations in the file. If we are reading from a file in which we cannot jump – such as a .gz compressed CSV file or stdin –
samples are taken only from the beginning of the file.
sniff_csv Function
It is possible to run the CSV sniffer as a separate step using the sniff_csv(filename) function, which returns the detected CSV prop‑
erties as a table with a single row. The sniff_csv function accepts an optional sample_size parameter to configure the number of
rows sampled.
FROM sniff_csv('my_file.csv');
FROM sniff_csv('my_file.csv', sample_size = 1000);
Delimiter Delimiter ,
Quote Quote character "
Escape Escape \
NewLineDelimiterNew‑line delimiter \r\n
Comment Comment character #
SkipRows Number of rows skipped 1
HasHeader Whether the CSV has a header true
Columns Column types encoded as a LIST of ({'name': 'VARCHAR', 'age': 'BIGINT'})
STRUCTs
DateFormat Date format %d/%m/%Y
TimestampFormat Timestamp Format %Y-%m-%dT%H:%M:%S.%f
UserArguments Arguments used to invoke sniff_csv sample_size = 1000
Prompt Prompt ready to be used to read the CSV FROM read_csv('my_file.csv', auto_
detect=false, delim=',', ...)
Prompt
The Prompt column contains a SQL command with the configurations detected by the sniffer.
Detection Steps
Dialect Detection
Dialect detection works by attempting to parse the samples using the set of considered values. The detected dialect is the dialect that has
(1) a consistent number of columns for each row, and (2) the highest number of columns for each row.
22
DuckDB Documentation
delim , | ; \t
quote " ' (empty)
escape " ' \ (empty)
FlightDate|UniqueCarrier|OriginCityName|DestCityName
1988-01-01|AA|New York, NY|Los Angeles, CA
1988-01-02|AA|New York, NY|Los Angeles, CA
1988-01-03|AA|New York, NY|Los Angeles, CA
In this example – the system selects the | as the delimiter. All rows are split into the same amount of columns, and there is more than one
column per row meaning the delimiter was actually found in the CSV file.
Type Detection
After detecting the dialect, the system will attempt to figure out the types of each of the columns. Note that this step is only performed if
we are calling read_csv. In case of the COPY statement the types of the table that we are copying into will be used instead.
The type detection works by attempting to convert the values in each column to the candidate types. If the conversion is unsuccessful, the
candidate type is removed from the set of candidate types for that column. After all samples have been handled – the remaining candidate
type with the highest priority is chosen. The default set of candidate types is given below, in order of priority:
Types
BOOLEAN
BIGINT
DOUBLE
TIME
DATE
TIMESTAMP
VARCHAR
Note everything can be cast to VARCHAR. This type has the lowest priority, i.e., columns are converted to VARCHAR if they cannot be cast
to anything else. In flights.csv the FlightDate column will be cast to a DATE, while the other columns will be cast to VARCHAR.
The set of candidate types that should be considered by the CSV reader can be explicitly specified using the auto_type_candidates
option.
In addition to the default set of candidate types, other types that may be specified using the auto_type_candidates options are:
23
DuckDB Documentation
Types
DECIMAL
FLOAT
INTEGER
SMALLINT
TINYINT
Even though the set of data types that can be automatically detected may appear quite limited, the CSV reader can configured to read
arbitrarily complex types by using the types‑option described in the next section.
Type detection can be entirely disabled by using the all_varchar option. If this is set all columns will remain as VARCHAR (as they
originally occur in the CSV file).
The detected types can be individually overridden using the types option. This option takes either of two options:
• A list of type definitions (e.g., types = ['INTEGER', 'VARCHAR', 'DATE']). This overrides the types of the columns in‑order
of occurrence in the CSV file.
• Alternatively, types takes a name → type map which overrides options of individual columns (e.g., types = {'quarter':
'INTEGER'}).
The set of column types that may be specified using the types option is not as limited as the types available for the auto_type_
candidates option: any valid type definition is acceptable to the types‑option. (To get a valid type definition, use the typeof()
function, or use the column_type column of the DESCRIBE result.)
The sniff_csv() function's Column field returns a struct with column names and types that can be used as a basis for overriding
types.
Header Detection
Header detection works by checking if the candidate header row deviates from the other rows in the file in terms of types. For example,
in flights.csv, we can see that the header row consists of only VARCHAR columns – whereas the values contain a DATE value for the
FlightDate column. As such – the system defines the first row as the header row and extracts the column names from the header row.
In files that do not have a header row, the column names are generated as column0, column1, etc.
Note that headers cannot be detected correctly if all columns are of type VARCHAR – as in this case the system cannot distinguish the
header row from the other rows in the file. In this case, the system assumes the file has a header. This can be overridden by setting the
header option to false.
DuckDB supports the ISO 8601 format format by default for timestamps, dates and times. Unfortunately, not all dates and times are for‑
matted using this standard. For that reason, the CSV reader also supports the dateformat and timestampformat options. Using this
format the user can specify a format string that specifies how the date or timestamp should be read.
As part of the auto‑detection, the system tries to figure out if dates and times are stored in a different representation. This is not always
possible – as there are ambiguities in the representation. For example, the date 01-02-2000 can be parsed as either January 2nd or
February 1st. Often these ambiguities can be resolved. For example, if we later encounter the date 21-02-2000 then we know that the
format must have been DD-MM-YYYY. MM-DD-YYYY is no longer possible as there is no 21nd month.
24
DuckDB Documentation
If the ambiguities cannot be resolved by looking at the data the system has a list of preferences for which date format to use. If the system
choses incorrectly, the user can specify the dateformat and timestampformat options manually.
The system considers the following formats for dates (dateformat). Higher entries are chosen over lower entries in case of ambiguities
(i.e., ISO 8601 is preferred over MM-DD-YYYY).
dateformat
ISO 8601
%y‑%m‑%d
%Y‑%m‑%d
%d‑%m‑%y
%d‑%m‑%Y
%m‑%d‑%y
%m‑%d‑%Y
The system considers the following formats for timestamps (timestampformat). Higher entries are chosen over lower entries in case of
ambiguities.
timestampformat
ISO 8601
%y‑%m‑%d %H:%M:%S
%Y‑%m‑%d %H:%M:%S
%d‑%m‑%y %H:%M:%S
%d‑%m‑%Y %H:%M:%S
%m‑%d‑%y %I:%M:%S %p
%m‑%d‑%Y %I:%M:%S %p
%Y‑%m‑%d %H:%M:%S.%f
CSV files can come in all shapes and forms, with some presenting many errors that make the process of cleanly reading them inherently
difficult. To help users read these files, DuckDB supports detailed error messages, the ability to skip faulty lines, and the possibility of
storing faulty lines in a temporary table to assist users with a data cleaning step.
Structural Errors
DuckDB supports the detection and skipping of several different structural errors. In this section, we will go over each error with an example.
For the examples, consider the following table:
• CAST: Casting errors occur when a column in the CSV file cannot be cast to the expected schema value. For example, the line Pe-
dro,The 90s would cause an error since the string The 90s cannot be cast to a date.
25
DuckDB Documentation
• MISSING COLUMNS: This error occurs if a line in the CSV file has fewer columns than expected. In our example, we expect two
columns; therefore, a row with just one value, e.g., Pedro, would cause this error.
• TOO MANY COLUMNS: This error occurs if a line in the CSV has more columns than expected. In our example, any line with more
than two columns would cause this error, e.g., Pedro,01-01-1992,pdet.
• UNQUOTED VALUE: Quoted values in CSV lines must always be unquoted at the end; if a quoted value remains quoted throughout,
it will cause an error. For example, assuming our scanner uses quote='"', the line "pedro"holanda, 01-01-1992 would
present an unquoted value error.
• LINE SIZE OVER MAXIMUM: DuckDB has a parameter that sets the maximum line size a CSV file can have, which by default is set
to 2,097,152 bytes. Assuming our scanner is set to max_line_size = 25, the line Pedro Holanda, 01-01-1992 would
produce an error, as it exceeds 25 bytes.
• INVALID UNICODE: DuckDB only supports UTF‑8 strings; thus, lines containing non‑UTF‑8 characters will produce an error. For
example, the line pedro\xff\xff, 01-01-1992 would be problematic.
By default, when performing a CSV read, if any structural errors are encountered, the scanner will immediately stop the scanning process
and throw the error to the user. These errors are designed to provide as much information as possible to allow users to evaluate them
directly in their CSV file.
file= people.csv
delimiter = , (Auto-Detected)
quote = " (Auto-Detected)
escape = " (Auto-Detected)
new_line = \r\n (Auto-Detected)
header = true (Auto-Detected)
skip_rows = 0 (Auto-Detected)
date_format = (DD-MM-YYYY) (Auto-Detected)
timestamp_format = (Auto-Detected)
null_padding=0
sample_size=20480
ignore_errors=false
all_varchar=0
The first block provides us with information regarding where the error occurred, including the line number, the original CSV line, and which
field was problematic:
26
DuckDB Documentation
Since the type of this field was auto‑detected, it suggests defining the field as a VARCHAR or fully utilizing the dataset for type detection.
Finally, the last block presents some of the options used in the scanner that can cause errors, indicating whether they were auto‑detected
or manually set by the user.
There are cases where CSV files may have multiple structural errors, and users simply wish to skip these and read the correct data. Reading
erroneous CSV files is possible by utilizing the ignore_errors option. With this option set, rows containing data that would otherwise
cause the CSV parser to generate an error will be ignored. In our example, we will demonstrate a CAST error, but note that any of the errors
described in our Structural Error section would cause the faulty line to be skipped.
Pedro,31
Oogie Boogie, three
If you read the CSV file, specifying that the first column is a VARCHAR and the second column is an INTEGER, loading the file would fail, as
the string three cannot be converted to an INTEGER.
However, with ignore_errors set, the second row of the file is skipped, outputting only the complete first row. For example:
FROM read_csv(
'faulty.csv',
columns = {'name': 'VARCHAR', 'age': 'INTEGER'},
ignore_errors = true
);
Outputs:
name age
Pedro 31
One should note that the CSV Parser is affected by the projection pushdown optimization. Hence, if we were to select only the name column,
both rows would be considered valid, as the casting error on the age would never occur. For example:
SELECT name
FROM read_csv('faulty.csv', columns = {'name': 'VARCHAR', 'age': 'INTEGER'});
Outputs:
27
DuckDB Documentation
name
Pedro
Oogie Boogie
Being able to read faulty CSV files is important, but for many data cleaning operations, it is also necessary to know exactly which lines are
corrupted and what errors the parser discovered on them. For scenarios like these, it is possible to use DuckDB's CSV Rejects Table feature.
By default, this feature creates two temporary tables.
Note that any of the errors described in our Structural Error section will be stored in the rejects tables. Also, if a line has multiple errors,
multiple entries will be stored for the same line, one for each error.
Reject Scans
Reject Errors
scan_id The internal ID used in DuckDB to represent that scanner, used to join UBIGINT
with reject scans tables
28
DuckDB Documentation
file_id The file_id represents a unique file in a scanner, used to join with reject UBIGINT
scans tables
line Line number, from the CSV File, where the error occurred. UBIGINT
line_byte_position Byte Position of the start of the line, where the error occurred. UBIGINT
byte_position Byte Position where the error occurred. UBIGINT
column_idx If the error happens in a specific column, the index of the column. UBIGINT
column_name If the error happens in a specific column, the name of the column. VARCHAR
error_type The type of the error that happened. ENUM
csv_line The original CSV line. VARCHAR
error_message The error message produced by DuckDB. VARCHAR
Parameters
The parameters listed below are used in the read_csv function to configure the CSV Rejects Table.
store_rejects If set to true, any errors in the file will be skipped and BOOLEAN False
stored in the default rejects temporary tables.
rejects_scan Name of a temporary table where the information of the VARCHAR reject_scans
scan information of faulty CSV file are stored.
rejects_table Name of a temporary table where the information of the VARCHAR reject_errors
faulty lines of a CSV file are stored.
rejects_limit Upper limit on the number of faulty records from a CSV file BIGINT 0
that will be recorded in the rejects table. 0 is used when no
limit should be applied.
To store the information of the faulty CSV lines in a rejects table, the user must simply set the store_rejects option to true. For exam‑
ple:
FROM read_csv(
'faulty.csv',
columns = {'name': 'VARCHAR', 'age': 'INTEGER'},
store_rejects = true
);
You can then query both the reject_scans and reject_errors tables, to retrieve information about the rejected tuples. For exam‑
ple:
FROM reject_scans;
Outputs:
29
DuckDB Documentation
FROM reject_errors;
Outputs:
Below is a collection of tips to help when attempting to import complex CSV files. In the examples, we use the flights.csv file.
If a file contains only string columns the header auto‑detection might fail. Provide the header option to override this behavior.
If the file does not contain a header, names will be auto‑generated by default. You can provide your own names with the names option.
The types flag can be used to override types of only certain columns by providing a struct of name → type mappings.
The COPY statement copies data directly into a table. The CSV reader uses the schema of the table instead of auto‑detecting types from
the file. This speeds up the auto‑detection, and prevents mistakes from being made during auto‑detection.
30
DuckDB Documentation
The union_by_name option can be used to unify the schema of files that have different or missing columns. For files that do not have
certain columns, NULL values are filled in.
31
DuckDB Documentation
32
JSON Files
JSON Overview
DuckDB supports SQL functions that are useful for reading values from existing JSON and creating new JSON data. JSON is supported with
the json extension which is shipped with most DuckDB distributions and is auto‑loaded on first use. If you would like to install or load it
manually, please consult the “Installing and Loading” page.
About JSON
JSON is an open standard file format and data interchange format that uses human‑readable text to store and transmit data objects con‑
sisting of attribute–value pairs and arrays (or other serializable values). While it is not a very efficient format for tabular data, it is very
commonly used, especially as a data interchange format.
Best practice. DuckDB implements multiple interfaces for JSON extraction: JSONPath and JSON Pointer. Both of them work with
the arrow operator (->) and the json_extract function call. It's best to pick one syntax and use it in your entire application.
Indexing
Warning. Following PostgreSQL's conventions, DuckDB uses 1‑based indexing for its ARRAY and LIST data types but 0‑based in‑
dexing for the JSON data type.
Examples
Loading JSON
SELECT *
FROM read_json('todos.json',
format = 'array',
columns = {userId: 'UBIGINT',
id: 'UBIGINT',
title: 'VARCHAR',
completed: 'BOOLEAN'});
33
DuckDB Documentation
CREATE TABLE todos (userId UBIGINT, id UBIGINT, title VARCHAR, completed BOOLEAN);
COPY todos FROM 'todos.json';
Alternatively, create a table without specifying the schema manually with a CREATE TABLE ... AS SELECT clause:
Writing JSON
Create a table with a column for storing JSON data and insert data into it:
"anatidae"
"anatidae"
anatidae
Creating JSON
Function Description
to_json(any) Create JSON from a value of any type. Our LIST is converted to a JSON array, and
our STRUCT and MAP are converted to a JSON object.
json_quote(any) Alias for to_json.
array_to_json(list) Alias for to_json that only accepts LIST.
34
DuckDB Documentation
Function Description
Examples:
SELECT to_json('duck');
"duck"
[1,2,3]
{"duck":42}
SELECT to_json(map(['duck'],[42]));
{"duck":42}
["duck",42,"goose",123]
{"duck":42,"goose":123}
{"goose":123,"duck":42}
Loading JSON
The DuckDB JSON reader can automatically infer which configuration flags to use by analyzing the JSON file. This will work correctly in most
situations, and should be the first option attempted. In rare situations where the JSON reader cannot figure out the correct configuration,
it is possible to manually configure the JSON reader to correctly parse the JSON file.
The read_json is the simplest method of loading JSON files: it automatically attempts to figure out the correct configuration of the JSON
reader. It also automatically deduces types of columns. In the following example, we use the todos.json file,
SELECT *
FROM read_json('todos.json')
LIMIT 5;
35
DuckDB Documentation
If we specify the columns, we can bypass the automatic detection. Note that not all columns need to be specified:
SELECT *
FROM read_json(
'todos.json',
columns = {userId: 'UBIGINT', completed: 'BOOLEAN'}
);
Multiple files can be read at once by providing a glob or a list of files. Refer to the multiple files section for more information.
Function Description
read_json_objects(filename) Read a JSON object from filename, where filename can also
be a list of files or a glob pattern.
read_ndjson_objects(filename) Alias for read_json_objects with the parameter format set
to 'newline_delimited'.
read_json_objects_auto(filename) Alias for read_json_objects with the parameter format set
to 'auto' .
Parameters
36
DuckDB Documentation
compression The compression type for the file. By default this will be VARCHAR 'auto'
detected automatically from the file extension (e.g.,
t.json.gz will use gzip, t.json will use none). Options
are 'none', 'gzip', 'zstd', and 'auto'.
filename Whether or not an extra filename column should be BOOL false
included in the result.
format Can be one of ['auto', 'unstructured', VARCHAR 'array'
'newline_delimited', 'array'].
hive_partitioning Whether or not to interpret the path as a Hive partitioned BOOL false
path.
ignore_errors Whether to ignore parse errors (only possible when BOOL false
format is 'newline_delimited').
maximum_sample_files The maximum number of JSON files sampled for BIGINT 32
auto‑detection.
maximum_object_size The maximum size of a JSON object (in bytes). UINTEGER 16777216
The format parameter specifies how to read the JSON from a file. With 'unstructured', the top‑level JSON is read, e.g. for
birds.json:
{
"duck": 42
}
{
"goose": [1, 2, 3]
}
┌──────────────────────────────┐
│ json │
│ json │
├──────────────────────────────┤
│ {\n "duck": 42\n} │
│ {\n "goose": [1, 2, 3]\n} │
└──────────────────────────────┘
With 'newline_delimited', NDJSON is read, where each JSON is separated by a newline (\n), e.g., for birds-nd.json:
{"duck": 42}
{"goose": [1, 2, 3]}
┌──────────────────────┐
│ json │
│ json │
├──────────────────────┤
│ {"duck": 42} │
│ {"goose": [1, 2, 3]} │
└──────────────────────┘
37
DuckDB Documentation
[
{
"duck": 42
},
{
"goose": [1, 2, 3]
}
]
┌──────────────────────────────────────┐
│ json │
│ json │
├──────────────────────────────────────┤
│ {\n "duck": 42\n } │
│ {\n "goose": [1, 2, 3]\n } │
└──────────────────────────────────────┘
DuckDB also supports reading JSON as a table, using the following functions:
Function Description
read_json(filename) Read JSON from filename, where filename can also be a list of files, or a glob
pattern.
read_json_auto(filename) Alias for read_json.
read_ndjson(filename) Alias for read_json with parameter format set to 'newline_delimited'.
read_ndjson_auto(filename) Alias for read_json with parameter format set to 'newline_delimited'.
Parameters
Besides the maximum_object_size, format, ignore_errors and compression, these functions have additional parameters:
auto_detect Whether to auto‑detect the names of the keys and data types of BOOL false
the values automatically
columns A struct that specifies the key names and value types contained STRUCT (empty)
within the JSON file (e.g., {key1: 'INTEGER', key2:
'VARCHAR'}). If auto_detect is enabled these will be
inferred
dateformat Specifies the date format to use when parsing dates. See Date VARCHAR 'iso'
Format
maximum_depth Maximum nesting depth to which the automatic schema BIGINT -1
detection detects types. Set to ‑1 to fully detect nested JSON
types
38
DuckDB Documentation
Note that DuckDB can convert JSON arrays directly to its internal LIST type, and missing keys become NULL:
SELECT *
FROM read_json(
['birds1.json', 'birds2.json'],
columns = {duck: 'INTEGER', goose: 'INTEGER[]', swan: 'DOUBLE'}
);
42 [1, 2, 3] NULL
43 [4, 5, 6] 3.3
DuckDB can read (and auto‑detect) a variety of formats, specified with the format parameter. Querying a JSON file that contains an
'array', e.g.:
[
{
"duck": 42,
"goose": 4.2
},
{
"duck": 43,
"goose": 4.3
}
]
Can be queried exactly the same as a JSON file that contains 'unstructured' JSON, e.g.:
39
DuckDB Documentation
{
"duck": 42,
"goose": 4.2
}
{
"duck": 43,
"goose": 4.3
}
duck goose
42 4.2
43 4.3
If your JSON file does not contain 'records', i.e., any other type of JSON than objects, DuckDB can still read it. This is specified with the
records parameter. The records parameter specifies whether the JSON contains records that should be unpacked into individual
columns, i.e., reading the following file with records:
duck goose
42 [1,2,3]
43 [4,5,6]
You can read the same file with records set to 'false', to get a single column, which is a STRUCT containing the data:
json
For additional examples reading more complex data, please see the “Shredding Deeply Nested JSON, One Vector at a Time” blog post.
When the json extension is installed, FORMAT JSON is supported for COPY FROM, IMPORT DATABASE, as well as COPY TO, and
EXPORT DATABASE. See the COPY statement and the IMPORT / EXPORT clauses.
By default, COPY expects newline‑delimited JSON. If you prefer copying data to/from a JSON array, you can specify ARRAY true, e.g.,
40
DuckDB Documentation
[
{"i":0},
{"i":1},
{"i":2},
{"i":3},
{"i":4}
]
Parameters
auto_detect Whether to auto‑detect detect the names of the keys and BOOL false
data types of the values automatically
columns A struct that specifies the key names and value types STRUCT (empty)
contained within the JSON file (e.g., {key1:
'INTEGER', key2: 'VARCHAR'}). If auto_detect
is enabled these will be inferred
compression The compression type for the file. By default this will be VARCHAR 'auto_detect'
detected automatically from the file extension (e.g.,
t.json.gz will use gzip, t.json will use none). Options
are 'uncompressed', 'gzip', 'zstd', and 'auto_
detect'.
convert_strings_to_ Whether strings representing integer values should be BOOL false
integers converted to a numerical type.
dateformat Specifies the date format to use when parsing dates. See VARCHAR 'iso'
Date Format
filename Whether or not an extra filename column should be BOOL false
included in the result.
format Can be one of ['auto', 'unstructured', VARCHAR 'array'
'newline_delimited', 'array']
hive_partitioning Whether or not to interpret the path as a Hive partitioned BOOL false
path.
ignore_errors Whether to ignore parse errors (only possible when BOOL false
format is 'newline_delimited')
maximum_depth Maximum nesting depth to which the automatic schema BIGINT -1
detection detects types. Set to ‑1 to fully detect nested
JSON types
41
DuckDB Documentation
maximum_object_size The maximum size of a JSON object (in bytes) UINTEGER 16777216
records Can be one of ['auto', 'true', 'false'] VARCHAR 'records'
sample_size Option to define number of sample objects for automatic UBIGINT 20480
JSON type detection. Set to ‑1 to scan the entire input file
timestampformat Specifies the date format to use when parsing timestamps. VARCHAR 'iso'
See Date Format
union_by_name Whether the schema's of multiple JSON files should be BOOL false
unified.
Writing JSON
The contents of tables or the result of queries can be written directly to a JSON file using the COPY statement. For example:
{"name":"Amsterdam","id":1}
{"name":"London","id":2}
JSON Type
DuckDB supports json via the JSON logical type. The JSON logical type is interpreted as JSON, i.e., parsed, in JSON functions rather
than interpreted as VARCHAR, i.e., a regular string (modulo the equality‑comparison caveat at the bottom of this page). All JSON creation
functions return values of this type.
We also allow any of DuckDB's types to be cast to JSON, and JSON to be cast back to any of DuckDB's types, for example, to cast JSON to
DuckDB's STRUCT type, run:
{'duck': 42}
And back:
{"duck":42}
This works for our nested types as shown in the example, but also for non‑nested types:
SELECT '2023-05-12'::DATE::JSON;
"2023-05-12"
The only exception to this behavior is the cast from VARCHAR to JSON, which does not alter the data, but instead parses and validates the
contents of the VARCHAR as JSON.
42
DuckDB Documentation
There are two extraction functions, which have their respective operators. The operators can only be used if the string is stored as the JSON
logical type. These functions supports the same two location notations as JSON Scalar functions.
json_exists(json, path)
json_extract(json, path) json_extract_path ->
json_extract_string(json, path) json_extract_path_text ->>
json_value(json, path)
Note that the arrow operator ->, which is used for JSON extracts, has a low precedence as it is also used in lambda functions.
Therefore, you need to surround the -> operator with parentheses when expressing operations such as equality comparisons (=). For
example:
Examples:
"anatidae"
"anatidae"
"duck"
"duck"
["duck", "goose"]
anatidae
43
DuckDB Documentation
anatidae
duck
duck
[duck, goose]
If multiple values need to be extracted from the same JSON, it is more efficient to extract a list of paths:
SELECT
json_extract(j, 'family') AS family,
json_extract(j, 'species') AS species
FROM example;
family species
”anatidae” [”duck”,”goose”,”swan”,null]
The following produces the same result but is faster and more memory‑efficient:
WITH extracted AS (
SELECT json_extract(j, ['family', 'species']) AS extracted_list
FROM example
)
SELECT
extracted_list[1] AS family,
extracted_list[2] AS species
FROM extracted;
The following scalar JSON functions can be used to gain information about the stored JSON values. With the exception of json_
valid(json), all JSON functions produce an error when invalid JSON is supplied.
We support two kinds of notations to describe locations within JSON: JSON Pointer and JSONPath.
Function Description
json_array_length(json[, path]) Return the number of elements in the JSON array json, or 0 if it is not a
JSON array. If path is specified, return the number of elements in the
JSON array at the given path. If path is a LIST, the result will be LIST
of array lengths.
44
DuckDB Documentation
Function Description
The JSONPointer syntax separates each field with a /. For example, to extract the first element of the array with key duck, you can do:
The JSONPath syntax separates fields with a ., and accesses array elements with [i], and always starts with $. Using the same example,
we can do the following:
JSONPath is more expressive, and can also access from the back of lists:
{"family":"anatidae","species":["duck","goose","swan",null]}
"anatidae"
"duck"
45
DuckDB Documentation
true
SELECT json_valid('{');
false
[4]
OBJECT
[family, species]
{"family":"VARCHAR","species":["VARCHAR"]}
["JSON"]
true
true
true
Function Description
json_group_array(any) Return a JSON array with all values of any in the aggregation.
json_group_object(key, value) Return a JSON object with all key, value pairs in the aggregation.
46
DuckDB Documentation
Function Description
Examples:
[42, 7]
{"duck":42,"goose":7}
{"family":"VARCHAR","species":["VARCHAR"],"coolness":"DOUBLE","hair":"BOOLEAN"}
In many cases, it is inefficient to extract values from JSON one‑by‑one. Instead, we can “extract” all values at once, transforming JSON to
the nested types LIST and STRUCT.
Function Description
The structure argument is JSON of the same form as returned by json_structure. The structure argument can be modified to
transform the JSON into the desired structure and types. It is possible to extract fewer key/value pairs than are present in the JSON, and it
is also possible to extract more: missing keys become NULL.
Examples:
47
DuckDB Documentation
The JSON extension can attempt to determine the format of a JSON file when setting format to auto. Here are some example JSON files
and the corresponding format settings that should be used.
In each of the below cases, the format setting was not needed, as DuckDB was able to infer it correctly, but it is included for illustrative
purposes. A query of this shape would work in each case:
SELECT *
FROM filename.json;
Format: newline_delimited
With format = 'newline_delimited' newline‑delimited JSON can be parsed. Each line is a JSON.
SELECT *
FROM read_json('records.json', format = 'newline_delimited');
key1 key2
value1 value1
value2 value2
value3 value3
Format: array
If the JSON file contains a JSON array of objects (pretty‑printed or not), array_of_objects may be used. To demonstrate its use, we
use the example file records-in-array.json:
[
{"key1":"value1", "key2": "value1"},
{"key1":"value2", "key2": "value2"},
{"key1":"value3", "key2": "value3"}
]
SELECT *
FROM read_json('records-in-array.json', format = 'array');
key1 key2
value1 value1
48
DuckDB Documentation
key1 key2
value2 value2
value3 value3
Format: unstructured
If the JSON file contains JSON that is not newline‑delimited or an array, unstructured may be used. To demonstrate its use, we use the
example file unstructured.json:
{
"key1":"value1",
"key2":"value1"
}
{
"key1":"value2",
"key2":"value2"
}
{
"key1":"value3",
"key2":"value3"
}
SELECT *
FROM read_json('unstructured.json', format = 'unstructured');
key1 key2
value1 value1
value2 value2
value3 value3
Records Settings
The JSON extension can attempt to determine whether a JSON file contains records when setting records = auto. When records =
true, the JSON extension expects JSON objects, and will unpack the fields of JSON objects into individual columns.
SELECT *
FROM read_json('records.json', records = true);
key1 key2
value1 value1
value2 value2
value3 value3
When records = false, the JSON extension will not unpack the top‑level objects, and create STRUCTs instead:
49
DuckDB Documentation
SELECT *
FROM read_json('records.json', records = false);
json
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
SELECT *
FROM read_json('arrays.json', records = false);
json
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
The json extension is shipped by default in DuckDB builds, otherwise, it will be transparently autoloaded on first use. If you would like to
install and load it manually, run:
INSTALL json;
LOAD json;
DuckDB provides functions to serialize and deserialize SELECT statements between SQL and JSON, as well as executing JSON serialized
statements.
50
DuckDB Documentation
The json_serialize_sql(varchar) function takes three optional parameters, skip_empty, skip_null, and format that can
be used to control the output of the serialized statements.
If you run the json_execute_serialized_sql(varchar) table function inside of a transaction the serialized statements will not
be able to see any transaction local changes. This is because the statements are executed in a separate query context. You can use the
PRAGMA json_execute_serialized_sql(varchar) pragma version to execute the statements in the same query context as the
pragma, although with the limitation that the serialized JSON must be provided as a constant string, i.e., you cannot do PRAGMA json_
execute_serialized_sql(json_serialize_sql(...)).
Note that these functions do not preserve syntactic sugar such as FROM * SELECT ..., so a statement round‑tripped through json_
deserialize_sql(json_serialize_sql(...)) may not be identical to the original statement, but should always be semanti‑
cally equivalent and produce the same output.
Examples
Simple example:
{"error":false,"statements":[{"node":{"type":"SELECT_NODE","modifiers":[],"cte_map":{"map":[]},"select_
list":[{"class":"CONSTANT","type":"VALUE_CONSTANT","alias":"","value":{"type":{"id":"INTEGER","type_
info":null},"is_null":false,"value":2}}],"from_table":{"type":"EMPTY","alias":"","sample":null},"where_
clause":null,"group_expressions":[],"group_sets":[],"aggregate_handling":"STANDARD_
HANDLING","having":null,"sample":null,"qualify":null}}]}
{"error":false,"statements":[{"node":{"type":"SELECT_NODE","select_
list":[{"class":"FUNCTION","type":"FUNCTION","function_
name":"+","children":[{"class":"CONSTANT","type":"VALUE_CONSTANT","value":{"type":{"id":"INTEGER"},"is_
null":false,"value":1}},{"class":"CONSTANT","type":"VALUE_
CONSTANT","value":{"type":{"id":"INTEGER"},"is_null":false,"value":2}}],"order_bys":{"type":"ORDER_
MODIFIER"},"distinct":false,"is_operator":true,"export_state":false}],"from_
table":{"type":"EMPTY"},"aggregate_handling":"STANDARD_HANDLING"}},{"node":{"type":"SELECT_
NODE","select_list":[{"class":"FUNCTION","type":"FUNCTION","function_
name":"+","children":[{"class":"COLUMN_REF","type":"COLUMN_REF","column_names":["a"]},{"class":"COLUMN_
REF","type":"COLUMN_REF","column_names":["b"]}],"order_bys":{"type":"ORDER_
MODIFIER"},"distinct":false,"is_operator":true,"export_state":false}],"from_table":{"type":"BASE_
TABLE","table_name":"tbl1"},"aggregate_handling":"STANDARD_HANDLING"}}]}
SELECT (1 + 2)
Example with deserialize and syntax sugar, which is lost during the transformation:
SELECT (1 + 2) FROM x
51
DuckDB Documentation
Parser Error: Error parsing json: parser: syntax error at or near "TOTALLY"
Caveats
Equality Comparison
Warning. Currently, equality comparison of JSON files can differ based on the context. In some cases, it is based on raw text com‑
parison, while in other cases, it uses logical content comparison.
SELECT
a != b, -- Space is part of physical JSON content. Despite equal logical content, values are treated
as not equal.
c != d, -- Same.
c[0] = d[0], -- Equality because space was removed from physical content of fields:
a = c[0], -- Indeed, field is equal to empty list without space...
b != c[0], -- ... but different from empty list with space.
FROM (
SELECT
'[]'::JSON AS a,
'[ ]'::JSON AS b,
'[[]]'::JSON AS c,
'[[ ]]'::JSON AS d
);
52
Multiple Files
DuckDB can read multiple files of different types (CSV, Parquet, JSON files) at the same time using either the glob syntax, or by providing a
list of files to read. See the combining schemas page for tips on reading files with different schemas.
CSV
Read all files with a name ending in .csv in the folder dir:
SELECT *
FROM 'dir/*.csv';
Read all files with a name ending in .csv, two directories deep:
SELECT *
FROM '*/*/*.csv';
Read all files with a name ending in .csv, at any depth in the folder dir:
SELECT *
FROM 'dir/**/*.csv';
SELECT *
FROM read_csv(['flights1.csv', 'flights2.csv']);
Read the CSV files flights1.csv and flights2.csv, unifying schemas by name and outputting a filename column:
SELECT *
FROM read_csv(['flights1.csv', 'flights2.csv'], union_by_name = true, filename = true);
Parquet
SELECT *
FROM 'test/*.parquet';
SELECT *
FROM read_parquet(['file1.parquet', 'file2.parquet', 'file3.parquet']);
SELECT *
FROM read_parquet(['folder1/*.parquet', 'folder2/*.parquet']);
Read all Parquet files that match the glob pattern at any depth:
SELECT *
FROM read_parquet('dir/**/*.parquet');
53
DuckDB Documentation
DuckDB can also read a series of Parquet files and treat them as if they were a single table. Note that this only works if the Parquet files
have the same schema. You can specify which Parquet files you want to read using a list parameter, glob pattern matching syntax, or a
combination of both.
List Parameter
The read_parquet function can accept a list of filenames as the input parameter.
SELECT *
FROM read_parquet(['file1.parquet', 'file2.parquet', 'file3.parquet']);
Glob Syntax
Any file name input to the read_parquet function can either be an exact filename, or use a glob syntax to read multiple files that match
a pattern.
Wildcard Description
Note that the ? wildcard in globs is not supported for reads over S3 due to HTTP encoding issues.
Here is an example that reads all the files that end with .parquet located in the test folder:
SELECT *
FROM read_parquet('test/*.parquet');
List of Globs
The glob syntax and the list input parameter can be combined to scan files that meet one of multiple patterns.
SELECT *
FROM read_parquet(['folder1/*.parquet', 'folder2/*.parquet']);
DuckDB can read multiple CSV files at the same time using either the glob syntax, or by providing a list of files to read.
Filename
The filename argument can be used to add an extra filename column to the result that indicates which row came from which file. For
example:
54
DuckDB Documentation
SELECT *
FROM read_csv(['flights1.csv', 'flights2.csv'], union_by_name = true, filename = true);
The glob pattern matching syntax can also be used to search for filenames using the glob table function. It accepts one parameter: the
path to search (which may include glob patterns).
SELECT *
FROM glob('*');
file
test.csv
test.json
test.parquet
test2.csv
test2.parquet
todos.json
Combining Schemas
Examples
Combining Schemas
When reading from multiple files, we have to combine schemas from those files. That is because each file has its own schema that can
differ from the other files. DuckDB offers two ways of unifying schemas of multiple files: by column position and by column name.
By default, DuckDB reads the schema of the first file provided, and then unifies columns in subsequent files by column position. This works
correctly as long as all files have the same schema. If the schema of the files differs, you might want to use the union_by_name option
to allow DuckDB to construct the schema by reading all of the names instead.
55
DuckDB Documentation
Union by Position
By default, DuckDB unifies the columns of these different files by position. This means that the first column in each file is combined
together, as well as the second column in each file, etc. For example, consider the following two files.
flights1.csv:
FlightDate|UniqueCarrier|OriginCityName|DestCityName
1988-01-01|AA|New York, NY|Los Angeles, CA
1988-01-02|AA|New York, NY|Los Angeles, CA
flights2.csv:
FlightDate|UniqueCarrier|OriginCityName|DestCityName
1988-01-03|AA|New York, NY|Los Angeles, CA
Reading the two files at the same time will produce the following result set:
Union by Name
If you are processing multiple files that have different schemas, perhaps because columns have been added or renamed, it might be de‑
sirable to unify the columns of different files by name instead. This can be done by providing the union_by_name option. For example,
consider the following two files, where flights4.csv has an extra column (UniqueCarrier).
flights3.csv:
FlightDate|OriginCityName|DestCityName
1988-01-01|New York, NY|Los Angeles, CA
1988-01-02|New York, NY|Los Angeles, CA
flights4.csv:
FlightDate|UniqueCarrier|OriginCityName|DestCityName
1988-01-03|AA|New York, NY|Los Angeles, CA
Reading these when unifying column names by position results in an error – as the two files have a different number of columns. When
specifying the union_by_name option, the columns are correctly unified, and any missing values are set to NULL.
56
DuckDB Documentation
57
DuckDB Documentation
58
Parquet Files
Examples
If the file does not end in .parquet, use the read_parquet function:
SELECT *
FROM read_parquet('test.parq');
Use list parameter to read three Parquet files and treat them as a single table:
SELECT *
FROM read_parquet(['file1.parquet', 'file2.parquet', 'file3.parquet']);
SELECT *
FROM 'test/*.parquet';
Read all files that match the glob pattern, and include a filename column:
SELECT *
FROM read_parquet('test/*.parquet', filename = true);
Use a list of globs to read all Parquet files from two specific folders:
SELECT *
FROM read_parquet(['folder1/*.parquet', 'folder2/*.parquet']);
SELECT *
FROM read_parquet('https://some.url/some_file.parquet');
SELECT *
FROM parquet_metadata('test.parquet');
59
DuckDB Documentation
SELECT *
FROM parquet_file_metadata('test.parquet');
SELECT *
FROM parquet_kv_metadata('test.parquet');
SELECT *
FROM parquet_schema('test.parquet');
Write the results of a query to a Parquet file using the default compression (Snappy):
COPY
(SELECT * FROM tbl)
TO 'result-snappy.parquet'
(FORMAT 'parquet');
Write the results from a query to a Parquet file with specific compression and row group size:
COPY
(FROM generate_series(100_000))
TO 'test.parquet'
(FORMAT 'parquet', COMPRESSION 'zstd', ROW_GROUP_SIZE 100_000);
Parquet Files
Parquet files are compressed columnar files that are efficient to load and process. DuckDB provides support for both reading and writing
Parquet files in an efficient manner, as well as support for pushing filters and projections into the Parquet file scans.
Parquet data sets differ based on the number of files, the size of individual files, the compression algorithm used row group size, etc.
These have a significant effect on performance. Please consult the Performance Guide for details.
read_parquet Function
If your file ends in .parquet, the function syntax is optional. The system will automatically infer that you are reading a Parquet file:
Multiple files can be read at once by providing a glob or a list of files. Refer to the multiple files section for more information.
60
DuckDB Documentation
Parameters
There are a number of options exposed that can be passed to the read_parquet function or the COPY statement.
binary_as_string Parquet files generated by legacy writers do not correctly BOOL false
set the UTF8 flag for strings, causing string columns to be
loaded as BLOB instead. Set this to true to load binary
columns as strings.
encryption_config Configuration for Parquet encryption. STRUCT ‑
filename Whether or not an extra filename column should be BOOL false
included in the result.
file_row_number Whether or not to include the file_row_number BOOL false
column.
hive_partitioning Whether or not to interpret the path as a Hive partitioned BOOL true
path.
union_by_name Whether the columns of multiple schemas should be BOOL false
unified by name, rather than by position.
Partial Reading
DuckDB supports projection pushdown into the Parquet file itself. That is to say, when querying a Parquet file, only the columns required
for the query are read. This allows you to read only the part of the Parquet file that you are interested in. This will be done automatically
by DuckDB.
DuckDB also supports filter pushdown into the Parquet reader. When you apply a filter to a column that is scanned from a Parquet file,
the filter will be pushed down into the scan, and can even be used to skip parts of the file using the built‑in zonemaps. Note that this will
depend on whether or not your Parquet file contains zonemaps.
Filter and projection pushdown provide significant performance benefits. See our blog post “Querying Parquet with Precision Using
DuckDB” for more information.
You can also insert the data into a table or create a table from the Parquet file directly. This will load the data from the Parquet file and
insert it into the database:
If you wish to keep the data stored inside the Parquet file, but want to query the Parquet file directly, you can create a view over the read_
parquet function. You can then query the Parquet file as if it were a built‑in table:
61
DuckDB Documentation
DuckDB also has support for writing to Parquet files using the COPY statement syntax. See the COPY Statement page for details, including
all possible parameters for the COPY statement.
COPY
(SELECT * FROM tbl)
TO 'result-snappy.parquet'
(FORMAT 'parquet');
COPY tbl
TO 'result-zstd.parquet'
(FORMAT 'parquet', CODEC 'zstd');
Write tbl to a zstd‑compressed Parquet file with the lowest compression level yielding the fastest compression:
COPY tbl
TO 'result-zstd.parquet'
(FORMAT 'parquet', CODEC 'zstd', COMPRESSION_LEVEL 1);
COPY (
SELECT
42 AS number,
true AS is_even
) TO 'kv_metadata.parquet' (
FORMAT PARQUET,
KV_METADATA {
number: 'Answer to life, universe, and everything',
is_even: 'not ''odd''' -- single quotes in values must be escaped
}
);
COPY
'test.csv'
TO 'result-uncompressed.parquet'
(FORMAT 'parquet', CODEC 'uncompressed');
Write a query to a Parquet file with zstd‑compression (same as CODEC) and row group size:
COPY
(FROM generate_series(100_000))
TO 'row-groups-zstd.parquet'
(FORMAT PARQUET, COMPRESSION ZSTD, ROW_GROUP_SIZE 100_000);
62
DuckDB Documentation
COPY
(FROM generate_series(100_000))
TO 'result-lz4.parquet'
(FORMAT PARQUET, COMPRESSION LZ4);
Or:
COPY
(FROM generate_series(100_000))
TO 'result-lz4.parquet'
(FORMAT PARQUET, COMPRESSION LZ4_RAW);
DuckDB's EXPORT command can be used to export an entire database to a series of Parquet files. See the Export statement documentation
for more details:
Encryption
The support for Parquet files is enabled via extension. The parquet extension is bundled with almost all clients. However, if your client
does not bundle the parquet extension, the extension must be installed separately:
INSTALL parquet;
Parquet Metadata
The parquet_metadata function can be used to query the metadata contained within a Parquet file, which reveals various internal
details of the Parquet file such as the statistics of the different columns. This can be useful for figuring out what kind of skipping is possible
in Parquet files, or even to obtain a quick overview of what the different columns contain:
SELECT *
FROM parquet_metadata('test.parquet');
Field Type
file_name VARCHAR
row_group_id BIGINT
row_group_num_rows BIGINT
row_group_num_columns BIGINT
row_group_bytes BIGINT
column_id BIGINT
63
DuckDB Documentation
Field Type
file_offset BIGINT
num_values BIGINT
path_in_schema VARCHAR
type VARCHAR
stats_min VARCHAR
stats_max VARCHAR
stats_null_count BIGINT
stats_distinct_count BIGINT
stats_min_value VARCHAR
stats_max_value VARCHAR
compression VARCHAR
encodings VARCHAR
index_page_offset BIGINT
dictionary_page_offset BIGINT
data_page_offset BIGINT
total_compressed_size BIGINT
total_uncompressed_size BIGINT
key_value_metadata MAP(BLOB, BLOB)
Parquet Schema
The parquet_schema function can be used to query the internal schema contained within a Parquet file. Note that this is the schema
as it is contained within the metadata of the Parquet file. If you want to figure out the column names and types contained within a Parquet
file it is easier to use DESCRIBE.
SELECT *
FROM parquet_schema('test.parquet');
Field Type
file_name VARCHAR
name VARCHAR
type VARCHAR
type_length VARCHAR
repetition_type VARCHAR
num_children BIGINT
converted_type VARCHAR
64
DuckDB Documentation
Field Type
scale BIGINT
precision BIGINT
field_id BIGINT
logical_type VARCHAR
The parquet_file_metadata function can be used to query file‑level metadata such as the format version and the encryption algo‑
rithm used:
SELECT *
FROM parquet_file_metadata('test.parquet');
Field Type
file_name VARCHAR
created_by VARCHAR
num_rows BIGINT
num_row_groups BIGINT
format_version BIGINT
encryption_algorithm VARCHAR
footer_signing_key_metadata VARCHAR
The parquet_kv_metadata function can be used to query custom metadata defined as key‑value pairs:
SELECT *
FROM parquet_kv_metadata('test.parquet');
Field Type
file_name VARCHAR
key BLOB
value BLOB
Parquet Encryption
Starting with version 0.10.0, DuckDB supports reading and writing encrypted Parquet files. DuckDB broadly follows the Parquet Modular
Encryption specification with some limitations.
65
DuckDB Documentation
Using the PRAGMA add_parquet_key function, named encryption keys of 128, 192, or 256 bits can be added to a session. These keys
are stored in‑memory:
After specifying the key (e.g., key256), files can be encrypted as follows:
An encrypted Parquet file using a specific key (e.g., key256), can then be read as follows:
Or:
SELECT *
FROM read_parquet('tbl.parquet', encryption_config = {footer_key: 'key256'});
Limitations
1. It is not compatible with the encryption of, e.g., PyArrow, until the missing details are implemented.
2. DuckDB encrypts the footer and all columns using the footer_key. The Parquet specification allows encryption of individual
columns with different keys, e.g.:
However, this is unsupported at the moment and will cause an error to be thrown (for now):
Performance Implications
Note that encryption has some performance implications. Without encryption, reading/writing the lineitem table from TPC-H at SF1,
which is 6M rows and 15 columns, from/to a Parquet file takes 0.26 and 0.99 seconds, respectively. With encryption, this takes 0.64 and 2.21
seconds, both approximately 2.5× slower than the unencrypted version.
Parquet Tips
66
DuckDB Documentation
The union_by_name option can be used to unify the schema of files that have different or missing columns. For files that do not have
certain columns, NULL values are filled in:
SELECT *
FROM read_parquet('flights*.parquet', union_by_name = true);
Using a glob pattern upon read or a Hive partitioning structure are good ways to transparently handle multiple files.
Enabling PER_THREAD_OUTPUT
If the final number of Parquet files is not important, writing one file per thread can significantly improve performance:
COPY
(FROM generate_series(10_000_000))
TO 'test.parquet'
(FORMAT PARQUET, PER_THREAD_OUTPUT);
Selecting a ROW_GROUP_SIZE
The ROW_GROUP_SIZE parameter specifies the minimum number of rows in a Parquet row group, with a minimum value equal to
DuckDB's vector size, 2,048, and a default of 122,880. A Parquet row group is a partition of rows, consisting of a column chunk for each
column in the dataset.
Compression algorithms are only applied per row group, so the larger the row group size, the more opportunities to compress the data.
DuckDB can read Parquet row groups in parallel even within the same file and uses predicate pushdown to only scan the row groups whose
metadata ranges match the WHERE clause of the query. However there is some overhead associated with reading the metadata in each
group. A good approach would be to ensure that within each file, the total number of row groups is at least as large as the number of CPU
threads used to query that file. More row groups beyond the thread count would improve the speed of highly selective queries, but slow
down queries that must scan the whole file like aggregations.
To write a query to a Parquet file with a different row group size, run:
COPY
(FROM generate_series(100_000))
TO 'row-groups.parquet'
(FORMAT PARQUET, ROW_GROUP_SIZE 100_000);
The ROW_GROUPS_PER_FILE parameter creates a new Parquet file if the current one has a specified number of row groups.
COPY
(FROM generate_series(100_000))
TO 'output-directory'
(FORMAT PARQUET, ROW_GROUP_SIZE 20_000, ROW_GROUPS_PER_FILE 2);
67
DuckDB Documentation
If multiple threads are active, the number of row groups in a file may slightly exceed the specified number of row groups to limit the
amount of locking – similarly to the behaviour of FILE_SIZE_BYTES. However, if PER_THREAD_OUTPUT is set, only one thread
writes to each file, and it becomes accurate again.
68
Partitioning
Hive Partitioning
Examples
SELECT *
FROM read_parquet('orders/*/*/*.parquet', hive_partitioning = true);
COPY orders
TO 'orders' (FORMAT PARQUET, PARTITION_BY (year, month));
Note that the PARTITION_BY options cannot use expressions. You can produce columns on the fly using the following syntax:
When reading, the partition columns are read from the directory structure and can be included or excluded depending on the hive_
partitioning parameter.
Hive Partitioning
Hive partitioning is a partitioning strategy that is used to split a table into multiple files based on partition keys. The files are organized
into folders. Within each folder, the partition key has a value that is determined by the name of the folder.
Below is an example of a Hive partitioned file hierarchy. The files are partitioned on two keys (year and month).
orders
├── year=2021
│ ├── month=1
│ │ ├── file1.parquet
│ │ └── file2.parquet
│ └── month=2
│ └── file3.parquet
└── year=2022
├── month=11
│ ├── file4.parquet
│ └── file5.parquet
└── month=12
└── file6.parquet
Files stored in this hierarchy can be read using the hive_partitioning flag.
69
DuckDB Documentation
SELECT *
FROM read_parquet('orders/*/*/*.parquet', hive_partitioning = true);
When we specify the hive_partitioning flag, the values of the columns will be read from the directories.
Filter Pushdown
Filters on the partition keys are automatically pushed down into the files. This way the system skips reading files that are not necessary to
answer a query. For example, consider the following query on the above dataset:
SELECT *
FROM read_parquet('orders/*/*/*.parquet', hive_partitioning = true)
WHERE year = 2022
AND month = 11;
When executing this query, only the following files will be read:
orders
└── year=2022
└── month=11
├── file4.parquet
└── file5.parquet
Autodetection
By default the system tries to infer if the provided files are in a hive partitioned hierarchy. And if so, the hive_partitioning flag
is enabled automatically. The autodetection will look at the names of the folders and search for a 'key' = 'value' pattern. This
behavior can be overridden by using the hive_partitioning configuration option:
Hive Types
hive_types is a way to specify the logical types of the hive partitions in a struct:
SELECT *
FROM read_parquet(
'dir/**/*.parquet',
hive_partitioning = true,
hive_types = {'release': DATE, 'orders': BIGINT}
);
hive_types will be autodetected for the following types: DATE, TIMESTAMP and BIGINT. To switch off the autodetection, the flag
hive_types_autocast = 0 can be set.
Partitioned Writes
Examples
70
DuckDB Documentation
Write a table to a Hive partitioned data set of CSV files, allowing overwrites:
Write a table to a Hive partitioned data set of GZIP‑compressed CSV files, setting explicit data files' extension:
COPY orders TO 'orders' (FORMAT CSV, PARTITION_BY (year, month), COMPRESSION GZIP, FILE_EXTENSION
'csv.gz');
Partitioned Writes
When the PARTITION_BY clause is specified for the COPY statement, the files are written in a Hive partitioned folder hierarchy. The target
is the name of the root directory (in the example above: orders). The files are written in‑order in the file hierarchy. Currently, one file is
written per thread to each directory.
orders
├── year=2021
│ ├── month=1
│ │ ├── data_1.parquet
│ │ └── data_2.parquet
│ └── month=2
│ └── data_1.parquet
└── year=2022
├── month=11
│ ├── data_1.parquet
│ └── data_2.parquet
└── month=12
└── data_1.parquet
The values of the partitions are automatically extracted from the data. Note that it can be very expensive to write many partitions as many
files will be created. The ideal partition count depends on how large your data set is.
Best practice. Writing data into many small partitions is expensive. It is generally recommended to have at least 100 MB of data
per partition.
Overwriting
By default the partitioned write will not allow overwriting existing directories. Use the OVERWRITE_OR_IGNORE option to allow overwrit‑
ing an existing directory.
Filename Pattern
By default, files will be named data_0.parquet or data_0.csv. With the flag FILENAME_PATTERN a pattern with {i} or {uuid}
can be defined to create specific filenames:
Write a table to a Hive partitioned data set of .parquet files, with an index in the filename:
Write a table to a Hive partitioned data set of .parquet files, with unique filenames:
71
DuckDB Documentation
72
Appender
The Appender can be used to load bulk data into a DuckDB database. It is currently available in the C, C++, Go, Java, and Rust APIs. The
Appender is tied to a connection, and will use the transaction context of that connection when appending. An Appender always appends
to a single table in the database file.
DuckDB db;
Connection con(db);
// create the table
con.Query("CREATE TABLE people (id INTEGER, name VARCHAR)");
// initialize the appender
Appender appender(con, "people");
The AppendRow function is the easiest way of appending data. It uses recursive templates to allow you to put all the values of a single row
within one function call, as follows:
appender.AppendRow(1, "Mark");
Rows can also be individually constructed using the BeginRow, EndRow and Append methods. This is done internally by AppendRow,
and hence has the same performance characteristics.
appender.BeginRow();
appender.Append<int32_t>(2);
appender.Append<string>("Hannes");
appender.EndRow();
Any values added to the Appender are cached prior to being inserted into the database system for performance reasons. That means that,
while appending, the rows might not be immediately visible in the system. The cache is automatically flushed when the Appender goes
out of scope or when appender.Close() is called. The cache can also be manually flushed using the appender.Flush() method.
After either Flush or Close is called, all the data has been written to the database system.
While numbers and strings are rather self‑explanatory, dates, times and timestamps require some explanation. They can be directly ap‑
pended using the methods provided by duckdb::Date, duckdb::Time or duckdb::Timestamp. They can also be appended using
the internal duckdb::Value type, however, this adds some additional overheads and should be avoided if possible.
73
DuckDB Documentation
appender.AppendRow(
Value::DATE(1992, 1, 1),
Value::TIME(1, 1, 1, 0),
Value::TIMESTAMP(1992, 1, 1, 1, 1, 1, 0)
);
Commit Frequency
By default, the appender performs a commits every 204,800 rows. You can change this by explicitly using transactions and surrounding
your batches of AppendRow calls by BEGIN TRANSACTION and COMMIT statements.
If the Appender encounters a PRIMARY KEY conflict or a UNIQUE constraint violation, it fails and returns the following error:
Constraint Error: PRIMARY KEY or UNIQUE constraint violated: duplicate key "..."
In this case, the entire append operation fails and no rows are inserted.
• C
• Go
• Java (JDBC)
• Julia
• Rust
74
INSERT Statements
INSERT statements are the standard way of loading data into a relational database. When using INSERT statements, the values are
supplied row‑by‑row. While simple, there is significant overhead involved in parsing and processing individual INSERT statements. This
makes lots of individual row‑by‑row insertions very inefficient for bulk insertion.
Best practice. As a rule‑of‑thumb, avoid using lots of individual row‑by‑row INSERT statements when inserting more than a few
rows (i.e., avoid using INSERT statements as part of a loop). When bulk inserting data, try to maximize the amount of data that is
inserted per statement.
If you must use INSERT statements to load data in a loop, avoid executing the statements in auto‑commit mode. After every commit,
the database is required to sync the changes made to disk to ensure no data is lost. In auto‑commit mode every single statement will be
wrapped in a separate transaction, meaning fsync will be called for every statement. This is typically unnecessary when bulk loading and
will significantly slow down your program.
Tip. If you absolutely must use INSERT statements in a loop to load data, wrap them in calls to BEGIN TRANSACTION and COMMIT.
Syntax
For a more detailed description together with syntax diagram can be found, see the page on the INSERT statement.
75
DuckDB Documentation
76
DuckDB Documentation
Client APIs
77
Client APIs Overview
DuckDB is an in‑process database system and offers client APIs for several languages. These clients support the same DuckDB file format
and SQL syntax. Note: DuckDB database files are portable between different clients.
Support Tiers
Since there is such a wide variety of clients, the DuckDB team focuses their development effort on the most popular clients. To reflect
this, we distinguish three tiers of support for clients. Primary clients are the first to receive new features and are covered by community
support. Secondary clients receive new features but are not covered by community support. Finally, all tertiary clients are maintained by
third parties, so there are no feature or support guarantees for them.
79
DuckDB Documentation
The DuckDB clients listed above are open‑source and we welcome community contributions to these libraries. All primary and sec‑
ondary clients are available for the MIT license. For tertiary clients, please consult the repository for the license.
80
C
Overview
DuckDB implements a custom C API modelled somewhat following the SQLite C API. The API is contained in the duckdb.h header. Con‑
tinue to Startup & Shutdown to get started, or check out the Full API overview.
We also provide a SQLite API wrapper which means that if your applications is programmed against the SQLite C API, you can re‑link to
DuckDB and it should continue working. See the sqlite_api_wrapper folder in our source repository for more information.
Installation
The DuckDB C API can be installed as part of the libduckdb packages. Please see the installation page for details.
To use DuckDB, you must first initialize a duckdb_database handle using duckdb_open(). duckdb_open() takes as parameter the
database file to read and write from. The special value NULL (nullptr) can be used to create an in‑memory database. Note that for an
in‑memory database no data is persisted to disk (i.e., all data is lost when you exit the process).
With the duckdb_database handle, you can create one or many duckdb_connection using duckdb_connect(). While individual
connections are thread‑safe, they will be locked during querying. It is therefore recommended that each thread uses its own connection
to allow for the best parallel performance.
All duckdb_connections have to explicitly be disconnected with duckdb_disconnect() and the duckdb_database has to be
explicitly closed with duckdb_close() to avoid memory and file handle leaking.
Example
duckdb_database db;
duckdb_connection con;
// run queries...
// cleanup
duckdb_disconnect(&con);
duckdb_close(&db);
81
DuckDB Documentation
duckdb_open
Creates a new database or opens an existing database file stored at the given path. If no path is given a new in‑memory database is created
instead. The instantiated database should be closed with 'duckdb_close'.
Syntax
duckdb_state duckdb_open(
const char *path,
duckdb_database *out_database
);
Parameters
• path: Path to the database file on disk, or nullptr or :memory: to open an in‑memory database.
• out_database: The result database object.
duckdb_open_ext
Extended version of duckdb_open. Creates a new database or opens an existing database file stored at the given path. The instantiated
database should be closed with 'duckdb_close'.
Syntax
duckdb_state duckdb_open_ext(
const char *path,
duckdb_database *out_database,
duckdb_config config,
char **out_error
);
Parameters
• path: Path to the database file on disk, or nullptr or :memory: to open an in‑memory database.
• out_database: The result database object.
• config: (Optional) configuration used to start up the database system.
• out_error: If set and the function returns DuckDBError, this will contain the reason why the start‑up failed. Note that the error
must be freed using duckdb_free.
82
DuckDB Documentation
duckdb_close
Closes the specified database and de‑allocates all memory allocated for that database. This should be called after you are done with any
database allocated through duckdb_open or duckdb_open_ext. Note that failing to call duckdb_close (in case of e.g., a program
crash) will not cause data corruption. Still, it is recommended to always correctly close a database object after you are done with it.
Syntax
void duckdb_close(
duckdb_database *database
);
Parameters
duckdb_connect
Opens a connection to a database. Connections are required to query the database, and store transactional state associated with the
connection. The instantiated connection should be closed using 'duckdb_disconnect'.
Syntax
duckdb_state duckdb_connect(
duckdb_database database,
duckdb_connection *out_connection
);
Parameters
duckdb_interrupt
Syntax
void duckdb_interrupt(
duckdb_connection connection
);
Parameters
83
DuckDB Documentation
duckdb_query_progress
Syntax
duckdb_query_progress_type duckdb_query_progress(
duckdb_connection connection
);
Parameters
duckdb_disconnect
Closes the specified connection and de‑allocates all memory allocated for that connection.
Syntax
void duckdb_disconnect(
duckdb_connection *connection
);
Parameters
duckdb_library_version
Returns the version of the linked DuckDB, with a version postfix for dev versions
Usually used for developing C extensions that must return this for a compatibility check.
Syntax
);
Configuration
Configuration options can be provided to change different settings of the database system. Note that many of these settings can be changed
later on using PRAGMA statements as well. The configuration object should be created, filled with values and passed to duckdb_open_
ext.
84
DuckDB Documentation
Example
duckdb_database db;
duckdb_config config;
// run queries...
// cleanup
duckdb_close(&db);
duckdb_create_config
Initializes an empty configuration object that can be used to provide start‑up options for the DuckDB instance through duckdb_open_
ext. The duckdb_config must be destroyed using 'duckdb_destroy_config'
Note that duckdb_destroy_config should always be called on the resulting config, even if the function returns DuckDBError.
Syntax
duckdb_state duckdb_create_config(
duckdb_config *out_config
);
Parameters
85
DuckDB Documentation
duckdb_config_count
This returns the total amount of configuration options available for usage with duckdb_get_config_flag.
This should not be called in a loop as it internally loops over all the options.
Syntax
size_t duckdb_config_count(
);
duckdb_get_config_flag
Obtains a human‑readable name and description of a specific configuration option. This can be used to e.g. display configuration options.
This will succeed unless index is out of range (i.e., >= duckdb_config_count).
Syntax
duckdb_state duckdb_get_config_flag(
size_t index,
const char **out_name,
const char **out_description
);
Parameters
duckdb_set_config
Sets the specified option for the specified configuration. The configuration option is indicated by name. To obtain a list of config options,
see duckdb_get_config_flag.
This can fail if either the name is invalid, or if the value provided for the option is invalid.
Syntax
duckdb_state duckdb_set_config(
duckdb_config config,
const char *name,
const char *option
);
86
DuckDB Documentation
Parameters
duckdb_destroy_config
Destroys the specified configuration object and de‑allocates all memory allocated for the object.
Syntax
void duckdb_destroy_config(
duckdb_config *config
);
Parameters
Query
The duckdb_query method allows SQL queries to be run in DuckDB from C. This method takes two parameters, a (null‑terminated) SQL
query string and a duckdb_result result pointer. The result pointer may be NULL if the application is not interested in the result set
or if the query produces no result. After the result is consumed, the duckdb_destroy_result method should be used to clean up the
result.
Elements can be extracted from the duckdb_result object using a variety of methods. The duckdb_column_count can be used to
extract the number of columns. duckdb_column_name and duckdb_column_type can be used to extract the names and types of
individual columns.
Example
duckdb_state state;
duckdb_result result;
// create a table
state = duckdb_query(con, "CREATE TABLE integers (i INTEGER, j INTEGER);", NULL);
if (state == DuckDBError) {
// handle error
}
// insert three rows into the table
state = duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL);
if (state == DuckDBError) {
// handle error
}
// query rows again
state = duckdb_query(con, "SELECT * FROM integers", &result);
if (state == DuckDBError) {
// handle error
}
87
DuckDB Documentation
Value Extraction
Values can be extracted using either the duckdb_fetch_chunk function, or using the duckdb_value convenience functions. The
duckdb_fetch_chunk function directly hands you data chunks in DuckDB's native array format and can therefore be very fast. The
duckdb_value functions perform bounds‑ and type‑checking, and will automatically cast values to the desired type. This makes them
more convenient and easier to use, at the expense of being slower.
For optimal performance, use duckdb_fetch_chunk to extract data from the query result. The duckdb_value functions per‑
form internal type‑checking, bounds‑checking and casting which makes them slower.
duckdb_fetch_chunk
Below is an end‑to‑end example that prints the above result to CSV format using the duckdb_fetch_chunk function. Note that the
function is NOT generic: we do need to know exactly what the types of the result columns are.
duckdb_database db;
duckdb_connection con;
duckdb_open(nullptr, &db);
duckdb_connect(db, &con);
duckdb_result res;
duckdb_query(con, "CREATE TABLE integers (i INTEGER, j INTEGER);", NULL);
duckdb_query(con, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);", NULL);
duckdb_query(con, "SELECT * FROM integers;", &res);
88
DuckDB Documentation
} else {
printf("NULL");
}
printf(",");
if (duckdb_validity_row_is_valid(col2_validity, row)) {
printf("%d", col2_data[row]);
} else {
printf("NULL");
}
printf("\n");
}
duckdb_destroy_data_chunk(&result);
}
// clean-up
duckdb_destroy_result(&res);
duckdb_disconnect(&con);
duckdb_close(&db);
3,4
5,6
7,NULL
duckdb_value
Deprecated. The duckdb_value functions are deprecated and are scheduled for removal in a future release.
Below is an example that prints the above result to CSV format using the duckdb_value_varchar function. Note that the function is
generic: we do not need to know about the types of the individual result columns.
89
DuckDB Documentation
duckdb_query
Executes a SQL query within a connection and stores the full (materialized) result in the out_result pointer. If the query fails to execute,
DuckDBError is returned and the error message can be retrieved by calling duckdb_result_error.
Note that after running duckdb_query, duckdb_destroy_result must be called on the result object even if the query fails, other‑
wise the error stored within the result will not be freed correctly.
Syntax
duckdb_state duckdb_query(
duckdb_connection connection,
const char *query,
duckdb_result *out_result
);
Parameters
duckdb_destroy_result
Closes the result and de‑allocates all memory allocated for that connection.
Syntax
void duckdb_destroy_result(
duckdb_result *result
);
Parameters
duckdb_column_name
Returns the column name of the specified column. The result should not need to be freed; the column names will automatically be de‑
stroyed when the result is destroyed.
Syntax
const char *duckdb_column_name(
duckdb_result *result,
idx_t col
);
90
DuckDB Documentation
Parameters
duckdb_column_type
Syntax
duckdb_type duckdb_column_type(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_result_statement_type
Syntax
duckdb_statement_type duckdb_result_statement_type(
duckdb_result result
);
Parameters
duckdb_column_logical_type
91
DuckDB Documentation
Syntax
duckdb_logical_type duckdb_column_logical_type(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_column_count
Syntax
idx_t duckdb_column_count(
duckdb_result *result
);
Parameters
duckdb_row_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_row_count(
duckdb_result *result
);
Parameters
duckdb_rows_changed
Returns the number of rows changed by the query stored in the result. This is relevant only for INSERT/UPDATE/DELETE queries. For other
queries the rows_changed will be 0.
92
DuckDB Documentation
Syntax
idx_t duckdb_rows_changed(
duckdb_result *result
);
Parameters
duckdb_column_data
Deprecated. This method has been deprecated. Prefer using duckdb_result_get_chunk instead.
The function returns a dense array which contains the result data. The exact type stored in the array depends on the corresponding duckdb_
type (as provided by duckdb_column_type). For the exact type by which the data should be accessed, see the comments in the types
section or the DUCKDB_TYPE enum.
For example, for a column of type DUCKDB_TYPE_INTEGER, rows can be accessed in the following manner:
Syntax
void *duckdb_column_data(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_nullmask_data
Deprecated. This method has been deprecated. Prefer using duckdb_result_get_chunk instead.
Returns the nullmask of a specific column of a result in columnar format. The nullmask indicates for every row whether or not the corre‑
sponding row is NULL. If a row is NULL, the values present in the array provided by duckdb_column_data are undefined.
93
DuckDB Documentation
Syntax
bool *duckdb_nullmask_data(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_result_error
Returns the error message contained within the result. The error is only set if duckdb_query returns DuckDBError.
The result of this function must not be freed. It will be cleaned up when duckdb_destroy_result is called.
Syntax
Parameters
duckdb_result_error_type
Returns the result error type contained within the result. The error is only set if duckdb_query returns DuckDBError.
Syntax
duckdb_error_type duckdb_result_error_type(
duckdb_result *result
);
Parameters
94
DuckDB Documentation
Data Chunks
Data chunks represent a horizontal slice of a table. They hold a number of vectors, that can each hold up to the VECTOR_SIZE rows. The
vector size can be obtained through the duckdb_vector_size function and is configurable, but is usually set to 2048.
Data chunks and vectors are what DuckDB uses natively to store and represent data. For this reason, the data chunk interface is the most
efficient way of interfacing with DuckDB. Be aware, however, that correctly interfacing with DuckDB using the data chunk API does require
knowledge of DuckDB's internal vector format.
• Reading Data: Data chunks can be obtained from query results using the duckdb_fetch_chunk method, or as input to a user‑
defined function. In this case, the vector methods can be used to read individual values.
• Writing Data: Data chunks can be created using duckdb_create_data_chunk. The data chunk can then be filled with values
and used in duckdb_append_data_chunk to write data to the database.
The primary manner of interfacing with data chunks is by obtaining the internal vectors of the data chunk using the duckdb_data_
chunk_get_vector method. Afterwards, the vector methods can be used to read from or write to the individual vectors.
duckdb_create_data_chunk
Creates an empty data chunk with the specified column types. The result must be destroyed with duckdb_destroy_data_chunk.
Syntax
duckdb_data_chunk duckdb_create_data_chunk(
duckdb_logical_type *types,
idx_t column_count
);
Parameters
• types: An array of column types. Column types can not contain ANY and INVALID types.
• column_count: The number of columns.
duckdb_destroy_data_chunk
Destroys the data chunk and de‑allocates all memory allocated for that chunk.
95
DuckDB Documentation
Syntax
void duckdb_destroy_data_chunk(
duckdb_data_chunk *chunk
);
Parameters
duckdb_data_chunk_reset
Resets a data chunk, clearing the validity masks and setting the cardinality of the data chunk to 0. After calling this method, you must call
duckdb_vector_get_validity and duckdb_vector_get_data to obtain current data and validity pointers
Syntax
void duckdb_data_chunk_reset(
duckdb_data_chunk chunk
);
Parameters
duckdb_data_chunk_get_column_count
Syntax
idx_t duckdb_data_chunk_get_column_count(
duckdb_data_chunk chunk
);
Parameters
duckdb_data_chunk_get_vector
Retrieves the vector at the specified column index in the data chunk.
The pointer to the vector is valid for as long as the chunk is alive. It does NOT need to be destroyed.
Syntax
duckdb_vector duckdb_data_chunk_get_vector(
duckdb_data_chunk chunk,
idx_t col_idx
);
96
DuckDB Documentation
Parameters
duckdb_data_chunk_get_size
Syntax
idx_t duckdb_data_chunk_get_size(
duckdb_data_chunk chunk
);
Parameters
duckdb_data_chunk_set_size
Syntax
void duckdb_data_chunk_set_size(
duckdb_data_chunk chunk,
idx_t size
);
Parameters
Vectors
Vectors represent a horizontal slice of a column. They hold a number of values of a specific type, similar to an array. Vectors are the core
data representation used in DuckDB. Vectors are typically stored within data chunks.
The vector and data chunk interfaces are the most efficient way of interacting with DuckDB, allowing for the highest performance. However,
the interfaces are also difficult to use and care must be taken when using them.
Vector Format
Vectors are arrays of a specific data type. The logical type of a vector can be obtained using duckdb_vector_get_column_type. The
type id of the logical type can then be obtained using duckdb_get_type_id.
Vectors themselves do not have sizes. Instead, the parent data chunk has a size (that can be obtained through duckdb_data_chunk_
get_size). All vectors that belong to a data chunk have the same size.
97
DuckDB Documentation
Primitive Types
For primitive types, the underlying array can be obtained using the duckdb_vector_get_data method. The array can then be accessed
using the correct native type. Below is a table that contains a mapping of the duckdb_type to the native type of the array.
duckdb_type NativeType
DUCKDB_TYPE_BOOLEAN bool
DUCKDB_TYPE_TINYINT int8_t
DUCKDB_TYPE_SMALLINT int16_t
DUCKDB_TYPE_INTEGER int32_t
DUCKDB_TYPE_BIGINT int64_t
DUCKDB_TYPE_UTINYINT uint8_t
DUCKDB_TYPE_USMALLINT uint16_t
DUCKDB_TYPE_UINTEGER uint32_t
DUCKDB_TYPE_UBIGINT uint64_t
DUCKDB_TYPE_FLOAT float
DUCKDB_TYPE_DOUBLE double
DUCKDB_TYPE_TIMESTAMP duckdb_timestamp
DUCKDB_TYPE_DATE duckdb_date
DUCKDB_TYPE_TIME duckdb_time
DUCKDB_TYPE_INTERVAL duckdb_interval
DUCKDB_TYPE_HUGEINT duckdb_hugeint
DUCKDB_TYPE_UHUGEINT duckdb_uhugeint
DUCKDB_TYPE_VARCHAR duckdb_string_t
DUCKDB_TYPE_BLOB duckdb_string_t
DUCKDB_TYPE_TIMESTAMP_S duckdb_timestamp
DUCKDB_TYPE_TIMESTAMP_MS duckdb_timestamp
DUCKDB_TYPE_TIMESTAMP_NS duckdb_timestamp
DUCKDB_TYPE_UUID duckdb_hugeint
DUCKDB_TYPE_TIME_TZ duckdb_time_tz
DUCKDB_TYPE_TIMESTAMP_TZ duckdb_timestamp
NULL Values
Any value in a vector can be NULL. When a value is NULL, the values contained within the primary array at that index is undefined (and
can be uninitialized). The validity mask is a bitmask consisting of uint64_t elements. For every 64 values in the vector, one uint64_t
element exists (rounded up). The validity mask has its bit set to 1 if the value is valid, or set to 0 if the value is invalid (i.e .NULL).
The bits of the bitmask can be read directly, or the slower helper method duckdb_validity_row_is_valid can be used to check
whether or not a value is NULL.
The duckdb_vector_get_validity returns a pointer to the validity mask. Note that if all values in a vector are valid, this function
might return nullptr in which case the validity mask does not need to be checked.
98
DuckDB Documentation
Strings
String values are stored as a duckdb_string_t. This is a special struct that stores the string inline (if it is short, i.e., <= 12 bytes) or
a pointer to the string data if it is longer than 12 bytes.
typedef struct {
union {
struct {
uint32_t length;
char prefix[4];
char *ptr;
} pointer;
struct {
uint32_t length;
char inlined[12];
} inlined;
} value;
} duckdb_string_t;
The length can either be accessed directly, or the duckdb_string_is_inlined can be used to check if a string is inlined.
Decimals
Decimals are stored as integer values internally. The exact native type depends on the width of the decimal type, as shown in the following
table:
Width NativeType
<= 4 int16_t
<= 9 int32_t
<= 18 int64_t
<= 38 duckdb_hugeint
The duckdb_decimal_internal_type can be used to obtain the internal type of the decimal.
Decimals are stored as integer values multiplied by 10^scale. The scale of a decimal can be obtained using duckdb_decimal_scale.
For example, a decimal value of 10.5 with type DECIMAL(8, 3) is stored internally as an int32_t value of 10500. In order to obtain
the correct decimal value, the value should be divided by the appropriate power‑of‑ten.
Enums
Enums are stored as unsigned integer values internally. The exact native type depends on the size of the enum dictionary, as shown in the
following table:
The duckdb_enum_internal_type can be used to obtain the internal type of the enum.
99
DuckDB Documentation
In order to obtain the actual string value of the enum, the duckdb_enum_dictionary_value function must be used to obtain the
enum value that corresponds to the given dictionary entry. Note that the enum dictionary is the same for the entire column ‑ and so only
needs to be constructed once.
Structs
Structs are nested types that contain any number of child types. Think of them like a struct in C. The way to access struct data using
vectors is to access the child vectors recursively using the duckdb_struct_vector_get_child method.
The struct vector itself does not have any data (i.e., you should not use duckdb_vector_get_data method on the struct). However,
the struct vector itself does have a validity mask. The reason for this is that the child elements of a struct can be NULL, but the struct itself
can also be NULL.
Lists
Lists are nested types that contain a single child type, repeated x times per row. Think of them like a variable‑length array in C. The way to
access list data using vectors is to access the child vector using the duckdb_list_vector_get_child method.
The duckdb_vector_get_data must be used to get the offsets and lengths of the lists stored as duckdb_list_entry, that can
then be applied to the child vector.
typedef struct {
uint64_t offset;
uint64_t length;
} duckdb_list_entry;
Note that both list entries itself and any children stored in the lists can also be NULL. This must be checked using the validity mask again.
Arrays
Arrays are nested types that contain a single child type, repeated exactly array_size times per row. Think of them like a fixed‑size array
in C. Arrays work exactly the same as lists, except the length and offset of each entry is fixed. The fixed array size can be obtained by using
duckdb_array_type_array_size. The data for entry n then resides at offset = n * array_size, and always has length
= array_size.
Note that much like lists, arrays can still be NULL, which must be checked using the validity mask.
Examples
Below are several full end‑to‑end examples of how to interact with vectors.
duckdb_database db;
duckdb_connection con;
duckdb_open(nullptr, &db);
duckdb_connect(db, &con);
duckdb_result res;
duckdb_query(con, "SELECT CASE WHEN i%2=0 THEN NULL ELSE i END res_col FROM range(10) t(i)", &res);
100
DuckDB Documentation
duckdb_database db;
duckdb_connection con;
duckdb_open(nullptr, &db);
duckdb_connect(db, &con);
duckdb_result res;
duckdb_query(con, "SELECT CASE WHEN i%2=0 THEN CONCAT('short_', i) ELSE CONCAT('longstringprefix', i)
END FROM range(10) t(i)", &res);
101
DuckDB Documentation
duckdb_database db;
duckdb_connection con;
duckdb_open(nullptr, &db);
duckdb_connect(db, &con);
duckdb_result res;
duckdb_query(con, "SELECT CASE WHEN i%5=0 THEN NULL ELSE {'col1': i, 'col2': CASE WHEN i%2=0 THEN NULL
ELSE 100 + i * 42 END} END FROM range(10) t(i)", &res);
102
DuckDB Documentation
printf("{'col1': ");
if (!duckdb_validity_row_is_valid(col1_validity, row)) {
// col1 is NULL
printf("NULL");
} else {
printf("%lld", col1_data[row]);
}
printf(", 'col2': ");
if (!duckdb_validity_row_is_valid(col2_validity, row)) {
// col2 is NULL
printf("NULL");
} else {
printf("%lld", col2_data[row]);
}
printf("}\n");
}
duckdb_destroy_data_chunk(&result);
}
// clean-up
duckdb_destroy_result(&res);
duckdb_disconnect(&con);
duckdb_close(&db);
duckdb_database db;
duckdb_connection con;
duckdb_open(nullptr, &db);
duckdb_connect(db, &con);
duckdb_result res;
duckdb_query(con, "SELECT CASE WHEN i % 5 = 0 THEN NULL WHEN i % 2 = 0 THEN [i, i + 1] ELSE [i * 42,
NULL, i * 84] END FROM range(10) t(i)", &res);
103
DuckDB Documentation
continue;
}
// read the list offsets for this row
duckdb_list_entry list = list_data[row];
printf("[");
for (idx_t child_idx = list.offset; child_idx < list.offset + list.length; child_idx++) {
if (child_idx > list.offset) {
printf(", ");
}
if (!duckdb_validity_row_is_valid(child_validity, child_idx)) {
// col1 is NULL
printf("NULL");
} else {
printf("%lld", child_data[child_idx]);
}
}
printf("]\n");
}
duckdb_destroy_data_chunk(&result);
}
// clean-up
duckdb_destroy_result(&res);
duckdb_disconnect(&con);
duckdb_close(&db);
duckdb_vector_get_column_type
104
DuckDB Documentation
Syntax
duckdb_logical_type duckdb_vector_get_column_type(
duckdb_vector vector
);
Parameters
duckdb_vector_get_data
The data pointer can be used to read or write values from the vector. How to read or write values depends on the type of the vector.
Syntax
void *duckdb_vector_get_data(
duckdb_vector vector
);
Parameters
duckdb_vector_get_validity
The validity mask is a bitset that signifies null‑ness within the data chunk. It is a series of uint64_t values, where each uint64_t value contains
validity for 64 tuples. The bit is set to 1 if the value is valid (i.e., not NULL) or 0 if the value is invalid (i.e., NULL).
Syntax
uint64_t *duckdb_vector_get_validity(
duckdb_vector vector
);
Parameters
105
DuckDB Documentation
Return Value The pointer to the validity mask, or NULL if no validity mask is present
duckdb_vector_ensure_validity_writable
After this function is called, duckdb_vector_get_validity will ALWAYS return non‑NULL. This allows NULL values to be written to
the vector, regardless of whether a validity mask was present before.
Syntax
void duckdb_vector_ensure_validity_writable(
duckdb_vector vector
);
Parameters
duckdb_vector_assign_string_element
Syntax
void duckdb_vector_assign_string_element(
duckdb_vector vector,
idx_t index,
const char *str
);
Parameters
duckdb_vector_assign_string_element_len
Assigns a string element in the vector at the specified location. You may also use this function to assign BLOBs.
Syntax
void duckdb_vector_assign_string_element_len(
duckdb_vector vector,
idx_t index,
const char *str,
idx_t str_len
);
106
DuckDB Documentation
Parameters
duckdb_list_vector_get_child
Syntax
duckdb_vector duckdb_list_vector_get_child(
duckdb_vector vector
);
Parameters
duckdb_list_vector_get_size
Syntax
idx_t duckdb_list_vector_get_size(
duckdb_vector vector
);
Parameters
duckdb_list_vector_set_size
Syntax
duckdb_state duckdb_list_vector_set_size(
duckdb_vector vector,
idx_t size
);
107
DuckDB Documentation
Parameters
Return Value The duckdb state. Returns DuckDBError if the vector is nullptr.
duckdb_list_vector_reserve
After calling this method, you must call duckdb_vector_get_validity and duckdb_vector_get_data to obtain current data
and validity pointers
Syntax
duckdb_state duckdb_list_vector_reserve(
duckdb_vector vector,
idx_t required_capacity
);
Parameters
Return Value The duckdb state. Returns DuckDBError if the vector is nullptr.
duckdb_struct_vector_get_child
Syntax
duckdb_vector duckdb_struct_vector_get_child(
duckdb_vector vector,
idx_t index
);
Parameters
duckdb_array_vector_get_child
The resulting vector is valid as long as the parent vector is valid. The resulting vector has the size of the parent vector multiplied by the
array size.
108
DuckDB Documentation
Syntax
duckdb_vector duckdb_array_vector_get_child(
duckdb_vector vector
);
Parameters
duckdb_validity_row_is_valid
Returns whether or not a row is valid (i.e., not NULL) in the given validity mask.
Syntax
bool duckdb_validity_row_is_valid(
uint64_t *validity,
idx_t row
);
Parameters
duckdb_validity_set_row_validity
Syntax
void duckdb_validity_set_row_validity(
uint64_t *validity,
idx_t row,
bool valid
);
Parameters
109
DuckDB Documentation
duckdb_validity_set_row_invalid
Syntax
void duckdb_validity_set_row_invalid(
uint64_t *validity,
idx_t row
);
Parameters
duckdb_validity_set_row_valid
Syntax
void duckdb_validity_set_row_valid(
uint64_t *validity,
idx_t row
);
Parameters
Values
110
DuckDB Documentation
duckdb_destroy_value
Destroys the value and de‑allocates all memory allocated for that type.
Syntax
void duckdb_destroy_value(
duckdb_value *value
);
Parameters
111
DuckDB Documentation
duckdb_create_varchar
Syntax
duckdb_value duckdb_create_varchar(
const char *text
);
Parameters
duckdb_create_varchar_length
Syntax
duckdb_value duckdb_create_varchar_length(
const char *text,
idx_t length
);
Parameters
duckdb_create_bool
Syntax
duckdb_value duckdb_create_bool(
bool input
);
Parameters
112
DuckDB Documentation
duckdb_create_int8
Syntax
duckdb_value duckdb_create_int8(
int8_t input
);
Parameters
duckdb_create_uint8
Syntax
duckdb_value duckdb_create_uint8(
uint8_t input
);
Parameters
duckdb_create_int16
Syntax
duckdb_value duckdb_create_int16(
int16_t input
);
Parameters
duckdb_create_uint16
113
DuckDB Documentation
Syntax
duckdb_value duckdb_create_uint16(
uint16_t input
);
Parameters
duckdb_create_int32
Syntax
duckdb_value duckdb_create_int32(
int32_t input
);
Parameters
duckdb_create_uint32
Syntax
duckdb_value duckdb_create_uint32(
uint32_t input
);
Parameters
duckdb_create_uint64
Syntax
duckdb_value duckdb_create_uint64(
uint64_t input
);
114
DuckDB Documentation
Parameters
duckdb_create_int64
Syntax
duckdb_value duckdb_create_int64(
int64_t val
);
duckdb_create_hugeint
Syntax
duckdb_value duckdb_create_hugeint(
duckdb_hugeint input
);
Parameters
duckdb_create_uhugeint
Syntax
duckdb_value duckdb_create_uhugeint(
duckdb_uhugeint input
);
Parameters
115
DuckDB Documentation
duckdb_create_float
Syntax
duckdb_value duckdb_create_float(
float input
);
Parameters
duckdb_create_double
Syntax
duckdb_value duckdb_create_double(
double input
);
Parameters
duckdb_create_date
Syntax
duckdb_value duckdb_create_date(
duckdb_date input
);
Parameters
duckdb_create_time
116
DuckDB Documentation
Syntax
duckdb_value duckdb_create_time(
duckdb_time input
);
Parameters
duckdb_create_time_tz_value
Creates a value from a time_tz. Not to be confused with duckdb_create_time_tz, which creates a duckdb_time_tz_t.
Syntax
duckdb_value duckdb_create_time_tz_value(
duckdb_time_tz value
);
Parameters
duckdb_create_timestamp
Syntax
duckdb_value duckdb_create_timestamp(
duckdb_timestamp input
);
Parameters
duckdb_create_interval
Syntax
duckdb_value duckdb_create_interval(
duckdb_interval input
);
117
DuckDB Documentation
Parameters
duckdb_create_blob
Syntax
duckdb_value duckdb_create_blob(
const uint8_t *data,
idx_t length
);
Parameters
duckdb_get_bool
Syntax
bool duckdb_get_bool(
duckdb_value val
);
Parameters
duckdb_get_int8
Syntax
int8_t duckdb_get_int8(
duckdb_value val
);
118
DuckDB Documentation
Parameters
duckdb_get_uint8
Syntax
uint8_t duckdb_get_uint8(
duckdb_value val
);
Parameters
duckdb_get_int16
Syntax
int16_t duckdb_get_int16(
duckdb_value val
);
Parameters
duckdb_get_uint16
Syntax
uint16_t duckdb_get_uint16(
duckdb_value val
);
Parameters
119
DuckDB Documentation
duckdb_get_int32
Syntax
int32_t duckdb_get_int32(
duckdb_value val
);
Parameters
duckdb_get_uint32
Syntax
uint32_t duckdb_get_uint32(
duckdb_value val
);
Parameters
duckdb_get_int64
Syntax
int64_t duckdb_get_int64(
duckdb_value val
);
Parameters
120
DuckDB Documentation
duckdb_get_uint64
Syntax
uint64_t duckdb_get_uint64(
duckdb_value val
);
Parameters
duckdb_get_hugeint
Syntax
duckdb_hugeint duckdb_get_hugeint(
duckdb_value val
);
Parameters
duckdb_get_uhugeint
Syntax
duckdb_uhugeint duckdb_get_uhugeint(
duckdb_value val
);
Parameters
duckdb_get_float
121
DuckDB Documentation
Syntax
float duckdb_get_float(
duckdb_value val
);
Parameters
duckdb_get_double
Syntax
double duckdb_get_double(
duckdb_value val
);
Parameters
duckdb_get_date
Syntax
duckdb_date duckdb_get_date(
duckdb_value val
);
Parameters
duckdb_get_time
Syntax
duckdb_time duckdb_get_time(
duckdb_value val
);
122
DuckDB Documentation
Parameters
duckdb_get_time_tz
Syntax
duckdb_time_tz duckdb_get_time_tz(
duckdb_value val
);
Parameters
duckdb_get_timestamp
Syntax
duckdb_timestamp duckdb_get_timestamp(
duckdb_value val
);
Parameters
duckdb_get_interval
Syntax
duckdb_interval duckdb_get_interval(
duckdb_value val
);
Parameters
123
DuckDB Documentation
duckdb_get_value_type
Returns the type of the given value. The type is valid as long as the value is not destroyed. The type itself must not be destroyed.
Syntax
duckdb_logical_type duckdb_get_value_type(
duckdb_value val
);
Parameters
• val: A duckdb_value
duckdb_get_blob
Syntax
duckdb_blob duckdb_get_blob(
duckdb_value val
);
Parameters
duckdb_get_varchar
Obtains a string representation of the given value. The result must be destroyed with duckdb_free.
Syntax
char *duckdb_get_varchar(
duckdb_value value
);
Parameters
Return Value The string value. This must be destroyed with duckdb_free.
124
DuckDB Documentation
duckdb_create_struct_value
Creates a struct value from a type and an array of values. Must be destroyed with duckdb_destroy_value.
Syntax
duckdb_value duckdb_create_struct_value(
duckdb_logical_type type,
duckdb_value *values
);
Parameters
Return Value The struct value, or nullptr, if any child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_create_list_value
Creates a list value from a child (element) type and an array of values of length value_count. Must be destroyed with duckdb_
destroy_value.
Syntax
duckdb_value duckdb_create_list_value(
duckdb_logical_type type,
duckdb_value *values,
idx_t value_count
);
Parameters
Return Value The list value, or nullptr, if the child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_create_array_value
Creates an array value from a child (element) type and an array of values of length value_count. Must be destroyed with duckdb_
destroy_value.
Syntax
duckdb_value duckdb_create_array_value(
duckdb_logical_type type,
duckdb_value *values,
idx_t value_count
);
125
DuckDB Documentation
Parameters
Return Value The array value, or nullptr, if the child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_get_map_size
Syntax
idx_t duckdb_get_map_size(
duckdb_value value
);
Parameters
duckdb_get_map_key
Syntax
duckdb_value duckdb_get_map_key(
duckdb_value value,
idx_t index
);
Parameters
duckdb_get_map_value
Syntax
duckdb_value duckdb_get_map_value(
duckdb_value value,
idx_t index
);
126
DuckDB Documentation
Parameters
Types
DuckDB is a strongly typed database system. As such, every column has a single type specified. This type is constant over the entire column.
That is to say, a column that is labeled as an INTEGER column will only contain INTEGER values.
DuckDB also supports columns of composite types. For example, it is possible to define an array of integers (INTEGER[]). It is also possible
to define types as arbitrary structs (ROW(i INTEGER, j VARCHAR)). For that reason, native DuckDB type objects are not mere enums,
but a class that can potentially be nested.
Types in the C API are modeled using an enum (duckdb_type) and a complex class (duckdb_logical_type). For most primitive
types, e.g., integers or varchars, the enum is sufficient. For more complex types, such as lists, structs or decimals, the logical type must be
used.
127
DuckDB Documentation
Functions
The enum type of a column in the result can be obtained using the duckdb_column_type function. The logical type of a column can be
obtained using the duckdb_column_logical_type function.
duckdb_value
The duckdb_value functions will auto‑cast values as required. For example, it is no problem to use duckdb_value_double on a
column of type duckdb_value_int32. The value will be auto‑cast and returned as a double. Note that in certain cases the cast may
fail. For example, this can happen if we request a duckdb_value_int8 and the value does not fit within an int8 value. In this case, a
default value will be returned (usually 0 or nullptr). The same default value will also be returned if the corresponding value is NULL.
The duckdb_value_is_null function can be used to check if a specific value is NULL or not.
The exception to the auto‑cast rule is the duckdb_value_varchar_internal function. This function does not auto‑cast and only
works for VARCHAR columns. The reason this function exists is that the result does not need to be freed.
duckdb_fetch_chunk
The duckdb_fetch_chunk function can be used to read data chunks from a DuckDB result set, and is the most efficient way of reading
data from a DuckDB result using the C API. It is also the only way of reading data of certain types from a DuckDB result. For example, the
duckdb_value functions do not support structural reading of composite types (lists or structs) or more complex types like enums and
decimals.
For more information about data chunks, see the documentation on data chunks.
Hugeint Helpers
128
DuckDB Documentation
Decimal Helpers
duckdb_result_get_chunk
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetches a data chunk from the duckdb_result. This function should be called repeatedly until the result is exhausted.
This function supersedes all duckdb_value functions, as well as the duckdb_column_data and duckdb_nullmask_data func‑
tions. It results in significantly better performance, and should be preferred in newer code‑bases.
If this function is used, none of the other result functions can be used and vice versa (i.e., this function cannot be mixed with the legacy
result functions).
Use duckdb_result_chunk_count to figure out how many chunks there are in the result.
129
DuckDB Documentation
Syntax
duckdb_data_chunk duckdb_result_get_chunk(
duckdb_result result,
idx_t chunk_index
);
Parameters
Return Value The resulting data chunk. Returns NULL if the chunk index is out of bounds.
duckdb_result_is_streaming
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
bool duckdb_result_is_streaming(
duckdb_result result
);
Parameters
Return Value Whether or not the result object is of the type StreamQueryResult
duckdb_result_chunk_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_result_chunk_count(
duckdb_result result
);
Parameters
duckdb_result_return_type
130
DuckDB Documentation
Syntax
duckdb_result_type duckdb_result_return_type(
duckdb_result result
);
Parameters
duckdb_from_date
Decompose a duckdb_date object into year, month and date (stored as duckdb_date_struct).
Syntax
duckdb_date_struct duckdb_from_date(
duckdb_date date
);
Parameters
duckdb_to_date
Syntax
duckdb_date duckdb_to_date(
duckdb_date_struct date
);
Parameters
duckdb_is_finite_date
Syntax
bool duckdb_is_finite_date(
duckdb_date date
);
131
DuckDB Documentation
Parameters
duckdb_from_time
Decompose a duckdb_time object into hour, minute, second and microsecond (stored as duckdb_time_struct).
Syntax
duckdb_time_struct duckdb_from_time(
duckdb_time time
);
Parameters
duckdb_create_time_tz
Syntax
duckdb_time_tz duckdb_create_time_tz(
int64_t micros,
int32_t offset
);
Parameters
duckdb_from_time_tz
Use duckdb_from_time to further decompose the micros into hour, minute, second and microsecond.
Syntax
duckdb_time_tz_struct duckdb_from_time_tz(
duckdb_time_tz micros
);
132
DuckDB Documentation
Parameters
duckdb_to_time
Syntax
duckdb_time duckdb_to_time(
duckdb_time_struct time
);
Parameters
duckdb_from_timestamp
Syntax
duckdb_timestamp_struct duckdb_from_timestamp(
duckdb_timestamp ts
);
Parameters
duckdb_to_timestamp
Syntax
duckdb_timestamp duckdb_to_timestamp(
duckdb_timestamp_struct ts
);
Parameters
133
DuckDB Documentation
duckdb_is_finite_timestamp
Syntax
bool duckdb_is_finite_timestamp(
duckdb_timestamp ts
);
Parameters
duckdb_hugeint_to_double
Converts a duckdb_hugeint object (as obtained from a DUCKDB_TYPE_HUGEINT column) into a double.
Syntax
double duckdb_hugeint_to_double(
duckdb_hugeint val
);
Parameters
duckdb_double_to_hugeint
If the conversion fails because the double value is too big the result will be 0.
Syntax
duckdb_hugeint duckdb_double_to_hugeint(
double val
);
Parameters
134
DuckDB Documentation
duckdb_double_to_decimal
If the conversion fails because the double value is too big, or the width/scale are invalid the result will be 0.
Syntax
duckdb_decimal duckdb_double_to_decimal(
double val,
uint8_t width,
uint8_t scale
);
Parameters
duckdb_decimal_to_double
Converts a duckdb_decimal object (as obtained from a DUCKDB_TYPE_DECIMAL column) into a double.
Syntax
double duckdb_decimal_to_double(
duckdb_decimal val
);
Parameters
duckdb_create_logical_type
Creates a duckdb_logical_type from a primitive type. The resulting logical type must be destroyed with duckdb_destroy_
logical_type.
Returns an invalid logical type, if type is: DUCKDB_TYPE_INVALID, DUCKDB_TYPE_DECIMAL, DUCKDB_TYPE_ENUM, DUCKDB_
TYPE_LIST, DUCKDB_TYPE_STRUCT, DUCKDB_TYPE_MAP, DUCKDB_TYPE_ARRAY, or DUCKDB_TYPE_UNION.
Syntax
duckdb_logical_type duckdb_create_logical_type(
duckdb_type type
);
Parameters
135
DuckDB Documentation
duckdb_logical_type_get_alias
Returns the alias of a duckdb_logical_type, if set, else nullptr. The result must be destroyed with duckdb_free.
Syntax
char *duckdb_logical_type_get_alias(
duckdb_logical_type type
);
Parameters
duckdb_logical_type_set_alias
Syntax
void duckdb_logical_type_set_alias(
duckdb_logical_type type,
const char *alias
);
Parameters
duckdb_create_list_type
Creates a LIST type from its child type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_list_type(
duckdb_logical_type type
);
Parameters
136
DuckDB Documentation
duckdb_create_array_type
Creates an ARRAY type from its child type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_array_type(
duckdb_logical_type type,
idx_t array_size
);
Parameters
duckdb_create_map_type
Creates a MAP type from its key type and value type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_map_type(
duckdb_logical_type key_type,
duckdb_logical_type value_type
);
Parameters
duckdb_create_union_type
Creates a UNION type from the passed arrays. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_union_type(
duckdb_logical_type *member_types,
const char **member_names,
idx_t member_count
);
Parameters
137
DuckDB Documentation
duckdb_create_struct_type
Creates a STRUCT type based on the member types and names. The resulting type must be destroyed with duckdb_destroy_
logical_type.
Syntax
duckdb_logical_type duckdb_create_struct_type(
duckdb_logical_type *member_types,
const char **member_names,
idx_t member_count
);
Parameters
duckdb_create_enum_type
Creates an ENUM type from the passed member name array. The resulting type should be destroyed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_create_enum_type(
const char **member_names,
idx_t member_count
);
Parameters
• member_names: The array of names that the enum should consist of.
• member_count: The number of elements that were specified in the array.
duckdb_create_decimal_type
Creates a DECIMAL type with the specified width and scale. The resulting type should be destroyed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_create_decimal_type(
uint8_t width,
uint8_t scale
);
138
DuckDB Documentation
Parameters
duckdb_get_type_id
Syntax
duckdb_type duckdb_get_type_id(
duckdb_logical_type type
);
Parameters
duckdb_decimal_width
Syntax
uint8_t duckdb_decimal_width(
duckdb_logical_type type
);
Parameters
duckdb_decimal_scale
Syntax
uint8_t duckdb_decimal_scale(
duckdb_logical_type type
);
Parameters
139
DuckDB Documentation
duckdb_decimal_internal_type
Syntax
duckdb_type duckdb_decimal_internal_type(
duckdb_logical_type type
);
Parameters
duckdb_enum_internal_type
Syntax
duckdb_type duckdb_enum_internal_type(
duckdb_logical_type type
);
Parameters
duckdb_enum_dictionary_size
Syntax
uint32_t duckdb_enum_dictionary_size(
duckdb_logical_type type
);
Parameters
140
DuckDB Documentation
duckdb_enum_dictionary_value
Retrieves the dictionary value at the specified position from the enum.
Syntax
char *duckdb_enum_dictionary_value(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The string value of the enum type. Must be freed with duckdb_free.
duckdb_list_type_child_type
Retrieves the child type of the given LIST type. Also accepts MAP types. The result must be freed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_list_type_child_type(
duckdb_logical_type type
);
Parameters
duckdb_array_type_child_type
Syntax
duckdb_logical_type duckdb_array_type_child_type(
duckdb_logical_type type
);
Parameters
141
DuckDB Documentation
duckdb_array_type_array_size
Syntax
idx_t duckdb_array_type_array_size(
duckdb_logical_type type
);
Parameters
Return Value The fixed number of elements the values of this array type can store.
duckdb_map_type_key_type
Syntax
duckdb_logical_type duckdb_map_type_key_type(
duckdb_logical_type type
);
Parameters
Return Value The key type of the map type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_map_type_value_type
Syntax
duckdb_logical_type duckdb_map_type_value_type(
duckdb_logical_type type
);
Parameters
142
DuckDB Documentation
Return Value The value type of the map type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_struct_type_child_count
Syntax
idx_t duckdb_struct_type_child_count(
duckdb_logical_type type
);
Parameters
duckdb_struct_type_child_name
Syntax
char *duckdb_struct_type_child_name(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The name of the struct type. Must be freed with duckdb_free.
duckdb_struct_type_child_type
Retrieves the child type of the given struct type at the specified index.
Syntax
duckdb_logical_type duckdb_struct_type_child_type(
duckdb_logical_type type,
idx_t index
);
143
DuckDB Documentation
Parameters
Return Value The child type of the struct type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_union_type_member_count
Syntax
idx_t duckdb_union_type_member_count(
duckdb_logical_type type
);
Parameters
duckdb_union_type_member_name
Syntax
char *duckdb_union_type_member_name(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The name of the union member. Must be freed with duckdb_free.
duckdb_union_type_member_type
Retrieves the child type of the given union member at the specified index.
144
DuckDB Documentation
Syntax
duckdb_logical_type duckdb_union_type_member_type(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The child type of the union member. Must be destroyed with duckdb_destroy_logical_type.
duckdb_destroy_logical_type
Destroys the logical type and de‑allocates all memory allocated for that type.
Syntax
void duckdb_destroy_logical_type(
duckdb_logical_type *type
);
Parameters
duckdb_register_logical_type
Registers a custom type within the given connection. The type must have an alias
Syntax
duckdb_state duckdb_register_logical_type(
duckdb_connection con,
duckdb_logical_type type,
duckdb_create_type_info info
);
Parameters
145
DuckDB Documentation
Prepared Statements
A prepared statement is a parameterized query. The query is prepared with question marks (?) or dollar symbols ($1) indicating the
parameters of the query. Values can then be bound to these parameters, after which the prepared statement can be executed using those
parameters. A single query can be prepared once and executed many times.
• Easily supply parameters to functions while avoiding string concatenation/SQL injection attacks.
• Speeding up queries that will be executed many times with different parameters.
DuckDB supports prepared statements in the C API with the duckdb_prepare method. The duckdb_bind family of functions is used
to supply values for subsequent execution of the prepared statement using duckdb_execute_prepared. After we are done with the
prepared statement it can be cleaned up using the duckdb_destroy_prepare method.
Example
duckdb_prepared_statement stmt;
duckdb_result result;
if (duckdb_prepare(con, "INSERT INTO integers VALUES ($1, $2)", &stmt) == DuckDBError) {
// handle error
}
// clean up
duckdb_destroy_result(&result);
duckdb_destroy_prepare(&stmt);
After calling duckdb_prepare, the prepared statement parameters can be inspected using duckdb_nparams and duckdb_param_
type. In case the prepare fails, the error can be obtained through duckdb_prepare_error.
It is not required that the duckdb_bind family of functions matches the prepared statement parameter type exactly. The values will be
auto‑cast to the required value as required. For example, calling duckdb_bind_int8 on a parameter type of DUCKDB_TYPE_INTEGER
will work as expected.
Warning. Do not use prepared statements to insert large amounts of data into DuckDB. Instead it is recommended to use the Appen‑
der.
146
DuckDB Documentation
duckdb_prepare
Note that after calling duckdb_prepare, the prepared statement should always be destroyed using duckdb_destroy_prepare,
even if the prepare fails.
If the prepare fails, duckdb_prepare_error can be called to obtain the reason why the prepare failed.
Syntax
duckdb_state duckdb_prepare(
duckdb_connection connection,
const char *query,
duckdb_prepared_statement *out_prepared_statement
);
Parameters
duckdb_destroy_prepare
Closes the prepared statement and de‑allocates all memory allocated for the statement.
Syntax
void duckdb_destroy_prepare(
duckdb_prepared_statement *prepared_statement
);
Parameters
duckdb_prepare_error
Returns the error message associated with the given prepared statement. If the prepared statement has no error message, this returns
nullptr instead.
The error message should not be freed. It will be de‑allocated when duckdb_destroy_prepare is called.
147
DuckDB Documentation
Syntax
Parameters
duckdb_nparams
Returns the number of parameters that can be provided to the given prepared statement.
Syntax
idx_t duckdb_nparams(
duckdb_prepared_statement prepared_statement
);
Parameters
duckdb_parameter_name
Returns the name used to identify the parameter The returned string should be freed using duckdb_free.
Returns NULL if the index is out of range for the provided prepared statement.
Syntax
Parameters
• prepared_statement: The prepared statement for which to get the parameter name from.
duckdb_param_type
Returns the parameter type for the parameter at the given index.
Returns DUCKDB_TYPE_INVALID if the parameter index is out of range or the statement was not successfully prepared.
148
DuckDB Documentation
Syntax
duckdb_type duckdb_param_type(
duckdb_prepared_statement prepared_statement,
idx_t param_idx
);
Parameters
duckdb_clear_bindings
Syntax
duckdb_state duckdb_clear_bindings(
duckdb_prepared_statement prepared_statement
);
duckdb_prepared_statement_type
Syntax
duckdb_statement_type duckdb_prepared_statement_type(
duckdb_prepared_statement statement
);
Parameters
Appender
Appenders are the most efficient way of loading data into DuckDB from within the C interface, and are recommended for fast data loading.
The appender is much faster than using prepared statements or individual INSERT INTO statements.
Appends are made in row‑wise format. For every column, a duckdb_append_[type] call should be made, after which the row should
be finished by calling duckdb_appender_end_row. After all rows have been appended, duckdb_appender_destroy should be
used to finalize the appender and clean up the resulting memory.
Note that duckdb_appender_destroy should always be called on the resulting appender, even if the function returns DuckDBEr-
ror.
149
DuckDB Documentation
Example
duckdb_appender appender;
if (duckdb_appender_create(con, NULL, "people", &appender) == DuckDBError) {
// handle error
}
// append the first row (1, Mark)
duckdb_append_int32(appender, 1);
duckdb_append_varchar(appender, "Mark");
duckdb_appender_end_row(appender);
150
DuckDB Documentation
duckdb_appender_create
Syntax
duckdb_state duckdb_appender_create(
duckdb_connection connection,
const char *schema,
const char *table,
duckdb_appender *out_appender
);
Parameters
duckdb_appender_column_count
Returns the number of columns in the table that belongs to the appender.
Syntax
idx_t duckdb_appender_column_count(
duckdb_appender appender
);
Parameters
duckdb_appender_column_type
Syntax
duckdb_logical_type duckdb_appender_column_type(
duckdb_appender appender,
idx_t col_idx
);
151
DuckDB Documentation
Parameters
duckdb_appender_error
Returns the error message associated with the given appender. If the appender has no error message, this returns nullptr instead.
The error message should not be freed. It will be de‑allocated when duckdb_appender_destroy is called.
Syntax
Parameters
duckdb_appender_flush
Flush the appender to the table, forcing the cache of the appender to be cleared. If flushing the data triggers a constraint violation or any
other error, then all data is invalidated, and this function returns DuckDBError. It is not possible to append more values. Call duckdb_
appender_error to obtain the error message followed by duckdb_appender_destroy to destroy the invalidated appender.
Syntax
duckdb_state duckdb_appender_flush(
duckdb_appender appender
);
Parameters
duckdb_appender_close
Closes the appender by flushing all intermediate states and closing it for further appends. If flushing the data triggers a constraint violation
or any other error, then all data is invalidated, and this function returns DuckDBError. Call duckdb_appender_error to obtain the error
message followed by duckdb_appender_destroy to destroy the invalidated appender.
152
DuckDB Documentation
Syntax
duckdb_state duckdb_appender_close(
duckdb_appender appender
);
Parameters
duckdb_appender_destroy
Closes the appender by flushing all intermediate states to the table and destroying it. By destroying it, this function de‑allocates all memory
associated with the appender. If flushing the data triggers a constraint violation, then all data is invalidated, and this function returns
DuckDBError. Due to the destruction of the appender, it is no longer possible to obtain the specific error message with duckdb_appender_
error. Therefore, call duckdb_appender_close before destroying the appender, if you need insights into the specific error.
Syntax
duckdb_state duckdb_appender_destroy(
duckdb_appender *appender
);
Parameters
duckdb_appender_begin_row
A nop function, provided for backwards compatibility reasons. Does nothing. Only duckdb_appender_end_row is required.
Syntax
duckdb_state duckdb_appender_begin_row(
duckdb_appender appender
);
duckdb_appender_end_row
Finish the current row of appends. After end_row is called, the next row can be appended.
Syntax
duckdb_state duckdb_appender_end_row(
duckdb_appender appender
);
153
DuckDB Documentation
Parameters
duckdb_append_default
Append a DEFAULT value (NULL if DEFAULT not available for column) to the appender.
Syntax
duckdb_state duckdb_append_default(
duckdb_appender appender
);
duckdb_append_bool
Syntax
duckdb_state duckdb_append_bool(
duckdb_appender appender,
bool value
);
duckdb_append_int8
Syntax
duckdb_state duckdb_append_int8(
duckdb_appender appender,
int8_t value
);
duckdb_append_int16
Syntax
duckdb_state duckdb_append_int16(
duckdb_appender appender,
int16_t value
);
duckdb_append_int32
154
DuckDB Documentation
Syntax
duckdb_state duckdb_append_int32(
duckdb_appender appender,
int32_t value
);
duckdb_append_int64
Syntax
duckdb_state duckdb_append_int64(
duckdb_appender appender,
int64_t value
);
duckdb_append_hugeint
Syntax
duckdb_state duckdb_append_hugeint(
duckdb_appender appender,
duckdb_hugeint value
);
duckdb_append_uint8
Syntax
duckdb_state duckdb_append_uint8(
duckdb_appender appender,
uint8_t value
);
duckdb_append_uint16
Syntax
duckdb_state duckdb_append_uint16(
duckdb_appender appender,
uint16_t value
);
duckdb_append_uint32
155
DuckDB Documentation
Syntax
duckdb_state duckdb_append_uint32(
duckdb_appender appender,
uint32_t value
);
duckdb_append_uint64
Syntax
duckdb_state duckdb_append_uint64(
duckdb_appender appender,
uint64_t value
);
duckdb_append_uhugeint
Syntax
duckdb_state duckdb_append_uhugeint(
duckdb_appender appender,
duckdb_uhugeint value
);
duckdb_append_float
Syntax
duckdb_state duckdb_append_float(
duckdb_appender appender,
float value
);
duckdb_append_double
Syntax
duckdb_state duckdb_append_double(
duckdb_appender appender,
double value
);
duckdb_append_date
156
DuckDB Documentation
Syntax
duckdb_state duckdb_append_date(
duckdb_appender appender,
duckdb_date value
);
duckdb_append_time
Syntax
duckdb_state duckdb_append_time(
duckdb_appender appender,
duckdb_time value
);
duckdb_append_timestamp
Syntax
duckdb_state duckdb_append_timestamp(
duckdb_appender appender,
duckdb_timestamp value
);
duckdb_append_interval
Syntax
duckdb_state duckdb_append_interval(
duckdb_appender appender,
duckdb_interval value
);
duckdb_append_varchar
Syntax
duckdb_state duckdb_append_varchar(
duckdb_appender appender,
const char *val
);
duckdb_append_varchar_length
157
DuckDB Documentation
Syntax
duckdb_state duckdb_append_varchar_length(
duckdb_appender appender,
const char *val,
idx_t length
);
duckdb_append_blob
Syntax
duckdb_state duckdb_append_blob(
duckdb_appender appender,
const void *data,
idx_t length
);
duckdb_append_null
Syntax
duckdb_state duckdb_append_null(
duckdb_appender appender
);
duckdb_append_data_chunk
The types of the data chunk must exactly match the types of the table, no casting is performed. If the types do not match or the appender
is in an invalid state, DuckDBError is returned. If the append is successful, DuckDBSuccess is returned.
Syntax
duckdb_state duckdb_append_data_chunk(
duckdb_appender appender,
duckdb_data_chunk chunk
);
Parameters
Table Functions
The table function API can be used to define a table function that can then be called from within DuckDB in the FROM clause of a query.
158
DuckDB Documentation
duckdb_table_function duckdb_create_table_function();
void duckdb_destroy_table_function(duckdb_table_function *table_function);
void duckdb_table_function_set_name(duckdb_table_function table_function, const char *name);
void duckdb_table_function_add_parameter(duckdb_table_function table_function, duckdb_logical_type
type);
void duckdb_table_function_add_named_parameter(duckdb_table_function table_function, const char *name,
duckdb_logical_type type);
void duckdb_table_function_set_extra_info(duckdb_table_function table_function, void *extra_info,
duckdb_delete_callback_t destroy);
void duckdb_table_function_set_bind(duckdb_table_function table_function, duckdb_table_function_bind_t
bind);
void duckdb_table_function_set_init(duckdb_table_function table_function, duckdb_table_function_init_t
init);
void duckdb_table_function_set_local_init(duckdb_table_function table_function, duckdb_table_function_
init_t init);
void duckdb_table_function_set_function(duckdb_table_function table_function, duckdb_table_function_t
function);
void duckdb_table_function_supports_projection_pushdown(duckdb_table_function table_function, bool
pushdown);
duckdb_state duckdb_register_table_function(duckdb_connection con, duckdb_table_function function);
Table Function
159
DuckDB Documentation
duckdb_create_table_function
Syntax
duckdb_table_function duckdb_create_table_function(
);
duckdb_destroy_table_function
Syntax
void duckdb_destroy_table_function(
duckdb_table_function *table_function
);
Parameters
duckdb_table_function_set_name
Syntax
void duckdb_table_function_set_name(
duckdb_table_function table_function,
const char *name
);
Parameters
duckdb_table_function_add_parameter
Syntax
void duckdb_table_function_add_parameter(
duckdb_table_function table_function,
duckdb_logical_type type
);
160
DuckDB Documentation
Parameters
duckdb_table_function_add_named_parameter
Syntax
void duckdb_table_function_add_named_parameter(
duckdb_table_function table_function,
const char *name,
duckdb_logical_type type
);
Parameters
duckdb_table_function_set_extra_info
Assigns extra information to the table function that can be fetched during binding, etc.
Syntax
void duckdb_table_function_set_extra_info(
duckdb_table_function table_function,
void *extra_info,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_table_function_set_bind
Syntax
void duckdb_table_function_set_bind(
duckdb_table_function table_function,
duckdb_table_function_bind_t bind
);
161
DuckDB Documentation
Parameters
duckdb_table_function_set_init
Syntax
void duckdb_table_function_set_init(
duckdb_table_function table_function,
duckdb_table_function_init_t init
);
Parameters
duckdb_table_function_set_local_init
Syntax
void duckdb_table_function_set_local_init(
duckdb_table_function table_function,
duckdb_table_function_init_t init
);
Parameters
duckdb_table_function_set_function
Syntax
void duckdb_table_function_set_function(
duckdb_table_function table_function,
duckdb_table_function_t function
);
Parameters
162
DuckDB Documentation
duckdb_table_function_supports_projection_pushdown
Sets whether or not the given table function supports projection pushdown.
If this is set to true, the system will provide a list of all required columns in the init stage through the duckdb_init_get_column_
count and duckdb_init_get_column_index functions. If this is set to false (the default), the system will expect all columns to be
projected.
Syntax
void duckdb_table_function_supports_projection_pushdown(
duckdb_table_function table_function,
bool pushdown
);
Parameters
duckdb_register_table_function
The function requires at least a name, a bind function, an init function and a main function.
If the function is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_table_function(
duckdb_connection con,
duckdb_table_function function
);
Parameters
duckdb_bind_get_extra_info
Syntax
void *duckdb_bind_get_extra_info(
duckdb_bind_info info
);
Parameters
163
DuckDB Documentation
duckdb_bind_add_result_column
Syntax
void duckdb_bind_add_result_column(
duckdb_bind_info info,
const char *name,
duckdb_logical_type type
);
Parameters
duckdb_bind_get_parameter_count
Syntax
idx_t duckdb_bind_get_parameter_count(
duckdb_bind_info info
);
Parameters
duckdb_bind_get_parameter
Syntax
duckdb_value duckdb_bind_get_parameter(
duckdb_bind_info info,
idx_t index
);
Parameters
164
DuckDB Documentation
Return Value The value of the parameter. Must be destroyed with duckdb_destroy_value.
duckdb_bind_get_named_parameter
Syntax
duckdb_value duckdb_bind_get_named_parameter(
duckdb_bind_info info,
const char *name
);
Parameters
Return Value The value of the parameter. Must be destroyed with duckdb_destroy_value.
duckdb_bind_set_bind_data
Sets the user‑provided bind data in the bind object. This object can be retrieved again during execution.
Syntax
void duckdb_bind_set_bind_data(
duckdb_bind_info info,
void *bind_data,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_bind_set_cardinality
Sets the cardinality estimate for the table function, used for optimization.
Syntax
void duckdb_bind_set_cardinality(
duckdb_bind_info info,
idx_t cardinality,
bool is_exact
);
165
DuckDB Documentation
Parameters
duckdb_bind_set_error
Syntax
void duckdb_bind_set_error(
duckdb_bind_info info,
const char *error
);
Parameters
duckdb_init_get_extra_info
Syntax
void *duckdb_init_get_extra_info(
duckdb_init_info info
);
Parameters
duckdb_init_get_bind_data
Note that the bind data should be considered as read‑only. For tracking state, use the init data instead.
Syntax
void *duckdb_init_get_bind_data(
duckdb_init_info info
);
Parameters
166
DuckDB Documentation
duckdb_init_set_init_data
Sets the user‑provided init data in the init object. This object can be retrieved again during execution.
Syntax
void duckdb_init_set_init_data(
duckdb_init_info info,
void *init_data,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_init_get_column_count
This function must be used if projection pushdown is enabled to figure out which columns to emit.
Syntax
idx_t duckdb_init_get_column_count(
duckdb_init_info info
);
Parameters
duckdb_init_get_column_index
Returns the column index of the projected column at the specified position.
This function must be used if projection pushdown is enabled to figure out which columns to emit.
Syntax
idx_t duckdb_init_get_column_index(
duckdb_init_info info,
idx_t column_index
);
167
DuckDB Documentation
Parameters
duckdb_init_set_max_threads
Sets how many threads can process this table function in parallel (default: 1)
Syntax
void duckdb_init_set_max_threads(
duckdb_init_info info,
idx_t max_threads
);
Parameters
duckdb_init_set_error
Syntax
void duckdb_init_set_error(
duckdb_init_info info,
const char *error
);
Parameters
duckdb_function_get_extra_info
Syntax
void *duckdb_function_get_extra_info(
duckdb_function_info info
);
Parameters
168
DuckDB Documentation
duckdb_function_get_bind_data
Note that the bind data should be considered as read‑only. For tracking state, use the init data instead.
Syntax
void *duckdb_function_get_bind_data(
duckdb_function_info info
);
Parameters
duckdb_function_get_init_data
Syntax
void *duckdb_function_get_init_data(
duckdb_function_info info
);
Parameters
duckdb_function_get_local_init_data
Gets the thread‑local init data set by duckdb_init_set_init_data during the local_init.
Syntax
void *duckdb_function_get_local_init_data(
duckdb_function_info info
);
Parameters
169
DuckDB Documentation
duckdb_function_set_error
Syntax
void duckdb_function_set_error(
duckdb_function_info info,
const char *error
);
Parameters
Replacement Scans
The replacement scan API can be used to register a callback that is called when a table is read that does not exist in the catalog. For example,
when a query such as SELECT * FROM my_table is executed and my_table does not exist, the replacement scan callback will be
called with my_table as parameter. The replacement scan can then insert a table function with a specific parameter to replace the read
of the table.
duckdb_add_replacement_scan
Syntax
void duckdb_add_replacement_scan(
duckdb_database db,
duckdb_replacement_callback_t replacement,
void *extra_data,
duckdb_delete_callback_t delete_callback
);
Parameters
170
DuckDB Documentation
duckdb_replacement_scan_set_function_name
Sets the replacement function name. If this function is called in the replacement callback, the replacement scan is performed. If it is not
called, the replacement callback is not performed.
Syntax
void duckdb_replacement_scan_set_function_name(
duckdb_replacement_scan_info info,
const char *function_name
);
Parameters
duckdb_replacement_scan_add_parameter
Syntax
void duckdb_replacement_scan_add_parameter(
duckdb_replacement_scan_info info,
duckdb_value parameter
);
Parameters
duckdb_replacement_scan_set_error
Report that an error has occurred while executing the replacement scan.
Syntax
void duckdb_replacement_scan_set_error(
duckdb_replacement_scan_info info,
const char *error
);
Parameters
171
DuckDB Documentation
Complete API
Deprecated. The reference contains several deprecation notices. These concern methods whose long‑term availability is not guar‑
anteed as they may be removed in the future. That said, DuckDB's developers plan to carry out deprecations slowly as several of
the deprecated methods do not yet have a fully functional alternative. Therefore, they will not removed before the alternative is
available, and even then, there will be a grace period of a few minor versions before removing them. The reason that the methods
are already deprecated in v1.0 is to denote that they are not part of the v1.0 stable API, which contains methods that are available
long‑term.
Open Connect
Configuration
Query Execution
Result Functions
172
DuckDB Documentation
Helpers
173
DuckDB Documentation
Hugeint Helpers
Decimal Helpers
Prepared Statements
174
DuckDB Documentation
Extract Statements
175
DuckDB Documentation
Value Interface
176
DuckDB Documentation
Vector Interface
177
DuckDB Documentation
Scalar Functions
duckdb_scalar_function duckdb_create_scalar_function();
void duckdb_destroy_scalar_function(duckdb_scalar_function *scalar_function);
void duckdb_scalar_function_set_name(duckdb_scalar_function scalar_function, const char *name);
void duckdb_scalar_function_set_varargs(duckdb_scalar_function scalar_function, duckdb_logical_type
type);
void duckdb_scalar_function_set_special_handling(duckdb_scalar_function scalar_function);
void duckdb_scalar_function_set_volatile(duckdb_scalar_function scalar_function);
void duckdb_scalar_function_add_parameter(duckdb_scalar_function scalar_function, duckdb_logical_type
type);
void duckdb_scalar_function_set_return_type(duckdb_scalar_function scalar_function, duckdb_logical_type
type);
void duckdb_scalar_function_set_extra_info(duckdb_scalar_function scalar_function, void *extra_info,
duckdb_delete_callback_t destroy);
void duckdb_scalar_function_set_function(duckdb_scalar_function scalar_function, duckdb_scalar_function_
t function);
duckdb_state duckdb_register_scalar_function(duckdb_connection con, duckdb_scalar_function scalar_
function);
void *duckdb_scalar_function_get_extra_info(duckdb_function_info info);
void duckdb_scalar_function_set_error(duckdb_function_info info, const char *error);
duckdb_scalar_function_set duckdb_create_scalar_function_set(const char *name);
void duckdb_destroy_scalar_function_set(duckdb_scalar_function_set *scalar_function_set);
duckdb_state duckdb_add_scalar_function_to_set(duckdb_scalar_function_set set, duckdb_scalar_function
function);
duckdb_state duckdb_register_scalar_function_set(duckdb_connection con, duckdb_scalar_function_set set);
Aggregate Functions
duckdb_aggregate_function duckdb_create_aggregate_function();
void duckdb_destroy_aggregate_function(duckdb_aggregate_function *aggregate_function);
void duckdb_aggregate_function_set_name(duckdb_aggregate_function aggregate_function, const char *name);
void duckdb_aggregate_function_add_parameter(duckdb_aggregate_function aggregate_function, duckdb_
logical_type type);
void duckdb_aggregate_function_set_return_type(duckdb_aggregate_function aggregate_function, duckdb_
logical_type type);
void duckdb_aggregate_function_set_functions(duckdb_aggregate_function aggregate_function, duckdb_
aggregate_state_size state_size, duckdb_aggregate_init_t state_init, duckdb_aggregate_update_t update,
duckdb_aggregate_combine_t combine, duckdb_aggregate_finalize_t finalize);
void duckdb_aggregate_function_set_destructor(duckdb_aggregate_function aggregate_function, duckdb_
aggregate_destroy_t destroy);
duckdb_state duckdb_register_aggregate_function(duckdb_connection con, duckdb_aggregate_function
aggregate_function);
178
DuckDB Documentation
Table Functions
duckdb_table_function duckdb_create_table_function();
void duckdb_destroy_table_function(duckdb_table_function *table_function);
void duckdb_table_function_set_name(duckdb_table_function table_function, const char *name);
void duckdb_table_function_add_parameter(duckdb_table_function table_function, duckdb_logical_type
type);
void duckdb_table_function_add_named_parameter(duckdb_table_function table_function, const char *name,
duckdb_logical_type type);
void duckdb_table_function_set_extra_info(duckdb_table_function table_function, void *extra_info,
duckdb_delete_callback_t destroy);
void duckdb_table_function_set_bind(duckdb_table_function table_function, duckdb_table_function_bind_t
bind);
void duckdb_table_function_set_init(duckdb_table_function table_function, duckdb_table_function_init_t
init);
void duckdb_table_function_set_local_init(duckdb_table_function table_function, duckdb_table_function_
init_t init);
void duckdb_table_function_set_function(duckdb_table_function table_function, duckdb_table_function_t
function);
void duckdb_table_function_supports_projection_pushdown(duckdb_table_function table_function, bool
pushdown);
duckdb_state duckdb_register_table_function(duckdb_connection con, duckdb_table_function function);
179
DuckDB Documentation
Table Function
Replacement Scans
Profiling Info
Appender
180
DuckDB Documentation
Table Description
Arrow Interface
Threading Information
181
DuckDB Documentation
Cast Functions
duckdb_cast_function duckdb_create_cast_function();
void duckdb_cast_function_set_source_type(duckdb_cast_function cast_function, duckdb_logical_type
source_type);
void duckdb_cast_function_set_target_type(duckdb_cast_function cast_function, duckdb_logical_type
target_type);
void duckdb_cast_function_set_implicit_cast_cost(duckdb_cast_function cast_function, int64_t cost);
void duckdb_cast_function_set_function(duckdb_cast_function cast_function, duckdb_cast_function_t
function);
void duckdb_cast_function_set_extra_info(duckdb_cast_function cast_function, void *extra_info, duckdb_
delete_callback_t destroy);
void *duckdb_cast_function_get_extra_info(duckdb_function_info info);
duckdb_cast_mode duckdb_cast_function_get_cast_mode(duckdb_function_info info);
void duckdb_cast_function_set_error(duckdb_function_info info, const char *error);
void duckdb_cast_function_set_row_error(duckdb_function_info info, const char *error, idx_t row, duckdb_
vector output);
duckdb_state duckdb_register_cast_function(duckdb_connection con, duckdb_cast_function cast_function);
void duckdb_destroy_cast_function(duckdb_cast_function *cast_function);
duckdb_open
Creates a new database or opens an existing database file stored at the given path. If no path is given a new in‑memory database is created
instead. The instantiated database should be closed with 'duckdb_close'.
Syntax
duckdb_state duckdb_open(
const char *path,
duckdb_database *out_database
);
Parameters
• path: Path to the database file on disk, or nullptr or :memory: to open an in‑memory database.
• out_database: The result database object.
duckdb_open_ext
Extended version of duckdb_open. Creates a new database or opens an existing database file stored at the given path. The instantiated
database should be closed with 'duckdb_close'.
182
DuckDB Documentation
Syntax
duckdb_state duckdb_open_ext(
const char *path,
duckdb_database *out_database,
duckdb_config config,
char **out_error
);
Parameters
• path: Path to the database file on disk, or nullptr or :memory: to open an in‑memory database.
• out_database: The result database object.
• config: (Optional) configuration used to start up the database system.
• out_error: If set and the function returns DuckDBError, this will contain the reason why the start‑up failed. Note that the error
must be freed using duckdb_free.
duckdb_close
Closes the specified database and de‑allocates all memory allocated for that database. This should be called after you are done with any
database allocated through duckdb_open or duckdb_open_ext. Note that failing to call duckdb_close (in case of e.g., a program
crash) will not cause data corruption. Still, it is recommended to always correctly close a database object after you are done with it.
Syntax
void duckdb_close(
duckdb_database *database
);
Parameters
duckdb_connect
Opens a connection to a database. Connections are required to query the database, and store transactional state associated with the
connection. The instantiated connection should be closed using 'duckdb_disconnect'.
Syntax
duckdb_state duckdb_connect(
duckdb_database database,
duckdb_connection *out_connection
);
Parameters
183
DuckDB Documentation
duckdb_interrupt
Syntax
void duckdb_interrupt(
duckdb_connection connection
);
Parameters
duckdb_query_progress
Syntax
duckdb_query_progress_type duckdb_query_progress(
duckdb_connection connection
);
Parameters
duckdb_disconnect
Closes the specified connection and de‑allocates all memory allocated for that connection.
Syntax
void duckdb_disconnect(
duckdb_connection *connection
);
Parameters
duckdb_library_version
Returns the version of the linked DuckDB, with a version postfix for dev versions
Usually used for developing C extensions that must return this for a compatibility check.
184
DuckDB Documentation
Syntax
const char *duckdb_library_version(
);
duckdb_create_config
Initializes an empty configuration object that can be used to provide start‑up options for the DuckDB instance through duckdb_open_
ext. The duckdb_config must be destroyed using 'duckdb_destroy_config'
Note that duckdb_destroy_config should always be called on the resulting config, even if the function returns DuckDBError.
Syntax
duckdb_state duckdb_create_config(
duckdb_config *out_config
);
Parameters
duckdb_config_count
This returns the total amount of configuration options available for usage with duckdb_get_config_flag.
This should not be called in a loop as it internally loops over all the options.
Syntax
size_t duckdb_config_count(
);
duckdb_get_config_flag
Obtains a human‑readable name and description of a specific configuration option. This can be used to e.g. display configuration options.
This will succeed unless index is out of range (i.e., >= duckdb_config_count).
Syntax
duckdb_state duckdb_get_config_flag(
size_t index,
const char **out_name,
const char **out_description
);
185
DuckDB Documentation
Parameters
duckdb_set_config
Sets the specified option for the specified configuration. The configuration option is indicated by name. To obtain a list of config options,
see duckdb_get_config_flag.
This can fail if either the name is invalid, or if the value provided for the option is invalid.
Syntax
duckdb_state duckdb_set_config(
duckdb_config config,
const char *name,
const char *option
);
Parameters
duckdb_destroy_config
Destroys the specified configuration object and de‑allocates all memory allocated for the object.
Syntax
void duckdb_destroy_config(
duckdb_config *config
);
Parameters
duckdb_query
Executes a SQL query within a connection and stores the full (materialized) result in the out_result pointer. If the query fails to execute,
DuckDBError is returned and the error message can be retrieved by calling duckdb_result_error.
Note that after running duckdb_query, duckdb_destroy_result must be called on the result object even if the query fails, other‑
wise the error stored within the result will not be freed correctly.
186
DuckDB Documentation
Syntax
duckdb_state duckdb_query(
duckdb_connection connection,
const char *query,
duckdb_result *out_result
);
Parameters
duckdb_destroy_result
Closes the result and de‑allocates all memory allocated for that connection.
Syntax
void duckdb_destroy_result(
duckdb_result *result
);
Parameters
duckdb_column_name
Returns the column name of the specified column. The result should not need to be freed; the column names will automatically be de‑
stroyed when the result is destroyed.
Syntax
Parameters
187
DuckDB Documentation
duckdb_column_type
Syntax
duckdb_type duckdb_column_type(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_result_statement_type
Syntax
duckdb_statement_type duckdb_result_statement_type(
duckdb_result result
);
Parameters
duckdb_column_logical_type
Syntax
duckdb_logical_type duckdb_column_logical_type(
duckdb_result *result,
idx_t col
);
Parameters
188
DuckDB Documentation
duckdb_column_count
Syntax
idx_t duckdb_column_count(
duckdb_result *result
);
Parameters
duckdb_row_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_row_count(
duckdb_result *result
);
Parameters
duckdb_rows_changed
Returns the number of rows changed by the query stored in the result. This is relevant only for INSERT/UPDATE/DELETE queries. For other
queries the rows_changed will be 0.
Syntax
idx_t duckdb_rows_changed(
duckdb_result *result
);
Parameters
189
DuckDB Documentation
duckdb_column_data
Deprecated. This method has been deprecated. Prefer using duckdb_result_get_chunk instead.
The function returns a dense array which contains the result data. The exact type stored in the array depends on the corresponding duckdb_
type (as provided by duckdb_column_type). For the exact type by which the data should be accessed, see the comments in the types
section or the DUCKDB_TYPE enum.
For example, for a column of type DUCKDB_TYPE_INTEGER, rows can be accessed in the following manner:
Syntax
void *duckdb_column_data(
duckdb_result *result,
idx_t col
);
Parameters
duckdb_nullmask_data
Deprecated. This method has been deprecated. Prefer using duckdb_result_get_chunk instead.
Returns the nullmask of a specific column of a result in columnar format. The nullmask indicates for every row whether or not the corre‑
sponding row is NULL. If a row is NULL, the values present in the array provided by duckdb_column_data are undefined.
Syntax
bool *duckdb_nullmask_data(
duckdb_result *result,
idx_t col
);
Parameters
190
DuckDB Documentation
duckdb_result_error
Returns the error message contained within the result. The error is only set if duckdb_query returns DuckDBError.
The result of this function must not be freed. It will be cleaned up when duckdb_destroy_result is called.
Syntax
Parameters
duckdb_result_error_type
Returns the result error type contained within the result. The error is only set if duckdb_query returns DuckDBError.
Syntax
duckdb_error_type duckdb_result_error_type(
duckdb_result *result
);
Parameters
duckdb_result_get_chunk
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetches a data chunk from the duckdb_result. This function should be called repeatedly until the result is exhausted.
This function supersedes all duckdb_value functions, as well as the duckdb_column_data and duckdb_nullmask_data func‑
tions. It results in significantly better performance, and should be preferred in newer code‑bases.
If this function is used, none of the other result functions can be used and vice versa (i.e., this function cannot be mixed with the legacy
result functions).
Use duckdb_result_chunk_count to figure out how many chunks there are in the result.
191
DuckDB Documentation
Syntax
duckdb_data_chunk duckdb_result_get_chunk(
duckdb_result result,
idx_t chunk_index
);
Parameters
Return Value The resulting data chunk. Returns NULL if the chunk index is out of bounds.
duckdb_result_is_streaming
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
bool duckdb_result_is_streaming(
duckdb_result result
);
Parameters
Return Value Whether or not the result object is of the type StreamQueryResult
duckdb_result_chunk_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_result_chunk_count(
duckdb_result result
);
Parameters
duckdb_result_return_type
192
DuckDB Documentation
Syntax
duckdb_result_type duckdb_result_return_type(
duckdb_result result
);
Parameters
duckdb_value_boolean
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The boolean value at the specified location, or false if the value cannot be converted.
Syntax
bool duckdb_value_boolean(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_int8
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The int8_t value at the specified location, or 0 if the value cannot be converted.
Syntax
int8_t duckdb_value_int8(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_int16
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The int16_t value at the specified location, or 0 if the value cannot be converted.
Syntax
int16_t duckdb_value_int16(
duckdb_result *result,
idx_t col,
idx_t row
);
193
DuckDB Documentation
duckdb_value_int32
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The int32_t value at the specified location, or 0 if the value cannot be converted.
Syntax
int32_t duckdb_value_int32(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_int64
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The int64_t value at the specified location, or 0 if the value cannot be converted.
Syntax
int64_t duckdb_value_int64(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_hugeint
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_hugeint value at the specified location, or 0 if the value cannot be converted.
Syntax
duckdb_hugeint duckdb_value_hugeint(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_uhugeint
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_uhugeint value at the specified location, or 0 if the value cannot be converted.
194
DuckDB Documentation
Syntax
duckdb_uhugeint duckdb_value_uhugeint(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_decimal
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_decimal value at the specified location, or 0 if the value cannot be converted.
Syntax
duckdb_decimal duckdb_value_decimal(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_uint8
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The uint8_t value at the specified location, or 0 if the value cannot be converted.
Syntax
uint8_t duckdb_value_uint8(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_uint16
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The uint16_t value at the specified location, or 0 if the value cannot be converted.
Syntax
uint16_t duckdb_value_uint16(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_uint32
195
DuckDB Documentation
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The uint32_t value at the specified location, or 0 if the value cannot be converted.
Syntax
uint32_t duckdb_value_uint32(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_uint64
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The uint64_t value at the specified location, or 0 if the value cannot be converted.
Syntax
uint64_t duckdb_value_uint64(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_float
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The float value at the specified location, or 0 if the value cannot be converted.
Syntax
float duckdb_value_float(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_double
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The double value at the specified location, or 0 if the value cannot be converted.
Syntax
double duckdb_value_double(
duckdb_result *result,
idx_t col,
idx_t row
);
196
DuckDB Documentation
duckdb_value_date
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_date value at the specified location, or 0 if the value cannot be converted.
Syntax
duckdb_date duckdb_value_date(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_time
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_time value at the specified location, or 0 if the value cannot be converted.
Syntax
duckdb_time duckdb_value_time(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_timestamp
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_timestamp value at the specified location, or 0 if the value cannot be converted.
Syntax
duckdb_timestamp duckdb_value_timestamp(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_interval
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_interval value at the specified location, or 0 if the value cannot be converted.
197
DuckDB Documentation
Syntax
duckdb_interval duckdb_value_interval(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_varchar
Deprecated. This method has been deprecated. Use duckdb_value_string instead. This function does not work correctly if the string
contains null bytes.
Return Value The text value at the specified location as a null‑terminated string, or nullptr if the value cannot be converted. The result
must be freed with duckdb_free.
Syntax
char *duckdb_value_varchar(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_string
Warning. Deprecation notice. This method is scheduled for removal in a future release.
No support for nested types, and for other complex types. The resulting field ”string.data” must be freed with duckdb_free.
Return Value The string value at the specified location. Attempts to cast the result value to string.
Syntax
duckdb_string duckdb_value_string(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_varchar_internal
Deprecated. This method has been deprecated. Use duckdb_value_string_internal instead. This function does not work correctly if
the string contains null bytes.
Return Value The char* value at the specified location. ONLY works on VARCHAR columns and does not auto‑cast. If the column is NOT
a VARCHAR column this function will return NULL.
198
DuckDB Documentation
Syntax
char *duckdb_value_varchar_internal(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_string_internal
Deprecated. This method has been deprecated. Use duckdb_value_string_internal instead. This function does not work correctly if
the string contains null bytes.
Return Value The char* value at the specified location. ONLY works on VARCHAR columns and does not auto‑cast. If the column is NOT
a VARCHAR column this function will return NULL.
Syntax
duckdb_string duckdb_value_string_internal(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_blob
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value The duckdb_blob value at the specified location. Returns a blob with blob.data set to nullptr if the value cannot be con‑
verted. The resulting field ”blob.data” must be freed with duckdb_free.
Syntax
duckdb_blob duckdb_value_blob(
duckdb_result *result,
idx_t col,
idx_t row
);
duckdb_value_is_null
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Return Value Returns true if the value at the specified index is NULL, and false otherwise.
Syntax
bool duckdb_value_is_null(
duckdb_result *result,
idx_t col,
idx_t row
);
199
DuckDB Documentation
duckdb_malloc
Allocate size bytes of memory using the duckdb internal malloc function. Any memory allocated in this manner should be freed using
duckdb_free.
Syntax
void *duckdb_malloc(
size_t size
);
Parameters
duckdb_free
Syntax
void duckdb_free(
void *ptr
);
Parameters
duckdb_vector_size
The internal vector size used by DuckDB. This is the amount of tuples that will fit into a data chunk created by duckdb_create_data_
chunk.
Syntax
idx_t duckdb_vector_size(
);
duckdb_string_is_inlined
Whether or not the duckdb_string_t value is inlined. This means that the data of the string does not have a separate allocation.
Syntax
bool duckdb_string_is_inlined(
duckdb_string_t string
);
200
DuckDB Documentation
duckdb_string_t_length
Syntax
uint32_t duckdb_string_t_length(
duckdb_string_t string
);
Parameters
duckdb_string_t_data
Syntax
const char *duckdb_string_t_data(
duckdb_string_t *string
);
Parameters
duckdb_from_date
Decompose a duckdb_date object into year, month and date (stored as duckdb_date_struct).
Syntax
duckdb_date_struct duckdb_from_date(
duckdb_date date
);
Parameters
duckdb_to_date
201
DuckDB Documentation
Syntax
duckdb_date duckdb_to_date(
duckdb_date_struct date
);
Parameters
duckdb_is_finite_date
Syntax
bool duckdb_is_finite_date(
duckdb_date date
);
Parameters
duckdb_from_time
Decompose a duckdb_time object into hour, minute, second and microsecond (stored as duckdb_time_struct).
Syntax
duckdb_time_struct duckdb_from_time(
duckdb_time time
);
Parameters
duckdb_create_time_tz
202
DuckDB Documentation
Syntax
duckdb_time_tz duckdb_create_time_tz(
int64_t micros,
int32_t offset
);
Parameters
duckdb_from_time_tz
Use duckdb_from_time to further decompose the micros into hour, minute, second and microsecond.
Syntax
duckdb_time_tz_struct duckdb_from_time_tz(
duckdb_time_tz micros
);
Parameters
duckdb_to_time
Syntax
duckdb_time duckdb_to_time(
duckdb_time_struct time
);
Parameters
duckdb_from_timestamp
203
DuckDB Documentation
Syntax
duckdb_timestamp_struct duckdb_from_timestamp(
duckdb_timestamp ts
);
Parameters
duckdb_to_timestamp
Syntax
duckdb_timestamp duckdb_to_timestamp(
duckdb_timestamp_struct ts
);
Parameters
duckdb_is_finite_timestamp
Syntax
bool duckdb_is_finite_timestamp(
duckdb_timestamp ts
);
Parameters
duckdb_hugeint_to_double
Converts a duckdb_hugeint object (as obtained from a DUCKDB_TYPE_HUGEINT column) into a double.
Syntax
double duckdb_hugeint_to_double(
duckdb_hugeint val
);
204
DuckDB Documentation
Parameters
duckdb_double_to_hugeint
If the conversion fails because the double value is too big the result will be 0.
Syntax
duckdb_hugeint duckdb_double_to_hugeint(
double val
);
Parameters
duckdb_uhugeint_to_double
Converts a duckdb_uhugeint object (as obtained from a DUCKDB_TYPE_UHUGEINT column) into a double.
Syntax
double duckdb_uhugeint_to_double(
duckdb_uhugeint val
);
Parameters
duckdb_double_to_uhugeint
If the conversion fails because the double value is too big the result will be 0.
Syntax
duckdb_uhugeint duckdb_double_to_uhugeint(
double val
);
205
DuckDB Documentation
Parameters
duckdb_double_to_decimal
If the conversion fails because the double value is too big, or the width/scale are invalid the result will be 0.
Syntax
duckdb_decimal duckdb_double_to_decimal(
double val,
uint8_t width,
uint8_t scale
);
Parameters
duckdb_decimal_to_double
Converts a duckdb_decimal object (as obtained from a DUCKDB_TYPE_DECIMAL column) into a double.
Syntax
double duckdb_decimal_to_double(
duckdb_decimal val
);
Parameters
duckdb_prepare
Note that after calling duckdb_prepare, the prepared statement should always be destroyed using duckdb_destroy_prepare,
even if the prepare fails.
If the prepare fails, duckdb_prepare_error can be called to obtain the reason why the prepare failed.
206
DuckDB Documentation
Syntax
duckdb_state duckdb_prepare(
duckdb_connection connection,
const char *query,
duckdb_prepared_statement *out_prepared_statement
);
Parameters
duckdb_destroy_prepare
Closes the prepared statement and de‑allocates all memory allocated for the statement.
Syntax
void duckdb_destroy_prepare(
duckdb_prepared_statement *prepared_statement
);
Parameters
duckdb_prepare_error
Returns the error message associated with the given prepared statement. If the prepared statement has no error message, this returns
nullptr instead.
The error message should not be freed. It will be de‑allocated when duckdb_destroy_prepare is called.
Syntax
const char *duckdb_prepare_error(
duckdb_prepared_statement prepared_statement
);
Parameters
duckdb_nparams
Returns the number of parameters that can be provided to the given prepared statement.
207
DuckDB Documentation
Syntax
idx_t duckdb_nparams(
duckdb_prepared_statement prepared_statement
);
Parameters
duckdb_parameter_name
Returns the name used to identify the parameter The returned string should be freed using duckdb_free.
Returns NULL if the index is out of range for the provided prepared statement.
Syntax
Parameters
• prepared_statement: The prepared statement for which to get the parameter name from.
duckdb_param_type
Returns the parameter type for the parameter at the given index.
Returns DUCKDB_TYPE_INVALID if the parameter index is out of range or the statement was not successfully prepared.
Syntax
duckdb_type duckdb_param_type(
duckdb_prepared_statement prepared_statement,
idx_t param_idx
);
Parameters
duckdb_clear_bindings
208
DuckDB Documentation
Syntax
duckdb_state duckdb_clear_bindings(
duckdb_prepared_statement prepared_statement
);
duckdb_prepared_statement_type
Syntax
duckdb_statement_type duckdb_prepared_statement_type(
duckdb_prepared_statement statement
);
Parameters
duckdb_bind_value
Syntax
duckdb_state duckdb_bind_value(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_value val
);
duckdb_bind_parameter_index
Retrieve the index of the parameter for the prepared statement, identified by name
Syntax
duckdb_state duckdb_bind_parameter_index(
duckdb_prepared_statement prepared_statement,
idx_t *param_idx_out,
const char *name
);
duckdb_bind_boolean
209
DuckDB Documentation
Syntax
duckdb_state duckdb_bind_boolean(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
bool val
);
duckdb_bind_int8
Syntax
duckdb_state duckdb_bind_int8(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
int8_t val
);
duckdb_bind_int16
Syntax
duckdb_state duckdb_bind_int16(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
int16_t val
);
duckdb_bind_int32
Syntax
duckdb_state duckdb_bind_int32(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
int32_t val
);
duckdb_bind_int64
Syntax
duckdb_state duckdb_bind_int64(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
int64_t val
);
210
DuckDB Documentation
duckdb_bind_hugeint
Syntax
duckdb_state duckdb_bind_hugeint(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_hugeint val
);
duckdb_bind_uhugeint
Syntax
duckdb_state duckdb_bind_uhugeint(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_uhugeint val
);
duckdb_bind_decimal
Syntax
duckdb_state duckdb_bind_decimal(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_decimal val
);
duckdb_bind_uint8
Syntax
duckdb_state duckdb_bind_uint8(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
uint8_t val
);
duckdb_bind_uint16
211
DuckDB Documentation
Syntax
duckdb_state duckdb_bind_uint16(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
uint16_t val
);
duckdb_bind_uint32
Syntax
duckdb_state duckdb_bind_uint32(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
uint32_t val
);
duckdb_bind_uint64
Syntax
duckdb_state duckdb_bind_uint64(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
uint64_t val
);
duckdb_bind_float
Syntax
duckdb_state duckdb_bind_float(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
float val
);
duckdb_bind_double
Syntax
duckdb_state duckdb_bind_double(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
double val
);
212
DuckDB Documentation
duckdb_bind_date
Syntax
duckdb_state duckdb_bind_date(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_date val
);
duckdb_bind_time
Syntax
duckdb_state duckdb_bind_time(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_time val
);
duckdb_bind_timestamp
Syntax
duckdb_state duckdb_bind_timestamp(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_timestamp val
);
duckdb_bind_timestamp_tz
Syntax
duckdb_state duckdb_bind_timestamp_tz(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_timestamp val
);
duckdb_bind_interval
213
DuckDB Documentation
Syntax
duckdb_state duckdb_bind_interval(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
duckdb_interval val
);
duckdb_bind_varchar
Binds a null‑terminated varchar value to the prepared statement at the specified index.
Syntax
duckdb_state duckdb_bind_varchar(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
const char *val
);
duckdb_bind_varchar_length
Syntax
duckdb_state duckdb_bind_varchar_length(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
const char *val,
idx_t length
);
duckdb_bind_blob
Syntax
duckdb_state duckdb_bind_blob(
duckdb_prepared_statement prepared_statement,
idx_t param_idx,
const void *data,
idx_t length
);
duckdb_bind_null
214
DuckDB Documentation
Syntax
duckdb_state duckdb_bind_null(
duckdb_prepared_statement prepared_statement,
idx_t param_idx
);
duckdb_execute_prepared
Executes the prepared statement with the given bound parameters, and returns a materialized query result.
This method can be called multiple times for each prepared statement, and the parameters can be modified between calls to this func‑
tion.
Syntax
duckdb_state duckdb_execute_prepared(
duckdb_prepared_statement prepared_statement,
duckdb_result *out_result
);
Parameters
duckdb_execute_prepared_streaming
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Executes the prepared statement with the given bound parameters, and returns an optionally‑streaming query result. To determine if the
resulting query was in fact streamed, use duckdb_result_is_streaming
This method can be called multiple times for each prepared statement, and the parameters can be modified between calls to this func‑
tion.
Syntax
duckdb_state duckdb_execute_prepared_streaming(
duckdb_prepared_statement prepared_statement,
duckdb_result *out_result
);
Parameters
215
DuckDB Documentation
duckdb_extract_statements
Extract all statements from a query. Note that after calling duckdb_extract_statements, the extracted statements should always be
destroyed using duckdb_destroy_extracted, even if no statements were extracted.
If the extract fails, duckdb_extract_statements_error can be called to obtain the reason why the extract failed.
Syntax
idx_t duckdb_extract_statements(
duckdb_connection connection,
const char *query,
duckdb_extracted_statements *out_extracted_statements
);
Parameters
duckdb_prepare_extracted_statement
Prepare an extracted statement. Note that after calling duckdb_prepare_extracted_statement, the prepared statement should
always be destroyed using duckdb_destroy_prepare, even if the prepare fails.
If the prepare fails, duckdb_prepare_error can be called to obtain the reason why the prepare failed.
Syntax
duckdb_state duckdb_prepare_extracted_statement(
duckdb_connection connection,
duckdb_extracted_statements extracted_statements,
idx_t index,
duckdb_prepared_statement *out_prepared_statement
);
Parameters
duckdb_extract_statements_error
Returns the error message contained within the extracted statements. The result of this function must not be freed. It will be cleaned up
when duckdb_destroy_extracted is called.
216
DuckDB Documentation
Syntax
Parameters
duckdb_destroy_extracted
Syntax
void duckdb_destroy_extracted(
duckdb_extracted_statements *extracted_statements
);
Parameters
duckdb_pending_prepared
Executes the prepared statement with the given bound parameters, and returns a pending result. The pending result represents an inter‑
mediate structure for a query that is not yet fully executed. The pending result can be used to incrementally execute a query, returning
control to the client between tasks.
Note that after calling duckdb_pending_prepared, the pending result should always be destroyed using duckdb_destroy_
pending, even if this function returns DuckDBError.
Syntax
duckdb_state duckdb_pending_prepared(
duckdb_prepared_statement prepared_statement,
duckdb_pending_result *out_result
);
Parameters
217
DuckDB Documentation
duckdb_pending_prepared_streaming
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Executes the prepared statement with the given bound parameters, and returns a pending result. This pending result will create a streaming
duckdb_result when executed. The pending result represents an intermediate structure for a query that is not yet fully executed.
Note that after calling duckdb_pending_prepared_streaming, the pending result should always be destroyed using duckdb_
destroy_pending, even if this function returns DuckDBError.
Syntax
duckdb_state duckdb_pending_prepared_streaming(
duckdb_prepared_statement prepared_statement,
duckdb_pending_result *out_result
);
Parameters
duckdb_destroy_pending
Closes the pending result and de‑allocates all memory allocated for the result.
Syntax
void duckdb_destroy_pending(
duckdb_pending_result *pending_result
);
Parameters
duckdb_pending_error
The result of this function must not be freed. It will be cleaned up when duckdb_destroy_pending is called.
Syntax
Parameters
218
DuckDB Documentation
duckdb_pending_execute_task
Executes a single task within the query, returning whether or not the query is ready.
If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. If this returns
DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_task function should be called again. If this returns DUCKDB_
PENDING_ERROR, an error occurred during execution.
Syntax
duckdb_pending_state duckdb_pending_execute_task(
duckdb_pending_result pending_result
);
Parameters
Return Value The state of the pending result after the execution.
duckdb_pending_execute_check_state
If this returns DUCKDB_PENDING_RESULT_READY, the duckdb_execute_pending function can be called to obtain the result. If this re‑
turns DUCKDB_PENDING_RESULT_NOT_READY, the duckdb_pending_execute_check_state function should be called again. If this returns
DUCKDB_PENDING_ERROR, an error occurred during execution.
Syntax
duckdb_pending_state duckdb_pending_execute_check_state(
duckdb_pending_result pending_result
);
Parameters
duckdb_execute_pending
Fully execute a pending query result, returning the final query result.
If duckdb_pending_execute_task has been called until DUCKDB_PENDING_RESULT_READY was returned, this will return fast. Otherwise,
all remaining tasks must be executed first.
219
DuckDB Documentation
Syntax
duckdb_state duckdb_execute_pending(
duckdb_pending_result pending_result,
duckdb_result *out_result
);
Parameters
duckdb_pending_execution_is_finished
Syntax
bool duckdb_pending_execution_is_finished(
duckdb_pending_state pending_state
);
Parameters
duckdb_destroy_value
Destroys the value and de‑allocates all memory allocated for that type.
Syntax
void duckdb_destroy_value(
duckdb_value *value
);
Parameters
duckdb_create_varchar
220
DuckDB Documentation
Syntax
duckdb_value duckdb_create_varchar(
const char *text
);
Parameters
duckdb_create_varchar_length
Syntax
duckdb_value duckdb_create_varchar_length(
const char *text,
idx_t length
);
Parameters
duckdb_create_bool
Syntax
duckdb_value duckdb_create_bool(
bool input
);
Parameters
duckdb_create_int8
221
DuckDB Documentation
Syntax
duckdb_value duckdb_create_int8(
int8_t input
);
Parameters
duckdb_create_uint8
Syntax
duckdb_value duckdb_create_uint8(
uint8_t input
);
Parameters
duckdb_create_int16
Syntax
duckdb_value duckdb_create_int16(
int16_t input
);
Parameters
duckdb_create_uint16
Syntax
duckdb_value duckdb_create_uint16(
uint16_t input
);
222
DuckDB Documentation
Parameters
duckdb_create_int32
Syntax
duckdb_value duckdb_create_int32(
int32_t input
);
Parameters
duckdb_create_uint32
Syntax
duckdb_value duckdb_create_uint32(
uint32_t input
);
Parameters
duckdb_create_uint64
Syntax
duckdb_value duckdb_create_uint64(
uint64_t input
);
Parameters
223
DuckDB Documentation
duckdb_create_int64
Syntax
duckdb_value duckdb_create_int64(
int64_t val
);
duckdb_create_hugeint
Syntax
duckdb_value duckdb_create_hugeint(
duckdb_hugeint input
);
Parameters
duckdb_create_uhugeint
Syntax
duckdb_value duckdb_create_uhugeint(
duckdb_uhugeint input
);
Parameters
duckdb_create_float
224
DuckDB Documentation
Syntax
duckdb_value duckdb_create_float(
float input
);
Parameters
duckdb_create_double
Syntax
duckdb_value duckdb_create_double(
double input
);
Parameters
duckdb_create_date
Syntax
duckdb_value duckdb_create_date(
duckdb_date input
);
Parameters
duckdb_create_time
Syntax
duckdb_value duckdb_create_time(
duckdb_time input
);
225
DuckDB Documentation
Parameters
duckdb_create_time_tz_value
Creates a value from a time_tz. Not to be confused with duckdb_create_time_tz, which creates a duckdb_time_tz_t.
Syntax
duckdb_value duckdb_create_time_tz_value(
duckdb_time_tz value
);
Parameters
duckdb_create_timestamp
Syntax
duckdb_value duckdb_create_timestamp(
duckdb_timestamp input
);
Parameters
duckdb_create_interval
Syntax
duckdb_value duckdb_create_interval(
duckdb_interval input
);
Parameters
226
DuckDB Documentation
duckdb_create_blob
Syntax
duckdb_value duckdb_create_blob(
const uint8_t *data,
idx_t length
);
Parameters
duckdb_get_bool
Syntax
bool duckdb_get_bool(
duckdb_value val
);
Parameters
duckdb_get_int8
Syntax
int8_t duckdb_get_int8(
duckdb_value val
);
Parameters
227
DuckDB Documentation
duckdb_get_uint8
Syntax
uint8_t duckdb_get_uint8(
duckdb_value val
);
Parameters
duckdb_get_int16
Syntax
int16_t duckdb_get_int16(
duckdb_value val
);
Parameters
duckdb_get_uint16
Syntax
uint16_t duckdb_get_uint16(
duckdb_value val
);
Parameters
duckdb_get_int32
228
DuckDB Documentation
Syntax
int32_t duckdb_get_int32(
duckdb_value val
);
Parameters
duckdb_get_uint32
Syntax
uint32_t duckdb_get_uint32(
duckdb_value val
);
Parameters
duckdb_get_int64
Syntax
int64_t duckdb_get_int64(
duckdb_value val
);
Parameters
duckdb_get_uint64
Syntax
uint64_t duckdb_get_uint64(
duckdb_value val
);
229
DuckDB Documentation
Parameters
duckdb_get_hugeint
Syntax
duckdb_hugeint duckdb_get_hugeint(
duckdb_value val
);
Parameters
duckdb_get_uhugeint
Syntax
duckdb_uhugeint duckdb_get_uhugeint(
duckdb_value val
);
Parameters
duckdb_get_float
Syntax
float duckdb_get_float(
duckdb_value val
);
Parameters
230
DuckDB Documentation
duckdb_get_double
Syntax
double duckdb_get_double(
duckdb_value val
);
Parameters
duckdb_get_date
Syntax
duckdb_date duckdb_get_date(
duckdb_value val
);
Parameters
duckdb_get_time
Syntax
duckdb_time duckdb_get_time(
duckdb_value val
);
Parameters
231
DuckDB Documentation
duckdb_get_time_tz
Syntax
duckdb_time_tz duckdb_get_time_tz(
duckdb_value val
);
Parameters
duckdb_get_timestamp
Syntax
duckdb_timestamp duckdb_get_timestamp(
duckdb_value val
);
Parameters
duckdb_get_interval
Syntax
duckdb_interval duckdb_get_interval(
duckdb_value val
);
Parameters
duckdb_get_value_type
Returns the type of the given value. The type is valid as long as the value is not destroyed. The type itself must not be destroyed.
232
DuckDB Documentation
Syntax
duckdb_logical_type duckdb_get_value_type(
duckdb_value val
);
Parameters
• val: A duckdb_value
duckdb_get_blob
Syntax
duckdb_blob duckdb_get_blob(
duckdb_value val
);
Parameters
duckdb_get_varchar
Obtains a string representation of the given value. The result must be destroyed with duckdb_free.
Syntax
char *duckdb_get_varchar(
duckdb_value value
);
Parameters
Return Value The string value. This must be destroyed with duckdb_free.
duckdb_create_struct_value
Creates a struct value from a type and an array of values. Must be destroyed with duckdb_destroy_value.
233
DuckDB Documentation
Syntax
duckdb_value duckdb_create_struct_value(
duckdb_logical_type type,
duckdb_value *values
);
Parameters
Return Value The struct value, or nullptr, if any child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_create_list_value
Creates a list value from a child (element) type and an array of values of length value_count. Must be destroyed with duckdb_
destroy_value.
Syntax
duckdb_value duckdb_create_list_value(
duckdb_logical_type type,
duckdb_value *values,
idx_t value_count
);
Parameters
Return Value The list value, or nullptr, if the child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_create_array_value
Creates an array value from a child (element) type and an array of values of length value_count. Must be destroyed with duckdb_
destroy_value.
Syntax
duckdb_value duckdb_create_array_value(
duckdb_logical_type type,
duckdb_value *values,
idx_t value_count
);
Parameters
234
DuckDB Documentation
Return Value The array value, or nullptr, if the child type is DUCKDB_TYPE_ANY or DUCKDB_TYPE_INVALID.
duckdb_get_map_size
Syntax
idx_t duckdb_get_map_size(
duckdb_value value
);
Parameters
duckdb_get_map_key
Syntax
duckdb_value duckdb_get_map_key(
duckdb_value value,
idx_t index
);
Parameters
duckdb_get_map_value
Syntax
duckdb_value duckdb_get_map_value(
duckdb_value value,
idx_t index
);
Parameters
235
DuckDB Documentation
duckdb_create_logical_type
Creates a duckdb_logical_type from a primitive type. The resulting logical type must be destroyed with duckdb_destroy_
logical_type.
Returns an invalid logical type, if type is: DUCKDB_TYPE_INVALID, DUCKDB_TYPE_DECIMAL, DUCKDB_TYPE_ENUM, DUCKDB_
TYPE_LIST, DUCKDB_TYPE_STRUCT, DUCKDB_TYPE_MAP, DUCKDB_TYPE_ARRAY, or DUCKDB_TYPE_UNION.
Syntax
duckdb_logical_type duckdb_create_logical_type(
duckdb_type type
);
Parameters
duckdb_logical_type_get_alias
Returns the alias of a duckdb_logical_type, if set, else nullptr. The result must be destroyed with duckdb_free.
Syntax
char *duckdb_logical_type_get_alias(
duckdb_logical_type type
);
Parameters
duckdb_logical_type_set_alias
Syntax
void duckdb_logical_type_set_alias(
duckdb_logical_type type,
const char *alias
);
Parameters
236
DuckDB Documentation
duckdb_create_list_type
Creates a LIST type from its child type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_list_type(
duckdb_logical_type type
);
Parameters
duckdb_create_array_type
Creates an ARRAY type from its child type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_array_type(
duckdb_logical_type type,
idx_t array_size
);
Parameters
duckdb_create_map_type
Creates a MAP type from its key type and value type. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_map_type(
duckdb_logical_type key_type,
duckdb_logical_type value_type
);
Parameters
237
DuckDB Documentation
duckdb_create_union_type
Creates a UNION type from the passed arrays. The return type must be destroyed with duckdb_destroy_logical_type.
Syntax
duckdb_logical_type duckdb_create_union_type(
duckdb_logical_type *member_types,
const char **member_names,
idx_t member_count
);
Parameters
duckdb_create_struct_type
Creates a STRUCT type based on the member types and names. The resulting type must be destroyed with duckdb_destroy_
logical_type.
Syntax
duckdb_logical_type duckdb_create_struct_type(
duckdb_logical_type *member_types,
const char **member_names,
idx_t member_count
);
Parameters
duckdb_create_enum_type
Creates an ENUM type from the passed member name array. The resulting type should be destroyed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_create_enum_type(
const char **member_names,
idx_t member_count
);
238
DuckDB Documentation
Parameters
• member_names: The array of names that the enum should consist of.
• member_count: The number of elements that were specified in the array.
duckdb_create_decimal_type
Creates a DECIMAL type with the specified width and scale. The resulting type should be destroyed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_create_decimal_type(
uint8_t width,
uint8_t scale
);
Parameters
duckdb_get_type_id
Syntax
duckdb_type duckdb_get_type_id(
duckdb_logical_type type
);
Parameters
duckdb_decimal_width
Syntax
uint8_t duckdb_decimal_width(
duckdb_logical_type type
);
239
DuckDB Documentation
Parameters
duckdb_decimal_scale
Syntax
uint8_t duckdb_decimal_scale(
duckdb_logical_type type
);
Parameters
duckdb_decimal_internal_type
Syntax
duckdb_type duckdb_decimal_internal_type(
duckdb_logical_type type
);
Parameters
duckdb_enum_internal_type
Syntax
duckdb_type duckdb_enum_internal_type(
duckdb_logical_type type
);
Parameters
240
DuckDB Documentation
duckdb_enum_dictionary_size
Syntax
uint32_t duckdb_enum_dictionary_size(
duckdb_logical_type type
);
Parameters
duckdb_enum_dictionary_value
Retrieves the dictionary value at the specified position from the enum.
Syntax
char *duckdb_enum_dictionary_value(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The string value of the enum type. Must be freed with duckdb_free.
duckdb_list_type_child_type
Retrieves the child type of the given LIST type. Also accepts MAP types. The result must be freed with duckdb_destroy_logical_
type.
Syntax
duckdb_logical_type duckdb_list_type_child_type(
duckdb_logical_type type
);
Parameters
241
DuckDB Documentation
duckdb_array_type_child_type
Syntax
duckdb_logical_type duckdb_array_type_child_type(
duckdb_logical_type type
);
Parameters
duckdb_array_type_array_size
Syntax
idx_t duckdb_array_type_array_size(
duckdb_logical_type type
);
Parameters
Return Value The fixed number of elements the values of this array type can store.
duckdb_map_type_key_type
Syntax
duckdb_logical_type duckdb_map_type_key_type(
duckdb_logical_type type
);
Parameters
242
DuckDB Documentation
Return Value The key type of the map type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_map_type_value_type
Syntax
duckdb_logical_type duckdb_map_type_value_type(
duckdb_logical_type type
);
Parameters
Return Value The value type of the map type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_struct_type_child_count
Syntax
idx_t duckdb_struct_type_child_count(
duckdb_logical_type type
);
Parameters
duckdb_struct_type_child_name
Syntax
char *duckdb_struct_type_child_name(
duckdb_logical_type type,
idx_t index
);
Parameters
243
DuckDB Documentation
Return Value The name of the struct type. Must be freed with duckdb_free.
duckdb_struct_type_child_type
Retrieves the child type of the given struct type at the specified index.
Syntax
duckdb_logical_type duckdb_struct_type_child_type(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The child type of the struct type. Must be destroyed with duckdb_destroy_logical_type.
duckdb_union_type_member_count
Syntax
idx_t duckdb_union_type_member_count(
duckdb_logical_type type
);
Parameters
duckdb_union_type_member_name
Syntax
char *duckdb_union_type_member_name(
duckdb_logical_type type,
idx_t index
);
244
DuckDB Documentation
Parameters
Return Value The name of the union member. Must be freed with duckdb_free.
duckdb_union_type_member_type
Retrieves the child type of the given union member at the specified index.
Syntax
duckdb_logical_type duckdb_union_type_member_type(
duckdb_logical_type type,
idx_t index
);
Parameters
Return Value The child type of the union member. Must be destroyed with duckdb_destroy_logical_type.
duckdb_destroy_logical_type
Destroys the logical type and de‑allocates all memory allocated for that type.
Syntax
void duckdb_destroy_logical_type(
duckdb_logical_type *type
);
Parameters
duckdb_register_logical_type
Registers a custom type within the given connection. The type must have an alias
Syntax
duckdb_state duckdb_register_logical_type(
duckdb_connection con,
duckdb_logical_type type,
duckdb_create_type_info info
);
245
DuckDB Documentation
Parameters
duckdb_create_data_chunk
Creates an empty data chunk with the specified column types. The result must be destroyed with duckdb_destroy_data_chunk.
Syntax
duckdb_data_chunk duckdb_create_data_chunk(
duckdb_logical_type *types,
idx_t column_count
);
Parameters
• types: An array of column types. Column types can not contain ANY and INVALID types.
• column_count: The number of columns.
duckdb_destroy_data_chunk
Destroys the data chunk and de‑allocates all memory allocated for that chunk.
Syntax
void duckdb_destroy_data_chunk(
duckdb_data_chunk *chunk
);
Parameters
duckdb_data_chunk_reset
Resets a data chunk, clearing the validity masks and setting the cardinality of the data chunk to 0. After calling this method, you must call
duckdb_vector_get_validity and duckdb_vector_get_data to obtain current data and validity pointers
Syntax
void duckdb_data_chunk_reset(
duckdb_data_chunk chunk
);
Parameters
246
DuckDB Documentation
duckdb_data_chunk_get_column_count
Syntax
idx_t duckdb_data_chunk_get_column_count(
duckdb_data_chunk chunk
);
Parameters
duckdb_data_chunk_get_vector
Retrieves the vector at the specified column index in the data chunk.
The pointer to the vector is valid for as long as the chunk is alive. It does NOT need to be destroyed.
Syntax
duckdb_vector duckdb_data_chunk_get_vector(
duckdb_data_chunk chunk,
idx_t col_idx
);
Parameters
duckdb_data_chunk_get_size
Syntax
idx_t duckdb_data_chunk_get_size(
duckdb_data_chunk chunk
);
Parameters
247
DuckDB Documentation
duckdb_data_chunk_set_size
Syntax
void duckdb_data_chunk_set_size(
duckdb_data_chunk chunk,
idx_t size
);
Parameters
duckdb_vector_get_column_type
Syntax
duckdb_logical_type duckdb_vector_get_column_type(
duckdb_vector vector
);
Parameters
duckdb_vector_get_data
The data pointer can be used to read or write values from the vector. How to read or write values depends on the type of the vector.
Syntax
void *duckdb_vector_get_data(
duckdb_vector vector
);
Parameters
248
DuckDB Documentation
duckdb_vector_get_validity
The validity mask is a bitset that signifies null‑ness within the data chunk. It is a series of uint64_t values, where each uint64_t value contains
validity for 64 tuples. The bit is set to 1 if the value is valid (i.e., not NULL) or 0 if the value is invalid (i.e., NULL).
idx_t entry_idx = row_idx / 64; idx_t idx_in_entry = row_idx % 64; bool is_valid = validity_mask[entry_idx] & (1 « idx_in_entry);
Syntax
uint64_t *duckdb_vector_get_validity(
duckdb_vector vector
);
Parameters
Return Value The pointer to the validity mask, or NULL if no validity mask is present
duckdb_vector_ensure_validity_writable
After this function is called, duckdb_vector_get_validity will ALWAYS return non‑NULL. This allows null values to be written to the
vector, regardless of whether a validity mask was present before.
Syntax
void duckdb_vector_ensure_validity_writable(
duckdb_vector vector
);
Parameters
duckdb_vector_assign_string_element
Syntax
void duckdb_vector_assign_string_element(
duckdb_vector vector,
idx_t index,
const char *str
);
249
DuckDB Documentation
Parameters
duckdb_vector_assign_string_element_len
Assigns a string element in the vector at the specified location. You may also use this function to assign BLOBs.
Syntax
void duckdb_vector_assign_string_element_len(
duckdb_vector vector,
idx_t index,
const char *str,
idx_t str_len
);
Parameters
duckdb_list_vector_get_child
Syntax
duckdb_vector duckdb_list_vector_get_child(
duckdb_vector vector
);
Parameters
duckdb_list_vector_get_size
Syntax
idx_t duckdb_list_vector_get_size(
duckdb_vector vector
);
250
DuckDB Documentation
Parameters
duckdb_list_vector_set_size
Syntax
duckdb_state duckdb_list_vector_set_size(
duckdb_vector vector,
idx_t size
);
Parameters
Return Value The duckdb state. Returns DuckDBError if the vector is nullptr.
duckdb_list_vector_reserve
After calling this method, you must call duckdb_vector_get_validity and duckdb_vector_get_data to obtain current data
and validity pointers
Syntax
duckdb_state duckdb_list_vector_reserve(
duckdb_vector vector,
idx_t required_capacity
);
Parameters
Return Value The duckdb state. Returns DuckDBError if the vector is nullptr.
duckdb_struct_vector_get_child
251
DuckDB Documentation
Syntax
duckdb_vector duckdb_struct_vector_get_child(
duckdb_vector vector,
idx_t index
);
Parameters
duckdb_array_vector_get_child
The resulting vector is valid as long as the parent vector is valid. The resulting vector has the size of the parent vector multiplied by the
array size.
Syntax
duckdb_vector duckdb_array_vector_get_child(
duckdb_vector vector
);
Parameters
duckdb_validity_row_is_valid
Returns whether or not a row is valid (i.e., not NULL) in the given validity mask.
Syntax
bool duckdb_validity_row_is_valid(
uint64_t *validity,
idx_t row
);
Parameters
252
DuckDB Documentation
duckdb_validity_set_row_validity
Syntax
void duckdb_validity_set_row_validity(
uint64_t *validity,
idx_t row,
bool valid
);
Parameters
duckdb_validity_set_row_invalid
Syntax
void duckdb_validity_set_row_invalid(
uint64_t *validity,
idx_t row
);
Parameters
duckdb_validity_set_row_valid
Syntax
void duckdb_validity_set_row_valid(
uint64_t *validity,
idx_t row
);
Parameters
253
DuckDB Documentation
duckdb_create_scalar_function
Syntax
duckdb_scalar_function duckdb_create_scalar_function(
);
duckdb_destroy_scalar_function
Syntax
void duckdb_destroy_scalar_function(
duckdb_scalar_function *scalar_function
);
Parameters
duckdb_scalar_function_set_name
Syntax
void duckdb_scalar_function_set_name(
duckdb_scalar_function scalar_function,
const char *name
);
Parameters
duckdb_scalar_function_set_varargs
Sets the parameters of the given scalar function to varargs. Does not require adding parameters with duckdb_scalar_function_add_
parameter.
254
DuckDB Documentation
Syntax
void duckdb_scalar_function_set_varargs(
duckdb_scalar_function scalar_function,
duckdb_logical_type type
);
Parameters
duckdb_scalar_function_set_special_handling
Sets the parameters of the given scalar function to varargs. Does not require adding parameters with duckdb_scalar_function_add_
parameter.
Syntax
void duckdb_scalar_function_set_special_handling(
duckdb_scalar_function scalar_function
);
Parameters
duckdb_scalar_function_set_volatile
Sets the Function Stability of the scalar function to VOLATILE, indicating the function should be re‑run for every row. This limits optimization
that can be performed for the function.
Syntax
void duckdb_scalar_function_set_volatile(
duckdb_scalar_function scalar_function
);
Parameters
duckdb_scalar_function_add_parameter
Syntax
void duckdb_scalar_function_add_parameter(
duckdb_scalar_function scalar_function,
duckdb_logical_type type
);
255
DuckDB Documentation
Parameters
duckdb_scalar_function_set_return_type
Syntax
void duckdb_scalar_function_set_return_type(
duckdb_scalar_function scalar_function,
duckdb_logical_type type
);
Parameters
duckdb_scalar_function_set_extra_info
Assigns extra information to the scalar function that can be fetched during binding, etc.
Syntax
void duckdb_scalar_function_set_extra_info(
duckdb_scalar_function scalar_function,
void *extra_info,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_scalar_function_set_function
Syntax
void duckdb_scalar_function_set_function(
duckdb_scalar_function scalar_function,
duckdb_scalar_function_t function
);
Parameters
256
DuckDB Documentation
duckdb_register_scalar_function
If the function is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_scalar_function(
duckdb_connection con,
duckdb_scalar_function scalar_function
);
Parameters
duckdb_scalar_function_get_extra_info
Syntax
void *duckdb_scalar_function_get_extra_info(
duckdb_function_info info
);
Parameters
duckdb_scalar_function_set_error
Report that an error has occurred while executing the scalar function.
Syntax
void duckdb_scalar_function_set_error(
duckdb_function_info info,
const char *error
);
Parameters
257
DuckDB Documentation
duckdb_create_scalar_function_set
Syntax
duckdb_scalar_function_set duckdb_create_scalar_function_set(
const char *name
);
duckdb_destroy_scalar_function_set
Syntax
void duckdb_destroy_scalar_function_set(
duckdb_scalar_function_set *scalar_function_set
);
duckdb_add_scalar_function_to_set
Adds the scalar function as a new overload to the scalar function set.
Returns DuckDBError if the function could not be added, for example if the overload already exists.
Syntax
duckdb_state duckdb_add_scalar_function_to_set(
duckdb_scalar_function_set set,
duckdb_scalar_function function
);
Parameters
duckdb_register_scalar_function_set
If the set is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_scalar_function_set(
duckdb_connection con,
duckdb_scalar_function_set set
);
258
DuckDB Documentation
Parameters
duckdb_create_aggregate_function
Syntax
duckdb_aggregate_function duckdb_create_aggregate_function(
);
duckdb_destroy_aggregate_function
Syntax
void duckdb_destroy_aggregate_function(
duckdb_aggregate_function *aggregate_function
);
duckdb_aggregate_function_set_name
Syntax
void duckdb_aggregate_function_set_name(
duckdb_aggregate_function aggregate_function,
const char *name
);
Parameters
duckdb_aggregate_function_add_parameter
259
DuckDB Documentation
Syntax
void duckdb_aggregate_function_add_parameter(
duckdb_aggregate_function aggregate_function,
duckdb_logical_type type
);
Parameters
duckdb_aggregate_function_set_return_type
Syntax
void duckdb_aggregate_function_set_return_type(
duckdb_aggregate_function aggregate_function,
duckdb_logical_type type
);
Parameters
duckdb_aggregate_function_set_functions
Syntax
void duckdb_aggregate_function_set_functions(
duckdb_aggregate_function aggregate_function,
duckdb_aggregate_state_size state_size,
duckdb_aggregate_init_t state_init,
duckdb_aggregate_update_t update,
duckdb_aggregate_combine_t combine,
duckdb_aggregate_finalize_t finalize
);
Parameters
duckdb_aggregate_function_set_destructor
260
DuckDB Documentation
Syntax
void duckdb_aggregate_function_set_destructor(
duckdb_aggregate_function aggregate_function,
duckdb_aggregate_destroy_t destroy
);
Parameters
duckdb_register_aggregate_function
If the function is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_aggregate_function(
duckdb_connection con,
duckdb_aggregate_function aggregate_function
);
Parameters
duckdb_aggregate_function_set_special_handling
Syntax
void duckdb_aggregate_function_set_special_handling(
duckdb_aggregate_function aggregate_function
);
Parameters
duckdb_aggregate_function_set_extra_info
Assigns extra information to the scalar function that can be fetched during binding, etc.
261
DuckDB Documentation
Syntax
void duckdb_aggregate_function_set_extra_info(
duckdb_aggregate_function aggregate_function,
void *extra_info,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_aggregate_function_get_extra_info
Syntax
void *duckdb_aggregate_function_get_extra_info(
duckdb_function_info info
);
Parameters
duckdb_aggregate_function_set_error
Report that an error has occurred while executing the aggregate function.
Syntax
void duckdb_aggregate_function_set_error(
duckdb_function_info info,
const char *error
);
Parameters
duckdb_create_aggregate_function_set
262
DuckDB Documentation
Syntax
duckdb_aggregate_function_set duckdb_create_aggregate_function_set(
const char *name
);
duckdb_destroy_aggregate_function_set
Syntax
void duckdb_destroy_aggregate_function_set(
duckdb_aggregate_function_set *aggregate_function_set
);
duckdb_add_aggregate_function_to_set
Adds the aggregate function as a new overload to the aggregate function set.
Returns DuckDBError if the function could not be added, for example if the overload already exists.
Syntax
duckdb_state duckdb_add_aggregate_function_to_set(
duckdb_aggregate_function_set set,
duckdb_aggregate_function function
);
Parameters
duckdb_register_aggregate_function_set
If the set is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_aggregate_function_set(
duckdb_connection con,
duckdb_aggregate_function_set set
);
Parameters
263
DuckDB Documentation
duckdb_create_table_function
Syntax
duckdb_table_function duckdb_create_table_function(
);
duckdb_destroy_table_function
Syntax
void duckdb_destroy_table_function(
duckdb_table_function *table_function
);
Parameters
duckdb_table_function_set_name
Syntax
void duckdb_table_function_set_name(
duckdb_table_function table_function,
const char *name
);
Parameters
duckdb_table_function_add_parameter
Syntax
void duckdb_table_function_add_parameter(
duckdb_table_function table_function,
duckdb_logical_type type
);
264
DuckDB Documentation
Parameters
duckdb_table_function_add_named_parameter
Syntax
void duckdb_table_function_add_named_parameter(
duckdb_table_function table_function,
const char *name,
duckdb_logical_type type
);
Parameters
duckdb_table_function_set_extra_info
Assigns extra information to the table function that can be fetched during binding, etc.
Syntax
void duckdb_table_function_set_extra_info(
duckdb_table_function table_function,
void *extra_info,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_table_function_set_bind
Syntax
void duckdb_table_function_set_bind(
duckdb_table_function table_function,
duckdb_table_function_bind_t bind
);
265
DuckDB Documentation
Parameters
duckdb_table_function_set_init
Syntax
void duckdb_table_function_set_init(
duckdb_table_function table_function,
duckdb_table_function_init_t init
);
Parameters
duckdb_table_function_set_local_init
Syntax
void duckdb_table_function_set_local_init(
duckdb_table_function table_function,
duckdb_table_function_init_t init
);
Parameters
duckdb_table_function_set_function
Syntax
void duckdb_table_function_set_function(
duckdb_table_function table_function,
duckdb_table_function_t function
);
Parameters
266
DuckDB Documentation
duckdb_table_function_supports_projection_pushdown
Sets whether or not the given table function supports projection pushdown.
If this is set to true, the system will provide a list of all required columns in the init stage through the duckdb_init_get_column_
count and duckdb_init_get_column_index functions. If this is set to false (the default), the system will expect all columns to be
projected.
Syntax
void duckdb_table_function_supports_projection_pushdown(
duckdb_table_function table_function,
bool pushdown
);
Parameters
duckdb_register_table_function
The function requires at least a name, a bind function, an init function and a main function.
If the function is incomplete or a function with this name already exists DuckDBError is returned.
Syntax
duckdb_state duckdb_register_table_function(
duckdb_connection con,
duckdb_table_function function
);
Parameters
duckdb_bind_get_extra_info
Syntax
void *duckdb_bind_get_extra_info(
duckdb_bind_info info
);
Parameters
267
DuckDB Documentation
duckdb_bind_add_result_column
Syntax
void duckdb_bind_add_result_column(
duckdb_bind_info info,
const char *name,
duckdb_logical_type type
);
Parameters
duckdb_bind_get_parameter_count
Syntax
idx_t duckdb_bind_get_parameter_count(
duckdb_bind_info info
);
Parameters
duckdb_bind_get_parameter
Syntax
duckdb_value duckdb_bind_get_parameter(
duckdb_bind_info info,
idx_t index
);
Parameters
268
DuckDB Documentation
Return Value The value of the parameter. Must be destroyed with duckdb_destroy_value.
duckdb_bind_get_named_parameter
Syntax
duckdb_value duckdb_bind_get_named_parameter(
duckdb_bind_info info,
const char *name
);
Parameters
Return Value The value of the parameter. Must be destroyed with duckdb_destroy_value.
duckdb_bind_set_bind_data
Sets the user‑provided bind data in the bind object. This object can be retrieved again during execution.
Syntax
void duckdb_bind_set_bind_data(
duckdb_bind_info info,
void *bind_data,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_bind_set_cardinality
Sets the cardinality estimate for the table function, used for optimization.
Syntax
void duckdb_bind_set_cardinality(
duckdb_bind_info info,
idx_t cardinality,
bool is_exact
);
269
DuckDB Documentation
Parameters
duckdb_bind_set_error
Syntax
void duckdb_bind_set_error(
duckdb_bind_info info,
const char *error
);
Parameters
duckdb_init_get_extra_info
Syntax
void *duckdb_init_get_extra_info(
duckdb_init_info info
);
Parameters
duckdb_init_get_bind_data
Note that the bind data should be considered as read‑only. For tracking state, use the init data instead.
Syntax
void *duckdb_init_get_bind_data(
duckdb_init_info info
);
Parameters
270
DuckDB Documentation
duckdb_init_set_init_data
Sets the user‑provided init data in the init object. This object can be retrieved again during execution.
Syntax
void duckdb_init_set_init_data(
duckdb_init_info info,
void *init_data,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_init_get_column_count
This function must be used if projection pushdown is enabled to figure out which columns to emit.
Syntax
idx_t duckdb_init_get_column_count(
duckdb_init_info info
);
Parameters
duckdb_init_get_column_index
Returns the column index of the projected column at the specified position.
This function must be used if projection pushdown is enabled to figure out which columns to emit.
Syntax
idx_t duckdb_init_get_column_index(
duckdb_init_info info,
idx_t column_index
);
271
DuckDB Documentation
Parameters
duckdb_init_set_max_threads
Sets how many threads can process this table function in parallel (default: 1)
Syntax
void duckdb_init_set_max_threads(
duckdb_init_info info,
idx_t max_threads
);
Parameters
duckdb_init_set_error
Syntax
void duckdb_init_set_error(
duckdb_init_info info,
const char *error
);
Parameters
duckdb_function_get_extra_info
Syntax
void *duckdb_function_get_extra_info(
duckdb_function_info info
);
Parameters
272
DuckDB Documentation
duckdb_function_get_bind_data
Note that the bind data should be considered as read‑only. For tracking state, use the init data instead.
Syntax
void *duckdb_function_get_bind_data(
duckdb_function_info info
);
Parameters
duckdb_function_get_init_data
Syntax
void *duckdb_function_get_init_data(
duckdb_function_info info
);
Parameters
duckdb_function_get_local_init_data
Gets the thread‑local init data set by duckdb_init_set_init_data during the local_init.
Syntax
void *duckdb_function_get_local_init_data(
duckdb_function_info info
);
Parameters
273
DuckDB Documentation
duckdb_function_set_error
Syntax
void duckdb_function_set_error(
duckdb_function_info info,
const char *error
);
Parameters
duckdb_add_replacement_scan
Syntax
void duckdb_add_replacement_scan(
duckdb_database db,
duckdb_replacement_callback_t replacement,
void *extra_data,
duckdb_delete_callback_t delete_callback
);
Parameters
duckdb_replacement_scan_set_function_name
Sets the replacement function name. If this function is called in the replacement callback, the replacement scan is performed. If it is not
called, the replacement callback is not performed.
Syntax
void duckdb_replacement_scan_set_function_name(
duckdb_replacement_scan_info info,
const char *function_name
);
Parameters
274
DuckDB Documentation
duckdb_replacement_scan_add_parameter
Syntax
void duckdb_replacement_scan_add_parameter(
duckdb_replacement_scan_info info,
duckdb_value parameter
);
Parameters
duckdb_replacement_scan_set_error
Report that an error has occurred while executing the replacement scan.
Syntax
void duckdb_replacement_scan_set_error(
duckdb_replacement_scan_info info,
const char *error
);
Parameters
duckdb_get_profiling_info
Returns the root node of the profiling information. Returns nullptr, if profiling is not enabled.
Syntax
duckdb_profiling_info duckdb_get_profiling_info(
duckdb_connection connection
);
Parameters
duckdb_profiling_info_get_value
Returns the value of the metric of the current profiling info node. Returns nullptr, if the metric does not exist or is not enabled. Currently,
the value holds a string, and you can retrieve the string by calling the corresponding function: char *duckdb_get_varchar(duckdb_value
value).
275
DuckDB Documentation
Syntax
duckdb_value duckdb_profiling_info_get_value(
duckdb_profiling_info info,
const char *key
);
Parameters
Return Value The value of the metric. Must be freed with duckdb_destroy_value
duckdb_profiling_info_get_metrics
Returns the key‑value metric map of this profiling node as a MAP duckdb_value. The individual elements are accessible via the duckdb_
value MAP functions.
Syntax
duckdb_value duckdb_profiling_info_get_metrics(
duckdb_profiling_info info
);
Parameters
duckdb_profiling_info_get_child_count
Syntax
idx_t duckdb_profiling_info_get_child_count(
duckdb_profiling_info info
);
Parameters
duckdb_profiling_info_get_child
276
DuckDB Documentation
Syntax
duckdb_profiling_info duckdb_profiling_info_get_child(
duckdb_profiling_info info,
idx_t index
);
Parameters
duckdb_appender_create
Syntax
duckdb_state duckdb_appender_create(
duckdb_connection connection,
const char *schema,
const char *table,
duckdb_appender *out_appender
);
Parameters
duckdb_appender_column_count
Returns the number of columns in the table that belongs to the appender.
Syntax
idx_t duckdb_appender_column_count(
duckdb_appender appender
);
Parameters
277
DuckDB Documentation
duckdb_appender_column_type
Syntax
duckdb_logical_type duckdb_appender_column_type(
duckdb_appender appender,
idx_t col_idx
);
Parameters
duckdb_appender_error
Returns the error message associated with the given appender. If the appender has no error message, this returns nullptr instead.
The error message should not be freed. It will be de‑allocated when duckdb_appender_destroy is called.
Syntax
const char *duckdb_appender_error(
duckdb_appender appender
);
Parameters
duckdb_appender_flush
Flush the appender to the table, forcing the cache of the appender to be cleared. If flushing the data triggers a constraint violation or any
other error, then all data is invalidated, and this function returns DuckDBError. It is not possible to append more values. Call duckdb_
appender_error to obtain the error message followed by duckdb_appender_destroy to destroy the invalidated appender.
Syntax
duckdb_state duckdb_appender_flush(
duckdb_appender appender
);
Parameters
278
DuckDB Documentation
duckdb_appender_close
Closes the appender by flushing all intermediate states and closing it for further appends. If flushing the data triggers a constraint violation
or any other error, then all data is invalidated, and this function returns DuckDBError. Call duckdb_appender_error to obtain the error
message followed by duckdb_appender_destroy to destroy the invalidated appender.
Syntax
duckdb_state duckdb_appender_close(
duckdb_appender appender
);
Parameters
duckdb_appender_destroy
Closes the appender by flushing all intermediate states to the table and destroying it. By destroying it, this function de‑allocates all memory
associated with the appender. If flushing the data triggers a constraint violation, then all data is invalidated, and this function returns
DuckDBError. Due to the destruction of the appender, it is no longer possible to obtain the specific error message with duckdb_appender_
error. Therefore, call duckdb_appender_close before destroying the appender, if you need insights into the specific error.
Syntax
duckdb_state duckdb_appender_destroy(
duckdb_appender *appender
);
Parameters
duckdb_appender_begin_row
A nop function, provided for backwards compatibility reasons. Does nothing. Only duckdb_appender_end_row is required.
Syntax
duckdb_state duckdb_appender_begin_row(
duckdb_appender appender
);
duckdb_appender_end_row
Finish the current row of appends. After end_row is called, the next row can be appended.
279
DuckDB Documentation
Syntax
duckdb_state duckdb_appender_end_row(
duckdb_appender appender
);
Parameters
duckdb_append_default
Append a DEFAULT value (NULL if DEFAULT not available for column) to the appender.
Syntax
duckdb_state duckdb_append_default(
duckdb_appender appender
);
duckdb_append_bool
Syntax
duckdb_state duckdb_append_bool(
duckdb_appender appender,
bool value
);
duckdb_append_int8
Syntax
duckdb_state duckdb_append_int8(
duckdb_appender appender,
int8_t value
);
duckdb_append_int16
Syntax
duckdb_state duckdb_append_int16(
duckdb_appender appender,
int16_t value
);
280
DuckDB Documentation
duckdb_append_int32
Syntax
duckdb_state duckdb_append_int32(
duckdb_appender appender,
int32_t value
);
duckdb_append_int64
Syntax
duckdb_state duckdb_append_int64(
duckdb_appender appender,
int64_t value
);
duckdb_append_hugeint
Syntax
duckdb_state duckdb_append_hugeint(
duckdb_appender appender,
duckdb_hugeint value
);
duckdb_append_uint8
Syntax
duckdb_state duckdb_append_uint8(
duckdb_appender appender,
uint8_t value
);
duckdb_append_uint16
Syntax
duckdb_state duckdb_append_uint16(
duckdb_appender appender,
uint16_t value
);
281
DuckDB Documentation
duckdb_append_uint32
Syntax
duckdb_state duckdb_append_uint32(
duckdb_appender appender,
uint32_t value
);
duckdb_append_uint64
Syntax
duckdb_state duckdb_append_uint64(
duckdb_appender appender,
uint64_t value
);
duckdb_append_uhugeint
Syntax
duckdb_state duckdb_append_uhugeint(
duckdb_appender appender,
duckdb_uhugeint value
);
duckdb_append_float
Syntax
duckdb_state duckdb_append_float(
duckdb_appender appender,
float value
);
duckdb_append_double
Syntax
duckdb_state duckdb_append_double(
duckdb_appender appender,
double value
);
282
DuckDB Documentation
duckdb_append_date
Syntax
duckdb_state duckdb_append_date(
duckdb_appender appender,
duckdb_date value
);
duckdb_append_time
Syntax
duckdb_state duckdb_append_time(
duckdb_appender appender,
duckdb_time value
);
duckdb_append_timestamp
Syntax
duckdb_state duckdb_append_timestamp(
duckdb_appender appender,
duckdb_timestamp value
);
duckdb_append_interval
Syntax
duckdb_state duckdb_append_interval(
duckdb_appender appender,
duckdb_interval value
);
duckdb_append_varchar
Syntax
duckdb_state duckdb_append_varchar(
duckdb_appender appender,
const char *val
);
283
DuckDB Documentation
duckdb_append_varchar_length
Syntax
duckdb_state duckdb_append_varchar_length(
duckdb_appender appender,
const char *val,
idx_t length
);
duckdb_append_blob
Syntax
duckdb_state duckdb_append_blob(
duckdb_appender appender,
const void *data,
idx_t length
);
duckdb_append_null
Syntax
duckdb_state duckdb_append_null(
duckdb_appender appender
);
duckdb_append_data_chunk
The types of the data chunk must exactly match the types of the table, no casting is performed. If the types do not match or the appender
is in an invalid state, DuckDBError is returned. If the append is successful, DuckDBSuccess is returned.
Syntax
duckdb_state duckdb_append_data_chunk(
duckdb_appender appender,
duckdb_data_chunk chunk
);
Parameters
284
DuckDB Documentation
duckdb_table_description_create
Creates a table description object. Note that duckdb_table_description_destroy should always be called on the resulting table_
description, even if the function returns DuckDBError.
Syntax
duckdb_state duckdb_table_description_create(
duckdb_connection connection,
const char *schema,
const char *table,
duckdb_table_description *out
);
Parameters
duckdb_table_description_destroy
Syntax
void duckdb_table_description_destroy(
duckdb_table_description *table_description
);
Parameters
duckdb_table_description_error
Returns the error message associated with the given table_description. If the table_description has no error message, this returns
nullptr instead. The error message should not be freed. It will be de‑allocated when duckdb_table_description_destroy is
called.
Syntax
Parameters
285
DuckDB Documentation
duckdb_column_has_default
Check if the column at 'index' index of the table has a DEFAULT expression.
Syntax
duckdb_state duckdb_column_has_default(
duckdb_table_description table_description,
idx_t index,
bool *out
);
Parameters
duckdb_query_arrow
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Executes a SQL query within a connection and stores the full (materialized) result in an arrow structure. If the query fails to execute, Duck‑
DBError is returned and the error message can be retrieved by calling duckdb_query_arrow_error.
Note that after running duckdb_query_arrow, duckdb_destroy_arrow must be called on the result object even if the query fails,
otherwise the error stored within the result will not be freed correctly.
Syntax
duckdb_state duckdb_query_arrow(
duckdb_connection connection,
const char *query,
duckdb_arrow *out_result
);
Parameters
duckdb_query_arrow_schema
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetch the internal arrow schema from the arrow result. Remember to call release on the respective ArrowSchema object.
286
DuckDB Documentation
Syntax
duckdb_state duckdb_query_arrow_schema(
duckdb_arrow result,
duckdb_arrow_schema *out_schema
);
Parameters
duckdb_prepared_arrow_schema
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetch the internal arrow schema from the prepared statement. Remember to call release on the respective ArrowSchema object.
Syntax
duckdb_state duckdb_prepared_arrow_schema(
duckdb_prepared_statement prepared,
duckdb_arrow_schema *out_schema
);
Parameters
duckdb_result_arrow_array
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Convert a data chunk into an arrow struct array. Remember to call release on the respective ArrowArray object.
Syntax
void duckdb_result_arrow_array(
duckdb_result result,
duckdb_data_chunk chunk,
duckdb_arrow_array *out_array
);
Parameters
• result: The result object the data chunk have been fetched from.
• chunk: The data chunk to convert.
• out_array: The output array.
287
DuckDB Documentation
duckdb_query_arrow_array
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetch an internal arrow struct array from the arrow result. Remember to call release on the respective ArrowArray object.
This function can be called multiple time to get next chunks, which will free the previous out_array. So consume the out_array before
calling this function again.
Syntax
duckdb_state duckdb_query_arrow_array(
duckdb_arrow result,
duckdb_arrow_array *out_array
);
Parameters
duckdb_arrow_column_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_arrow_column_count(
duckdb_arrow result
);
Parameters
duckdb_arrow_row_count
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
idx_t duckdb_arrow_row_count(
duckdb_arrow result
);
288
DuckDB Documentation
Parameters
duckdb_arrow_rows_changed
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Returns the number of rows changed by the query stored in the arrow result. This is relevant only for INSERT/UPDATE/DELETE queries. For
other queries the rows_changed will be 0.
Syntax
idx_t duckdb_arrow_rows_changed(
duckdb_arrow result
);
Parameters
duckdb_query_arrow_error
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Returns the error message contained within the result. The error is only set if duckdb_query_arrow returns DuckDBError.
The error message should not be freed. It will be de‑allocated when duckdb_destroy_arrow is called.
Syntax
Parameters
duckdb_destroy_arrow
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Closes the result and de‑allocates all memory allocated for the arrow result.
289
DuckDB Documentation
Syntax
void duckdb_destroy_arrow(
duckdb_arrow *result
);
Parameters
duckdb_destroy_arrow_stream
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Syntax
void duckdb_destroy_arrow_stream(
duckdb_arrow_stream *stream_p
);
Parameters
duckdb_execute_prepared_arrow
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Executes the prepared statement with the given bound parameters, and returns an arrow query result. Note that after running duckdb_
execute_prepared_arrow, duckdb_destroy_arrow must be called on the result object.
Syntax
duckdb_state duckdb_execute_prepared_arrow(
duckdb_prepared_statement prepared_statement,
duckdb_arrow *out_result
);
Parameters
duckdb_arrow_scan
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Scans the Arrow stream and creates a view with the given name.
290
DuckDB Documentation
Syntax
duckdb_state duckdb_arrow_scan(
duckdb_connection connection,
const char *table_name,
duckdb_arrow_stream arrow
);
Parameters
duckdb_arrow_array_scan
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Scans the Arrow array and creates a view with the given name. Note that after running duckdb_arrow_array_scan, duckdb_
destroy_arrow_stream must be called on the out stream.
Syntax
duckdb_state duckdb_arrow_array_scan(
duckdb_connection connection,
const char *table_name,
duckdb_arrow_schema arrow_schema,
duckdb_arrow_array arrow_array,
duckdb_arrow_stream *out_stream
);
Parameters
duckdb_execute_tasks
Will return after max_tasks have been executed, or if there are no more tasks present.
Syntax
void duckdb_execute_tasks(
duckdb_database database,
idx_t max_tasks
);
291
DuckDB Documentation
Parameters
duckdb_create_task_state
Creates a task state that can be used with duckdb_execute_tasks_state to execute tasks until duckdb_finish_execution is called on
the state.
Syntax
duckdb_task_state duckdb_create_task_state(
duckdb_database database
);
Parameters
Return Value The task state that can be used with duckdb_execute_tasks_state.
duckdb_execute_tasks_state
The thread will keep on executing tasks forever, until duckdb_finish_execution is called on the state. Multiple threads can share the same
duckdb_task_state.
Syntax
void duckdb_execute_tasks_state(
duckdb_task_state state
);
Parameters
duckdb_execute_n_tasks_state
The thread will keep on executing tasks until either duckdb_finish_execution is called on the state, max_tasks tasks have been executed or
there are no more tasks to be executed.
Syntax
idx_t duckdb_execute_n_tasks_state(
duckdb_task_state state,
idx_t max_tasks
);
292
DuckDB Documentation
Parameters
Return Value The amount of tasks that have actually been executed
duckdb_finish_execution
Syntax
void duckdb_finish_execution(
duckdb_task_state state
);
Parameters
duckdb_task_state_is_finished
Syntax
bool duckdb_task_state_is_finished(
duckdb_task_state state
);
Parameters
Return Value Whether or not duckdb_finish_execution has been called on the task state
duckdb_destroy_task_state
Note that this should not be called while there is an active duckdb_execute_tasks_state running on the task state.
Syntax
void duckdb_destroy_task_state(
duckdb_task_state state
);
Parameters
293
DuckDB Documentation
duckdb_execution_is_finished
Syntax
bool duckdb_execution_is_finished(
duckdb_connection con
);
Parameters
duckdb_stream_fetch_chunk
Warning. Deprecation notice. This method is scheduled for removal in a future release.
Fetches a data chunk from the (streaming) duckdb_result. This function should be called repeatedly until the result is exhausted.
If this function is used, none of the other result functions can be used and vice versa (i.e., this function cannot be mixed with the legacy
result functions or the materialized result functions).
It is not known beforehand how many chunks will be returned by this result.
Syntax
duckdb_data_chunk duckdb_stream_fetch_chunk(
duckdb_result result
);
Parameters
Return Value The resulting data chunk. Returns NULL if the result has an error.
duckdb_fetch_chunk
Fetches a data chunk from a duckdb_result. This function should be called repeatedly until the result is exhausted.
It is not known beforehand how many chunks will be returned by this result.
Syntax
duckdb_data_chunk duckdb_fetch_chunk(
duckdb_result result
);
294
DuckDB Documentation
Parameters
Return Value The resulting data chunk. Returns NULL if the result has an error.
duckdb_create_cast_function
Syntax
duckdb_cast_function duckdb_create_cast_function(
);
duckdb_cast_function_set_source_type
Syntax
void duckdb_cast_function_set_source_type(
duckdb_cast_function cast_function,
duckdb_logical_type source_type
);
Parameters
duckdb_cast_function_set_target_type
Syntax
void duckdb_cast_function_set_target_type(
duckdb_cast_function cast_function,
duckdb_logical_type target_type
);
Parameters
duckdb_cast_function_set_implicit_cast_cost
Sets the ”cost” of implicitly casting the source type to the target type using this function.
295
DuckDB Documentation
Syntax
void duckdb_cast_function_set_implicit_cast_cost(
duckdb_cast_function cast_function,
int64_t cost
);
Parameters
duckdb_cast_function_set_function
Syntax
void duckdb_cast_function_set_function(
duckdb_cast_function cast_function,
duckdb_cast_function_t function
);
Parameters
duckdb_cast_function_set_extra_info
Assigns extra information to the cast function that can be fetched during execution, etc.
Syntax
void duckdb_cast_function_set_extra_info(
duckdb_cast_function cast_function,
void *extra_info,
duckdb_delete_callback_t destroy
);
Parameters
duckdb_cast_function_get_extra_info
Syntax
void *duckdb_cast_function_get_extra_info(
duckdb_function_info info
);
296
DuckDB Documentation
Parameters
duckdb_cast_function_get_cast_mode
Get the cast execution mode from the given function info.
Syntax
duckdb_cast_mode duckdb_cast_function_get_cast_mode(
duckdb_function_info info
);
Parameters
duckdb_cast_function_set_error
Report that an error has occurred while executing the cast function.
Syntax
void duckdb_cast_function_set_error(
duckdb_function_info info,
const char *error
);
Parameters
duckdb_cast_function_set_row_error
Report that an error has occurred while executing the cast function, setting the corresponding output row to NULL.
Syntax
void duckdb_cast_function_set_row_error(
duckdb_function_info info,
const char *error,
idx_t row,
duckdb_vector output
);
297
DuckDB Documentation
Parameters
duckdb_register_cast_function
Syntax
duckdb_state duckdb_register_cast_function(
duckdb_connection con,
duckdb_cast_function cast_function
);
Parameters
duckdb_destroy_cast_function
Syntax
void duckdb_destroy_cast_function(
duckdb_cast_function *cast_function
);
Parameters
298
C++ API
Warning. DuckDB's C++ API is internal. It is not guaranteed to be stable and can change without notice. If you would like to build an
application on DuckDB, we recommend using the C API.
Installation
The DuckDB C++ API can be installed as part of the libduckdb packages. Please see the installation page for details.
DuckDB implements a custom C++ API. This is built around the abstractions of a database instance (DuckDB class), multiple Connections
to the database instance and QueryResult instances as the result of queries. The header file for the C++ API is duckdb.hpp.
To use DuckDB, you must first initialize a DuckDB instance using its constructor. DuckDB() takes as parameter the database file to read
and write from. The special value nullptr can be used to create an in‑memory database. Note that for an in‑memory database no
data is persisted to disk (i.e., all data is lost when you exit the process). The second parameter to the DuckDB constructor is an optional
DBConfig object. In DBConfig, you can set various database parameters, for example the read/write mode or memory limits. The
DuckDB constructor may throw exceptions, for example if the database file is not usable.
With the DuckDB instance, you can create one or many Connection instances using the Connection() constructor. While connections
should be thread‑safe, they will be locked during querying. It is therefore recommended that each thread uses its own connection if you
are in a multithreaded environment.
DuckDB db(nullptr);
Connection con(db);
Querying
Connections expose the Query() method to send a SQL query string to DuckDB from C++. Query() fully materializes the query result as
a MaterializedQueryResult in memory before returning at which point the query result can be consumed. There is also a streaming
API for queries, see further below.
// create a table
con.Query("CREATE TABLE integers (i INTEGER, j INTEGER)");
299
DuckDB Documentation
The MaterializedQueryResult instance contains firstly two fields that indicate whether the query was successful. Query will not
throw exceptions under normal circumstances. Instead, invalid queries or other issues will lead to the success Boolean field in the query
result instance to be set to false. In this case an error message may be available in error as a string. If successful, other fields are set:
the type of statement that was just executed (e.g., StatementType::INSERT_STATEMENT) is contained in statement_type. The
high‑level (“Logical type”/“SQL type”) types of the result set columns are in types. The names of the result columns are in the names
string vector. In case multiple result sets are returned, for example because the result set contained multiple statements, the result set can
be chained using the next field.
DuckDB also supports prepared statements in the C++ API with the Prepare() method. This returns an instance of PreparedState-
ment. This instance can be used to execute the prepared statement with parameters. Below is an example:
Warning. Do not use prepared statements to insert large amounts of data into DuckDB. See the data import documentation for
better options.
UDF API
The UDF API allows the definition of user‑defined functions. It is exposed in duckdb:Connection through the methods: Cre-
ateScalarFunction(), CreateVectorizedFunction(), and variants. These methods created UDFs into the temporary schema
(TEMP_SCHEMA) of the owner connection that is the only one allowed to use and change them.
CreateScalarFunction
The user can code an ordinary scalar function and invoke the CreateScalarFunction() to register and afterward use the UDF in a
SELECT statement, for instance:
The CreateScalarFunction() methods automatically creates vectorized scalar UDFs so they are as efficient as built‑in functions, we
have two variants of this method interface as follows:
1.
• template parameters:
This method automatically discovers from the template typenames the corresponding LogicalTypes:
• bool → LogicalType::BOOLEAN
• int8_t → LogicalType::TINYINT
• int16_t → LogicalType::SMALLINT
• int32_t → LogicalType::INTEGER
300
DuckDB Documentation
• int64_t →LogicalType::BIGINT
• float → LogicalType::FLOAT
• double → LogicalType::DOUBLE
• string_t → LogicalType::VARCHAR
In DuckDB some primitive types, e.g., int32_t, are mapped to the same LogicalType: INTEGER, TIME and DATE, then for disam‑
biguation the users can use the following overloaded method.
2.
int32_t udf_date(int32_t a) {
return a;
}
• template parameters:
This function checks the template types against the LogicalTypes passed as arguments and they must match as follow:
• LogicalTypeId::BOOLEAN → bool
• LogicalTypeId::TINYINT → int8_t
• LogicalTypeId::SMALLINT → int16_t
• LogicalTypeId::DATE, LogicalTypeId::TIME, LogicalTypeId::INTEGER → int32_t
• LogicalTypeId::BIGINT, LogicalTypeId::TIMESTAMP → int64_t
• LogicalTypeId::FLOAT, LogicalTypeId::DOUBLE, LogicalTypeId::DECIMAL → double
• LogicalTypeId::VARCHAR, LogicalTypeId::CHAR, LogicalTypeId::BLOB → string_t
• LogicalTypeId::VARBINARY → blob_t
CreateVectorizedFunction
/*
* This vectorized function copies the input values to the result vector
*/
template<typename TYPE>
static void udf_vectorized(DataChunk &args, ExpressionState &state, Vector &result) {
// set the result vector type
result.vector_type = VectorType::FLAT_VECTOR;
301
DuckDB Documentation
• args is a DataChunk that holds a set of input vectors for the UDF that all have the same length;
• expr is an ExpressionState that provides information to the query's expression state;
• result: is a Vector to store the result values.
• ConstantVector;
• DictionaryVector;
• FlatVector;
• ListVector;
• StringVector;
• StructVector;
• SequenceVector.
1.
• template parameters:
302
DuckDB Documentation
This method automatically discovers from the template typenames the corresponding LogicalTypes:
• bool → LogicalType::BOOLEAN;
• int8_t → LogicalType::TINYINT;
• int16_t → LogicalType::SMALLINT
• int32_t → LogicalType::INTEGER
• int64_t → LogicalType::BIGINT
• float → LogicalType::FLOAT
• double → LogicalType::DOUBLE
• string_t → LogicalType::VARCHAR
2.
303
DuckDB Documentation
304
CLI
CLI API
Installation
The DuckDB CLI (Command Line Interface) is a single, dependency‑free executable. It is precompiled for Windows, Mac, and Linux for both
the stable version and for nightly builds produced by GitHub Actions. Please see the installation page under the CLI tab for download
links.
The DuckDB CLI is based on the SQLite command line shell, so CLI‑client‑specific functionality is similar to what is described in the SQLite
documentation (although DuckDB's SQL syntax follows PostgreSQL conventions with a few exceptions).
DuckDB has a tldr page, which summarizes the most common uses of the CLI client. If you have tldr installed, you can display it by
running tldr duckdb.
Getting Started
Once the CLI executable has been downloaded, unzip it and save it to any directory. Navigate to that directory in a terminal and enter the
command duckdb to run the executable. If in a PowerShell or POSIX shell environment, use the command ./duckdb instead.
Usage
Options
The [OPTIONS] part encodes arguments for the CLI client. Common options include:
For a full list of options, see the command line arguments page.
When no [FILENAME] argument is provided, the DuckDB CLI will open a temporary in‑memory database. You will see DuckDB's version
number, the information on the connection and a prompt starting with a D.
duckdb
305
DuckDB Documentation
To open or create a persistent database, simply include a path as a command line argument:
duckdb my_database.duckdb
Once the CLI has been opened, enter a SQL statement followed by a semicolon, then hit enter and it will be executed. Results will be
displayed in a table in the terminal. If a semicolon is omitted, hitting enter will allow for multi‑line SQL statements to be entered.
my_column
quack
The CLI supports all of DuckDB's rich SQL syntax including SELECT, CREATE, and ALTER statements.
Editor Features
The CLI supports autocompletion, and has sophisticated editor features and syntax highlighting on certain platforms.
To exit the CLI, press Ctrl+D if your platform supports it. Otherwise, press Ctrl+C or use the .exit command. If used a persistent
database, DuckDB will automatically checkpoint (save the latest edits to disk) and close. This will remove the .wal file (the write‑ahead
log) and consolidate all of your data into the single‑file database.
Dot Commands
In addition to SQL syntax, special dot commands may be entered into the CLI client. To use one of these commands, begin the line with a
period (.) immediately followed by the name of the command you wish to execute. Additional arguments to the command are entered,
space separated, after the command. If an argument must contain a space, either single or double quotes may be used to wrap that pa‑
rameter. Dot commands must be entered on a single line, and no whitespace may occur before the period. No semicolon is required at the
end of the line.
Frequently‑used configurations can be stored in the file ~/.duckdbrc, which will be loaded when starting the CLI client. See the Config‑
uring the CLI section below for further information on these options.
Below, we summarize a few important dot commands. To see all available commands, see the dot commands page or use the .help
command.
In addition to connecting to a database when opening the CLI, a new database connection can be made by using the .open command. If
no additional parameters are supplied, a new in‑memory database connection is created. This database will not be persisted when the CLI
connection is closed.
306
DuckDB Documentation
.open
The .open command optionally accepts several options, but the final parameter can be used to indicate a path to a persistent database
(or where one should be created). The special string :memory: can also be used to open a temporary in‑memory database.
.open persistent.duckdb
Warning. .open closes the current database. To keep the current database, while adding a new database, use the ATTACH state‑
ment.
One important option accepted by .open is the --readonly flag. This disallows any editing of the database. To open in read only mode,
the database must already exist. This also means that a new in‑memory database can't be opened in read only mode since in‑memory
databases are created upon connection.
Output Formats
The .mode dot command may be used to change the appearance of the tables returned in the terminal output. These include the de‑
fault duckbox mode, csv and json mode for ingestion by other tools, markdown and latex for documents, and insert mode for
generating SQL statements.
By default, the DuckDB CLI sends results to the terminal's standard output. However, this can be modified using either the .output or
.once commands. For details, see the documentation for the output dot command.
The DuckDB CLI can read both SQL commands and dot commands from an external file instead of the terminal using the .read command.
This allows for a number of commands to be run in sequence and allows command sequences to be saved and reused.
The .read command requires only one argument: the path to the file containing the SQL and/or commands to execute. After running the
commands in the file, control will revert back to the terminal. Output from the execution of that file is governed by the same .output and
.once commands that have been discussed previously. This allows the output to be displayed back to the terminal, as in the first example
below, or out to another file, as in the second example.
In this example, the file select_example.sql is located in the same directory as duckdb.exe and contains the following SQL state‑
ment:
SELECT *
FROM generate_series(5);
.read select_example.sql
The output below is returned to the terminal by default. The formatting of the table can be adjusted using the .output or .once com‑
mands.
| generate_series |
|----------------:|
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
307
DuckDB Documentation
Multiple commands, including both SQL and dot commands, can also be run in a single .read command. In this example, the file write_
markdown_to_file.sql is located in the same directory as duckdb.exe and contains the following commands:
.mode markdown
.output series.md
SELECT *
FROM generate_series(5);
.read write_markdown_to_file.sql
In this case, no output is returned to the terminal. Instead, the file series.md is created (or replaced if it already existed) with the
markdown‑formatted results shown here:
| generate_series |
|----------------:|
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
Several dot commands can be used to configure the CLI. On startup, the CLI reads and executes all commands in the file ~/.duckdbrc,
including dot commands and SQL statements. This allows you to store the configuration state of the CLI. You may also point to a different
initialization file using the -init.
As an example, a file in the same directory as the DuckDB CLI named prompt.sql will change the DuckDB prompt to be a duck head and
run a SQL statement. Note that the duck head is built with Unicode characters and does not work in all terminal environments (e.g., in
Windows, unless running with WSL and using the Windows Terminal).
This outputs:
Non‑Interactive Usage
To read/process a file and exit immediately, pipe the file contents in to duckdb:
308
DuckDB Documentation
To execute a command with SQL text passed in directly from the command line, call duckdb with two arguments: the database location
(or :memory:), and a string with the SQL statement to execute.
Loading Extensions
To load extensions, use DuckDB's SQL INSTALL and LOAD commands as you would other SQL statements.
INSTALL fts;
LOAD fts;
When in a Unix environment, it can be useful to pipe data between multiple commands. DuckDB is able to read data from stdin as well
as write to stdout using the file location of stdin (/dev/stdin) and stdout (/dev/stdout) within SQL commands, as pipes act very
similarly to file handles.
First, read a file and pipe it to the duckdb CLI executable. As arguments to the DuckDB CLI, pass in the location of the database to open,
in this case, an in‑memory database, and a SQL command that utilizes /dev/stdin as a file location.
woot
42
43
To write back to stdout, the copy command can be used with the /dev/stdout file location.
cat test.csv | \
duckdb -c "COPY (SELECT * FROM read_csv('/dev/stdin')) TO '/dev/stdout' WITH (FORMAT 'csv', HEADER)"
woot
42
43
Examples
To retrieve the home directory's path from the HOME environment variable, use:
309
DuckDB Documentation
home
/Users/user_name
The output of the getenv function can be used to set configuration options. For example, to set the NULL order based on the environment
variable DEFAULT_NULL_ORDER, use:
The getenv function can only be run when the enable_external_access is set to true (the default setting). It is only available in
the CLI client and is not supported in other DuckDB clients.
Prepared Statements
The DuckDB CLI supports executing prepared statements in addition to regular SELECT statements. To create and execute a prepared
statement in the CLI client, use the PREPARE clause and the EXECUTE statement.
The table below summarizes DuckDB's command line options. To list all command line options, use the command:
duckdb -help
For a list of dot commands available in the CLI shell, see the Dot Commands page.
Argument Description
310
DuckDB Documentation
Argument Description
Dot Commands
Dot commands are available in the DuckDB CLI client. To use one of these commands, begin the line with a period (.) immediately followed
by the name of the command you wish to execute. Additional arguments to the command are entered, space separated, after the command.
If an argument must contain a space, either single or double quotes may be used to wrap that parameter. Dot commands must be entered
on a single line, and no whitespace may occur before the period. No semicolon is required at the end of the line. To see available commands,
use the .help command.
Dot Commands
Command Description
311
DuckDB Documentation
Command Description
312
DuckDB Documentation
The .help text may be filtered by passing in a text string as the second argument.
.help m
.maxrows COUNT Sets the maximum number of rows for display (default: 40). Only for duckbox mode.
.maxwidth COUNT Sets the maximum width in characters. 0 defaults to terminal width. Only for duckbox
mode.
.mode MODE ?TABLE? Set output mode
By default, the DuckDB CLI sends results to the terminal's standard output. However, this can be modified using either the .output or
.once commands. Pass in the desired output file location as a parameter. The .once command will only output the next set of results
and then revert to standard out, but .output will redirect all subsequent output to that file location. Note that each result will overwrite
the entire file at that destination. To revert back to standard output, enter .output with no file parameter.
In this example, the output format is changed to markdown, the destination is identified as a Markdown file, and then DuckDB will write
the output of the SQL statement to that file. Output is then reverted to standard output using .output with no parameter.
.mode markdown
.output my_results.md
SELECT 'taking flight' AS output_column;
.output
SELECT 'back to the terminal' AS displayed_column;
| output_column |
|---------------|
| taking flight |
| displayed_column |
|----------------------|
| back to the terminal |
A common output format is CSV, or comma separated values. DuckDB supports SQL syntax to export data as CSV or Parquet, but the CLI‑
specific commands may be used to write a CSV instead if desired.
.mode csv
.once my_output_file.csv
SELECT 1 AS col_1, 2 AS col_2
UNION ALL
SELECT 10 AS col1, 20 AS col_2;
col_1,col_2
1,2
10,20
By passing special options (flags) to the .once command, query results can also be sent to a temporary file and automatically opened in
the user's default program. Use either the -e flag for a text file (opened in the default text editor), or the -x flag for a CSV file (opened in
the default spreadsheet editor). This is useful for more detailed inspection of query results, especially if there is a relatively large result set.
The .excel command is equivalent to .once -x.
.once -e
SELECT 'quack' AS hello;
313
DuckDB Documentation
The results then open in the default text file editor of the system, for example:
All DuckDB clients support querying the database schema with SQL, but the CLI has additional dot commands that can make it easier to
understand the contents of a database. The .tables command will return a list of tables in the database. It has an optional argument
that will filter the results according to a LIKE pattern.
For example, to filter to only tables that contain an l, use the LIKE pattern %l%.
.tables %l%
fliers walkers
The .schema command will show all of the SQL statements used to define the schema of the database.
.schema
By default the shell includes support for syntax highlighting. The CLI's syntax highlighter can be configured using the following com‑
mands.
.highlight off
.highlight on
.constant
[red|green|yellow|blue|magenta|cyan|white|brightblack|brightred|brightgreen|brightyellow|brightblue|brightmagenta
.constantcode [terminal_code]
314
DuckDB Documentation
.keyword
[red|green|yellow|blue|magenta|cyan|white|brightblack|brightred|brightgreen|brightyellow|brightblue|brightmagenta
.keywordcode [terminal_code]
DuckDB supports SQL syntax to directly query or import CSV files, but the CLI‑specific commands may be used to import a CSV instead if
desired. The .import command takes two arguments and also supports several options. The first argument is the path to the CSV file,
and the second is the name of the DuckDB table to create. Since DuckDB requires stricter typing than SQLite (upon which the DuckDB CLI
is based), the destination table must be created before using the .import command. To automatically detect the schema and create a
table from a CSV, see the read_csv examples in the import docs.
In this example, a CSV file is generated by changing to CSV mode and setting an output file location:
.mode csv
.output import_example.csv
SELECT 1 AS col_1, 2 AS col_2 UNION ALL SELECT 10 AS col1, 20 AS col_2;
Now that the CSV has been written, a table can be created with the desired schema and the CSV can be imported. The output is reset to the
terminal to avoid continuing to edit the output file specified above. The --skip N option is used to ignore the first row of data since it is
a header row and the table has already been created with the correct column names.
.mode csv
.output
CREATE TABLE test_table (col_1 INTEGER, col_2 INTEGER);
.import import_example.csv test_table --skip 1
Note that the .import command utilizes the current .mode and .separator settings when identifying the structure of the data to
import. The --csv option can be used to override that behavior.
Output Formats
The .mode dot command may be used to change the appearance of the tables returned in the terminal output. In addition to customizing
the appearance, these modes have additional benefits. This can be useful for presenting DuckDB output elsewhere by redirecting the
terminal output to a file. Using the insert mode will build a series of SQL statements that can be used to insert the data at a later point.
The markdown mode is particularly useful for building documentation and the latex mode is useful for writing academic papers.
Mode Description
315
DuckDB Documentation
Mode Description
.mode
.mode markdown
SELECT 'quacking intensifies' AS incoming_ducks;
| incoming_ducks |
|----------------------|
| quacking intensifies |
The output appearance can also be adjusted with the .separator command. If using an export mode that relies on a separator (csv or
tabs for example), the separator will be reset when the mode is changed. For example, .mode csv will set the separator to a comma (,).
Using .separator "|" will then convert the output to be pipe‑separated.
.mode csv
SELECT 1 AS col_1, 2 AS col_2
UNION ALL
SELECT 10 AS col1, 20 AS col_2;
col_1,col_2
1,2
10,20
.separator "|"
SELECT 1 AS col_1, 2 AS col_2
UNION ALL
SELECT 10 AS col1, 20 AS col_2;
col_1|col_2
1|2
10|20
Editing
316
DuckDB Documentation
The linenoise‑based CLI editor is currently only available for macOS and Linux.
DuckDB's CLI uses a line‑editing library based on linenoise, which has shortcuts that are based on Emacs mode of readline. Below is a list
of available commands.
Moving
Key Action
History
Key Action
Changing Text