Skip to content

Commit 6b74cd9

Browse files
authored
CSHARP-4697: Expression not supported: -i.A (#1125)
1 parent ee0a266 commit 6b74cd9

File tree

4 files changed

+204
-2
lines changed

4 files changed

+204
-2
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/SerializationHelper.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Collections;
1718
using System.Linq.Expressions;
1819
using MongoDB.Bson;
@@ -24,6 +25,31 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
2425
{
2526
internal static class SerializationHelper
2627
{
28+
public static void EnsureRepresentationIsNumeric(Expression expression, IBsonSerializer serializer)
29+
{
30+
if (serializer is IRepresentationConfigurable representationConfigurableSerializer)
31+
{
32+
EnsureRepresentationIsNumeric(expression, serializer.ValueType, representationConfigurableSerializer.Representation);
33+
}
34+
35+
static void EnsureRepresentationIsNumeric(Expression expression, Type valueType, BsonType representation)
36+
{
37+
if (!IsNumericRepresentation(representation))
38+
{
39+
throw new ExpressionNotSupportedException(expression, because: $"serializer for type {valueType} uses a non-numeric representation: {representation}");
40+
}
41+
}
42+
43+
static bool IsNumericRepresentation(BsonType representation)
44+
{
45+
return representation switch
46+
{
47+
BsonType.Decimal128 or BsonType.Double or BsonType.Int32 or BsonType.Int64 => true,
48+
_ => false
49+
};
50+
}
51+
}
52+
2753
public static BsonValue SerializeValue(IBsonSerializer serializer, ConstantExpression constantExpression, Expression containingExpression)
2854
{
2955
var value = constantExpression.Value;

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/ExpressionToAggregationExpressionTranslator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using MongoDB.Bson.Serialization;
2020
using MongoDB.Bson.Serialization.Serializers;
2121
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
22-
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2322
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
2423

2524
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
@@ -71,6 +70,8 @@ public static AggregationExpression Translate(TranslationContext context, Expres
7170
return MemberExpressionToAggregationExpressionTranslator.Translate(context, (MemberExpression)expression);
7271
case ExpressionType.MemberInit:
7372
return MemberInitExpressionToAggregationExpressionTranslator.Translate(context, (MemberInitExpression)expression);
73+
case ExpressionType.Negate:
74+
return NegateExpressionToAggregationExpressionTranslator.Translate(context, (UnaryExpression)expression);
7475
case ExpressionType.New:
7576
return NewExpressionToAggregationExpressionTranslator.Translate(context, (NewExpression)expression);
7677
case ExpressionType.NewArrayInit:
@@ -123,7 +124,7 @@ public static AggregationExpression TranslateLambdaBody(
123124
var lambdaReturnType = lambdaExpression.ReturnType;
124125
var bodySerializer = translatedBody.Serializer;
125126
var bodyType = bodySerializer.ValueType;
126-
if (bodyType != lambdaReturnType)
127+
if (bodyType != lambdaReturnType)
127128
{
128129
if (lambdaReturnType.IsAssignableFrom(bodyType))
129130
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq.Expressions;
17+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
19+
20+
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
21+
{
22+
internal static class NegateExpressionToAggregationExpressionTranslator
23+
{
24+
public static AggregationExpression Translate(TranslationContext context, UnaryExpression expression)
25+
{
26+
if (expression.NodeType == ExpressionType.Negate)
27+
{
28+
var operandTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, expression.Operand);
29+
SerializationHelper.EnsureRepresentationIsNumeric(expression, operandTranslation.Serializer);
30+
31+
var ast = AstExpression.Subtract(0, operandTranslation.Ast);
32+
return new AggregationExpression(expression, ast, operandTranslation.Serializer);
33+
}
34+
35+
throw new ExpressionNotSupportedException(expression);
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using FluentAssertions;
18+
using MongoDB.Bson;
19+
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Driver.Linq;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators
24+
{
25+
public class NegateExpressionToAggregationExpressionTranslatorTests: Linq3IntegrationTest
26+
{
27+
[Fact]
28+
public void Negate_int_should_work()
29+
{
30+
var collection = CreateCollection();
31+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Int);
32+
33+
var stages = Translate(collection, queryable);
34+
AssertStages(
35+
stages,
36+
"{ $project : { _v : { $subtract : [0, '$Int'] }, _id : 0 } }");
37+
38+
var results = queryable.ToList();
39+
results.Should().Equal(10, -5, 0, int.MaxValue, -int.MaxValue);
40+
}
41+
42+
[Fact]
43+
public void Negate_long_should_work()
44+
{
45+
var collection = CreateCollection();
46+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Long);
47+
48+
var stages = Translate(collection, queryable);
49+
AssertStages(
50+
stages,
51+
"{ $project : { _v : { $subtract : [0, '$Long'] }, _id : 0 } }");
52+
53+
var results = queryable.ToList();
54+
results.Should().Equal(10L, -5L, 0L, long.MaxValue, -long.MaxValue);
55+
}
56+
57+
[Fact]
58+
public void Negate_single_should_work()
59+
{
60+
var collection = CreateCollection();
61+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Single);
62+
63+
var stages = Translate(collection, queryable);
64+
AssertStages(
65+
stages,
66+
"{ $project : { _v : { $subtract : [0, '$Single'] }, _id : 0 } }");
67+
68+
var results = queryable.ToList();
69+
results.Should().Equal(10F, -5F, 0F, float.MaxValue, -float.MaxValue);
70+
}
71+
72+
[Fact]
73+
public void Negate_double_should_work()
74+
{
75+
var collection = CreateCollection();
76+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.Double);
77+
78+
var stages = Translate(collection, queryable);
79+
AssertStages(
80+
stages,
81+
"{ $project : { _v : { $subtract : [0, '$Double'] }, _id : 0 } }");
82+
83+
var results = queryable.ToList();
84+
results.Should().Equal(10.0, -5.0, 0.0, double.MaxValue, -double.MaxValue);
85+
}
86+
87+
[Fact]
88+
public void Negate_decimal128_should_work()
89+
{
90+
var collection = CreateCollection();
91+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.DecimalAsDecimal128);
92+
93+
var stages = Translate(collection, queryable);
94+
AssertStages(
95+
stages,
96+
"{ $project : { _v : { $subtract : [0, '$DecimalAsDecimal128'] }, _id : 0 } }");
97+
98+
var results = queryable.ToList();
99+
results.Should().Equal(10.0m, -5.0m, 0.0m, decimal.MaxValue, -decimal.MaxValue);
100+
}
101+
102+
[Fact]
103+
public void Negate_decimal_as_string_should_throw()
104+
{
105+
var collection = CreateCollection();
106+
var queryable = Queryable.Select(collection.AsQueryable(), i => -i.DecimalAsString);
107+
108+
var exception = Record.Exception(() => Translate(collection, queryable));
109+
exception.Should().BeOfType<ExpressionNotSupportedException>();
110+
}
111+
112+
private IMongoCollection<Data> CreateCollection()
113+
{
114+
var collection = GetCollection<Data>("test");
115+
CreateCollection(
116+
collection,
117+
new Data { Id = 1, Int = -10, Double = -10, Single = -10, Long = -10, DecimalAsString = -10, DecimalAsDecimal128 = -10},
118+
new Data { Id = 2, Int = 5, Double = 5, Single = 5, Long = 5, DecimalAsString = 5, DecimalAsDecimal128 = 5},
119+
new Data { Id = 3, Int = 0, Double = 0, Single = 0, Long = 0, DecimalAsString = 0, DecimalAsDecimal128 = 0},
120+
new Data { Id = 4, Int = -int.MaxValue, Double = -double.MaxValue, Single = -float.MaxValue, Long = -long.MaxValue, DecimalAsString = -decimal.MaxValue, DecimalAsDecimal128 = -decimal.MaxValue},
121+
new Data { Id = 5, Int = int.MaxValue, Double = double.MaxValue, Single = float.MaxValue, Long = long.MaxValue, DecimalAsString = decimal.MaxValue, DecimalAsDecimal128 = decimal.MaxValue});
122+
return collection;
123+
}
124+
125+
private class Data
126+
{
127+
public int Id { get; set; }
128+
public int Int { get; set; }
129+
public long Long { get; set; }
130+
public float Single { get; set; }
131+
public double Double { get; set; }
132+
public decimal DecimalAsString { get; set; }
133+
[BsonRepresentation(BsonType.Decimal128)]
134+
public decimal DecimalAsDecimal128 { get; set; }
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)