efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Failures with "JSON data type cannot be used in OpenJson function"

Open AndriySvyryd opened this issue 1 year ago • 14 comments

Several tests, like AdHocJsonQuerySqlServerJsonTypeTest.Contains_on_nested_collection_with_init_only_navigation and ComplexJsonCollectionSqlServerTest.Count are failing with

JSON data type cannot be used in OpenJson function.

SELECT [r].[Id], [r].[Name], [r].[OptionalRelated], [r].[RelatedCollection], [r].[RequiredRelated]
FROM [RootEntity] AS [r]
WHERE (
    SELECT COUNT(*)
    FROM OPENJSON([r].[RelatedCollection], '$') AS [r0]) = 2

AndriySvyryd avatar Aug 21 '25 21:08 AndriySvyryd

JSON data type cannot be used in OpenJson function

This error means that the JSON data type is still being in use with Azure SQL; #36596 disabled that, but it seems like we still don't flow forward commits from release/10.0-rc1 to release/10.0 and to main (I remember us mentioning that this isn't in place recently). I verified that when running e.g. ComplexJsonCollectionSqlServerTest.Count on release/10.0-rc1 (as opposed to release/10.0), the test passes (the JSON data type doesn't get used).

Do we know how to set up the flow or who to contact? Or should I manually cherry-pick this commit to the forward branches?

roji avatar Aug 23 '25 19:08 roji

One additional note: #36596 did leave out updating [SqlServerCondition(SqlServerCondition.SupportsJsonType)] to not run JSON type tests on Azure SQL, so tests in AdHocJsonQuerySqlServerJsonTypeTest (and possibly other test suites) do fail when running rc.1 against Azure SQL. After correcting this all query tests pass against SQL Azure (apart from various flakiness/availability failures).

I'll fix this in release/10.0, but as this is a temporary test-only issue (and we don't run CI against Azure SQL), it shouldn't be important (probably not worth patching release/10.0-rc1).

roji avatar Aug 24 '25 06:08 roji

Closing: #36739 reenabled the JSON data type against Azure SQL Database, now that it has full support. Ran the tests in AdHocJsonQuerySqlServerJsonTypeTest and AdHocJsonQuerySqlServerTest against Azure SQL and everything passes (apart from a couple out-of-date SQL baslines, fixing these in #36814).

roji avatar Sep 17 '25 11:09 roji

There appears to be a regression with this feature and Azure SQL. .UseAzureSql(...) uses the new json datatype as expected, but any EF query that uses OpenJSON fails with an Azure SQL error JSON data type cannot be used in OpenJson function. I have tried this on multiple Azure SQL instances with db compatibility level 170.

OpenJSON + json datatype works as expected in SQL 2025.

Happy to open a new issue if needed.

gpshonik avatar Nov 21 '25 19:11 gpshonik

@gpshonik What compatibility level is your database?

ErikEJ avatar Nov 21 '25 20:11 ErikEJ

170 in AzureSQL.

gpshonik avatar Nov 21 '25 20:11 gpshonik

@ErikEJ Not an EF query per se, but even this simple example throws in Azure SQL, works in SQL 2025.

DECLARE @JsonTest TABLE (
    Id int,
    Data json
);

INSERT INTO @JsonTest (Id, Data)
VALUES (1, N'{"name": "TestItem", "value": 100}');

SELECT 
    t.Id,
    j.name,
    j.value
FROM @JsonTest AS t
CROSS APPLY OPENJSON(t.Data)
    WITH (
        name nvarchar(50) '$.name',
        value int '$.value'
    ) AS j;

Azure SQL: Msg 13658, Level 16, State 2, Line 9 JSON data type cannot be used in OpenJson function.

edit: link to docs: https://learn.microsoft.com/en-us/sql/t-sql/data-types/json-data-type?view=azuresqldb-current&preserve-view=true#function-support

gpshonik avatar Nov 21 '25 21:11 gpshonik

@gpshonik I've just executed the SQL fragment above on an Azure SQL database and it worked fine... Assuming the compatibility level is set correctly as you've said, there may be some issue with that specific database; can you try creating a new Azure SQL server (just for testing) and try that out there (in the same region)? If the error persists, can you please open a support ticket with Azure SQL itself - as this is supposed to be supported?

(the note in the Azure SQL docs that you linked to refers to SQL Server 2025, where it was indeed unsupported until recently, AFAIK that comment is outdated and OPENJSON accepts the json type even on SQL Server 2025)

roji avatar Nov 22 '25 07:11 roji

@roji Thank you very much for confirming! We will open a support ticket with Azure. We are definitely hitting a region related issue.

Test Results

For the first test, I deployed a brand new Azure SQL instance to the same eastus region and was able to reproduce the exception immediately.

Image Image

For the second test, I deployed a brand new Azure SQL instance to a westus (same subscription) and was not able to reproduce the issue.

Image

gpshonik avatar Nov 22 '25 16:11 gpshonik

You can try SELECT @@VERSION and compare

ErikEJ avatar Nov 22 '25 17:11 ErikEJ

The versions of the working and non-working Azure SQL Instances are identical (screenshots above).

gpshonik avatar Nov 23 '25 17:11 gpshonik

@gpshonik then yeah, that definitely sounds like an Azure-side issue then.

roji avatar Nov 24 '25 11:11 roji

I opened a ticket and the Azure SQL Product Group said that the feature that enables JSON type support with OPENJSON is still rolling out to regions, East US should be completed by end of week. So if it's not working in another region, wait and try retesting next week.

techrazor avatar Nov 25 '25 20:11 techrazor

@techrazor thanks for the update, I got similar feedback internally. You can of course disable usage of the JSON type if you wish, or wait until support is rolled out.

roji avatar Nov 25 '25 20:11 roji