Skip to content

Commit

Permalink
Revert "[Auto Suggest] Add MDS Support Along with A Few Cleanup and t…
Browse files Browse the repository at this point in the history
…ests (#7463)"

This reverts commit 9f68352.
  • Loading branch information
kavilla committed Jul 26, 2024
1 parent f66ef3b commit 65b9171
Show file tree
Hide file tree
Showing 16 changed files with 20,951 additions and 280 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,3 @@ snapshots.js

# Yarn local mirror content
.yarn-local-mirror

# Ignore the generated antlr files
/src/plugins/data/public/antlr/opensearch_sql/grammar/.antlr
45 changes: 33 additions & 12 deletions src/plugins/data/public/antlr/opensearch_sql/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { monaco } from '@osd/monaco';
import { monaco } from 'packages/osd-monaco/target';
import { Lexer as LexerType, ParserRuleContext, Parser as ParserType } from 'antlr4ng';
import { CodeCompletionCore } from 'antlr4-c3';
import {
Expand All @@ -21,9 +21,10 @@ import { createParser } from './parse';
import { SqlErrorListener } from './sql_error_listerner';
import { findCursorTokenIndex } from '../shared/cursor';
import { openSearchSqlAutocompleteData } from './opensearch_sql_autocomplete';
import { getUiSettings } from '../../services';
import { SQL_SYMBOLS } from './constants';
import { QuerySuggestion, QuerySuggestionGetFnArgs } from '../../autocomplete';
import { fetchTableSchemas } from '../shared/utils';
import { QuerySuggestionGetFnArgs } from '../../autocomplete';
import { fetchColumnValues, fetchTableSchemas } from '../shared/utils';

export interface SuggestionParams {
position: monaco.Position;
Expand Down Expand Up @@ -52,39 +53,60 @@ export const getSuggestions = async ({
column: position?.column || selectionEnd,
});

const finalSuggestions = [] as QuerySuggestion[];
const finalSuggestions = [];

try {
// Fetch columns and values
if ('suggestColumns' in suggestions && (suggestions.suggestColumns?.tables?.length ?? 0) > 0) {
const tableNames = suggestions.suggestColumns?.tables?.map((table) => table.name) ?? [];
const schemas = await fetchTableSchemas(tableNames, api, dataSetManager);
const schemas = await fetchTableSchemas(tableNames, api, connectionService);

schemas.forEach((schema) => {
if (schema.body?.fields?.length > 0) {
const columns = schema.body.fields.find((col: any) => col.name === 'COLUMN_NAME');
const fieldTypes = schema.body.fields.find((col: any) => col.name === 'DATA_TYPE');
if (columns && fieldTypes) {
finalSuggestions.push(
...columns.values.map((col: string) => ({
...columns.values.map((col: string, index: number) => ({
text: col,
type: monaco.languages.CompletionItemKind.Field,
type: 'field',
fieldType: fieldTypes.values[index],
}))
);
}
}
});

// later TODO: fetch column values, currently within the industry, it's not a common practice to suggest
// values due to different types of the columns as well as the performance impact. For now just avoid it.
if (
'suggestValuesForColumn' in suggestions &&
/\S/.test(suggestions.suggestValuesForColumn as string) &&
suggestions.suggestValuesForColumn !== undefined
) {
const values = await fetchColumnValues(
tableNames,
suggestions.suggestValuesForColumn as string,
api,
connectionService
);
values.forEach((value) => {
if (value.body?.fields?.length > 0) {
finalSuggestions.push(
...value.body.fields[0].values.map((colVal: string) => ({
text: `'${colVal}'`,
type: 'value',
}))
);
}
});
}
}

// Fill in aggregate functions
if ('suggestAggregateFunctions' in suggestions && suggestions.suggestAggregateFunctions) {
finalSuggestions.push(
...SQL_SYMBOLS.AGREGATE_FUNCTIONS.map((af) => ({
text: af,
type: monaco.languages.CompletionItemKind.Function,
type: 'function',
}))
);
}
Expand All @@ -94,13 +116,12 @@ export const getSuggestions = async ({
finalSuggestions.push(
...(suggestions.suggestKeywords ?? []).map((sk) => ({
text: sk.value,
type: monaco.languages.CompletionItemKind.Keyword,
type: 'keyword',
}))
);
}
} catch (error) {
// TODO: pipe error to the UI
return [];
}

return finalSuggestions;
Expand Down
Loading

0 comments on commit 65b9171

Please sign in to comment.