SQL Server: OPENJSON WITH ... AS JSON columns carry a json type mapping even when the server returns nvarchar(max)
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Start by tracing TransformJsonQueryToTable, the relational CreateSelect path that creates column expressions, and SqlServerQuerySqlGenerator.GenerateColumnInfo. Compare the mappings for primitive collections and nested JSON navigations with the SQL emitted on Azure SQL and below compatibility level 170; then review the JSON_CONTAINS and JSON_VALUE ... RETURNING paths and their tests. Done means AS JSON columns carry mappings matching their returned SQL type, affected query baselines are updated, and the workaround in #39715 is no longer needed.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Problem
On platforms where OPENJSON ... WITH cannot use json (Azure SQL, and SQL Server below compatibility level 170), AS JSON columns are returned as nvarchar(max). SqlServerQuerySqlGenerator.GenerateColumnInfo already accounts for this and emits nvarchar(max) in the WITH clause. However, the query tree still describes these columns with a json store type:
TransformJsonQueryToTableassignselement.StoreTypeMapping(primitive collections) andjsonQueryExpression.JsonColumn.TypeMapping(nested owned/complex navigations) to theAS JSONcolumn infos.- The column expressions that refer to them, created via the relational
CreateSelectfrom the model's mappings, inherit the samejsonmapping.
Everything downstream that decides based on the store type is therefore misled.
Known consequences
JSON_CONTAINSover nested collections fails with error 8116 (#39626). TheContainstranslation gate (Json: { TypeMapping: SqlServerJsonTypeMapping }) accepts the column, andEF.Functions.JsonContainspasses it through as is.
#39715 works around this inSqlServerJsonPostprocessor. It works out where the document came from (OPENJSON columns, subqueries, set operations, CASE/COALESCE, etc.) and addsCAST(... AS json)unless the document is provably nativejson.JSON_VALUE ... RETURNINGis likely affected too (needs confirmation). The generator usesRETURNINGwhenjsonScalarExpression.Json.TypeMapping?.StoreType is "json". According to the generator's own comment,RETURNINGis only supported overjson, notnvarchar(max). A scalar property navigated through a nestedAS JSONcolumn on Azure SQL at compat 170 (e.g.e.Details.Tags.Any(t => t.Child.SomeInt > 0)) probably producesJSON_VALUE([t].[Child], '$.SomeInt' RETURNING int)overnvarchar(max).- The rule in
SqlServerJsonPostprocessorthat casts json comparisons tonvarchar(max)also applies to these columns, which is unnecessary because they're already text.
Proposal
Give AS JSON columns a mapping that matches what the server actually returns: nvarchar(max) wherever the generator emits nvarchar(max). The correct store type then flows through the rest of the pipeline:
- The
Contains→JSON_CONTAINSgate stops matching on these platforms and falls back toIN (SELECT value FROM OPENJSON(...)), as at compat 160. EF.Functions.JsonContainsonly needs a local rule at translation time: cast the document tojsonwhen its mapping isn'tjson.- Type mappings for CASE/COALESCE, subquery projections and set operations are inferred correctly, so no provenance analysis is needed.
- The generator stops using
RETURNINGover text. - The engine/compat special case in
GenerateColumnInfobecomes unnecessary. It is currently duplicated in #39715.
Once this lands, the JSON_CONTAINS handling added to SqlServerJsonPostprocessor in #39715 (including the _tables tracking and the ISqlServerSingletonOptions dependency) can be removed.
Open points
- Primitive collections: we need an
nvarchar(max)variant of the json primitive-collection mapping that keeps the element mapping, comparer and JSON reader/writer. Nested navigations can useSqlServerStructuralJsonTypeMapping.NvarcharMaxDefault. - Where the mapping is set: the column expressions come from the relational
CreateSelect, not fromColumnInfo, so this probably needs a provider hook there. - Mixed CASE/COALESCE: if one branch is native
jsonand another is text, the inferred mapping may still bejson. Decide whether to normalize, or keep a cast for this case. - Baselines: Azure SQL and compat < 170 baselines for nested JSON queries will change. The changes are expected corrections.
Related
- #39625 / #39706: JSON_CONTAINS search value type handling
- #39626 / #39715: JSON_CONTAINS over nested JSON documents (current workaround)
- Ngôn ngữ chính
- C#
- Star
- 14.8k
- Fork
- 3.5k
- Merge trung bình
- 3 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 182
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/efcore
-
customer-reported
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translateCó thể đã có người làm @cincuranet đã nhận hôm nay. Đang mởarea-groupby area-query
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
dotnet/efcore#39833 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
SQLite: Polygon.Length translates to GLength, which returns 0Có thể đã có người làm @cincuranet đã nhận hôm nay. Đang mởarea-query area-sqlite
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
dotnet/efcore#39826 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsCó thể đã có người làm @AndriySvyryd đã nhận 1 ngày trước. Đang mởarea-migrations
dotnet/efcore#39824 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Querying a TPC base type with a JSON complex property throwsCó thể đã có người làm @cincuranet đã nhận 1 ngày trước. Đang mởarea-query
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 22/100
dotnet/efcore#39814 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của dotnet/efcore
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
SubtitleEdit/subtitleedit#17247 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Restore test cleanup fails on Windows when restored files are read-onlyCó thể đã có người làm @JamBalaya56562 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
duplicati/duplicati#8046 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Bundles Documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
pyrevitlabs/pyRevit#4285 ·
Maintainer thường phản hồi trong vòng 1 ngày