We present
Logica, a novel
open source Logic Programming language. A successor to
Yedalog (a language developed at Google earlier) it is a Datalog-like logic programming language. Logica code compiles to SQL and runs on Google BigQuery (with experimental support for PostgreSQL and SQLite), but it is much more concise and supports the clean and reusable abstraction mechanisms that SQL lacks. It supports modules and imports, it can be used from an interactive Python notebook and it even makes
testing your queries natural and easy.
“Data is the new oil”, they say, and SQL is so far the
lingua franca for working with data. When SQL (or “Structured English Query Language”, as it was first named) was invented in the 1970s, its authors might not have imagined the popularity that it would reach half a century later. Today, systems ranging from tiny smart watch applications to enterprise IT solutions, read and write their data using SQL. Even the browser that you are using to read this post now might
have a working built-in SQL database in it.
Despite the widespread adoption, SQL is not flawless. Constructing statements from long chains of English words (which are often capitalized to keep the old-fashioned COBOL spirit of the 70s alive!) can be very verbose—a single query spanning hundreds of lines is a routine occurrence. The main flaw of SQL, however, lies in its very limited
support for abstraction.
Good programming is about creating small, understandable, reusable pieces of logic that can be tested, given names, and organized into packages which can later be used to construct more useful pieces of logic. SQL resists this workflow. Although you can encapsulate certain repeated computations into
views and
functions, the syntax and support for these can vary among implementations, the notions of
packages and
imports are generally nonexistent, and higher-level constructions (e.g. passing a function to a function) are impossible.
This inherent resistance to decomposition of logic into bite-sized pieces is what leads into the contrived, lengthy queries, the copy-pasted chunks of code and, eventually, unmaintainable, un
structured (note the irony) SQL codebases. To make things worse, SQL code is rarely tested, because “
testing SQL queries” sounds rather esoteric to most engineers, at best. Because of that, a number of
alternative query languages and
libraries have been developed. Of those,
systems based on logic programming perhaps come the closest to addressing SQL’s limitations.
Logic programming languages solve problems of SQL by using syntax of mathematical propositional logic rather than natural English language. The language of formal logic was designed by mathematicians specifically to make expression of complex statements easier and suits this purpose much better than natural language. Logica extends classical Logic programming syntax further, most notably with aggregation, hence the name, which stands for
Logica = Logic + Aggregation.
Let us see how it all works. SQL operates with
relations, which are sets of
rows. In logic programming the analog of a
relation is a
predicate. While a predicate is a set of rows, we
think of it as a
logical condition, which describes the rows of a relation. Here is, for example, the definition of a simple predicate:
MagicNumber(x: 2); MagicNumber(x: 3); MagicNumber(x: 5); |
The definition claims that the condition MagicNumber(x) must hold when X is precisely either 2, 3, or 5. That means, if we were to query this predicate (i.e. request all values of X that satisfy it), the output should be a “relation” with a single column X and rows 2, 3, and 5. The SQL equivalent would be:SELECT 2 AS x UNION ALL SELECT 3 AS x UNION ALL SELECT 5 AS x; |
Rather than listing the individual values, we could have defined the predicate by encoding a logical condition upon X as follows:MagicNumber(x:) :- x in [2, 3, 5]; |
Now, here is where the magic starts. Firstly, any table in your database is itself already a predicate, so the following definition:MagicComment(comment_text:) :- `comments`(user_id:, comment_text:), user_id == 5; |
Defines a predicate MagicComment, which includes precisely those comment_text values, which are present in the comments table where user_id == 5. In SQL this would read:SELECT comment_text FROM comments WHERE user_id = 5; |
Observe what happens if we replace the condition “user_id == 5” in our predicate with MagicNumber(x: user_id):MagicComment(comment_text:) :- `comments`(user_id:, comment_text:), MagicNumber(x: user_id); |
Here, we are querying for comments of users whose ID is one of the “magic numbers” we just defined above. Note how easily we could reuse a previously defined piece of code without having to copy anything around. We could now even extract the MagicNumber to a common module and import it in wherever it is needed:import my_project.magic.MagicNumber; |
As a final example, let us mock the comments table, in a unittest of a query.import my_project.magic.MagicComment;
MockComments(user_id: 1, comment_text: "Hello"); MockComments(user_id: 2, comment_text: "Logic"); MockComments(user_id: 3, comment_text: "Programming");
MagicCommentTest := MagicComment(`comments`: MockComments); |
If we query the MagicComment predicate here, it will not try to read the comments table in the database. Instead, it will use the predicate we just defined, thus letting us verify its correctness by testing the output (it must include two rows “Logic” and “Programming”). Observe how natural and frictionless many of the good programming practices become with Logica, and compare that to what you would have to do to achieve the same using bare SQL.
There is much more to Logica, so make sure you give it a try—chances are, you will love it! Start with
this tutorial to learn Logica. Even if you do not end up using it in your next project, learning a new powerful language may open your mind to new ideas and perspectives on data processing and computing in general.
The simple examples above are only a small sample of how concise Logica code can be over SQL for complex queries. In particular, we did not even touch the topic of aggregations in this article. For all of this see
examples section of the Logica
open source repository.
We also hope that some of the readers consider contributing to Logica development. That’s what open source is all about!
By Konstantin Tretyakov and Evgeny Skvortsov – Logica Open Source Project