public int CompareTo(CosmosArray cosmosArray)
        {
            UInt128 hash1 = DistinctHash.GetHash(this);
            UInt128 hash2 = DistinctHash.GetHash(cosmosArray);

            return(hash1.CompareTo(hash2));
        }
Esempio n. 2
0
        public int CompareTo(CosmosArray cosmosArray)
        {
            UInt128 hash1 = DistinctHash.GetHash(this);
            UInt128 hash2 = DistinctHash.GetHash(cosmosArray);

            return(UInt128BinaryComparer.Singleton.Compare(hash1, hash2));
        }
 private static void ExecuteBenchmark(CosmosElement cosmosElement)
 {
     for (int i = 0; i < 100000; i++)
     {
         DistinctHash.GetHash(cosmosElement);
     }
 }
            public void AddPayload(RewrittenGroupByProjection rewrittenGroupByProjection)
            {
                UInt128 groupByKeysHash = DistinctHash.GetHash(rewrittenGroupByProjection.GroupByItems);

                if (!this.table.TryGetValue(groupByKeysHash, out SingleGroupAggregator singleGroupAggregator))
                {
                    singleGroupAggregator = SingleGroupAggregator.TryCreate(
                        EmptyAggregateOperators,
                        this.groupByAliasToAggregateType,
                        this.orderedAliases,
                        this.hasSelectValue,
                        continuationToken: null).Result;
                    this.table[groupByKeysHash] = singleGroupAggregator;
                }

                CosmosElement payload = rewrittenGroupByProjection.Payload;

                singleGroupAggregator.AddValues(payload);
            }
Esempio n. 5
0
            public void AddPayload(RewrittenGroupByProjection rewrittenGroupByProjection)
            {
                // For VALUE queries the payload will be undefined if the field was undefined.
                if (rewrittenGroupByProjection.TryGetPayload(out CosmosElement payload))
                {
                    UInt128 groupByKeysHash = DistinctHash.GetHash(rewrittenGroupByProjection.GroupByItems);

                    if (!this.table.TryGetValue(groupByKeysHash, out SingleGroupAggregator singleGroupAggregator))
                    {
                        singleGroupAggregator = SingleGroupAggregator.TryCreate(
                            EmptyAggregateOperators,
                            this.groupByAliasToAggregateType,
                            this.orderedAliases,
                            this.hasSelectValue,
                            continuationToken: null).Result;
                        this.table[groupByKeysHash] = singleGroupAggregator;
                    }

                    singleGroupAggregator.AddValues(payload);
                }
            }
        /// <summary>
        /// Compares to objects and returns their partial sort relationship.
        /// </summary>
        /// <param name="element1">The first element to compare.</param>
        /// <param name="element2">The second element to compare.</param>
        /// <returns>
        /// Less than zero if obj1 comes before obj2 in the sort order.
        /// Zero if obj1 and obj2 are interchangeable in the sort order.
        /// Greater than zero if obj2 comes before obj1 in the sort order.
        /// </returns>
        public int Compare(CosmosElement element1, CosmosElement element2)
        {
            if (object.ReferenceEquals(element1, element2))
            {
                return(0);
            }

            if (object.ReferenceEquals(element1, MinValueItem.Singleton))
            {
                return(-1);
            }

            if (object.ReferenceEquals(element2, MinValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element1, MaxValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element2, MaxValueItem.Singleton))
            {
                return(-1);
            }

            if (element1 == Undefined)
            {
                return(-1);
            }

            if (element2 == Undefined)
            {
                return(1);
            }

            CosmosElementType type1 = element1.Type;

            int cmp = CompareTypes(element1, element2);

            if (cmp == 0)
            {
                // If they are the same type then you need to break the tie.
                switch (type1)
                {
                case CosmosElementType.Boolean:
                    cmp = Comparer <bool> .Default.Compare(
                        (element1 as CosmosBoolean).Value,
                        (element2 as CosmosBoolean).Value);

                    break;

                case CosmosElementType.Null:
                    // All nulls are the same.
                    cmp = 0;
                    break;

                case CosmosElementType.Number:
                    CosmosNumber number1 = element1 as CosmosNumber;
                    CosmosNumber number2 = element2 as CosmosNumber;
                    if (number1.NumberType == CosmosNumberType.Number64)
                    {
                        // Both are Number64, so compare as Number64
                        cmp = number1.Value.CompareTo(number2.Value);
                    }
                    else
                    {
                        // We have a number with precision
                        if (number2.Value.IsInteger)
                        {
                            // compare as longs, since all types have an implicit conversion with full fidelity.
                            long integer1 = Number64.ToLong(number1.Value);
                            long integer2 = Number64.ToLong(number2.Value);
                            cmp = Comparer <long> .Default.Compare(integer1, integer2);
                        }
                        else
                        {
                            // compare as doubles, since all types have an implicit conversion with full fidelity.
                            double double1 = Number64.ToDouble(number1.Value);
                            double double2 = Number64.ToDouble(number2.Value);
                            cmp = Comparer <double> .Default.Compare(double1, double2);
                        }
                    }

                    break;

                case CosmosElementType.String:
                    CosmosString string1 = element1 as CosmosString;
                    CosmosString string2 = element2 as CosmosString;
                    cmp = string.CompareOrdinal(
                        string1.Value,
                        string2.Value);
                    break;

                case CosmosElementType.Guid:
                    CosmosGuid guid1 = element1 as CosmosGuid;
                    CosmosGuid guid2 = element2 as CosmosGuid;
                    cmp = guid1.Value.CompareTo(guid2.Value);
                    break;

                case CosmosElementType.Binary:
                    CosmosBinary binary1 = element1 as CosmosBinary;
                    CosmosBinary binary2 = element2 as CosmosBinary;
                    cmp = ItemComparer.CompareTo(binary1, binary2);
                    break;

                case CosmosElementType.Array:
                case CosmosElementType.Object:
                {
                    UInt128 hash1 = DistinctHash.GetHash(element1);
                    UInt128 hash2 = DistinctHash.GetHash(element2);
                    return(hash1.CompareTo(hash2));
                }

                default:
                    throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {type1}");
                }
            }

            return(cmp);
        }
Esempio n. 7
0
        public override async Task <QueryResponseCore> DrainAsync(
            int maxElements,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Draining GROUP BY is broken down into two stages:
            QueryResponseCore response;

            if (!this.Source.IsDone)
            {
                // Stage 1:
                // Drain the groupings fully from all continuation and all partitions
                QueryResponseCore sourceResponse = await base.DrainAsync(int.MaxValue, cancellationToken);

                if (!sourceResponse.IsSuccess)
                {
                    return(sourceResponse);
                }

                foreach (CosmosElement result in sourceResponse.CosmosElements)
                {
                    // Aggregate the values for all groupings across all continuations.
                    RewrittenGroupByProjection groupByItem = new RewrittenGroupByProjection(result);
                    UInt192 groupByKeysHash = DistinctHash.GetHash(groupByItem.GroupByItems);

                    if (!this.groupingTable.TryGetValue(groupByKeysHash, out SingleGroupAggregator singleGroupAggregator))
                    {
                        singleGroupAggregator = SingleGroupAggregator.Create(
                            this.cosmosQueryClient,
                            EmptyAggregateOperators,
                            this.groupByAliasToAggregateType,
                            this.orderedAliases,
                            this.hasSelectValue,
                            continuationToken: null);
                        this.groupingTable[groupByKeysHash] = singleGroupAggregator;
                    }

                    CosmosElement payload = groupByItem.Payload;
                    singleGroupAggregator.AddValues(payload);
                }

                // We need to give empty pages until the results are fully drained.
                response = QueryResponseCore.CreateSuccess(
                    result: EmptyResults,
                    continuationToken: null,
                    disallowContinuationTokenMessage: GroupByDocumentQueryExecutionComponent.ContinuationTokenNotSupportedWithGroupBy,
                    activityId: sourceResponse.ActivityId,
                    requestCharge: sourceResponse.RequestCharge,
                    diagnostics: sourceResponse.Diagnostics,
                    responseLengthBytes: sourceResponse.ResponseLengthBytes);

                this.isDone = false;
            }
            else
            {
                // Stage 2:
                // Emit the results from the grouping table page by page
                IEnumerable <SingleGroupAggregator> groupByValuesList = this.groupingTable
                                                                        .OrderBy(kvp => kvp.Key)
                                                                        .Skip(this.numPagesDrainedFromGroupingTable * maxElements)
                                                                        .Take(maxElements)
                                                                        .Select(kvp => kvp.Value);

                List <CosmosElement> results = new List <CosmosElement>();
                foreach (SingleGroupAggregator groupByValues in groupByValuesList)
                {
                    results.Add(groupByValues.GetResult());
                }

                response = QueryResponseCore.CreateSuccess(
                    result: results,
                    continuationToken: null,
                    disallowContinuationTokenMessage: GroupByDocumentQueryExecutionComponent.ContinuationTokenNotSupportedWithGroupBy,
                    activityId: null,
                    requestCharge: 0,
                    diagnostics: QueryResponseCore.EmptyDiagnostics,
                    responseLengthBytes: 0);

                this.numPagesDrainedFromGroupingTable++;
                if (this.numPagesDrainedFromGroupingTable * maxElements >= this.groupingTable.Count)
                {
                    this.isDone = true;
                }
            }

            return(response);
        }
Esempio n. 8
0
        /// <summary>
        /// Compares to objects and returns their partial sort relationship.
        /// </summary>
        /// <param name="element1">The first element to compare.</param>
        /// <param name="element2">The second element to compare.</param>
        /// <returns>
        /// Less than zero if obj1 comes before obj2 in the sort order.
        /// Zero if obj1 and obj2 are interchangeable in the sort order.
        /// Greater than zero if obj2 comes before obj1 in the sort order.
        /// </returns>
        public int Compare(CosmosElement element1, CosmosElement element2)
        {
            if (object.ReferenceEquals(element1, element2))
            {
                return(0);
            }

            if (object.ReferenceEquals(element1, MinValueItem.Singleton))
            {
                return(-1);
            }

            if (object.ReferenceEquals(element2, MinValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element1, MaxValueItem.Singleton))
            {
                return(1);
            }

            if (object.ReferenceEquals(element2, MaxValueItem.Singleton))
            {
                return(-1);
            }

            if (element1 == Undefined)
            {
                return(-1);
            }

            if (element2 == Undefined)
            {
                return(1);
            }

            CosmosElementType type1 = element1.Type;

            int cmp = CompareTypes(element1, element2);

            if (cmp == 0)
            {
                // If they are the same type then you need to break the tie.
                switch (type1)
                {
                case CosmosElementType.Boolean:
                    cmp = Comparer <bool> .Default.Compare(
                        (element1 as CosmosBoolean).Value,
                        (element2 as CosmosBoolean).Value);

                    break;

                case CosmosElementType.Null:
                    // All nulls are the same.
                    cmp = 0;
                    break;

                case CosmosElementType.Number:
                    CosmosNumber number1 = element1 as CosmosNumber;
                    CosmosNumber number2 = element2 as CosmosNumber;
                    if (number1.NumberType == CosmosNumberType.Number64)
                    {
                        double double1;
                        if (number1.IsFloatingPoint)
                        {
                            double1 = number1.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double1 = number1.AsInteger().Value;
                        }

                        double double2;
                        if (number2.IsFloatingPoint)
                        {
                            double2 = number2.AsFloatingPoint().Value;
                        }
                        else
                        {
                            double2 = number2.AsInteger().Value;
                        }

                        cmp = Comparer <double> .Default.Compare(
                            double1,
                            double2);
                    }
                    else if (number1.IsFloatingPoint)
                    {
                        double double1 = number1.AsFloatingPoint().Value;
                        double double2 = number2.AsFloatingPoint().Value;
                        cmp = Comparer <double> .Default.Compare(double1, double2);
                    }
                    else
                    {
                        long integer1 = number1.AsInteger().Value;
                        long integer2 = number2.AsInteger().Value;
                        cmp = Comparer <long> .Default.Compare(integer1, integer2);
                    }

                    break;

                case CosmosElementType.String:
                    CosmosString string1 = element1 as CosmosString;
                    CosmosString string2 = element2 as CosmosString;
                    cmp = string.CompareOrdinal(
                        string1.Value,
                        string2.Value);
                    break;

                case CosmosElementType.Guid:
                    CosmosGuid guid1 = element1 as CosmosGuid;
                    CosmosGuid guid2 = element2 as CosmosGuid;
                    cmp = guid1.Value.CompareTo(guid2.Value);
                    break;

                case CosmosElementType.Binary:
                    CosmosBinary binary1 = element1 as CosmosBinary;
                    CosmosBinary binary2 = element2 as CosmosBinary;
                    cmp = ItemComparer.CompareTo(binary1, binary2);
                    break;

                case CosmosElementType.Array:
                case CosmosElementType.Object:
                {
                    UInt128 hash1 = DistinctHash.GetHash(element1);
                    UInt128 hash2 = DistinctHash.GetHash(element2);
                    return(hash1.CompareTo(hash2));
                }

                default:
                    throw new ArgumentException($"Unknown: {nameof(CosmosElementType)}: {type1}");
                }
            }

            return(cmp);
        }