EvgSkv / logica

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

Home Page:https://logica.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sqlite: Custom Aggregation function returning none.

RAbraham opened this issue · comments

Hi,
Re: the following code in the tutorial.

@Engine("sqlite");
AnonymizedCodeContribution(cl_lengths: [110, 220, 405], org: "ads");
AnonymizedCodeContribution(cl_lengths: [30, 51, 95], org: "ads");
AnonymizedCodeContribution(cl_lengths: [10, 20, 1000], org: "games");

HarmonicMean(x) = Sum(1) / Sum(1 / x);

OrgStats(
    org:,
    mean_cl_size? Avg= cl_size,
    harmonic_mean_cl_size? HarmonicMean= cl_size) distinct :-
  AnonymizedCodeContribution(cl_lengths:, org:),
  cl_size in cl_lengths;

The output from BigQuery has values for harmonic_mean_cl_size



  | org | mean_cl_size | harmonic_mean_cl_size
-- | -- | -- | --
ads | 151.833333 | 75.402468
games | 343.333333 | 19.867550


but when I run the above code for sqlite, I get none for the same column



  | org | mean_cl_size | harmonic_mean_cl_size
-- | -- | -- | --
ads | 151.833333 | None
games | 343.333333 | None


Generated SQL Query:

The following query is stored at OrgStats_sql variable.
WITH t_0_AnonymizedCodeContribution AS (SELECT * FROM (
  
    SELECT
      JSON_ARRAY(110, 220, 405) AS cl_lengths,
      'ads' AS org
   UNION ALL
  
    SELECT
      JSON_ARRAY(30, 51, 95) AS cl_lengths,
      'ads' AS org
   UNION ALL
  
    SELECT
      JSON_ARRAY(10, 20, 1000) AS cl_lengths,
      'games' AS org
  
) AS UNUSED_TABLE_NAME  )
SELECT
  AnonymizedCodeContribution.org AS org,
  AVG(x_5.value) AS mean_cl_size,
  ((SUM(1)) / (SUM(((1) / (x_5.value))))) AS harmonic_mean_cl_size
FROM
  t_0_AnonymizedCodeContribution AS AnonymizedCodeContribution, JSON_EACH(AnonymizedCodeContribution.cl_lengths) as x_5
GROUP BY org;

Another issue caused by SQLite's integer division.
We should update it to:

%%logica OrgStats
@Engine("sqlite");
AnonymizedCodeContribution(cl_lengths: [110, 220, 405], org: "ads");
AnonymizedCodeContribution(cl_lengths: [30, 51, 95], org: "ads");
AnonymizedCodeContribution(cl_lengths: [10, 20, 1000], org: "games");

HarmonicMean(x) = Sum(1.0) / Sum(1.0 / x);

OrgStats(
    org:,
    mean_cl_size? Avg= cl_size,
    harmonic_mean_cl_size? HarmonicMean= cl_size) distinct :-
  AnonymizedCodeContribution(cl_lengths:, org:),
  cl_size in cl_lengths;

👍 . please close at will.