flux icon indicating copy to clipboard operation
flux copied to clipboard

Add ability to update objects as part of `map` and other functions

Open nathanielc opened this issue 6 years ago • 6 comments

Currently the map function can either merge the result with the group key or it can explicitly define all new columns. There is no way to generically say given an object update just these columns.

Other langauges for example OCaml have awith keyword like this:

|> map(fn:(r) => {r with _value: r._value+2}

That function updates r with the _value value column, all other columns of r are preserved whether they are part of the group key or not. Adding a similar syntax to Flux would remove the need to have the mergeKey boolean on map.

Here is OCaml's description of the with record expression:

The expression { expr with field1 [= expr1] ; … ; fieldn [= exprn] } builds a fresh record with fields field1 … fieldn equal to expr1 … exprn, and all other fields having the same value as in the record expr. In other terms, it returns a shallow copy of the record expr, except for the fields field1 … fieldn, which are initialized to expr1 … exprn.

http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#htoc69

nathanielc avatar Oct 31 '18 21:10 nathanielc

JavaScript uses the spread operator to do something similar:

rows.map(r => {...r, _value: r._value + 2})

chnn avatar Oct 31 '18 21:10 chnn

What if you want to update multiple columns at the same time or insert a new one and update one? Possible to do?

|> map(fn:(r) => {r with {_value: r._value+2, foo:"bar"}}

pauldix avatar Nov 02 '18 23:11 pauldix

You can comma separate as many as you need.

|> map(fn:(r) => {r with _value: r._value+2, foo:"bar"}

nathanielc avatar Nov 29 '18 18:11 nathanielc

this is now supported for row functions #1303 , but type checking is not working generally within the language.

aanthony1243 avatar Jun 17 '19 15:06 aanthony1243

For the type bits see #1378

nathanielc avatar Jun 17 '19 15:06 nathanielc

Seems like this can be closed in favor of https://github.com/influxdata/flux/issues/1378 ?

russorat avatar Mar 04 '20 06:03 russorat