{"id":13439895,"url":"https://github.com/EvgSkv/logica","last_synced_at":"2025-03-20T09:31:01.366Z","repository":{"id":38353307,"uuid":"302758707","full_name":"EvgSkv/logica","owner":"EvgSkv","description":"Logica is a logic programming language that compiles to SQL. It runs on Google BigQuery, PostgreSQL and SQLite.","archived":false,"fork":false,"pushed_at":"2024-05-29T04:49:10.000Z","size":5422,"stargazers_count":1686,"open_issues_count":32,"forks_count":89,"subscribers_count":37,"default_branch":"main","last_synced_at":"2024-05-29T16:29:46.428Z","etag":null,"topics":["bigquery","datalog","language","logic-programming","logica","postgresql","presto","prolog","prolog-implementation","sql","sqlite","trino"],"latest_commit_sha":null,"homepage":"https://logica.dev","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EvgSkv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-09T21:39:24.000Z","updated_at":"2024-05-30T18:56:20.004Z","dependencies_parsed_at":"2024-05-29T05:29:04.935Z","dependency_job_id":null,"html_url":"https://github.com/EvgSkv/logica","commit_stats":{"total_commits":576,"total_committers":20,"mean_commits":28.8,"dds":0.28125,"last_synced_commit":"e2df0ce7121e6d605ec819ca0688522cf425a20f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvgSkv%2Flogica","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvgSkv%2Flogica/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvgSkv%2Flogica/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EvgSkv%2Flogica/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EvgSkv","download_url":"https://codeload.github.com/EvgSkv/logica/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221745191,"owners_count":16873733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bigquery","datalog","language","logic-programming","logica","postgresql","presto","prolog","prolog-implementation","sql","sqlite","trino"],"created_at":"2024-07-31T03:01:17.980Z","updated_at":"2025-03-20T09:31:01.360Z","avatar_url":"https://github.com/EvgSkv.png","language":"Jupyter Notebook","readme":"\u003c!--\nCopyright 2020 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n     http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n--\u003e\n\n# Logica: language of Big Data\n\nLogica is an open source declarative logic programming\nlanguage for data manipulation. Logica is a successor to\n[Yedalog](https://research.google/pubs/pub43462/),\na language created at Google earlier.\n\n## Why?\n\nLogica is for engineers, data scientists and other specialists who want to use\nlogic programming syntax when writing queries and pipelines for databases and datawarehouses.\nLogica programs run on\n[BigQuery](https://cloud.google.com/bigquery), [Postgres](https://postgresql.org) and [SQLite](https://www.sqlite.org/).\n\nLogica compiles to SQL and gives you access to the power of SQL ecosystem\nwith the convenience of logic programming syntax. \n\nThis is useful because\nSQL engines are magnitudes more powerful than state of the art native\nlogic programming engines. For example, BigQuery is a distributed datawarehouse and thus logic programs written\nin Logica can be easily parallelized onto thousands of servers. Postgres and SQLite are among most popular databases, they are\ncapable of processing substantial volumes of data right on your machine.\n\nWe encourage you to try Logica, especially if\n\n*   you already use logic programming and need more computational power, **or**\n*   you already have data in BigQuery, PostgreSQL or SQLite, **or**\n*   you want to learn logic programming and apply it to processing of Big Data.\n\nSupport for more SQL dialects and engines is coming in the future.\n\n## I have not heard of logic programming. What is it?\n\nLogic programming is a declarative programming paradigm where the program is\nwritten as a set of logical statements.\n\nLogic programming was developed in academia from the late 60s. Prolog and\nDatalog are the most prominent examples of logic programming languages. Logica\nis a language of the Datalog family.\n\nDatalog and relational databases start from the same idea: think of data\nas relations and think of data manipulation as a sequence of operations over\nthese relations. But Datalog and SQL differ in how these operations are\ndescribed. Datalog is inspired by the mathematical syntax of the first order\npropositional logic and SQL follows the syntax of natural language.\n\nSQL was based on the natural language to give access to databases to the people\nwithout formal training in computer programming or mathematics. This convenience\nmay become costly when the logic that you want to express is non trivial.\nThere are many examples of hard-to-read SQL queries that correspond to simple\nlogic programs.\n\n## How does Logica work?\n\nLogica compiles the logic program into a SQL expression, so it can be executed\non BigQuery, the state of the art SQL engine.\n\nAmong database theoreticians Datalog and SQL are known to be _equivalent_. And\nindeed the conversion from Datalog to SQL and back is often straightforward.\nHowever there are a few nuances, for example how to treat disjunction and\nnegation. In Logica we tried to make choices that make understanding of the\nresulting SQL structure as easy as possible, thus empowering user to write\nprograms that are executed efficiently.\n\n## Why is it called _Logica_?\n\n_Logica_ stands for _**Logic** with **a**ggregation_.\n\n## How to learn?\n\nLearn basics of Logica with the [CoLab tutorial](https://colab.research.google.com/github/EvgSkv/logica/blob/main/tutorial/Logica_DuckDB_tutorial.ipynb) located at [`tutorial`](https://github.com/EvgSkv/logica/tree/main/tutorial) folder.\nSee examples of using Logica in [`examples`](https://github.com/EvgSkv/logica/tree/main/examples) folder.\n\nTutorial and examples show how to access Logica from CoLab. You can also install Logica command line tool.\n\n## Prerequisites\n\nTo run Logica programs on BigQuery you will need a \n[Google Cloud Project](https://console.cloud.google.com/projectcreate).\nOnce you have a project you can run Logica programs in CoLab providing your project id.\n\nTo run Logica locally you need [Python3](https://www.python.org/downloads/).\n\nTo initiate Logica predicates execution from the command line \nyou will need `bq`, a\nBigQuery [command line tool](https://cloud.google.com/bigquery/docs/bq-command-line-tool). For that you need to install \n[Google Cloud SDK](https://cloud.google.com/sdk/docs/install).\n\n## Installation\n\nGoogle Cloud Project is the only thing you need to run Logica in Colab, see\n[Hello World example](https://colab.research.google.com/github/EvgSkv/logica/blob/main/examples/Logica_example_Hello_World.ipynb).\n\nYou can install Logica command with `pip` as follows.\n\n```sh\n# Install.\npython3 -m pip install logica\n# Run:\n# To see usage message.\npython3 -m logica\n# To print SQL for HelloWorld program.\npython3 -m logica - print Greet \u003c\u003c\u003c'@Engine(\"sqlite\"); Greet(greeting: \"Hello world!\")'\n# To run HelloWorld program on SQLite.\npython3 -m logica - run Greet \u003c\u003c\u003c'@Engine(\"sqlite\"); Greet(greeting: \"Hello world!\")'\n```\n\nIf your `PATH` includes Python's `bin` folder then you will also be able to\nrun it simply as\n\n```sh\nlogica - print Greet \u003c\u003c\u003c'Greet(greeting: \"Hello world!\")'\n```\n\nAlternatively, you can clone GitHub repository:\n\n```sh\ngit clone https://github.com/evgskv/logica\ncd logica\n./logica - print Greet \u003c\u003c\u003c'Greet(greeting: \"Hello world!\")'\n```\n\n\n## Code samples\n\nHere a couple examples of how Logica code looks like.\n\n### Prime numbers\n\nFind prime numbers less than 30 with SQLite.\n\nProgram `primes.l`:\n```\n# Using SQLite engine.\n@Engine(\"sqlite\");\n\n# Define numbers 1 to 30.\nNumber(x + 1) :- x in Range(30);\n\n# Defining composite numbers.\nComposite(a * b) distinct :- Number(a), Number(b), a \u003e 1, b \u003e 1;\n\n# Defining primes as \"not composite\".\nPrime(n) distinct :- Number(n), n \u003e 1, ~Composite(n);\n```\n\nRunning `primes.l`\n```sh\n$ logica primes.l run Prime\n+-------+\n| prime |\n+-------+\n|     2 |\n|     3 |\n|     5 |\n|     7 |\n|    11 |\n|    13 |\n|    17 |\n|    19 |\n|    23 |\n|    29 |\n+-------+\n```\n\n### Cities with largest beer variety\n\nLet's use beer variety dataset from [plotly](https://github.com/plotly/datasets/blob/master/beers.csv).\n\nLet us find top 5 states with largest variety of beers. In each state we will pick city with the largest\nvariety in the state.\n\nTo run this example you will need to install DuckDB if you don't yet have it on your system. Luckily installing DuckDB is easy:\n\n```\npython3 -m pip install duckdb\n```\n\nProgram `beer.l`:\n\n```\n@Engine(\"duckdb\");\n\n@Ground(Beer);\nBeer(..r) :- \n    `('https://github.com/plotly/datasets/blob/master/beers.csv?raw=true')`(..r);\n\nBeersInState(state) += 1 :- Beer(state:);\nBeersInCity(state, city) += 1 :- Beer(state:, city:);\n\nArgMax5(x) = ArgMaxK(x, 5);\nBestCityForBeer(state:, city:,\n                city_beers: BeersInCity(state, city),\n                state_beers: BeersInState(state)) :-\n    state in ArgMax5{s -\u003e BeersInState(s)},\n    city = ArgMax{c -\u003e BeersInCity(state, c)};\n```\n\nRunning `beer.l`:\n\n```\n# logica beer.l run BestCityForBeer\n+-------+--------------+------------+-------------+\n| state | city         | city_beers | state_beers |\n+-------+--------------+------------+-------------+\n| IN    | Indianapolis | 43         | 139         |\n| CO    | Boulder      | 41         | 265         |\n| CA    | San Diego    | 42         | 183         |\n| TX    | Austin       | 25         | 130         |\n| MI    | Grand Rapids | 66         | 162         |\n+-------+--------------+------------+-------------+\n```\n\n\u003c!--\n### News mentions\n\nWho was mentioned in the news in 2020 the most?\nLet's query [GDELT Project](https://www.gdeltproject.org/) dataset.\n\nProgram `mentions.l`\n```\n@OrderBy(Mentions, \"mentions desc\");\n@Limit(Mentions, 10);\nMentions(person:, mentions? += 1) distinct :-\n  gdelt-bq.gdeltv2.gkg(persons:, date:),\n  Substr(ToString(date), 0, 4) == \"2020\",\n  the_persons == Split(persons, \";\"),\n  person in the_persons;\n```\n\nRunning `mentions.l`\n```\n$ logica mentions.l run Mentions\n+----------------+----------+\n|     person     | mentions |\n+----------------+----------+\n| donald trump   |  3624228 |\n| joe biden      |  1591320 |\n| los angeles    |  1221998 |\n| george floyd   |   923472 |\n| boris johnson  |   845955 |\n| barack obama   |   541672 |\n| vladimir putin |   486428 |\n| bernie sanders |   409224 |\n| andrew cuomo   |   375594 |\n| nancy pelosi   |   375373 |\n+----------------+----------+\n```\n\nNote that cities of Los Angeles and Las Vegas are mentioned in this table due to known\nmissclasification issue in the GDELT data analysis.\n--\u003e \n## Feedback\n\nFeel free to create [github issues](https://github.com/EvgSkv/logica/issues)\nfor bugs and feature requests.\n\nYou questions and comments are welcome at our [github discussions](https://github.com/EvgSkv/logica/discussions)!\n\n\n\n---\n\nUnless otherwise noted, the Logica source files are distributed\nunder the Apache 2.0 license found in the LICENSE file.\n\nThis is not an officially supported Google product.\n\n\n","funding_links":[],"categories":["HarmonyOS","Relational data","Jupyter Notebook","language","sqlite"],"sub_categories":["Windows Manager","SQL transpilers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEvgSkv%2Flogica","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEvgSkv%2Flogica","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEvgSkv%2Flogica/lists"}