-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Describe the bug
Using TO_TIMESTAMP(...) in a WHERE filter causes query rewrite to fail when the query includes aggregation (COUNT(DISTINCT ...)).
Cube returns: SQL Error [XX000]: ERROR: Database Execution Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':00:00.000Z) AND (fact_test.start_date < 2026-01-15T00:00:00.000Z))) GROUP BY ' at line 11
The error occurs because when the orginal query is rewritten, the timestamp value is not placed between quotes.
To Reproduce
Steps to reproduce the behavior:
Database source: MySQL
Minimally reproducible Cube Schema:
cubes:
- name: fact_test
sql: |
select 1 as form_id, 'A' as worklist, '2026-01-13 10:00:00' as start_date
UNION ALL
select 2 as form_id, 'A' as worklist, '2026-01-14 11:00:00' as start_date
UNION ALL
select 3 as form_id, 'B' as worklist, '2026-01-16 12:00:00' as start_date
dimensions:
- name: form_id
sql: form_id
type: number
primary_key: true
public: true
- name: worklist
sql: worklist
type: string
- name: start_date
sql: start_date
type: time
measures:
- name: count
type: countConnect to Cube SQL API from a SQL client (for example DBeaver).
Run an aggregate query on a MySQL-backed cube table with TO_TIMESTAMP filters:
SELECT count(distinct form_id)
FROM public.fact_test
WHERE start_date < TO_TIMESTAMP('2026-01-15 00:00:02.000000', 'YYYY-MM-DD HH24:MI:SS.US')Error:
SQL Error [XX000]: ERROR: Database Execution Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':00:02.000Z))
) AS `fact_test`
LIMIT 50000' at line 11Looking in the logs, you will see that in where clause the timestamp is not between quotes.
SELECT `fact_test`.`count_distinct_f` `count_distinct_f`
FROM (
SELECT
COUNT(DISTINCT `fact_test`.form_id) `count_distinct_f`
FROM
(select 1 as form_id, 'A' as worklist, timestamp('2026-01-13 10:00:00') as start_date
UNION ALL
select 2 as form_id, 'A' as worklist, timestamp('2026-01-14 11:00:00') as start_date
UNION ALL
select 3 as form_id, 'B' as worklist, timestamp('2026-01-16 12:00:00') as start_date
) AS `fact_test` WHERE ((`fact_test`.start_date < 2026-01-15T00:00:02.000Z))
) AS `fact_test`
LIMIT 50000Expected behavior
The query should be rewritten successfully and return grouped counts.
TO_TIMESTAMP('YYYY-MM-DD', ...) date filters should work consistently in aggregate queries the same way they do in non-aggregate queries. Therefor the timestamp value should be between quotes.
Version:
- Cube version: 1.6.14 (error first occurs in 1.3.27)
Additional context
- The error was introduced in v1.3.27 when timestamp_literal: '{{ value }}' has been added to BaseQuery.js
- Issue identified using MySQL and DuckDb as source database