Skip to content

Commit

Permalink
implement esql ast visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimkibana committed Jul 30, 2024
1 parent a9ae3e5 commit c388c6f
Show file tree
Hide file tree
Showing 18 changed files with 2,455 additions and 0 deletions.
369 changes: 369 additions & 0 deletions packages/kbn-esql-ast/src/__tests__/ast_parser.commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,369 @@
/*
* 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.
*/

import { getAstAndSyntaxErrors as parse } from '../ast_parser';

describe('commands', () => {
describe('correctly formatted, basic usage', () => {
it('SHOW', () => {
const query = 'SHOW info';
const { ast } = parse(query);

expect(ast).toMatchObject([
{
type: 'command',
name: 'show',
args: [
{
type: 'function',
name: 'info',
},
],
},
]);
});

it('META', () => {
const query = 'META functions';
const { ast } = parse(query);

expect(ast).toMatchObject([
{
type: 'command',
name: 'meta',
args: [
{
type: 'function',
name: 'functions',
},
],
},
]);
});

it('FROM', () => {
const query = 'FROM index';
const { ast } = parse(query);

expect(ast).toMatchObject([
{
type: 'command',
name: 'from',
args: [
{
type: 'source',
name: 'index',
},
],
},
]);
});

it('ROW', () => {
const query = 'ROW 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{
type: 'command',
name: 'row',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('EVAL', () => {
const query = 'FROM index | EVAL 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'eval',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('STATS', () => {
const query = 'FROM index | STATS 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'stats',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('LIMIT', () => {
const query = 'FROM index | LIMIT 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'limit',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('KEEP', () => {
const query = 'FROM index | KEEP abc';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'keep',
args: [
{
type: 'column',
name: 'abc',
},
],
},
]);
});

it('SORT', () => {
const query = 'FROM index | SORT 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'sort',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('WHERE', () => {
const query = 'FROM index | WHERE 1';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'where',
args: [
{
type: 'literal',
value: 1,
},
],
},
]);
});

it('DROP', () => {
const query = 'FROM index | DROP abc';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'drop',
args: [
{
type: 'column',
name: 'abc',
},
],
},
]);
});

it('RENAME', () => {
const query = 'FROM index | RENAME a AS b, c AS d';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'rename',
args: [
{
type: 'option',
name: 'as',
args: [
{
type: 'column',
name: 'a',
},
{
type: 'column',
name: 'b',
},
],
},
{
type: 'option',
name: 'as',
args: [
{
type: 'column',
name: 'c',
},
{
type: 'column',
name: 'd',
},
],
},
],
},
]);
});

it('DISSECT', () => {
const query = 'FROM index | DISSECT a "b" APPEND_SEPARATOR="c"';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'dissect',
args: [
{
type: 'column',
name: 'a',
},
{
type: 'literal',
value: '"b"',
},
{
type: 'option',
name: 'append_separator',
args: [
{
type: 'literal',
value: '"c"',
},
],
},
],
},
]);
});

it('GROK', () => {
const query = 'FROM index | GROK a "b"';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'grok',
args: [
{
type: 'column',
name: 'a',
},
{
type: 'literal',
value: '"b"',
},
],
},
]);
});

it('ENRICH', () => {
const query = 'FROM index | ENRICH a ON b WITH c, d';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'enrich',
args: [
{
type: 'source',
name: 'a',
},
{
type: 'option',
name: 'on',
args: [
{
type: 'column',
name: 'b',
},
],
},
{
type: 'option',
name: 'with',
},
],
},
]);
});

it('MV_EXPAND', () => {
const query = 'FROM index | MV_EXPAND a ';
const { ast } = parse(query);

expect(ast).toMatchObject([
{},
{
type: 'command',
name: 'mv_expand',
args: [
{
type: 'column',
name: 'a',
},
],
},
]);
});
});
});
25 changes: 25 additions & 0 deletions packages/kbn-esql-ast/src/__tests__/ast_parser.literal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

import { getAstAndSyntaxErrors as parse } from '../ast_parser';
import { ESQLLiteral } from '../types';

describe('literal expression', () => {
it('numeric expression captures "value", and "name" fields', () => {
const text = 'ROW 1';
const { ast } = parse(text);
const literal = ast[0].args[0] as ESQLLiteral;

expect(literal).toMatchObject({
type: 'literal',
literalType: 'number',
name: '1',
value: 1,
});
});
});
Loading

0 comments on commit c388c6f

Please sign in to comment.