Skip to content

Commit

Permalink
fix(gen/tables): passing seed to tables generator (#4866)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
skartikey authored Jun 13, 2022
1 parent 9e77d56 commit 2ceafc4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libflux/go/libflux/buildinfo.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions stdlib/internal/gen/gen.flux
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
8 changes: 8 additions & 0 deletions stdlib/internal/gen/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions stdlib/internal/gen/tables_test.flux
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2ceafc4

Please sign in to comment.