Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch up dev. #398

Merged
merged 272 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
272 commits
Select commit Hold shift + click to select a range
f66295c
first commit
ksyusha123 Oct 7, 2023
b24a42d
second commit
ksyusha123 Oct 7, 2023
d172734
Refining.
EvgSkv Oct 11, 2023
aef6ce2
fixed and added recursion
ksyusha123 Oct 11, 2023
070ccdc
Refining.
EvgSkv Oct 11, 2023
ac3e3f9
Refining.
EvgSkv Oct 17, 2023
61b8c57
Refining.
EvgSkv Oct 17, 2023
db77801
Added type retrieval to main script (#30)
Themplarer Oct 18, 2023
a4d48a8
pg types parser
Oct 18, 2023
d0a1197
refactoring
Oct 18, 2023
a521924
Merge remote-tracking branch 'origin/pg-types-parser' into type_retri…
artemworld Oct 25, 2023
6c535d6
tst
artemworld Oct 25, 2023
3f73256
fix
Themplarer Oct 25, 2023
bab802f
Refining.
EvgSkv Oct 25, 2023
01f58e4
Fixing tilde parsing bug.
EvgSkv Oct 28, 2023
47c9ac6
Refining.
EvgSkv Nov 1, 2023
bc365d1
Better recursion unfolding error message.
EvgSkv Nov 1, 2023
d1a9d76
Better recursion unfolding error message.
EvgSkv Nov 1, 2023
0c0506b
Better recursion unfolding error message.
EvgSkv Nov 1, 2023
924ab94
described SQL queries and isolated parameters
Themplarer Nov 2, 2023
b4c38b3
Merge pull request #32 from ksyusha123/printing_test
Themplarer Nov 2, 2023
ff1964c
removed query details
Themplarer Nov 2, 2023
f5d9f50
Merge pull request #33 from ksyusha123/expert-review-fixes
Themplarer Nov 2, 2023
9fe9763
fixed formatting and added copywrites
ksyusha123 Nov 2, 2023
0eee8ba
Fix Array aggregate operator type.
EvgSkv Nov 5, 2023
2461c7b
chore
artemworld Nov 7, 2023
338e99e
Fix extraction with positional args.
EvgSkv Nov 7, 2023
6edd49e
Removing Enumerate function.
EvgSkv Nov 8, 2023
0e64587
Repare psql RangeOf.
EvgSkv Nov 8, 2023
d261c49
Update recursion error message.
EvgSkv Nov 11, 2023
c885310
Update recursion error message.
EvgSkv Nov 11, 2023
63b23bd
Update recursion error message.
EvgSkv Nov 11, 2023
5d97339
Making psql try to connect before installing DB.
EvgSkv Nov 12, 2023
e1464d7
fixes
Themplarer Nov 13, 2023
fb02a3a
fix
Themplarer Nov 13, 2023
bc6787e
optimization
Themplarer Nov 14, 2023
96d0587
fix
Themplarer Nov 14, 2023
f391861
Image drawing utility.
EvgSkv Nov 14, 2023
cca8e7a
Image drawing utility.
EvgSkv Nov 14, 2023
67bde21
Image drawing utility.
EvgSkv Nov 14, 2023
70edfa4
Image drawing utility.
EvgSkv Nov 14, 2023
2670fcc
Merge pull request #34 from ksyusha123/reformat
Themplarer Nov 14, 2023
c08a288
Image drawing utility.
EvgSkv Nov 14, 2023
58bbedf
Image drawing utility.
EvgSkv Nov 14, 2023
2001298
Image drawing utility.
EvgSkv Nov 15, 2023
1238c00
Utilities to draw hexagons.
EvgSkv Nov 18, 2023
1db76ec
Utilities to draw hexagons.
EvgSkv Nov 18, 2023
c6e3544
review fixes
ksyusha123 Nov 22, 2023
3d6c820
review fixes 2
ksyusha123 Nov 22, 2023
14f299b
rewrote types' unpacking
Themplarer Nov 22, 2023
e32eef5
review fixes 2
ksyusha123 Nov 22, 2023
c32b033
Merge pull request #35 from ksyusha123/review-fixes
ksyusha123 Nov 23, 2023
6924bf4
review fixes
Themplarer Nov 27, 2023
0c0104a
fix
Themplarer Nov 27, 2023
30c447a
Merge pull request #300 from EvgSkv/ti2023
EvgSkv Nov 27, 2023
298dd98
fix
Nov 28, 2023
254ce73
Merge pull request #37 from ksyusha123/reviewww
ksyusha123 Nov 28, 2023
f784ded
Type retrieval for Postgres
EvgSkv Nov 28, 2023
f332edb
Merge pull request #302 from EvgSkv/main
EvgSkv Dec 5, 2023
f708416
Refining.
EvgSkv Dec 5, 2023
c878ce7
Fix a type error freezing bug.
EvgSkv Dec 5, 2023
66ac55a
Optimize equality resolution.
EvgSkv Dec 5, 2023
07d0cbf
Making DateAddDay to work on SQLite engine.
EvgSkv Dec 7, 2023
e3cb5bf
Merge pull request #303 from EvgSkv/ti2023
EvgSkv Dec 7, 2023
40b4b87
bq templates
ksyusha123 Dec 4, 2023
7f920c0
Added types retrieving
Themplarer Dec 10, 2023
380818a
pg review fixes
Dec 13, 2023
97f20e4
Merge pull request #39 from ksyusha123/pg-review-fixes
ksyusha123 Dec 14, 2023
8e5232a
Capitalizing module prefix.
EvgSkv Dec 19, 2023
4fd1ea9
Avatar library.
EvgSkv Dec 20, 2023
20118f6
upgrade
ksyusha123 Dec 21, 2023
05051a8
fixed authentication in colab
ksyusha123 Dec 21, 2023
d1a1f1c
Code review fixes
Themplarer Dec 25, 2023
0781554
Merge branch 'bigquery' into bq-upgrade
ksyusha123 Dec 29, 2023
5d23f43
Merge pull request #40 from ksyusha123/bq-upgrade
ksyusha123 Dec 29, 2023
dfa8abb
fix
ksyusha123 Dec 29, 2023
886012e
refactoring
ksyusha123 Dec 30, 2023
5e869a2
Merge branch 'bigquery'
ksyusha123 Dec 30, 2023
d7c745d
psql creds
ksyusha123 Jan 4, 2024
c4746c3
fix connection string
ksyusha123 Jan 5, 2024
abb5444
Merge pull request #41 from ksyusha123/psql-creds
ksyusha123 Jan 6, 2024
d415913
Updating avatar library.
EvgSkv Jan 9, 2024
aea8b02
Add date difference function.
EvgSkv Jan 9, 2024
8ee26fb
review fixes
ksyusha123 Jan 12, 2024
081ed2e
Merge pull request #304 from ksyusha123/main
EvgSkv Jan 13, 2024
70c77f8
Add disjuction to avatar.py.
EvgSkv Jan 19, 2024
e78007d
Add AsJson method to avatar.Literal.
EvgSkv Jan 24, 2024
a43e3c8
Add options argument to basic Graph drawing methods.
EvgSkv Mar 7, 2024
8fbc3c0
Add options argument to basic Graph drawing methods.
EvgSkv Mar 7, 2024
fa7f14d
Merge pull request #307 from EvgSkv/ti2023
EvgSkv Mar 7, 2024
c0c3d6f
Fixing unprovoked typechecking.
EvgSkv Apr 5, 2024
c664bd5
Adding explanation of use of BigQuery functions.
EvgSkv Apr 17, 2024
37d81af
init duckdb
yilinxia May 8, 2024
8c2220f
Update logica.vim
EvgSkv May 10, 2024
bb99de6
Make logica.py run on remote psql connection.
EvgSkv May 17, 2024
5ee1a4f
Merge pull request #311 from EvgSkv/ti2023
EvgSkv May 17, 2024
58a376d
Make sqlite use logica_home by default whenever it was attached.
EvgSkv May 17, 2024
24e0dfc
Merge pull request #312 from EvgSkv/ti2023
EvgSkv May 17, 2024
e20a20e
Make AnyValue work in SQLite and PostgreSQL.
EvgSkv May 18, 2024
14a5030
Merge pull request #314 from EvgSkv/ti2023
EvgSkv May 18, 2024
cde1f57
Add sublime syntax file.
EvgSkv May 29, 2024
2f9a105
Update sublime syntax.
EvgSkv May 29, 2024
06ad479
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
d00e6fb
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
5654d9f
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
3428d7a
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
1433911
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
49679cf
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
b5f45b7
Adding user defined operators.
EvgSkv May 30, 2024
40d4dc7
Allow ++ to be used for list concatenation.
EvgSkv May 30, 2024
f591971
Adding user defined operators.
EvgSkv May 30, 2024
9fcd28b
Merge pull request #315 from EvgSkv/ti2023
EvgSkv May 30, 2024
6141620
Merge pull request #316 from EvgSkv/main
EvgSkv May 30, 2024
d8fef20
Fixing a bug in recursion within aggregate expressions.
EvgSkv Jun 2, 2024
b5454c8
Fixing a bug in recursion within aggregate expressions.
EvgSkv Jun 2, 2024
77c2746
Move Postgres tests to run from Python library.
EvgSkv Jun 5, 2024
9350259
Make empty comines work in Postgres.
EvgSkv Jun 7, 2024
8b52211
Merge pull request #317 from EvgSkv/ti2023
EvgSkv Jun 7, 2024
a86f131
Improve graph, allow single rule recursion.
EvgSkv Jun 8, 2024
afcd8c6
Merge pull request #318 from EvgSkv/ti2023
EvgSkv Jun 8, 2024
43f1d47
Add simple syntax for aggregating expressions.
EvgSkv Jun 9, 2024
e09033d
Merge pull request #319 from EvgSkv/ti2023
EvgSkv Jun 9, 2024
d2bd4bb
Add simple syntax for aggregating expressions.
EvgSkv Jun 9, 2024
fa00302
Merge pull request #320 from EvgSkv/ti2023
EvgSkv Jun 9, 2024
97abcb9
Reestablish Postgres connection in colab.
EvgSkv Jun 11, 2024
7160a7b
Merge pull request #321 from EvgSkv/ti2023
EvgSkv Jun 11, 2024
659c65b
Improving graph plotting. Updating primes example.
EvgSkv Jun 12, 2024
9c8de8c
Merge pull request #322 from EvgSkv/ti2023
EvgSkv Jun 12, 2024
01c201e
Keeping on necessary edges in ascii graph visualization.
EvgSkv Jun 13, 2024
482075e
Merge pull request #323 from EvgSkv/ti2023
EvgSkv Jun 13, 2024
5710670
Nicer text rendering.
EvgSkv Jun 13, 2024
fda6b4f
Merge pull request #324 from EvgSkv/ti2023
EvgSkv Jun 13, 2024
96fa344
Horizontal Recursion.
EvgSkv Jun 14, 2024
80cd9d6
Merge pull request #325 from EvgSkv/ti2023
EvgSkv Jun 14, 2024
a41854d
Handle nil in recursion, seamlessly droping what is not needed.
EvgSkv Jun 15, 2024
669a7f0
Merge pull request #326 from EvgSkv/ti2023
EvgSkv Jun 15, 2024
df51b94
Negation syntax parsing improvement.
EvgSkv Jun 17, 2024
8de479c
Merge pull request #327 from EvgSkv/ti2023
EvgSkv Jun 17, 2024
f7dc612
Speeding up functor application.
EvgSkv Jun 18, 2024
485c7db
Merge pull request #329 from EvgSkv/ti2023
EvgSkv Jun 18, 2024
12e9e20
Add function to run Logica from Python easier.
EvgSkv Jun 18, 2024
6f76c9e
Merge pull request #330 from EvgSkv/ti2023
EvgSkv Jun 18, 2024
43ba921
Iterative recursion.
EvgSkv Jun 19, 2024
4261761
Merge pull request #331 from EvgSkv/ti2023
EvgSkv Jun 19, 2024
ff21868
Iterative recursion.
EvgSkv Jun 19, 2024
217726f
Merge pull request #332 from EvgSkv/ti2023
EvgSkv Jun 19, 2024
a7a9932
fix connect
yilinxia Jun 20, 2024
9a68b7e
argmin argmax
yilinxia Jun 20, 2024
84802ac
Iterative recursion.
EvgSkv Jun 20, 2024
e87668e
Merge pull request #335 from EvgSkv/ti2023
EvgSkv Jun 20, 2024
5b8db05
Iterative recursion.
EvgSkv Jun 20, 2024
846c280
Merge pull request #336 from EvgSkv/ti2023
EvgSkv Jun 20, 2024
eec953b
Fixing typo in a test.
EvgSkv Jun 20, 2024
d188dd5
Merge pull request #337 from EvgSkv/ti2023
EvgSkv Jun 20, 2024
0033d1a
Update to RunPredicateFromString.
EvgSkv Jun 20, 2024
629273a
Merge pull request #338 from EvgSkv/ti2023
EvgSkv Jun 20, 2024
c7ea0bc
Iterative recursion.
EvgSkv Jun 21, 2024
117e3f8
Merge pull request #339 from EvgSkv/ti2023
EvgSkv Jun 21, 2024
3e7adea
fix duckdb list
yilinxia Jun 21, 2024
5a41233
remove redundant scripts
yilinxia Jun 21, 2024
b4eee1d
Propositional implication and equivalence.
EvgSkv Jun 21, 2024
bd52519
Merge pull request #340 from EvgSkv/ti2023
EvgSkv Jun 21, 2024
e0e415f
Making functions compile in psql.
EvgSkv Jun 21, 2024
248c9a1
Merge pull request #341 from EvgSkv/ti2023
EvgSkv Jun 21, 2024
1da5a0d
Merge pull request #333 from yilinxia/duckdb
EvgSkv Jun 21, 2024
914aac9
Merge pull request #342 from EvgSkv/main
EvgSkv Jun 21, 2024
628ff02
Improving DuckDB support.
EvgSkv Jun 22, 2024
114d9aa
Improving DuckDB support.
EvgSkv Jun 22, 2024
cc889cc
Merge pull request #343 from EvgSkv/ti2023
EvgSkv Jun 22, 2024
a12be46
Let creation of new aggregation functions.
EvgSkv Jun 22, 2024
1e3c4a8
Duck typing.
EvgSkv Jun 25, 2024
71e4d2c
Merge pull request #344 from EvgSkv/ti2023
EvgSkv Jun 25, 2024
1f9fbbe
Duck typing.
EvgSkv Jun 25, 2024
b9d54e7
Merge pull request #345 from EvgSkv/ti2023
EvgSkv Jun 25, 2024
6e3b8b7
Duck typing.
EvgSkv Jun 27, 2024
cd245db
Merge pull request #346 from EvgSkv/ti2023
EvgSkv Jun 27, 2024
8ceb11e
Check OrderBy validity.
EvgSkv Jun 28, 2024
05c08d2
Merge pull request #347 from EvgSkv/ti2023
EvgSkv Jun 28, 2024
76127c8
Duck typing.
EvgSkv Jun 28, 2024
e52591f
Merge pull request #348 from EvgSkv/ti2023
EvgSkv Jun 28, 2024
96341aa
Duck typing.
EvgSkv Jun 28, 2024
9beb96c
Duck typing.
EvgSkv Jun 28, 2024
431a32f
Duck typing.
EvgSkv Jun 28, 2024
51c25e6
Duck typing.
EvgSkv Jun 28, 2024
0396bcb
Merge pull request #349 from EvgSkv/ti2023
EvgSkv Jun 28, 2024
6602e04
Making DuckDB connection configurable.
EvgSkv Jun 30, 2024
8d15d13
Merge pull request #352 from EvgSkv/ti2023
EvgSkv Jun 30, 2024
8be8166
Creating an empty struct in psql.
EvgSkv Jul 1, 2024
af07b45
Merge pull request #353 from EvgSkv/ti2023
EvgSkv Jul 1, 2024
7397c82
Allow threads selection in duckdb.
EvgSkv Jul 3, 2024
d951e6d
Merge pull request #354 from EvgSkv/ti2023
EvgSkv Jul 3, 2024
c7eb06c
Allow predicates with no arguments.
EvgSkv Jul 5, 2024
9d91b56
Merge pull request #355 from EvgSkv/ti2023
EvgSkv Jul 5, 2024
1f8e4d5
Support copying tables to files in DuckDB.
EvgSkv Jul 6, 2024
7769f1e
Merge pull request #356 from EvgSkv/ti2023
EvgSkv Jul 6, 2024
c949059
Adding stop condition for iterative recursion.
EvgSkv Jul 7, 2024
8b571ea
Merge pull request #357 from EvgSkv/ti2023
EvgSkv Jul 7, 2024
041bbe1
Updating README example.
EvgSkv Jul 9, 2024
2eccc82
Updating README example.
EvgSkv Jul 9, 2024
7477f9d
Time type.
EvgSkv Jul 11, 2024
5c645ec
Merge pull request #358 from EvgSkv/ti2023
EvgSkv Jul 11, 2024
86af5c6
Report annotation error to the user.
EvgSkv Sep 6, 2024
6f00044
Merge pull request #368 from EvgSkv/ti2023
EvgSkv Sep 6, 2024
d9f8e30
Make recursion 32 deep and iterative when DuckDB is the engine.
EvgSkv Sep 9, 2024
ebb127d
Merge pull request #369 from EvgSkv/ti2023
EvgSkv Sep 9, 2024
2db5e8e
Merge pull request #370 from EvgSkv/main
EvgSkv Sep 9, 2024
e11d0d0
Implement slowdown in robot.html.
EvgSkv Sep 12, 2024
5f16cc2
Merge pull request #371 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
e58df95
Implementing slowdown of robot.html.
EvgSkv Sep 12, 2024
0c0f4fa
Merge pull request #372 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
263bb97
Implementing slowdown of robot.html.
EvgSkv Sep 12, 2024
be3d0ff
Merge pull request #373 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
4a95c16
Implementing slowdown of robot.html.
EvgSkv Sep 12, 2024
9b40225
Merge pull request #374 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
3f90a5f
Implementing slowdown of robot.html.
EvgSkv Sep 12, 2024
b995bf4
Merge pull request #375 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
398c33c
Add iteration info in the graphviz display.
EvgSkv Sep 12, 2024
be35ec5
Add iteration info in the graphviz display.
EvgSkv Sep 12, 2024
3740559
Merge pull request #376 from EvgSkv/ti2023
EvgSkv Sep 12, 2024
15ffe24
Implementing slowdown of robot.html.
EvgSkv Sep 13, 2024
dd5cc4a
Merge pull request #377 from EvgSkv/ti2023
EvgSkv Sep 13, 2024
6aa940f
Allow integers and strings as functor arguments.
EvgSkv Sep 14, 2024
6790cbe
Merge pull request #378 from EvgSkv/ti2023
EvgSkv Sep 14, 2024
121cbb5
Add type of Coalesce.
EvgSkv Sep 26, 2024
7df85d7
Merge pull request #379 from EvgSkv/ti2023
EvgSkv Sep 26, 2024
b45ad2d
Fix missing typing of empty lists.
EvgSkv Sep 27, 2024
6198f0f
Merge pull request #380 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
3bfe0bf
Enable orbit of a recursive predicate.
EvgSkv Sep 27, 2024
579722b
Merge pull request #381 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
6d1102c
Use -1 in recursion depth for infinity.
EvgSkv Sep 27, 2024
7ad2da9
Merge pull request #382 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
564219e
Automatically include stop as satellite.
EvgSkv Sep 27, 2024
f84bddb
Merge pull request #383 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
5721dd1
Remove incorrect edges from execution.
EvgSkv Sep 27, 2024
5f82a68
Merge pull request #384 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
6006dfa
Stop the whole iteration once at least one process stopped.
EvgSkv Sep 27, 2024
f6ac1bf
Merge pull request #385 from EvgSkv/ti2023
EvgSkv Sep 27, 2024
24c5019
Improve stop signal error message.
EvgSkv Oct 4, 2024
0b0c0f0
Merge pull request #386 from EvgSkv/ti2023
EvgSkv Oct 4, 2024
2cf6475
Merge pull request #387 from EvgSkv/main
EvgSkv Oct 4, 2024
552cf79
Helping DuckDB not to crash because of order by zero.
EvgSkv Oct 8, 2024
7dd2498
Merge pull request #388 from EvgSkv/ti2023
EvgSkv Oct 8, 2024
fb10ec1
Fixing type error for aggregation. Making iterative recursion work as…
EvgSkv Oct 11, 2024
d1425f1
Merge pull request #389 from EvgSkv/ti2023
EvgSkv Oct 11, 2024
b9d112b
Test of iterative functor.
EvgSkv Oct 11, 2024
7abb7c6
Merge pull request #390 from EvgSkv/ti2023
EvgSkv Oct 11, 2024
112ed11
Make DuckDB run from RunQueryPandas.
EvgSkv Oct 13, 2024
986c605
Merge pull request #393 from EvgSkv/ti2023
EvgSkv Oct 13, 2024
45abca9
MergeList for DuckDB and decoupling of SQLite connection.
EvgSkv Oct 22, 2024
da7e971
Merge pull request #396 from EvgSkv/ti2023
EvgSkv Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea
__pycache__/
docs/.DS_Store
env
86 changes: 65 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ 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 on
[BigQuery](https://cloud.google.com/bigquery).
logic programming syntax when writing queries and pipelines for databases and datawarehouses.
Logica programs run on
[BigQuery](https://cloud.google.com/bigquery), [Postgres](https://postgresql.org) and [SQLite](https://www.sqlite.org/).

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.
Logica compiles to SQL and gives you access to the power of SQL ecosystem
with the convenience of logic programming syntax.

This is useful because
SQL enginers are magnitudes more powerful than state of the art native
logic programming engines. For example, BigQuery is a distributed datawarehouse and thus logic programs written
in Logica can be easily parallelized onto thousands of servers. Postgres and SQLite are among most popular databases, they are
capable 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 use SQL, but feel unsatisfied about its readability, **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.

In the future we plan to support more SQL dialects and engines.
Support for more SQL dialects and engines is coming in the future.

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

Expand Down Expand Up @@ -140,18 +145,14 @@ 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,
x % y == 0
);
# 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);
```

Running `primes.l`
Expand All @@ -173,6 +174,49 @@ $ logica primes.l run Prime
+-------+
```

### Cities with largest beer variety

Let's use beer variety dataset from [plotly](https://github.com/plotly/datasets/blob/master/beers.csv).

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

Program `beer.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)};
```

Running `beer.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 |
+-------+--------------+------------+-------------+
```

<!--
### News mentions

Who was mentioned in the news in 2020 the most?
Expand Down Expand Up @@ -210,7 +254,7 @@ $ logica mentions.l run Mentions

Note that cities of Los Angeles and Las Vegas are mentioned in this table due to known
missclasification issue in the GDELT data analysis.

-->
## Feedback

Feel free to create [github issues](https://github.com/EvgSkv/logica/issues)
Expand Down
82 changes: 69 additions & 13 deletions colab_logica.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import os

import pandas
import duckdb

from .parser_py import parse
from .common import sqlite3_logica
from .common import duckdb_logica

BQ_READY = True # By default.

Expand Down Expand Up @@ -98,7 +100,14 @@ def SetDbConnection(connection):
global DB_CONNECTION
DB_CONNECTION = connection

REMEMBERED_PREVIOUS_MODE = None
def ConnectToPostgres(mode='interactive'):
global REMEMBERED_PREVIOUS_MODE
if mode == 'reconnect':
mode = REMEMBERED_PREVIOUS_MODE
print('Reconnecting to Postgres.')
else:
REMEMBERED_PREVIOUS_MODE = mode
connection = psql_logica.ConnectToPostgres(mode)
SetDbConnection(connection)
global DEFAULT_ENGINE
Expand Down Expand Up @@ -157,6 +166,12 @@ def ParseList(line):
predicates = [p.strip() for p in line.split(',')]
return predicates

def ParseListAndMaybeFile(line):
if '>' in line:
predicate_list_str, storage_file = line.split('>')
return ParseList(predicate_list_str.strip()), storage_file.strip()
else:
return ParseList(line), None

def RunSQL(sql, engine, connection=None, is_final=False):
if engine == 'bigquery':
Expand All @@ -172,6 +187,18 @@ def RunSQL(sql, engine, connection=None, is_final=False):
return df
else:
psql_logica.PostgresExecute(sql, connection)
elif engine == 'duckdb':
if is_final:
df = connection.sql(sql).df()
for c in df.columns:
if df.dtypes[c] == 'float64':
if df[c].isna().values.any():
return df
if set(df[c] - df[c].astype(int)) == {0.0}:
df[c] = df[c].astype(int)
return df
else:
connection.sql(sql)
elif engine == 'sqlite':
try:
if is_final:
Expand Down Expand Up @@ -207,11 +234,27 @@ def __init__(self):
def __call__(self, sql, engine, is_final):
return RunSQL(sql, engine, self.connection, is_final)

class DuckdbRunner(object):
def __init__(self):
global DB_CONNECTION
if not DB_CONNECTION:
DB_CONNECTION = duckdb.connect()
self.connection = DB_CONNECTION

def __call__(self, sql, engine, is_final):
return RunSQL(sql, engine, self.connection, is_final)


class PostgresRunner(object):
def __init__(self):
global DB_CONNECTION
global DB_ENGINE
if not DB_CONNECTION:
try:
ConnectToLocalPostgres()
except:
pass

if not DB_CONNECTION:
print("Assuming this is running on Google CoLab in a temporary")
print("environment.")
Expand All @@ -225,6 +268,10 @@ def __init__(self):
self.connection = DB_CONNECTION

def __call__(self, sql, engine, is_final):
global DB_CONNECTION
if self.connection and self.connection.closed and REMEMBERED_PREVIOUS_MODE:
ConnectToPostgres('reconnect')
self.connection = DB_CONNECTION
return RunSQL(sql, engine, self.connection, is_final)


Expand All @@ -234,7 +281,7 @@ def ShowError(error_text):

def Logica(line, cell, run_query):
"""Running Logica predicates and storing results."""
predicates = ParseList(line)
predicates, maybe_storage_file = ParseListAndMaybeFile(line)
if not predicates:
ShowError('No predicates to run.')
return
Expand All @@ -257,7 +304,6 @@ def Logica(line, cell, run_query):
except infer.TypeErrorCaughtException as e:
e.ShowMessage()
return

engine = program.annotations.Engine()

if engine == 'bigquery' and not BQ_READY:
Expand All @@ -272,13 +318,16 @@ def Logica(line, cell, run_query):

bar = TabBar(predicates + ['(Log)'])
logs_idx = len(predicates)

executions = []
sub_bars = []
ip = IPython.get_ipython()
for idx, predicate in enumerate(predicates):
with bar.output_to(logs_idx):
try:
if storage_file_name := maybe_storage_file:
with open(storage_file_name, 'w') as storage_file:
storage_file.write(cell)
print('\x1B[3mProgram saved to %s.\x1B[0m' % storage_file_name)
sql = program.FormattedPredicateSql(predicate)
executions.append(program.execution)
ip.push({predicate + '_sql': sql})
Expand Down Expand Up @@ -307,13 +356,15 @@ def Logica(line, cell, run_query):
sql_runner = SqliteRunner()
elif engine == 'psql':
sql_runner = PostgresRunner()
elif engine == 'duckdb':
sql_runner = DuckdbRunner()
elif engine == 'bigquery':
EnsureAuthenticatedUser()
sql_runner = RunSQL
else:
raise Exception('Logica only supports BigQuery, PostgreSQL and SQLite '
'for now.')
try:
try:
result_map = concertina_lib.ExecuteLogicaProgram(
executions, sql_runner=sql_runner, sql_engine=engine,
display_mode=DISPLAY_MODE)
Expand All @@ -337,6 +388,19 @@ def Logica(line, cell, run_query):
print('The query was not run.')
print(' ') # To activate the tabbar.


def ConnectToLocalPostgres():
import psycopg2
connection = psycopg2.connect(host='localhost', database='logica', user='logica', password='logica')
connection.autocommit = True

print('Connected.')
global DEFAULT_ENGINE
global DB_CONNECTION
DEFAULT_ENGINE = 'psql'
DB_CONNECTION = connection


def PostgresJumpStart():
# Install postgresql server.
print("Installing and configuring an empty PostgreSQL database.")
Expand Down Expand Up @@ -377,12 +441,4 @@ def PostgresJumpStart():
return
print('Installation succeeded. Connecting...')
# Connect to the database.
import psycopg2
connection = psycopg2.connect(host='localhost', database='logica', user='logica', password='logica')
connection.autocommit = True

print('Connected.')
global DEFAULT_ENGINE
global DB_CONNECTION
DEFAULT_ENGINE = 'psql'
DB_CONNECTION = connection
ConnectToLocalPostgres()
Loading