|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +import { synth } from '../../../..'; |
| 10 | +import type { ESQLFieldWithMetadata, ESQLUserDefinedColumn } from '../../types'; |
| 11 | +import { columnsAfter } from './columns_after'; |
| 12 | + |
| 13 | +// Test data fixtures |
| 14 | +const createPreviousFields = (fields: Array<[string, string]>): ESQLFieldWithMetadata[] => |
| 15 | + fields.map(([name, type]) => ({ name, type } as ESQLFieldWithMetadata)); |
| 16 | + |
| 17 | +describe('INLINESTATS', () => { |
| 18 | + const createUserDefinedColumn = ( |
| 19 | + name: string, |
| 20 | + type: ESQLFieldWithMetadata['type'], |
| 21 | + location = { min: 0, max: 10 } |
| 22 | + ): ESQLUserDefinedColumn => ({ name, type, location }); |
| 23 | + |
| 24 | + const createContext = (userDefinedColumns: Array<[string, ESQLUserDefinedColumn[]]> = []) => ({ |
| 25 | + userDefinedColumns: new Map(userDefinedColumns), |
| 26 | + fields: new Map<string, ESQLFieldWithMetadata>([ |
| 27 | + ['field1', { name: 'field1', type: 'keyword' }], |
| 28 | + ['count', { name: 'count', type: 'double' }], |
| 29 | + ]), |
| 30 | + }); |
| 31 | + |
| 32 | + const expectColumnsAfter = ( |
| 33 | + command: string, |
| 34 | + previousFields: ESQLFieldWithMetadata[], |
| 35 | + userColumns: Array<[string, string]>, |
| 36 | + expectedResult: Array<[string, string]> |
| 37 | + ) => { |
| 38 | + const context = createContext( |
| 39 | + userColumns.map(([name, type]) => [name, [createUserDefinedColumn(name, type as 'keyword')]]) |
| 40 | + ); |
| 41 | + const result = columnsAfter(synth.cmd(command), previousFields, context); |
| 42 | + const expected = createPreviousFields(expectedResult); |
| 43 | + expect(result).toEqual(expected); |
| 44 | + }; |
| 45 | + it('preserves all previous columns and adds the user defined column, when no grouping is given', () => { |
| 46 | + const previousFields = createPreviousFields([ |
| 47 | + ['field1', 'keyword'], |
| 48 | + ['field2', 'double'], |
| 49 | + ]); |
| 50 | + |
| 51 | + expectColumnsAfter( |
| 52 | + 'INLINESTATS var0=AVG(field2)', |
| 53 | + previousFields, |
| 54 | + [['var0', 'double']], |
| 55 | + [ |
| 56 | + ['field1', 'keyword'], |
| 57 | + ['field2', 'double'], |
| 58 | + ['var0', 'double'], |
| 59 | + ] |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('preserves all previous columns and adds the escaped column, when no grouping is given', () => { |
| 64 | + const previousFields = createPreviousFields([ |
| 65 | + ['field1', 'keyword'], |
| 66 | + ['field2', 'double'], |
| 67 | + ]); |
| 68 | + |
| 69 | + expectColumnsAfter( |
| 70 | + 'INLINESTATS AVG(field2)', |
| 71 | + previousFields, |
| 72 | + [['AVG(field2)', 'double']], |
| 73 | + [ |
| 74 | + ['field1', 'keyword'], |
| 75 | + ['field2', 'double'], |
| 76 | + ['AVG(field2)', 'double'], |
| 77 | + ] |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('preserves all previous columns and adds the escaped column, with grouping', () => { |
| 82 | + const previousFields = createPreviousFields([ |
| 83 | + ['field1', 'keyword'], |
| 84 | + ['field2', 'double'], |
| 85 | + ]); |
| 86 | + |
| 87 | + // Note: Unlike STATS, INLINESTATS doesn't care about BY clause for column preservation |
| 88 | + expectColumnsAfter( |
| 89 | + 'INLINESTATS AVG(field2) BY field1', |
| 90 | + previousFields, |
| 91 | + [['AVG(field2)', 'double']], |
| 92 | + [ |
| 93 | + ['field1', 'keyword'], |
| 94 | + ['field2', 'double'], |
| 95 | + ['AVG(field2)', 'double'], |
| 96 | + ] |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('preserves all previous columns and adds user defined and grouping columns', () => { |
| 101 | + const previousFields = createPreviousFields([ |
| 102 | + ['field1', 'keyword'], |
| 103 | + ['field2', 'double'], |
| 104 | + ['@timestamp', 'date'], |
| 105 | + ]); |
| 106 | + |
| 107 | + expectColumnsAfter( |
| 108 | + 'INLINESTATS AVG(field2) BY buckets=BUCKET(@timestamp,50,?_tstart,?_tend)', |
| 109 | + previousFields, |
| 110 | + [ |
| 111 | + ['AVG(field2)', 'double'], |
| 112 | + ['buckets', 'unknown'], |
| 113 | + ], |
| 114 | + [ |
| 115 | + ['field1', 'keyword'], |
| 116 | + ['field2', 'double'], |
| 117 | + ['@timestamp', 'date'], |
| 118 | + ['AVG(field2)', 'double'], |
| 119 | + ['buckets', 'unknown'], |
| 120 | + ] |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles duplicate column names by keeping the original column type', () => { |
| 125 | + const previousFields = createPreviousFields([ |
| 126 | + ['field1', 'keyword'], |
| 127 | + ['field2', 'double'], |
| 128 | + ['avg_field', 'integer'], // This will be preserved since it comes first |
| 129 | + ]); |
| 130 | + |
| 131 | + expectColumnsAfter( |
| 132 | + 'INLINESTATS avg_field=AVG(field2)', |
| 133 | + previousFields, |
| 134 | + [['avg_field', 'double']], // This will be ignored due to duplicate name |
| 135 | + [ |
| 136 | + ['field1', 'keyword'], |
| 137 | + ['field2', 'double'], |
| 138 | + ['avg_field', 'integer'], |
| 139 | + ] |
| 140 | + ); |
| 141 | + }); |
| 142 | +}); |
0 commit comments