Skip to content

Conversation

@simolus3
Copy link
Contributor

Evaluating streams using subqueries with multiple parameters is currently broken in two ways. To illustrate this, consider this example taken from a report on Discord:

SELECT *
FROM "Scene"
WHERE
  project IN (
    SELECT project
    FROM "ProjectInvitation"
    WHERE
    AND (auth.parameters() ->> 'haystack_id') IN "appliedTo"
    AND project = subscription.parameter('project')
  )

This subquery has two parameter match clauses: the IN operator and the direct equals.

The first issue is that we used to compute the WHERE clause of the subquery with SqlTools.compileClause. That method will merge the AND into a single ParameterMatchClause, which is valid for bucket definitions but not for streams! Since each parameter is an explicit object with streams, this would cause two parameters to be derived from the same ParameterMatchClause, which then causes values for the two parameters to get duplicated:

  • Before: evaluateParameterRow(projectInvitation, {project: 'p', appliedTo: '[1,2]'}) yields lookup with keys [1,p], [1,p], [2, p], [2, p].
  • After: Yields lookup with keys [1, p], [2, p].

The reason we used compileClause is that we only support a subset of filter operators for WHERE clauses in subqueries, and those happened to align with what SqlTools.compileClause would support. So the first part of the fix is to compile subquery predicates with the same logic as we compile the outer query, and then applying a validator to ensure there are no nested subqueries or other unsupported filters.

A second issue is that StreamVariant.findStaticInstantiations (used in Subquery.compileEvaluator) was missing a transform from "list of values per parameter" to "list of lookups" via the cartesian product. That's only relevant for subqueries with more than one parameter:

  • Before: Would attempt to lookup [1], [p].
  • After: Looks up the correct key [1, p].

@simolus3 simolus3 requested a review from rkistner November 27, 2025 09:58
@changeset-bot
Copy link

changeset-bot bot commented Nov 27, 2025

🦋 Changeset detected

Latest commit: 9870e89

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@powersync/service-sync-rules Patch
@powersync/service-jpgwire Patch
@powersync/service-core-tests Patch
@powersync/service-core Patch
@powersync/lib-services-framework Patch
@powersync/service-module-mongodb-storage Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres-storage Patch
@powersync/service-module-postgres Patch
@powersync/lib-service-postgres Patch
@powersync/service-module-core Patch
@powersync/service-image Patch
test-client Patch
@powersync/service-rsocket-router Patch
@powersync/lib-service-mongodb Patch
@powersync/service-schema Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@simolus3 simolus3 merged commit 0156d10 into main Nov 27, 2025
22 checks passed
@simolus3 simolus3 deleted the subquery-fix branch November 27, 2025 11:47
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.

3 participants