Package: logica
Logica: language of Big DataLogica is an open source declarative logic programming
language for data manipulation. Logica is a successor toYedalog,
a language created at Google earlier.Why?Logica is for engineers, data scientists and other specialists who want to use
logic programming syntax when writing queries and pipelines to run onBigQuery.Logica compiles to StandardSQL and gives you access to the power of BigQuery
engine with the convenience of logic programming syntax. This is useful because
BigQuery is magnitudes more powerful than state of the art native
logic programming engines.We encourage you to try Logica, especially ifyou already use logic programming and need more computational power,oryou use SQL, but feel unsatisfied about its readability,oryou want to learn logic programming and apply it to processing of Big Data.In the future we plan to support more SQL dialects and engines.I have not heard of logic programming. What is it?Logic programming is a declarative programming paradigm where the program is
written as a set of logical statements.Logic programming was developed in academia from the late 60s. Prolog and
Datalog are the most prominent examples of logic programming languages. Logica
is a language of the Datalog family.Datalog and relational databases start from the same idea: think of data
as relations and think of data manipulation as a sequence of operations over
these relations. But Datalog and SQL differ in how these operations are
described. Datalog is inspired by the mathematical syntax of the first order
propositional logic and SQL follows the syntax of natural language.SQL was based on the natural language to give access to databases to the people
without formal training in computer programming or mathematics. This convenience
may become costly when the logic that you want to express is non trivial.
There are many examples of hard-to-read SQL queries that correspond to simple
logic programs.How does Logica work?Logica compiles the logic program into a SQL expression, so it can be executed
on BigQuery, the state of the art SQL engine.Among database theoreticians Datalog and SQL are known to beequivalent. And
indeed the conversion from Datalog to SQL and back is often straightforward.
However there are a few nuances, for example how to treat disjunction and
negation. In Logica we tried to make choices that make understanding of the
resulting SQL structure as easy as possible, thus empowering user to write
programs that are executed efficiently.Why is it calledLogica?Logicastands forLogicwithaggregation.How to learn?Learn basics of Logica with theCoLab tutoriallocated attutorialfolder.
See examples of using Logica inexamplesfolder.Tutorial and examples show how to access Logica from CoLab. You can also install Logica command line tool.PrerequisitesTo run Logica programs on BigQuery you will need aGoogle Cloud Project.
Once you have a project you can run Logica programs in CoLab providing your project id.To run Logica locally you needPython3.To initiate Logica predicates execution from the command line
you will needbq, a
BigQuerycommand line tool. For that you need to installGoogle Cloud SDK.InstallationGoogle Cloud Project is the only thing you need to run Logica in Colab, seeHello World example.You can install Logica command withpipas follows.# Install.python3-mpipinstalllogica# Run:# To see usage message.python3-mlogica# To print SQL for HelloWorld program.python3-mlogica-printGreet<<<'Greet(greeting: "Hello world!")'If yourPATHincludes Python'sbinfolder then you will also be able to
run it simply aslogica-printGreet<<<'Greet(greeting: "Hello world!")'Alternatively, you can clone GitHub repository:gitclonehttps://github.com/evgskv/logicacdlogica
./logica-printGreet<<<'Greet(greeting: "Hello world!")'Code samplesHere a couple examples of how Logica code looks like.Prime numbersFind prime numbers less than 30.Programprimes.l:# Define natural numbers from 1 to 29.
N(x) :- x in Range(30);
# Define primes.
Prime(prime: x) :-
  N(x),
  x > 1,
  ~(
    N(y),
    y > 1,
    y != x,
    x % y == 0
  );Runningprimes.l$logicaprimes.lrunPrime
+-------+|prime|+-------+|2||3||5||7||11||13||17||19||23||29|+-------+News mentionsWho was mentioned in the news in 2020 the most?
Let's queryGDELT Projectdataset.Programmentions.l@OrderBy(Mentions, "mentions desc");
@Limit(Mentions, 10);
Mentions(person:, mentions? += 1) distinct :-
  gdelt-bq.gdeltv2.gkg(persons:, date:),
  Substr(ToString(date), 0, 4) == "2020",
  the_persons == Split(persons, ";"),
  person in the_persons;Runningmentions.l$ logica mentions.l run Mentions
+----------------+----------+
|     person     | mentions |
+----------------+----------+
| donald trump   |  3624228 |
| joe biden      |  1591320 |
| los angeles    |  1221998 |
| george floyd   |   923472 |
| boris johnson  |   845955 |
| barack obama   |   541672 |
| vladimir putin |   486428 |
| bernie sanders |   409224 |
| andrew cuomo   |   375594 |
| nancy pelosi   |   375373 |
+----------------+----------+Note that cities of Los Angeles and Las Vegas are mentioned in this table due to known
missclasification issue in the GDELT data analysis.FeedbackFeel free to creategithub issuesfor bugs and feature requests.You questions and comments are welcome at ourgithub discussions!Unless otherwise noted, the Logica source files are distributed
under the Apache 2.0 license found in the LICENSE file.This is not an officially supported Google product.