RWTH Aachen Computer Science i5/dbis assets for Lecture Datenbanken und Informationssysteme
Project description
DBIS Relational Algebra
This library provides a Python implementation of the relational algebra.
Features
- Create expressions of the relational algebra in python.
- Load data from SQLite tables.
- Evaluate expressions on the data.
- Convert these expressions to text in LaTeX math mode.
- Convert a relation / the result of an expression to a Markdown table.
Installation
Install via pip:
pip install dbis-relational-algebra
Usage
Overview of supported operators
- Cross Product / Cartesian Product (
*
) - Difference (
-
) - Division (
/
) - Intersection (
&
) - Left Semijoin
- Natural Join
- Projection
- Rename
- Right Semijoin
- Selection
- Theta Join
- Union (
|
)
The set operators Union, Intersection, and Difference require the relations to be union-compatible.
Formulas
For the Theta Join and the Selection, a formula is used to specify the join or selection condition. These formulas can be created using the following operators:
- And
- Or
- Not
- Equals
- GreaterEquals
- GreaterThan
- LessEquals
- LessThan
In the comparators, two values have to be specified. At least one of these values must be a python str
, which references a column of the relation.
Loading data & Evaluating an expression
To load data, an SQLite connection can be used (recommended). This connection must be passed to the relational algebra expression for the evaluation.
It is also possible to load a relation with data by hand (not recommended):
relation = Relation(name="R")
relation.add_attributes(["a", "b", "c"])
relation.add_rows([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])
An expression can be created by using the operators and formulas listed above. The expression can then be evaluated on the data:
# Cross Product RxS, see above
expression = Relation("R") * Relation("S")
result = expression.evaluate(sql_con=connection)
# Theta Join R.a = S.b, see above
expression = ThetaJoin("R", "S", Not(Equals("R.a", "S.b")))
result = expression.evaluate(sql_con=connection)
The rows and column names of a relation (result) are accessible using the following attributes:
result.attributes # list of column names (str)
result.rows # set of rows (tuple)
Best practices:
- After joining two relations or the cross product of two relations, you should always give column names that appear in both relations a new distinct name.
- After joining two relations, the cross product of two relations, or some set operation on two relations, you should always give the resulting relation a new distinct name.
- When referencing a column in a comparator, it is recommended that this column should be referred to using a detailed description, i.e. refer to column
a
of relationR
as"R.a"
instead of"a"
.
Developer Notes
A few design choices were made:
- Internally, the data is stored in a pandas DataFrame. This accelerates the relational algebra operators greatly.
- In relational algebra, a column
a
from a relationR
can be referred to asa
andR.a
. Internally, the column name is always stored using the full name, i.e.R.a
. This is done to avoid ambiguities when a columna
is present in multiple relations. - When joining two relations (or also cross product), the relational algebra provides no guidelines on how the resulting relation should be named. Thus, if
a
is a column of relationR
, joining relationsR
andS
results in a relation, whereR.a
andS.a
might refer to this columna
(depending on ifa
also references a column inS
). Thus, generally speaking, joining two relationsR
andS
will internally result in a relation namedR+S
, and the columnR.a
will now be namedR+S.a
(if there is no columnS.a
).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dbis-relational-algebra-1.1.6.tar.gz
.
File metadata
- Download URL: dbis-relational-algebra-1.1.6.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf049fbfed7ae942c1b4f6c3610f4376405d665c9e1a9895e8cef4d3edd2ca69 |
|
MD5 | 81817757f02d6960debe64612cb81640 |
|
BLAKE2b-256 | aed9a3248b1773d88b37b04d7d06fbff85bb84ca1ab6e355023df36b992dd606 |
File details
Details for the file dbis_relational_algebra-1.1.6-py3-none-any.whl
.
File metadata
- Download URL: dbis_relational_algebra-1.1.6-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ee06c2b7f582ea3b69e8a92faa7e9cc266aa044927e5e01fb5fee26679f0096 |
|
MD5 | 4b1796340b877badfcd35caca9cdb6a0 |
|
BLAKE2b-256 | 85a63776b7bc9bfe59ef992e082b20269526c4ec330c21aefeaf2002286f11bd |