~/datapy v0.2.0 on pypi

corey@mccarty.dev:~/datapy$

Large files, pipes and memory

Datapy is meant to work on files far larger than memory, in the middle of a shell pipeline, the way grep and awk do.

How a query is run

Datapy looks at the statement and picks one of two ways to run it. You do not choose; the result is the same either way.

The query How it runs Memory
picks columns and filters rows: -s, -w, -l streamed: rows are read, checked and written a batch at a time small and fixed, whatever the file size
sorts, groups, counts, removes duplicates, or uses a subquery: -o, -g, count(*)... loaded: the file is first copied into a temporary database on disk small and fixed; uses temporary disk space about the size of the file

A streamed query starts writing rows almost immediately and stops reading the file as soon as it has enough, so these return at once on a file of any size:

datapy -sa huge.csv -w 'amount > 990' -l 10
datapy -sa huge.csv -w 'amount > 990' | head

Statements given with -q, or as a whole statement, are classified the same way. When datapy cannot be sure a statement is row-by-row (for example it calls a function datapy does not know) it loads the file, which is slower but always correct.

Pipes

Leave the file out, or name it -, to read standard input. Results go to standard output as csv, so datapy can sit anywhere in a pipeline:

zcat orders.csv.gz | datapy -sa -w 'amount > 990' | datapy -s 'city, count(*)' -g city

Output is flushed every thousand rows. Datapy reads the first thousand rows before writing anything (see below), so it is not suited to a slow live feed such as tail -f.

Column types come from the first 1,000 rows

To avoid reading a file twice, datapy decides whether each column is a number or text from its first 1,000 rows. For a file with fewer rows than that, every row is considered.

If a column looks numeric at the start but is not really (an id column whose first leading zero appears on row 5,000, say), later values are affected: a value that is not a number at all stays text, but 007 would be read as 7. Tell datapy to keep such a column as text:

datapy -sa parts.csv -t code
datapy -sa parts.csv -t 'code, $4'

Measurements

python tools/benchmark.py generates a file and times datapy next to awk and sort. These figures are from one run on one laptop (Python 3.12, gawk, WSL2) and are only meant to show the shape: compare the rows with each other.

Task Tool 2,000,000 rows (84 MB) 10,000,000 rows (426 MB)
filter rows datapy 2.5 s, 20 MB 12.3 s, 20 MB
filter rows awk 0.7 s, 4 MB 3.5 s, 4 MB
first 10 matches datapy 0.0 s, 16 MB 0.0 s, 16 MB
count per city datapy 3.1 s, 21 MB 15.2 s, 21 MB
count per city awk 0.6 s, 4 MB 2.9 s, 4 MB
sort by amount datapy 5.7 s, 21 MB 27.6 s, 22 MB
sort by amount sort 0.4 s, 335 MB 2.3 s, 1,654 MB
filter rows, piped in datapy 2.6 s, 20 MB 12.8 s, 20 MB

In short: memory stays at about 20 MB however large the file is, and datapy takes roughly four to five times as long as awk for filtering and counting. It is slower because every row passes through Python's csv parser, which is also what lets it handle quoted fields that contain commas or line breaks.

From Python

datapy.stream() gives a script the same behaviour: it returns the rows one at a time instead of collecting them.

import datapy

with datapy.stream("select name from people.csv where age > 40") as rows:
    for row in rows:
        print(row["name"])

datapy.query() and DataFile.query() return every row at once, which is convenient for small results. datapy.open() always loads the file into the temporary on-disk database, because it is built for running many queries against one file.