Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Tentative fix for #1128
Browse files Browse the repository at this point in the history
The subquery that builds the final contract tree when querying with
links uses an inner join to get the nested link. Roughly:

```
JOIN (
  // Build the linked contract...
) AS linked
ON edge.source = linked.id
```

These joins are nested in the case of nested links and there is a
toplevel `LATERAL`. In the case of two nested optional links, the
`LATERAL` subquery looks roughly like this:

```
FROM
  cards,
  LATERAL (
    // ...
    JOIN (
      // ...
    ) AS linked
    ON edge1.source = linked.id
    // ...
  )
WHERE edge0.source = cards.id
```

If the optional contracts don't exist, `linked.id` will be null and the
`edge1.to = linked.id` condition will fail. As a consequence, the whole
lateral will be empty and so will the result. The fix is change those
inner joins into left joins.

This is a tentative fix because I have fuzzy memories of making this
exact change and causing unrelated tests to fail.

Fixes #1128
Change-type: patch
Signed-off-by: Carol Schulze <carol@balena.io>
  • Loading branch information
Ereski committed Dec 20, 2021
1 parent 51573b2 commit f2e3b8a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/backend/postgres/jsonschema2sql/sql-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const pushLinkedLateral = (
.pushLeftJoin(cardsTable, lateralJoinFilter, 'linked')
.pushGroupBy('orderedEdges', SqlPath.fromArray(['source']));
for (const [nestedLateral, nestedLateralAlias] of nestedLaterals) {
lateral.pushInnerJoin(
lateral.pushLeftJoin(
nestedLateral,
new LiteralSql(`${nestedLateralAlias}.source = linked.id`),
nestedLateralAlias,
Expand Down
42 changes: 42 additions & 0 deletions test/integration/kernel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7648,5 +7648,47 @@ describe('Kernel', () => {

await once(stream, 'closed');
});

it('issue #1128: should be able to query for two nested optional links', async () => {
const card = await ctx.kernel.insertCard(
ctx.context,
ctx.kernel.sessions!.admin,
{
slug: ctx.generateRandomSlug(),
type: 'card@1.0.0',
},
);

const results = await ctx.kernel.query(
ctx.context,
ctx.kernel.sessions!.admin,
{
properties: {
id: {
const: card.id,
},
},
anyOf: [
{
$$links: {
'is attached to': {
anyOf: [
{
$$links: {
'is attached to': {},
},
},
true,
],
},
},
},
true,
],
},
);

expect(results).toEqual([card]);
});
});
});

0 comments on commit f2e3b8a

Please sign in to comment.