How to fetch data from SQL and use it as filter condition

Asked in a Github issue #3424

I want fetch data from SQL and use it as filter condition… how to do it?

  1. Use sql.from() to query the SQL data source
  2. Do one of the following:
  3. Reference values in the extracted array or record in your filter function.

Here’s a Postgres example:

import "sql"

sqlValues = sql.from(
    driverName: "postgres",
    dataSourceName: "postgresql://username:password@localhost:5432",
    query: "SELECT labels FROM example_table WHERE this = 'something'",
    |> findColumn(fn: (key) => true, column: "example-col")

from(bucket: "example-bucket")
    |> range(start: -1d)
    |> filter(fn: (r) => r.someTag == sqlValues[0])
1 Like

Thanks for the example. In the final filter you select a specific element by doing sqlValues[0]
How can I filter for all the values in sqlValues. Ideally I would avoid contains() because of known performance issue

I’m looking for a way to do this as well without contains().