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

FieldValues: Use plain arrays instead of Vector (part 1 of 2) #66187

Merged
merged 46 commits into from
Apr 14, 2023

Conversation

ryantxu
Copy link
Member

@ryantxu ryantxu commented Apr 7, 2023

What is this feature?

This adds basic basic functions to the existing Vector interface. This will allow Vector and normal arrays to share more of the the same key types.

This is technically a breaking change because we are requiring new functions on an interface -- however it would be an extremely esoteric use case to implement the Vector interface directly rather than use one of the existing implementaions.

Why do we need this feature?

Ideally the frame.fields[idx].values element would be a simple arrary... however we originally used apache arrow in the frontend, and it required an accessor function. It seemed ok at the time, but is really a mess and pretty confusing.

In 10, we would like to support using values as either a simple array or Vector. This PR helps unify the typing requirements so we do not need to check isArray(vector) all the time

Who is this feature for?

Developers

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

Release notice breaking change

Additional functions (map/filter/forEach/iterator) have been added to the root Vector interface. Any code using vectors will continue to work unchanged, but in the rare case that you have implemented Vector directly, it be missing these functions. The easiest fix is to extend FunctionalVector.

The ArrayVector class now extends the native JavaScript Array and gains all of its prototype/instance methods as a result.

@ryantxu ryantxu added add to changelog area/dataframe breaking change Relevant for changelog generation no-backport Skip backport of PR labels Apr 7, 2023
@ryantxu ryantxu added this to the 10.0.0 milestone Apr 7, 2023
@ryantxu ryantxu requested review from a team as code owners April 7, 2023 20:33
@ryantxu ryantxu requested review from tskarhed, ashharrison90 and academo and removed request for a team April 7, 2023 20:33
@ryantxu ryantxu requested a review from mckn April 7, 2023 20:45
@github-actions
Copy link
Contributor

github-actions bot commented Apr 7, 2023

⚠️   Possible breaking changes

(Open the links below in a new tab to go to the correct steps)

grafana-data has possible breaking changes (more info)

Console output
Read our guideline

@grafanabot grafanabot added the levitate breaking change A label indicating a breaking change and assigned by Levitate. label Apr 7, 2023
@ryantxu ryantxu requested a review from charandas April 7, 2023 21:05
@leeoniya leeoniya changed the title GrafanaData: Add basic functional capability to the root Vector interface FieldValues: Use plain arrays instead of Vector (part 1 of 3) Apr 7, 2023
gtk-grafana and others added 3 commits April 7, 2023 16:43
…and name matchers (#65237)

Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
@ryantxu ryantxu requested a review from a team as a code owner April 8, 2023 00:22
return this.buffer;
constructor(buffer?: T[]) {
super();
this.buffer = buffer ?? [];
Copy link
Contributor

@leeoniya leeoniya Apr 14, 2023

Choose a reason for hiding this comment

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

this now shallow-copies (via setter) the passed-in buffer rather than absorbing it by reference. we can't avoid this since ArrayVector now extends Array, . this new behavior required fixes in two places which had these buffer pre-allocation/mutation patterns:

  1. pre-allocate a buffer via let buf = new Array(10)
  2. wrap it with field.values = ArrayVector(buf)
  3. mutate the buffer expecting it to reflect in field.values via buf[i] = 100

see: https://github.com/grafana/grafana/pull/66187/files#diff-ad5e547fdd6a3a9ee328725eba17a5bc2b079e5b29102ae9f08a6b07e5024185R60


  1. Create a new mutable frame: let mutFrame = new MutableDataFrame();
  2. Create some field: let field = {name: 'a', values: new ArrayVector()}
  3. Add field: mutFrame.addField(field) (this shallow copies field internally, and wraps .values buffers in ArrayVector)
  4. Try to add values to the field, expecting the values to change in the mutable frame field's copy: field.values.add("a")

see: https://github.com/grafana/grafana/pull/66187/files#diff-5c952fb064796024a148c909807222ed0c7ba53efc3fe39cc05ae1edfe43a73aR72-R75

@leeoniya leeoniya changed the title FieldValues: Use plain arrays instead of Vector (part 1 of 3) FieldValues: Use plain arrays instead of Vector (part 1 of 2) Apr 14, 2023
@github-actions
Copy link
Contributor

Backend code coverage report for PR #66187
No changes

@github-actions
Copy link
Contributor

Frontend code coverage report for PR #66187

Plugin Main PR Difference
elasticsearch 78.09% 78.09% 0%
loki 83.8% 83.8% 0%

Comment on lines -62 to -64
expect(result.fields[0].values.toArray()).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(result.fields[1].values.toArray()).toStrictEqual([4, null, 6, null, null, null, null, null, null, 8]);
expect(result.fields[2].values.toArray()).toStrictEqual(['a', null, 'b', null, null, null, null, null, null, 'c']);
Copy link
Contributor

@leeoniya leeoniya Apr 14, 2023

Choose a reason for hiding this comment

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

NOTE: all the .toArray() removals in this PR are not essential to get the tests passing; they're just drive-by simplifications that can technically be pulled into Part 2: #66224

the key changes here are the .toStrictEqual() -> .toEqual()

Copy link
Contributor

@matyax matyax left a comment

Choose a reason for hiding this comment

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

Loki part looks good 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/dataframe area/frontend breaking change Relevant for changelog generation levitate breaking change A label indicating a breaking change and assigned by Levitate. no-backport Skip backport of PR type/docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants