Skip to content
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

[BUG] Caching issue causes query to return wrong results #2271

Open
lapulpeta opened this issue Jan 15, 2023 · 0 comments
Open

[BUG] Caching issue causes query to return wrong results #2271

lapulpeta opened this issue Jan 15, 2023 · 0 comments
Labels

Comments

@lapulpeta
Copy link

Version
v5.0.15

Describe the bug
Query returns wrong results.

Code to Reproduce

using LiteDB;

var dbfile = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
try
{
    using (var database = new LiteDatabase(dbfile))
    {
        var acol = database.GetCollection<A>();

        const string id1 = "a";
        const string id2 = "b";

        acol.Upsert(new A()
        {
            Id = "1",
            ListB =
            {
                new B() { Key = "x", Values = { id1 } },
            }
        });
        acol.Upsert(new A()
        {
            Id = "2",
            ListB =
            {
                new B() { Key = "x", Values = { id2 } },
            }
        });

        List<A> alist;

        // Wrong results
        alist = acol.Query().Where(n => n.ListB.Where(t => t.Key == "x" && t.Values[0] == id1).Any()).ToList();
        Console.WriteLine(alist[0].Id); // Should output 1 => OK
        alist = acol.Query().Where(n => n.ListB.Where(t => t.Key == "x" && t.Values[0] == id2).Any()).ToList();
        Console.WriteLine(alist[0].Id); // Should output 2 but outputs 1 => WRONG
        // If we swap id1 and id2, we will get 2 in both outputs, which is also wrong

        // Correct results if we load all objects to memory and then run query
        alist = acol.Query().ToList().Where(n => n.ListB.Where(t => t.Key == "x" && t.Values[0] == id1).Any()).ToList();
        Console.WriteLine(alist[0].Id); // Should output 1 => OK
        alist = acol.Query().ToList().Where(n => n.ListB.Where(t => t.Key == "x" && t.Values[0] == id2).Any()).ToList();
        Console.WriteLine(alist[0].Id); // Should output 2 => OK

        Console.ReadKey();
    }
}
finally
{
    File.Delete(dbfile);
}

public class A
{
    public string Id { get; set; }
    public List<B> ListB { get; set; } = new();
}

public class B
{
    public string Key { get; set; }
    public List<string> Values { get; set; } = new();
}

Expected behavior
Query should return correct result.

Additional context
Apparently the inner function is using the parameters passed in the previous call due to the cache in BsonExpression. If we bypass the cache in BsonExpression.Compile() then the query returns the correct results.

@lapulpeta lapulpeta added the bug label Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant