Skip to content

Commit

Permalink
fix query predicate bug for NotEqual expression type (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrakib authored Aug 16, 2023
1 parent 8f0e204 commit 819bda8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected override void SplitBinaryExpression(BinaryExpression expression, Stack
}
}

private static string BuildEqualityPredicate(MemberInfo member, ConstantExpression expression, string memberStr)
private static string BuildEqualityPredicate(MemberInfo member, ConstantExpression expression, string memberStr, bool negated = false)
{
var sb = new StringBuilder();
var fieldAttribute = member.GetCustomAttribute<SearchFieldAttribute>();
Expand All @@ -157,6 +157,11 @@ private static string BuildEqualityPredicate(MemberInfo member, ConstantExpressi
throw new InvalidOperationException("Searches can only be performed on fields marked with a RedisFieldAttribute with the SearchFieldType not set to None");
}

if (negated)
{
sb.Append("-");
}

sb.Append($"{memberStr}:");
var searchFieldType = fieldAttribute.SearchFieldType != SearchFieldType.INDEXED
? fieldAttribute.SearchFieldType
Expand Down Expand Up @@ -189,7 +194,7 @@ private string BuildQueryPredicate(ExpressionType expType, MemberExpression memb
ExpressionType.GreaterThanOrEqual => $"{memberStr}:[{constExpression.Value} inf]",
ExpressionType.LessThanOrEqual => $"{memberStr}:[-inf {constExpression.Value}]",
ExpressionType.Equal => BuildEqualityPredicate(member.Member, constExpression, memberStr),
ExpressionType.NotEqual => $"{memberStr} : -{{{constExpression.Value}}}",
ExpressionType.NotEqual => BuildEqualityPredicate(member.Member, constExpression, memberStr, true),
_ => string.Empty
};
return queryPredicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,19 @@ public async Task GetGroupCount()
Assert.True(1<=result["COUNT"]);
}
}

[Fact]
public async Task GetGroupCountWithNegationQuery()
{
Setup();
var collection = new RedisAggregationSet<Person>(_connection);
var results = await collection
.Where(x => x.RecordShell.Age != 0)
.GroupBy(x => x.RecordShell.Age).CountGroupMembers().ToListAsync();
foreach (var result in results)
{
Assert.True(1 <= result["COUNT"]);
}
}
}
}

0 comments on commit 819bda8

Please sign in to comment.