github teaguesterling/plinking_duck v0.18.0
v0.18.0 — ATTACH genotype views (breaking); WHERE on ID, CPRA, genotype and IID narrows the read; shared .pgen header cache; fast .psam.parquet at 8M samples; DuckDB v2.0.1

46 minutes ago

This release makes the common cohort queries one WHERE on an ATTACH view.
c.genotypes is now one row per variant x sample (breaking). A WHERE on
CHROM/POS, ID, CPRA, genotype or IID now narrows what read_pfile reads, so
those queries take tenths of a second at 10M variants or 8M samples. Each
.pgen header is decoded once per database and shared by every reader and
thread. The writer is faster and writes smaller parquet companions and an
optional ID index. It builds for DuckDB v2.0.1 (#152).

BREAKING — ATTACH c.genotypes is tall; five genotype views (#139)
ATTACH ... (TYPE pfile_catalog) now has five genotype views:
- c.genotypes: one row per variant x sample, with IID, genotype,
CPRA and the freq columns.
- c.genotypes_by_variant: one row per variant, one column per IID.
- c.genotypes_by_sample: one row per sample, one column per variant
ID (unique_column_names).
- c.genotypes_by_variant_list: the old c.genotypes.
- c.genotypes_by_sample_list: the old c.genotypes_by_sample.
Migrate a query on the old views by adding _list to its name.
The common reads are now one WHERE:
SELECT * FROM c.genotypes WHERE ID = 'rs123';
SELECT DISTINCT IID FROM c.genotypes
WHERE ID IN ('rs1', 'rs2', 'rs3') AND genotype = 2;
- On a matrix with holes, c.genotypes works. The wide and list views
raise, except the variant-row views when there is one sample block.
- The wide views' bind cost grows faster than linear: DESCRIBE of the
by-sample read took 0.64 / 2.38 / 5.19 s at 10k / 20k / 30k
variants (#139).

New — a WHERE narrows read_pfile, and every ATTACH view
- WHERE CHROM = ... [AND POS ...] loads only that region's variants,
as region := does (#135). 10M variants, c.genotypes WHERE CHROM AND
POS: 3.48 s -> 0.116 s local, 5.81 s -> 0.70 s remote cold.
A text .pvar is still parsed in full.
- On DuckDB v2.0, a WHERE on CHROM, POS, ID or __ordinal narrows the
.pgen read again (#120; it read every variant since v2.0.0).
wes_chr10, WHERE POS BETWEEN over HTTP: 43 GETs / 11.2 MB ->
4 GETs / 1.0 MB.
- WHERE ID = / IN uses an optional ID index,

.pvar.index.parquet
(#140). 10M variants, WHERE ID =: 3.38-3.45 s -> 0.10-0.13 s local,
6.3-6.7 s -> 1.43-1.47 s remote. COPY ... (FORMAT pfile, index)
writes the index (#141); the docs give a SQL recipe for files from
other tools.
- The virtual column CPRA (CHROM:POS:REF:ALT) in variant and genotype
orient (#138). SELECT * leaves it out. WHERE CPRA = / IN narrows
the read as CHROM + POS does.
- In genotype orient, a WHERE on genotype becomes the scan's genotype
filter, as include_genotypes (#137). 1M samples, ID IN (3 IDs) AND
genotype = 2: 0.80 s -> 0.54 s.
- In genotype orient on a single cohort, WHERE IID = / IN narrows the
sample set (#148). 8M samples, cached ATTACH, text .psam, WHERE ID
AND IID: 1.14 s -> 0.31 s.
- freq := true in genotype orient adds the six plink_freq columns to
every row (#134). A WHERE IID does not narrow those values.

New — the .pgen header is decoded once and shared (#143, #144)
read_pfile, read_pgen and every plink_* function share one decoded
header per file, across threads and queries, in DuckDB's object cache
(counted against memory_limit). Turn it off with
SET plinking_pgen_header_cache = false.
- 10M variants, full scan, peak memory at 16 threads:
3519 MB -> 2231 MB (#143).
- 10M variants, 16 threads, plink_freq: 34 decodes, 4.68 s, 3613 MB
-> 1 decode, 3.77 s, 2325 MB (#144).
- Cost: a 1-thread cohort full scan keeps every cell's header:
1521 MB -> 1949 MB peak (#143).

New — ATTACH caches catalog metadata (CACHE option, #136, #147)
- ATTACH ... (TYPE pfile_catalog, CACHE ), default true, reuses
the parsed .pfile.json, the discovered companion paths and each
.psam (#136). Remote 20 x 2 matrix, warm c.genotypes WHERE POS:
0.435 s -> 0.181 s. The cache is a snapshot; it never checks the
files again.
- The cache keeps only each .psam's IID, FID and SEX, compactly
(#147). 8M samples, text .psam, c.genotypes WHERE ID AND IID:
2.4 s / 2889 MB -> 1.2-1.4 s / 1822 MB. A query that projects
another .psam column re-parses the text file: 3.0 s -> 5.2-5.6 s.

New — a .psam.parquet cohort is fast at 8M samples (#145, #149, #150)
Measured on plink2 --dummy 8000000 4.
- read_pfile(samples := ['per5']), variant orient: 11.4 s -> 0.032 s
(#150). genotype orient WHERE IID = 'per5': 0.52-0.73 s -> 0.134 s.
- The analyses' .psam.parquet load reads only IID, FID and SEX:
6595 ms -> 695 ms (#150). plink_freq(samples := ['per5']):
12.4 s -> 5.7 s.
- ATTACH c.samples reads the .psam.parquet, and read_psam streams a
non-native source (#149). c.samples count(): 5.3 s -> 0.023 s;
read_psam('x.psam.parquet') count(
): 8.0 s -> 0.003 s.
- Reading a parquet SEX column: 8.75 s -> 0.87 s (#145).
A text .psam is unchanged; the docs recommend .psam.parquet at scale.

New — writer
- The .pgen writer reads the genotypes column once per chunk (#129).
200 variants x 1M samples, TINYINT[]: 41.6 s -> 1.09 s (38x);
INTEGER[]: 40.7 s -> 4.09 s. The output is byte-identical.
- psam_parquet writes a .psam.parquet companion, in FORMAT pfile and
every sharded layout (#119), and in standalone FORMAT psam (#123).
- pvar_parquet: a .pvar.parquet companion with __vidx, which a region
read prunes to about one row group (#116). A ~200-variant gene in a
5M-variant block: 116 MB -> 0.2 MB fetched.
- Companions are parquet format version 2 (#132): 6.3x smaller for
400k variants, 3.7x smaller for 1M samples. A reader must support
parquet V2; DuckDB does.
- The .pvar.parquet is written in 20,480-row groups (#133). 10M
variants, remote region read: 3.36 s -> 1.46 s.
- Standalone FORMAT psam writes every input column, with SEX in
PLINK coding, not only #IID (#130).
- Docs for psam_parquet and the standalone companion flags (#128).

New — analyses and reads
- plink_freq and plink_missing read pfile catalogs of every kind (v1,
v2, N x 1, 1 x K, N x K) and return the rows of the un-sharded
source (#131).
- Sample-sharded carrier reads use pgenlib's sparse decode again
(#122). 1M samples x 200 rare variants, 4 shards: 0.644 s ->
0.260 s.
- .pgen files read through the VFS use DuckDB's external file cache
(#126). A repeated remote query fetches 0 blocks; an 18-cell grid,
2 passes: 336 GETs / 82.2 MB -> 18 / 11.2 MB. Cost: a cold point
query fetches 1 GET / 2.1 MB, where it fetched 3 / 0.8 MB.
- One query fetches a .pgen header's blocks once (#125). 18-cell
grid, 30 slices, 1 thread: 258 GETs / 64.7 MB -> 168 / 41.1 MB.

Changed — behaviour that a query can see
- read_pfile: a duplicate IID in a .psam.parquet that no samples :=
name or WHERE IID matches is no longer an error (#150). A name that
two samples share is still an error, with the same text.
- read_pfile: samples := [''] on a .psam.parquet with a NULL IID is
now "not found" (#150).
- read_pfile: a .psam.parquet SEX column is INTEGER and PAT/MAT are
VARCHAR, as from a text .psam (#145). Before, SEX took the parquet's
type, and every integer type except INTEGER came back all NULL.
- read_psam, c.samples: a foreign .psam.parquet gets the text .psam's
rules (SEX INTEGER, PAT/MAT '0' -> NULL, 'NA' -> NULL) (#149).
read_psam('') now works. A missing table raises
DuckDB's Catalog Error, not "read_psam: failed to read source".
- A bad .pvar is reported when the query runs, not at DESCRIBE (#135).
- A damaged .pgen index errors when the scan opens the file, not at
bind (#125). The error still names the file.
- Writer: a HUGEINT call too large for BIGINT gets the "out of range"
error, not a cast error (#129).
- Error text "failed to initialize '

' (phase 2)" is now
"failed to open (phase 2) for '

'" (#144).

Fixes
- An integer IID or FID column in a .psam.parquet (pandas int64)
crashed plink_freq and the other analyses with SIGSEGV (#150). It
is now read as text.
- plinking_pgen_io = 'localize': a v1 catalog copies cell 0 only if
the scan keeps it; a prepared statement keeps its bind's route and
temp dir; a failed copy removes its temp (#117).
- A fresh COPY with psam_parquet or pvar_parquet to a ~ or file://
path no longer deletes the companion and the text file (#123, #124).
- The .pgen header cache key builds on macOS (#151; the bug was in
#143, never released).

Build and CI
- Builds for DuckDB v2.0.1, the registry default (#152).
- #142 and #146 carried #137-#141, #143-#145 to main; they add no
code.
Tested on main de9161c (push run 45330456492): ran tests: 155 passed on
osx_arm64 and linux_amd64. linux_arm64 is compiled, not tested ("Tests
are not supported for linux_arm64").

Known limitations
- plink_freq and the other analyses still load every IID for
samples := [names]: 5.7 s at 8M samples (#150).
- A text .psam is parsed in full; use .psam.parquet at scale.
- A stale .pvar.parquet with rows missing inside a pushed region gives
a partial result with no error, as region := did (#135).
- Where measured, numbers are from Claude Opus runs on longbottom,
given in each PR; "remote" is a local HTTP range server at 40 ms
latency.

Don't miss a new plinking_duck release

NewReleases is sending notifications on new releases.