Skip to content

Commit 3ba8dc3

Browse files
sanych-sunrstam
authored andcommitted
CSHARP-4622: Process constant expression to a filter query (#1074)
1 parent 9d87871 commit 3ba8dc3

File tree

5 files changed

+151
-15
lines changed

5 files changed

+151
-15
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Optimizers/AstSimplifier.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,6 @@ static AstExpression UltimateGetFieldInput(AstGetFieldExpression getField)
307307
}
308308
}
309309

310-
public override AstNode VisitMatchStage(AstMatchStage node)
311-
{
312-
node = (AstMatchStage)base.VisitMatchStage(node);
313-
314-
if (node.Filter is AstExprFilter exprFilter &&
315-
exprFilter.Expression is AstConstantExpression constantExpression &&
316-
constantExpression.Value is BsonBoolean booleanValue)
317-
{
318-
var simpleFilter = booleanValue.Value ? AstFilter.MatchesEverything() : AstFilter.MatchesNothing();
319-
return AstStage.Match(simpleFilter);
320-
}
321-
322-
return node;
323-
}
324-
325310
public override AstNode VisitProjectStageSetFieldSpecification(AstProjectStageSetFieldSpecification node)
326311
{
327312
node = (AstProjectStageSetFieldSpecification)base.VisitProjectStageSetFieldSpecification(node);

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionToFilterTranslator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ private static AstFilter TranslateUsingQueryOperators(TranslationContext context
110110

111111
case ExpressionType.TypeIs:
112112
return TypeIsExpressionToFilterTranslator.Translate(context, (TypeBinaryExpression)expression);
113+
114+
case ExpressionType.Constant:
115+
return ConstantExpressionToFilterTranslator.Translate(context, (ConstantExpression)expression);
113116
}
114117

115118
if (expression.Type == typeof(bool))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Filters;
18+
19+
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators
20+
{
21+
internal static class ConstantExpressionToFilterTranslator
22+
{
23+
public static AstFilter Translate(TranslationContext context, ConstantExpression expression)
24+
{
25+
if (expression.Type == typeof(bool))
26+
{
27+
return ((bool)expression.Value) ? AstFilter.MatchesEverything() : AstFilter.MatchesNothing();
28+
}
29+
30+
throw new ExpressionNotSupportedException(expression);
31+
}
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 FluentAssertions;
17+
using MongoDB.Driver.Linq;
18+
using MongoDB.TestHelpers.XunitExtensions;
19+
using Xunit;
20+
21+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
22+
{
23+
public class CSharp4622Tests : Linq3IntegrationTest
24+
{
25+
[Theory]
26+
[ParameterAttributeData]
27+
public void ReplaceOne(
28+
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider)
29+
{
30+
var collection = GetCollection(linqProvider);
31+
var options = new ReplaceOptions { IsUpsert = true };
32+
var data = new Data { Id = 8, Text = "updated" };
33+
34+
var result = collection.ReplaceOne(d => true, data, options);
35+
36+
result.UpsertedId.Should().Be(8);
37+
}
38+
39+
private IMongoCollection<Data> GetCollection(LinqProvider linqProvider)
40+
{
41+
var collection = GetCollection<Data>("test", linqProvider);
42+
CreateCollection(collection);
43+
return collection;
44+
}
45+
46+
private class Data
47+
{
48+
public int Id { get; set; }
49+
public string Text { get; set; }
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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;
17+
using System.Collections.Generic;
18+
using System.Linq.Expressions;
19+
using FluentAssertions;
20+
using MongoDB.Bson;
21+
using MongoDB.Driver.Linq;
22+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
23+
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators;
24+
using Xunit;
25+
26+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Translators.ExpressionToFilterTranslators.ExpressionTranslators
27+
{
28+
public class ConstantExpressionToFilterTranslatorTests
29+
{
30+
[Theory]
31+
[MemberData(nameof(SupportedConstantExpressions))]
32+
public void Translate_should_process_supported_constants(ConstantExpression expression, BsonValue expectedFilter)
33+
{
34+
var result = ConstantExpressionToFilterTranslator.Translate(null, expression);
35+
36+
result.Render().Should().Be(expectedFilter);
37+
}
38+
39+
[Theory]
40+
[MemberData(nameof(NotSupportedConstantExpressions))]
41+
public void Translate_throws_on_not_supported_constants(ConstantExpression expression)
42+
{
43+
var ex = Record.Exception(() => ConstantExpressionToFilterTranslator.Translate(null, expression));
44+
45+
ex.Should().BeOfType<ExpressionNotSupportedException>();
46+
}
47+
48+
public static IEnumerable<object[]> SupportedConstantExpressions()
49+
{
50+
yield return new object[] { Expression.Constant(true), AstFilter.MatchesEverything().Render() };
51+
yield return new object[] { Expression.Constant(false), AstFilter.MatchesNothing().Render() };
52+
}
53+
54+
public static IEnumerable<object[]> NotSupportedConstantExpressions()
55+
{
56+
yield return new object[] { Expression.Constant(null) };
57+
yield return new object[] { Expression.Constant(1) };
58+
yield return new object[] { Expression.Constant(0) };
59+
yield return new object[] { Expression.Constant(string.Empty) };
60+
yield return new object[] { Expression.Constant("ignore") };
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)