Azure / azure-cosmos-dotnet-v2

Contains samples and utilities relating to the Azure Cosmos DB .NET SDK
MIT License
578 stars 836 forks source link

Bitwise query operators not working for values larger than 30 bits #329

Open ghost opened 7 years ago

ghost commented 7 years ago

Has anyone seen issues with the Bitwise query not working correctly with larger values? I created a test that creates a document with a long value of a power of two then does a query using the bitwise operator against the value and it only works up to 1073741824. After that the record is not located.

Seems that it works to the 30th bit and then no longer. I didn't find this limit documented, can you confirm and document please?

bchong95 commented 7 years ago

Hi do you by any chance have a reproduce with specific numbers? I tried the max int like so:

select 
(2147483647 ^ 2147483647), 
(2147483647 & 2147483647), 
(2147483647 | 2147483647)

and got back the following results

[{
  "$1": 0,
  "$2": 2147483647,
  "$3": 2147483647
}]
ghost commented 7 years ago

Sure, maybe you can see my error, here's the code:

        var rr = await _client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("MyDb"), appCollection);

        for (long l = 1; l != 0; l = l << 1)
        {
            var t1 = new Test() { Partition = "a", Name = "b", lValue = l };
            var rr1 = await _client.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri("MyDb", "Test"), t1);
        }

        for (long l = 1; l != 0; l = l << 1)
        {
            var q = _client.CreateDocumentQuery<Test>(UriFactory.CreateDocumentCollectionUri("MyDb", "Test"), $"SELECT * FROM c WHERE ((c.lvalue & {l}) > 0)").AsEnumerable().FirstOrDefault();
            if (null != q)
            {
                Console.WriteLine($"Bitwise works for {l}.");
            }
            else
                Console.WriteLine($"Bitwise didn't work for {l}.");
        }

and the output:

Bitwise works for 1.

Bitwise works for 2.

Bitwise works for 4.

Bitwise works for 8.

Bitwise works for 16.

Bitwise works for 32.

Bitwise works for 64.

Bitwise works for 128.

Bitwise works for 256.

Bitwise works for 512.

Bitwise works for 1024.

Bitwise works for 2048.

Bitwise works for 4096.

Bitwise works for 8192.

Bitwise works for 16384.

Bitwise works for 32768.

Bitwise works for 65536.

Bitwise works for 131072.

Bitwise works for 262144.

Bitwise works for 524288.

Bitwise works for 1048576.

Bitwise works for 2097152.

Bitwise works for 4194304.

Bitwise works for 8388608.

Bitwise works for 16777216.

Bitwise works for 33554432.

Bitwise works for 67108864.

Bitwise works for 134217728.

Bitwise works for 268435456.

Bitwise works for 536870912.

Bitwise works for 1073741824.

Bitwise didn't work for 2147483648.

Bitwise didn't work for 4294967296.

Bitwise didn't work for 8589934592.

Bitwise didn't work for 17179869184.

Bitwise didn't work for 34359738368.

Bitwise didn't work for 68719476736.

Bitwise didn't work for 137438953472.

Bitwise didn't work for 274877906944.

Bitwise didn't work for 549755813888.

Bitwise didn't work for 1099511627776.

Bitwise didn't work for 2199023255552.

Bitwise didn't work for 4398046511104.

Bitwise didn't work for 8796093022208.

Bitwise didn't work for 17592186044416.

Bitwise didn't work for 35184372088832.

Bitwise didn't work for 70368744177664.

Bitwise didn't work for 140737488355328.

Bitwise didn't work for 281474976710656.

Bitwise didn't work for 562949953421312.

Bitwise didn't work for 1125899906842624.

Bitwise didn't work for 2251799813685248.

Bitwise didn't work for 4503599627370496.

Bitwise didn't work for 9007199254740992.

Bitwise didn't work for 18014398509481984.

Bitwise didn't work for 36028797018963968.

Bitwise didn't work for 72057594037927936.

Bitwise didn't work for 144115188075855872.

Bitwise didn't work for 288230376151711744.

Bitwise didn't work for 576460752303423488.

Bitwise didn't work for 1152921504606846976.

Bitwise didn't work for 2305843009213693952.

Bitwise didn't work for 4611686018427387904.

Bitwise didn't work for -9223372036854775808.


From: Brandon Chong notifications@github.com Sent: Friday, September 15, 2017 1:14 PM To: Azure/azure-documentdb-dotnet Cc: Todd Abel; Author Subject: Re: [Azure/azure-documentdb-dotnet] Bitwise query operators not working for values larger than 30 bits (#329)

Hi do you by any chance have a reproduce with specific numbers? I tried the max int like so:

select (2147483647 ^ 2147483647), (2147483647 & 2147483647), (2147483647 | 2147483647)

and got back the following results

[{ "$1": 0, "$2": 2147483647, "$3": 2147483647 }]

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-documentdb-dotnet/issues/329#issuecomment-329890520, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AEQvD-VVUcCnnticlFtYWeBarnom-G9tks5sitqygaJpZM4PDzKF.

seesharprun commented 2 years ago

The blog post when bitwise operators were announced mentions that it works just like C# bitwise operators.

In some rough testing, I believe that up to 32-bit unsigned integers will work (max: 4,294,967,295)

We are also discussing this in MicrosoftDocs/azure-docs#81973 and are waiting on a confirmation from an engineer to the SQL query side.