GitHub - chicagobuss/zpq: zig parquet library with some tricks up its sleeve

GitHub

6 min read Original article ↗

zpq is a small Unix tool for Parquet. It queries, filters, aggregates and rewrites Parquet files, on your disk or straight from S3, and prints NDJSON, CSV or Parquet for the next tool in the pipe. It does the least work a query needs for an exact answer: footers and statistics first, and only the pages that matter. DECIMAL stays in exact integers, and the same rows come back in the same order at any thread count.

The project site, with every benchmark and example: zpq.chicagobuss.dev.

Install

Homebrew (Apple-silicon macOS and Linux):

brew install chicagobuss/zpq/zpq

A static binary from the latest release. Each archive holds bin/, share/ and MANIFEST.json with no top folder, so give it one:

curl -sSL https://github.com/chicagobuss/zpq/releases/latest/download/zpq-x86_64-linux-musl.tar.gz \
  | (mkdir zpq && tar xz -C zpq)
export PATH="$PWD/zpq/bin:$PATH"
Platform Archive
Linux x86_64 (needs AVX2) zpq-x86_64-linux-musl.tar.gz
Linux x86_64, older CPUs without AVX2 zpq-x86_64-linux-musl-compat.tar.gz
Linux arm64 zpq-aarch64-linux-musl.tar.gz
macOS, Apple silicon zpq-aarch64-macos.tar.gz
macOS, Intel zpq-x86_64-macos.tar.gz

Debian, Ubuntu, Fedora, RHEL: the release also carries zpq_0.5.0_amd64.deb, zpq_0.5.0_arm64.deb, zpq-0.5.0-1.x86_64.rpm and zpq-0.5.0-1.aarch64.rpm.

sudo apt install ./zpq_0.5.0_amd64.deb      # or: sudo dnf install ./zpq-0.5.0-1.x86_64.rpm

The default Linux x86_64 build needs AVX2; on older CPUs use the -compat build.

Build it yourself. All you need is Zig 0.17.0: Zig's bundled clang builds the C codecs, and the first build fetches checksum-pinned BoringSSL prebuilts. Cross-compiling is one flag.

git clone https://github.com/chicagobuss/zpq && cd zpq
zig build cli -Doptimize=ReleaseFast                                  # zig-out/bin/zpq
zig build cli -Doptimize=ReleaseFast -Dtarget=aarch64-linux-musl      # a static arm64 Linux binary, from anywhere

First commands

These run on ClickBench's public hits_0.parquet (122 MB, one million rows of web analytics). The output shown is the real output; more examples, each with its output, are on the site's Examples page.

Look inside a file without reading its data pages:

$ zpq inspect hits_0.parquet | head -12
hits_0.parquet: Parquet v1, 1000000 rows, 122446530 bytes, 2 row groups
  created_by: parquet-cpp version 1.5.1-SNAPSHOT; sorting_columns: unsupported
  row group 0: 450560 rows, 188923477 uncompressed bytes
    WatchID INT64 SNAPPY: 3883794/3883596 compressed/uncompressed bytes
      min=4611687214012840539 (unknown) max=9223369186199968220 (unknown); nulls=0
      index=absent/absent, Bloom=absent, dictionary fallback=present
      hint: page pruning unavailable: column index absent
      hint: compression saves no bytes: compressed >= uncompressed
      hint: dictionary fallback: mixed data encodings in encoding_stats
    JavaEnable INT32 SNAPPY: 20529/24207 compressed/uncompressed bytes
      min=0 (unknown) max=1 (unknown); nulls=0
      index=absent/absent, Bloom=absent, dictionary fallback=absent

Query it with SQL and pipe the rows to jq:

$ zpq query --query "SELECT SearchPhrase, count(*) AS c FROM 'hits_0.parquet' \
  WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 3" | jq -c .
{"SearchPhrase":"ведомосквы вместу","c":4943}
{"SearchPhrase":"ведомосквы вы из","c":2471}
{"SearchPhrase":"ведомосквиталия страции","c":2026}

Filter and aggregate with flags instead of SQL:

$ zpq query hits_0.parquet --filter 'IsMobile = 1' \
  --aggregate 'count(*) AS n, count(DISTINCT UserID) AS users, avg(ResolutionWidth) AS width'
{"n":35239,"users":5146,"width":846.5504696501035}

Write Parquet with -o, then read it back as CSV:

$ zpq query hits_0.parquet --filter "SearchPhrase <> ''" --select 'UserID, RegionID, SearchPhrase' -o phrases.parquet
$ zpq query phrases.parquet --limit 3 --format csv
UserID,RegionID,SearchPhrase
-9214751021948998350,2,авомосква веб каменисный
-9214751021948998350,2,авомосква веб каменисный
-9213728704863893851,103,чагин выпуска на волна 2 сезон 24 резюме онлайн

Read straight from S3. A glob reads all 100 files; count(*) comes from their footers alone:

$ S3_NO_SIGN_REQUEST=1 AWS_REGION=eu-central-1 zpq query \
  's3://clickhouse-public-datasets/hits_compatible/athena_partitioned/hits_*.parquet' --aggregate 'count(*) AS n'
{"n":99997497}

Files zpq writes use Parquet's version 2 encodings by default (--parquet-version v2: DELTA where it fits and RLE_DICTIONARY, with a version 2 footer). For readers that only understand version 1 files, add --parquet-version v1 (PLAIN / PLAIN_DICTIONARY and a version 1 footer).

Three ways in

  • The zpq CLI, shown above. zpq --help and man zpq list every option; docs/getting-started.md walks through more, and docs/output-format.md is the output contract.
  • zpq-lambda, a static AWS Lambda bootstrap for provided.al2023 that starts cold in tens of milliseconds and reads and writes S3 natively, with no AWS SDK. tools/serverless/fanout.py fans one query out across many workers by row group (docs/lambda-and-s3.md).
  • libzpq, a C library (docs/libzpq.md), with a Python binding, python/zpq.py, that needs no compiled extension. SQL through libzpq is experimental. libzpq can also return answers as Arrow C Data streams (experimental): python/zpq_arrow.py reads them into PyArrow, and a Spark DataSource V2, spark.read.format("zpq"), runs Parquet scans and partial aggregates in zpq and takes the rows back as Arrow batches.

What zpq isn't

zpq works on one table at a time: there are no joins and no dataframe API yet. Use DuckDB for those, and let zpq do the Parquet part: filter and project with zpq, write Parquet or NDJSON, and read that into DuckDB.

How fast

On 464 benchmark queries (ClickBench, PDS-H and 390 synthetic aggregation cases), run hot with a fresh process for every query, zpq 0.5.1 is faster than DuckDB 2.0 on 446 of them and faster than Polars 2.1 on 459, the fastest of the three on 442, and never more than 1.5× behind. Every query, the method, and the few places zpq is still behind are on the site's Benchmarks page.

Limits

  • --max-memory is a working budget, not a hard cap: it counts aggregation, DISTINCT and string memory, but not the binary, thread stacks or mapped input pages, so a query can go somewhat past it (a 256 MiB budget peaked at about 284 MiB in testing). The answer stays exact, or the query refuses cleanly. For a hard ceiling, run zpq under a cgroup memory.max or ulimit -v.
  • No joins and no window functions; some other SQL is refused with exit 2 and a message.
  • JSON prints DATE and TIMESTAMP as their stored integers; CSV prints ISO text.
  • SQL through libzpq, Arrow results and the Spark data source are experimental; Windows is not built.

The full list is under "Known limits" in CHANGELOG.md.

Learn more

  • How it works: every optimization, Parquet itself, testing, the write path, Lambda fan-out, the JVM and NaN.
  • Examples: a guide with real output for each command.
  • CHANGELOG.md: everything new in 0.5.0, and what to do when upgrading from 0.4.x.

Reference

Build and contribute

just build             # ReleaseFast: the CLI and the Lambda bootstrap
just check             # the commit gate: fixtures, unit tests, every case against DuckDB
just full              # the full gate (tools/check/README.md)
just test-integration  # Lambda integration tests

The check tiers are described in tools/check/README.md, test coverage in docs/test-coverage.md, and toolchain, release archives and packaging in docs/building.md. Contributors and coding agents start with AGENTS.md.

License

MIT. See THIRD_PARTY_NOTICES.md for third-party dependency licenses.