Ejemplo n.º 1
0
        private static void AddSuggestionsForTerm(IReadOnlyCollection <Table> targetTables, IntelliSenseResult result, TermExpression lastTerm, IntelliSenseGuidance guidance, List <IntelliSenseItem> suggestions)
        {
            if (lastTerm != null && lastTerm.ColumnName == "*")
            {
                object termValue = lastTerm.Value;
                List <Tuple <string, int, int> > columnsForTerm = new List <Tuple <string, int, int> >();

                foreach (Table table in targetTables)
                {
                    DataBlockResult columns = table.Query(new TermInColumnsQuery(termValue.ToString(), GetCompleteQueryPrefix(result)));
                    for (int i = 0; i < columns.Values.RowCount; ++i)
                    {
                        columnsForTerm.Add(new Tuple <string, int, int>((string)columns.Values[i, 0], (int)columns.Values[i, 1], (int)columns.Total));
                    }
                }

                // Sort overall set by frequency descending
                columnsForTerm.Sort((left, right) => ((double)right.Item2 / (double)right.Item3).CompareTo((double)left.Item2 / (double)left.Item3));

                // Add top 10 suggestions
                int countToReturn = Math.Min(10, columnsForTerm.Count);
                for (int i = 0; i < countToReturn; ++i)
                {
                    suggestions.Add(new IntelliSenseItem(QueryTokenCategory.ColumnName, QueryParser.WrapColumnName(columnsForTerm[i].Item1) + " : " + QueryParser.WrapValue(termValue), columnsForTerm[i].Item2, columnsForTerm[i].Item3));
                }
            }
        }
Ejemplo n.º 2
0
        private static void AddValueDistribution(IntelliSenseResult result, TermExpression lastTerm, IntelliSenseGuidance guidance, List <IntelliSenseItem> suggestions, Table singleTable, ColumnDetails singleColumn)
        {
            string completeQuery = GetCompleteQueryPrefix(result);

            bool inclusive = (lastTerm.Operator == Operator.LessThanOrEqual || lastTerm.Operator == Operator.GreaterThan);
            bool reverse   = (lastTerm.Operator == Operator.GreaterThan || lastTerm.Operator == Operator.GreaterThanOrEqual);

            // Recommend the top ten values in the column with the prefix typed so far
            DataBlockResult distribution = singleTable.Query(new DistributionQuery(singleColumn.Name, completeQuery, inclusive));

            if (distribution.Total == 0 || distribution.Details.Succeeded == false)
            {
                return;
            }

            int countSoFar;

            if (reverse)
            {
                countSoFar = (int)distribution.Values[distribution.Values.RowCount - 1, 1];

                for (int i = distribution.Values.RowCount - 2; i >= 0; --i)
                {
                    string value         = distribution.Values[i, 0].ToString();
                    double frequency     = (double)countSoFar / (double)(distribution.Total);
                    int    countForRange = (int)distribution.Values[i, 1];

                    if ((distribution.Values.RowCount == 2 || (int)distribution.Values[i + 1, 1] > 0) && value.StartsWith(guidance.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        suggestions.Add(new IntelliSenseItem(QueryTokenCategory.Value, QueryParser.WrapValue(distribution.Values[i, 0]), countSoFar, distribution.Total));
                    }

                    countSoFar += countForRange;
                }
            }
            else
            {
                countSoFar = 0;

                for (int i = 0; i < distribution.Values.RowCount - 1; ++i)
                {
                    string value         = distribution.Values[i, 0].ToString();
                    int    countForRange = (int)distribution.Values[i, 1];
                    countSoFar += countForRange;

                    double frequency = (double)countSoFar / (double)(distribution.Total);

                    if ((distribution.Values.RowCount == 2 || countForRange > 0) && value.StartsWith(guidance.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        suggestions.Add(new IntelliSenseItem(QueryTokenCategory.Value, QueryParser.WrapValue(distribution.Values[i, 0]), countSoFar, distribution.Total));
                    }
                }
            }
        }