Relational Algebra Parsing Tools

PCRS relies on the Relational Algebra Parser and Translator (RAPT) project to support relational algebra exercises. RAPT is a Python package created by two U of T students, Olessia Karpova and Noel D'Souza. The source is available.

RAPT is designed to be extensible and to be used as the basis for classroom tools. A set of basic tools for parsing, visualizing, and executing relational algebra statements, including a live interpreter, are available through the rapture project.

RAPT Demo

----- install rapt -----
$ pip install rapt

----- load some demo tables into a psql table -----
$ cat demo.psql
CREATE TABLE sauropods (
    name character varying(80) NOT NULL,
    age int NOT NULL,
    happiness int NOT NULL
);

COPY sauropods (name, age, happiness) FROM stdin;
Brontosaurus    152000000       5
Diplodocus      152000000       14
Rapetosaurus    66000000        3
\.

CREATE TABLE raptors (
    name character varying(80) NOT NULL,
    happiness int NOT NULL
);

COPY raptors (name, happiness) FROM stdin;
Jonas   3
Kyle    7
DeMar   14
Bruno   11
Patrick 8
\.

CREATE TABLE astronauts (
    name character varying(80) NOT NULL,
    species character varying(80) NOT NULL,
    happy boolean NOT NULL,
    trips int NOT NULL
);

COPY astronauts (name, species, happy, trips) FROM stdin;
Gagarin Diplodocus      t       1
Leonov  Diplodocus      t       2
Shepard Brontosaurus    t       2
\.
$ psql -f demo.psql

----- fetch rapture and move into the base directory -----
$ cd ~/rapture

----- Use rapture to generate the sql from an RA statement -----
$ python3 rapture.py sql "\p_{raptors.name} (raptors \natural_join sauropods);"
SELECT raptors.name FROM (SELECT raptors.name, raptors.happiness FROM raptors) AS raptors NATURAL JOIN (SELECT sauropods.name, sauropods.age, sauropods.happiness FROM sauropods) AS sauropods

----- Use rapture to generate a PDF diagraming an RA statement -----
$ python3 rapture.py pdf "\p_{raptors.name} (raptors \natural_join sauropods);"

----- The pdf should be in out.pdf -----
$ ls -la out.pdf
19377506 -rw-r--r--@ 1 peters43  staff  18581 16 Feb 15:31 out.pdf

----- Use the interpreter to execute RA statements as a demo -----
$ python3 rapture.py repl
Welcome to the Rapture REPL.

The REPL uses Rapt to translate and execute relational
algebra statements on a database.

Type help or ? to list commands.

π raptors
 name   | happiness
---------+-----------
 Jonas   |         3
 Kyle    |         7
 DeMar   |        14
 Bruno   |        11
 Patrick |         8
 Jonas   |         3
 Kyle    |         7
 DeMar   |        14
 Bruno   |        11
 Patrick |         8
(10 rows)


SQL: SELECT raptors.name, raptors.happiness FROM raptors

π \s_{happiness > 5} raptors;
  name   | happiness
---------+-----------
 Kyle    |         7
 DeMar   |        14
 Bruno   |        11
 Patrick |         8
 Kyle    |         7
 DeMar   |        14
 Bruno   |        11
 Patrick |         8
(8 rows)


SQL: SELECT raptors.name, raptors.happiness FROM raptors WHERE happiness > 5

π draw out
----- The most recent statement will be emitted as a PDF diagram -----