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 @@ -7,29 +7,29 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { parse } from '..';
import { Parser } from '..';

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

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

it('errors when trailing map argument is the single function argument', () => {
const query = 'ROW fn({"foo" : "bar"})';
const { errors } = parse(query);
const { errors } = Parser.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);
const { root, errors } = Parser.parse(query);

expect(errors.length).toBe(0);
expect(ast).toMatchObject([
expect(root.commands).toMatchObject([
{
type: 'command',
name: 'row',
Expand Down Expand Up @@ -70,10 +70,10 @@ describe('map expression', () => {
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);
const { root, errors } = Parser.parse(query);

expect(errors.length).toBe(0);
expect(ast).toMatchObject([
expect(root.commands).toMatchObject([
{
type: 'command',
name: 'row',
Expand Down Expand Up @@ -179,4 +179,224 @@ describe('map expression', () => {
},
]);
});

it('can parse nested map expression', () => {
const query = 'ROW fn(1, {"foo" : {"bar" : "baz"}})';
const { root, errors } = Parser.parse(query);

expect(errors.length).toBe(0);
expect(root.commands).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: 'map',
entries: [
{
type: 'map-entry',
key: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'bar',
},
value: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'baz',
},
},
],
},
},
],
},
],
},
],
},
]);
});

it('can parse all literal types', () => {
const query = `ROW fn(1, {
"int": 1,
"map": {
"bar" : "baz",
"oneMoreMap": { "a": 1 },
"emptyMap": {}
},
"bool": TRUE,
"str": "hello",
"list": [1, 2, 3],
"float": 1.23,
"null": NULL
})`;
const { root, errors } = Parser.parse(query);

expect(errors.length).toBe(0);
expect(root.commands).toMatchObject([
{
type: 'command',
name: 'row',
args: [
{
type: 'function',
name: 'fn',
args: [
{
type: 'literal',
value: 1,
},
{
type: 'map',
entries: [
{
type: 'map-entry',
key: {
valueUnquoted: 'int',
},
value: { type: 'literal', literalType: 'integer', value: 1 },
},
{
type: 'map-entry',
key: {
valueUnquoted: 'map',
},
value: {
type: 'map',
entries: [
{
type: 'map-entry',
key: {
valueUnquoted: 'bar',
},
value: {
type: 'literal',
literalType: 'keyword',
valueUnquoted: 'baz',
},
},
{
type: 'map-entry',
key: {
valueUnquoted: 'oneMoreMap',
},
value: {
type: 'map',
entries: [
{
type: 'map-entry',
key: {
valueUnquoted: 'a',
},
value: { type: 'literal', literalType: 'integer', value: 1 },
},
],
},
},
{
type: 'map-entry',
key: {
valueUnquoted: 'emptyMap',
},
value: {
type: 'map',
entries: [],
},
},
],
},
},
{
type: 'map-entry',
key: {
valueUnquoted: 'bool',
},
value: { type: 'literal', literalType: 'boolean', value: 'TRUE' },
},
{
type: 'map-entry',
key: {
valueUnquoted: 'str',
},
value: { type: 'literal', literalType: 'keyword', valueUnquoted: 'hello' },
},
{
type: 'map-entry',
key: {
valueUnquoted: 'list',
},
value: {
type: 'list',
values: [
{ type: 'literal', literalType: 'integer', value: 1 },
{ type: 'literal', literalType: 'integer', value: 2 },
{ type: 'literal', literalType: 'integer', value: 3 },
],
},
},
{
type: 'map-entry',
key: {
valueUnquoted: 'float',
},
value: { type: 'literal', literalType: 'double', value: 1.23 },
},
{
type: 'map-entry',
key: {
valueUnquoted: 'null',
},
value: { type: 'literal', literalType: 'null' },
},
],
},
],
},
],
},
]);
});

it('lists are not nestable', () => {
const query = 'ROW fn(1, {"foo" : [{"bar" : "baz"}]})';
const { errors } = Parser.parse(query);

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

describe('malformed', () => {
it('report errors for incomplete field values', () => {
const query = 'ROW FN(1, { "foo":';
const { errors } = Parser.parse(query);

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

it('report errors for incomplete field values - 2', () => {
const query = 'ROW FN(1, { "foo"';
const { errors } = Parser.parse(query);

expect(errors.length > 0).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2411,8 +2411,13 @@ export class CstToAstConverter {

for (const entryCtx of entryCtxs) {
const entry = this.fromMapEntryExpression(entryCtx);

if (entry) {
map.entries.push(entry);

if (entry.incomplete) {
map.incomplete = true;
}
} else {
map.incomplete = true;
}
Expand All @@ -2426,17 +2431,40 @@ export class CstToAstConverter {
const valueCtx = ctx._value;

if (keyCtx && valueCtx) {
let value: ast.ESQLAstExpression | undefined;
const key = this.toStringLiteral(keyCtx) as ast.ESQLStringLiteral;

const value = this.fromConstant(valueCtx.constant()) as ast.ESQLAstExpression;
if (!key) {
return undefined;
}

const constantCtx = valueCtx.constant();

const entry = Builder.expression.entry(key, value, {
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
});
if (constantCtx) {
value = this.fromConstant(constantCtx) as ast.ESQLAstExpression;
}

const mapExpressionCtx = valueCtx.mapExpression();

return entry;
if (mapExpressionCtx) {
value = this.fromMapExpression(mapExpressionCtx);
}

if (!value) {
value = this.fromParserRuleToUnknown(valueCtx);
value.incomplete = true;
}

if (value) {
const entry = Builder.expression.entry(key, value, {
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception) || key.incomplete || value.incomplete,
});

return entry;
}
}
return undefined;
}

// ----------------------------------------------------- constant expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ describe('single line query', () => {

expect(text).toBe('ROW FN(1, {"foo": "bar", "baz": NULL})');
});

test('supports nested maps', () => {
assertReprint('ROW FN(1, {"foo": "bar", "baz": {"a": 1, "b": 2}})');
});
});

describe('literals expressions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ FROM index
expect(text).toBe(`ROW F(0, {})`);
});

test('supports nested maps', () => {
assertReprint('ROW FN(1, {"foo": "bar", "baz": {"a": 1, "b": 2}})');
});

test('empty map (multiline)', () => {
const src = `ROW F(0, {"a": 0}) | LIMIT 1`;
const { root } = parse(src);
Expand Down Expand Up @@ -896,6 +900,21 @@ FROM index
"abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz"
})`);
});

test('supports wrapping in nested maps', () => {
assertReprint(
`ROW
FN(
1,
{
"map":
{
"aaaaaaaaaaaaaaaaaaaaa": 111111111111111,
"bbbbbbbbbbbbbbbbbbbbbbbb": 222222222222222
}
})`
);
});
});

describe('binary expressions', () => {
Expand Down
Loading