private static void AddTopColumnValues(IntelliSenseResult result, TermExpression lastTerm, IntelliSenseGuidance guidance, List <IntelliSenseItem> suggestions, Table singleTable, ColumnDetails singleColumn)
        {
            string completeQuery = GetCompleteQueryPrefix(result);

            // Recommend the top ten values in the column with the prefix typed so far
            DistinctResult topValues = singleTable.Query(new DistinctQueryTop(singleColumn.Name, lastTerm.Value.ToString(), completeQuery, 10));
            int            total     = (int)topValues.Total;

            if (topValues.Total == 0)
            {
                return;
            }

            // Walk values in order for ==, :, ::, backwards with inverse percentages for !=
            bool isNotEquals = lastTerm.Operator == Operator.NotEquals;
            int  start       = isNotEquals ? topValues.Values.RowCount - 1 : 0;
            int  end         = isNotEquals ? -1 : topValues.Values.RowCount;
            int  step        = isNotEquals ? -1 : 1;

            for (int i = start; i != end; i += step)
            {
                string value         = topValues.Values[i, 0].ToString();
                int    countForValue = (int)topValues.Values[i, 1];
                if (isNotEquals)
                {
                    countForValue = (int)topValues.Total - countForValue;
                }

                if ((countForValue > 1 || total <= 10) && value.StartsWith(guidance.Value, StringComparison.OrdinalIgnoreCase) && value.Length < 100)
                {
                    suggestions.Add(new IntelliSenseItem(QueryTokenCategory.Value, QueryParser.WrapValue(topValues.Values[i, 0]), countForValue, topValues.Total));
                }
            }
        }
Beispiel #2
0
        public override DistinctResult Compute(Partition p)
        {
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            DistinctResult result = new DistinctResult(this);

            // Verify the column exists
            if (!p.ContainsColumn(this.Column))
            {
                result.Details.AddError(ExecutionDetails.ColumnDoesNotExist, this.Column);
                return(result);
            }

            // Find the set of items matching the where clause
            ShortSet whereSet = new ShortSet(p.Count);

            this.Where.TryEvaluate(p, whereSet, result.Details);

            if (result.Details.Succeeded)
            {
                // Count the occurences of each value
                Dictionary <object, int> countByValue = new Dictionary <object, int>();
                IUntypedColumn           column       = p.Columns[this.Column];
                int rowCount = 0;

                for (int i = 0; i < column.Count; ++i)
                {
                    ushort lid = (ushort)i;
                    if (whereSet.Contains(lid))
                    {
                        object value = column[lid];

                        int count;
                        countByValue.TryGetValue(value, out count);
                        countByValue[value] = count + 1;

                        rowCount++;
                    }
                }

                // Convert the top this.Count rows by count into a DataBlock
                result.Values = ToDataBlock(countByValue, this.Column, (int)this.Count);

                result.AllValuesReturned = result.Values.RowCount == countByValue.Count;
                result.Total             = rowCount;
            }

            return(result);
        }
Beispiel #3
0
        public override DistinctResult Merge(DistinctResult[] partitionResults)
        {
            if (partitionResults == null)
            {
                throw new ArgumentNullException("partitionResults");
            }
            if (partitionResults.Length == 0)
            {
                throw new ArgumentException("Length==0 not supported", "partitionResults");
            }
            if (!partitionResults[0].Details.Succeeded)
            {
                return(partitionResults[0]);
            }

            DistinctResult mergedResult = new DistinctResult(this);

            mergedResult.ColumnType        = partitionResults[0].ColumnType;
            mergedResult.AllValuesReturned = true;

            Dictionary <object, int> countByValue = new Dictionary <object, int>();

            for (int partitionIndex = 0; partitionIndex < partitionResults.Length; ++partitionIndex)
            {
                DistinctResult result = partitionResults[partitionIndex];
                DataBlock      block  = result.Values;

                // Add count per value together from each partition
                for (int rowIndex = 0; rowIndex < block.RowCount; ++rowIndex)
                {
                    object value = block[rowIndex, 0];
                    int    countFromPartition = (int)block[rowIndex, 1];

                    int count;
                    countByValue.TryGetValue(value, out count);
                    countByValue[value] = count + countFromPartition;
                }

                // Merge other properties
                mergedResult.Details.Merge(result.Details);
                mergedResult.AllValuesReturned &= result.AllValuesReturned;
                mergedResult.Total             += result.Total;
            }

            mergedResult.Values = ToDataBlock(countByValue, this.Column, (int)this.Count);

            return(mergedResult);
        }
Beispiel #4
0
        private static void AddTopColumnValues(IntelliSenseResult result, TermExpression lastTerm, IntelliSenseGuidance guidance, List <IntelliSenseItem> suggestions, Table singleTable, ColumnDetails singleColumn)
        {
            // Lame, to turn single terms into AllQuery [normally they return nothing]
            string completeQuery = QueryParser.Parse(result.Complete).ToString();

            // Recommend the top ten values in the column with the prefix typed so far
            DistinctResult topValues = singleTable.Query(new DistinctQueryTop(singleColumn.Name, completeQuery, 10));
            int            total     = (int)topValues.Total;

            if (topValues.Total == 0)
            {
                return;
            }

            // Walk values in order for ==, :, ::, backwards with inverse percentages for !=
            bool isNotEquals = lastTerm.Operator == Operator.NotEquals;
            int  start       = isNotEquals ? topValues.Values.RowCount - 1 : 0;
            int  end         = isNotEquals ? -1 : topValues.Values.RowCount;
            int  step        = isNotEquals ? -1 : 1;

            for (int i = start; i != end; i += step)
            {
                string value         = topValues.Values[i, 0].ToString();
                int    countForValue = (int)topValues.Values[i, 1];
                if (isNotEquals)
                {
                    countForValue = (int)topValues.Total - countForValue;
                }
                double frequency = (double)countForValue / (double)(topValues.Total);

                if ((countForValue > 1 || total <= 10) && value.StartsWith(guidance.Value, StringComparison.OrdinalIgnoreCase))
                {
                    string hint = (countForValue == topValues.Total ? "all" : frequency.ToString("P0"));
                    suggestions.Add(new IntelliSenseItem(QueryTokenCategory.Value, QueryScanner.WrapValue(value), hint));
                }
            }
        }
Beispiel #5
0
        public override DistinctResult Compute(Partition p)
        {
            if (p == null)
            {
                throw new ArgumentNullException("p");
            }
            DistinctResult result = new DistinctResult(this);

            // Verify the column exists
            if (!p.ContainsColumn(this.Column))
            {
                result.Details.AddError(ExecutionDetails.ColumnDoesNotExist, this.Column);
                return(result);
            }

            // Find the set of items matching the base where clause
            ShortSet whereSet = new ShortSet(p.Count);

            this.Where.TryEvaluate(p, whereSet, result.Details);

            // Capture the total of the base query
            result.Total = whereSet.Count();

            // Add a prefix filter for the prefix so far, if any and the column can prefix match
            if (!String.IsNullOrEmpty(this.ValuePrefix))
            {
                ExecutionDetails prefixDetails = new ExecutionDetails();
                ShortSet         prefixSet     = new ShortSet(p.Count);
                new TermExpression(this.Column, Operator.StartsWith, this.ValuePrefix).TryEvaluate(p, prefixSet, prefixDetails);
                if (prefixDetails.Succeeded)
                {
                    whereSet.And(prefixSet);
                }
            }

            if (result.Details.Succeeded)
            {
                // Count the occurences of each value
                Dictionary <object, int> countByValue = new Dictionary <object, int>();
                IUntypedColumn           column       = p.Columns[this.Column];

                for (int i = 0; i < column.Count; ++i)
                {
                    ushort lid = (ushort)i;
                    if (whereSet.Contains(lid))
                    {
                        object value = column[lid];

                        int count;
                        countByValue.TryGetValue(value, out count);
                        countByValue[value] = count + 1;
                    }
                }

                // Convert the top this.Count rows by count into a DataBlock
                result.Values = ToDataBlock(countByValue, this.Column, (int)this.Count);

                result.AllValuesReturned = result.Values.RowCount == countByValue.Count;
            }

            return(result);
        }