From 2ceafc44fb7c95267f4629ca3c5278dee9998416 Mon Sep 17 00:00:00 2001 From: Sunil Kartikey Date: Mon, 13 Jun 2022 10:24:31 +0100 Subject: [PATCH] fix(gen/tables): passing seed to tables generator (#4866) * fix(gen/tables): passing seed to tables generator Adding seed to tables generator to get the very same sequence each time(helpful in writing test) at the moment the seed defaults to the current time, which generates a different sequence each time. useful in #4307 * fix: passing pointer for Seed so that it defaults to nil not zero --- libflux/go/libflux/buildinfo.gen.go | 3 ++- stdlib/internal/gen/gen.flux | 2 ++ stdlib/internal/gen/tables.go | 8 ++++++++ stdlib/internal/gen/tables_test.flux | 28 ++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 stdlib/internal/gen/tables_test.flux diff --git a/libflux/go/libflux/buildinfo.gen.go b/libflux/go/libflux/buildinfo.gen.go index 74f40a1572..ee6e5196a1 100644 --- a/libflux/go/libflux/buildinfo.gen.go +++ b/libflux/go/libflux/buildinfo.gen.go @@ -257,7 +257,8 @@ var sourceHashes = map[string]string{ "stdlib/internal/boolean/boolean.flux": "1c0a1564911dd5f4c35c17efb32158ab31c51b20ac0f25175545e50aadc87c94", "stdlib/internal/debug/debug.flux": "104485f6a8d6e1cd2fc8111367687fed4cef2a63b5a7657124571b89b0e2420c", "stdlib/internal/debug/debug_test.flux": "063349cd314ed79906a96d84ed3c6829c734d1607e0bbae0654bdc9e22210e3c", - "stdlib/internal/gen/gen.flux": "320c005cffe97f28324a21054089cf78a2e6c511c639c4c0d1df727e46072c05", + "stdlib/internal/gen/gen.flux": "680830a7337f5f6f72927e8c43248138e77d719ef09df4af06f2d00f7932d730", + "stdlib/internal/gen/tables_test.flux": "14a3d3542aa0815bf3fb912b045c23161322acff731bfa04ce380c5da9866654", "stdlib/internal/influxql/influxql.flux": "51739e7d9026cae356fdef79a4340d2bda7fc10d1daa1a7adeae06014f85049c", "stdlib/internal/location/location.flux": "e810d8264dd1590f9556259cfa76272c8e5698a0262509d0ca12bfd4c71342f5", "stdlib/internal/promql/join_test.flux": "87fde732a159a2cffb81dbc21eebf70df352b69e04781e6e4526f57336b30146", diff --git a/stdlib/internal/gen/gen.flux b/stdlib/internal/gen/gen.flux index d66116237c..bf45c44f0a 100644 --- a/stdlib/internal/gen/gen.flux +++ b/stdlib/internal/gen/gen.flux @@ -8,8 +8,10 @@ package gen // - n: Number of rows to generate. // - nulls: Percentage chance that a null value will be used in the input. Valid value range is `[0.0 - 1.0]`. // - tags: Set of tags with their cardinality to generate. +// - seed: Pass seed to tables generator to get the very same sequence each time builtin tables : ( n: int, ?nulls: float, ?tags: [{name: string, cardinality: int}], + ?seed: int, ) => stream[{A with _time: time, _value: float}] diff --git a/stdlib/internal/gen/tables.go b/stdlib/internal/gen/tables.go index b7a8d57437..5615f62e97 100644 --- a/stdlib/internal/gen/tables.go +++ b/stdlib/internal/gen/tables.go @@ -26,6 +26,7 @@ type TablesOpSpec struct { N int `json:"n"` Tags []Tag `json:"tags,omitempty"` Nulls float64 `json:"nulls,omitempty"` + Seed *int64 `json:"Seed,omitempty"` } func init() { @@ -91,6 +92,12 @@ func createTablesOpSpec(args flux.Arguments, a *flux.Administration) (flux.Opera spec.Nulls = nulls } + if seed, ok, err := args.GetInt("seed"); err != nil { + return nil, err + } else if ok { + spec.Seed = &seed + } + return spec, nil } @@ -116,6 +123,7 @@ func newTablesProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.Pro schema := gen.Schema{ NumPoints: spec.N, Nulls: spec.Nulls, + Seed: spec.Seed, } if len(spec.Tags) > 0 { diff --git a/stdlib/internal/gen/tables_test.flux b/stdlib/internal/gen/tables_test.flux new file mode 100644 index 0000000000..4aed20d7a9 --- /dev/null +++ b/stdlib/internal/gen/tables_test.flux @@ -0,0 +1,28 @@ +package gen + + +import "array" +import "testing" +import "internal/gen" +import "internal/debug" + +option now = () => 2030-01-01T00:00:00Z + +testcase gen_tables_seed { + got = + gen.tables(n: 5, seed: 123) + |> drop(columns: ["_time"]) + + want = + array.from( + rows: [ + {_value: -39.7289264835779}, + {_value: 3.561659508431711}, + {_value: 34.956531511845476}, + {_value: -53.72013420347799}, + {_value: 49.20701082669753}, + ], + ) + + testing.diff(got: got, want: want) +}