DuckDB

sq’s DuckDB driver implements connectivity for DuckDB . It makes use of the backing duckdb/duckdb-go/v2 library, which statically links libduckdb. The driver implements all optional sq driver features.

Add source

Use sq add to add a source. The location argument is the filepath to the DuckDB file. For example:

# Relative path
$ sq add ./sakila.duckdb

# Absolute path
$ sq add /Users/neilotoole/sakila.duckdb

sq can detect that a file is a DuckDB datafile via the .duckdb or .ddb extension, or by the DUCK magic header at byte offset 8. If auto-detection doesn’t trigger, specify the driver explicitly:

$ sq add --driver=duckdb ./sakila.duckdb

Use the duckdb:// scheme to pass connection parameters or to open an in-memory database:

# Persistent file via URI
$ sq add 'duckdb:///abs/path/sakila.duckdb'

# In-memory database
$ sq add 'duckdb://:memory:'

# File with connection parameters
$ sq add 'duckdb:///path/sakila.duckdb?memory_limit=4GB&threads=4'

Bundled extensions

The driver statically links the standard set of in-tree DuckDB extensions. They are available immediately — no INSTALL or LOAD required:

ExtensionPurpose
jsonJSON read/write functions
parquetParquet read/write (read_parquet())
icuICU collations and Unicode functions
ftsFull-text search
httpfsHTTP(S) and S3 file access
excelExcel read (excel_open())
inetIP address types and functions
autocompleteSQL auto-completion helpers
tpchTPC-H benchmark tables
tpcdsTPC-DS benchmark tables

Because the extensions are statically bundled, queries like the following work without any setup:

-- Query a Parquet file directly
SELECT * FROM read_parquet('file.parquet');

-- Query a remote CSV via HTTPS
SELECT * FROM read_csv_auto('https://example.com/data.csv');

-- Query an S3 object (set AWS credentials first)
SELECT * FROM read_parquet('s3://bucket/key.parquet');

Connection parameters

Pass parameters as URL query strings after the file path:

$ sq add 'duckdb:///path/db.duckdb?memory_limit=4GB&threads=4'
ParameterValuesDescription
access_modeREAD_WRITE, READ_ONLYOpen the database read-only
memory_limite.g. 4GBMaximum memory DuckDB may use
threadsintegerNumber of threads
default_orderASC, DESCDefault sort order
default_null_orderNULLS_FIRST, NULLS_LASTDefault NULL sort position
enable_external_accesstrue, falseAllow reading from external files/URLs
enable_object_cachetrue, falseCache metadata for remote objects
temp_directorypathDirectory for temporary files
wal_autocheckpointe.g. 1000WAL autocheckpoint threshold (pages)

DuckDB supports many more settings ; the list above covers the parameters most commonly used from the CLI.

Type mapping

DuckDB typesq kindNotes
BOOLEANbool
TINYINTBIGINT, UTINYINT, USMALLINT, UINTEGERint
HUGEINT, UHUGEINT, UBIGINT, INT128decimalPromoted to decimal because values can exceed int64.
FLOAT, REAL, DOUBLEfloat
DECIMAL(p,s)decimal
VARCHAR, TEXT, STRINGtext
BLOB, BYTEAbytes
DATEdate
TIME, TIME WITH TIME ZONEtime
TIMESTAMP, TIMESTAMP_S/MS/NS, TIMESTAMPTZdatetime
INTERVALtext"<months> months <days> days <micros> μs" (see #612 ).
UUIDtextHex-dash form.
JSONtextAlready JSON.
LIST / ARRAYtextGo-style stringification; JSON projection planned (#609 ).
STRUCTtextGo-style stringification; JSON projection planned (#609 ).
MAPtextGo-style stringification; JSON projection planned (#609 ).
ENUMtextUnderlying value.
BITtextBit-string.

Composite types (LIST, STRUCT, MAP) are currently stringified via Go’s default formatting (fmt.Sprintf("%v", v)). First-class JSON projection via DuckDB’s to_json(col) is planned as a follow-up — see #609 .

Limitations

  • DuckDB enforces a single writer per database file. If two sq processes open the same .duckdb file simultaneously, the second will receive a lock error. For sq’s typical single-shot CLI usage this is rarely a problem, but scripts that parallelize sq against the same file should serialize writes. Read-only access (access_mode=READ_ONLY) from multiple processes is safe.