#17 Google Logica – Language of Big Data

Google Logica is an open source declarative logic programming language in the field of data manipulation. Yedalog was predeccessor of Logica.

google logica

Google Logica is an open source declarative logic programming language in the field of data manipulation. Yedalog was predecessor of Logica. Yedalog was also created at Google before. Logica is for data scientists, engineers and other specialists who want to use logic programming syntax while writing queries and pipelines to run on Big Query.

Logica stands for Logic with aggregation.

Why to check Google Logica?

Compiler compiles Logica to StandardSQL and gives developer access to the power of BigQuery engine with the easy usability of logic programming syntax. This is helpful because BigQuery is much more powerful than state of the art native logic programming engines.

Explore logica if you find yourself in one or the below conditions:

Already using a programming language and require more computational power.

Readability is a concern while using SQL.

You are keen to learn a logic programming and apply it to Big Data Use cases.

Hearing Logic programming for the first time. What is it?

Logic programming is a declarative programming standard where the program is written with a set of logical executable statements.

This programming concept was first developed in academia from the late 60s. Prolog and Datalog are the most common examples of logic programming languages.

Logica is one of the languages of the Datalog family.

Datalog and relational databases start from the same thought process,

Conceptualize data as relations and data manipulation as a sequence of operations over these relations. But Datalog and SQL differ from operation aspects. Datalog is inspired completely by the mathematical syntax whereas SQL follows the syntax of natural language.

SQL was based on the natural language to give access to databases to the developer community without formal training in any programming languages. This usability may become pricy when the logic that you want to express is important. There are many examples present over internet of hard-to-read SQL queries that correspond to simple logic programs.

How does Google Logica work?

Logica compiles written logic program into a SQL programming expression, so it can be executed on BigQuery (SQL Engine).

Among database practitioners Datalog and SQL are known to be very much similar. The conversion from Datalog to SQL and back is often straight. However there are a few small differences, for e.g. how to utilize disjunction and negation. In Logica, Google has tried to make SQL structure as easy as possible, thus empowering user to write programs that are executed very efficiently.

How to learn?

Learn basics of Google Logica with the CoLab tutorial located at tutorial folder. See e.g. of using Logica in examples folder.

Tutorial and examples show how to access Logica from CoLab. You can also install Logica command line tool.

Setup Prerequisites

To run Logica programs on BigQuery you will need a Google Cloud Project. Once you have a project you can run Logica programs in CoLab providing your project id.

To run Logica locally you need Python3.

To initiate Logica predicates execution from the command line you will need bq, a BigQuery command line tool. For that you need to install Google Cloud SDK.

Installation

Google Cloud Project is the only thing you need to run Google Logica in Colab, see Hello World example.

You can install Logica command with pip as follows.

# Install.
python3 -m pip install logica
# Run:
# To see usage message.
python3 -m logica
# To print SQL for HelloWorld program.
python3 -m logica - print Greet <<<'Greet(greeting: "Hello world!")'

If your PATH includes Python’s bin folder then you will also be able to run it simply as

logica - print Greet <<<'Greet(greeting: "Hello world!")'

Alternatively, you can clone GitHub repository:

git clone https://github.com/evgskv/logica
cd logica
./logica - print Greet <<<'Greet(greeting: "Hello world!")'

Code samples

Here a couple examples of how Logica code looks like.

Prime numbers

Find prime numbers less than 30.

Program primes.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,
    Mod(x, y) == 0
  );

Conclusion

The Logica tutorial is already available on Google.

Google Open Source Logica

Developers should start learning Logica if they are working with Google platforms and Big Data world. This is here to stay. Keep looking for more Teknonauts.com

Leave a Reply

Your email address will not be published. Required fields are marked *