Skip to content

fix(bigquery): translate EXTRACT(DOW/DOY) case-insensitively for SQL API#10678

Open
tlangton3 wants to merge 2 commits intocube-js:masterfrom
tlangton3:cube/fix-bigquery-extract-dow-case
Open

fix(bigquery): translate EXTRACT(DOW/DOY) case-insensitively for SQL API#10678
tlangton3 wants to merge 2 commits intocube-js:masterfrom
tlangton3:cube/fix-bigquery-extract-dow-case

Conversation

@tlangton3
Copy link
Copy Markdown
Contributor

Summary

Fixes #10644. When querying via the SQL API (Postgres wire protocol), EXTRACT(DOW FROM column) (and EXTRACT(DOY FROM column)) fail on BigQuery with:

A valid date part name is required but found dow

This pattern originates from Tableau connecting via the SQL API.

Changes

  • Override the extract expression template in BigqueryQuery.ts to use date_part|upper for the DOW/DOY comparisons, matching the existing DATETRUNC template's case-insensitive convention
  • Add an integration test that exercises EXTRACT(DOW FROM column) against BigQuery
  • Add an MSSQL skip for the new test (MSSQL has no EXTRACT builtin)

Implementation Details

The BigQuery extract template uses a Jinja if to translate Postgres-style DOW/DOY date parts to BigQuery's DAYOFWEEK/DAYOFYEAR:

'EXTRACT({% if date_part == \'DOW\' %}DAYOFWEEK{% elif date_part == \'DOY\' %}DAYOFYEAR{% else %}{{ date_part }}{% endif %} FROM {{ expr }})'

The comparisons are case-sensitive. DataFusion can pass date_part as lowercase 'dow', causing the if to fall through to {{ date_part }} and emit EXTRACT(dow FROM ...) — which BigQuery rejects.

The fix adds the |upper filter to both comparisons:

'EXTRACT({% if date_part|upper == \'DOW\' %}DAYOFWEEK{% elif date_part|upper == \'DOY\' %}DAYOFYEAR{% else %}{{ date_part }}{% endif %} FROM {{ expr }})'

This mirrors the existing templates.functions.DATETRUNC template which already correctly uses date_part|upper == 'WEEK'.

Why simple EXTRACT(DOW FROM CURRENT_DATE) cases work without the fix

DataFusion's ConstEvaluator constant-folds EXTRACT(DOW FROM CURRENT_DATE) to an integer literal at plan time (CURRENT_DATE is a Stable UDF). The folded integer is sent to BigQuery — the template is never invoked. Only EXTRACT(DOW FROM column_reference) (and similar non-foldable cases) reach the template and trigger the bug.

Testing

  • Reproduced the bug locally against live BigQuery before applying the fix (same error as in the issue)
  • After fix: integration test SQL API: EXTRACT DOW from column reference in SQL push down passes
  • Full BigQuery integration suite with Tesseract ON: 103 passed, 2 pre-existing failures (SQL API: ungrouped pre-agg and Tesseract: SQL API: Timeshift measure from cube), no regressions
  • TypeScript compilation passes

Linear Ticket

N/A

The BigQuery extract template uses case-sensitive equality (date_part == 'DOW')
to translate Postgres-style DOW/DOY date parts to BigQuery's DAYOFWEEK/DAYOFYEAR.
DataFusion can pass date_part as lowercase 'dow', causing the comparison to
fall through and emit an invalid 'dow' literal that BigQuery rejects with
"A valid date part name is required but found dow".

Use the |upper filter so the comparison is robust to whatever casing
DataFusion provides — mirrors the existing DATETRUNC template convention.

Fixes cube-js#10644
@github-actions github-actions bot added javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members. labels Apr 13, 2026
@tlangton3 tlangton3 marked this pull request as ready for review April 13, 2026 09:59
@tlangton3 tlangton3 requested review from a team as code owners April 13, 2026 09:59
The MySQL legacy rewrite engine doesn't recognise EXTRACT(DOW FROM column)
as a Cube filter pattern (Error during rewrite: Can't detect Cube query).
Skip the new test for MySQL — same convention as existing
"Date/time comparison with date_trunc with SQL push down" which is also
skipped on MySQL.

Postgres test failures in CI are missing-snapshot only — the query executes
correctly, snapshot will be generated by maintainer's --updateSnapshot run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL API fails to translate EXTRACT(DOW ...) to BigQuery DAYOFWEEK in complex WHERE clauses

1 participant