Skip to content

fixing issue with non-nullable numerics #375

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 1 commit into from
May 8, 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
27 changes: 14 additions & 13 deletions src/Redis.OM/Common/ExpressionParserUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,6 @@ private static string GetOperandStringForMember(MemberExpression member, bool tr
return string.Join("|", ulids);
}

if (resolved is IEnumerable<int?> ints)
{
var sb = new StringBuilder();
sb.Append('|');
foreach (var i in ints)
{
sb.Append($"[{i} {i}]|");
}

sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}

if (resolvedType.IsArray || resolvedType.GetInterfaces().Contains(typeof(IEnumerable)))
{
var asEnumerable = (IEnumerable)resolved;
Expand All @@ -368,6 +355,20 @@ private static string GetOperandStringForMember(MemberExpression member, bool tr
elementType = resolvedType.GenericTypeArguments.FirstOrDefault();
}

if (elementType != null && TypeDeterminationUtilities.IsNumeric(elementType))
{
var sb = new StringBuilder();
sb.Append('|');

foreach (var item in asEnumerable)
{
sb.Append(FormattableString.Invariant($"[{item} {item}]|"));
}

sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}

if (elementType != null && elementType.IsEnum)
{
if (treatEnumsAsInt)
Expand Down
126 changes: 126 additions & 0 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3041,5 +3041,131 @@ public void TestSelectNestedObject()
"$.Address.ForwardingAddress"
));
}

[Fact]
public void NonNullableNumericFieldContains()
{
var ints = new int[] { 1, 2, 3 };
var bytes = new byte[] { 4, 5, 6 };
var sbytes = new sbyte[] { 7, 8, 9 };
var shorts = new short[] { 10, 11, 12 };
var uints = new uint[] { 13, 14, 15 };
var longs = new long[] { 16, 17, 18 };
var ulongs = new ulong[] { 19, 20, 21 };
var doubles = new double[] { 22.5, 23, 24 };
var floats = new float[] { 25.5F, 26, 27 };
var ushorts = new ushort[] { 28, 29, 30 };
_mock.Setup(x => x.Execute(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(_mockReply);
var collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => ints.Contains(x.Integer));
collection.ToList();
var expected = $"@{nameof(ObjectWithNumerics.Integer)}:[1 1]|@{nameof(ObjectWithNumerics.Integer)}:[2 2]|@{nameof(ObjectWithNumerics.Integer)}:[3 3]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => bytes.Contains(x.Byte));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.Byte)}:[4 4]|@{nameof(ObjectWithNumerics.Byte)}:[5 5]|@{nameof(ObjectWithNumerics.Byte)}:[6 6]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => sbytes.Contains(x.SByte));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.SByte)}:[7 7]|@{nameof(ObjectWithNumerics.SByte)}:[8 8]|@{nameof(ObjectWithNumerics.SByte)}:[9 9]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => shorts.Contains(x.Short));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.Short)}:[10 10]|@{nameof(ObjectWithNumerics.Short)}:[11 11]|@{nameof(ObjectWithNumerics.Short)}:[12 12]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => ushorts.Contains(x.UShort));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.UShort)}:[28 28]|@{nameof(ObjectWithNumerics.UShort)}:[29 29]|@{nameof(ObjectWithNumerics.UShort)}:[30 30]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => uints.Contains(x.UInt));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.UInt)}:[13 13]|@{nameof(ObjectWithNumerics.UInt)}:[14 14]|@{nameof(ObjectWithNumerics.UInt)}:[15 15]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => longs.Contains(x.Long));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.Long)}:[16 16]|@{nameof(ObjectWithNumerics.Long)}:[17 17]|@{nameof(ObjectWithNumerics.Long)}:[18 18]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => ulongs.Contains(x.ULong));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.ULong)}:[19 19]|@{nameof(ObjectWithNumerics.ULong)}:[20 20]|@{nameof(ObjectWithNumerics.ULong)}:[21 21]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => doubles.Contains(x.Double));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.Double)}:[22.5 22.5]|@{nameof(ObjectWithNumerics.Double)}:[23 23]|@{nameof(ObjectWithNumerics.Double)}:[24 24]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));

collection = new RedisCollection<ObjectWithNumerics>(_mock.Object).Where(x => floats.Contains(x.Float));
collection.ToList();
expected = $"@{nameof(ObjectWithNumerics.Float)}:[25.5 25.5]|@{nameof(ObjectWithNumerics.Float)}:[26 26]|@{nameof(ObjectWithNumerics.Float)}:[27 27]";
_mock.Verify(x => x.Execute(
"FT.SEARCH",
"objectwithnumerics-idx",
expected,
"LIMIT",
"0",
"100"));
}
}
}
28 changes: 28 additions & 0 deletions test/Redis.OM.Unit.Tests/Serialization/ObjectWithNumerics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests;

[Document(StorageType = StorageType.Json)]
public class ObjectWithNumerics
{
[Indexed]
public int Integer { get; set; }
[Indexed]
public byte Byte { get; set; }
[Indexed]
public sbyte SByte { get; set; }
[Indexed]
public short Short { get; set; }
[Indexed]
public ushort UShort { get; set; }
[Indexed]
public uint UInt { get; set; }
[Indexed]
public long Long { get; set; }
[Indexed]
public ulong ULong { get; set; }
[Indexed]
public double Double { get; set; }
[Indexed]
public float Float { get; set; }
}