Rewrite APPLY over json_each into a join on SQLite#38688
Open
Aykuttonpc wants to merge 1 commit into
Open
Conversation
SQLite doesn't support APPLY, so queries projecting a JSON collection failed with "APPLY not supported". Its table-valued functions can reference preceding tables in the FROM clause though, which is what APPLY is needed for here. Add SqliteApplyFlatteningExpressionVisitor, which runs before the APPLY validator and rewrites CROSS/OUTER APPLY over a subquery whose only table is json_each into a CROSS/LEFT JOIN over that function, inlining the subquery's projection into the containing SELECT. Only subqueries that do nothing else (no predicate, ordering, paging, grouping or DISTINCT) are rewritten, since lifting those out would change the meaning of the query. Part of dotnet#34375
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.
SQLite doesn't support APPLY, so projecting a JSON collection currently fails with
ApplyNotSupported. SQLite's table-valued functions can reference preceding tables in the FROM clause though, so the APPLY that EF produces for these queries can be expressed as a regular join.This adds
SqliteApplyFlatteningExpressionVisitor, which runs before the existing APPLY validator and rewrites CROSS/OUTER APPLY over a subquery whose only table isjson_eachinto a CROSS/LEFT JOIN over that function, inlining the subquery's projection into the containing SELECT.Json_collection_in_projection_with_anonymous_projection_of_scalarsnow translates to:Scope
Only subqueries that do nothing beyond projecting out of
json_eachare rewritten:Lifting a predicate or a limit out of the subquery would change what the query means, so anything else still goes through the validator and reports
ApplyNotSupportedas before. That's why this says "Part of" rather than "Fixes".Tests
The full SQLite functional suite passes (38367 tests, 0 failures). Nine tests that previously asserted
ApplyNotSupportednow run against the real query, so their overrides were removed:JsonQuerySqliteTest.Json_collection_in_projection_with_anonymous_projection_of_scalarsComplexJsonProjectionSqliteTestandOwnedJsonProjectionSqliteTest:SelectMany_associate_collection,SelectMany_nested_collection_on_required_associate,SelectMany_nested_collection_on_optional_associateThe SelectMany ones weren't something I targeted, they started working as a side effect of the rewrite.
One open question
The LEFT JOIN uses a constant true predicate, which renders as
ON 1, since there is no real join condition. Let me know if you'd rather see something else there.Part of #34375