Skip to content

Commit

Permalink
Dealing with literal boolean values in complex WHERE clauses. Closes G…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Dec 12, 2023
1 parent 282a355 commit bc46d29
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
31 changes: 31 additions & 0 deletions src/LinqTests/Bugs/Bug_2850_missed_field_reducing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;

namespace LinqTests.Bugs;

public class Bug_2850_missed_field_reducing : BugIntegrationContext
{
public async Task RunQuery(bool include, int resultCount)
{
var results = await theSession.Query<Target>().Where(x => include || !x.Flag).CountAsync();
results.ShouldBe(resultCount);
}

[Fact]
public async Task pass_bool_into_query()
{
await theStore.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(Target));

var targets = Target.GenerateRandomData(100).ToArray();
await theStore.BulkInsertAsync(targets);

var count = targets.Count(x => x.Flag);

await RunQuery(true, 100);
await RunQuery(false, 100 - count);
}
}
24 changes: 12 additions & 12 deletions src/Marten/Linq/Parsing/WhereClauseParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using Marten.Exceptions;
using Marten.Linq.Members;
Expand Down Expand Up @@ -49,6 +50,13 @@ protected override Expression VisitMember(MemberExpression node)
{
if (node.Type == typeof(bool))
{
// Check if it's a literal field. See https://github.com/JasperFx/marten/issues/2850
if (node.TryToParseConstant(out var constant))
{
_holder.Register(constant.Value.Equals(true) ? new WhereFragment("true") : new WhereFragment("false"));
return null;
}

var field = _members.MemberFor(node);
if (field is IBooleanField b)
{
Expand All @@ -65,18 +73,6 @@ protected override Expression VisitMember(MemberExpression node)
return base.VisitMember(node);
}

private IQueryableMember? findHoldingMember(SimpleExpression? @object, SimpleExpression[] arguments)
{
if (@object?.Member != null) return @object.Member;

foreach (var argument in arguments)
{
if (argument.Member != null) return argument.Member;
}

return null;
}

protected override Expression VisitMethodCall(MethodCallExpression node)
{
var parser = _options.Linq.FindMethodParser(node);
Expand Down Expand Up @@ -162,6 +158,10 @@ protected override Expression VisitUnary(UnaryExpression node)

return returnValue;
}
else if (node.NodeType == ExpressionType.OrElse)
{
Debug.WriteLine(node);
}

return null;
}
Expand Down

0 comments on commit bc46d29

Please sign in to comment.