public async Task BasicTests()
        {
            AggressivePrefetchTestCase[] testCases = new[]
            {
                MakeTest(
                    query: "SELECT VALUE COUNT(1) FROM c",
                    continuationCount: 3,
                    partitionCount: 3,
                    expectedDocument: CosmosNumber64.Create(DocumentCount)),
                MakeTest(
                    query: "SELECT VALUE MAX(c.pk) FROM c",
                    continuationCount: 3,
                    partitionCount: 3,
                    expectedDocument: CosmosNumber64.Create(DocumentCount)),
                MakeTest(
                    query: "SELECT VALUE MIN(c.pk) FROM c",
                    continuationCount: 3,
                    partitionCount: 3,
                    expectedDocument: CosmosNumber64.Create(1)),
                MakeTest(
                    query: "SELECT VALUE SUM(1) FROM c",
                    continuationCount: 3,
                    partitionCount: 3,
                    expectedDocument: CosmosNumber64.Create(DocumentCount))
            };

            foreach (AggressivePrefetchTestCase testCase in testCases)
            {
                await RunTest(testCase);
            }
        }
        public static CosmosElement ToCosmosElement(OrderByContinuationToken orderByContinuationToken)
        {
            CosmosElement        compositeContinuationToken = ParallelContinuationToken.ToCosmosElement(orderByContinuationToken.ParallelContinuationToken);
            List <CosmosElement> orderByItemsRaw            = new List <CosmosElement>();

            foreach (OrderByItem orderByItem in orderByContinuationToken.OrderByItems)
            {
                orderByItemsRaw.Add(OrderByItem.ToCosmosElement(orderByItem));
            }

            CosmosArray orderByItems = CosmosArray.Create(orderByItemsRaw);

            CosmosElement filter = orderByContinuationToken.Filter == null?CosmosNull.Create() : (CosmosElement)CosmosString.Create(orderByContinuationToken.Filter);

            CosmosObject cosmosObject = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { PropertyNames.CompositeToken, compositeContinuationToken },
                { PropertyNames.OrderByItems, orderByItems },
                { PropertyNames.Rid, CosmosString.Create(orderByContinuationToken.Rid) },
                { PropertyNames.SkipCount, CosmosNumber64.Create(orderByContinuationToken.SkipCount) },
                { PropertyNames.Filter, filter },
            });

            return(cosmosObject);
        }
        public void SqlUnaryScalarExpressionTest()
        {
            SqlLiteralScalarExpression five        = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5));
            SqlLiteralScalarExpression trueBoolean = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.True);

            {
                SqlUnaryScalarExpression bitwiseNot = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.BitwiseNot, five);
                AssertEvaluation(CosmosNumber64.Create(~5), bitwiseNot);
            }

            {
                SqlLiteralScalarExpression largeNumber = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(130679749712577953));
                SqlUnaryScalarExpression   bitwiseNot  = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.BitwiseNot, largeNumber);
                AssertEvaluation(CosmosNumber64.Create(-1022657953), bitwiseNot);
            }

            {
                SqlUnaryScalarExpression not = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Not, trueBoolean);
                AssertEvaluation(CosmosBoolean.Create(!true), not);
            }

            {
                SqlUnaryScalarExpression minus = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Minus, five);
                AssertEvaluation(CosmosNumber64.Create(-5), minus);
            }

            {
                SqlUnaryScalarExpression plus = SqlUnaryScalarExpression.Create(SqlUnaryScalarOperatorKind.Plus, five);
                AssertEvaluation(CosmosNumber64.Create(5), plus);
            }
        }
        public void SqlLiteralScalarExpressionTest()
        {
            SqlLiteralScalarExpression numberLiteral = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1));

            AssertEvaluation(CosmosNumber64.Create(1), numberLiteral);

            SqlLiteralScalarExpression stringLiteral = SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("Hello"));

            AssertEvaluation(CosmosString.Create("Hello"), stringLiteral);

            SqlLiteralScalarExpression trueLiteral = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.True);

            AssertEvaluation(CosmosBoolean.Create(true), trueLiteral);

            SqlLiteralScalarExpression falseLiteral = SqlLiteralScalarExpression.Create(SqlBooleanLiteral.False);

            AssertEvaluation(CosmosBoolean.Create(false), falseLiteral);

            SqlLiteralScalarExpression nullLiteral = SqlLiteralScalarExpression.Create(SqlNullLiteral.Singleton);

            AssertEvaluation(CosmosNull.Create(), nullLiteral);

            SqlLiteralScalarExpression undefinedLiteral = SqlLiteralScalarExpression.Create(SqlUndefinedLiteral.Create());

            AssertEvaluation(Undefined, undefinedLiteral);
        }
        public void SqlArrayCreateScalarExpressionTest()
        {
            SqlArrayCreateScalarExpression inner = SqlArrayCreateScalarExpression.Create(
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)));

            AssertEvaluation(
                CosmosArray.Create(
                    new List <CosmosElement>()
            {
                CosmosNumber64.Create(1),
                CosmosNumber64.Create(2),
                CosmosNumber64.Create(3),
            }),
                inner);

            SqlArrayCreateScalarExpression outer = SqlArrayCreateScalarExpression.Create(inner);

            AssertEvaluation(
                CosmosArray.Create(
                    new List <CosmosElement>()
            {
                CosmosArray.Create(
                    new List <CosmosElement>()
                {
                    CosmosNumber64.Create(1),
                    CosmosNumber64.Create(2),
                    CosmosNumber64.Create(3),
                })
            }),
                outer);
        }
Exemplo n.º 6
0
        private CosmosElement CreateVertexPropertyPrimitiveValueElement(object value)
        {
            switch (value)
            {
            case bool boolValue:
                return(CosmosBoolean.Create(boolValue));

            case double doubleValue:
                return(CosmosNumber64.Create(doubleValue));

            case float floatValue:
                return(CosmosNumber64.Create(floatValue));

            case int intValue:
                return(CosmosNumber64.Create(intValue));

            case long longValue:
                return(CosmosNumber64.Create(longValue));

            case string stringValue:
                return(CosmosString.Create(stringValue));

            default:
                throw new AssertFailedException($"Invalid Gremlin property value object type: {value.GetType().Name}.");
            }
        }
Exemplo n.º 7
0
            public UInt128 Visit(CosmosNumber64 cosmosNumber64, UInt128 seed)
            {
                UInt128  hash  = seed == RootHashSeed ? RootCache.Number64 : MurmurHash3.Hash128(HashSeeds.Number64, seed);
                Number64 value = cosmosNumber64.GetValue();

                Number64.DoubleEx doubleExValue = Number64.ToDoubleEx(value);
                return(MurmurHash3.Hash128(doubleExValue, hash));
            }
Exemplo n.º 8
0
 public void ABS()
 {
     AssertEvaluation(
         CosmosNumber64.Create(Math.Abs(5)),
         SqlFunctionCallScalarExpression.CreateBuiltin(
             SqlFunctionCallScalarExpression.Identifiers.Abs,
             five));
 }
Exemplo n.º 9
0
        public void TestCosmosElement()
        {
            CosmosNumber64        cosmosElement = CosmosNumber64.Create(42);
            ReadOnlyMemory <byte> result        = JsonSerializer.Serialize(cosmosElement);
            CosmosNumber64        readValue     = (CosmosNumber64)CosmosElement.CreateFromBuffer(result);

            Assert.AreEqual(expected: cosmosElement.Value, actual: readValue.Value);
        }
Exemplo n.º 10
0
        public void TestNumber()
        {
            {
                Number64 value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosNumber64        cosmosNumber = (CosmosNumber64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                sbyte value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt8            cosmosNumber = (CosmosInt8)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                short value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt16           cosmosNumber = (CosmosInt16)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                int value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt32           cosmosNumber = (CosmosInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                long value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosInt64           cosmosNumber = (CosmosInt64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                uint value = 32;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosUInt32          cosmosNumber = (CosmosUInt32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                float value = 32.1337f;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat32         cosmosNumber = (CosmosFloat32)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }

            {
                double value = 32.1337;
                ReadOnlyMemory <byte> result       = JsonSerializer.Serialize(value);
                CosmosFloat64         cosmosNumber = (CosmosFloat64)CosmosElement.CreateFromBuffer(result);
                Assert.AreEqual(expected: value, actual: cosmosNumber.Value);
            }
        }
Exemplo n.º 11
0
            /// <summary>
            /// Returns the average or undefined if any of the intermediate averages resulted in an undefined value.
            /// </summary>
            /// <returns>The average or undefined if any of the intermediate averages resulted in an undefined value.</returns>
            public CosmosNumber GetAverage()
            {
                if (!this.Sum.HasValue || this.Count <= 0)
                {
                    return(null);
                }

                return(CosmosNumber64.Create(this.Sum.Value / this.Count));
            }
Exemplo n.º 12
0
            public UInt128 Visit(CosmosNumber64 cosmosNumber64, UInt128 seed)
            {
                UInt128     hash   = MurmurHash3.Hash128(CosmosNumberHasher.Number64HashSeed, seed);
                Number64    value  = cosmosNumber64.GetValue();
                Span <byte> buffer = stackalloc byte[Number64.SizeOf];

                value.CopyTo(buffer);
                return(MurmurHash3.Hash128(buffer, hash));
            }
        /// <summary>
        /// Gets the current sum.
        /// </summary>
        /// <returns>The current sum.</returns>
        public CosmosElement GetResult()
        {
            if (double.IsNaN(this.globalSum))
            {
                return(null);
            }

            return(CosmosNumber64.Create(this.globalSum));
        }
Exemplo n.º 14
0
        public void Numbers()
        {
            List <Input> inputs = new List <Input>()
            {
                new Input(
                    description: "positive zero",
                    partitionKeyValue: CosmosNumber64.Create(0.0)),
                new Input(
                    description: "negative zero",
                    partitionKeyValue: CosmosNumber64.Create(-0.0)),
                new Input(
                    description: "positive number",
                    partitionKeyValue: CosmosNumber64.Create(1)),
                new Input(
                    description: "negative number",
                    partitionKeyValue: CosmosNumber64.Create(-1)),
                new Input(
                    description: nameof(double.Epsilon),
                    partitionKeyValue: CosmosNumber64.Create(double.Epsilon)),
                new Input(
                    description: nameof(double.MaxValue),
                    partitionKeyValue: CosmosNumber64.Create(double.MaxValue)),
                new Input(
                    description: nameof(double.MinValue),
                    partitionKeyValue: CosmosNumber64.Create(double.MinValue)),
                new Input(
                    description: nameof(double.NaN),
                    partitionKeyValue: CosmosNumber64.Create(double.NaN)),
                new Input(
                    description: "long " + nameof(double.NegativeInfinity),
                    partitionKeyValue: CosmosNumber64.Create(double.NegativeInfinity)),
                new Input(
                    description: "long " + nameof(double.PositiveInfinity),
                    partitionKeyValue: CosmosNumber64.Create(double.PositiveInfinity)),
                new Input(
                    description: "long " + nameof(long.MaxValue),
                    partitionKeyValue: CosmosNumber64.Create(long.MaxValue)),
                new Input(
                    description: "long " + nameof(long.MaxValue) + " minus 1",
                    partitionKeyValue: CosmosNumber64.Create(long.MaxValue - 1)),
                new Input(
                    description: "long " + nameof(long.MinValue),
                    partitionKeyValue: CosmosNumber64.Create(long.MinValue)),
                new Input(
                    description: nameof(MaxSafeInteger),
                    partitionKeyValue: CosmosNumber64.Create(MaxSafeInteger)),
                new Input(
                    description: nameof(MaxSafeInteger) + " Minus 1",
                    partitionKeyValue: CosmosNumber64.Create(MaxSafeInteger - 1)),
                new Input(
                    description: nameof(MaxSafeInteger) + " Plus 1",
                    partitionKeyValue: CosmosNumber64.Create(MaxSafeInteger + 1)),
            };

            this.ExecuteTestSuite(inputs);
        }
Exemplo n.º 15
0
 public static CosmosElement ToCosmosElement(VersionedAndRidCheckedCompositeToken token)
 {
     return(CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
     {
         { PropertyNames.Version, CosmosNumber64.Create((long)token.VersionNumber) },
         { PropertyNames.Rid, CosmosString.Create(token.Rid) },
         { PropertyNames.Continuation, token.ContinuationToken },
     }));
 }
 private static QueryState CreateQueryState(int count)
 {
     return(new QueryState(
                CosmosString.Create(
                    CosmosObject.Create(
                        new Dictionary <string, CosmosElement>()
     {
         { "continuationCount", CosmosNumber64.Create(++count) },
     })
                    .ToString())));
 }
Exemplo n.º 17
0
 private CosmosElement CreateVertexPropertyPrimitiveValueElement(object value)
 {
     return(value switch
     {
         bool boolValue => CosmosBoolean.Create(boolValue),
         double doubleValue => CosmosNumber64.Create(doubleValue),
         float floatValue => CosmosNumber64.Create(floatValue),
         int intValue => CosmosNumber64.Create(intValue),
         long longValue => CosmosNumber64.Create(longValue),
         string stringValue => CosmosString.Create(stringValue),
         _ => throw new AssertFailedException($"Invalid Gremlin property value object type: {value.GetType().Name}."),
     });
Exemplo n.º 18
0
        private static CosmosElement PerformUnaryNumberOperation(
            Func <double, double> unaryOperation,
            CosmosElement operand)
        {
            if (!(operand is CosmosNumber operandAsNumber))
            {
                return(Undefined);
            }

            double result = unaryOperation(Number64.ToDouble(operandAsNumber.Value));

            return(CosmosNumber64.Create(result));
        }
            public static CosmosElement ToCosmosElement(AverageInfo averageInfo)
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>();

                if (averageInfo.Sum.HasValue)
                {
                    dictionary.Add(AverageInfo.SumName, CosmosNumber64.Create(averageInfo.Sum.Value));
                }

                dictionary.Add(AverageInfo.CountName, CosmosNumber64.Create(averageInfo.Count));

                return(CosmosObject.Create(dictionary));
            }
Exemplo n.º 20
0
                public static CosmosElement ToCosmosElement(TakeContinuationToken takeContinuationToken)
                {
                    Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
                    {
                        {
                            TakeContinuationToken.PropertyNames.SourceToken,
                            takeContinuationToken.SourceToken
                        },
                        {
                            TakeContinuationToken.PropertyNames.TakeCount,
                            CosmosNumber64.Create(takeContinuationToken.TakeCount)
                        },
                    };

                    return(CosmosObject.Create(dictionary));
                }
Exemplo n.º 21
0
                public static CosmosElement ToCosmosElement(OffsetContinuationToken offsetContinuationToken)
                {
                    Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
                    {
                        {
                            OffsetContinuationToken.ProperytNames.SkipCountProperty,
                            CosmosNumber64.Create(offsetContinuationToken.Offset)
                        },
                        {
                            OffsetContinuationToken.ProperytNames.SourceTokenProperty,
                            offsetContinuationToken.SourceToken
                        }
                    };

                    return(CosmosObject.Create(dictionary));
                }
Exemplo n.º 22
0
        private static CosmosElement PerformBinaryNumberOperation(
            Func <double, double, double> operation,
            CosmosElement left,
            CosmosElement right)
        {
            if (!(left is CosmosNumber leftAsNumber))
            {
                return(Undefined);
            }

            if (!(right is CosmosNumber rightAsNumber))
            {
                return(Undefined);
            }

            double result = operation(Number64.ToDouble(leftAsNumber.Value), Number64.ToDouble(rightAsNumber.Value));

            return(CosmosNumber64.Create(result));
        }
Exemplo n.º 23
0
        public void SqlSelectValueSpecTest()
        {
            CosmosObject john = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["name"] = CosmosString.Create("John"),
                ["age"]  = CosmosNumber64.Create(25)
            });

            CosmosObject johnWrapped = CosmosObject.Create(new Dictionary <string, CosmosElement>
            {
                ["c"] = john
            });

            SqlScalarExpression cDotName = TestUtils.CreatePathExpression("c", "name");

            SqlSelectValueSpec valueSpec = SqlSelectValueSpec.Create(cDotName);

            AssertEvaluation(CosmosString.Create("John"), valueSpec, johnWrapped);
        }
        public void SqlCoalesceScalarExpressionTest()
        {
            SqlLiteralScalarExpression nullLiteral      = SqlLiteralScalarExpression.Create(SqlNullLiteral.Singleton);
            SqlLiteralScalarExpression undefinedLiteral = SqlLiteralScalarExpression.Create(SqlUndefinedLiteral.Create());
            SqlLiteralScalarExpression five             = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5));
            SqlLiteralScalarExpression three            = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3));

            SqlCoalesceScalarExpression nullCoalesceFive = SqlCoalesceScalarExpression.Create(nullLiteral, five);

            AssertEvaluation(CosmosNull.Create(), nullCoalesceFive);

            SqlCoalesceScalarExpression undefinedCoalesceFive = SqlCoalesceScalarExpression.Create(undefinedLiteral, five);

            AssertEvaluation(CosmosNumber64.Create(5), undefinedCoalesceFive);

            SqlCoalesceScalarExpression threeCoalesceFive = SqlCoalesceScalarExpression.Create(three, five);

            AssertEvaluation(CosmosNumber64.Create(3), threeCoalesceFive);
        }
Exemplo n.º 25
0
        public void ROUND()
        {
            // ROUND(4.84090499760142E+15)
            // offline engine has double precision errors
            AssertEvaluation(
                expected: CosmosNumber64.Create(4.84090499760142E+15),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.Round,
                    SqlLiteralScalarExpression.Create(
                        SqlNumberLiteral.Create(
                            JToken.Parse("4840904997601420").Value <double>()))));

            // ROUND(0.5, 1)
            // -> should round up
            AssertEvaluation(
                expected: CosmosNumber64.Create(1),
                sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.Round,
                    SqlLiteralScalarExpression.Create(
                        SqlNumberLiteral.Create(0.5))));
        }
Exemplo n.º 26
0
            public override CosmosElement GetCosmosElementContinuationToken()
            {
                Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>()
                {
                    {
                        UnorderdDistinctMap.PropertyNames.Numbers,
                        CosmosArray.Create(this.numbers.Select(x => CosmosNumber64.Create(x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength4,
                        CosmosArray.Create(this.stringsLength4.Select(x => CosmosUInt32.Create(x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength8,
                        CosmosArray.Create(this.stringsLength8.Select(x => CosmosInt64.Create((long)x)))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength16,
                        CosmosArray.Create(this.stringsLength16.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.StringsLength16Plus,
                        CosmosArray.Create(this.stringsLength16Plus.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.Arrays,
                        CosmosArray.Create(this.arrays.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.Object,
                        CosmosArray.Create(this.objects.Select(x => CosmosBinary.Create(UInt128.ToByteArray(x))))
                    },
                    {
                        UnorderdDistinctMap.PropertyNames.SimpleValues,
                        CosmosString.Create(this.simpleValues.ToString())
                    }
                };

                return(CosmosObject.Create(dictionary));
            }
        public void SqlSubqueryScalarExpressionTest()
        {
            SqlLiteralScalarExpression five  = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5));
            SqlLiteralScalarExpression three = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3));

            // (SELECT VALUE 5 + 3)
            SqlSubqueryScalarExpression subqueryScalarExpression = SqlSubqueryScalarExpression.Create(
                SqlQuery.Create(
                    SqlSelectClause.Create(
                        SqlSelectValueSpec.Create(
                            SqlBinaryScalarExpression.Create(
                                SqlBinaryScalarOperatorKind.Add,
                                five,
                                three))),
                    fromClause: null,
                    whereClause: null,
                    groupByClause: null,
                    orderByClause: null,
                    offsetLimitClause: null));

            AssertEvaluation(CosmosNumber64.Create(5 + 3), subqueryScalarExpression);
        }
        public void VerifyNumberSort()
        {
            List <CosmosElement> manuallySortedList = new List <CosmosElement>()
            {
                CosmosNumber64.Create(-1),
                CosmosNumber64.Create(0),
                CosmosNumber64.Create(1),

                CosmosInt8.Create(-1),
                CosmosInt8.Create(0),
                CosmosInt8.Create(1),

                CosmosInt16.Create(-1),
                CosmosInt16.Create(0),
                CosmosInt16.Create(1),

                CosmosInt32.Create(-1),
                CosmosInt32.Create(0),
                CosmosInt32.Create(1),


                CosmosInt64.Create(-1),
                CosmosInt64.Create(0),
                CosmosInt64.Create(1),

                CosmosUInt32.Create(0),
                CosmosUInt32.Create(1),

                CosmosFloat32.Create(-1),
                CosmosFloat32.Create(0),
                CosmosFloat32.Create(1),

                CosmosFloat64.Create(-1),
                CosmosFloat64.Create(0),
                CosmosFloat64.Create(1),
            };

            VerifySort(manuallySortedList);
        }
Exemplo n.º 29
0
        public async Task TestAggregateFunctionsAsync()
        {
            AggregateTestArgs args = new AggregateTestArgs()
            {
                NumberOfDocsWithSamePartitionKey       = 37,
                NumberOfDocumentsDifferentPartitionKey = 43,
                PartitionKey       = "key",
                UniquePartitionKey = "uniquePartitionKey",
                Field  = "field",
                Values = new object[] { false, true, "abc", "cdfg", "opqrs", "ttttttt", "xyz" },
            };

            List <string> documents = new List <string>(args.NumberOfDocumentsDifferentPartitionKey + args.NumberOfDocsWithSamePartitionKey);

            foreach (object val in args.Values)
            {
                Document doc;
                doc = new Document();
                doc.SetPropertyValue(args.PartitionKey, val);
                doc.SetPropertyValue("id", Guid.NewGuid().ToString());

                documents.Add(doc.ToString());
            }

            for (int i = 0; i < args.NumberOfDocsWithSamePartitionKey; ++i)
            {
                Document doc = new Document();
                doc.SetPropertyValue(args.PartitionKey, args.UniquePartitionKey);
                documents.Add(doc.ToString());
            }

            Random random = new Random();

            for (int i = 0; i < args.NumberOfDocumentsDifferentPartitionKey; ++i)
            {
                Document doc = new Document();
                doc.SetPropertyValue(args.PartitionKey, random.NextDouble());
                documents.Add(doc.ToString());
            }

            await this.CreateIngestQueryDeleteAsync <AggregateTestArgs>(
                ConnectionModes.Direct | ConnectionModes.Gateway,
                CollectionTypes.SinglePartition | CollectionTypes.MultiPartition,
                documents,
                ImplementationAsync,
                args,
                "/" + args.PartitionKey);

            async Task ImplementationAsync(
                Container container,
                IReadOnlyList <CosmosObject> inputDocuments,
                AggregateTestArgs aggregateTestArgs)
            {
                IReadOnlyList <CosmosObject> documentsWherePkIsANumber = inputDocuments
                                                                         .Where(doc =>
                {
                    return(double.TryParse(
                               doc[aggregateTestArgs.PartitionKey].ToString(),
                               out double result));
                })
                                                                         .ToList();
                double numberSum = documentsWherePkIsANumber
                                   .Sum(doc =>
                {
                    if (!doc.TryGetValue(aggregateTestArgs.PartitionKey, out CosmosNumber number))
                    {
                        Assert.Fail("Failed to get partition key from document");
                    }

                    return(Number64.ToDouble(number.Value));
                });
                double count = documentsWherePkIsANumber.Count();

                AggregateQueryArguments[] aggregateQueryArgumentsList = new AggregateQueryArguments[]
                {
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "AVG",
                        ExpectedValue     = CosmosNumber64.Create(numberSum / count),
                        Predicate         = $"IS_NUMBER(r.{aggregateTestArgs.PartitionKey})",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "AVG",
                        ExpectedValue     = null,
                        Predicate         = "true",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "COUNT",
                        ExpectedValue     = CosmosNumber64.Create(documents.Count()),
                        Predicate         = "true",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "MAX",
                        ExpectedValue     = CosmosString.Create("xyz"),
                        Predicate         = "true",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "MIN",
                        ExpectedValue     = CosmosBoolean.Create(false),
                        Predicate         = "true",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "SUM",
                        ExpectedValue     = CosmosNumber64.Create(numberSum),
                        Predicate         = $"IS_NUMBER(r.{aggregateTestArgs.PartitionKey})",
                    },
                    new AggregateQueryArguments()
                    {
                        AggregateOperator = "SUM",
                        ExpectedValue     = null,
                        Predicate         = $"true",
                    },
                };

                foreach (int maxDoP in new[] { 0, 10 })
                {
                    foreach (AggregateQueryArguments argument in aggregateQueryArgumentsList)
                    {
                        string[] queryFormats = new[]
                        {
                            "SELECT VALUE {0}(r.{1}) FROM r WHERE {2}",
                            "SELECT VALUE {0}(r.{1}) FROM r WHERE {2} ORDER BY r.{1}"
                        };

                        foreach (string queryFormat in queryFormats)
                        {
                            string query = string.Format(
                                CultureInfo.InvariantCulture,
                                queryFormat,
                                argument.AggregateOperator,
                                aggregateTestArgs.PartitionKey,
                                argument.Predicate);
                            string message = string.Format(
                                CultureInfo.InvariantCulture,
                                "query: {0}, data: {1}",
                                query,
                                JsonConvert.SerializeObject(argument));

                            List <CosmosElement> items = await QueryTestsBase.RunQueryAsync(
                                container,
                                query,
                                new QueryRequestOptions()
                            {
                                MaxConcurrency = maxDoP,
                            });

                            if (argument.ExpectedValue == null)
                            {
                                Assert.AreEqual(0, items.Count, message);
                            }
                            else
                            {
                                Assert.AreEqual(1, items.Count, message);
                                CosmosElement expected = argument.ExpectedValue;
                                CosmosElement actual   = items.Single();

                                if ((expected is CosmosNumber expectedNumber) && (actual is CosmosNumber actualNumber))
                                {
                                    Assert.AreEqual(Number64.ToDouble(expectedNumber.Value), Number64.ToDouble(actualNumber.Value), .01);
                                }
Exemplo n.º 30
0
 public override CosmosElement Visit(SqlNumberLiteral literal) => CosmosNumber64.Create(literal.Value);