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

CSHARP-5527: Support $sigmoid expression #1638

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 src/MongoDB.Driver/Core/Misc/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class Feature
private static readonly Feature __setWindowFields = new Feature("SetWindowFields", WireVersion.Server50);
private static readonly Feature __setWindowFieldsLocf = new Feature("SetWindowFieldsLocf", WireVersion.Server52);
private static readonly Feature __shardedTransactions = new Feature("ShardedTransactions", WireVersion.Server42);
private static readonly Feature __sigmoidOperator = new Feature("SigmoidOperator", WireVersion.Server81);
private static readonly Feature __snapshotReads = new Feature("SnapshotReads", WireVersion.Server50, notSupportedMessage: "Snapshot reads require MongoDB 5.0 or later");
private static readonly Feature __sortArrayOperator = new Feature("SortArrayOperator", WireVersion.Server52);
private static readonly Feature __speculativeAuthentication = new Feature("SpeculativeAuthentication", WireVersion.Server44);
Expand Down Expand Up @@ -430,6 +431,11 @@ public class Feature
/// Gets the sharded transactions feature.
/// </summary>
public static Feature ShardedTransactions => __shardedTransactions;

/// <summary>
/// Gets the $sigmoid operator feature.
/// </summary>
public static Feature SigmoidOperator => __sigmoidOperator;

/// <summary>
/// Gets the snapshot reads feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal enum AstUnaryOperator
Round,
SetIntersection,
SetUnion,
Sigmoid,
Sin,
Sinh,
Size,
Expand Down Expand Up @@ -161,6 +162,7 @@ public static string Render(this AstUnaryOperator @operator)
AstUnaryOperator.Round => "$round",
AstUnaryOperator.SetIntersection => "$setIntersection",
AstUnaryOperator.SetUnion => "$setUnion",
AstUnaryOperator.Sigmoid => "$sigmoid",
AstUnaryOperator.Sin => "$sin",
AstUnaryOperator.Sinh => "$sinh",
AstUnaryOperator.Size => "$size",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ internal static class MongoDBMathMethod
// private static fields
private static readonly MethodInfo __degreesToRadians;
private static readonly MethodInfo __radiansToDegrees;
private static readonly MethodInfo __sigmoid;

// static constructor
static MongoDBMathMethod()
{
__degreesToRadians = ReflectionInfo.Method((double degrees) => MongoDBMath.DegreesToRadians(degrees));
__radiansToDegrees = ReflectionInfo.Method((double radians) => MongoDBMath.RadiansToDegrees(radians));
__sigmoid = ReflectionInfo.Method((double value) => MongoDBMath.Sigmoid(value));
}

// public properties
public static MethodInfo DegreesToRadians => __degreesToRadians;
public static MethodInfo RadiansToDegrees => __radiansToDegrees;
public static MethodInfo Sigmoid => __sigmoid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
case "SequenceEqual": return SequenceEqualMethodToAggregationExpressionTranslator.Translate(context, expression);
case "SetEquals": return SetEqualsMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Shift": return ShiftMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Sigmoid": return SigmoidMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Split": return SplitMethodToAggregationExpressionTranslator.Translate(context, expression);
case "Sqrt": return SqrtMethodToAggregationExpressionTranslator.Translate(context, expression);
case "StrLenBytes": return StrLenBytesMethodToAggregationExpressionTranslator.Translate(context, expression);
Expand Down
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(MongoDBMathMethod.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);
}
}
}
10 changes: 10 additions & 0 deletions src/MongoDB.Driver/Linq/MongoDBMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ public static double RadiansToDegrees(double radians)
{
return radians / (Math.PI / 180.0);
}

/// <summary>
/// Transforms a real-valued input into a value between 0 and 1.
/// </summary>
/// <param name="value">The input value.</param>
/// <returns>The transformed value.</returns>
public static double Sigmoid(double value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is a perfectly reasonable place to declare this method we should discuss a bit whether going forward we want all new methods like this to live in the Mql class.

{
return 1.0 / (1.0 + Math.Exp(-value));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* 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.Collections.Generic;
using System.Linq;
using FluentAssertions;
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 => MongoDBMath.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});
}

public class C
{
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 },
];
}
}
}