Scaling to infinity and beyond: the Unix backend

blog
serious
web-scale
unix
Author

Phillip Cloud

Published

April 1, 2024

The Unix backend for Ibis

We’re happy to announce a new Ibis backend built on the world’s best known web scale technology: Unix pipes.

Why?

Why not? Pipes rock and they automatically stream data between operators and scale to your hard drive.

What’s not to love?

Demo

All production ready backends ship with amazing demos.

The Unix backend is no different. Let’s see it in action.

First we’ll install the Unix backend.

pip install ibish

Like all production-ready libraries ibish depends on the latest commit of ibis-framework.

Next we’ll download some data.

!curl -LsS 'https://storage.googleapis.com/ibis-examples/penguins/20240322T125036Z-9aae2/penguins.csv.gz' | zcat > penguins.csv
import ibis
import ibish


ibis.options.interactive = True

unix = ibish.connect({"p": "penguins.csv"})

t = unix.table("p")
t
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen39.118.7181.03750.0male  2007 │
│ Adelie Torgersen39.517.4186.03800.0female2007 │
│ Adelie Torgersen40.318.0195.03250.0female2007 │
│ Adelie TorgersenNULLNULLNULLNULLNULL2007 │
│ Adelie Torgersen36.719.3193.03450.0female2007 │
│ Adelie Torgersen39.320.6190.03650.0male  2007 │
│ Adelie Torgersen38.917.8181.03625.0female2007 │
│ Adelie Torgersen39.219.6195.04675.0male  2007 │
│ Adelie Torgersen34.118.1193.03475.0NULL2007 │
│ Adelie Torgersen42.020.2190.04250.0NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

Sweet, huh?

Let’s filter the data and look at only the year 2009.

expr = t.filter(t.year == 2009)
expr
┏━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Biscoe35.017.9192.03725.0female2009 │
│ Adelie Biscoe41.020.0203.04725.0male  2009 │
│ Adelie Biscoe37.716.0183.03075.0female2009 │
│ Adelie Biscoe37.820.0190.04250.0male  2009 │
│ Adelie Biscoe37.918.6193.02925.0female2009 │
│ Adelie Biscoe39.718.9184.03550.0male  2009 │
│ Adelie Biscoe38.617.2199.03750.0female2009 │
│ Adelie Biscoe38.220.0190.03900.0male  2009 │
│ Adelie Biscoe38.117.0181.03175.0female2009 │
│ Adelie Biscoe43.219.0197.04775.0male  2009 │
│  │
└─────────┴────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

We can sort the result of that too, and filter again.

expr = (
    expr.order_by("species", ibis.desc("bill_length_mm"))
    .filter(lambda t: t.island == "Biscoe")
)
expr
┏━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island  bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Biscoe45.620.3191.04600.0male  2009 │
│ Adelie Biscoe43.219.0197.04775.0male  2009 │
│ Adelie Biscoe42.718.3196.04075.0male  2009 │
│ Adelie Biscoe42.219.5197.04275.0male  2009 │
│ Adelie Biscoe41.020.0203.04725.0male  2009 │
│ Adelie Biscoe39.717.7193.03200.0female2009 │
│ Adelie Biscoe39.718.9184.03550.0male  2009 │
│ Adelie Biscoe39.620.7191.03900.0female2009 │
│ Adelie Biscoe38.617.2199.03750.0female2009 │
│ Adelie Biscoe38.220.0190.03900.0male  2009 │
│  │
└─────────┴────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

There’s even support for joins and aggregations!

Let’s count the number of island, species pairs and sort descending by the count.

expr = (
    t.group_by("island", "species")
    .agg(n=lambda t: t.count())
    .order_by(ibis.desc("n"))
)
expr
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
┃ island     species    n     ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
│ stringstringint64 │
├───────────┼───────────┼───────┤
│ Biscoe   Gentoo   124 │
│ Dream    Chinstrap68 │
│ Dream    Adelie   56 │
│ TorgersenAdelie   52 │
│ Biscoe   Adelie   44 │
└───────────┴───────────┴───────┘

For kicks, let’s compare that to the DuckDB backend to make sure we’re able to count stuff.

To be extra awesome, we’ll reuse the same expression to do the computation.

ddb = ibis.duckdb.connect()
ddb.read_csv("penguins.csv", table_name="p")
ibis.memtable(ddb.to_pyarrow(expr.unbind()))
1
The read_csv is necessary so that the expression’s table name–p–matches one inside the DuckDB database.
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
┃ island     species    n     ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
│ stringstringint64 │
├───────────┼───────────┼───────┤
│ Biscoe   Gentoo   124 │
│ Dream    Chinstrap68 │
│ Dream    Adelie   56 │
│ TorgersenAdelie   52 │
│ Biscoe   Adelie   44 │
└───────────┴───────────┴───────┘

How does it work?

Glad you asked!

The Unix backend for Ibis was built over the course of a few hours, which is about the time it takes to make a production ready Ibis backend.

Broadly speaking, the Unix backend:

  1. Produces a shell command for each Ibis table operation.
  2. Produces a nominal output location for the output of that command, in the form of a named pipe opened in write mode.
  3. Reads output from the named pipe output location of the root of the expression tree.
  4. Calls pandas.read_csv on that output.

Shell commands only allow a single input from stdin.

However, joins accept > 1 input so we need a way to stream more than one input to a join operation.

Named pipes support the semantics of “unnamed” pipes (FIFO queue behavior) but can be used in pipelines with nodes that have more a single input since they exist as paths on the file system.

Expressions

Ibis expressions are an abstract representation of an analytics computation over tabular data.

Ibis ships a public API, whose instances we call expressions.

Expressions have an associated type–accessible via their type() method–that determines what methods are available on them.

Expressions are ignorant of their underlying implementation: their composability is determined solely by their type.

This type is determined by the expression’s underlying operation.

The two-layer model makes it easy to describe operations in terms of the data types produced by an expression, rather than as instances of a specific class in a hierarchy.

This allows Ibis maintainers to alter expression API implementations without changing those APIs making it easier to maintain and easier to keep stable than if we had a complex (but not necessarily deep!) class hierarchy.

Operations, though, are really where the nitty gritty implementation details start.

Operations

Ibis operations are lightweight classes that model the tree structure of a computation.

They have zero or more inputs, whose types and values are constrained by Ibis’s type system.

Notably operations are not part of Ibis’s public API.

When we talk about “compilation” in Ibis, we’re talking about the process of converting an operation into something that the backend knows how to execute.

In the case of this 1̵0̸0̵%̵ p̶̺̑r̴̛ͅo̵̒ͅḍ̴̌u̷͇͒c̵̠̈t̷͍̿i̶̪͐o̸̳̾n̷͓̄-r̵̡̫̞͓͆̂̏ẽ̸̪̱̽ͅā̸̤̹̘̅̓͝d̵͇̞̏̂̔̽y̴̝͎̫̬͋̇̒̅ Unix backend, each operation is compiled into a list of strings that represent the shell command to run to execute the operation.

In other backends, like DuckDB, these compilation rules produce a sqlglot object.

The compile method is also the place where the backend has a chance to invoke custom rewrite rules over operations.

Rewrites are a very useful tool for the Unix backend. For example, the join command (yep, it’s in coreutils!) that we use to execute inner joins with this backend requires that the inputs be sorted, otherwise the results won’t be correct. So, I added a rewrite rule that replaces the left and right relations in a join operation with equivalent relations sorted on the join keys.

Once you obtain the output of compile, it’s up to the backend what to do next.

Backend implementation

At this point we’ve got our shell commands and some output locations created as named pipes.

What next?

Well, we need to execute the commands and write their output to the corresponding named pipe.

You might think

I’ll just loop over the operations, open the pipe in write mode and call subprocess.Popen(cmd, stdout=named_pipe).

Not a bad thought, but the semantics of named pipes do not abide such thoughts :)

Named pipes, when opened in write mode, will block until a corresponding handle is opened in read mode.

Futures using a scoped thread pool are a decent way to handle this.

The idea is to launch every node concurrently and then read from the last node’s output. This initial read of the root node’s output pipe kicks off the cascade of other reads necessary to move data through the pipeline.

The Unix backend thus constructs a scoped ThreadPoolExecutor() using a context manager and submits a task for each operation to the executor. Importantly, opening the named pipe in write mode happens inside the task, to avoid blocking the main thread while waiting for a reader to be opened.

The final output task’s path is then passed directly to read_csv, and we’ve now got the result of our computation.

Show me the commands already!

Roger that.

expr = (
    t.filter([t.year == 2009])
    .select(
        "year", "species", "flipper_length_mm", island=lambda t: t.island.lower()
    )
    .group_by("island", "species")
    .agg(n=lambda t: t.count(), avg=lambda t: t.island.upper().length().mean())
    .order_by("n")
    .mutate(ilength=lambda t: t.island.length())
    .limit(5)
)
print(unix.explain(expr))
1
explain isn’t a public method and not likely to become one any time soon.
tail --lines +2 /home/cloud/src/ibis/docs/posts/unix-backend/penguins.csv > t0
awk -F , '{ if (($8 == 2009)) { print }}' t0 > t1
awk -F , '{ print $8 "," $1 "," $5 "," tolower($2) }' t1 > t2
awk -F , '{
  agg0[$4","$2]++
  agg1[$4","$2] += length(toupper($4))
}
END { for (key in agg0) print key "," agg0[key] "," agg1[key]/NR }' t2 > t3
sort -t , -k 3,3n t3 > t4
awk -F , '{ print $1 "," $2 "," $3 "," $4 "," length($1) }' t4 > t5
head --lines 5 t5 > t6

Conclusion

If you’ve gotten this far hopefully you’ve had a good laugh.

Let’s wrap up with some final thoughts.

Things to do

Things to avoid doing

  • Putting this into production
Back to top