Skip to content

Commit

Permalink
[ES|QL] Add automated script to sync operators and add support for MA…
Browse files Browse the repository at this point in the history
…TCH operators (elastic#205565)

Closes elastic#188978
Closes elastic#199092

This PR addresses elastic#199092 and
adds automated script to sync operators and adds support for MATCH and
its equivalent `:` match operator.

https://github.com/user-attachments/assets/8f78345d-f1a0-4be4-8eba-2aae35225842

https://github.com/user-attachments/assets/eb9e63e3-0086-45a5-b782-199225895afc

### Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [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
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...

---------

Co-authored-by: Stratoula Kalafateli <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
(cherry picked from commit 6fed083)
  • Loading branch information
qn895 committed Jan 29, 2025
1 parent d852ec2 commit d216d84
Show file tree
Hide file tree
Showing 16 changed files with 5,697 additions and 331 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('WHERE <expression>', () => {
const expectedComparisonWithDateSuggestions = [
...getDateLiterals(),
...getFieldNamesByType(['date']),
// all functions compatible with a keywordField type
...getFunctionSignaturesByReturnType('where', ['date'], { scalar: true }),
...getFieldNamesByType(['date_nanos']),
...getFunctionSignaturesByReturnType('where', ['date', 'date_nanos'], { scalar: true }),
];
await assertSuggestions(
'from a | where dateField == /',
Expand Down Expand Up @@ -213,10 +213,18 @@ describe('WHERE <expression>', () => {
]);
await assertSuggestions('from index | WHERE not /', [
...getFieldNamesByType('boolean').map((name) => attachTriggerCommand(`${name} `)),
...getFunctionSignaturesByReturnType('where', 'boolean', { scalar: true }),
...getFunctionSignaturesByReturnType('where', 'boolean', { scalar: true }, undefined, [
':',
]),
]);
await assertSuggestions('FROM index | WHERE NOT ENDS_WITH(keywordField, "foo") /', [
...getFunctionSignaturesByReturnType('where', 'boolean', { builtin: true }, ['boolean']),
...getFunctionSignaturesByReturnType(
'where',
'boolean',
{ builtin: true },
['boolean'],
[':']
),
pipeCompleteItem,
]);
await assertSuggestions('from index | WHERE keywordField IS NOT/', [
Expand Down Expand Up @@ -291,9 +299,13 @@ describe('WHERE <expression>', () => {
const { assertSuggestions } = await setup();

await assertSuggestions('FROM index | WHERE doubleField + doubleField /', [
...getFunctionSignaturesByReturnType('where', 'any', { builtin: true, skipAssign: true }, [
'double',
]),
...getFunctionSignaturesByReturnType(
'where',
'any',
{ builtin: true, skipAssign: true },
['double'],
[':']
),
]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import {
getOperatorSuggestions,
getSuggestionsAfterNot,
} from './factories';
import { EDITOR_MARKER, METADATA_FIELDS } from '../shared/constants';
import { EDITOR_MARKER, FULL_TEXT_SEARCH_FUNCTIONS, METADATA_FIELDS } from '../shared/constants';
import { getAstContext, removeMarkerArgFromArgsList } from '../shared/context';
import {
buildQueryUntilPreviousCommand,
Expand Down Expand Up @@ -1229,10 +1229,12 @@ async function getFunctionArgsSuggestions(
}))
);
}

// could also be in stats (bucket) but our autocomplete is not great yet
if (
(getTypesFromParamDefs(typesToSuggestNext).includes('date') &&
['where', 'eval'].includes(command.name)) ||
['where', 'eval'].includes(command.name) &&
!FULL_TEXT_SEARCH_FUNCTIONS.includes(fnDefinition.name)) ||
(command.name === 'stats' &&
typesToSuggestNext.some((t) => t && t.type === 'date' && t.constantOnly === true))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { getOverlapRange, getSuggestionsToRightOfOperatorExpression } from '../.
import { getPosition } from './util';
import { pipeCompleteItem } from '../../complete_items';
import {
EDITOR_MARKER,
UNSUPPORTED_COMMANDS_BEFORE_MATCH,
UNSUPPORTED_COMMANDS_BEFORE_QSTR,
} from '../../../shared/constants';
Expand Down Expand Up @@ -169,12 +170,14 @@ export async function suggest(
if (priorCommands.some((c) => UNSUPPORTED_COMMANDS_BEFORE_QSTR.has(c))) {
ignored.push('qstr');
}

const columnSuggestions = await getColumnsByType('any', [], {
advanceCursor: true,
openSuggestions: true,
});

const last = fullTextAst?.[fullTextAst.length - 1];
let columnSuggestions: SuggestionRawDefinition[] = [];
if (!last?.text?.endsWith(`:${EDITOR_MARKER}`)) {
columnSuggestions = await getColumnsByType('any', [], {
advanceCursor: true,
openSuggestions: true,
});
}
suggestions.push(
...columnSuggestions,
...getFunctionSuggestions({ command: 'where', ignored })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ export async function getSuggestionsToRightOfOperatorExpression({
operatorReturnType === 'unknown' || operatorReturnType === 'unsupported'
? 'any'
: operatorReturnType,
ignored: ['='],
ignored: ['=', ':'],
})
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/

import { i18n } from '@kbn/i18n';
import { ESQL_NUMBER_TYPES, isNumericType } from '../shared/esql_types';
import { isNumericType } from '../shared/esql_types';
import type { FunctionDefinition, FunctionParameterType, FunctionReturnType } from './types';

import { operatorsFunctionDefinitions } from './generated/operators';
type MathFunctionSignature = [FunctionParameterType, FunctionParameterType, FunctionReturnType];

function createMathDefinition(
Expand Down Expand Up @@ -384,150 +384,6 @@ export const comparisonFunctions: FunctionDefinition[] = [
},
].map((op): FunctionDefinition => createComparisonDefinition(op));

const likeFunctions: FunctionDefinition[] = [
// Skip the insensitive case equality until it gets restored back
// new special comparison operator for strings only
// {
// name: '=~',
// description: i18n.translate('kbn-esql-validation-autocomplete.esql.definition.equalToCaseInsensitiveDoc', {
// defaultMessage: 'Case insensitive equality',
// }),
// },
{
name: 'like',
description: i18n.translate('kbn-esql-validation-autocomplete.esql.definition.likeDoc', {
defaultMessage: 'Filter data based on string patterns',
}),
},
{ name: 'not_like', description: '' },
{
name: 'rlike',
description: i18n.translate('kbn-esql-validation-autocomplete.esql.definition.rlikeDoc', {
defaultMessage: 'Filter data based on string regular expressions',
}),
},
{ name: 'not_rlike', description: '' },
].map(({ name, description }) => {
const def: FunctionDefinition = {
type: 'builtin' as const,
ignoreAsSuggestion: /not/.test(name),
name,
description,
supportedCommands: ['eval', 'where', 'row', 'sort'],
supportedOptions: ['by'],
signatures: [
{
params: [
{ name: 'left', type: 'text' as const },
{ name: 'right', type: 'text' as const },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'text' as const },
{ name: 'right', type: 'keyword' as const },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'keyword' as const },
{ name: 'right', type: 'text' as const },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'keyword' as const },
{ name: 'right', type: 'keyword' as const },
],
returnType: 'boolean',
},
],
};

return def;
});

const inFunctions: FunctionDefinition[] = [
{
name: 'in',
description: i18n.translate('kbn-esql-validation-autocomplete.esql.definition.inDoc', {
defaultMessage:
'Tests if the value an expression takes is contained in a list of other expressions',
}),
},
{ name: 'not_in', description: '' },
].map<FunctionDefinition>(({ name, description }) => ({
// set all arrays to type "any" for now
// this only applies to the "in" operator
// e.g. "foo" in ( "foo", "bar" )
//
// we did this because the "in" operator now supports
// mixed-type arrays like ( "1.2.3", versionVar )
// because of implicit casting.
//
// we need to revisit with more robust validation
type: 'builtin',
ignoreAsSuggestion: /not/.test(name),
name,
description,
supportedCommands: ['eval', 'where', 'row', 'sort'],
signatures: [
...ESQL_NUMBER_TYPES.map((type) => ({
params: [
{ name: 'left', type: type as FunctionParameterType },

{ name: 'right', type: 'any[]' as FunctionParameterType },
],
returnType: 'boolean' as FunctionReturnType,
})),
{
params: [
{ name: 'left', type: 'keyword' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'text' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'boolean' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'date' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'version' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
{
params: [
{ name: 'left', type: 'ip' },
{ name: 'right', type: 'any[]' },
],
returnType: 'boolean',
},
],
}));

export const logicalOperators: FunctionDefinition[] = [
{
name: 'and',
Expand Down Expand Up @@ -681,10 +537,7 @@ const otherDefinitions: FunctionDefinition[] = [
];

export const builtinFunctions: FunctionDefinition[] = [
...mathFunctions,
...comparisonFunctions,
...likeFunctions,
...inFunctions,
...operatorsFunctionDefinitions,
...logicalOperators,
...nullFunctions,
...otherDefinitions,
Expand Down
Loading

0 comments on commit d216d84

Please sign in to comment.