-
Notifications
You must be signed in to change notification settings - Fork 524
Query: Adds LINQ extension method for ORDER BY RANK, FullTextScore and RRF #4993
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
739fd59
Function signature and basic test
3297114
Add construct for OrderByRank to the DOM
5a75674
update test
bed61fa
g4 file
00bceb1
base grammar change
5d9e380
update dom
1623b15
update baseline
387490e
merge with latest master
6f78411
merge with SQL DOM pr
8759593
merge w master
781c102
update with DOM from master
420caef
update baseline
09fe0f2
Merge branch 'master' into users/leminh/FTRankLINQ
leminh98 8fd19de
update RRF interface
bfb5912
Changes to unit test
8c3c0e7
fix composition bug
5136b6c
Merge branch 'master' into users/leminh/FTRankLINQ
22fc735
fix typo
f8c7760
Update with latest master and new test suite
2b081f9
update ftscore to use params
d68fc4d
update line ending
679844e
Merge branch 'master' into users/leminh/FTRankLINQ
leminh98 69ebfae
Update error message for OrderByRank
687f0f5
Merge branch 'users/leminh/FTRankLINQ' of https://github.com/Azure/az…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
243 changes: 123 additions & 120 deletions
243
Microsoft.Azure.Cosmos/src/Linq/BuiltinFunctions/BuiltinFunctionVisitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,121 +1,124 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Linq | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq.Expressions; | ||
| using Microsoft.Azure.Cosmos; | ||
| using Microsoft.Azure.Cosmos.Spatial; | ||
| using Microsoft.Azure.Cosmos.SqlObjects; | ||
| using Microsoft.Azure.Documents; | ||
|
|
||
| internal abstract class BuiltinFunctionVisitor | ||
| { | ||
| public SqlScalarExpression Visit(MethodCallExpression methodCallExpression, TranslationContext context) | ||
| { | ||
| SqlScalarExpression result = this.VisitExplicit(methodCallExpression, context); | ||
| if (result != null) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| result = this.VisitImplicit(methodCallExpression, context); | ||
| if (result != null) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name)); | ||
| } | ||
|
|
||
| public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context) | ||
| { | ||
| Type declaringType; | ||
|
|
||
| // Method could be an extension method | ||
| if (methodCallExpression.Method.IsStatic && methodCallExpression.Method.IsExtensionMethod()) | ||
| { | ||
| if (methodCallExpression.Arguments.Count < 1) | ||
| { | ||
| // Extension methods should has at least 1 argument, this should never happen | ||
| // Throwing ArgumentException instead of assert | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| declaringType = methodCallExpression.Arguments[0].Type; | ||
|
|
||
| if (methodCallExpression.Method.DeclaringType.GeUnderlyingSystemType() == typeof(CosmosLinqExtensions)) | ||
| { | ||
| // CosmosLinq Extensions can be RegexMatch, DocumentId or Type check functions (IsString, IsBool, etc.) | ||
| if ((methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.RegexMatch)) || | ||
| (methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContains)) || | ||
| (methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContainsAll)) || | ||
| (methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.FullTextContainsAny))) | ||
| { | ||
| return StringBuiltinFunctions.Visit(methodCallExpression, context); | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Linq | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq.Expressions; | ||
| using Microsoft.Azure.Cosmos; | ||
| using Microsoft.Azure.Cosmos.Spatial; | ||
| using Microsoft.Azure.Cosmos.SqlObjects; | ||
| using Microsoft.Azure.Documents; | ||
|
|
||
| internal abstract class BuiltinFunctionVisitor | ||
| { | ||
| public SqlScalarExpression Visit(MethodCallExpression methodCallExpression, TranslationContext context) | ||
| { | ||
| SqlScalarExpression result = this.VisitExplicit(methodCallExpression, context); | ||
| if (result != null) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| result = this.VisitImplicit(methodCallExpression, context); | ||
| if (result != null) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name)); | ||
| } | ||
|
|
||
| public static SqlScalarExpression VisitBuiltinFunctionCall(MethodCallExpression methodCallExpression, TranslationContext context) | ||
| { | ||
| Type declaringType; | ||
| bool isExtensionMethod = methodCallExpression.Method.IsExtensionMethod(); | ||
| // Method could be an extension method | ||
| // RRF doesn't have "this" qualifier, so it's not considered an extension method by the compiler, and so needed to be checked separately | ||
| if (methodCallExpression.Method.IsStatic && | ||
| (methodCallExpression.Method.IsExtensionMethod() | ||
| || methodCallExpression.Method.Name.Equals(nameof(CosmosLinqExtensions.RRF)))) | ||
| { | ||
| if (methodCallExpression.Arguments.Count < 1) | ||
| { | ||
| // Extension methods should has at least 1 argument, this should never happen | ||
| // Throwing ArgumentException instead of assert | ||
| throw new ArgumentException(); | ||
| } | ||
|
|
||
| declaringType = methodCallExpression.Arguments[0].Type; | ||
|
|
||
| if (methodCallExpression.Method.DeclaringType.GeUnderlyingSystemType() == typeof(CosmosLinqExtensions)) | ||
| { | ||
| // CosmosLinq Extensions can be RegexMatch, DocumentId or Type check functions (IsString, IsBool, etc.) | ||
| switch (methodCallExpression.Method.Name) | ||
| { | ||
| case nameof(CosmosLinqExtensions.RegexMatch): | ||
| case nameof(CosmosLinqExtensions.FullTextContains): | ||
| case nameof(CosmosLinqExtensions.FullTextContainsAll): | ||
| case nameof(CosmosLinqExtensions.FullTextContainsAny): | ||
| return StringBuiltinFunctions.Visit(methodCallExpression, context); | ||
| case nameof(CosmosLinqExtensions.DocumentId): | ||
| case nameof(CosmosLinqExtensions.RRF): | ||
| case nameof(CosmosLinqExtensions.FullTextScore): | ||
| return OtherBuiltinSystemFunctions.Visit(methodCallExpression, context); | ||
| default: | ||
| return TypeCheckFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| if (methodCallExpression.Method.Name == nameof(CosmosLinqExtensions.DocumentId)) | ||
| { | ||
| return OtherBuiltinSystemFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| return TypeCheckFunctions.Visit(methodCallExpression, context); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| declaringType = methodCallExpression.Method.DeclaringType; | ||
| } | ||
|
|
||
| // Check order matters, some extension methods work for both strings and arrays | ||
|
|
||
| // Math functions | ||
| if (declaringType == typeof(Math)) | ||
| { | ||
| return MathBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // ToString with String and Guid only becomes passthrough | ||
| if (methodCallExpression.Method.Name == "ToString" && | ||
| methodCallExpression.Arguments.Count == 0 && | ||
| methodCallExpression.Object != null && | ||
| ((methodCallExpression.Object.Type == typeof(string)) || | ||
| (methodCallExpression.Object.Type == typeof(Guid)))) | ||
| { | ||
| return ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context); | ||
| } | ||
|
|
||
| // String functions or ToString with Objects that are not strings and guids | ||
| if ((declaringType == typeof(string)) || | ||
| (methodCallExpression.Method.Name == "ToString" && | ||
| methodCallExpression.Arguments.Count == 0 && | ||
| methodCallExpression.Object != null)) | ||
| { | ||
| return StringBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // Array functions | ||
| if (declaringType.IsEnumerable()) | ||
| { | ||
| return ArrayBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // Spatial functions | ||
| if (typeof(Geometry).IsAssignableFrom(declaringType)) | ||
| { | ||
| return SpatialBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name)); | ||
| } | ||
|
|
||
| protected abstract SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context); | ||
|
|
||
| protected abstract SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| declaringType = methodCallExpression.Method.DeclaringType; | ||
| } | ||
|
|
||
| // Check order matters, some extension methods work for both strings and arrays | ||
|
|
||
| // Math functions | ||
| if (declaringType == typeof(Math)) | ||
| { | ||
| return MathBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // ToString with String and Guid only becomes passthrough | ||
| if (methodCallExpression.Method.Name == "ToString" && | ||
| methodCallExpression.Arguments.Count == 0 && | ||
| methodCallExpression.Object != null && | ||
| ((methodCallExpression.Object.Type == typeof(string)) || | ||
| (methodCallExpression.Object.Type == typeof(Guid)))) | ||
| { | ||
| return ExpressionToSql.VisitNonSubqueryScalarExpression(methodCallExpression.Object, context); | ||
| } | ||
|
|
||
| // String functions or ToString with Objects that are not strings and guids | ||
| if ((declaringType == typeof(string)) || | ||
| (methodCallExpression.Method.Name == "ToString" && | ||
| methodCallExpression.Arguments.Count == 0 && | ||
| methodCallExpression.Object != null)) | ||
| { | ||
| return StringBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // Array functions | ||
| if (declaringType.IsEnumerable()) | ||
| { | ||
| return ArrayBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| // Spatial functions | ||
| if (typeof(Geometry).IsAssignableFrom(declaringType)) | ||
| { | ||
| return SpatialBuiltinFunctions.Visit(methodCallExpression, context); | ||
| } | ||
|
|
||
| throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name)); | ||
| } | ||
|
|
||
| protected abstract SqlScalarExpression VisitExplicit(MethodCallExpression methodCallExpression, TranslationContext context); | ||
|
|
||
| protected abstract SqlScalarExpression VisitImplicit(MethodCallExpression methodCallExpression, TranslationContext context); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.