Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,67 @@ describe('order', () => {
expect(text).toBe('a.b.c ASC NULLS FIRST');
});
});

describe('map', () => {
test('can construct an empty map', () => {
const node1 = Builder.expression.map();
const node2 = Builder.expression.map({});
const node3 = Builder.expression.map({
entries: [],
});

expect(node1).toMatchObject({
type: 'map',
entries: [],
});
expect(node2).toMatchObject({
type: 'map',
entries: [],
});
expect(node3).toMatchObject({
type: 'map',
entries: [],
});
});

test('can construct a map with two keys', () => {
const node = Builder.expression.map({
entries: [
Builder.expression.entry('foo', Builder.expression.literal.integer(1)),
Builder.expression.entry('bar', Builder.expression.literal.integer(2)),
],
});

expect(node).toMatchObject({
type: 'map',
entries: [
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'foo',
},
value: {
type: 'literal',
literalType: 'integer',
value: 1,
},
},
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'bar',
},
value: {
type: 'literal',
literalType: 'integer',
value: 2,
},
},
],
});
});
});
38 changes: 38 additions & 0 deletions src/platform/packages/shared/kbn-esql-ast/src/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
ESQLNullLiteral,
BinaryExpressionOperator,
ESQLParamKinds,
ESQLMap,
ESQLMapEntry,
} from '../types';
import { AstNodeParserFields, AstNodeTemplate, PartialFields } from './types';

Expand Down Expand Up @@ -439,6 +441,42 @@ export namespace Builder {
};
};
}

export const map = (
template: Omit<AstNodeTemplate<ESQLMap>, 'name' | 'entries'> &
Partial<Pick<ESQLMap, 'entries'>> = {},
fromParser?: Partial<AstNodeParserFields>
): ESQLMap => {
const entries = template.entries ?? [];

return {
...template,
...Builder.parserFields(fromParser),
name: '',
type: 'map',
entries,
};
};

export const entry = (
key: string | ESQLMapEntry['key'],
value: ESQLMapEntry['value'],
fromParser?: Partial<AstNodeParserFields>,
template?: Omit<AstNodeTemplate<ESQLMapEntry>, 'key' | 'value'>
): ESQLMapEntry => {
if (typeof key === 'string') {
key = Builder.expression.literal.string(key);
}

return {
...template,
...Builder.parserFields(fromParser),
name: '',
type: 'map-entry',
key,
value,
};
};
}

export const identifier = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* 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 { parse } from '..';

describe('map expression', () => {
it('function call with an empty trailing map errors', () => {
const query = 'ROW fn(1, {})';
const { errors } = parse(query);

expect(errors.length > 0).toBe(true);
});

it('errors when trailing map argument is the single function argument', () => {
const query = 'ROW fn({"foo" : "bar"})';
const { errors } = parse(query);

expect(errors.length > 0).toBe(true);
});

it('function call with a trailing map with a single entry', () => {
const query = 'ROW fn(1, {"foo" : "bar"})';
const { ast, errors } = parse(query);

expect(errors.length).toBe(0);
expect(ast).toMatchObject([
{
type: 'command',
name: 'row',
args: [
{
type: 'function',
name: 'fn',
args: [
{
type: 'literal',
value: 1,
},
{
type: 'map',
entries: [
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'foo',
},
value: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'bar',
},
},
],
},
],
},
],
},
]);
});

it('multiple trailing map arguments with multiple keys', () => {
const query =
'ROW fn(1, fn2(1, {"a": TRUE, /* asdf */ "b" : 123}), {"foo" : "bar", "baz" : [1, 2, 3]})';
const { ast, errors } = parse(query);

expect(errors.length).toBe(0);
expect(ast).toMatchObject([
{
type: 'command',
name: 'row',
args: [
{
type: 'function',
name: 'fn',
args: [
{
type: 'literal',
value: 1,
},
{
type: 'function',
name: 'fn2',
args: [
{
type: 'literal',
value: 1,
},
{
type: 'map',
entries: [
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'a',
},
value: {
type: 'literal',
literalType: 'boolean',
value: 'TRUE',
},
},
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'b',
},
value: {
type: 'literal',
literalType: 'integer',
value: 123,
},
},
],
},
],
},
{
type: 'map',
entries: [
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'foo',
},
value: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'bar',
},
},
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'baz',
},
value: {
type: 'list',
values: [
{
type: 'literal',
literalType: 'integer',
value: 1,
},
{
type: 'literal',
literalType: 'integer',
value: 2,
},
{
type: 'literal',
literalType: 'integer',
value: 3,
},
],
},
},
],
},
],
},
],
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import { createGrokCommand } from './factories/grok';
import { createStatsCommand } from './factories/stats';
import { createChangePointCommand } from './factories/change_point';
import { createWhereCommand } from './factories/where';
import { createRowCommand } from './factories/row';

export class ESQLAstBuilderListener implements ESQLParserListener {
private ast: ESQLAst = [];
Expand Down Expand Up @@ -112,9 +113,9 @@ export class ESQLAstBuilderListener implements ESQLParserListener {
* @param ctx the parse tree
*/
exitRowCommand(ctx: RowCommandContext) {
const command = createCommand('row', ctx);
const command = createRowCommand(ctx);

this.ast.push(command);
command.args.push(...collectAllFields(ctx.fields()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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", 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 { RowCommandContext } from '../../antlr/esql_parser';
import { ESQLCommand } from '../../types';
import { createCommand } from '../factories';
import { collectAllFields } from '../walkers';

export const createRowCommand = (ctx: RowCommandContext): ESQLCommand<'row'> => {
const command = createCommand('row', ctx);
const fields = collectAllFields(ctx.fields());

command.args.push(...fields);

return command;
};
Loading