Skip to content

Commit

Permalink
feat(iox): add microsecond and nanosecond support to iox.sqlInterval (#…
Browse files Browse the repository at this point in the history
…5400)

* feat(iox): add microsecond and nanosecond support to iox.sqlInterval

* chore(docs): updated documentation for iox.sqlInterval
  • Loading branch information
sanderson authored Mar 27, 2023
1 parent 8596d0b commit d952a12
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions libflux/go/libflux/buildinfo.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ var sourceHashes = map[string]string{
"stdlib/experimental/http/requests/requests.flux": "579f5d578722a425f350db50dc2697a8bce7bcddc3737d094ae96126b6f96c56",
"stdlib/experimental/influxdb/influxdb.flux": "503f68779c9d8a4c307d371cce8071826d0462a00e486190b3dfa681f9b620dc",
"stdlib/experimental/integral_test.flux": "4c9b6c866884839027946e93793689b8b3d328e2e777f8ee2f8973ffd72be036",
"stdlib/experimental/iox/iox.flux": "3e7d8607afc32aa21472fc72682cfaa64cec5b2733d55112cdeef3635a171fa9",
"stdlib/experimental/iox/iox_test.flux": "04624caf7807128826564e18bafde5ad8033eaf0f8c6edfe0c9300c03c88aac0",
"stdlib/experimental/iox/iox.flux": "43e7b3246ca9ff2f0fd63176faa7bab6f35b0b38a25601c91347039d428abb85",
"stdlib/experimental/iox/iox_test.flux": "976472a9da9d30069451ee992c188ad424cf94bf976fdefafb9a92eab562ddba",
"stdlib/experimental/join_right_side_more_cols_test.flux": "788bd7fe3dc6d324453484bdee64f1e408c291a43ffa3744e930c7ae5e533db7",
"stdlib/experimental/join_test.flux": "a48f78765b34d8011e45e0333cdc10ed578076f9a34443e50c6f7633978ea549",
"stdlib/experimental/json/json.flux": "50479ced0a3ae185164cbecd5f071f849ea30b727385afec8d525ff98c7460be",
Expand Down
16 changes: 6 additions & 10 deletions stdlib/experimental/iox/iox.flux
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ builtin sql : (bucket: string, query: string) => stream[A] where A: Record

// sqlInterval converts a duration value to a SQL interval string.
//
// SQL interval strings support down to millisecond precision.
// Any microsecond or nanosecond duration units are dropped from the duration value.
// If the duration only consists of microseconds or nanosecond units,
// `iox.sqlInterval()` returns `1 millisecond`.
// Duration values must be positive to work as a SQL interval string.
//
// ## Parameters
Expand All @@ -83,12 +79,12 @@ builtin sql : (bucket: string, query: string) => stream[A] where A: Record
// windowInterval = 1d12h
// sqlQuery = "
// SELECT
// DATE_BIN(INTERVAL '${iox.sqlInterval(d: windowInterval)}', time, TIMESTAMP '2023-01-01T00:00:00Z')
// DATE_BIN(INTERVAL '${iox.sqlInterval(d: windowInterval)}', time, TIMESTAMP '2023-01-01T00:00:00Z') AS time_bin,
// COUNT(field1)
// FROM
// measurement
// GROUP BY
// time
// time_bin
// "
//
// iox.sql(bucket: "example-bucket", query: sqlQuery)
Expand All @@ -101,8 +97,9 @@ sqlInterval = (d) => {
_durationString = string(v: d)
_pipeRegex = (v=<-, r, t) => regexp.replaceAllString(v: v, r: r, t: t)
_intervalString =
_pipeRegex(v: _durationString, r: /[\d]+(us|ns)/, t: "")
|> _pipeRegex(r: /([^\d]+)/, t: " $1 ")
_pipeRegex(v: _durationString, r: /([^\d]+)/, t: " $1 ")
|> _pipeRegex(r: / ns /, t: " nanoseconds ")
|> _pipeRegex(r: / us /, t: " microseconds ")
|> _pipeRegex(r: / ms /, t: " milliseconds ")
|> _pipeRegex(r: / s /, t: " seconds ")
|> _pipeRegex(r: / m /, t: " minutes ")
Expand All @@ -111,7 +108,6 @@ sqlInterval = (d) => {
|> _pipeRegex(r: / w /, t: " weeks ")
|> _pipeRegex(r: / mo /, t: " months ")
|> _pipeRegex(r: / y /, t: " years ")
_output = if _intervalString == "" then "1 millisecond" else _intervalString

return strings.trimSpace(v: _output)
return strings.trimSpace(v: _intervalString)
}
2 changes: 1 addition & 1 deletion stdlib/experimental/iox/iox_test.flux
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ testcase iox_sql_interval_testcase {
{_value: "12 years 3 months 4 weeks"},
{_value: "1 weeks 6 days 10 hours 5 minutes"},
{_value: "2 minutes 3 seconds 56 milliseconds"},
{_value: "1 millisecond"},
{_value: "123 microseconds 56 nanoseconds"},
],
)

Expand Down

0 comments on commit d952a12

Please sign in to comment.