-
Notifications
You must be signed in to change notification settings - Fork 187
Description
For reference, this started off as a query on the InfluxDB Community #python Slack channel: https://influxcommunity.slack.com/archives/CHQ5VG6F8/p1629450632004000 which received a response and workaround from Jakub Bednář (@bednar ): https://influxcommunity.slack.com/archives/CHQ5VG6F8/p1629456788006500
Proposal:
Add support for the Python List data type to be used as a query parameter
Current behavior:
Defining a Python List and then attempting to use it as a query parameter with query_api.query() and the contains()
function results in a influxdb_client.rest.ApiException: (400)
error.
Desired behavior:
To use standard Python data types, such as Lists, without the need to write code to convert Lists into an acceptable format first. At the very least, the limitation should be documented.
Alternatives considered:
Jakub Bednář (@bednar ) on Slack suggested I use this:
bars_ast = ArrayExpression("ArrayExpression",
elements=list(map(lambda it: StringLiteral("StringLiteral", str(it)), bars)))
params = {"bars": bars_ast}
Use case:
Allows users to implement their code in a Python-natural way.
Original example from Slack post for reference:
I'm trying to use the influxdb-client-python library to make (I think) a simple query, however I'm having issues using a Python list as a parameter. I feel like I'm overlooking something very simple. My non-working code:
bars = ["bar1", "bar2", "bar3"]
params = {"bars": bars}
tables = query_api.query('''
from(bucket: "foobars")
|> range(start: -6h, stop: now())
...
|> filter(fn: (r) => contains(value: r.barID, set: bars))
|> last()
''', params = params)
Executing that returns (trimmed):
influxdb_client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'Vary': 'Accept-Encoding', 'X-Platform-Error-Code': 'invalid', 'Date': 'Fri, 20 Aug 2021 09:00:03 GMT', 'Transfer-Encoding': 'chunked'})
HTTP response body: b'{"code":"invalid","message":"compilation failed: extern json parse error: unknown variant `bar1`, expected one of `Identifier`, `ArrayExpression`, ....<snip>
If I replace the filter line like below, it works fine:
|> filter(fn: (r) => contains(value: r.barID, set: ["bar1", "bar2", "bar3"]))