Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KQL] Fix node builder with empty array #155901

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this also work?

Suggested change
return nodes.length === 1 ? nodes[0] : nodeTypes.function.buildNode('or', nodes);
return nodeTypes.function.buildNode('or', nodes);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spoke to you about this offline, but just adding a note here. Returning the first node is a slight optimization (don't need to build an extra node, and don't have to wrap the ES query in an additional unnecessary bool clause).

},
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',
});
});
});