Repository navigation
Rewrite APPLY over json_each into a join on SQLite - #38688
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves SQLite query translation by rewriting certain CROSS/OUTER APPLY patterns (which SQLite doesn’t support) into equivalent CROSS/LEFT JOIN forms for trivial json_each subqueries, enabling previously-failing JSON collection projections to translate and execute successfully.
Changes:
- Added
SqliteApplyFlatteningExpressionVisitorto flatten eligible APPLY-over-json_eachsubqueries into joins. - Hooked the new visitor into
SqliteQueryTranslationPostprocessorbefore the existing APPLY validator. - Updated/removes SQLite functional test overrides that previously asserted
ApplyNotSupported, and added a new SQL assertion for the now-supported translation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.Sqlite.FunctionalTests/Query/JsonQuerySqliteTest.cs | Updates a test to assert the new JOIN-based SQL translation instead of ApplyNotSupported. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/OwnedJson/OwnedJsonProjectionSqliteTest.cs | Removes overrides that existed only to assert APPLY-not-supported for SelectMany JSON cases. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonProjectionSqliteTest.cs | Removes overrides that existed only to assert APPLY-not-supported for SelectMany JSON cases. |
| src/EFCore.Sqlite.Core/Query/Internal/SqliteQueryTranslationPostprocessor.cs | Invokes the new flattening visitor before running the APPLY validator. |
| src/EFCore.Sqlite.Core/Query/Internal/SqliteApplyFlatteningExpressionVisitor.cs | Implements the APPLY-to-JOIN rewrite and projection inlining for trivial json_each subqueries. |
|
Thanks @copilot, good catch. Fixed by running the projection inliner over the table list as well, so a sibling table referencing the flattened alias (like a nested json_each) gets remapped instead of dangling. Full SQLite functional suite is still green. |
|
@Aykuttonpc Rebase on latest main please |
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
Copilot noted that column references to a flattened APPLY's alias inside sibling tables (e.g. a nested json_each keyed off it) weren't being remapped, so they could point at a now-removed alias. Run the projection inliner over the new table list as well.
180d931 to
50bd183
Compare
|
Rebased this onto latest main. The conflict was in About the red I opened #38949 with the fix in case it is useful. The legs that actually exercise this change are all passing here: Sqlite on ubuntu and windows, Microsoft.Data.Sqlite on both, SqlServer 2019, 2022 and 2025, Cosmos and API Baselines. Locally the full SQLite functional suite is clean as well. |
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
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