Skip to content

Commit

Permalink
[ES|QL] Fix WHERE autocomplete with MATCH before LIMIT (elastic#210607)
Browse files Browse the repository at this point in the history
## Summary

Related PR elastic#199032


Fixes `WHERE` autocomplete with `MATCH` before `LIMIT`.

The previous check was filtering suggestions based on all present
commands, not just the previous one.

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
darnautov authored Feb 12, 2025
1 parent a402f4e commit 1ccb6db
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,24 @@ describe('WHERE <expression>', () => {
}
});

test('filters suggestions based on previous commands', async () => {
const { assertSuggestions } = await setup();

await assertSuggestions('from a | where / | limit 3', [
...getFieldNamesByType('any')
.map((field) => `${field} `)
.map(attachTriggerCommand),
...allEvalFns,
]);

await assertSuggestions('from a | limit 3 | where / ', [
...getFieldNamesByType('any')
.map((field) => `${field} `)
.map(attachTriggerCommand),
...allEvalFns.filter((fn) => fn.label !== 'QSTR' && fn.label !== 'MATCH'),
]);
});

test('suggests operators after a field name', async () => {
const { assertSuggestions } = await setup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const policies = [
/**
* Utility to filter down the function list for the given type
* It is mainly driven by the return type, but it can be filtered upon with the last optional argument "paramsTypes"
* jsut make sure to pass the arguments in the right order
* just make sure to pass the arguments in the right order
* @param command current command context
* @param expectedReturnType the expected type returned by the function
* @param functionCategories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import { uniq, uniqBy } from 'lodash';
import type {
AstProviderFn,
ESQLAst,
ESQLAstItem,
ESQLCommand,
ESQLCommandOption,
Expand Down Expand Up @@ -163,10 +162,6 @@ export async function suggest(
const { ast } = await astProvider(correctedQuery);
const astContext = getAstContext(innerText, ast, offset);

// But we also need the full ast for the full query
const correctedFullQuery = correctQuerySyntax(fullText, context);
const { ast: fullAst } = await astProvider(correctedFullQuery);

if (astContext.type === 'comment') {
return [];
}
Expand Down Expand Up @@ -230,7 +225,6 @@ export async function suggest(
getPolicies,
getPolicyMetadata,
resourceRetriever?.getPreferences,
fullAst,
resourceRetriever
);
}
Expand Down Expand Up @@ -417,7 +411,6 @@ async function getSuggestionsWithinCommandExpression(
getPolicies: GetPoliciesFn,
getPolicyMetadata: GetPolicyMetadataFn,
getPreferences?: () => Promise<{ histogramBarTarget: number } | undefined>,
fullAst?: ESQLAst,
callbacks?: ESQLCallbacks
) {
const commandDef = getCommandDefinition(command.name);
Expand All @@ -438,7 +431,7 @@ async function getSuggestionsWithinCommandExpression(
(expression: ESQLAstItem | undefined) =>
getExpressionType(expression, references.fields, references.variables),
getPreferences,
fullAst,
commands,
commandDef,
callbacks
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { i18n } from '@kbn/i18n';
import { type ESQLAstItem, ESQLAst, ESQLCommand, mutate, LeafPrinter } from '@kbn/esql-ast';
import { type ESQLAstItem, ESQLCommand, mutate, LeafPrinter } from '@kbn/esql-ast';
import type { ESQLAstJoinCommand } from '@kbn/esql-ast';
import type { ESQLCallbacks } from '../../../shared/types';
import {
Expand Down Expand Up @@ -104,7 +104,7 @@ export const suggest: CommandBaseDefinition<'join'>['suggest'] = async (
getSuggestedVariableName: () => string,
getExpressionType: (expression: ESQLAstItem | undefined) => SupportedDataType | 'unknown',
getPreferences?: () => Promise<{ histogramBarTarget: number } | undefined>,
fullTextAst?: ESQLAst,
previousCommands?: ESQLCommand[],
definition?: CommandDefinition<'join'>,
callbacks?: ESQLCallbacks
): Promise<SuggestionRawDefinition[]> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
type ESQLCommand,
type ESQLSingleAstItem,
type ESQLFunction,
ESQLAst,
} from '@kbn/esql-ast';
import { logicalOperators } from '../../../definitions/builtin';
import { isParameterType, type SupportedDataType } from '../../../definitions/types';
Expand Down Expand Up @@ -42,7 +41,7 @@ export async function suggest(
_getSuggestedVariableName: () => string,
getExpressionType: (expression: ESQLAstItem | undefined) => SupportedDataType | 'unknown',
_getPreferences?: () => Promise<{ histogramBarTarget: number } | undefined>,
fullTextAst?: ESQLAst
previousCommands?: ESQLCommand[]
): Promise<SuggestionRawDefinition[]> {
const suggestions: SuggestionRawDefinition[] = [];

Expand Down Expand Up @@ -163,15 +162,15 @@ export async function suggest(

case 'empty_expression':
// Don't suggest MATCH or QSTR after unsupported commands
const priorCommands = fullTextAst?.map((a) => a.name) ?? [];
const priorCommands = previousCommands?.map((a) => a.name) ?? [];
const ignored = [];
if (priorCommands.some((c) => UNSUPPORTED_COMMANDS_BEFORE_MATCH.has(c))) {
ignored.push('match');
}
if (priorCommands.some((c) => UNSUPPORTED_COMMANDS_BEFORE_QSTR.has(c))) {
ignored.push('qstr');
}
const last = fullTextAst?.[fullTextAst.length - 1];
const last = previousCommands?.[previousCommands.length - 1];
let columnSuggestions: SuggestionRawDefinition[] = [];
if (!last?.text?.endsWith(`:${EDITOR_MARKER}`)) {
columnSuggestions = await getColumnsByType('any', [], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type {
ESQLAst,
ESQLAstItem,
ESQLCommand,
ESQLCommandOption,
Expand Down Expand Up @@ -210,7 +209,7 @@ export interface CommandBaseDefinition<CommandName extends string> {
getSuggestedVariableName: () => string,
getExpressionType: (expression: ESQLAstItem | undefined) => SupportedDataType | 'unknown',
getPreferences?: () => Promise<{ histogramBarTarget: number } | undefined>,
fullTextAst?: ESQLAst,
previousCommands?: ESQLCommand[],
definition?: CommandDefinition<CommandName>,
callbacks?: ESQLCallbacks
) => Promise<SuggestionRawDefinition[]>;
Expand Down

0 comments on commit 1ccb6db

Please sign in to comment.