Skip to content

Commit

Permalink
Addressed a couple of TODOs.
Browse files Browse the repository at this point in the history
  • Loading branch information
adityasa committed Jan 27, 2024
1 parent f9bb592 commit 510034c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 1 addition & 3 deletions Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,6 @@ private static Collection VisitMethodCall(MethodCallExpression inputExpression,
{
if (inputExpression.Arguments.Count == 1)
{
// ISSUE-TODO-adityasa-2024/1/26 - Disallow other overloads of FirstOrDefault.
SqlNumberLiteral sqlNumberLiteral;
bool success = TryGetSqlNumberLiteral(1, out sqlNumberLiteral);
Debug.Assert(success, "ExpressionToSQL Assert!", "SqlNumberLiteral Construction must succeed!");
Expand All @@ -1281,8 +1280,7 @@ private static Collection VisitMethodCall(MethodCallExpression inputExpression,
}
else
{
// ISSUE-TODO-adityasa-2024/1/26 - Improve error message.
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, inputExpression.Method.Name));
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.InvalidArgumentsCount, inputExpression.Method.Name, 0, inputExpression.Arguments.Count - 1));
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,23 @@ WHERE (root["Flag"] AND (NOT root["Flag"]))]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[FirstOrDefault with explicit default]]></Description>
<Description><![CDATA[FirstOrDefault with explicit (inline) default]]></Description>
<Expression><![CDATA[query.FirstOrDefault(new Data())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[]]></SqlQuery>
<ErrorMessage><![CDATA[Method 'FirstOrDefault' is not supported.]]></ErrorMessage>
<ErrorMessage><![CDATA[Incorrect number of arguments for method 'FirstOrDefault'. Expected '0' but received '1'.]]></ErrorMessage>
<SerializedResults><![CDATA[null]]></SerializedResults>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[FirstOrDefault with explicit default from function invocation]]></Description>
<Expression><![CDATA[query.FirstOrDefault(value(Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests.LinqScalarFunctionBaselineTests).GetDefaultData())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[]]></SqlQuery>
<ErrorMessage><![CDATA[Incorrect number of arguments for method 'FirstOrDefault'. Expected '0' but received '1'.]]></ErrorMessage>
<SerializedResults><![CDATA[null]]></SerializedResults>
</Output>
</Result>
Expand All @@ -143,24 +154,35 @@ WHERE (root["Flag"] AND (NOT root["Flag"]))]]></SqlQuery>
</Input>
<Output>
<SqlQuery><![CDATA[]]></SqlQuery>
<ErrorMessage><![CDATA[Method 'FirstOrDefault' is not supported.]]></ErrorMessage>
<ErrorMessage><![CDATA[Incorrect number of arguments for method 'FirstOrDefault'. Expected '0' but received '1'.]]></ErrorMessage>
<SerializedResults><![CDATA[null]]></SerializedResults>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[FirstOrDefault with explicit default and predicate]]></Description>
<Description><![CDATA[FirstOrDefault with explicit (inline) default and predicate]]></Description>
<Expression><![CDATA[query.FirstOrDefault(_ => True, new Data())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[]]></SqlQuery>
<ErrorMessage><![CDATA[Method 'FirstOrDefault' is not supported.]]></ErrorMessage>
<ErrorMessage><![CDATA[Incorrect number of arguments for method 'FirstOrDefault'. Expected '0' but received '2'.]]></ErrorMessage>
<SerializedResults><![CDATA[null]]></SerializedResults>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[FirstOrDefault with explicit default from function invocation and predicate]]></Description>
<Expression><![CDATA[query.FirstOrDefault(_ => True, value(Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests.LinqScalarFunctionBaselineTests).GetDefaultData())]]></Expression>
</Input>
<Output>
<SqlQuery><![CDATA[]]></SqlQuery>
<ErrorMessage><![CDATA[Incorrect number of arguments for method 'FirstOrDefault'. Expected '0' but received '2'.]]></ErrorMessage>
<SerializedResults><![CDATA[null]]></SerializedResults>
</Output>
</Result>
<Result>
<Input>
<Description><![CDATA[Select (FirstOrDefault) -> Min 1]]></Description>
<Description><![CDATA[Nested FirstOrDefault 1]]></Description>
<Expression><![CDATA[query.Select(data => data.Multiples.FirstOrDefault()).Min(), Object)]]></Expression>
</Input>
<Output>
Expand All @@ -171,7 +193,7 @@ WHERE (root["Flag"] AND (NOT root["Flag"]))]]></SqlQuery>
</Result>
<Result>
<Input>
<Description><![CDATA[Select (FirstOrDefault) -> Min 2]]></Description>
<Description><![CDATA[Nested FirstOrDefault 2]]></Description>
<Expression><![CDATA[query.Select(data => new List`1() {Void Add(Int32)(1), Void Add(Int32)(2), Void Add(Int32)(3)}.FirstOrDefault()).Min(), Object)]]></Expression>
</Input>
<Output>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,21 @@ public void TestFirstOrDefault()
/////////////////

// ISSUE-TODO-adityasa-2024/1/26 - Support FirstOrDefault overloads.
// Please note, this requires potential support for user code invocation in context of rest of the client code (except maybe some simple cases).
// We do not currently do this for any other scenarios.

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"FirstOrDefault with explicit default",
"FirstOrDefault with explicit (inline) default",
b => getQuery(b)
.FirstOrDefault(new Data())));

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"FirstOrDefault with explicit default from function invocation",
b => getQuery(b)
.FirstOrDefault(this.GetDefaultData())));

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"FirstOrDefault with predicate",
Expand All @@ -141,20 +149,26 @@ public void TestFirstOrDefault()

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"FirstOrDefault with explicit default and predicate",
"FirstOrDefault with explicit (inline) default and predicate",
b => getQuery(b)
.FirstOrDefault(_ => true, new Data())));

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"Select (FirstOrDefault) -> Min 1",
"FirstOrDefault with explicit default from function invocation and predicate",
b => getQuery(b)
.FirstOrDefault(_ => true, this.GetDefaultData())));

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"Nested FirstOrDefault 1",
b => getQuery(b)
.Select(data => data.Multiples.FirstOrDefault())
.Min()));

// Unsupported
inputs.Add(new LinqScalarFunctionInput(
"Select (FirstOrDefault) -> Min 2",
"Nested FirstOrDefault 2",
b => getQuery(b)
.Select(data => new List<int> { 1, 2, 3 }.FirstOrDefault())
.Min()));
Expand All @@ -178,6 +192,11 @@ public void TestFirstOrDefault()
this.ExecuteTestSuite(inputs);
}

private Data GetDefaultData()
{
return new Data();
}

public override LinqScalarFunctionOutput ExecuteTest(LinqScalarFunctionInput input)
{
lastExecutedScalarQuery = null;
Expand Down

0 comments on commit 510034c

Please sign in to comment.