SQL Server: OPENJSON WITH ... AS JSON columns carry a json type mapping even when the server returns nvarchar(max)

Đang mở
#39,719 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp, sql
Lĩnh vực
backend, database

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ả

area-query area-sqlserver

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:

  • TransformJsonQueryToTable assigns element.StoreTypeMapping (primitive collections) and jsonQueryExpression.JsonColumn.TypeMapping (nested owned/complex navigations) to the AS JSON column infos.
  • The column expressions that refer to them, created via the relational CreateSelect from the model's mappings, inherit the same json mapping.

Everything downstream that decides based on the store type is therefore misled.

Known consequences

  1. JSON_CONTAINS over nested collections fails with error 8116 (#39626). The Contains translation gate (Json: { TypeMapping: SqlServerJsonTypeMapping }) accepts the column, and EF.Functions.JsonContains passes it through as is.
    #39715 works around this in SqlServerJsonPostprocessor. It works out where the document came from (OPENJSON columns, subqueries, set operations, CASE/COALESCE, etc.) and adds CAST(... AS json) unless the document is provably native json.
  2. JSON_VALUE ... RETURNING is likely affected too (needs confirmation). The generator uses RETURNING when jsonScalarExpression.Json.TypeMapping?.StoreType is "json". According to the generator's own comment, RETURNING is only supported over json, not nvarchar(max). A scalar property navigated through a nested AS JSON column on Azure SQL at compat 170 (e.g. e.Details.Tags.Any(t => t.Child.SomeInt > 0)) probably produces JSON_VALUE([t].[Child], '$.SomeInt' RETURNING int) over nvarchar(max).
  3. The rule in SqlServerJsonPostprocessor that casts json comparisons to nvarchar(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_CONTAINS gate stops matching on these platforms and falls back to IN (SELECT value FROM OPENJSON(...)), as at compat 160.
  • EF.Functions.JsonContains only needs a local rule at translation time: cast the document to json when its mapping isn't json.
  • Type mappings for CASE/COALESCE, subquery projections and set operations are inferred correctly, so no provenance analysis is needed.
  • The generator stops using RETURNING over text.
  • The engine/compat special case in GenerateColumnInfo becomes 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 use SqlServerStructuralJsonTypeMapping.NvarcharMaxDefault.
  • Where the mapping is set: the column expressions come from the relational CreateSelect, not from ColumnInfo, so this probably needs a provider hook there.
  • Mixed CASE/COALESCE: if one branch is native json and another is text, the inferred mapping may still be json. 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

Mở trong Codespaces

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.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/efcore

Tất cả issue của dotnet/efcore

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.