Skip to content

Commit

Permalink
[8.8] [KQL] Fix node builder with empty array (#155901) (#156077)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.8`:
- [[KQL] Fix node builder with empty array
(#155901)](#155901)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Lukas
Olson","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-04-27T18:28:12Z","message":"[KQL]
Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic
for `nodeBuilder` when passing an empty array
to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a
`KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:KQL","release_note:skip","backport:skip","Team:DataDiscovery","v8.9.0"],"number":155901,"url":"https://github.com/elastic/kibana/pull/155901","mergeCommit":{"message":"[KQL]
Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic
for `nodeBuilder` when passing an empty array
to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a
`KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/155901","number":155901,"mergeCommit":{"message":"[KQL]
Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic
for `nodeBuilder` when passing an empty array
to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a
`KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae"}}]}]
BACKPORT-->

Co-authored-by: Lukas Olson <[email protected]>
  • Loading branch information
kibanamachine and lukasolson authored Apr 27, 2023
1 parent 1d1b4df commit f657db7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
39 changes: 39 additions & 0 deletions packages/kbn-es-query/src/kuery/node_types/node_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ describe('nodeBuilder', () => {
});

describe('and method', () => {
test('no clauses', () => {
const node = nodeBuilder.and([]);
const query = toElasticsearchQuery(node);
expect(node).toMatchInlineSnapshot(`
Object {
"arguments": Array [],
"function": "and",
"type": "function",
}
`);
expect(query).toMatchInlineSnapshot(`
Object {
"bool": Object {
"filter": Array [],
},
}
`);
});

test('single clause', () => {
const nodes = [nodeBuilder.is('foo', 'bar')];
const query = toElasticsearchQuery(nodeBuilder.and(nodes));
Expand Down Expand Up @@ -166,6 +185,26 @@ describe('nodeBuilder', () => {
});

describe('or method', () => {
test('no clauses', () => {
const node = nodeBuilder.or([]);
const query = toElasticsearchQuery(node);
expect(node).toMatchInlineSnapshot(`
Object {
"arguments": Array [],
"function": "or",
"type": "function",
}
`);
expect(query).toMatchInlineSnapshot(`
Object {
"bool": Object {
"minimum_should_match": 1,
"should": Array [],
},
}
`);
});

test('single clause', () => {
const nodes = [nodeBuilder.is('foo', 'bar')];
const query = toElasticsearchQuery(nodeBuilder.or(nodes));
Expand Down
8 changes: 4 additions & 4 deletions packages/kbn-es-query/src/kuery/node_types/node_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import type { RangeFilterParams } from '../../filters';
import { KueryNode, nodeTypes } from '../types';

export const nodeBuilder = {
is: (fieldName: string, value: string | KueryNode) => {
is: (fieldName: string, value: string | KueryNode): KueryNode => {
return nodeTypes.function.buildNodeWithArgumentNodes('is', [
nodeTypes.literal.buildNode(fieldName),
typeof value === 'string' ? nodeTypes.literal.buildNode(value) : value,
]);
},
or: (nodes: KueryNode[]): KueryNode => {
return nodes.length > 1 ? nodeTypes.function.buildNode('or', nodes) : nodes[0];
return nodes.length === 1 ? nodes[0] : nodeTypes.function.buildNode('or', nodes);
},
and: (nodes: KueryNode[]): KueryNode => {
return nodes.length > 1 ? nodeTypes.function.buildNode('and', nodes) : nodes[0];
return nodes.length === 1 ? nodes[0] : nodeTypes.function.buildNode('and', nodes);
},
range: (
fieldName: string,
operator: keyof Pick<RangeFilterParams, 'gt' | 'gte' | 'lt' | 'lte'>,
value: number | string
) => {
): KueryNode => {
return nodeTypes.function.buildNodeWithArgumentNodes('range', [
nodeTypes.literal.buildNode(fieldName),
operator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,11 @@ describe('SearchSessionService', () => {
const [[findOptions]] = savedObjectsClient.find.mock.calls;
expect(findOptions).toMatchInlineSnapshot(`
Object {
"filter": undefined,
"filter": Object {
"arguments": Array [],
"function": "and",
"type": "function",
},
"page": 0,
"perPage": 5,
"type": "search-session",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ describe('convertRuleIdsToKueryNode', () => {
});

test('should convert empty ids array correctly', () => {
expect(convertRuleIdsToKueryNode([])).toEqual(undefined);
expect(convertRuleIdsToKueryNode([])).toEqual({
arguments: [],
function: 'or',
type: 'function',
});
});
});

0 comments on commit f657db7

Please sign in to comment.