Skip to content

fix(drivers): athena custom-granularity pre-aggregation build - #10950

Merged
waralexrom merged 3 commits into
masterfrom
cube-athena-custom-granularity-athena
May 28, 2026
Merged

waralexrom merged 3 commits into
masterfrom
cube-athena-custom-granularity-athena

Conversation

@waralexrom

Copy link
Copy Markdown
Member

Summary

CORE-410: Athena pre-aggregation builds for custom granularities fail when an export bucket is configured (NOT_SUPPORTED: Unsupported Hive type: timestamp(3) with time zone). Cast dateBin origin to plain TIMESTAMP so Hive can serialize the rollup output column.

Changes

  • PrestodbQuery.dateBin: wrap origin in CAST(... AS TIMESTAMP) so date_add returns a plain TIMESTAMP instead of timestamp with time zone. Inherited by Athena and Trino, brings the custom-granularity rollup column shape in line with what timeGroupedColumn/date_trunc already emits for standard granularities.
  • Add a unit regression in athena-query.test.ts asserting the new loadSql shape for a custom-granularity rollup.
  • Close a coverage gap in cubejs-testing-drivers: introduce a unique build_only_half_year granularity and a ECommerce.TBuildOnlyHalfYear rollup, built explicitly under must built pre-aggregations for type === 'athena'. The granularity name is unique to this rollup so no query test matches it — snapshots stay stable in both legacy and Tesseract planner modes.

Testing

  • cd packages/cubejs-schema-compiler && yarn tsc && TZ=UTC node_modules/.bin/jest dist/test/unit/athena-query.test.js — both unit tests pass; without the fix the new regression fails (TZ-aware origin leaks into loadSql).
  • Reproduced the original Hive failure against an Athena export bucket with the fix reverted; with the fix in place the CREATE TABLE ... AS SELECT succeeds. CI matrix covers Athena in both use_tesseract_sql_planner: true and false.

@waralexrom
waralexrom requested review from a team as code owners May 27, 2026 15:53
@github-actions github-actions Bot added the javascript Pull requests that update Javascript code label May 27, 2026
@claude

claude Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @waralexrom's task in 1m 22s —— View job


Code Review: PR #10950

  • Gather context and read changed files
  • Review core fix in PrestodbQuery.ts
  • Review unit test in athena-query.test.ts
  • Review testing driver fixture changes
  • Post comprehensive review

Summary

Clean, well-scoped fix for CORE-410. The root cause is correctly identified: from_iso8601_timestamp() returns timestamp with time zone, and date_add inherits the type of its third argument (the origin). When Athena writes the result to a Hive-formatted export bucket, Hive rejects timestamp(3) with time zone. The fix wraps the origin in CAST(... AS TIMESTAMP) to strip the timezone, which is consistent with the existing pattern already used in convertTz (line 55) and the SEQUENCE templates (lines 189, 194).

Detailed Review

Core Fix — PrestodbQuery.ts

Correct and consistent. The change at line 76:

const originExpr = `CAST(${this.timeStampCast(`'${origin}'`)} AS TIMESTAMP)`;

follows the same CAST(from_iso8601_timestamp(...) AS TIMESTAMP) pattern used elsewhere in the file. Since AthenaQuery and TrinoQuery both extend PrestodbQuery and neither overrides dateBin, the fix propagates to all three dialects.

One consideration for Trino: Trino's convertTz explicitly uses CAST((field AT TIME ZONE ...) AS TIMESTAMP), so it also benefits from a plain TIMESTAMP origin. No issue here — just confirming the inheritance is intentional and correct.

The originExpr is referenced twice in the date_add/date_diff template (lines 78–83). Since it's assigned to a local const, there's no risk of the CAST being evaluated twice at the SQL engine level — it's just string interpolation. Good.

Unit Test — athena-query.test.ts

The test is well-structured:

  1. Positive assertion (toMatch) confirms the CAST wrapper is present in loadSql.
  2. Negative assertion (not.toMatch) confirms that a bare from_iso8601_timestamp(...) no longer appears as the direct argument to date_add.

The comment block (lines 67–72) is substantive and explains the why — the Hive serialization constraint. This is one of the cases where a comment genuinely adds value.

Minor observations:

  • The test hardcodes '2026-04-11' / '2026-04-12' as a date range — these are future dates but that's fine for unit tests since they're just generating SQL strings, not executing against a real database.
  • The negative regex date_add\([^)]*from_iso8601_timestamp\('[^']+'\)\s*\) could theoretically have false negatives if the generated SQL has nested parentheses in the first date_add argument, but for this specific SQL shape it's adequate.

Integration Test Fixtures — _schemas.json + athena.json + testQueries.ts

The approach is smart: a uniquely-named granularity (build_only_half_year) and rollup (TBuildOnlyHalfYear) ensure the build path is exercised without any query test accidentally matching this rollup — keeping existing snapshots stable.

The type === 'athena' guard in testQueries.ts (line 234) correctly scopes this to Athena only, since the Hive export bucket issue is Athena-specific.

One thing to note: the pre-aggregation is referenced as ECommerce.TBuildOnlyHalfYearExternal in testQueries.ts, but the fixture in athena.json defines it as TBuildOnlyHalfYear (no External suffix). This follows the existing convention where the External suffix is appended by the test framework — confirmed by the pattern of TAExternal, SAExternal, etc. in the same file. Consistent.

Verdict

Looks good to merge. The fix is minimal, correctly targeted, consistent with existing patterns in the file, and well-tested at both the unit and integration levels. No security, performance, or correctness concerns.

…bucket

`PrestodbQuery.dateBin` used `from_iso8601_timestamp(...)` as the
`date_add` origin, which returns `timestamp with time zone`. Because
`date_add` inherits the type of its third argument, custom-granularity
rollup columns ended up typed as `timestamp(3) with time zone` — a type
Hive cannot serialize when writing to an Athena export bucket, failing
the CTAS with `NOT_SUPPORTED: Unsupported Hive type: timestamp(3) with
time zone`. Wrap the origin in `CAST(... AS TIMESTAMP)` so the result
is a plain TIMESTAMP, matching the shape already produced by
`timeGroupedColumn`/`date_trunc` for standard granularities.

Also close a testing-drivers E2E coverage gap: until now no rollup with
a custom granularity was ever built against the Athena export bucket
in CI, so the regression had no test to catch it. Introduce a unique
`build_only_half_year` granularity and a `TBuildOnlyHalfYear` rollup
on `ECommerce.orderDate`, built explicitly in the `must built
pre-aggregations` step. The granularity name is unique to this rollup
so no query test can match it (keeping snapshots stable in both
legacy and Tesseract planner modes).

Refs CORE-410.
@codecov

codecov Bot commented May 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.50%. Comparing base (f3da859) to head (0460dc5).
⚠️ Report is 7 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (f3da859) and HEAD (0460dc5). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (f3da859) HEAD (0460dc5)
cubesql 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #10950       +/-   ##
===========================================
- Coverage   83.57%   58.50%   -25.07%     
===========================================
  Files         254      216       -38     
  Lines       76126    17006    -59120     
  Branches        0     3454     +3454     
===========================================
- Hits        63622     9950    -53672     
+ Misses      12504     6553     -5951     
- Partials        0      503      +503     
Flag Coverage Δ
cube-backend 58.50% <100.00%> (?)
cubesql ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Extend the custom-granularity pre-aggregation build to all driver
fixtures, not just Athena. The `build_only_half_year` granularity and
the `ECommerce.TBuildOnlyHalfYear` rollup are now defined in every
fixture, and the `must built pre-aggregations` step unconditionally
builds it.

Drives CI to surface any other drivers that struggle with rollup-build
from a custom-granularity time grouping. Drivers that genuinely cannot
support this can be skipped per-fixture once we see which ones break.
…fYear rollup

ClickHouseQuery.preAggregationLoadSql refuses to compile a rollup
without an indexes block (it needs at least one to produce the
`ORDER BY` clause of the underlying MergeTree). Mirror what the
other ECommerce rollups in clickhouse.json do.
@claude

claude Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @waralexrom's task in 2m 18s —— View job


Code Review: PR #10950 — Athena custom-granularity pre-aggregation build fix

  • Gather context and read all changed files
  • Review core fix in PrestodbQuery.ts
  • Review unit test in athena-query.test.ts
  • Review testing driver fixture changes
  • Review testQueries.ts integration test logic
  • Post comprehensive review

Summary

Clean, well-targeted fix for CORE-410. The root cause is correctly identified: from_iso8601_timestamp() returns timestamp with time zone, and date_add inherits the type from its third argument (the origin). When Athena writes the result to a Hive-formatted export bucket, Hive rejects timestamp(3) with time zone. The fix wraps the origin in CAST(... AS TIMESTAMP) to strip the timezone — consistent with the existing pattern already used in convertTz (line 55) and the SEQUENCE templates (lines 189, 194).

Detailed Review

Core Fix — PrestodbQuery.ts (line 76)

Correct and consistent. The one-line change:

const originExpr = `CAST(${this.timeStampCast(`'${origin}'`)} AS TIMESTAMP)`;

follows the exact same CAST(from_iso8601_timestamp(...) AS TIMESTAMP) pattern used in three other places in this file:

  • convertTz (line 55)
  • generated_time_series_select SEQUENCE call (line 189)
  • generated_time_series_with_cte_range_source SEQUENCE call (line 194)

Inheritance is correct. Both AthenaQuery and TrinoQuery extend PrestodbQuery and neither overrides dateBin, so the fix propagates to all three dialects. For Trino, convertTz already uses CAST(... AS TIMESTAMP), so a plain TIMESTAMP origin in dateBin is consistent.

The originExpr is referenced twice in the date_add/date_diff template (lines 78–83). Since it's a local const, the CAST(...) is only a string-interpolation concern — no risk of double evaluation at the SQL engine level.

No concerns on this change.

Unit Test — athena-query.test.ts

Well-structured regression test:

  1. Positive assertion (toMatch) confirms the CAST(from_iso8601_timestamp(...) AS TIMESTAMP) wrapper is present in loadSql.
  2. Negative assertion (not.toMatch) confirms that a bare from_iso8601_timestamp(...) no longer appears as a direct argument to date_add.

The comment block (lines 67–72) explains the why — the Hive serialization constraint. Good use of comments for a non-obvious invariant.

No issues found.

Integration Test Fixtures

Approach is well-designed. The uniquely-named granularity (build_only_half_year) and rollup (TBuildOnlyHalfYear) ensure the build path is exercised without any query test accidentally matching this rollup — keeping existing snapshots stable.

Note on scope evolution: The PR description mentions a type === 'athena' guard, but the final code (commit cc7fd71) broadened this to build the rollup for every driver — which provides better coverage. The ClickHouse fixture (commit 0460dc5) correctly adds the required indexes block, matching the pattern used by other ClickHouse pre-aggregations in that file.

All 9 driver fixture files (athena, bigquery, clickhouse, databricks-jdbc, mssql, mysql, postgres, redshift, snowflake) consistently define the same TBuildOnlyHalfYear rollup structure (with the ClickHouse variant adding the required index). Clean and complete.

testQueries.ts

The build call at line 234 references ECommerce.TBuildOnlyHalfYearExternal, which follows the existing convention where the External suffix is appended by the test framework (consistent with TAExternal, SAExternal, etc.). The placement inside the 'must build pre-aggregations' block is correct.

Verdict

Looks good to merge. The fix is minimal, correctly targeted, consistent with existing patterns, and well-tested at both unit and integration levels. No security, performance, or correctness concerns.

@waralexrom
waralexrom merged commit f39bcf0 into master May 28, 2026
119 of 120 checks passed
@waralexrom
waralexrom deleted the cube-athena-custom-granularity-athena branch May 28, 2026 10:10
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ Proxy