redash icon indicating copy to clipboard operation
redash copied to clipboard

Optional Parameters Support

Open arikfr opened this issue 5 years ago • 42 comments

Today to make a parameter optional, the user needs to have a workaround in the query logic itself. For example:

select count(0)
from queries
where 'All' = '{{org_id}}' or org_id::varchar = '{{org_id}}'

In this case when the value of org_id is All, then we don't filter by it. It will be great if the user could mark a whole section as optional if no parameter is provided. The templating language we use (Mustache) actually supports this:

select count(0)
from queries
{{#org_id}}
where 'All' = '{{org_id}}' or org_id::varchar = '{{org_id}}'
{{/org_id}}

If no value for {{org_id}} is provided, then the section between {{#org_id}} and {{/org_id}} won't be rendered. So in theory, we can just allow for passing empty values for parameters and it works.

This has two issues though:

  1. Any false-y value will make this section not render. Including 0 (zero), which might be acceptable in some cases.
  2. This might make the implementation of #2904 more complicated.

We should probably take this into account when implementing #2904, and revisit once it's indeed implemented.

arikfr avatar Oct 28 '18 12:10 arikfr

we are evaluating Redash but got stuck on date range filters. This has been an issue for two years? That's very discouraging.

https://discuss.redash.io/t/add-ui-for-query-params-at-dashboard-level/236

Maxhodges avatar Nov 10 '18 07:11 Maxhodges

@Maxhodges the discussion you linked to started 2 years ago but over time changed topic.

Date Range parameters are supported.

arikfr avatar Nov 12 '18 08:11 arikfr

Maybe we can preprocess a query with Jinja?

pavelpatrin avatar Apr 02 '19 13:04 pavelpatrin

Metabase solved this using bracket syntax: [[AND created >= {{start_date}} ]].

If the variable referenced within the double brackets (start_date) exists then the contents of the brackets is parsed into the query, otherwise the line is ignored.

This is quite elegant in my opinion and it doesn't invite users to abuse default values.

smick avatar Apr 29 '19 23:04 smick

This mustache template workaround did not work for me. Am I doing this wrong?

image

joekjoshua avatar May 09 '19 12:05 joekjoshua

@joekjoshua we still don't allow empty values for parameters...

@pavelpatrin we're thinking about using Jinja instead of Mustache here. Just not sure how safe it is (even with the sandbox) and it introduces some issues with frontend compatibility, as it can be only processed on the backend. But it's definitely an option.

arikfr avatar May 13 '19 14:05 arikfr

Hi,@arikfr ,how was it going about optional parameters?

netmole avatar Aug 23 '19 13:08 netmole

I've used this in the past for SQL-safe Jinja queries - maybe it could be an option here? https://github.com/hashedin/jinjasql

evan-burke avatar Nov 20 '19 02:11 evan-burke

Any news? This is pretty critical for us.

renekyewski avatar Dec 02 '19 11:12 renekyewski

@arikfr will the next release support optional parameters?

adikhel avatar Jan 21 '20 10:01 adikhel

Critical also for us.

duncanita avatar Jan 24 '20 16:01 duncanita

Critical for us too.

darleisantossoares avatar Jan 24 '20 21:01 darleisantossoares

Optional Parameters feature will be very welcome here too. Mustache could be also useable with a Boolean value, even without allowing empty values. How about adding a system parameter that will control the mustache template workaround?

Any timeline for supporting Jinja?

erels avatar Jan 27 '20 13:01 erels

What I did was to put the jinja syntax as comments in the sql editor so it wont bother the frontend , then parse it with jinjasql (for safety) , conditionally to os param (new param). if the os param is not set, it will be ignored because of the comments.

something like this:

Select name, — {% if {{ detail_flag }} and {{ address_flag }} %} — Lname, comments, address1, address2 , city — {% elif {{ detail_flag } and not {{ address_flag }} %} — , Lname , city — {% else %} — , city — {% endif %} From customer

What do you think about this? I'll submit the code if you want to try/check it

erels avatar Feb 18 '20 16:02 erels

Brach is here

erels avatar Feb 19 '20 13:02 erels

Critical for us too.

chenyuantao avatar Mar 01 '20 08:03 chenyuantao

Im using this Jinja support branch successfully. It supports the rendering of the query fully based on the input parameters. See example above.

erels avatar Mar 02 '20 05:03 erels

+1 for this, we need this as well.

floriank avatar May 15 '20 08:05 floriank

👍 this will be really helpful

Sonivaibhav26 avatar Aug 14 '20 10:08 Sonivaibhav26

I think redash can support optional parameter like {{paramName, defaultValue}} , when input empty string, then parameter paramName set the defaultValue.

dengc367 avatar Sep 22 '20 08:09 dengc367

Any ideas if this will be merged sometime?

nicolasgnr avatar Sep 30 '20 12:09 nicolasgnr

+1

idoshilon avatar Oct 28 '20 09:10 idoshilon

+1

shubhamgoyal41 avatar Nov 02 '20 04:11 shubhamgoyal41

+1

zkid18 avatar Nov 25 '20 02:11 zkid18

+1

persiyanov avatar Nov 25 '20 12:11 persiyanov

request for this too

Naokimi avatar Dec 02 '20 04:12 Naokimi

+1

SORC3r3r avatar Mar 01 '21 13:03 SORC3r3r

+1

wmorin avatar Mar 03 '21 17:03 wmorin

+1

clac1212 avatar Apr 01 '21 13:04 clac1212

+1

RichardSmaldone avatar Apr 02 '21 23:04 RichardSmaldone

+1

mat-mfb avatar Apr 15 '21 03:04 mat-mfb

+1

cfwme avatar Apr 22 '21 17:04 cfwme

+1 :)

antbofh avatar Apr 23 '21 13:04 antbofh

A bit different than the workaround in the OP (I could not make it to work). This worked for me:

SELECT *
FROM CLIENTS
WHERE (client_id = '{{ client_id }}' OR 'all' = '{{ client_id }}')
  AND (activated = TRUE OR something_else = TRUE);

Than manually add a description for the param so users know what to type in:

image

Result: image

hoto avatar Apr 26 '21 11:04 hoto

Critical++ also for us

samzong avatar Jul 05 '21 13:07 samzong

+1

marohds avatar Oct 20 '21 18:10 marohds

+1 Critical

kshishkin82 avatar Oct 25 '21 09:10 kshishkin82

Any updates on this? This feature would be awesome, it's kind of weird how this hasn't been implemented yet

instplanet avatar May 05 '22 03:05 instplanet

And updates on this? This is a bit weird that this is not supported. Thanks!

yuhao-xu avatar May 17 '22 13:05 yuhao-xu

Is this already supported? A major road block for me.

jypogoy avatar Aug 15 '22 06:08 jypogoy

I like, how it works in metabase

For example:

[[ and {{start_date]] >= “2018-01-01”]]

If start_date is empty or null, all condition removed from sql query

vldmr-k avatar Aug 21 '22 10:08 vldmr-k

The feature is already supported ? Thanks!

toplinuxsir avatar Aug 28 '22 12:08 toplinuxsir