influxdata / flux

Flux is a lightweight scripting language for querying databases (like InfluxDB) and working with data. It's part of InfluxDB 1.7 and 2.0, but can be run independently of those.
https://influxdata.com
MIT License
769 stars 153 forks source link

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

Open nathanielc opened 5 years ago

nathanielc commented 5 years ago

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

chnn commented 5 years ago

JavaScript uses the spread operator to do something similar:

rows.map(r => {...r, _value: r._value + 2})
pauldix commented 5 years ago

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"}}
nathanielc commented 5 years ago

You can comma separate as many as you need.

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

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

nathanielc commented 5 years ago

For the type bits see #1378

russorat commented 4 years ago

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

github-actions[bot] commented 1 day ago

This issue has had no recent activity and will be closed soon.