From c4526d3ed64fb7275456078b78a25399dbe58b4c Mon Sep 17 00:00:00 2001 From: AJ Rotolo Date: Wed, 23 Sep 2026 12:30:04 -0500 Subject: [PATCH 1/4] fix: emit IS NOT NULL for a NotEquals filter with a null value --- CHANGELOG.md | 11 ++ TychoDB.UnitTests/NotEqualsNullTests.cs | 156 ++++++++++++++++++++++++ TychoDB/FilterBuilder.cs | 6 +- 3 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 TychoDB.UnitTests/NotEqualsNullTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d0930a..e587f50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 5.3.1 (unreleased) + +### Fixed + +- **`Filter(NotEquals, x => x.Member, null)` matched nothing.** The scalar path bound the null as a + parameter and emitted `JSON_EXTRACT(...) <> NULL`, which is never true in SQL, so the "member is + present" test silently returned no rows on every 5.x release. 4.x concatenated `<> ''`, which + behaved as `IS NOT NULL` by accident. The scalar path now emits `IS NOT NULL`, exactly as + `Equals` with null emits `IS NULL` and as the list-member path already did. An empty string is + present, so it still matches; `NotEquals` with a non-null value still excludes absent members. + ## 5.3.0 (unreleased) ### Added diff --git a/TychoDB.UnitTests/NotEqualsNullTests.cs b/TychoDB.UnitTests/NotEqualsNullTests.cs new file mode 100644 index 0000000..d41ea2a --- /dev/null +++ b/TychoDB.UnitTests/NotEqualsNullTests.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace TychoDB.UnitTests; + +/// +/// Filter(NotEquals, x => x.Member, null) is the "member is present" test, mirroring +/// Equals with null as the "member is absent" test. Binding the null as a parameter renders +/// <> NULL, which is never true in SQL, so the filter silently matched nothing; the +/// scalar path has to emit IS NOT NULL exactly as the list-member path already does. +/// +[TestClass] +public class NotEqualsNullTests +{ + public class Inner + { + public string Label { get; set; } + } + + public class Doc + { + public long Id { get; set; } + + public DateTime? When { get; set; } + + public string Text { get; set; } + + public bool? Flag { get; set; } + + public Inner Nested { get; set; } + } + + public static IEnumerable Serializers + { + get + { + yield return new object[] { new NewtonsoftJsonSerializer(), "newtonsoft" }; + yield return new object[] { new SystemTextJsonSerializer(), "stj" }; + } + } + + private static async Task<(Tycho Db, string Path)> SeedAsync(IJsonSerializer jsonSerializer) + { + var path = Path.Combine(Path.GetTempPath(), "tycho-notequals-null-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(path); + + var db = + new Tycho(path, jsonSerializer, requireTypeRegistration: true) + .AddTypeRegistrationWithCustomKeySelector(x => x.Id) + .Connect(); + + await db.WriteObjectsAsync( + new[] + { + new Doc { Id = 1, When = new DateTime(2026, 1, 1), Text = "a", Flag = true, Nested = new Inner { Label = "x" } }, + new Doc { Id = 2, When = null, Text = null, Flag = null, Nested = null }, + new Doc { Id = 3, When = new DateTime(2026, 1, 3), Text = string.Empty, Flag = false, Nested = new Inner { Label = null } }, + new Doc { Id = 4, When = null, Text = "d", Flag = null, Nested = null }, + }); + + return (db, path); + } + + private static async Task IdsAsync(Tycho db, FilterBuilder filter) + => (await db.ReadObjectsAsync(filter: filter)).Select(x => x.Id).OrderBy(x => x).ToArray(); + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEqualsNull_OnNullableDateTime_ReturnsRowsWithAValue(IJsonSerializer jsonSerializer, string label) + { + var (db, path) = await SeedAsync(jsonSerializer); + using var scoped = db; + + var present = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.When, null)); + var absent = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.When, null)); + + present.ShouldBe(new long[] { 1, 3 }, label); + absent.ShouldBe(new long[] { 2, 4 }, label); + + db.Disconnect(); + Directory.Delete(path, true); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEqualsNull_OnString_KeepsEmptyStrings(IJsonSerializer jsonSerializer, string label) + { + var (db, path) = await SeedAsync(jsonSerializer); + using var scoped = db; + + var present = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Text, null)); + var presentAndNonEmpty = + await IdsAsync( + db, + FilterBuilder.Create() + .Filter(FilterType.NotEquals, x => x.Text, null) + .And() + .Filter(FilterType.NotEquals, x => x.Text, string.Empty)); + + present.ShouldBe(new long[] { 1, 3, 4 }, label); + presentAndNonEmpty.ShouldBe(new long[] { 1, 4 }, label); + + db.Disconnect(); + Directory.Delete(path, true); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEqualsNull_OnNullableBool_ReturnsRowsWithAValue(IJsonSerializer jsonSerializer, string label) + { + var (db, path) = await SeedAsync(jsonSerializer); + using var scoped = db; + + var present = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Flag, null)); + + present.ShouldBe(new long[] { 1, 3 }, label); + + db.Disconnect(); + Directory.Delete(path, true); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEqualsNull_OnNestedObject_ReturnsRowsWhereObjectIsPresent(IJsonSerializer jsonSerializer, string label) + { + var (db, path) = await SeedAsync(jsonSerializer); + using var scoped = db; + + var present = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Nested, null)); + + present.ShouldBe(new long[] { 1, 3 }, label); + + db.Disconnect(); + Directory.Delete(path, true); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_WithValue_StillExcludesNulls(IJsonSerializer jsonSerializer, string label) + { + var (db, path) = await SeedAsync(jsonSerializer); + using var scoped = db; + + var notA = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Text, "a")); + + notA.ShouldBe(new long[] { 3, 4 }, label); + + db.Disconnect(); + Directory.Delete(path, true); + } +} diff --git a/TychoDB/FilterBuilder.cs b/TychoDB/FilterBuilder.cs index 0387360..e488ba0 100644 --- a/TychoDB/FilterBuilder.cs +++ b/TychoDB/FilterBuilder.cs @@ -894,12 +894,10 @@ private void BuildSimpleFilter(StringBuilder commandBuilder, in Filter filter, I break; case FilterType.NotEquals: - if (filter.IsPropertyPathBool) + if (filter.Value is null) { AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(NotEquals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.AppendLine(); + commandBuilder.Append(IsNotNull).AppendLine(); } else { From fe1cbea97234e1c05937547bff5a99537d7e0003 Mon Sep 17 00:00:00 2001 From: AJ Rotolo Date: Thu, 24 Sep 2026 14:05:00 -0500 Subject: [PATCH 2/4] fix: compare NotEquals values in the same form as Equals --- CHANGELOG.md | 7 + TychoDB.UnitTests/NotEqualsDateTimeTests.cs | 82 ++++++++ TychoDB.UnitTests/NotEqualsValueTests.cs | 199 ++++++++++++++++++++ TychoDB/FilterBuilder.cs | 159 ++++++++-------- 4 files changed, 366 insertions(+), 81 deletions(-) create mode 100644 TychoDB.UnitTests/NotEqualsDateTimeTests.cs create mode 100644 TychoDB.UnitTests/NotEqualsValueTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index e587f50..b522281 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ behaved as `IS NOT NULL` by accident. The scalar path now emits `IS NOT NULL`, exactly as `Equals` with null emits `IS NULL` and as the list-member path already did. An empty string is present, so it still matches; `NotEquals` with a non-null value still excludes absent members. +- **`NotEquals` with a date-time or numeric value could return the row it should exclude.** The + negation bound a `DateTime`/`DateTimeOffset` as culture-formatted text rather than in the + serializer's date format, so no stored value ever compared equal and every present row came + back; and it compared a numeric member without the `CAST(… as NUMERIC)` that `Equals` uses, + so a text value such as `"2"` on a numeric path excluded nothing. `Equals` and `NotEquals` now + share one type dispatch with only the operator swapped, on the scalar and list-member paths + alike. ## 5.3.0 (unreleased) diff --git a/TychoDB.UnitTests/NotEqualsDateTimeTests.cs b/TychoDB.UnitTests/NotEqualsDateTimeTests.cs new file mode 100644 index 0000000..0416fbf --- /dev/null +++ b/TychoDB.UnitTests/NotEqualsDateTimeTests.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace TychoDB.UnitTests; + +/// +/// NotEquals has to bind a date-time value in the form the serializer wrote it, exactly +/// as Equals does. Bound as DateTime.ToString() instead, the value never equals +/// any stored text, so the negation admits every row, including the one it exists to exclude. +/// +[TestClass] +public class NotEqualsDateTimeTests +{ + private static readonly DateTime FirstDay = new(2026, 1, 1, 8, 0, 0, DateTimeKind.Utc); + private static readonly DateTime SecondDay = new(2026, 1, 2, 8, 0, 0, DateTimeKind.Utc); + private static readonly DateTime ThirdDay = new(2026, 1, 3, 8, 0, 0, DateTimeKind.Utc); + + public static IEnumerable Serializers + { + get + { + yield return new object[] { new SystemTextJsonSerializer(), "stj" }; + yield return new object[] { new NewtonsoftJsonSerializer(), "nsj" }; + } + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_OnDateTimeProperty_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var ids = await ReadIdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.When, FirstDay)); + + ids.ShouldBe(new[] { 2, 3 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_OnDateTimeOffsetProperty_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var ids = await ReadIdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.WhenOffset, new DateTimeOffset(FirstDay))); + + ids.ShouldBe(new[] { 2, 3 }, label); + } + + private static async Task SeedAsync(IJsonSerializer jsonSerializer) + { + var db = new Tycho(Path.GetTempPath(), jsonSerializer, dbName: $"{Guid.NewGuid()}.db", rebuildCache: true, requireTypeRegistration: false); + await db.ConnectAsync(); + + var docs = new[] + { + new Doc { Id = 1, When = FirstDay, WhenOffset = new DateTimeOffset(FirstDay) }, + new Doc { Id = 2, When = SecondDay, WhenOffset = new DateTimeOffset(SecondDay) }, + new Doc { Id = 3, When = ThirdDay, WhenOffset = new DateTimeOffset(ThirdDay) }, + }; + + await db.WriteObjectsAsync(docs, x => x.Id.ToString()); + + return db; + } + + private static async Task ReadIdsAsync(Tycho db, FilterBuilder filter) + => (await db.ReadObjectsAsync(filter: filter)).Select(x => x.Id).OrderBy(x => x).ToArray(); + + public class Doc + { + public int Id { get; set; } + + public DateTime When { get; set; } + + public DateTimeOffset WhenOffset { get; set; } + } +} diff --git a/TychoDB.UnitTests/NotEqualsValueTests.cs b/TychoDB.UnitTests/NotEqualsValueTests.cs new file mode 100644 index 0000000..5eb585a --- /dev/null +++ b/TychoDB.UnitTests/NotEqualsValueTests.cs @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; + +namespace TychoDB.UnitTests; + +/// +/// NotEquals has to compare against exactly the form Equals does, or its negation +/// admits the very row it should exclude: a date-time bound as culture text never equals the +/// stored ISO form, and a numeric member compared without its CAST never equals a bound +/// text value. Both the scalar and the list-member paths are pinned here. +/// +[TestClass] +public class NotEqualsValueTests +{ + private static readonly DateTime First = new(2026, 1, 1, 8, 0, 0, DateTimeKind.Utc); + private static readonly DateTime Second = new(2026, 1, 3, 8, 0, 0, DateTimeKind.Utc); + private static readonly DateTimeOffset FirstOffset = new(First); + private static readonly DateTimeOffset SecondOffset = new(Second); + + public class Item + { + public DateTime When { get; set; } + + public decimal Amount { get; set; } + } + + public class Doc + { + public int Id { get; set; } + + public DateTime When { get; set; } + + public DateTimeOffset WhenOffset { get; set; } + + public DateTime? MaybeWhen { get; set; } + + public int Count { get; set; } + + public decimal Amount { get; set; } + + public List Items { get; set; } + } + + public static IEnumerable Serializers + { + get + { + yield return new object[] { new NewtonsoftJsonSerializer(), "newtonsoft" }; + yield return new object[] { new SystemTextJsonSerializer(), "stj" }; + } + } + + private static async Task SeedAsync(IJsonSerializer jsonSerializer) + { + var db = new Tycho(Path.GetTempPath(), jsonSerializer, dbName: $"{Guid.NewGuid()}.db", rebuildCache: true, requireTypeRegistration: false); + await db.ConnectAsync(); + + await db.WriteObjectsAsync( + new[] + { + new Doc + { + Id = 1, + When = First, + WhenOffset = FirstOffset, + MaybeWhen = First, + Count = 1, + Amount = 1.5m, + Items = new List { new() { When = First, Amount = 1.5m } }, + }, + new Doc + { + Id = 2, + When = Second, + WhenOffset = SecondOffset, + MaybeWhen = null, + Count = 2, + Amount = 2m, + Items = new List { new() { When = Second, Amount = 2m } }, + }, + }, + x => x.Id.ToString()); + + return db; + } + + private static async Task IdsAsync(Tycho db, FilterBuilder filter) + => (await db.ReadObjectsAsync(filter: filter)).Select(x => x.Id).OrderBy(x => x).ToArray(); + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_DateTime_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var equal = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.When, First)); + var notEqual = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.When, First)); + + equal.ShouldBe(new[] { 1 }, label); + notEqual.ShouldBe(new[] { 2 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_DateTimeOffset_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var equal = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.WhenOffset, FirstOffset)); + var notEqual = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.WhenOffset, FirstOffset)); + + equal.ShouldBe(new[] { 1 }, label); + notEqual.ShouldBe(new[] { 2 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_NullableDateTime_ExcludesTheEqualRowAndAbsentMembers(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var equal = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.MaybeWhen, First)); + var notEqual = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.MaybeWhen, First)); + var notSecond = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.MaybeWhen, Second)); + + equal.ShouldBe(new[] { 1 }, label); + notEqual.ShouldBeEmpty(label); + notSecond.ShouldBe(new[] { 1 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_NumericPath_WithTextValue_MirrorsEquals(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var equalAmount = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.Amount, "1.5")); + var notEqualAmount = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Amount, "1.5")); + var notEqualCount = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Count, "1")); + + equalAmount.ShouldBe(new[] { 1 }, label); + notEqualAmount.ShouldBe(new[] { 2 }, label); + notEqualCount.ShouldBe(new[] { 2 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_NumericPath_WithNumericValue_ExcludesTheEqualRow(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + + var notEqualAmount = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Amount, 1.5m)); + var notEqualCount = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Count, 1)); + + notEqualAmount.ShouldBe(new[] { 2 }, label); + notEqualCount.ShouldBe(new[] { 2 }, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public void NotEquals_ListMemberDateTime_BindsTheSerializerDateForm(IJsonSerializer jsonSerializer, string label) + { + var equal = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.Equals, x => x.Items, x => x.When, First), out var equalParameters); + var notEqual = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Items, x => x.When, First), out var notEqualParameters); + + equal.ShouldContain("VAL.value = " + FilterParameters.ParameterPrefix); + notEqual.ShouldContain("VAL.value <> " + FilterParameters.ParameterPrefix); + equalParameters.Values.ShouldBe(new object[] { First.ToString(jsonSerializer.DateTimeSerializationFormat) }, label); + notEqualParameters.Values.ShouldBe(equalParameters.Values, label); + } + + [TestMethod] + [DynamicData(nameof(Serializers))] + public void NotEquals_ListMemberNumeric_RendersTheNumericCast(IJsonSerializer jsonSerializer, string label) + { + var equal = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.Equals, x => x.Items, x => x.Amount, "1.5"), out var equalParameters); + var notEqual = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Items, x => x.Amount, "1.5"), out var notEqualParameters); + + equal.ShouldContain("CAST(VAL.value as NUMERIC) = " + FilterParameters.ParameterPrefix); + notEqual.ShouldContain("CAST(VAL.value as NUMERIC) <> " + FilterParameters.ParameterPrefix); + equalParameters.Values.ShouldBe(new object[] { "1.5" }, label); + notEqualParameters.Values.ShouldBe(equalParameters.Values, label); + } + + private static string Render(IJsonSerializer jsonSerializer, FilterBuilder filter, out FilterParameters parameters) + { + var sb = new StringBuilder(); + parameters = new FilterParameters(); + filter.Build(sb, jsonSerializer, parameters); + + return sb.ToString(); + } +} diff --git a/TychoDB/FilterBuilder.cs b/TychoDB/FilterBuilder.cs index e488ba0..2474e28 100644 --- a/TychoDB/FilterBuilder.cs +++ b/TychoDB/FilterBuilder.cs @@ -646,29 +646,7 @@ private void BuildExistsFilter(StringBuilder commandBuilder, in Filter filter, I break; case FilterType.Equals: - AppendExistsPrefix(commandBuilder, filter); - if (filter.Value is null) - { - commandBuilder.Append(ValValue).Append(IsNull).Append(ExistsEnd).AppendLine(); - } - else if (filter.IsPropertyValuePathNumeric) - { - commandBuilder.Append(CastValNumeric).Append(Equals); - AppendNumericValue(commandBuilder, parameters, filter.Value); - commandBuilder.Append(ExistsEnd).AppendLine(); - } - else if (filter.IsPropertyValuePathDateTime) - { - var dateTimeString = GetDateTimeString(filter.Value, jsonSerializer); - commandBuilder.Append(ValValue).Append(Equals).Append(parameters.Add(dateTimeString)).Append(ExistsEnd).AppendLine(); - } - else - { - commandBuilder.Append(ValValue).Append(Equals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.Append(ExistsEnd).AppendLine(); - } - + AppendListMemberEquality(commandBuilder, filter, jsonSerializer, parameters, negated: false); break; case FilterType.GreaterThan: @@ -700,18 +678,7 @@ private void BuildExistsFilter(StringBuilder commandBuilder, in Filter filter, I break; case FilterType.NotEquals: - AppendExistsPrefix(commandBuilder, filter); - if (filter.Value is null) - { - commandBuilder.Append(ValValue).Append(IsNotNull).Append(ExistsEnd).AppendLine(); - } - else - { - commandBuilder.Append(ValValue).Append(NotEquals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.Append(ExistsEnd).AppendLine(); - } - + AppendListMemberEquality(commandBuilder, filter, jsonSerializer, parameters, negated: true); break; case FilterType.StartsWith: @@ -830,39 +797,7 @@ private void BuildSimpleFilter(StringBuilder commandBuilder, in Filter filter, I break; case FilterType.Equals: - if (filter.Value is null) - { - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(IsNull).AppendLine(); - } - else if (filter.IsPropertyPathBool) - { - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(Equals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.AppendLine(); - } - else if (filter.IsPropertyPathNumeric) - { - AppendCastNumeric(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(Equals); - AppendNumericValue(commandBuilder, parameters, filter.Value); - commandBuilder.AppendLine(); - } - else if (filter.IsPropertyPathDateTime) - { - var dateTimeString = GetDateTimeString(filter.Value, jsonSerializer); - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(Equals).Append(parameters.Add(dateTimeString)).AppendLine(); - } - else - { - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(Equals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.AppendLine(); - } - + AppendScalarEquality(commandBuilder, filter, jsonSerializer, parameters, negated: false); break; case FilterType.GreaterThan: @@ -894,19 +829,7 @@ private void BuildSimpleFilter(StringBuilder commandBuilder, in Filter filter, I break; case FilterType.NotEquals: - if (filter.Value is null) - { - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(IsNotNull).AppendLine(); - } - else - { - AppendJsonExtract(commandBuilder, filter.PropertyPath!); - commandBuilder.Append(NotEquals); - AppendValue(commandBuilder, parameters, filter.Value); - commandBuilder.AppendLine(); - } - + AppendScalarEquality(commandBuilder, filter, jsonSerializer, parameters, negated: true); break; case FilterType.StartsWith: @@ -922,6 +845,80 @@ private void BuildSimpleFilter(StringBuilder commandBuilder, in Filter filter, I } } + /// + /// Renders path = value or path <> value. Both operators share one type + /// dispatch so the negation compares against exactly the form the equality does: a null + /// value becomes IS NULL / IS NOT NULL, a numeric property keeps its + /// CAST(… as NUMERIC), and a date-time value is formatted the way the serializer + /// wrote it. + /// + private static void AppendScalarEquality(StringBuilder commandBuilder, in Filter filter, IJsonSerializer jsonSerializer, FilterParameters parameters, bool negated) + { + if (filter.Value is null) + { + AppendJsonExtract(commandBuilder, filter.PropertyPath!); + commandBuilder.Append(negated ? IsNotNull : IsNull).AppendLine(); + return; + } + + var comparison = negated ? NotEquals : Equals; + + if (filter.IsPropertyPathNumeric) + { + AppendCastNumeric(commandBuilder, filter.PropertyPath!); + commandBuilder.Append(comparison); + AppendNumericValue(commandBuilder, parameters, filter.Value); + } + else if (filter.IsPropertyPathDateTime) + { + AppendJsonExtract(commandBuilder, filter.PropertyPath!); + commandBuilder.Append(comparison).Append(parameters.Add(GetDateTimeString(filter.Value, jsonSerializer))); + } + else + { + AppendJsonExtract(commandBuilder, filter.PropertyPath!); + commandBuilder.Append(comparison); + AppendValue(commandBuilder, parameters, filter.Value); + } + + commandBuilder.AppendLine(); + } + + /// + /// The list-member form of : an EXISTS over the + /// array's elements with the same shared type dispatch for both operators. + /// + private void AppendListMemberEquality(StringBuilder commandBuilder, in Filter filter, IJsonSerializer jsonSerializer, FilterParameters parameters, bool negated) + { + AppendExistsPrefix(commandBuilder, filter); + + if (filter.Value is null) + { + commandBuilder.Append(ValValue).Append(negated ? IsNotNull : IsNull); + } + else + { + var comparison = negated ? NotEquals : Equals; + + if (filter.IsPropertyValuePathNumeric) + { + commandBuilder.Append(CastValNumeric).Append(comparison); + AppendNumericValue(commandBuilder, parameters, filter.Value); + } + else if (filter.IsPropertyValuePathDateTime) + { + commandBuilder.Append(ValValue).Append(comparison).Append(parameters.Add(GetDateTimeString(filter.Value, jsonSerializer))); + } + else + { + commandBuilder.Append(ValValue).Append(comparison); + AppendValue(commandBuilder, parameters, filter.Value); + } + } + + commandBuilder.Append(ExistsEnd).AppendLine(); + } + /// /// Renders path IN (…) / path NOT IN (…). The path is emitted through the same /// helpers the scalar comparisons use, so a numeric property keeps its From 032a8d98a038c3e6e90166e33a0c1eadd1a34b3b Mon Sep 17 00:00:00 2001 From: Michael Stonis <120685+michaelstonis@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:26:48 -0500 Subject: [PATCH 3/4] fix: compare a text value on a date-time path as given Equals and NotEquals sent every non-null value on a DateTime path through GetDateTimeString, which returns an empty string for anything that is not a DateTime or DateTimeOffset. NotEquals with an already formatted string then rendered <> '' and returned every row with a date, where v5.3.0 returned the right rows. The date branch is now taken only for an actual date; other values are bound as given. Adds scalar and list-member tests for a text value on a date path, the changelog notes for it and for NaN on numeric members, and dates the 5.3.0 changelog section. Co-Authored-By: Claude Fable 5.1 --- CHANGELOG.md | 13 +++++++++-- TychoDB.UnitTests/NotEqualsValueTests.cs | 29 ++++++++++++++++++++++++ TychoDB/FilterBuilder.cs | 4 ++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b522281..3165f10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,17 @@ so a text value such as `"2"` on a numeric path excluded nothing. `Equals` and `NotEquals` now share one type dispatch with only the operator swapped, on the scalar and list-member paths alike. - -## 5.3.0 (unreleased) + - **A text value on a date-time path is compared as given.** Passing an already formatted + string — `Filter(FilterType.NotEquals, x => x.When, "2026-01-01T08:00:00Z")`, until now the + only way to make `NotEquals` on a date work — keeps returning the same rows. The same value + with `Equals` was compared against an empty string and matched nothing; it now matches the + rows storing that text. + - **`NotEquals` on a numeric member now treats non-numeric text as `0`,** as `Equals` already + did: both serializers write `double.NaN` as the string `"NaN"`, which `CAST(… as NUMERIC)` + reads as `0`, so `Filter(FilterType.NotEquals, x => x.Score, 0d)` no longer returns a row + storing `NaN`. + +## 5.3.0 — 2026-09-02 ### Added diff --git a/TychoDB.UnitTests/NotEqualsValueTests.cs b/TychoDB.UnitTests/NotEqualsValueTests.cs index 5eb585a..c53f704 100644 --- a/TychoDB.UnitTests/NotEqualsValueTests.cs +++ b/TychoDB.UnitTests/NotEqualsValueTests.cs @@ -134,6 +134,20 @@ public async Task NotEquals_NullableDateTime_ExcludesTheEqualRowAndAbsentMembers notSecond.ShouldBe(new[] { 1 }, label); } + [TestMethod] + [DynamicData(nameof(Serializers))] + public async Task NotEquals_DateTimePath_WithTextValue_ComparesAgainstTheTextAsGiven(IJsonSerializer jsonSerializer, string label) + { + using var db = await SeedAsync(jsonSerializer); + var first = First.ToString(jsonSerializer.DateTimeSerializationFormat); + + var equal = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.Equals, x => x.When, first)); + var notEqual = await IdsAsync(db, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.When, first)); + + equal.ShouldBe(new[] { 1 }, label); + notEqual.ShouldBe(new[] { 2 }, label); + } + [TestMethod] [DynamicData(nameof(Serializers))] public async Task NotEquals_NumericPath_WithTextValue_MirrorsEquals(IJsonSerializer jsonSerializer, string label) @@ -175,6 +189,21 @@ public void NotEquals_ListMemberDateTime_BindsTheSerializerDateForm(IJsonSeriali notEqualParameters.Values.ShouldBe(equalParameters.Values, label); } + [TestMethod] + [DynamicData(nameof(Serializers))] + public void NotEquals_ListMemberDateTime_WithTextValue_BindsTheTextAsGiven(IJsonSerializer jsonSerializer, string label) + { + var first = First.ToString(jsonSerializer.DateTimeSerializationFormat); + + var equal = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.Equals, x => x.Items, x => x.When, first), out var equalParameters); + var notEqual = Render(jsonSerializer, FilterBuilder.Create().Filter(FilterType.NotEquals, x => x.Items, x => x.When, first), out var notEqualParameters); + + equal.ShouldContain("VAL.value = " + FilterParameters.ParameterPrefix); + notEqual.ShouldContain("VAL.value <> " + FilterParameters.ParameterPrefix); + equalParameters.Values.ShouldBe(new object[] { first }, label); + notEqualParameters.Values.ShouldBe(equalParameters.Values, label); + } + [TestMethod] [DynamicData(nameof(Serializers))] public void NotEquals_ListMemberNumeric_RendersTheNumericCast(IJsonSerializer jsonSerializer, string label) diff --git a/TychoDB/FilterBuilder.cs b/TychoDB/FilterBuilder.cs index 2474e28..5833126 100644 --- a/TychoDB/FilterBuilder.cs +++ b/TychoDB/FilterBuilder.cs @@ -869,7 +869,7 @@ private static void AppendScalarEquality(StringBuilder commandBuilder, in Filter commandBuilder.Append(comparison); AppendNumericValue(commandBuilder, parameters, filter.Value); } - else if (filter.IsPropertyPathDateTime) + else if (filter.IsPropertyPathDateTime && filter.Value is DateTime or DateTimeOffset) { AppendJsonExtract(commandBuilder, filter.PropertyPath!); commandBuilder.Append(comparison).Append(parameters.Add(GetDateTimeString(filter.Value, jsonSerializer))); @@ -905,7 +905,7 @@ private void AppendListMemberEquality(StringBuilder commandBuilder, in Filter fi commandBuilder.Append(CastValNumeric).Append(comparison); AppendNumericValue(commandBuilder, parameters, filter.Value); } - else if (filter.IsPropertyValuePathDateTime) + else if (filter.IsPropertyValuePathDateTime && filter.Value is DateTime or DateTimeOffset) { commandBuilder.Append(ValValue).Append(comparison).Append(parameters.Add(GetDateTimeString(filter.Value, jsonSerializer))); } From 6b3e2ac23a6e570c0b36b0ba71ff49141800c6d7 Mon Sep 17 00:00:00 2001 From: Michael Stonis <120685+michaelstonis@users.noreply.github.com> Date: Mon, 5 Oct 2026 14:14:55 -0500 Subject: [PATCH 4/4] docs: date the 5.3.1 changelog section Co-Authored-By: Claude Fable 5.1 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4b92b1..117fd5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 5.3.1 (unreleased) +## 5.3.1 — 2026-10-05 ### Fixed