EvgSkv / logica

Logica is a logic programming language that compiles to SQL. It runs on DuckDB, Google BigQuery, PostgreSQL and SQLite.
https://logica.dev
Apache License 2.0
1.77k stars 93 forks source link

no such function: MagicalEntangle #361

Open mikko-ahonen opened 3 months ago

mikko-ahonen commented 3 months ago

I am having an issue with sqlite database. I just installed fresh logica and sqlite3 (pip install logica, apt install sqlite3) on Debian.

root@80dcb49fb4fc:/src# cat foo.l
@Engine("sqlite");

Foo(id:"E1", name:"Foo 1");
Foo(id:"E2", name:"Foo 2");

Bar(id:id, name:name) :- Foo(id:"E1", name:);

CountNotE1(count? += 1) distinct :- Foo(id:), ~Bar(id:);
root@80dcb49fb4fc:/src# python -m logica foo.l print CountNotE1 > foo.sql
root@80dcb49fb4fc:/src# cat foo.sql
WITH t_0_Foo AS (SELECT * FROM (

    SELECT
      'E1' AS id,
      'Foo 1' AS name
   UNION ALL

    SELECT
      'E2' AS id,
      'Foo 2' AS name

) AS UNUSED_TABLE_NAME  )
SELECT
  SUM(1) AS count
FROM
  t_0_Foo AS Foo
WHERE
  ((SELECT
    MIN(MagicalEntangle(1, x_3.value)) AS logica_value
  FROM
    t_0_Foo AS t_1_Foo, JSON_EACH(JSON_ARRAY(0)) as x_3
  WHERE
    (t_1_Foo.id = 'E1')) IS NULL);
root@80dcb49fb4fc:/src# sqlite3 ':memory:' '.read foo.sql'
Error: near line 1: no such function: MagicalEntangle
root@80dcb49fb4fc:/src# 
EvgSkv commented 2 months ago

Hello @mikko-ahonen !

Thank you for clear instructions to reproduce the error!

Yes, this is exected. Raw SQLite is generally missing some functions that advanced Logica functions need to be implemented, so Logica is enriching SQLite with more functions from Python. This includes MagicalEntangle function which is used to disambiguate scope in aggregating expressions. You are using negation, which is implemented via aggregating expression and thus getting this error.

You can run only very simple programs via sqlite3 binary.

It is supposed that you'd run Logica program directly with logica like so:

$ python3 -m logica foo.l run CountNotE1
+-------+
| count |
+-------+
| 1     |
+-------+

Btw, in the rule Bar(id:id, name:name) :- Foo(id:"E1", name:); you don't have variable id assigned. It's probably intended to be as such Bar(id:id, name:name) :- Foo(id:, name:), id = "E1";.

Is this is a workable method for you?

If not, I'd be curious to learn about your usecase and we can try to find a solution.

Thank you! Evgeny.

mikko-ahonen commented 2 months ago

This is not really a problem for me, as I am executing the queries from python.

But I think this could bite others starting with the product. I would expect "print" command to print out sufficient functions so that I can copy paste the code into the SQLite command line tool.

This would help the debugging.

But like said, this is not blocking me or anything.

mikko-ahonen commented 2 months ago

Just to add, I imagine typically people who are taking the product for a spin would do exactly like I did. So they probably use print because that shows you exactly what is happening, and then you can copy-paste it to command line SQL client. Also using SQLite is probably the first option because it does not require you to set iany server up. In many places in the public docs it appears as if only BigTable is supported which was a huge blocker for me. Luckily I tried it out anyway.

EvgSkv commented 2 months ago

Yeah, that's a good point. I'll be clean up docs asap. Thank you!