private object CosmosElementToPartitionKeyObject(CosmosElement cosmosElement) { // TODO: Leverage original serialization and avoid re-serialization (bug) switch (cosmosElement.Type) { case CosmosElementType.String: CosmosString cosmosString = cosmosElement as CosmosString; return(cosmosString.Value); case CosmosElementType.Number: CosmosNumber cosmosNumber = cosmosElement as CosmosNumber; return(cosmosNumber.AsFloatingPoint()); case CosmosElementType.Boolean: CosmosBoolean cosmosBool = cosmosElement as CosmosBoolean; return(cosmosBool.Value); case CosmosElementType.Guid: CosmosGuid cosmosGuid = cosmosElement as CosmosGuid; return(cosmosGuid.Value); default: throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, RMResources.UnsupportedPartitionKeyComponentValue, cosmosElement)); } }
private PartitionKey CosmosElementToPartitionKeyObject(CosmosElement cosmosElement) { // TODO: Leverage original serialization and avoid re-serialization (bug) switch (cosmosElement.Type) { case CosmosElementType.String: CosmosString cosmosString = cosmosElement as CosmosString; return(new PartitionKey(cosmosString.Value)); case CosmosElementType.Number: CosmosNumber cosmosNumber = cosmosElement as CosmosNumber; double value = Number64.ToDouble(cosmosNumber.Value); return(new PartitionKey(value)); case CosmosElementType.Boolean: CosmosBoolean cosmosBool = cosmosElement as CosmosBoolean; return(new PartitionKey(cosmosBool.Value)); case CosmosElementType.Null: return(PartitionKey.Null); default: throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, RMResources.UnsupportedPartitionKeyComponentValue, cosmosElement)); } }
public override CosmosElement Visit(SqlInScalarExpression scalarExpression, CosmosElement document) { CosmosElement expression = scalarExpression.Needle.Accept(this, document); if (expression == Undefined) { return(Undefined); } HashSet <CosmosElement> items = new HashSet <CosmosElement>(); foreach (SqlScalarExpression item in scalarExpression.Haystack) { items.Add(item.Accept(this, document)); } bool contains = items.Contains(expression); if (scalarExpression.Not) { contains = !contains; } return(CosmosBoolean.Create(contains)); }
private static PartitionKey CosmosElementToPartitionKeyObject(IReadOnlyList <CosmosElement> cosmosElementList) { PartitionKeyBuilder partitionKeyBuilder = new PartitionKeyBuilder(); foreach (CosmosElement cosmosElement in cosmosElementList) { if (cosmosElement == null) { partitionKeyBuilder.AddNoneType(); } else { _ = cosmosElement switch { CosmosString cosmosString => partitionKeyBuilder.Add(cosmosString.Value), CosmosNumber cosmosNumber => partitionKeyBuilder.Add(Number64.ToDouble(cosmosNumber.Value)), CosmosBoolean cosmosBoolean => partitionKeyBuilder.Add(cosmosBoolean.Value), CosmosNull _ => partitionKeyBuilder.AddNullValue(), _ => throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, RMResources.UnsupportedPartitionKeyComponentValue, cosmosElement)), }; } } return(partitionKeyBuilder.Build()); }
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); }
private static CosmosElement PerformLogicalOr(CosmosElement left, CosmosElement right) { bool leftIsBoolean = left is CosmosBoolean; bool rightIsBoolean = right is CosmosBoolean; // If the expression is true || <anything>, then the result is true if (leftIsBoolean && (left as CosmosBoolean).Value) { return(CosmosBoolean.Create(true)); } if (rightIsBoolean && (right as CosmosBoolean).Value) { return(CosmosBoolean.Create(true)); } if (!leftIsBoolean) { return(Undefined); } if (!rightIsBoolean) { return(Undefined); } bool result = (left as CosmosBoolean).Value || (right as CosmosBoolean).Value; return(CosmosBoolean.Create(result)); }
public void SqlBetweenScalarExpressionTest() { SqlBetweenScalarExpression threeBetweenFourAndFive = SqlBetweenScalarExpression.Create( SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(4)), SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)), SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5)), not: false); AssertEvaluation(CosmosBoolean.Create(true), threeBetweenFourAndFive); SqlBetweenScalarExpression threeNotBetweenFourAndFive = SqlBetweenScalarExpression.Create( SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(4)), SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)), SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(5)), not: true); AssertEvaluation(CosmosBoolean.Create(false), threeNotBetweenFourAndFive); SqlBetweenScalarExpression trueBetweenTrueAndTrueNested = SqlBetweenScalarExpression.Create( SqlLiteralScalarExpression.Create(SqlBooleanLiteral.Create(true)), threeBetweenFourAndFive, threeBetweenFourAndFive, not: false); AssertEvaluation(CosmosBoolean.Create(true), trueBetweenTrueAndTrueNested); }
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}."); } }
public override CosmosElement Visit(SqlExistsScalarExpression scalarExpression, CosmosElement document) { // Only run on the current document since the subquery is always correlated. IEnumerable <CosmosElement> subqueryResults = SqlInterpreter.ExecuteQuery( new CosmosElement[] { document }, scalarExpression.Subquery); return(CosmosBoolean.Create(subqueryResults.Any())); }
public TryCatch <object> Visit(CosmosBoolean cosmosBoolean, Type type) { if (type != typeof(bool)) { return(TryCatch <object> .FromException(Visitor.Exceptions.ExpectedBoolean)); } return(TryCatch <object> .FromResult(cosmosBoolean.Value?Visitor.BoxedValues.True : Visitor.BoxedValues.False)); }
public UInt128 Visit(CosmosBoolean cosmosBoolean, UInt128 seed) { if (seed == RootHashSeed) { return(cosmosBoolean.Value ? RootCache.True : RootCache.False); } return(MurmurHash3.Hash128( cosmosBoolean.Value ? HashSeeds.True : HashSeeds.False, seed)); }
public void CONTAINS() { // CONTAINS("hello", "") // -> all strings contain empty string. AssertEvaluation( expected: CosmosBoolean.Create(true), sqlScalarExpression: SqlFunctionCallScalarExpression.CreateBuiltin( SqlFunctionCallScalarExpression.Identifiers.Contains, SqlLiteralScalarExpression.Create(SqlStringLiteral.Create("Hello")), SqlLiteralScalarExpression.Create(SqlStringLiteral.Create(string.Empty)))); }
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}."), });
private static CosmosElement PerformBinaryEquality( Func <bool, bool> equalityFunction, CosmosElement left, CosmosElement right) { if ((left == Undefined) || (right == Undefined)) { return(Undefined); } return(CosmosBoolean.Create(equalityFunction(left == right))); }
public override CosmosElement GetCosmosElementContinuationToken() { Dictionary <string, CosmosElement> dictionary = new Dictionary <string, CosmosElement>(); dictionary.Add(nameof(this.initialized), CosmosBoolean.Create(this.initialized)); if (this.value != null) { dictionary.Add(nameof(this.value), this.value); } return(CosmosObject.Create(dictionary)); }
private static CosmosElement PerformUnaryBooleanOperation( Func <bool, bool> unaryOperation, CosmosElement operand) { if (!(operand is CosmosBoolean operandAsBoolean)) { return(Undefined); } bool result = unaryOperation(operandAsBoolean.Value); return(CosmosBoolean.Create(result)); }
private static CosmosElement PerformBinaryInequality( Func <int, bool> inequalityFunction, CosmosElement left, CosmosElement right) { if (!Utils.TryCompare(left, right, out int comparison)) { return(Undefined); } bool result = inequalityFunction(comparison); return(CosmosBoolean.Create(result)); }
public void TestBool() { { ReadOnlyMemory <byte> result = JsonSerializer.Serialize(true); CosmosBoolean cosmosBoolean = (CosmosBoolean)CosmosElement.CreateFromBuffer(result); Assert.IsTrue(cosmosBoolean.Value); } { ReadOnlyMemory <byte> result = JsonSerializer.Serialize(false); CosmosBoolean cosmosBoolean = (CosmosBoolean)CosmosElement.CreateFromBuffer(result); Assert.IsFalse(cosmosBoolean.Value); } }
public TryCatch <object> Visit(CosmosArray cosmosArray, Type type) { bool isReadOnlyList = type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IReadOnlyList <>)); if (!isReadOnlyList) { return(TryCatch <object> .FromException(Visitor.Exceptions.ExpectedArray)); } Type genericArgumentType = type.GenericTypeArguments.First(); Type listType = typeof(List <>).MakeGenericType(genericArgumentType); IList list = (IList)Activator.CreateInstance(listType); foreach (CosmosElement arrayItem in cosmosArray) { TryCatch <object> tryGetMaterializedArrayItem; if (genericArgumentType == typeof(object)) { Type dotNetType = arrayItem switch { CosmosArray _ => typeof(IReadOnlyList <object>), CosmosBoolean _ => typeof(bool), CosmosNull _ => typeof(object), CosmosNumber _ => typeof(Number64), CosmosObject _ => typeof(object), CosmosString _ => typeof(string), CosmosGuid _ => typeof(Guid), CosmosBinary _ => typeof(ReadOnlyMemory <byte>), _ => throw new ArgumentOutOfRangeException($"Unknown cosmos element type."), }; tryGetMaterializedArrayItem = arrayItem.Accept(this, dotNetType); } else { tryGetMaterializedArrayItem = arrayItem.Accept(this, genericArgumentType); } if (tryGetMaterializedArrayItem.Failed) { return(tryGetMaterializedArrayItem); } list.Add(tryGetMaterializedArrayItem.Result); } return(TryCatch <object> .FromResult(list)); }
/// <summary> /// Adds a JToken to this map if it hasn't already been added. /// </summary> /// <param name="cosmosElement">The element to add.</param> /// <param name="hash">The hash of the token.</param> /// <returns>Whether or not the item was added to this Distinct Map.</returns> public override bool Add(CosmosElement cosmosElement, out UInt128 hash) { // Unordered distinct does not need to return a valid hash. // Since it doesn't need the last hash for a continuation. hash = default; return(cosmosElement switch { CosmosArray cosmosArray => this.AddArrayValue(cosmosArray), CosmosBoolean cosmosBoolean => this.AddSimpleValue(cosmosBoolean.Value ? SimpleValues.True : SimpleValues.False), CosmosNull _ => this.AddSimpleValue(SimpleValues.Null), CosmosNumber cosmosNumber => this.AddNumberValue(cosmosNumber.Value), CosmosObject cosmosObject => this.AddObjectValue(cosmosObject), CosmosString cosmosString => this.AddStringValue(cosmosString.Value), _ => throw new ArgumentOutOfRangeException($"Unexpected {nameof(CosmosElement)}: {cosmosElement}"), });
private static IReadOnlyList <(string serializedToken, CosmosElement element)> TokenTestData() { Guid guid = Guid.Parse("69D5AB17-C94A-4173-A278-B59D0D9C7C37"); byte[] randomBytes = guid.ToByteArray(); string hexString = PartitionKeyInternal.HexConvert.ToHex(randomBytes, 0, randomBytes.Length); return(new List <(string, CosmosElement)> { ("[42, 37]", CosmosArray.Parse("[42, 37]")), ($@"{{C_Binary(""0x{hexString}"")}}", CosmosBinary.Create(new ReadOnlyMemory <byte>(randomBytes))), ("false", CosmosBoolean.Create(false)), ($@"{{C_Guid(""{guid}"")}}", CosmosGuid.Create(guid)), ("null", CosmosNull.Create()), ("1", CosmosInt64.Create(1)), ("{\"foo\": false}", CosmosObject.Parse("{\"foo\": false}")), ("asdf", CosmosString.Create("asdf")) });
public void Singletons() { List <Input> inputs = new List <Input>() { new Input( description: "Undefined", partitionKeyValue: null), new Input( description: "null", partitionKeyValue: CosmosNull.Create()), new Input( description: "true", partitionKeyValue: CosmosBoolean.Create(true)), new Input( description: "false", partitionKeyValue: CosmosBoolean.Create(false)), }; this.ExecuteTestSuite(inputs); }
public void SqlInScalarExpressionTest() { SqlLiteralScalarExpression one = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)); SqlLiteralScalarExpression two = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2)); SqlLiteralScalarExpression three = SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)); SqlInScalarExpression oneInOneTwoThree = SqlInScalarExpression.Create(one, false, one, two, three); AssertEvaluation(CosmosBoolean.Create(true), oneInOneTwoThree); SqlInScalarExpression oneNotInOneTwoThree = SqlInScalarExpression.Create(one, true, one, two, three); AssertEvaluation(CosmosBoolean.Create(false), oneNotInOneTwoThree); SqlInScalarExpression oneInTwoThree = SqlInScalarExpression.Create(one, false, two, three); AssertEvaluation(CosmosBoolean.Create(false), oneInTwoThree); SqlInScalarExpression oneNotInTwoThree = SqlInScalarExpression.Create(one, true, two, three); AssertEvaluation(CosmosBoolean.Create(true), oneNotInTwoThree); }
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); }
private static void VisitCosmosBoolean(CosmosBoolean cosmosBoolean, IJsonWriter jsonWriter) { jsonWriter.WriteBoolValue(cosmosBoolean.Value); }
public UInt128 Visit(CosmosBoolean cosmosBoolean, UInt128 seed) { return(MurmurHash3.Hash128( cosmosBoolean.Value ? CosmosElementHasher.TrueHashSeed : CosmosElementHasher.FalseHashSeed, seed)); }
public override CosmosElement Visit(SqlBooleanLiteral literal) => CosmosBoolean.Create(literal.Value);
public CosmosElement Visit(CosmosBoolean cosmosBoolean, CosmosElement indexer) => Undefined;
public bool Visit(CosmosBoolean cosmosBoolean) => false;