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

Esql composer ideas #60

Open
wants to merge 4 commits into
base: esql-composer
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions packages/kbn-esql-composer/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { from } from './src/commands/from';
Expand Down
9 changes: 5 additions & 4 deletions packages/kbn-esql-composer/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
Expand Down
24 changes: 17 additions & 7 deletions packages/kbn-esql-composer/src/commands/append.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Command, QueryOperator } from '../types';
import { isObject } from 'lodash';
import { Command, QueryOperator, Params, Query } from '../types';

export function append(command: Command | string): QueryOperator {
return (source) => {
export function append({
command,
params,
}: {
command: Command | string;
params?: Params;
}): QueryOperator {
return (source): Query => {
const nextCommand = typeof command === 'string' ? { body: command } : command;

return {
...source,
commands: source.commands.concat(nextCommand),
params: !!params ? source.params.concat(isObject(params) ? params : [params]) : source.params,
};
};
}
24 changes: 14 additions & 10 deletions packages/kbn-esql-composer/src/commands/drop.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { drop } from './drop';
import { from } from './from';

describe('drop', () => {
const source = from('logs-*');
it('handles single strings', () => {
expect(source.pipe(drop('log.level', 'service.name')).asString()).toEqual(
'FROM `logs-*`\n\t| DROP `log.level`, `service.name`'
);
const pipeline = source.pipe(drop('log.level', 'service.name'));
const queryRequest = pipeline.asRequest();

expect(queryRequest.query).toEqual('FROM `logs-*`\n\t| DROP `log.level`, `service.name`');
});

it('handles arrays of strings', () => {
expect(source.pipe(drop(['log.level', 'service.name'])).asString()).toEqual(
'FROM `logs-*`\n\t| DROP `log.level`, `service.name`'
);
const pipeline = source.pipe(drop(['log.level', 'service.name']));
const queryRequest = pipeline.asRequest();

expect(queryRequest.query).toEqual('FROM `logs-*`\n\t| DROP `log.level`, `service.name`');
});
});
25 changes: 14 additions & 11 deletions packages/kbn-esql-composer/src/commands/drop.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { escapeIdentifier } from '../utils/escape_identifier';
import { escapeIdentifier } from '../utils/formatters';
import { append } from './append';

export function drop(...columns: Array<string | string[]>) {
return append(
`DROP ${columns
.flatMap((column) => column)
.map((column) => escapeIdentifier(column))
.join(', ')}`
);
const command = `DROP ${columns
.flatMap((column) => column)
.map((column) => escapeIdentifier(column))
.join(', ')}`;

return append({
command,
});
}
48 changes: 48 additions & 0 deletions packages/kbn-esql-composer/src/commands/eval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { evaluate } from './eval';
import { from } from './from';

describe('evaluate', () => {
const source = from('logs-*');

it('handles single strings', () => {
const pipeline = source.pipe(
evaluate('type = CASE(languages <= 1, "monolingual",languages <= 2, "bilingual","polyglot")')
);

expect(pipeline.asString()).toEqual(
'FROM `logs-*`\n\t| EVAL type = CASE(languages <= 1, "monolingual",languages <= 2, "bilingual","polyglot")'
);
});

it('handles EVAL with params', () => {
const pipeline = source.pipe(
evaluate('hour = DATE_TRUNC(1 hour, ?ts)', {
ts: {
identifier: '@timestamp',
},
})
);
const queryRequest = pipeline.asRequest();

expect(queryRequest.query).toEqual('FROM `logs-*`\n\t| EVAL hour = DATE_TRUNC(1 hour, ?ts)');
expect(queryRequest.params).toEqual([
{
ts: {
identifier: '@timestamp',
},
},
]);
expect(pipeline.asString()).toEqual(
'FROM `logs-*`\n\t| EVAL hour = DATE_TRUNC(1 hour, `@timestamp`)'
);
});
});
15 changes: 8 additions & 7 deletions packages/kbn-esql-composer/src/commands/eval.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { QueryOperator } from '../types';
import { Params } from '../types';
import { append } from './append';

export function evaluate(body: string): QueryOperator {
return append(`EVAL ${body}`);
export function evaluate(body: string, params?: Params) {
return append({ command: `EVAL ${body}`, params });
}
10 changes: 6 additions & 4 deletions packages/kbn-esql-composer/src/commands/from.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { from } from './from';

describe('from', () => {
Expand Down
13 changes: 8 additions & 5 deletions packages/kbn-esql-composer/src/commands/from.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { createPipeline } from '../create_pipeline';
import type { QueryPipeline } from '../types';
import { escapeIdentifier } from '../utils/escape_identifier';
import { escapeIdentifier } from '../utils/formatters';

export function from(...patterns: Array<string | string[]>): QueryPipeline {
const allPatterns = patterns.flatMap((pattern) => pattern);
Expand All @@ -18,5 +20,6 @@ export function from(...patterns: Array<string | string[]>): QueryPipeline {
body: `FROM ${allPatterns.map((pattern) => escapeIdentifier(pattern)).join(',')}`,
},
],
params: [],
});
}
28 changes: 16 additions & 12 deletions packages/kbn-esql-composer/src/commands/keep.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { from } from './from';
import { keep } from './keep';

describe('keep', () => {
const source = from('logs-*');
it('handles single strings', () => {
expect(source.pipe(keep('log.level', 'service.name')).asString()).toEqual(
'FROM `logs-*`\n\t| KEEP `log.level`, `service.name`'
);
it('should build KEEP from single strings', () => {
const pipeline = source.pipe(keep('log.level', 'service.name'));
const queryRequest = pipeline.asRequest();

expect(queryRequest.query).toEqual('FROM `logs-*`\n\t| KEEP `log.level`, `service.name`');
});

it('handles arrays of strings', () => {
expect(source.pipe(keep(['log.level', 'service.name'])).asString()).toEqual(
'FROM `logs-*`\n\t| KEEP `log.level`, `service.name`'
);
it('should build KEEP from array of strings', () => {
const pipeline = source.pipe(keep(['log.level', 'service.name']));
const queryRequest = pipeline.asRequest();

expect(queryRequest.query).toEqual('FROM `logs-*`\n\t| KEEP `log.level`, `service.name`');
});
});
23 changes: 12 additions & 11 deletions packages/kbn-esql-composer/src/commands/keep.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { escapeIdentifier } from '../utils/escape_identifier';
import { escapeIdentifier } from '../utils/formatters';
import { append } from './append';

export function keep(...columns: Array<string | string[]>) {
return append(
`KEEP ${columns
.flatMap((column) => column)
.map((column) => escapeIdentifier(column))
.join(', ')}`
);
const command = `KEEP ${columns
.flatMap((column) => column)
.map((column) => escapeIdentifier(column))
.join(', ')}`;

return append({ command });
}
11 changes: 6 additions & 5 deletions packages/kbn-esql-composer/src/commands/limit.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { append } from './append';

export function limit(value: number) {
return append(`LIMIT ${value}`);
return append({ command: `LIMIT ${value}` });
}
Loading