-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lens] Create mathColumn function to improve performance #101908
Changes from 3 commits
08121d9
517be0e
22f61e9
4b65c74
9d1b40d
c3fe29d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from '../types'; | ||
import { math, MathArguments } from './math'; | ||
import { Datatable, DatatableColumn, getType } from '../../expression_types'; | ||
|
||
export type MathColumnArguments = MathArguments & { | ||
id: string; | ||
name?: string; | ||
copyMetaFrom?: string | null; | ||
}; | ||
|
||
export const mathColumn: ExpressionFunctionDefinition< | ||
'mathColumn', | ||
Datatable, | ||
MathColumnArguments, | ||
Datatable | ||
> = { | ||
name: 'mathColumn', | ||
type: 'datatable', | ||
inputTypes: ['datatable'], | ||
help: i18n.translate('expressions.functions.mathColumnHelpText', { | ||
defaultMessage: | ||
'Adds a column calculated as the result of other columns. ' + | ||
'Changes are made only when you provide arguments.' + | ||
'See also {alterColumnFn} and {staticColumnFn}.', | ||
values: { | ||
alterColumnFn: '`alterColumn`', | ||
staticColumnFn: '`staticColumn`', | ||
}, | ||
}), | ||
args: { | ||
...math.args, | ||
id: { | ||
types: ['string'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.idHelpText', { | ||
defaultMessage: 'id of the resulting column. Must be unique.', | ||
}), | ||
required: true, | ||
}, | ||
name: { | ||
types: ['string'], | ||
aliases: ['_', 'column'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.nameHelpText', { | ||
defaultMessage: 'The name of the resulting column. Names are not required to be unique.', | ||
}), | ||
required: true, | ||
}, | ||
copyMetaFrom: { | ||
types: ['string', 'null'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.copyMetaFromHelpText', { | ||
defaultMessage: | ||
"If set, the meta object from the specified column id is copied over to the specified target column. If the column doesn't exist it silently fails.", | ||
}), | ||
required: false, | ||
default: null, | ||
}, | ||
}, | ||
fn: (input, args, context) => { | ||
const columns = [...input.columns]; | ||
const existingColumnIndex = columns.findIndex(({ id }) => { | ||
return id === args.id; | ||
}); | ||
if (existingColumnIndex > -1) { | ||
throw new Error('ID must be unique'); | ||
} | ||
|
||
const newRows = input.rows.map((row) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have this processed in async chunks, in order to give the thread some time to run some small tasks here and there if very big tables are passed. |
||
return { | ||
...row, | ||
[args.id]: math.fn( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still calling I propose we cache the ast by not calling Can be done in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the memoization to tinymath in this PR as it definitely improves the overall speed. |
||
{ | ||
type: 'datatable', | ||
columns: input.columns, | ||
rows: [row], | ||
}, | ||
{ | ||
expression: args.expression, | ||
onError: args.onError, | ||
}, | ||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This object could be declared on top and reused over and over. Just saving some memory. I've made also an experiment reusing the same table "template" above, but in terms of performance results were negligible for a medium size table, so not worth the hack. |
||
context | ||
), | ||
}; | ||
}); | ||
const type = newRows.length ? getType(newRows[0][args.id]) : 'null'; | ||
const newColumn: DatatableColumn = { | ||
id: args.id, | ||
name: args.name ?? args.id, | ||
meta: { type, params: { id: type } }, | ||
}; | ||
if (args.copyMetaFrom) { | ||
const metaSourceFrom = columns.find(({ id }) => id === args.copyMetaFrom); | ||
newColumn.meta = { ...newColumn.meta, ...(metaSourceFrom?.meta || {}) }; | ||
} | ||
|
||
columns.push(newColumn); | ||
|
||
return { | ||
type: 'datatable', | ||
columns, | ||
rows: newRows, | ||
} as Datatable; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 { mathColumn } from '../math_column'; | ||
import { functionWrapper, testTable } from './utils'; | ||
|
||
describe('mathColumn', () => { | ||
const fn = functionWrapper(mathColumn); | ||
|
||
it('throws if the id is used', () => { | ||
expect(() => fn(testTable, { id: 'price', name: 'price', expression: 'price * 2' })).toThrow( | ||
`ID must be unique` | ||
); | ||
}); | ||
|
||
it('applies math to each row by id', () => { | ||
const result = fn(testTable, { id: 'output', name: 'output', expression: 'quantity * price' }); | ||
expect(result.columns).toEqual([ | ||
...testTable.columns, | ||
{ id: 'output', name: 'output', meta: { params: { id: 'number' }, type: 'number' } }, | ||
]); | ||
expect(result.rows[0]).toEqual({ | ||
in_stock: true, | ||
name: 'product1', | ||
output: 60500, | ||
price: 605, | ||
quantity: 100, | ||
time: 1517842800950, | ||
}); | ||
}); | ||
|
||
it('handles onError', () => { | ||
const args = { | ||
id: 'output', | ||
name: 'output', | ||
expression: 'quantity / 0', | ||
}; | ||
expect(() => fn(testTable, args)).toThrowError(`Cannot divide by 0`); | ||
expect(() => fn(testTable, { ...args, onError: 'throw' })).toThrow(); | ||
expect(fn(testTable, { ...args, onError: 'zero' }).rows[0].output).toEqual(0); | ||
expect(fn(testTable, { ...args, onError: 'false' }).rows[0].output).toEqual(false); | ||
expect(fn(testTable, { ...args, onError: 'null' }).rows[0].output).toEqual(null); | ||
}); | ||
|
||
it('should copy over the meta information from the specified column', async () => { | ||
const result = await fn( | ||
{ | ||
...testTable, | ||
columns: [ | ||
...testTable.columns, | ||
{ | ||
id: 'myId', | ||
name: 'myName', | ||
meta: { type: 'date', params: { id: 'number', params: { digits: 2 } } }, | ||
}, | ||
], | ||
rows: testTable.rows.map((row) => ({ ...row, myId: Date.now() })), | ||
}, | ||
{ id: 'output', name: 'name', copyMetaFrom: 'myId', expression: 'price + 2' } | ||
); | ||
|
||
expect(result.type).toBe('datatable'); | ||
expect(result.columns[result.columns.length - 1]).toEqual({ | ||
id: 'output', | ||
name: 'name', | ||
meta: { type: 'date', params: { id: 'number', params: { digits: 2 } } }, | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't test (I can do tomorrow), but is this actually memoizing? Looking at the memoize-one source code, it seems like
memoizeOne
itself is not memoized on the passed-in function so it would create a new memoization closure on each call without actually ever hitting the cache.Looks like the
memoizeOne
call should be moved outside of theparse
functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're totally right, the memoizeOne function returns a instance each time!