Skip to content

fix(schema-compiler): report self and circular member references at compile time (#4) - #12206

Open
bhargavmehta01 wants to merge 3 commits into
cube-js:masterfrom
bhargavmehta01:fix/schema-compiler-circular-member-references
Open

bhargavmehta01 wants to merge 3 commits into
cube-js:masterfrom
bhargavmehta01:fix/schema-compiler-circular-member-references

Conversation

@bhargavmehta01

Copy link
Copy Markdown

Check List

  • Tests have been run in packages where changes have been made if available
  • Linter has been run for changed code
  • Tests for the changes have been added if not covered yet
  • Docs have been added / updated if required (n/a)

Issue Reference this PR resolves

Fixes #4

Description of Changes Made

A member whose sql references itself, such as

foo: { sql: `${foo}`, type: `string` }

or that reaches itself through other members of the same cube (a -> b -> a), can never be resolved. The original RangeError: Maximum call stack size exceeded no longer happens, since Tesseract now stops at CUBEJS_MAX_MEMBER_RESOLUTION_DEPTH. But the model still compiles cleanly and only fails at query time, with an error that doesn't say the member points back at itself:

Member 'visitors.foo' references members more than 160 levels deep (through 'visitors.foo'), against a limit of 160. ... Reference fewer members in a chain, or raise CUBEJS_MAX_MEMBER_RESOLUTION_DEPTH.

Raising the limit, as that message suggests, can't help here. This PR adds the self-reference guard the issue asks for in the schema compiler, so the model fails at compile time with the cycle spelled out:

Member 'visitors.foo' references itself in its sql. A member can't reference itself.
Member 'visitors.a' has a circular reference: visitors.a -> visitors.b -> visitors.c -> visitors.a. A member can't reference itself through other members.

How it works:

  • prepareMembers already calls collectUsedCubeReferences for each member's sql. It now also records the same-cube member names from pathReferencesUsed (bare ${foo}, ${CUBE.foo}, and ${createdAt.granularity} all resolve there). Nothing extra is evaluated.
  • checkCircularMemberReferences runs a DFS over that per-cube graph (measures, dimensions, and segments together) and reports each cycle once through the error reporter.
  • Cross-cube cycles are out of scope here and are still stopped by Tesseract's depth limit.

Verification

I couldn't run the full monorepo jest setup locally. Instead I applied the equivalent change to the published @cubejs-backend/schema-compiler@1.8.3 build and checked:

  • Self reference (${foo} and ${CUBE.foo}), 2-member and 3-member dimension cycles, a measure cycle, and a YAML self reference all fail at compile time with the messages above.
  • A valid model that uses ${CUBE}, ${CUBE.id}, a custom granularity reference, a number measure over ${count}, a segment, a cross-cube reference, and a view still compiles, and its query SQL is unchanged.
  • Every model under test/unit/fixtures compiles with exactly the same results before and after the change (no new errors).

New tests are in test/unit/circular-member-references.test.ts.

@bhargavmehta01
bhargavmehta01 requested a review from a team as a code owner October 11, 2026 04:54
@github-actions github-actions Bot added javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members. labels Oct 11, 2026
@bhargavmehta01
bhargavmehta01 force-pushed the fix/schema-compiler-circular-member-references branch 2 times, most recently from 6db9c76 to a077364 Compare October 17, 2026 16:14
…ompile time (cube-js#4)

Signed-off-by: Bhargav Mehta <bhargav.mehta01@gmail.com>
@bhargavmehta01
bhargavmehta01 force-pushed the fix/schema-compiler-circular-member-references branch from a077364 to 9cbba5d Compare November 1, 2026 04:20
@bhargavmehta01

Copy link
Copy Markdown
Author

Rebased on latest master (9cbba5d), no conflicts, and the new circular-member-references unit tests pass locally. The CI workflows for this fork PR are waiting on maintainer approval. @ovr @waralexrom could you approve the workflow runs and take a look when you get a chance? Thanks!

@bhargavmehta01

Copy link
Copy Markdown
Author

Audit update: GitHub reports no merge conflicts and no review threads are open. The change remains scoped to same-cube member-reference cycles with focused compiler tests; no unrelated changes were made. Build/review workflows need maintainer approval before their results can establish review readiness. No fresh full-monorepo validation was performed in this audit.

@bhargavmehta01

Copy link
Copy Markdown
Author

Updated the existing branch with current master in dc8af35. All 6 circular-member-reference regressions pass on the updated source, including YAML, self references, multi-member cycles, and non-cyclic models. Validation used isolated transpilation/Jest with published Cube 1.8.26 dependencies; a full monorepo build/typecheck still needs upstream CI approval.

@bhargavmehta01

Copy link
Copy Markdown
Author

Updated the existing branch with current master (7f033f3e3) at 49b4b3f4a. All 6 circular-member-reference regressions pass on the updated source. Validation uses the isolated Jest/TypeScript setup with published Cube 1.8.26 dependencies; a full monorepo build/typecheck remains unverified. The fresh upstream Build and review workflows are action_required, so maintainer workflow approval is still needed before this can be called fully review-ready. No review discussions are unresolved.

This branch has not been deployed

No deployments
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.

Self reference guard in schema compiler

1 participant