Skip to content

Commit

Permalink
feat(YQL): suggest view and external table in select statements (#188)
Browse files Browse the repository at this point in the history
Co-authored-by: Elena Makarova <[email protected]>
  • Loading branch information
Raubzeug and Elena Makarova authored May 31, 2024
1 parent dcda173 commit 416248c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
24 changes: 14 additions & 10 deletions src/autocomplete/databases/yql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ function getTopicSuggestions({anyRuleInList}: GetParticularSuggestionProps): boo
return anyRuleInList([YQLParser.RULE_drop_topic_stmt, YQLParser.RULE_alter_topic_stmt]);
}

function getViewSuggestions({anyRuleInList}: GetParticularSuggestionProps): boolean | undefined {
if (!anyRuleInList(YQLParser.RULE_id_or_at)) {
return;
}
return anyRuleInList(YQLParser.RULE_drop_view_stmt);
function getViewSuggestions({allRulesInList}: GetParticularSuggestionProps): boolean | undefined {
return (
allRulesInList([YQLParser.RULE_drop_view_stmt, YQLParser.RULE_id_or_at]) ||
allRulesInList([YQLParser.RULE_table_ref, YQLParser.RULE_id_table_or_type])
);
}

function getReplicationSuggestions({
Expand All @@ -215,17 +215,21 @@ function getReplicationSuggestions({
}

function getExternalTableSuggestions({
anyRuleInList,
allRulesInList,
tokenStream,
cursorTokenIndex,
}: GetParticularSuggestionProps): boolean | undefined {
if (!anyRuleInList(YQLParser.RULE_id_or_at)) {
return;
}
const hasPreviousTokenExternal = Boolean(
getPreviousToken(tokenStream, tokenDictionary, cursorTokenIndex, YQLParser.EXTERNAL),
);
return anyRuleInList(YQLParser.RULE_drop_table_stmt) && hasPreviousTokenExternal;

const externalTableInDropTable =
allRulesInList([YQLParser.RULE_id_or_at, YQLParser.RULE_drop_table_stmt]) &&
hasPreviousTokenExternal;
return (
externalTableInDropTable ||
allRulesInList([YQLParser.RULE_table_ref, YQLParser.RULE_id_table_or_type])
);
}

function getExternalDatasourceSuggestions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('should suggest keywords after FULL OUTER', () => {
test('should suggest tables after JOIN', () => {
const autocompleteResult = parseYqQueryWithCursor('SELECT * FROM test_table JOIN |');

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestAggregateFunctions).toBeFalsy();
expect(autocompleteResult.suggestWindowFunctions).toBeFalsy();
expect(autocompleteResult.suggestFunctions).toBeFalsy();
Expand All @@ -80,7 +80,7 @@ test('should suggest tables after JOIN between statements', () => {
'SELECT * FROM before_table; SELECT * FROM test_table JOIN | ; SELECT * FROM after_table;',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestAggregateFunctions).toBeFalsy();
expect(autocompleteResult.suggestWindowFunctions).toBeFalsy();
expect(autocompleteResult.suggestFunctions).toBeFalsy();
Expand Down
12 changes: 6 additions & 6 deletions src/autocomplete/databases/yql/tests/yq/select/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('should suggest properly after FROM', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest properly after *', () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ test('should suggest properly after FROM', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestTableFunctions).toBeTruthy();
});

Expand All @@ -81,7 +81,7 @@ test('should suggest properly after FROM when typing table name', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestColumns).toBeFalsy();
});

Expand All @@ -90,7 +90,7 @@ test('should suggest tables between statements', () => {
'SELECT * FROM before_table; SELECT * FROM | ; SELECT * FROM after_table;',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest properly after table name', () => {
Expand Down Expand Up @@ -298,15 +298,15 @@ test('should suggest properly after LIMIT', () => {

test('should suggest tables with inline comment', () => {
const autocompleteResult = parseYqQueryWithCursor('SELECT * FROM | --SELECT * FROM test_table');
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest tables with multiline comment', () => {
const autocompleteResult = parseYqQueryWithCursor(
'SELECT * FROM | /*SELECT * FROM test_table*/',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should not report errors', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('should suggest keywords after FULL OUTER', () => {
test('should suggest tables after JOIN', () => {
const autocompleteResult = parseYqlQueryWithCursor('SELECT * FROM test_table JOIN |');

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestAggregateFunctions).toBeFalsy();
expect(autocompleteResult.suggestWindowFunctions).toBeFalsy();
expect(autocompleteResult.suggestFunctions).toBeFalsy();
Expand All @@ -80,7 +80,7 @@ test('should suggest tables after JOIN between statements', () => {
'ALTER TABLE before_table DROP COLUMN id; SELECT * FROM test_table JOIN | ; ALTER TABLE after_table DROP COLUMN id;',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestAggregateFunctions).toBeFalsy();
expect(autocompleteResult.suggestWindowFunctions).toBeFalsy();
expect(autocompleteResult.suggestFunctions).toBeFalsy();
Expand Down
12 changes: 6 additions & 6 deletions src/autocomplete/databases/yql/tests/yql/select/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('should suggest properly after FROM', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest properly after *', () => {
Expand Down Expand Up @@ -72,7 +72,7 @@ test('should suggest properly after FROM', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestTableFunctions).toBeTruthy();
});

Expand All @@ -81,7 +81,7 @@ test('should suggest properly after FROM when typing table name', () => {

const keywordsSuggestion: KeywordSuggestion[] = [{value: 'ANY'}];
expect(autocompleteResult.suggestKeywords).toEqual(keywordsSuggestion);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
expect(autocompleteResult.suggestColumns).toBeFalsy();
});

Expand All @@ -90,7 +90,7 @@ test('should suggest tables between statements', () => {
'ALTER TABLE before_table DROP COLUMN id; SELECT * FROM | ; ALTER TABLE after_table DROP COLUMN id;',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest properly after table name', () => {
Expand Down Expand Up @@ -300,15 +300,15 @@ test('should suggest tables with inline comment', () => {
const autocompleteResult = parseYqlQueryWithCursor(
'SELECT * FROM | --SELECT * FROM test_table',
);
expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should suggest tables with multiline comment', () => {
const autocompleteResult = parseYqlQueryWithCursor(
'SELECT * FROM | /*SELECT * FROM test_table*/',
);

expect(autocompleteResult.suggestEntity).toEqual(['table']);
expect(autocompleteResult.suggestEntity).toEqual(['table', 'view', 'externalTable']);
});

test('should not report errors', () => {
Expand Down

0 comments on commit 416248c

Please sign in to comment.