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

Metabase Share Expression is useless with Cube #8359

Open
pauldheinrichs opened this issue Jun 14, 2024 · 3 comments
Open

Metabase Share Expression is useless with Cube #8359

pauldheinrichs opened this issue Jun 14, 2024 · 3 comments
Assignees
Labels
api:sql Issues related to SQL API bug Something isn't working

Comments

@pauldheinrichs
Copy link

pauldheinrichs commented Jun 14, 2024

Describe the bug
When utilizing the share expression in Metabase.

It generates a decimal value representing a percentage, the problem is cube converts assumes the as an int so the Share result expression is always either 0 or 1 rendering it pretty much useless. Changing the casting manually to enforce the usages of ::double resolves the issue.

Latest Cube release
Driver: Redshift
CUBE_SQL_PUSH_DOWN: enabled

Problem SQL

 DATE_TRUNC('week', "source"."created_date") AS "created_date",
  SUM(
    CASE
      WHEN "source"."count" = 1 THEN 1
      ELSE 0.0
    END
  ) / COUNT(*) AS "one video"

Manually casting either side of these expressions to ::double resolves the problem IE:

  SUM(
    CASE
      WHEN "source"."count" = 1 THEN 1
      ELSE 0.0
    END
  ) / COUNT(*)::double AS "one video"

Full SQL:

SELECT
  DATE_TRUNC('week', "source"."created_date") AS "created_date",
  SUM(
    CASE
      WHEN "source"."count" = 1 THEN 1
      ELSE 0.0
    END
  ) / COUNT(*) AS "one video"
FROM
  (
    SELECT
      DATE_TRUNC('week', "public"."table"."created_date") AS "created_date",
      "public"."table"."user_id" AS "user_id",
      count(distinct "public"."table"."video_id") AS "count"
    FROM
      "public"."table"
   
WHERE
      (
        "public"."table"."created_date" >= DATE_TRUNC('quarter', NOW())
      )
     
   AND (
        "public"."table"."created_date" < DATE_TRUNC('quarter', (NOW() + INTERVAL '3 month'))
      )
   
GROUP BY
      DATE_TRUNC('week', "public"."table"."created_date"),
      "public"."all_videos"."user_id"
   
ORDER BY
      DATE_TRUNC('week', "public"."table"."created_date") ASC,
      "public"."table"."user_id" ASC
  ) AS "source"
GROUP BY
  DATE_TRUNC('week', "source"."created_date")
ORDER BY
  DATE_TRUNC('week', "source"."created_date") ASC
=
@igorlukanin
Copy link
Member

Hi @pauldheinrichs 👋

I assume this is not a bug but rather Cube conforming to the SQL standard (and, more specifically, it's implementation in Postgres). As you can see in the Postgres documentation, "(integer division truncates results." If you'd like to use floating-point division, at least one of the arguments should be floating-point. Casting them, either on the data model side or the querying side, is a way to go here.

@igorlukanin igorlukanin added question The issue is a question. Please use Stack Overflow for questions. api:sql Issues related to SQL API labels Jun 19, 2024
@igorlukanin igorlukanin self-assigned this Jun 19, 2024
@pauldheinrichs
Copy link
Author

@igorlukanin Appreciate the response, but i believe the following part of the query 0.0 is what converts it to a floating

 select SUM(
    CASE
      WHEN 1 = 1 THEN 1
      ELSE 0.0
    END
  ) / 1 AS "one video"

To confirm this i've run the following query in postgres, redshift, and cube being the only engine which results in a non-floating point number

Cube Query:
image

Redshift Query:
image

Postgres Query:
image

@igorlukanin
Copy link
Member

I see. That makes sense! The workaround here would be to multiple the whole CASE expression by 1.0. However, I believe we'll conform with the Postgres implementation eventually.

@igorlukanin igorlukanin added bug Something isn't working and removed question The issue is a question. Please use Stack Overflow for questions. labels Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:sql Issues related to SQL API bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants