Parameterize all SQL values when multiplexing subscription queries #2942
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.
Description
When multiplexing subscription queries, we lose out on significant opportunities to group multiple queries together because we only parameterize over SQL values that originally came from GraphQL variables. For ordinary queries, that makes sense, since parameterization only really helps us reuse query plans, and we can only reuse query plans if all values come from GraphQL variables (since the generated SQL won’t change).
For the purposes of live queries, however, it’s too pessimistic. For example, consider the following query:
This query might be executed with similar values for
$condition, such as{"album_id": {"_eq": "1"}}and{"album_id": {"_eq": "2"}}. Since all that changes in the generated SQL is the value being compared against, we can still take advantage of the similarity between the two queries by multiplexing them together.As it was useful for implementing the test suite, this set of changes also adds support for explaining subscriptions, reusing some of the functionality from #2650 and otherwise subsuming it.
Affected components
Related Issues
https://github.com/hasura/graphql-engine-internal/issues/260#issuecomment-532699211
Solution and Design
To implement this, multiplexed queries now have a third input, synthetic variables, in addition to query and session variables. Synthetic variables are collected from the generated SQL in the order they appear, and they are passed as a JSON array and accessed by index.
Synthetic variable values can be cached and reused by reusable query plans, so their validated values are also stored in
ReusableLiveQueryPlanvalues.Steps to test and verify
The test suite verifies the implementation by checking that the generated SQL is identical for two queries that previously generated different SQL.