forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b53dc27
commit 6ccadc8
Showing
23 changed files
with
904 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { append } from './commands/append'; | ||
import { | ||
QueryOperator, | ||
BuilderCommand, | ||
Params, | ||
ChainedCommand, | ||
QueryBuilderToOperator, | ||
} from './types'; | ||
|
||
export abstract class QueryBuilder implements QueryBuilderToOperator { | ||
protected readonly commands: BuilderCommand[] = []; | ||
|
||
public abstract build(): ChainedCommand; | ||
|
||
public toQueryOperator(): QueryOperator { | ||
return append(this.build()); | ||
} | ||
|
||
protected buildChain(): ChainedCommand { | ||
const commandParts: string[] = []; | ||
const bindingParts: Params[] = []; | ||
|
||
for (let i = 0; i < this.commands.length; i++) { | ||
const currentCondition = this.commands[i]; | ||
|
||
if (i > 0) { | ||
commandParts.push(currentCondition.type); | ||
} | ||
|
||
if (typeof currentCondition.command === 'function') { | ||
const innerCommand = currentCondition.command().buildChain(); | ||
commandParts.push( | ||
currentCondition.nested ? `(${innerCommand.command})` : innerCommand.command | ||
); | ||
bindingParts.push(innerCommand.bindings ?? []); | ||
} else { | ||
commandParts.push(currentCondition.command); | ||
bindingParts.push(currentCondition.bindings ?? []); | ||
} | ||
} | ||
|
||
return { | ||
command: commandParts.join(' '), | ||
bindings: bindingParts.flatMap((binding) => binding), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
/* | ||
* 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, | ||
bindings, | ||
}: { | ||
command: Command | string; | ||
bindings?: Params; | ||
}): QueryOperator { | ||
return (source): Query => { | ||
const nextCommand = typeof command === 'string' ? { body: command } : command; | ||
|
||
return { | ||
...source, | ||
commands: source.commands.concat(nextCommand), | ||
bindings: !!bindings | ||
? source.bindings.concat(isObject(bindings) ? bindings : [bindings]) | ||
: source.bindings, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { 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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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")' | ||
); | ||
expect(pipeline.getBindings()).toEqual([]); | ||
}); | ||
|
||
it('handles chained EVAL', () => { | ||
const ids = ['host1', 'host2', 'host3']; | ||
const pipeline = source.pipe( | ||
evaluate('entity.type = ?', 'host') | ||
.concat('entity.display_name = COALESCE(?, entity.id)', 'some_host') | ||
.concat(`entity.id = CONCAT(${ids.map(() => '?').join()})`, ids) | ||
); | ||
expect(pipeline.asString()).toEqual( | ||
'FROM `logs-*`\n\t| EVAL entity.type = ?, entity.display_name = COALESCE(?, entity.id), entity.id = CONCAT(?,?,?)' | ||
); | ||
expect(pipeline.getBindings()).toEqual(['host', 'some_host', 'host1', 'host2', 'host3']); | ||
}); | ||
|
||
it('handles EVAL with params', () => { | ||
const pipeline = source.pipe( | ||
evaluate('hour = DATE_TRUNC(1 hour, ?ts)', { | ||
ts: { | ||
identifier: '@timestamp', | ||
}, | ||
}) | ||
); | ||
|
||
expect(pipeline.asString()).toEqual('FROM `logs-*`\n\t| EVAL hour = DATE_TRUNC(1 hour, ?ts)'); | ||
expect(pipeline.getBindings()).toEqual([ | ||
{ | ||
ts: { | ||
identifier: '@timestamp', | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,42 @@ | ||
/* | ||
* 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 { append } from './append'; | ||
import { QueryBuilder } from '../builder'; | ||
import { ChainedCommand, Params } from '../types'; | ||
|
||
export function evaluate(body: string): QueryOperator { | ||
return append(`EVAL ${body}`); | ||
const EVAL = 'EVAL'; | ||
|
||
class EvalBuilder extends QueryBuilder { | ||
private constructor(body: string, bindings?: Params) { | ||
super(); | ||
this.commands.push({ command: body, bindings, type: EVAL }); | ||
} | ||
|
||
public static create(body: string, bindings?: Params) { | ||
return new EvalBuilder(body, bindings); | ||
} | ||
|
||
public concat(body: string, bindings?: Params) { | ||
this.commands.push({ command: body, bindings, type: EVAL }); | ||
return this; | ||
} | ||
|
||
public build(): ChainedCommand { | ||
const { command, bindings } = this.buildChain(); | ||
|
||
return { | ||
command: `${EVAL} ${command.replace(/\s+EVAL/g, ',')}`, | ||
bindings, | ||
}; | ||
} | ||
} | ||
|
||
export function evaluate(body: string, bindings?: Params) { | ||
return EvalBuilder.create(body, bindings); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { 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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` }); | ||
} |
Oops, something went wrong.