cube-js / cube-js/cube

SQL error when using GBQ APPROX_QUANTILES function and granularity

Open
#7,914 1 comment 3 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Describe the bug**

Median computation with the `APPROX_QUANTILES()` BigQuery function and `granularity` sets to `week` returns an error ⬇️

```
SELECT list expression references employee_daily_snapshots_median_salary_cumulative__employee_daily_snapshots.snapshot_date which is neither grouped nor aggregated at [82:73]
```

with the measure `medianSalary` defines as :

```
medianSalary: {
title: 'employee.salary.medianTotal',
type: 'number',
sql: CUBE => `APPROX_QUANTILES(${CUBE.salaryTotal}, 100)[OFFSET(50)]`,
rollingWindow: {
trailing: `1 day`,
offset: `end`
}
}
```

and the dimension `salaryTotal` as :
```
salaryTotal: {
title: 'employee.salary.total',
sql: CUBE => `${CUBE}.remuneration.salary.total`,
type: 'number'
}
```
It works for the average measure computation + granularity or for the median computation without granularity.

Print of the wrong SQL request sent by Cube to GBQ ⬇️

```
SELECT
q_0.`employee_daily_snapshots__timestamp_week`,
`employee_daily_snapshots__median_salary_total` `employee_daily_snapshots__median_salary_total`
FROM
(
WITH `EmployeeDailySnapshots.timestamp_series` AS (
SELECT
DATETIME(TIMESTAMP(dates.f)) date_from,
DATETIME(TIMESTAMP(dates.t)) date_to
FROM
(
select
'2024-01-01T00:00:00.000' f,
'2024-01-07T23:59:59.999' t
UNION
ALL
select
'2024-01-08T00:00:00.000' f,
'2024-01-14T23:59:59.999' t
UNION
ALL
select
'2024-01-15T00:00:00.000' f,
'2024-01-21T23:59:59.999' t
UNION
ALL
select
'2024-01-22T00:00:00.000' f,
'2024-01-28T23:59:59.999' t
UNION
ALL
select
'2024-01-29T00:00:00.000' f,
'2024-02-04T23:59:59.999' t
UNION
ALL
select
'2024-02-05T00:00:00.000' f,
'2024-02-11T23:59:59.999' t
UNION
ALL
select
'2024-02-12T00:00:00.000' f,
'2024-02-18T23:59:59.999' t
UNION
ALL
select
'2024-02-19T00:00:00.000' f,
'2024-02-25T23:59:59.999' t
UNION
ALL
select
'2024-02-26T00:00:00.000' f,
'2024-03-03T23:59:59.999' t
UNION
ALL
select
'2024-03-04T00:00:00.000' f,
'2024-03-10T23:59:59.999' t
UNION
ALL
select
'2024-03-11T00:00:00.000' f,
'2024-03-17T23:59:59.999' t
) AS dates
)
SELECT
`employee_daily_snapshots_median_salary_total_cumulative__outer_series`.`date_from` `employee_daily_snapshots__timestamp_week`,
`employee_daily_snapshots__median_salary_total`
FROM
`EmployeeDailySnapshots.timestamp_series` `employee_daily_snapshots_median_salary_total_cumulative__outer_series`
LEFT JOIN (
SELECT
`EmployeeDailySnapshots.timestamp_series`.`date_from` `employee_daily_snapshots__timestamp_week`,
-- Missing aggregate function like 'SUM()' for the `employee_daily_snapshots__median_salary_total` here
`employee_daily_snapshots__median_salary_total` `employee_daily_snapshots__median_salary_total`
FROM
`EmployeeDailySnapshots.timestamp_series`
INNER JOIN (
SELECT
DATETIME(
TIMESTAMP(
`employee_daily_snapshots_median_salary_total_cumulative__employee_daily_snapshots`.snapshot_date
),
'UTC'
) `employee_daily_snapshots__timestamp_week`,
APPROX_QUANTILES(
`employee_daily_snapshots_median_salary_total_cumulative__employee_daily_snapshots`.remuneration.salary.total,
100
) [OFFSET(50)] `employee_daily_snapshots__median_salary_total`
FROM
(
SELECT
*
FROM
`4_kpi.kpi_employee_daily_snapshot`
WHERE
(
TIMESTAMP(snapshot_date) >= TIMESTAMP("2024-01-01T00:00:00.000Z")
AND TIMESTAMP(snapshot_date) <= TIMESTAMP("2024-03-12T23:59:59.999Z") )
) AS `employee_daily_snapshots_median_salary_total_cumulative__employee_daily_snapshots`
WHERE
(
DATETIME(
TIMESTAMP(
`employee_daily_snapshots_median_salary_total_cumulative__employee_daily_snapshots`.snapshot_date
),
'UTC'
) > DATETIME_SUB(
DATETIME(TIMESTAMP("2024-01-01T00:00:00.000")),
INTERVAL 1 day
)
AND DATETIME(
TIMESTAMP(
`employee_daily_snapshots_median_salary_total_cumulative__employee_daily_snapshots`.snapshot_date
),
'UTC'
) <= DATETIME(TIMESTAMP("2024-03-17T23:59:59.999"))
)
-- Missing `GROUP BY 1` here
) AS `employee_daily_snapshots_median_salary_total_cumulative__base` ON `employee_daily_snapshots_median_salary_total_cumulative__base`.`employee_daily_snapshots__timestamp_week` > DATETIME_SUB(
`EmployeeDailySnapshots.timestamp_series`.`date_to`,
INTERVAL 1 day
)
AND `employee_daily_snapshots_median_salary_total_cumulative__base`.`employee_daily_snapshots__timestamp_week` <= `EmployeeDailySnapshots.timestamp_series`.`date_to`
GROUP BY
1
) AS `employee_daily_snapshots_median_salary_total_cumulative__outer_base` ON `employee_daily_snapshots_median_salary_total_cumulative__outer_series`.`date_from` = `employee_daily_snapshots_median_salary_total_cumulative__outer_base`.`employee_daily_snapshots__timestamp_week`
) as q_0
ORDER BY
1 ASC
LIMIT
10000
```

**Version:**
[0.34.57]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.