Skip to content

Don't join an or at the start of a group to the previous CTE - #2948

Open
verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/nested-or-ambiguous-column
Open

verveguy wants to merge 1 commit into
instantdb:mainfrom
liminisapp:fix/nested-or-ambiguous-column

Conversation

@verveguy

Copy link
Copy Markdown
Contributor

Fixes #2947.

accumulate-ctes already avoids joining an or's gather CTE to the previous CTE when that CTE is the last one of a sibling or branch. It still joins it in two other cases:

  • when the or starts a branch of an outer or, where the previous CTE comes from before the outer or;
  • when the or starts a pattern group, where the previous CTE belongs to the previous query.

The first selects the earlier CTE's columns twice, so Postgres rejects them as ambiguous. The second builds the or from another query's result rows, so it returns nothing when that query is empty.

This treats an or at the start of a group (group-idx 0) like a single pattern there. cte-for-pattern already does the same, through start-of-group?, and doesn't join to the previous CTE.

or-at-the-start-of-a-group covers both shapes on the zeneca app. Without the fix, the first assertion fails with ambiguous-column and the second returns no users. I also ran the instant.db.*, instant.reactive.* and instant.admin.* suites; all 393 tests passed, with 19,653 assertions.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HRKkuGrp63pNCgPK1hVdC7

An or's gather cte was joined to the cte before it even when that cte
belonged to something else: the patterns before an enclosing or, or the
previous query. The first selected those columns twice, so a where like
a AND (b OR ((c OR d) AND e)) failed with an ambiguous column. The second
built the or from another query's rows, so it returned nothing when that
query was empty. Treat an or at group-idx 0 as cte-for-pattern treats a
single pattern there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRKkuGrp63pNCgPK1hVdC7
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: eb01214b-72bf-42de-94fc-00bd42613430
📥 Commits

Reviewing files that changed from the base of the PR and between b97a116 and 8884f86.

📒 Files selected for processing: 2
  • server/src/instant/db/datalog.clj
  • server/test/instant/db/instaql_test.clj

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

The CTE accumulation logic now starts an OR gather without a previous CTE when the group begins at index zero. Two regression tests cover nested OR conditions and a later query whose preceding query has no matches.

Changes: OR Group CTE Handling

Layer / File(s) Summary
Group-start OR handling and regression coverage
server/src/instant/db/datalog.clj, server/test/instant/db/instaql_test.clj
accumulate-ctes now starts the OR gather without a previous CTE when the group index is zero. Regression tests cover a nested OR condition and an OR query following a query with no matches.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: dwwoelfel

Merge Risk: ⚪ Minimal · up to 8884f

The change addresses the two reported OR-query failures, with no identified blocker beyond normal test checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main fix: preventing an OR at the start of a group from joining the previous CTE.
Description check Passed The description directly explains the affected cases, the implementation change, regression coverage, and test results.
Linked Issues check Passed Issue #2947 requires an or at group-idx 0 to avoid joining the preceding CTE, including nested and later top-level query cases. The supplied change summary states that accumulate-ctes now passes…
Out of Scope Changes check Passed The reported changes are limited to the accumulate-ctes implementation and a regression test in server/test/instant/db/instaql_test.clj. Both changes directly support issue #2947. No unrelated cha…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Full details: Linked Issues check

Explanation

Issue #2947 requires an or at group-idx 0 to avoid joining the preceding CTE, including nested and later top-level query cases. The supplied change summary states that accumulate-ctes now passes -1 to or-gather-cte for the start-of-group case. The added or-at-the-start-of-a-group test covers both reported cases and checks the expected results. The reported test suites passed with 393 tests and 19,653 assertions.

Full details: Out of Scope Changes check

Explanation

The reported changes are limited to the accumulate-ctes implementation and a regression test in server/test/instant/db/instaql_test.clj. Both changes directly support issue #2947. No unrelated change is identified.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An or at the start of a where group joins to the wrong CTE: ambiguous column, or an empty result

1 participant