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

(Rolling window?) aggregate generates invalid BQ SQL (Unexpected keyword WITH) #8193

Closed
mfulop opened this issue Apr 25, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@mfulop
Copy link

mfulop commented Apr 25, 2024

The query below with the cube schema (below) generates a SQL query that is invalid (BigQuery reports Syntax error: Unexpected keyword WITH at [2:5])

{
  "query": {
    "timezone": "UTC",
    "measures": [
      "SubmissionsTest.totalCount"
    ],
    "segments": [
    ],
    "filters": [],
    "dimensions": [
    ],
    "timeDimensions": [
      {
        "dimension": "SubmissionsTest.createdatDate",
        "granularity": "month",
        "dateRange": [
          "3/1/2024",
          "4/1/2024"
        ]
      }
    ]
  },
  "queryType": "multi"
}

The generated SQL is:

SELECT q_0.`submissions_test__createdat_date_month`, `submissions_test__total_count` `submissions_test__total_count` FROM \n    WITH `SubmissionsTest.createdatDate_series` AS (SELECT DATETIME(TIMESTAMP(dates.f)) date_from, DATETIME(TIMESTAMP(dates.t)) date_to FROM (select '2024-03-01T00:00:00.000' f, '2024-03-31T23:59:59.999' t UNION ALL select '2024-04-01T00:00:00.000' f, '2024-04-30T23:59:59.999' t) AS dates) SELECT `submissions_test_total_count_cumulative__outer_series`.`date_from` `submissions_test__createdat_date_month`, `submissions_test__total_count` FROM\n    `SubmissionsTest.createdatDate_series` `submissions_test_total_count_cumulative__outer_series`\n    LEFT JOIN (\n      SELECT `SubmissionsTest.createdatDate_series`.`date_from` `submissions_test__createdat_date_month`, sum(`submissions_test__total_count`) `submissions_test__total_count` FROM `SubmissionsTest.createdatDate_series`\n      INNER JOIN (SELECT DATETIME_TRUNC(DATETIME(PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%E*SZ', `submissions_test_total_count_cumulative__submissions_test`.createdat), 'UTC'), MONTH) `submissions_test__createdat_date_month`, count(*) `submissions_test__total_count` FROM xxxxxx AS `submissions_test_total_count_cumulative__submissions_test`  WHERE (DATETIME(PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%E*SZ', `submissions_test_total_count_cumulative__submissions_test`.createdat), 'UTC') <= DATETIME(TIMESTAMP(?))) GROUP BY 1) AS `submissions_test_total_count_cumulative__base` ON `submissions_test_total_count_cumulative__base`.`submissions_test__createdat_date_month` <= `SubmissionsTest.createdatDate_series`.`date_to`\n       GROUP BY 1\n    ) AS `submissions_test_total_count_cumulative__outer_base` ON `submissions_test_total_count_cumulative__outer_series`.`date_from` = `submissions_test_total_count_cumulative__outer_base`.`submissions_test__createdat_date_month`\n     as q_0  ORDER BY 1 ASC LIMIT 10000

Guarding WITH with (parentheses) makes the query valid.

Minimally reproducible Cube Schema

cube('SubmissionsTest', {
  sql: `SELECT * FROM xxx`,

  segments: {
  },

  measures: { 
    count: {
      sql: `${CUBE}._id`,
      type: `count`
    },
    totalCount: {
      type: `count`,
      rollingWindow: {
        trailing: `unbounded`
      }
    }
  },

  dimensions: {
    createdatDate: {
      sql: `PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%E*SZ', ${CUBE}.createdat)`,
      type: `time`
    },
    createdat: {
      sql: `${CUBE}.createdat`,
      type: `string`
    }
  }
})

Version:
"@cubejs-backend/bigquery-driver": "0.35.22",
"@cubejs-backend/cubestore-driver": "0.35.22",
"@cubejs-backend/server": "0.35.22",

@mfulop mfulop changed the title (Rolling window) aggregate generates invalid BQ SQL (Unexpected keyword WITH) (Rolling window?) aggregate generates invalid BQ SQL (Unexpected keyword WITH) Apr 25, 2024
@mfulop
Copy link
Author

mfulop commented Apr 25, 2024

works in 0.34.62, broken in 0.35.0

@paveltiunov paveltiunov added the bug Something isn't working label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants