Skip to content

fix query predicate bug for NotEqual expression type #399

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

Merged
merged 3 commits into from
Aug 16, 2023
Merged
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
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"]);
}
}
}
}