Skip to content
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] Error on array values in math #102371

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -69,25 +69,40 @@ export const mathColumn: ExpressionFunctionDefinition<
return id === args.id;
});
if (existingColumnIndex > -1) {
throw new Error('ID must be unique');
throw new Error(
i18n.translate('expressions.functions.mathColumn.uniqueIdError', {
defaultMessage: 'ID must be unique',
})
);
}

const newRows = input.rows.map((row) => {
return {
...row,
[args.id]: math.fn(
{
type: 'datatable',
columns: input.columns,
rows: [row],
},
{
expression: args.expression,
onError: args.onError,
},
context
),
};
const result = math.fn(
{
type: 'datatable',
columns: input.columns,
rows: [row],
},
{
expression: args.expression,
onError: args.onError,
},
context
);

if (Array.isArray(result)) {
if (result.length === 1) {
return { ...row, [args.id]: result[0] };
}
throw new Error(
i18n.translate('expressions.functions.mathColumn.arrayValueError', {
defaultMessage: 'Cannot perform math on array values at {name}',
values: { name: args.name },
})
);
}

return { ...row, [args.id]: result };
});
const type = newRows.length ? getType(newRows[0][args.id]) : 'null';
const newColumn: DatatableColumn = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ describe('mathColumn', () => {
});
});

it('extracts a single array value, but not a multi-value array', () => {
const arrayTable = {
...testTable,
rows: [
{
name: 'product1',
time: 1517842800950, // 05 Feb 2018 15:00:00 GMT
price: [605, 500],
quantity: [100],
in_stock: true,
},
],
};
const args = {
id: 'output',
name: 'output',
expression: 'quantity',
};
expect(fn(arrayTable, args).rows[0].output).toEqual(100);
expect(() => fn(arrayTable, { ...args, expression: 'price' })).toThrowError(
`Cannot perform math on array values`
);
});

it('handles onError', () => {
const args = {
id: 'output',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('getType()', () => {
});

test('throws if object has no .type property', () => {
expect(() => getType([])).toThrow();
expect(() => getType({})).toThrow();
expect(() => getType({ _type: 'foo' })).toThrow();
expect(() => getType({ tipe: 'foo' })).toThrow();
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/expressions/common/expression_types/get_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

export function getType(node: any) {
if (node == null) return 'null';
if (Array.isArray(node)) {
throw new Error('Unexpected array value encountered.');
}
if (typeof node === 'object') {
if (!node.type) throw new Error('Objects must have a type property');
return node.type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const mathOperation: OperationDefinition<MathIndexPatternColumn, 'managed
function: 'mathColumn',
arguments: {
id: [columnId],
name: [columnId],
name: [column.label],
expression: [astToString(column.params.tinymathAst)],
onError: ['null'],
},
Expand Down