Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Auto Suggest] PPL & SQL Value Suggestion #8275

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8275.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Autocomplete Value Suggestion ([#8275](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8275))
2 changes: 2 additions & 0 deletions src/plugins/data/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export const UI_SETTINGS = {
FILTERS_PINNED_BY_DEFAULT: 'filters:pinnedByDefault',
FILTERS_EDITOR_SUGGEST_VALUES: 'filterEditor:suggestValues',
QUERY_ENHANCEMENTS_ENABLED: 'query:enhancements:enabled',
QUERY_ENHANCEMENTS_SUGGEST_VALUES: 'query:enhancements:suggestValues',
QUERY_ENHANCEMENTS_SUGGEST_VALUES_LIMIT: 'query:enhancements:suggestValuesLimit',
QUERY_DATAFRAME_HYDRATION_STRATEGY: 'query:dataframe:hydrationStrategy',
SEARCH_QUERY_LANGUAGE_BLOCKLIST: 'search:queryLanguageBlocklist',
NEW_HOME_PAGE: 'home:useNewHomePage',
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

42 changes: 38 additions & 4 deletions src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import { monaco } from '@osd/monaco';
import { CursorPosition, OpenSearchPplAutocompleteResult } from '../shared/types';
import { fetchFieldSuggestions, parseQuery } from '../shared/utils';
import { fetchColumnValues, fetchFieldSuggestions, parseQuery } from '../shared/utils';
import { openSearchPplAutocompleteData } from './opensearch_ppl_autocomplete';
import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete';
import { SuggestionItemDetailsTags } from '../shared/constants';
import { PPL_AGGREGATE_FUNTIONS } from './constants';
import { PPL_AGGREGATE_FUNCTIONS } from './constants';
import { OpenSearchPPLParser } from './.generated/OpenSearchPPLParser';

export const getSuggestions = async ({
Expand All @@ -40,16 +40,50 @@
finalSuggestions.push(...fetchFieldSuggestions(indexPattern, (f: any) => `${f} `));
}

if (suggestions.suggestValuesForColumn) {
// get dataset for connecting to the cluster currently engaged
const dataset = services.data.query.queryString.getQuery().dataset;

Check warning on line 45 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L45

Added line #L45 was not covered by tests

// take the column and push in values for that column
const res = await fetchColumnValues(

Check warning on line 48 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L48

Added line #L48 was not covered by tests
[indexPattern.title],
suggestions.suggestValuesForColumn,
services,
dataset
);
paulstn marked this conversation as resolved.
Show resolved Hide resolved

let i = 0;
finalSuggestions.push(

Check warning on line 56 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L55-L56

Added lines #L55 - L56 were not covered by tests
...res.body.fields[0].values.map((val: any) => {
paulstn marked this conversation as resolved.
Show resolved Hide resolved
i++;
return {

Check warning on line 59 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L58-L59

Added lines #L58 - L59 were not covered by tests
text: val.toString(),
insertText: typeof val === 'string' ? `"${val}" ` : `${val} `,
paulstn marked this conversation as resolved.
Show resolved Hide resolved
type: monaco.languages.CompletionItemKind.Value,
detail: SuggestionItemDetailsTags.Value,
sortText: i.toString().padStart(3, '0'),
joshuali925 marked this conversation as resolved.
Show resolved Hide resolved
};
})
);
}

if (suggestions.suggestAggregateFunctions) {
finalSuggestions.push(
...PPL_AGGREGATE_FUNTIONS.map((af) => ({
...PPL_AGGREGATE_FUNCTIONS.map((af) => ({

Check warning on line 72 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L72

Added line #L72 was not covered by tests
text: `${af}()`,
type: monaco.languages.CompletionItemKind.Function,
insertText: af + '(${1:expr}) ',
detail: SuggestionItemDetailsTags.AggregateFunction,
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
}))
);
// separately include count as there will be nothing within the parens
finalSuggestions.push({

Check warning on line 81 in src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/code_completion.ts#L81

Added line #L81 was not covered by tests
text: `count()`,
type: monaco.languages.CompletionItemKind.Function,
insertText: 'count() ',
detail: SuggestionItemDetailsTags.AggregateFunction,
});
}

if (suggestions.suggestSourcesOrTables) {
Expand All @@ -61,7 +95,7 @@
});
}

// create the sortlist
// create the keyword sortlist
const suggestionImportance = new Map<number, string>();
suggestionImportance.set(OpenSearchPPLParser.PIPE, '0');
suggestionImportance.set(OpenSearchPPLParser.COMMA, '1');
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/data/public/antlr/opensearch_ppl/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

export const PPL_AGGREGATE_FUNTIONS = [
export const PPL_AGGREGATE_FUNCTIONS = [
'avg',
'count',
'sum',
'min',
'max',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ tableSourceClause
;

renameClasue
: orignalField = wcFieldExpression AS renamedField = wcFieldExpression
: orignalField = fieldExpression AS renamedField = ID
paulstn marked this conversation as resolved.
Show resolved Hide resolved
;

byClause
Expand Down Expand Up @@ -258,7 +258,7 @@ logicalExpression
;

comparisonExpression
: left = valueExpression comparisonOperator right = valueExpression # compareExpr
: left = valueExpression comparisonOperator right = literalValue # compareExpr
| valueExpression IN valueList # inExpr
;

Expand Down Expand Up @@ -696,11 +696,11 @@ stringLiteral
;

integerLiteral
: (PLUS | MINUS)? INTEGER_LITERAL
: INTEGER_LITERAL
paulstn marked this conversation as resolved.
Show resolved Hide resolved
;

decimalLiteral
: (PLUS | MINUS)? DECIMAL_LITERAL
: DECIMAL_LITERAL
;

booleanLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@
OpenSearchPPLParser.COMMA,
OpenSearchPPLParser.PLUS,
OpenSearchPPLParser.MINUS,
// OpenSearchPPLParser.EQUAL,
// OpenSearchPPLParser.NOT_EQUAL,
// OpenSearchPPLParser.LESS,
// OpenSearchPPLParser.NOT_LESS,
// OpenSearchPPLParser.GREATER,
// OpenSearchPPLParser.NOT_GREATER,
// OpenSearchPPLParser.OR,
// OpenSearchPPLParser.AND,
// OpenSearchPPLParser.XOR,
// OpenSearchPPLParser.NOT,
OpenSearchPPLParser.EQUAL,
OpenSearchPPLParser.NOT_EQUAL,
OpenSearchPPLParser.LESS,
OpenSearchPPLParser.NOT_LESS,
OpenSearchPPLParser.GREATER,
OpenSearchPPLParser.NOT_GREATER,
OpenSearchPPLParser.OR,
OpenSearchPPLParser.AND,
OpenSearchPPLParser.XOR,
OpenSearchPPLParser.NOT,
OpenSearchPPLParser.LT_PRTHS,
OpenSearchPPLParser.RT_PRTHS,
];
for (let i = firstFunctionIndex; i <= lastFunctionIndex; i++) {
if (!operatorsToInclude.includes(i)) {
Expand All @@ -64,6 +66,8 @@
CLOSING_BRACKET: OpenSearchPPLParser.RT_PRTHS,
SEARCH: OpenSearchPPLParser.SEARCH,
SOURCE: OpenSearchPPLParser.SOURCE,
PIPE: OpenSearchPPLParser.PIPE,
ID: OpenSearchPPLParser.ID,
};

const rulesToVisit = new Set([
Expand All @@ -78,6 +82,7 @@
OpenSearchPPLParser.RULE_multiFieldRelevanceFunctionName,
OpenSearchPPLParser.RULE_positionFunctionName,
OpenSearchPPLParser.RULE_evalFunctionName,
OpenSearchPPLParser.RULE_literalValue,
]);

export function processVisitedRules(
Expand All @@ -88,6 +93,7 @@
let suggestSourcesOrTables: OpenSearchPplAutocompleteResult['suggestSourcesOrTables'];
let suggestAggregateFunctions = false;
let shouldSuggestColumns = false;
let suggestValuesForColumn: string | undefined;

for (const [ruleId, rule] of rules) {
switch (ruleId) {
Expand All @@ -101,6 +107,22 @@
}
case OpenSearchPPLParser.RULE_tableQualifiedName: {
suggestSourcesOrTables = SourceOrTableSuggestion.TABLES;
break;

Check warning on line 110 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L110

Added line #L110 was not covered by tests
}
case OpenSearchPPLParser.RULE_literalValue: {
let currentIndex = cursorTokenIndex - 1;
while (currentIndex > -1) {
const token = tokenStream.get(currentIndex);

Check warning on line 115 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L113-L115

Added lines #L113 - L115 were not covered by tests
if (token.type === tokenDictionary.PIPE) {
break;

Check warning on line 117 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L117

Added line #L117 was not covered by tests
}
if (token.type === tokenDictionary.ID) {
suggestValuesForColumn = token.text;
break;

Check warning on line 121 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L120-L121

Added lines #L120 - L121 were not covered by tests
}
currentIndex--;

Check warning on line 123 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L123

Added line #L123 was not covered by tests
}
break;

Check warning on line 125 in src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_ppl/opensearch_ppl_autocomplete.ts#L125

Added line #L125 was not covered by tests
}
}
}
Expand All @@ -109,6 +131,7 @@
suggestSourcesOrTables,
suggestAggregateFunctions,
shouldSuggestColumns,
suggestValuesForColumn,
};
}

Expand Down Expand Up @@ -145,7 +168,7 @@
const result: OpenSearchPplAutocompleteResult = {
...baseResult,
...suggestionsFromRules,
suggestColumns: shouldSuggestColumns ? ({ name: '' } as TableContextSuggestion) : undefined,
suggestColumns: shouldSuggestColumns ? ({} as TableContextSuggestion) : undefined,
};
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from ./src/plugins/data/public/antlr/opensearch_sql/grammar/OpenSearchSQLLexer.g4 by ANTLR 4.13.1
// Generated from grammar/OpenSearchSQLLexer.g4 by ANTLR 4.13.1

import * as antlr from "antlr4ng";
import { Token } from "antlr4ng";
Expand Down

Large diffs are not rendered by default.

Loading
Loading