Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Python Lists as a query parameter #318

Closed
MediumBrownToast opened this issue Aug 23, 2021 · 1 comment · Fixed by #319
Closed

Support for Python Lists as a query parameter #318

MediumBrownToast opened this issue Aug 23, 2021 · 1 comment · Fixed by #319
Labels
enhancement New feature or request
Milestone

Comments

@MediumBrownToast
Copy link

MediumBrownToast commented Aug 23, 2021

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"]))

@bednar
Copy link
Contributor

bednar commented Aug 23, 2021

Hi @MediumBrownToast,

I've prepared PR #319. If you would like to use dev version of client then install client via:

pip install git+https://github.com/influxdata/influxdb-client-python.git@feat/query-params-array

Regards

@bednar bednar added this to the 1.21.0 milestone Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants