getredash / redash

Make Your Company Data Driven. Connect to any data source, easily visualize, dashboard and share your data.
http://redash.io/
BSD 2-Clause "Simplified" License
26.14k stars 4.36k forks source link

Optional Parameters Support #3011

Open arikfr opened 5 years ago

arikfr commented 5 years ago

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.

Maxhodges commented 5 years ago

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

arikfr commented 5 years ago

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

Date Range parameters are supported.

pavelpatrin commented 5 years ago

Maybe we can preprocess a query with Jinja?

smick commented 5 years ago

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.

joekjoshua commented 5 years ago

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

image

arikfr commented 5 years ago

@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.

netmole commented 5 years ago

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

evan-burke commented 4 years ago

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

renekyewski commented 4 years ago

Any news? This is pretty critical for us.

adikhel commented 4 years ago

@arikfr will the next release support optional parameters?

duncanita commented 4 years ago

Critical also for us.

darleisantossoares commented 4 years ago

Critical for us too.

erels commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Brach is here

chenyuantao commented 4 years ago

Critical for us too.

erels commented 4 years ago

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

floriank commented 4 years ago

+1 for this, we need this as well.

Sonivaibhav26 commented 4 years ago

👍 this will be really helpful

dengc367 commented 4 years ago

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

nicolasgnr commented 4 years ago

Any ideas if this will be merged sometime?

idoshilon commented 3 years ago

+1

shubhamgoyal41 commented 3 years ago

+1

zkid18 commented 3 years ago

+1

persiyanov commented 3 years ago

+1

Naokimi commented 3 years ago

request for this too

SORC3r3r commented 3 years ago

+1

wmorin commented 3 years ago

+1

clac1212 commented 3 years ago

+1

RichardSmaldone commented 3 years ago

+1

mat-mfb commented 3 years ago

+1

cfwme commented 3 years ago

+1

antbofh commented 3 years ago

+1 :)

hoto commented 3 years ago

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

samzong commented 3 years ago

Critical++ also for us

marohds commented 2 years ago

+1

kshishkin82 commented 2 years ago

+1 Critical

instplanet commented 2 years ago

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

yuhao-xu commented 2 years ago

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

jypogoy commented 2 years ago

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

vldmr-k commented 2 years ago

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

toplinuxsir commented 2 years ago

The feature is already supported ? Thanks!

nathanlmeyers commented 1 year ago

+1

lemaadi commented 1 year ago

+1

algirdas-iterato commented 1 year ago

+1

RuiFG commented 1 year ago

+1

trungbui2000 commented 1 year ago

+1

AnthonyDugarte commented 1 year ago

+1

soulwawa commented 1 year ago

+1

9mm commented 1 year ago

I was mindblown to discover something so basic is not in here... I was like, there's no way this can be? Why must every single input be required as opposed to just returning an empty string I can work with to make things optional myself?