-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5527: Add support for $sigmoid expression in LINQ #1638
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
5 commits
Select commit
Hold shift + click to select a range
92ebf60
CSHARP-5527: Support $sigmoid expression
adelinowona ac9b5e2
address pr review
adelinowona bac7ac6
move sigmoid to Mql class
adelinowona 468ad8d
remove extra spaces
adelinowona 2d56a5d
add tests for non-numeric representation
adelinowona 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
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
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
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
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
47 changes: 47 additions & 0 deletions
47
...ExpressionTranslators/MethodTranslators/SigmoidMethodToAggregationExpressionTranslator.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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System.Linq; | ||
| using System.Linq.Expressions; | ||
| using MongoDB.Bson.Serialization.Serializers; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
| using MongoDB.Driver.Linq.Linq3Implementation.Reflection; | ||
|
|
||
| namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
| { | ||
| internal static class SigmoidMethodToAggregationExpressionTranslator | ||
| { | ||
| public static TranslatedExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
| { | ||
| var method = expression.Method; | ||
| var arguments = expression.Arguments; | ||
|
|
||
| if (method.Is(MqlMethod.Sigmoid)) | ||
| { | ||
| var valueExpression = arguments.Single(); | ||
| var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); | ||
| SerializationHelper.EnsureRepresentationIsNumeric(expression, valueExpression, valueTranslation); | ||
|
|
||
| return new TranslatedExpression( | ||
| expression, | ||
| AstExpression.Unary(AstUnaryOperator.Sigmoid, valueTranslation.Ast), | ||
| DoubleSerializer.Instance); | ||
| } | ||
|
|
||
| throw new ExpressionNotSupportedException(expression); | ||
| } | ||
| } | ||
| } |
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
88 changes: 88 additions & 0 deletions
88
...ssionTranslators/MethodTranslators/SigmoidMethodToAggregationExpressionTranslatorTests.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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* Copyright 2010-present MongoDB Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using FluentAssertions; | ||
| using MongoDB.Bson; | ||
| using MongoDB.Bson.Serialization.Attributes; | ||
| using MongoDB.Driver.Core.Misc; | ||
| using MongoDB.Driver.Linq; | ||
| using MongoDB.Driver.TestHelpers; | ||
| using Xunit; | ||
|
|
||
| namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
| { | ||
| public class SigmoidMethodToAggregationExpressionTranslatorTests : LinqIntegrationTest<SigmoidMethodToAggregationExpressionTranslatorTests.ClassFixture> | ||
| { | ||
| public SigmoidMethodToAggregationExpressionTranslatorTests(ClassFixture fixture) | ||
| : base(fixture, server => server.Supports(Feature.SigmoidOperator)) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Sigmoid_should_work() | ||
| { | ||
| var collection = Fixture.Collection; | ||
|
|
||
| var queryable = collection | ||
| .AsQueryable() | ||
| .Select(x => Mql.Sigmoid(x.X)); | ||
|
|
||
| var stages = Translate(collection, queryable); | ||
| AssertStages(stages, "{ $project : { _v : { $sigmoid : '$X' }, _id : 0 } }"); | ||
|
|
||
| var result = queryable.ToList(); | ||
| result.Should().BeEquivalentTo(new[] { 0.7310585786300049, 0.9933071490757153, 0.999997739675702, 0.9999999992417439}); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Sigmoid_with_non_numeric_representation_should_throw() | ||
| { | ||
| var exception = Record.Exception(() => | ||
| { | ||
| var collection = Fixture.Collection; | ||
|
|
||
| var queryable = collection | ||
| .AsQueryable() | ||
| .Select(x => Mql.Sigmoid(x.Y)); | ||
|
|
||
| Translate(collection, queryable); | ||
| }); | ||
|
|
||
| exception.Should().BeOfType<ExpressionNotSupportedException>(); | ||
| exception?.Message.Should().Contain("uses a non-numeric representation"); | ||
| } | ||
|
|
||
| public class C | ||
| { | ||
| [BsonRepresentation(BsonType.String)] | ||
| public double Y { get; set; } | ||
| public double X { get; set; } | ||
| } | ||
|
|
||
| public sealed class ClassFixture : MongoCollectionFixture<C> | ||
| { | ||
| protected override IEnumerable<C> InitialData => | ||
| [ | ||
| new() { X = 1.0 }, | ||
| new() { X = 5.0 }, | ||
| new() { X = 13.0 }, | ||
| new() { X = 21.0 }, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: I would suggest test to check failure in case non-numeric representation.