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

[Expressions] [Lens] Add id and copyMetaFrom arg to mapColumn fn + add configurable onError argument to math fn #90481

Merged
merged 23 commits into from
Feb 25, 2021

Conversation

dej611
Copy link
Contributor

@dej611 dej611 commented Feb 5, 2021

Summary

Fixes #90423

This PR moves the mapColumn and math expressions to the shared expressions plugin in order to enable Lens to use them for the "Formula" features (see issue for more details).

  • Move mapColumn
    • Add new id argument (feature + help + tests)
    • Add new copyMetaFrom argument (feature + help + tests)
  • Move math
    • Add new onError argument (feature + help + tests)
    • Migrate to new returning types (feature + help + tests)
  • cleanup

Checklist

Delete any items that are not applicable to this PR.

@dej611 dej611 added v8.0.0 Feature:Lens Feature:ExpressionLanguage Interpreter expression language (aka canvas pipeline) labels Feb 5, 2021
@dej611
Copy link
Contributor Author

dej611 commented Feb 9, 2021

@elasticmachine merge upstream

@dej611
Copy link
Contributor Author

dej611 commented Feb 11, 2021

@elasticmachine merge upstream

@dej611
Copy link
Contributor Author

dej611 commented Feb 15, 2021

@elasticmachine merge upstream

@dej611 dej611 added v7.13.0 Team:Visualizations Visualization editors, elastic-charts and infrastructure release_note:enhancement labels Feb 16, 2021
@dej611 dej611 marked this pull request as ready for review February 16, 2021 13:29
@dej611 dej611 requested review from a team as code owners February 16, 2021 13:29
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-app (Team:KibanaApp)

|===

*Returns:* `number`
*Returns:* `number` | `boolean` | `null`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to extends the return type to cover the onError returning values

Copy link
Member

@ppisljar ppisljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code LGTM

@dej611 dej611 changed the title [Expressions] [Lens] Move mapColumn and Math from canvas to expressions plugin [Expressions] [Lens] Add id and copyMetaFrom to mapColumn fn + add configurable onError argument to math fn Feb 17, 2021
@dej611 dej611 changed the title [Expressions] [Lens] Add id and copyMetaFrom to mapColumn fn + add configurable onError argument to math fn [Expressions] [Lens] Add id and copyMetaFrom arg to mapColumn fn + add configurable onError argument to math fn Feb 17, 2021
@dej611
Copy link
Contributor Author

dej611 commented Feb 17, 2021

Renamed PR for changelog

types: ['string'],
help: i18n.translate('expressions.functions.math.args.onErrorHelpText', {
defaultMessage:
"In case the {TINYMATH} evaluation fails or returns NaN, the return value is specified by onError. When `'throw'`, it will throw an exception, terminating expression execution (default).",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should specify valid values instead of just being of type string (to allow auto-complete)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that as first option, but could not find a way to map a string union to a specific type for expressions. All the examples I check in the documentation have the string type assigned and documenting the valid values on the documentation side.

}
return result;
} catch (e) {
if (isDatatable(input) && input.rows.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The onError logic has to happen in the catch handler as well to catch things like divide by zero

@dej611
Copy link
Contributor Author

dej611 commented Feb 18, 2021

@elasticmachine merge upstream

@dej611
Copy link
Contributor Author

dej611 commented Feb 19, 2021

@elasticmachine merge upstream

@dej611 dej611 requested a review from flash1293 February 19, 2021 13:18
@flash1293
Copy link
Contributor

@elasticmachine merge upstream

Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks almost good to me, just found one bug


return Promise.all(rowPromises).then((rows) => {
const existingColumnIndex = columns.findIndex(({ name }) => name === args.name);
const type = rows.length ? getType(rows[0][args.name]) : 'null';
Copy link
Contributor

@flash1293 flash1293 Feb 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If id and name are both used, this lookup won't work (same issue with the row above). It should use columnId as well (probably worth a functional test)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the issue. I did misinterpret it initially. There's a test already to cover this specific case.

Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit, otherwise looks good

types: ['string', 'null'],
help: i18n.translate('expressions.functions.mapColumn.args.copyMetaFromHelpText', {
defaultMessage:
"If set, the meta object from the specified column id is copied over to the specified target column. Throws an exception if the column doesn't exist",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says the meta object is copied by specified column id, but it is actually using the name. I think we should use the id in line 107 as it's more stable)

It also says "Throws an exception if the column doesn't exist", but it will actually just silently not copy over meta. I think this behavior is OK, but we should adjust documentation.

Copy link
Contributor

@wylieconlon wylieconlon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, except I agree with Joe's comment that metadata should be copied by ID instead of by name.

aliases: ['exp', 'fn', 'function'],
help: i18n.translate('expressions.functions.mapColumn.args.expressionHelpText', {
defaultMessage:
'A {CANVAS} expression that is passed to each row as a single row {DATATABLE}.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that you didn't write this text, but maybe if you're making changes you can correct it. Two things to correct:

  1. The row is passed into the expression, but the text has it backwards
  2. Canvas expression vs expression
Suggested change
'A {CANVAS} expression that is passed to each row as a single row {DATATABLE}.',
'An expression that is executed on every row, provided with a single-row {DATATABLE} context and returning the cell value.',

@dej611
Copy link
Contributor Author

dej611 commented Feb 23, 2021

@elasticmachine merge upstream

Copy link
Contributor

@flash1293 flash1293 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left two nits about extending the unit tests a little

{
id: 'myId',
name: 'myName',
meta: { type: 'date' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we extends this test a little with more meta information?

expect(result.columns).toHaveLength(1);
expect(result.columns[0]).toHaveProperty('name', 'name');
expect(result.columns[0]).toHaveProperty('id', 'name');
expect(result.columns[0].meta).toHaveProperty('type', 'null');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test here that still infers the type correctly from the first row if the meta information transfer fails?

@dej611
Copy link
Contributor Author

dej611 commented Feb 24, 2021

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
canvas 1149 1145 -4
expressions 105 149 +44
total +40

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
canvas 1.4MB 1.4MB +38.4KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
canvas 592.1KB 548.7KB -43.4KB
expressions 165.8KB 210.8KB +45.0KB
total +1.5KB
Unknown metric groups

async chunk count

id before after diff
canvas 6 7 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Contributor

@cqliu1 cqliu1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Thanks for updating the function reference docs

@dej611 dej611 merged commit 0194739 into elastic:master Feb 25, 2021
@dej611 dej611 deleted the expression/mapColumn-ext branch February 25, 2021 08:43
dej611 added a commit to dej611/kibana that referenced this pull request Feb 25, 2021
…d configurable onError argument to math fn (elastic#90481)

Co-authored-by: Kibana Machine <[email protected]>
dej611 added a commit that referenced this pull request Feb 25, 2021
…d configurable onError argument to math fn (#90481) (#92810)

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
gmmorris added a commit to gmmorris/kibana that referenced this pull request Feb 25, 2021
* master: (38 commits)
  Fixes Cypress flake by adding pipe, click, and should (elastic#92762)
  [Discover] Fix filtering selected sidebar fields (elastic#91828)
  [ML] Fixes positions of calendar arrow buttons in start datafeed modal (elastic#92625)
  [dev/build_ts_refs] check that commit in outDirs matches mergeBase (elastic#92513)
  add dep on `@kbn/config` so it is built first
  [Expressions] [Lens] Add id and copyMetaFrom arg to mapColumn fn + add configurable onError argument to math fn (elastic#90481)
  [Lens] Fix Workspace hidden when using Safari (elastic#92616)
  [Lens] Fixes vertical alignment validation messages (elastic#91878)
  forbid x-elastic-product-origin header in elasticsearch configuration (elastic#92359)
  [Security Solution][Detections] Set default indicator path to reduce friction with new filebeat modules (elastic#92081)
  [ILM][Accessibility] Added A11y test for ILM new policy form. (elastic#92570)
  [Security Solution][Exceptions] - Fixes exceptions builder UI where invalid values can cause overwrites of other values (elastic#90634)
  Automatically generated Api documentation (elastic#86232)
  Increase index pattern select limit to 1000 (elastic#92093)
  [core.logging] Add RewriteAppender for filtering LogMeta. (elastic#91492)
  [Security Solution][Detection Rules] Update prebuilt rule threats to match schema (elastic#92281)
  [Security Solutions][Detection Engine] Fixes bug with not being able to duplicate indicator matches (elastic#92565)
  [Dashboard] Export appropriate references from byValue panels (elastic#91567)
  [Upgrade Assistant] Align code between branches (elastic#91862)
  [Security Solution][Case] Fix alerts push (elastic#91638)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:ExpressionLanguage Interpreter expression language (aka canvas pipeline) Feature:Lens release_note:enhancement Team:Visualizations Visualization editors, elastic-charts and infrastructure v7.13.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Math on expression improvements
7 participants