Skip to content

[BUG] Comparison with index not processed correctly #1684

Closed
@mikhail-khalizev

Description

@mikhail-khalizev

Version
5.0.8

Describe the bug
Hi.

Or I made some stupid mistake in my code and don’t understand it. Or, a comparison condition with an index is not processed correctly.

Take a look at the test below: it adds 10 elements, and then all the elements from the collection are queried in different ways. The first two tests are successful. And the last 2 for some reason fail.

Code to Reproduce

public class StateModel
{
    [BsonId] 
    public int Id { get; set; }
}


[Fact]
public void StateTest()
{
    using (var db = new LiteDatabase(":memory:"))
    {
        var col = db.GetCollection<StateModel>("col");

        var count = 10;
        var firstId = 5;
        var lastId = firstId + count - 1;

        for (var i = firstId; i <= lastId; i++)
        {
            col.Insert(new StateModel
            {
               Id = i
            });
        }

        var list = col.Query().ToList();
        list.Should().HaveCount(count);

        list = col.Query()
            .Where(x => firstId <= x.Id)
            .ToList();
        list.Should().HaveCount(count);

        list = col.Query()
            .Where(x => x.Id <= lastId)
            .ToList();
        list.Should().HaveCount(count);

        list = col.Query()
            .Where(x => firstId <= x.Id && x.Id <= lastId)
            .ToList();
        list.Should().HaveCount(count);
    }
}

Expected behavior
Test passed.

Activity

lbnascimento

lbnascimento commented on May 14, 2020

@lbnascimento
Contributor

@mikhail-khalizev This issue has been fixed in master and its fix will be present in the next incremental release.

In the meantime, make sure the indexed field is always on the left part of the expression (e.g.: x.Id >= firstId instead of firstId <= x.Id).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lbnascimento@mikhail-khalizev

        Issue actions

          [BUG] Comparison with index not processed correctly · Issue #1684 · litedb-org/LiteDB