Skip to content

Commit

Permalink
ppl pref rule field name expansion
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sebastian <[email protected]>
  • Loading branch information
paulstn committed Jan 15, 2025
1 parent 25c06cd commit 26782e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as c3 from 'antlr4-c3';
import { ParseTree, TokenStream } from 'antlr4ng';
import { ParseTree, Token, TokenStream } from 'antlr4ng';
import {
AutocompleteData,
AutocompleteResultBase,
Expand All @@ -16,6 +16,7 @@ import {
} from '../shared/types';
import { OpenSearchPPLLexer } from './.generated/OpenSearchPPLLexer';
import { OpenSearchPPLParser } from './.generated/OpenSearchPPLParser';
import { removePotentialBackticks } from '../shared/utils';

// These are keywords that we do not want to show in autocomplete
export function getIgnoredTokens(): number[] {
Expand Down Expand Up @@ -72,6 +73,8 @@ const tokenDictionary: any = {
EQUAL: OpenSearchPPLParser.EQUAL,
IN: OpenSearchPPLParser.IN,
COMMA: OpenSearchPPLParser.COMMA,
BACKTICK_QUOTE: OpenSearchPPLParser.BQUOTA_STRING,
DOT: OpenSearchPPLParser.DOT,
};

const rulesToVisit = new Set([
Expand Down Expand Up @@ -167,13 +170,33 @@ export function processVisitedRules(
break;
}

const validIDToken = (token: Token) => {
return token.type === tokenDictionary.ID || token.type === tokenDictionary.BACKTICK_QUOTE;
};

while (currentIndex > -1) {
const token = tokenStream.get(currentIndex);
if (token.type === tokenDictionary.PIPE) {
if (!token || token.type === tokenDictionary.PIPE) {
break;

Check warning on line 180 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#L180

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

// NOTE: according to grammar, backticks in PPL are only possible for fields
if (validIDToken(token)) {
let combinedText = token.text ?? '';

// stitch together IDs separated by DOTs
let lookBehindIndex = currentIndex;
while (lookBehindIndex > -1) {
lookBehindIndex--;
const prevToken = tokenStream.get(lookBehindIndex);
if (!prevToken || prevToken.type !== tokenDictionary.DOT) {
break;
}
lookBehindIndex--;

Check warning on line 195 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#L195

Added line #L195 was not covered by tests
combinedText = `${tokenStream.get(lookBehindIndex).text ?? ''}.${combinedText}`;
}

suggestValuesForColumn = removePotentialBackticks(combinedText);
break;
}
currentIndex--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
getPreviousToken,
} from './table';
import { shouldSuggestTemplates } from './parse';
import { removePotentialBackticks } from '../shared/utils';

const tokenDictionary: TokenDictionary = {
SPACE: OpenSearchSQLParser.SPACE,
Expand Down Expand Up @@ -187,10 +188,6 @@ export function processVisitedRules(
);
};

const removePotentialBackticks = (str: string): string => {
return str.replace(/^`?|\`?$/g, ''); // removes backticks only if they exist at the beginning and end
};

/**
* creates a list of the tokens from the start of the pedicate to the end
* intentionally omit all tokens with type SPACE
Expand Down Expand Up @@ -223,7 +220,7 @@ export function processVisitedRules(
continue;

Check warning on line 220 in src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts#L220

Added line #L220 was not covered by tests
}
sigTokens[sigTokens.length - 1].text +=

Check warning on line 222 in src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts#L222

Added line #L222 was not covered by tests
'.' + removePotentialBackticks(nextToken.text ?? '');
'.' + removePotentialBackticks(nextToken?.text ?? '');
continue;
}

Check warning on line 225 in src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/opensearch_sql/opensearch_sql_autocomplete.ts#L224-L225

Added lines #L224 - L225 were not covered by tests
sigTokens.push(token);
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/antlr/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface IDataSourceRequestHandlerParams {
title: string;
}

export const removePotentialBackticks = (str: string): string => {
return str.replace(/^`?|\`?$/g, ''); // removes backticks only if they exist at the beginning and end
};

// Function to get raw suggestion data
export const getRawSuggestionData$ = (
queryString: QueryStringContract,
Expand Down

0 comments on commit 26782e2

Please sign in to comment.