Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Logica is a logic programming language that compiles to SQL. It runs on DuckDB, Google BigQuery, PostgreSQL and SQLite.

License

NotificationsYou must be signed in to change notification settings

EvgSkv/logica

Repository files navigation

Logica is an open source declarative logic programminglanguage 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 uselogic programming syntax when writing queries and pipelines for databases and datawarehouses.Logica programs run onBigQuery,Postgres andSQLite.

Logica compiles to SQL and gives you access to the power of SQL ecosystemwith the convenience of logic programming syntax.

This is useful becauseSQL engines are magnitudes more powerful than state of the art nativelogic programming engines. For example, BigQuery is a distributed datawarehouse and thus logic programs writtenin Logica can be easily parallelized onto thousands of servers. Postgres and SQLite are among most popular databases, they arecapable of processing substantial volumes of data right on your machine.

We encourage you to try Logica, especially if

  • you already use logic programming and need more computational power,or
  • you already have data in BigQuery, PostgreSQL or SQLite,or
  • you want to learn logic programming and apply it to processing of Big Data.

Support for more SQL dialects and engines is coming in the future.

I have not heard of logic programming. What is it?

Logic programming is a declarative programming paradigm where the program iswritten as a set of logical statements.

Logic programming was developed in academia from the late 60s. Prolog andDatalog are the most prominent examples of logic programming languages. Logicais a language of the Datalog family.

Datalog and relational databases start from the same idea: think of dataas relations and think of data manipulation as a sequence of operations overthese relations. But Datalog and SQL differ in how these operations aredescribed. Datalog is inspired by the mathematical syntax of the first orderpropositional logic and SQL follows the syntax of natural language.

SQL was based on the natural language to give access to databases to the peoplewithout formal training in computer programming or mathematics. This conveniencemay 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 simplelogic programs.

How does Logica work?

Logica compiles the logic program into a SQL expression, so it can be executedon BigQuery, the state of the art SQL engine.

Among database theoreticians Datalog and SQL are known to beequivalent. Andindeed the conversion from Datalog to SQL and back is often straightforward.However there are a few nuances, for example how to treat disjunction andnegation. In Logica we tried to make choices that make understanding of theresulting SQL structure as easy as possible, thus empowering user to writeprograms that are executed efficiently.

Why is it calledLogica?

Logica stands forLogic withaggregation.

How to learn?

Learn basics of Logica with theCoLab tutorial located attutorial folder.See examples of using Logica inexamples folder.

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

Prerequisites

To 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 lineyou will needbq, aBigQuerycommand line tool. For that you need to installGoogle Cloud SDK.

Installation

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

You can install Logica command withpip 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<<<'@Engine("sqlite"); Greet(greeting: "Hello world!")'# To run HelloWorld program on SQLite.python3 -m logica - run Greet<<<'@Engine("sqlite"); Greet(greeting: "Hello world!")'

If yourPATH includes Python'sbin folder then you will also be able torun it simply as

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

Alternatively, you can clone GitHub repository:

git clone https://github.com/evgskv/logicacd 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 with SQLite.

Programprimes.l:

# Using SQLite engine.@Engine("sqlite");# Define numbers 1 to 30.Number(x + 1) :- x in Range(30);# Defining composite numbers.Composite(a * b) distinct :- Number(a), Number(b), a > 1, b > 1;# Defining primes as "not composite".Prime(n) distinct :- Number(n), n > 1, ~Composite(n);

Runningprimes.l

$ logica primes.l run Prime+-------+| prime|+-------+|     2||     3||     5||     7||    11||    13||    17||    19||    23||    29|+-------+

Cities with largest beer variety

Let's use beer variety dataset fromplotly.

Let us find top 5 states with largest variety of beers. In each state we will pick city with the largestvariety in the state.

To run this example you will need to install DuckDB if you don't yet have it on your system. Luckily installing DuckDB is easy:

python3 -m pip install duckdb

Programbeer.l:

@Engine("duckdb");@Ground(Beer);Beer(..r) :-     `('https://github.com/plotly/datasets/blob/master/beers.csv?raw=true')`(..r);BeersInState(state) += 1 :- Beer(state:);BeersInCity(state, city) += 1 :- Beer(state:, city:);ArgMax5(x) = ArgMaxK(x, 5);BestCityForBeer(state:, city:,                city_beers: BeersInCity(state, city),                state_beers: BeersInState(state)) :-    state in ArgMax5{s -> BeersInState(s)},    city = ArgMax{c -> BeersInCity(state, c)};

Runningbeer.l:

# logica beer.l run BestCityForBeer+-------+--------------+------------+-------------+| state | city         | city_beers | state_beers |+-------+--------------+------------+-------------+| IN    | Indianapolis | 43         | 139         || CO    | Boulder      | 41         | 265         || CA    | San Diego    | 42         | 183         || TX    | Austin       | 25         | 130         || MI    | Grand Rapids | 66         | 162         |+-------+--------------+------------+-------------+

Feedback

Feel 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 distributedunder the Apache 2.0 license found in the LICENSE file.

This is not an officially supported Google product.


[8]ページ先頭

©2009-2025 Movatter.jp