Skip to content

Eng 1062 optimize all relations#558

Open
maparent wants to merge 4 commits into
mainfrom
ENG-1062-optimize-all-relations
Open

Eng 1062 optimize all relations#558
maparent wants to merge 4 commits into
mainfrom
ENG-1062-optimize-all-relations

Conversation

@maparent

@maparent maparent commented Nov 19, 2025

Copy link
Copy Markdown
Collaborator

Use a pseudo-relation for a query for all relations. Rework the algorithm for grouping results, which means adding some selections.

https://www.loom.com/share/ced1e4cf67ec4acdb97db690431ee9a0

@linear

linear Bot commented Nov 19, 2025

Copy link
Copy Markdown

@supabase

supabase Bot commented Nov 19, 2025

Copy link
Copy Markdown

This pull request has been ignored for the connected project zytfjzqyijgagqxrzbmz because there are no changes detected in packages/database/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@maparent maparent changed the base branch from main to eng-1012-update-query-builder-relation-conditions-to-use-reified November 19, 2025 19:13
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch 2 times, most recently from c3c3760 to 86e97e1 Compare November 27, 2025 18:40
@maparent maparent force-pushed the eng-1012-update-query-builder-relation-conditions-to-use-reified branch 4 times, most recently from 048ce21 to e5f66c1 Compare November 27, 2025 21:04
Base automatically changed from eng-1012-update-query-builder-relation-conditions-to-use-reified to main November 27, 2025 21:06
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 86e97e1 to 4ee948f Compare November 27, 2025 21:09
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 4ee948f to c94af43 Compare December 10, 2025 16:41
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from c94af43 to c23d381 Compare December 24, 2025 22:01
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from c23d381 to 4db181b Compare January 12, 2026 15:38
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 4db181b to 278e684 Compare April 29, 2026 17:57
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 158b75d to 370ba81 Compare May 13, 2026 13:57
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 370ba81 to e3ca7ca Compare May 21, 2026 13:41
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from e3ca7ca to 7d1efe8 Compare June 22, 2026 22:06
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
discourse-graph Skipped Skipped Jun 24, 2026 1:24pm

Request Review

@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 7d1efe8 to 374a2ce Compare June 22, 2026 22:08
@maparent maparent force-pushed the ENG-1062-optimize-all-relations branch from 374a2ce to 926d8a8 Compare June 23, 2026 11:59
text: `node:${conditionUid}-Context`,
});
}
if (ANY_RELATION_REGEX.test(r.label)) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detecting the "all relations" case

});

const relationsWithComplement = Array.from(uniqueRelations.values());
const queryRelations = useReifiedRelations

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a new case for all relations

nodeTextByType,
onResult,
);
if (

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we look at all relations at once, we have to re-group by relation type here.

return "?relSchema";
},
mapper: () => {
// not sure here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results are the same between both versions, so I think I got it right, but I'll admit to a bit of guesswork here.

@maparent maparent marked this pull request as ready for review June 23, 2026 14:39
@maparent maparent requested a review from mdroidian June 23, 2026 14:39

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 potential issues.

Open in Devin Review

Comment thread apps/roam/src/utils/getDiscourseContextResults.ts Outdated
text: ruid.endsWith("-false")
? relation.r.label
: relation.r.complement,
target: targetUid,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 target set to UID instead of node type, producing undefined in results

In the reified relations regrouping block, target is set to targetUid (a block UID). This value is later used at line 323 as nodeTextByType[r.relation.target] to look up the display text, but nodeTextByType is keyed by node type strings (e.g., "QUE", "CLM", "*"), not UIDs. This will always resolve to undefined, breaking the "group by target" feature in DiscourseContext.tsx:46 which maps results via res.target. In the non-reified path, target is correctly set to the node type via buildQueryConfig at line 124 (isComplement ? r.source : r.destination). The reified path should similarly use the relation's source or destination type.

Suggested change
target: targetUid,
target: ruid.endsWith("-true") ? relation.r.source : relation.r.destination,
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread apps/roam/src/utils/getDiscourseContextResults.ts
Comment thread apps/roam/src/utils/getDiscourseContextResults.ts
@mdroidian

Copy link
Copy Markdown
Member

@maparent Devin flagged a few issues here. Could you take a look and address them, then re-tag me once they’re resolved?

@maparent

Copy link
Copy Markdown
Collaborator Author

@mdroidian resolved the issues.

@mdroidian mdroidian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the high-level goal here: in reified-relations mode, discourse context should not have to run one query per relation type. A single “all relations touching this node” query, followed by regrouping, is the right direction.

I don’t think we should merge the current implementation shape as-is, though. It feels too brittle/indirect for a core discourse context path.

The main issue is that hasSchema and effectiveSource are being added as synthetic predefined selections so we can carry internal relation metadata out of the query. Predefined selections are part of the Query Builder/user-facing selection layer; these values are not real user selections, they are internal Datalog variables needed by this one caller. The placeholder mapper implementations returning "?relSchema" / "?relSource" are a signal that this is not really a selection abstraction. That also forces relSchema and relSource to be preserved globally in the relation translator.

There are also some correctness issues in the regrouping:

  • The complement handling looks backwards. For keys ending in -true, the code sets isComplement: true but uses relation.r.label for label/text; for -false, it uses relation.r.complement. Based on how uniqueRelations is built, and based on the existing text = isComplement ? r.complement : r.label logic, those should be flipped.
  • id becomes the grouped key like ${relationUid}-${direction} instead of the underlying relation schema id. That changes the meaning of the result id from relation id to implementation grouping key. Direction already has isComplement, so the grouped key should not escape as id.
  • onResult behavior changed. Previously executeQueries received onResult and could call it per relation query as each query completed; now results are buffered, empty groups are filtered, and callbacks fire at the end. That may be intentional, but it is a behavior change outside just the reified-relations optimization and should be tested/called out.

There are a few ways we could take this. I’ll elaborate on two:

  • Option 1: add a findVariables internal API.
  • Option 2: special-case the discourse context query outside of the Query Builder translator.

I put together an example of Option 1 here: #1160

That keeps the one-query approach and still reuses the existing Has Any Relation To translator, but it adds an internal findVariables API to fireQuery instead of adding fake predefined selections. So discourse context can explicitly ask for the scoped relSchema / relSource variables it needs, without exposing them as Query Builder selections.

Option 2 would be to special-case discourse context directly: since the discourse context overlay is the hot path here, we could build a direct reified-relation Datalog query for this flow and map the stored relation metadata into groups ourselves. That would be less reusable with Query Builder, but it may be the clearest implementation if this optimization is specifically for discourse context.

My recommendation is that we should not keep the fake predefined-selection approach. I’d be comfortable with either Option 1 from the example PR, or Option 2 as a dedicated discourse-context query path, but the current version is coupling too many layers in a brittle way.

? [
{
r: {
id: ANY_RELATION_ID,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you add the id outside r as well, it'll match the type RelationWithComplement cleanly

{
r: {
id: ANY_RELATION_ID,
complement: "Has Any Relation To",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reuse ANY_RELATION_REGEX.source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants