Skip to content

Commit bff6493

Browse files
committed
Moving SQL and PPL Provider Registration to Query Enhancements Plugin
Issue Resolved: opensearch-project#9584 Signed-off-by: Anan <[email protected]>
1 parent 0a9c003 commit bff6493

File tree

9 files changed

+337
-296
lines changed

9 files changed

+337
-296
lines changed

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

+12-86
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
* GitHub history for details.
1010
*/
1111

12-
import { monaco } from '@osd/monaco';
1312
import { CursorPosition, OpenSearchPplAutocompleteResult } from '../shared/types';
14-
import {
15-
fetchColumnValues,
16-
formatFieldsToSuggestions,
17-
formatValuesToSuggestions,
18-
parseQuery,
19-
} from '../shared/utils';
13+
import { parseQuery } from '../shared/utils';
2014
import { openSearchPplAutocompleteData } from './opensearch_ppl_autocomplete';
2115
import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete';
22-
import { SuggestionItemDetailsTags } from '../shared/constants';
23-
import { PPL_AGGREGATE_FUNCTIONS, PPL_SUGGESTION_IMPORTANCE } from './constants';
2416

17+
/**
18+
* @deprecated PPL suggestion provider has been moved to the query_enhancements plugin.
19+
* This function is kept for backward compatibility and will be removed in a future release.
20+
*/
2521
export const getSuggestions = async ({
2622
selectionStart,
2723
selectionEnd,
@@ -30,85 +26,15 @@ export const getSuggestions = async ({
3026
position,
3127
query,
3228
services,
33-
}: QuerySuggestionGetFnArgs) => {
34-
if (!services || !services.appName || !indexPattern) return [];
35-
try {
36-
const { lineNumber, column } = position || {};
37-
const suggestions = getOpenSearchPplAutoCompleteSuggestions(query, {
38-
line: lineNumber || selectionStart,
39-
column: column || selectionEnd,
40-
});
41-
42-
const finalSuggestions: QuerySuggestion[] = [];
43-
44-
if (suggestions.suggestColumns) {
45-
finalSuggestions.push(...formatFieldsToSuggestions(indexPattern, (f: any) => `${f} `, '3'));
46-
}
47-
48-
if (suggestions.suggestValuesForColumn) {
49-
finalSuggestions.push(
50-
...formatValuesToSuggestions(
51-
await fetchColumnValues(
52-
indexPattern.title,
53-
suggestions.suggestValuesForColumn,
54-
services,
55-
indexPattern.fields.find((field) => field.name === suggestions.suggestValuesForColumn),
56-
datasetType
57-
).catch(() => []),
58-
(val: any) => (typeof val === 'string' ? `"${val}" ` : `${val} `)
59-
)
60-
);
61-
}
62-
63-
if (suggestions.suggestAggregateFunctions) {
64-
finalSuggestions.push(
65-
...PPL_AGGREGATE_FUNCTIONS.map((af) => ({
66-
text: `${af}()`,
67-
type: monaco.languages.CompletionItemKind.Function,
68-
insertText: af + ' ',
69-
detail: SuggestionItemDetailsTags.AggregateFunction,
70-
}))
71-
);
72-
}
73-
74-
if (suggestions.suggestSourcesOrTables) {
75-
finalSuggestions.push({
76-
text: indexPattern.title,
77-
type: monaco.languages.CompletionItemKind.Struct,
78-
insertText: `${indexPattern.title} `,
79-
detail: SuggestionItemDetailsTags.Table,
80-
});
81-
}
82-
83-
if (suggestions.suggestRenameAs) {
84-
finalSuggestions.push({
85-
text: 'as',
86-
insertText: 'as ',
87-
type: monaco.languages.CompletionItemKind.Keyword,
88-
detail: SuggestionItemDetailsTags.Keyword,
89-
});
90-
}
91-
92-
// Fill in PPL keywords
93-
if (suggestions.suggestKeywords?.length) {
94-
finalSuggestions.push(
95-
...suggestions.suggestKeywords.map((sk) => ({
96-
text: sk.value.toLowerCase(),
97-
insertText: `${sk.value.toLowerCase()} `,
98-
type: monaco.languages.CompletionItemKind.Keyword,
99-
detail: SuggestionItemDetailsTags.Keyword,
100-
// sortText is the only option to sort suggestions, compares strings
101-
sortText: PPL_SUGGESTION_IMPORTANCE.get(sk.id) ?? '9' + sk.value.toLowerCase(), // '9' used to devalue every other suggestion
102-
}))
103-
);
104-
}
105-
106-
return finalSuggestions;
107-
} catch (e) {
108-
return [];
109-
}
29+
}: QuerySuggestionGetFnArgs): Promise<QuerySuggestion[]> => {
30+
// Return empty array - the actual implementation has been moved to query_enhancements plugin
31+
return [];
11032
};
11133

34+
/**
35+
* Helper function to get PPL autocomplete suggestions
36+
* This is still exported for use by the query_enhancements plugin
37+
*/
11238
export const getOpenSearchPplAutoCompleteSuggestions = (
11339
query: string,
11440
cursor: CursorPosition

src/plugins/data/public/antlr/opensearch_sql/code_completion.ts

+12-146
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,15 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { monaco } from '@osd/monaco';
7-
import {
8-
ColumnValuePredicate,
9-
CursorPosition,
10-
OpenSearchSqlAutocompleteResult,
11-
} from '../shared/types';
6+
import { CursorPosition, OpenSearchSqlAutocompleteResult } from '../shared/types';
127
import { openSearchSqlAutocompleteData } from './opensearch_sql_autocomplete';
13-
import { SQL_SUGGESTION_IMPORTANCE, SQL_SYMBOLS } from './constants';
148
import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete';
15-
import {
16-
fetchColumnValues,
17-
formatFieldsToSuggestions,
18-
formatValuesToSuggestions,
19-
parseQuery,
20-
} from '../shared/utils';
21-
import { SuggestionItemDetailsTags } from '../shared/constants';
22-
23-
export interface SuggestionParams {
24-
position: monaco.Position;
25-
query: string;
26-
}
27-
28-
export interface ISuggestionItem {
29-
text: string;
30-
type: string;
31-
fieldType?: string;
32-
}
9+
import { parseQuery } from '../shared/utils';
3310

11+
/**
12+
* @deprecated SQL suggestion provider has been moved to the query_enhancements plugin.
13+
* This function is kept for backward compatibility and will be removed in a future release.
14+
*/
3415
export const getSuggestions = async ({
3516
selectionStart,
3617
selectionEnd,
@@ -40,129 +21,14 @@ export const getSuggestions = async ({
4021
query,
4122
services,
4223
}: QuerySuggestionGetFnArgs): Promise<QuerySuggestion[]> => {
43-
if (!services || !services.appName || !indexPattern) return [];
44-
try {
45-
const { lineNumber, column } = position || {};
46-
const suggestions = getOpenSearchSqlAutoCompleteSuggestions(query, {
47-
line: lineNumber || selectionStart,
48-
column: column || selectionEnd,
49-
});
50-
51-
const finalSuggestions: QuerySuggestion[] = [];
52-
53-
// Fetch columns and values
54-
if (suggestions.suggestColumns?.tables?.length) {
55-
finalSuggestions.push(...formatFieldsToSuggestions(indexPattern, (f: any) => `${f} `, '2'));
56-
}
57-
58-
if (suggestions.suggestColumnValuePredicate) {
59-
switch (suggestions.suggestColumnValuePredicate) {
60-
case ColumnValuePredicate.COLUMN: {
61-
finalSuggestions.push(
62-
...formatFieldsToSuggestions(indexPattern, (f: any) => `${f} `, '2')
63-
);
64-
break;
65-
}
66-
case ColumnValuePredicate.OPERATOR: {
67-
finalSuggestions.push({
68-
text: '=',
69-
insertText: '= ',
70-
type: monaco.languages.CompletionItemKind.Operator,
71-
detail: SuggestionItemDetailsTags.Operator,
72-
sortText: '0',
73-
});
74-
break;
75-
}
76-
case ColumnValuePredicate.LPAREN: {
77-
finalSuggestions.push({
78-
text: '(',
79-
insertText: '( ',
80-
type: monaco.languages.CompletionItemKind.Keyword,
81-
detail: SuggestionItemDetailsTags.Keyword,
82-
sortText: '0',
83-
});
84-
break;
85-
}
86-
case ColumnValuePredicate.END_IN_TERM: {
87-
finalSuggestions.push({
88-
text: ',',
89-
insertText: ', ',
90-
type: monaco.languages.CompletionItemKind.Keyword,
91-
detail: SuggestionItemDetailsTags.Keyword,
92-
sortText: '0',
93-
});
94-
finalSuggestions.push({
95-
text: ')',
96-
insertText: ') ',
97-
type: monaco.languages.CompletionItemKind.Keyword,
98-
detail: SuggestionItemDetailsTags.Keyword,
99-
sortText: '08',
100-
});
101-
break;
102-
}
103-
case ColumnValuePredicate.VALUE: {
104-
if (suggestions.suggestValuesForColumn) {
105-
finalSuggestions.push(
106-
...formatValuesToSuggestions(
107-
await fetchColumnValues(
108-
indexPattern.title,
109-
suggestions.suggestValuesForColumn,
110-
services,
111-
indexPattern.fields.find(
112-
(field) => field.name === suggestions.suggestValuesForColumn
113-
),
114-
datasetType
115-
).catch(() => []),
116-
(val: any) => (typeof val === 'string' ? `'${val}' ` : `${val} `)
117-
)
118-
);
119-
}
120-
break;
121-
}
122-
}
123-
}
124-
125-
// Fill in aggregate functions
126-
if (suggestions.suggestAggregateFunctions) {
127-
finalSuggestions.push(
128-
...SQL_SYMBOLS.AGREGATE_FUNCTIONS.map((af) => ({
129-
text: af,
130-
type: monaco.languages.CompletionItemKind.Function,
131-
insertText: `${af} `,
132-
detail: SuggestionItemDetailsTags.AggregateFunction,
133-
}))
134-
);
135-
}
136-
137-
if (suggestions.suggestViewsOrTables) {
138-
finalSuggestions.push({
139-
text: indexPattern.title,
140-
type: monaco.languages.CompletionItemKind.Struct,
141-
insertText: `${indexPattern.title} `,
142-
detail: SuggestionItemDetailsTags.Table,
143-
});
144-
}
145-
146-
// Fill in SQL keywords
147-
if (suggestions.suggestKeywords?.length) {
148-
finalSuggestions.push(
149-
...suggestions.suggestKeywords.map((sk) => ({
150-
text: sk.value,
151-
type: monaco.languages.CompletionItemKind.Keyword,
152-
insertText: `${sk.value} `,
153-
detail: SuggestionItemDetailsTags.Keyword,
154-
sortText: SQL_SUGGESTION_IMPORTANCE.get(sk.id) ?? '9' + sk.value.toLowerCase(),
155-
}))
156-
);
157-
}
158-
159-
return finalSuggestions;
160-
} catch (error) {
161-
// TODO: Handle errors appropriately, possibly logging or displaying a message to the user
162-
return [];
163-
}
24+
// Return empty array - the actual implementation has been moved to query_enhancements plugin
25+
return [];
16426
};
16527

28+
/**
29+
* Helper function to get SQL autocomplete suggestions
30+
* This is still exported for use by the query_enhancements plugin
31+
*/
16632
export const getOpenSearchSqlAutoCompleteSuggestions = (
16733
query: string,
16834
cursor: CursorPosition

src/plugins/data/public/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,28 @@ export {
306306
AutocompleteStart,
307307
} from './autocomplete';
308308

309+
// Export shared utilities for query suggestions
310+
export {
311+
fetchColumnValues,
312+
formatFieldsToSuggestions,
313+
formatValuesToSuggestions,
314+
parseQuery,
315+
} from './antlr/shared/utils';
316+
export { SuggestionItemDetailsTags } from './antlr/shared/constants';
317+
318+
// Export ANTLR-related types and constants for SQL
319+
export { SQL_SUGGESTION_IMPORTANCE, SQL_SYMBOLS } from './antlr/opensearch_sql/constants';
320+
export { openSearchSqlAutocompleteData } from './antlr/opensearch_sql/opensearch_sql_autocomplete';
321+
export { getOpenSearchSqlAutoCompleteSuggestions } from './antlr/opensearch_sql/code_completion';
322+
323+
// Export ANTLR-related types and constants for PPL
324+
export {
325+
PPL_AGGREGATE_FUNCTIONS,
326+
PPL_SUGGESTION_IMPORTANCE,
327+
} from './antlr/opensearch_ppl/constants';
328+
export { openSearchPplAutocompleteData } from './antlr/opensearch_ppl/opensearch_ppl_autocomplete';
329+
export { getOpenSearchPplAutoCompleteSuggestions } from './antlr/opensearch_ppl/code_completion';
330+
309331
/*
310332
* Search:
311333
*/

src/plugins/data/public/plugin.ts

-4
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ import { DataSourceFactory } from './data_sources/datasource';
9191
import { registerDefaultDataSource } from './data_sources/register_default_datasource';
9292
import { DefaultDslDataSource } from './data_sources/default_datasource';
9393
import { DEFAULT_DATA_SOURCE_TYPE } from './data_sources/constants';
94-
import { getSuggestions as getSQLSuggestions } from './antlr/opensearch_sql/code_completion';
9594
import { getSuggestions as getDQLSuggestions } from './antlr/dql/code_completion';
96-
import { getSuggestions as getPPLSuggestions } from './antlr/opensearch_ppl/code_completion';
9795
import { createStorage, DataStorage, UI_SETTINGS } from '../common';
9896

9997
declare module '../../ui_actions/public' {
@@ -177,9 +175,7 @@ export class DataPublicPlugin
177175
);
178176

179177
const autoComplete = this.autocomplete.setup(core);
180-
autoComplete.addQuerySuggestionProvider('SQL', getSQLSuggestions);
181178
autoComplete.addQuerySuggestionProvider('kuery', getDQLSuggestions);
182-
autoComplete.addQuerySuggestionProvider('PPL', getPPLSuggestions);
183179

184180
const useNewSavedQueriesUI =
185181
core.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED) &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export { getSQLSuggestions } from './sql_suggestions';
7+
export { getPPLSuggestions } from './ppl_suggestions';

0 commit comments

Comments
 (0)