Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query : Adds support for Lambda expression reuse in LINQ expressions #3826

Merged
2 commits merged into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Microsoft.Azure.Cosmos/src/Linq/TranslationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public void PopParameter()
{
ParameterExpression last = this.lambdaParametersStack[this.lambdaParametersStack.Count - 1];
this.lambdaParametersStack.RemoveAt(this.lambdaParametersStack.Count - 1);
this.substitutions.Remove(last);
}

/// <summary>
Expand Down Expand Up @@ -337,6 +338,11 @@ public Expression Lookup(ParameterExpression parameter)
return null;
}

internal void Remove(ParameterExpression parameter)
{
this.substitutionTable.Remove(parameter);
}

public const string InputParameterName = "root";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public void ExecuteTestSuite(IEnumerable<TInput> inputs, [CallerMemberName] stri
Please run the ..\azure-cosmos-dotnet-v3\UpdateContracts.ps1 script to update the baselines.
Expected: {baselineTextSuffix},
Actual: {outputTextSuffix},
OutputPath: {outputPath},
BaselinePath: {baselinePath}");
OutputPath: {Path.GetFullPath(outputPath)},
BaselinePath: {Path.GetFullPath(baselinePath)}");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Results>
<Result>
<Input>
<Description><![CDATA[Where -> Where with same predicate instance]]></Description>
<Expression><![CDATA[query.Where(DisplayClass.predicate).Where(DisplayClass.predicate)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE root
FROM root
WHERE ((root["Int"] = 5) AND (root["Int"] = 5))]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Where -> Select with same predicate instance]]></Description>
<Expression><![CDATA[query.Where(DisplayClass.predicate).Select(DisplayClass.predicate)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE (root["Int"] = 5)
FROM root
WHERE (root["Int"] = 5)]]></SqlQuery>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Where -> Select -> Where with same predicate instance]]></Description>
<Expression><![CDATA[query.Where(DisplayClass.predicate2).Select(c => new AnonymousType(Int = 10, Result = c)).Where(DisplayClass.predicate2)]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[
SELECT VALUE {"Int": 10, "Result": root}
FROM root
WHERE ((ToString(root) = "a") AND (ToString({"Int": 10, "Result": root}) = "a"))]]></SqlQuery>
</Output>
</Result>
</Results>
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,25 @@ orderby f.FamilyId descending
this.ExecuteTestSuite(inputs);
}

[TestMethod]
public void TestLambdaReuse()
{
List<LinqTestInput> inputs = new List<LinqTestInput>();

System.Linq.Expressions.Expression<Func<Family, bool>> predicate = f => f.Int == 5;
inputs.Add(new LinqTestInput("Where -> Where with same predicate instance", b => getQuery(b).Where(predicate).Where(predicate)));
inputs.Add(new LinqTestInput("Where -> Select with same predicate instance", b => getQuery(b).Where(predicate).Select(predicate)));

System.Linq.Expressions.Expression<Func<object, bool>> predicate2 = f => f.ToString() == "a";
inputs.Add(new LinqTestInput("Where -> Select -> Where with same predicate instance",
b => getQuery(b)
.Where(predicate2)
.Select(c => new { Int = 10, Result = c })
.Where(predicate2)));

this.ExecuteTestSuite(inputs);
}

[TestMethod]
public void TestThenByTranslation()
{
Expand Down Expand Up @@ -1922,7 +1941,7 @@ public async Task ValidateLinqQueries()
Gender = "female",
Grade = 1,
Pets = new List<Pet>() { pet, new Pet() { GivenName = "koko" } },
Things = new Dictionary<string, string>() { { "A", "B" }, { "C", "D" } },
Things = new Dictionary<string, string>() { { "A", "B" }, { "C", "D" } }
};

Address address = new Address { State = "NY", County = "Manhattan", City = "NY" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<None Remove="BaselineTest\TestBaseline\EndToEndTraceWriterBaselineTests.StreamPointOperationsAsync.xml" />
<None Remove="BaselineTest\TestBaseline\EndToEndTraceWriterBaselineTests.TypedPointOperationsAsync.xml" />
<None Remove="BaselineTest\TestBaseline\IndexMetricsParserBaselineTest.IndexUtilizationParse.xml" />
<None Remove="BaselineTest\TestBaseline\LinqGeneralBaselineTests.TestLambdaReuse.xml" />
<None Remove="BaselineTest\TestBaseline\LinqTranslationBaselineTests.TestDateTimeJsonConverterTimezones.xml" />
<None Remove="BaselineTest\TestBaseline\LinqTranslationBaselineTests.TestMemberAccessWithNullableTypes.xml" />
</ItemGroup>
Expand Down Expand Up @@ -283,6 +284,11 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Azure.Cosmos.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="BaselineTest\TestBaseline\LinqGeneralBaselineTests.TestLambdaReuse.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Update="Tracing\EndToEndTraceWriterBaselineTests.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down