From 7d4caafd9e241cc4d600e7dbe9a9373ff1dd442a Mon Sep 17 00:00:00 2001 From: Eric Wei Date: Tue, 20 Aug 2024 16:10:23 -0700 Subject: [PATCH] [Autocomplete - SQL] Minor interface change to add suggestion type and move suggestion provider registration location (#7758) * add table/source as prefix to suggested fields Signed-off-by: Eric * add type to column Signed-off-by: Eric * move registeration to osd/monaco Signed-off-by: Eric * add detail Signed-off-by: Eric * Changeset file for PR #7758 created/updated --------- Signed-off-by: Eric Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/7758.yml | 2 ++ .../src/xjson/lexer_rules/opensearchsql.ts | 4 ++++ .../antlr/opensearch_sql/code_completion.ts | 17 +++++++++++++++-- .../data/public/antlr/shared/constants.ts | 10 ++++++++++ .../providers/query_suggestion_provider.ts | 14 ++++++++++++-- .../public/ui/query_editor/query_editor.tsx | 4 +--- 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/7758.yml create mode 100644 src/plugins/data/public/antlr/shared/constants.ts diff --git a/changelogs/fragments/7758.yml b/changelogs/fragments/7758.yml new file mode 100644 index 000000000000..ecc1f7b1defa --- /dev/null +++ b/changelogs/fragments/7758.yml @@ -0,0 +1,2 @@ +feat: +- Minor interface change and move suggestion provider registration location ([#7758](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7758)) \ No newline at end of file diff --git a/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts b/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts index b204c63e83c4..0ff29b71c09d 100644 --- a/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts +++ b/packages/osd-monaco/src/xjson/lexer_rules/opensearchsql.ts @@ -155,3 +155,7 @@ export const lexerRules = { ], }, } as monaco.languages.IMonarchLanguage; + +monaco.languages.register({ + id: ID, +}); diff --git a/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts b/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts index bfd0d9d2da9c..dc5a4f96ff74 100644 --- a/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts +++ b/src/plugins/data/public/antlr/opensearch_sql/code_completion.ts @@ -25,6 +25,7 @@ import { SQL_SYMBOLS } from './constants'; import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete'; import { fetchTableSchemas } from '../shared/utils'; import { IDataFrameResponse, IFieldType } from '../../../common'; +import { SuggestionItemDetailsTags } from '../shared/constants'; export interface SuggestionParams { position: monaco.Position; @@ -65,13 +66,17 @@ export const getSuggestions = async ({ (schemas as IDataFrameResponse[]).forEach((schema: IDataFrameResponse) => { if ('body' in schema && schema.body && 'fields' in schema.body) { const columns = schema.body.fields.find((col: IFieldType) => col.name === 'COLUMN_NAME'); - const fieldTypes = schema.body.fields.find((col: IFieldType) => col.name === 'DATA_TYPE'); + const fieldTypes = schema.body.fields.find((col: IFieldType) => col.name === 'TYPE_NAME'); if (columns && fieldTypes) { finalSuggestions.push( - ...columns.values.map((col: string) => ({ + ...columns.values.map((col: string, index: number) => ({ text: col, type: monaco.languages.CompletionItemKind.Field, + insertText: col, + detail: fieldTypes.values[index], + start: 0, + end: 0, })) ); } @@ -85,6 +90,10 @@ export const getSuggestions = async ({ ...SQL_SYMBOLS.AGREGATE_FUNCTIONS.map((af) => ({ text: af, type: monaco.languages.CompletionItemKind.Function, + insertText: af, + detail: SuggestionItemDetailsTags.AggregateFunction, + start: 0, + end: 0, })) ); } @@ -95,6 +104,10 @@ export const getSuggestions = async ({ ...suggestions.suggestKeywords.map((sk) => ({ text: sk.value, type: monaco.languages.CompletionItemKind.Keyword, + insertText: sk.value, + detail: SuggestionItemDetailsTags.Keyword, + start: 0, + end: 0, })) ); } diff --git a/src/plugins/data/public/antlr/shared/constants.ts b/src/plugins/data/public/antlr/shared/constants.ts new file mode 100644 index 000000000000..833fc2aef2b4 --- /dev/null +++ b/src/plugins/data/public/antlr/shared/constants.ts @@ -0,0 +1,10 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +// suggestion item details tags +export const enum SuggestionItemDetailsTags { + Keyword = 'Keyword', + AggregateFunction = 'Aggregate Function', +} diff --git a/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts b/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts index 636a4a1993b6..3da29999d69a 100644 --- a/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts +++ b/src/plugins/data/public/autocomplete/providers/query_suggestion_provider.ts @@ -59,7 +59,7 @@ export interface QuerySuggestionGetFnArgs { /** @public **/ export interface QuerySuggestionBasic { - type: QuerySuggestionTypes | monaco.languages.CompletionItemKind; + type: QuerySuggestionTypes; description?: string | JSX.Element; end: number; start: number; @@ -74,5 +74,15 @@ export interface QuerySuggestionField extends QuerySuggestionBasic { field: IFieldType; } +export interface SqlMonacoCompatibleQuerySuggestion + extends Pick { + type: monaco.languages.CompletionItemKind; + text: string; + detail: string; +} + /** @public **/ -export type QuerySuggestion = QuerySuggestionBasic | QuerySuggestionField; +export type QuerySuggestion = + | QuerySuggestionBasic + | QuerySuggestionField + | SqlMonacoCompatibleQuerySuggestion; diff --git a/src/plugins/data/public/ui/query_editor/query_editor.tsx b/src/plugins/data/public/ui/query_editor/query_editor.tsx index 2ff643f74254..97edf2867a86 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor.tsx @@ -31,9 +31,6 @@ import { QueryControls } from '../../query/query_string/language_service/get_que import { RecentQueriesTable } from '../../query/query_string/language_service/recent_query'; import { DefaultInputProps } from './editors'; -const LANGUAGE_ID_SQL = 'SQL'; -monaco.languages.register({ id: LANGUAGE_ID_SQL }); - const LANGUAGE_ID_KUERY = 'kuery'; monaco.languages.register({ id: LANGUAGE_ID_KUERY }); @@ -300,6 +297,7 @@ export default class QueryEditorUI extends Component { kind: s.type as monaco.languages.CompletionItemKind, insertText: s.insertText ?? s.text, range, + detail: 'detail' in s ? s.detail : '', })) : [], incomplete: false,