Support circular references in Sync Streams#608
Merged
Conversation
🦋 Changeset detectedLatest commit: 8650664 The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
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 |
rkistner
reviewed
Apr 16, 2026
Contributor
rkistner
left a comment
There was a problem hiding this comment.
Another case still failing (picked up by Codex):
SELECT a.*
FROM a, b, json_each(a.tags) AS j
WHERE a.k = b.k AND j.value = b.vI'm not sure if it is actually feasible or useful to support this case, but maybe we can have a more descriptive error message?
748d9d3 to
8650664
Compare
Contributor
Author
We should be able to support that as well, I've fixed that and added a test 👍 |
rkistner
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For Sync Streams, building a graph of parameter lookups is implemented by going through expressions and recursively handling dependencies. E.g. if a stream has outputs of a table
aand we encounter a filter expressiona.foo = b.bar, we'd try to resolvebas a parameter lookup recursively before looking at additional expressions. This generally works well, because it both resolves partition keys and parameter outputs while sorting the flat list of expressions topologically, giving us a description for a querier.Because of this recursion, we can't support circular references between result sets. Initially, I believed that circular references are inherently unsupported (we need to look up parameters in sequence, after all). Then I looked at an actual instance of this in the wild, and realized that the circular reference isn't really all that circular:
Currently, we start by analyzing
invoices, trackstoresas a dependency due toinvoices.store_name = stores.name. When parsingstores, we discoverinvoices.region = stores.regionand crash due to a circular reference. The fix is very simple: Ignore expressions referencing a result set that is currently being analyzed, so we'd then:invoices, trackstoresas a dependency due toinvoices.store_name = stores.name.stores, findstores.owner = auth.user_id()as the only expression and create a pending parameter lookup partitioned byownerwithout any outputs.invoices.store_name = stores.nameexpression by addingstores.nameas an output to the lookup andinvoices.store_nameas a bucket parameter.invoices.region = stores.regionexpression in the context ofinvoices, realize we've resolvedstoresalready and just do the same add output + bucket parameter step with the resolved parameter lookup.The same strategy works for larger reference chains as well.