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

Even numbers (in tutorial) does not work for sqlite #149

Open
RAbraham opened this issue Jul 23, 2021 · 2 comments
Open

Even numbers (in tutorial) does not work for sqlite #149

RAbraham opened this issue Jul 23, 2021 · 2 comments

Comments

@RAbraham
Copy link
Contributor

Hi,
If I run this, I don't get even numbers.

%%logica Even
@Engine("sqlite");

Z(x) :- x in Range(20);

Even(x) :- Z(x), Z(x / 2);
print('Even numbers:', ', '.join(map(str, Even['col0'])))

Even numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

This is the generated SQL:

The following query is stored at Even_sql variable.
SELECT
  x_4.value AS col0
FROM
  JSON_EACH((select json_group_array(n) from (with recursive t as(select 0 as n union all select n + 1 as n from t where n + 1 < 20) select n from t))) as x_4, JSON_EACH((select json_group_array(n) from (with recursive t as(select 0 as n union all select n + 1 as n from t where n + 1 < 20) select n from t))) as x_6
WHERE
  (x_6.value = ((x_4.value) / (2)));
@EvgSkv
Copy link
Owner

EvgSkv commented Jul 28, 2021

Apparently this is because integer division in SQLite results in an integer number. To make example work we need to do it as follows:

%%logica Even
@Engine("sqlite");

Z(x) :- x in Range(20);

Even(x) :- Z(x), Z(x / 2.0);

We should either change the original (because this form works in BQ as well), or create a standalone SQLite tutorial.

@RAbraham
Copy link
Contributor Author

RAbraham commented Aug 4, 2021

changing the original works 👍 . fyi, this happens in postgres too. I'll leave this open if you like to use it as a bookmark but please feel free to close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants