Parameterized Query Failing to Generate a Range

I am trying to execute a parameterized flux query using the Go client. I have isolated the error to the range function within the flux query.

Here is my parameters struct:

parameters := struct {
Start string json:"start"
}{
“-1h”,
}

Here’s the query:
Query := from(bucket: "my_bucket") |> range(start: duration(v: params.start)) |> filter(fn: (r) => r._measurement == "my_measurement" and r._field == "my_field")

I get the following error:

“panic: invalid: error in building plan while starting program: cannot query an empty range”

If I replace params.start with “-1h” the range function works as intended and the query succeeds. It seems as if the value for params.start is not making it into the flux query.

The Go client readme on GitHub shows a parameterized query example like this:
parameters := struct {
Start string json:"start"
Field string json:"field"
Value float64 json:"value"
}{
“-1h”,
“temperature”,
25,
}
// Query with parameters
query := from(bucket:"my-bucket") |> range(start: duration(params.start)) |> filter(fn: (r) => r._measurement == "stat") |> filter(fn: (r) => r._field == params.field) |> filter(fn: (r) => r._value > params.value)

When I try to call the duration function in this example (which is not inline with the duration documentation) I get the following error:

"panic: invalid: compilation failed: error @2:6-3:90: expected RPAREN, got EOF

error @2:19-2:41: expected comma in property list, got DOT

error @2:34-2:40: unexpected token for property key: DOT (.)"

Hello @rrmangum
You named your parameters not params so I think that might be part of it.

Also excse my lack of go knowledge which is essentially 0 but why not?

type Params struct {
    start string
}

// Usage
dur := "-1h"
params := Params{
    start: dur,
}

Can you share the full script though? The error seems to be a lack of parentheses.

Hi @Anaisdg,

In the Go client I pass in the struct when executing the query. I can choose any name I want there and it does not have to be params. I know this works because I am able to access the measurement and field parameters through params.measurement and params.field.

It’s just when I try to access the start parameter through params.start that the range function fails because it is not receiving the start value through the params.start reference.

I can hard code “-1h” into the the value for the duration type cast within the range function and the query succeeds. I can also remove the duration type cast and execute the range function as range(start: -1h) and it also succeeds.

The full script:

client := influxdb2.NewClient(url, token)
queryAPI := client.QueryAPI(org)

parameters := struct {
Start string json:"start"
Measurement string json:"measurement"
Field string json:"field"
}{
“-1h”,
“My_Measurement”,
“My_Field”
}

Query := from(bucket: "my_bucket") |> range(start: duration(v: params.start)) |> filter(fn: (r) => r._measurement == params.measurement and r._field == params.field)

lastTLMCmd, err := queryAPI.QueryWithParams(
context.Background(),
query,
parameters)

if err == nil {
influxData.LastTLMCmd = parseFluxQuery(lastTLMCmd)
} else {
panic(err)
}

The parseFluxQuery function is just some formatting I am doing after the flux query succeeds.