Litedb: [BUG] ArgumentException thrown when subtracting TimeSpan from DateTimeOffset in query

Created on 9 Oct 2020  路  2Comments  路  Source: mbdavid/LiteDB

Version
LiteDB: 5.0.9
OS: Windows 10 latest, Ubuntu 18.04
.NET: netcoreapp3.1

Describe the bug

An ArgumentException is thrown when querying collections that use the minus - operator where the operands are a DateTimeOffset and a TimeSpan.

Code to Reproduce

    public class TestType
    {
        [BsonId]
        public int Id { get; set; }
        [BsonField]
        public string Foo { get; set; }
        [BsonField]
        public DateTimeOffset Timestamp { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var db = new LiteDatabase("testdb.db");
            var collection = db.GetCollection<TestType>(nameof(TestType));

            // sample data
            collection.Insert(new TestType()
            {
                Foo = "abc",
                Timestamp = DateTimeOffset.UtcNow,
            });
            collection.Insert(new TestType()
            {
                Foo = "def",
                Timestamp = DateTimeOffset.UtcNow,
            });

            // filter from 1 hour in the past to 1 hour in the future
            var timeRange = TimeSpan.FromHours(2);

            var working = collection // works, working should be of count = 2
                .Find(x => x.Timestamp > DateTimeOffset.UtcNow.Add(-timeRange))
                .ToList();

            var broken = collection // throws exception
                .Find(x => x.Timestamp > (DateTimeOffset.UtcNow - timeRange))
                .ToList();
        }

Expected behavior
Expected behavior is that the lists working and broken should both contain 2 results, and that no exceptions should be thrown. The usual built in operator for DateTimeOffset and TimeSpan should be used.

Screenshots/Stacktrace

   at System.DateTime.Add(Double value, Int32 scale)
   at System.DateTime.AddDays(Double value)
   at LiteDB.BsonExpressionOperators.MINUS(BsonValue left, BsonValue right)
   at LiteDB.BsonExpression.ExecuteScalar(IEnumerable`1 source, BsonDocument root, BsonValue current, Collation collation)
   at LiteDB.BsonExpression.ExecuteScalar(BsonDocument root, Collation collation)
   at LiteDB.Engine.BasePipe.<Filter>d__8.MoveNext()
   at LiteDB.Engine.QueryPipe.<Select>d__2.MoveNext()
   at LiteDB.Engine.QueryExecutor.<>c__DisplayClass10_0.<<ExecuteQuery>g__RunQuery|0>d.MoveNext()
   at LiteDB.BsonDataReader..ctor(IEnumerable`1 values, String collection)
   at LiteDB.Engine.QueryExecutor.ExecuteQuery(Boolean executionPlan)
   at LiteDB.Engine.QueryExecutor.ExecuteQuery()
   at LiteDB.Engine.LiteEngine.Query(String collection, Query query)
   at LiteDB.LiteQueryable`1.<ToDocuments>d__26.MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at testlitedbbug.Program.Main(String[] args) in C:\Users\Chris\source\repos\testlitedbbug\testlitedbbug\Program.cs:line 46

Additional context
I recently migrated from LiteDb version 4.1.4, and this code was working.

bug

All 2 comments

It looks like Error occurs when long value (Ticks from Timespan) is being tried to cast to double.
Is it okay if I take over this bug and submit a pull request ?

@Chris-Johnston Thanks to @mshambharkar 's PR, it is now fixed in the latest master. The fix will be present in the next incremental release.

Was this page helpful?
0 / 5 - 0 ratings