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 @@ -73,218 +73,4 @@ describe('commands.stats', () => {
});
});
});

describe('.summarizeCommand()', () => {
it('returns summary of a simple field, defined through assignment', () => {
const src = 'FROM index | STATS foo = agg(bar)';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary).toMatchObject({
command,
aggregates: {
foo: {
arg: {
type: 'function',
name: '=',
},
field: 'foo',
column: {
type: 'column',
name: 'foo',
},
definition: {
type: 'function',
name: 'agg',
args: [
{
type: 'column',
name: 'bar',
},
],
},
terminals: [
{
type: 'column',
name: 'bar',
},
],
usedFields: new Set(['bar']),
},
},
});
});

it('can summarize field defined without assignment', () => {
const src = 'FROM index | STATS agg( /* haha 😅 */ max(foo), bar, baz)';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary).toMatchObject({
command,
aggregates: {
'`agg( /* haha 😅 */ max(foo), bar, baz)`': {
arg: {
type: 'function',
name: 'agg',
},
field: '`agg( /* haha 😅 */ max(foo), bar, baz)`',
column: {
type: 'column',
name: '`agg( /* haha 😅 */ max(foo), bar, baz)`',
},
definition: {
type: 'function',
name: 'agg',
},
terminals: [
{
type: 'column',
name: 'foo',
},
{
type: 'column',
name: 'bar',
},
{
type: 'column',
name: 'baz',
},
],
usedFields: new Set(['foo', 'bar', 'baz']),
},
},
});
});

it('returns a map of stats about two fields', () => {
const src = 'FROM index | STATS foo = agg(f1) + agg(f2), a.b = agg(f3)';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary).toMatchObject({
aggregates: {
foo: {
field: 'foo',
},
'a.b': {
field: 'a.b',
},
},
});
});

describe('BY option', () => {
it('can collect fields from the BY option', () => {
const src = 'FROM index | STATS max(1) BY abc';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary.aggregates).toEqual({
'`max(1)`': expect.any(Object),
});
});

it('returns all "grouping" fields', () => {
const src = 'FROM index | STATS max(1) BY a, b, c';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary.aggregates).toEqual({
'`max(1)`': expect.any(Object),
});
expect(summary.grouping).toMatchObject({
a: expect.any(Object),
b: expect.any(Object),
c: expect.any(Object),
});
});

it('returns grouping destination fields', () => {
const src = 'FROM index | STATS max(1) BY a, b, c';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary.aggregates).toEqual({
'`max(1)`': expect.any(Object),
});
expect(summary.grouping).toMatchObject({
a: expect.any(Object),
b: expect.any(Object),
c: expect.any(Object),
});
});

it('returns grouping "used" fields', () => {
const src = 'FROM index | STATS max(1) BY a, b, c';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary.grouping).toMatchObject({
a: expect.any(Object),
b: expect.any(Object),
c: expect.any(Object),
});
});

it('can have params and quoted fields in grouping', () => {
const src = 'FROM index | STATS max(1) BY `a😎`, ?123, a.?b.?0.`😎`';
const query = EsqlQuery.fromSrc(src);

const command = commands.stats.byIndex(query.ast, 0)!;
const summary = commands.stats.summarizeCommand(query, command);

expect(summary.aggregates).toEqual({
'`max(1)`': expect.any(Object),
});
expect(summary.grouping).toMatchObject({
'`a😎`': expect.any(Object),
// '?123': expect.any(Object),
'a.?b.?0.`😎`': expect.any(Object),
});
});
});
});

describe('.summarize()', () => {
it('can summarize multiple stats commands', () => {
const src = 'FROM index | LIMIT 1 | STATS agg() | LIMIT 2 | STATS max(a, b, c), max2(d.e)';
const query = EsqlQuery.fromSrc(src);
const summary = commands.stats.summarize(query);

expect(summary).toMatchObject([
{
aggregates: {
'`agg()`': {
field: '`agg()`',
},
},
},
{
aggregates: {
'`max(a, b, c)`': {
field: '`max(a, b, c)`',
},
'`max2(d.e)`': {
field: '`max2(d.e)`',
},
},
},
]);
});
});
});
Loading
Loading