55package iox
66
77
8+ import "regexp"
9+ import "strings"
10+
811// from reads from the selected bucket and measurement in an IOx storage node.
912//
1013// This function creates a source that reads data from IOx. Output data is
@@ -15,6 +18,17 @@ package iox
1518// - bucket: IOx bucket to read data from.
1619// - measurement: Measurement to read data from.
1720//
21+ // ## Examples
22+ //
23+ // ### Use Flux to query data from IOx
24+ // ```no_run
25+ // import "experimental/iox"
26+ //
27+ // iox.from(bucket: "example-bucket", measurement: "example-measurement")
28+ // |> range(start: -1d)
29+ // |> filter(fn: (r) => r._field == "example-field")
30+ // ```
31+ //
1832// ## Metadata
1933// tags: inputs
2034builtin from : (bucket: string, measurement: string) => stream[{A with _time: time}] where A: Record
@@ -25,9 +39,79 @@ builtin from : (bucket: string, measurement: string) => stream[{A with _time: ti
2539//
2640// ## Parameters
2741// - bucket: IOx bucket to read data from.
28- // - query: Query to execute.
42+ // - query: SQL query to execute.
43+ //
44+ // ## Examples
45+ //
46+ // ### Use SQL to query data from IOx
47+ // ```no_run
48+ // import "experimental/iox"
49+ //
50+ // iox.sql(bucket: "example-bucket", query: "SELECT * FROM measurement")
51+ // ```
2952//
3053// ## Metadata
3154// introduced: 0.186.0
3255// tags: inputs
3356builtin sql : (bucket: string, query: string) => stream[A] where A: Record
57+
58+ // sqlInterval converts a duration value to a SQL interval string.
59+ //
60+ // SQL interval strings support down to millisecond precision.
61+ // Any microsecond or nanosecond duration units are dropped from the duration value.
62+ // If the duration only consists of microseconds or nanosecond units,
63+ // `iox.sqlInterval()` returns `1 millisecond`.
64+ // Duration values must be positive to work as a SQL interval string.
65+ //
66+ // ## Parameters
67+ // - d: Duration value to convert to SQL interval string.
68+ //
69+ // ## Examples
70+ //
71+ // ### Convert a duration to a SQL interval
72+ // ```no_run
73+ // import "experimental/iox"
74+ //
75+ // iox.sqlInterval(d: 1y2mo3w4d5h6m7s8ms)
76+ // // Returns 1 years 2 months 3 weeks 4 days 5 hours 6 minutes 7 seconds 8 milliseconds
77+ // ```
78+ //
79+ // ### Use a Flux duration to define a SQL interval
80+ // ```no_run
81+ // import "experimental/iox"
82+ //
83+ // windowInterval = 1d12h
84+ // sqlQuery = "
85+ // SELECT
86+ // DATE_BIN(INTERVAL '${iox.sqlInterval(d: windowInterval)}', time, TIMESTAMP '2023-01-01T00:00:00Z')
87+ // COUNT(field1)
88+ // FROM
89+ // measurement
90+ // GROUP BY
91+ // time
92+ // "
93+ //
94+ // iox.sql(bucket: "example-bucket", query: sqlQuery)
95+ // ```
96+ //
97+ // ## Metadata
98+ // introduced: NEXT
99+ // tags: sql, type-conversions
100+ sqlInterval = (d) => {
101+ _durationString = string(v: d)
102+ _pipeRegex = (v=<-, r, t) => regexp.replaceAllString(v: v, r: r, t: t)
103+ _intervalString =
104+ _pipeRegex(v: _durationString, r: /[\d]+(us|ns)/, t: "")
105+ |> _pipeRegex(r: /([^\d]+)/, t: " $1 ")
106+ |> _pipeRegex(r: / ms /, t: " milliseconds ")
107+ |> _pipeRegex(r: / s /, t: " seconds ")
108+ |> _pipeRegex(r: / m /, t: " minutes ")
109+ |> _pipeRegex(r: / h /, t: " hours ")
110+ |> _pipeRegex(r: / d /, t: " days ")
111+ |> _pipeRegex(r: / w /, t: " weeks ")
112+ |> _pipeRegex(r: / mo /, t: " months ")
113+ |> _pipeRegex(r: / y /, t: " years ")
114+ _output = if _intervalString == "" then "1 millisecond" else _intervalString
115+
116+ return strings.trimSpace(v: _output)
117+ }
0 commit comments