Skip to content

Commit

Permalink
Add missing check for predicate in primitive collection simplificatio…
Browse files Browse the repository at this point in the history
…ns (#33933)

Fixes #33932
  • Loading branch information
roji authored Jun 9, 2024
1 parent 61f1902 commit b84707f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
Subquery:
{
Tables: [SqlServerOpenJsonExpression { Arguments: [SqlParameterExpression parameter] } openJsonExpression],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Expand Down Expand Up @@ -466,6 +467,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
&& source.QueryExpression is SelectExpression
{
Tables: [SqlServerOpenJsonExpression { Arguments: [var jsonArrayColumn] } openJsonExpression],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ protected override QueryableMethodTranslatingExpressionVisitor CreateSubqueryVis
&& source.QueryExpression is SelectExpression
{
Tables: [TableValuedFunctionExpression { Name: "json_each", Schema: null, IsBuiltIn: true, Arguments: [var array] }],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Expand Down Expand Up @@ -196,6 +197,7 @@ protected override QueryableMethodTranslatingExpressionVisitor CreateSubqueryVis
&& source.QueryExpression is SelectExpression
{
Tables: [TableValuedFunctionExpression { Name: "json_each", Schema: null, IsBuiltIn: true, Arguments: [var array] }],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Expand Down Expand Up @@ -465,6 +467,7 @@ protected override ShapedQueryExpression TransformJsonQueryToTable(JsonQueryExpr
Name: "json_each", Schema: null, IsBuiltIn: true, Arguments: [var jsonArrayColumn]
} jsonEachExpression
],
Predicate: null,
GroupBy: [],
Having: null,
IsDistinct: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,40 @@ public override Task Column_collection_Length(bool async)
SELECT c
FROM root c
WHERE ((c["Discriminator"] = "PrimitiveCollectionsEntity") AND (ARRAY_LENGTH(c["Ints"]) = 2))
""");
});

public override Task Column_collection_Count_with_predicate(bool async)
=> CosmosTestHelpers.Instance.NoSyncTest(
async, async a =>
{
await base.Column_collection_Count_with_predicate(a);
AssertSql(
"""
SELECT c
FROM root c
WHERE ((c["Discriminator"] = "PrimitiveCollectionsEntity") AND ((
SELECT VALUE COUNT(1)
FROM i IN c["Ints"]
WHERE (i > 1)) = 2))
""");
});

public override Task Column_collection_Where_Count(bool async)
=> CosmosTestHelpers.Instance.NoSyncTest(
async, async a =>
{
await base.Column_collection_Where_Count(a);
AssertSql(
"""
SELECT c
FROM root c
WHERE ((c["Discriminator"] = "PrimitiveCollectionsEntity") AND ((
SELECT VALUE COUNT(1)
FROM i IN c["Ints"]
WHERE (i > 1)) = 2))
""");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,20 @@ public virtual Task Column_collection_Length(bool async)
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Length == 2));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Count_with_predicate(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Count(i => i > 1) == 2));

[ConditionalTheory] // #33932
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_Where_Count(bool async)
=> AssertQuery(
async,
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Where(i => i > 1).Count() == 2));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Column_collection_index_int(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ public override Task Column_collection_Count_method(bool async)
public override Task Column_collection_Length(bool async)
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Length(async));

public override Task Column_collection_Count_with_predicate(bool async)
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Count_with_predicate(async));

public override Task Column_collection_Where_Count(bool async)
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Where_Count(async));

public override Task Column_collection_index_int(bool async)
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_index_int(async));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,36 @@ FROM OPENJSON([p].[Ints]) AS [i]) = 2
""");
}

public override async Task Column_collection_Count_with_predicate(bool async)
{
await base.Column_collection_Count_with_predicate(async);

AssertSql(
"""
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE (
SELECT COUNT(*)
FROM OPENJSON([p].[Ints]) WITH ([value] int '$') AS [i]
WHERE [i].[value] > 1) = 2
""");
}

public override async Task Column_collection_Where_Count(bool async)
{
await base.Column_collection_Where_Count(async);

AssertSql(
"""
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
FROM [PrimitiveCollectionsEntity] AS [p]
WHERE (
SELECT COUNT(*)
FROM OPENJSON([p].[Ints]) WITH ([value] int '$') AS [i]
WHERE [i].[value] > 1) = 2
""");
}

public override async Task Column_collection_index_int(bool async)
{
await base.Column_collection_index_int(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,37 @@ WHERE json_array_length("p"."Ints") = 2
""");
}

public override async Task Column_collection_Count_with_predicate(bool async)
{
await base.Column_collection_Count_with_predicate(async);

AssertSql(
"""
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
FROM "PrimitiveCollectionsEntity" AS "p"
WHERE (
SELECT COUNT(*)
FROM json_each("p"."Ints") AS "i"
WHERE "i"."value" > 1) = 2
""");
}

// #33932
public override async Task Column_collection_Where_Count(bool async)
{
await base.Column_collection_Where_Count(async);

AssertSql(
"""
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
FROM "PrimitiveCollectionsEntity" AS "p"
WHERE (
SELECT COUNT(*)
FROM json_each("p"."Ints") AS "i"
WHERE "i"."value" > 1) = 2
""");
}

public override async Task Column_collection_index_int(bool async)
{
await base.Column_collection_index_int(async);
Expand Down

0 comments on commit b84707f

Please sign in to comment.