PiXOT97 commented on issue #34543:
URL: https://github.com/apache/superset/issues/34543#issuecomment-3417015851

   Hello, forgot to post my workournd, sorry. @msyavuz  Here you go guys:
   
   Single Parameter Filter with Delimiter
   The Problem
   When embedding Superset dashboards with multiple URL parameters, Jinja 
template race conditions can occur, causing filters to fail or behave 
unpredictably:
   
   ```
   
CAR_IDS=1,2,3&BRAND_IDS=10,20&CHASSIS_IDS=100,200&DEALER_COUNTRY=USA&DEALER_CITY=Boston&START_DATE=2025-07-01&END_DATE=2025-08-31
   ```
   
   ### The Solution
   Instead of multiple URL parameters, use a **single parameter** with a 
delimiter (`~`) to separate different filter types:
   ```
   ?INJECT_FILTERS=1,2,3~10,20~100,200~USA~Boston~~2025-07-01~2025-08-31
   ```
   
   ### Parameter Structure
   The `INJECT_FILTERS` parameter uses `~` (tilde) as a positional separator 
and `,` (comma) to separate multiple values within each position:
   ```
   INJECT_FILTERS = "part0~part1~part2~part3~part4~part5~part6~part7"
   Position Mapping (Car Example):
   
   parts[0] = CAR_IDS (e.g., "1,2,3")
   parts[1] = BRAND_IDS (e.g., "10,20")
   parts[2] = CHASSIS_IDS (e.g., "100,200")
   parts[3] = DEALER_COUNTRY (e.g., "USA")
   parts[4] = DEALER_CITY (e.g., "Boston")
   parts[5] = CAR_TYPE (e.g., "Sedan,SUV")
   parts[6] = START_DATE (YYYY-MM-DD format)
   parts[7] = END_DATE (YYYY-MM-DD format)
   ```
   Important: The order is always respected. If a parameter is not needed, 
leave it empty between delimiters (e.g., ~~ for missing parameters).
   Implementation in SQL/Jinja
   
   Example: Single Value Parameter (Car Type)
   ```
   WHERE
     {% if filter_values('car_type') %}
       car_type IN (
         {% for val in filter_values('car_type') %}
           '{{ val }}'{{ "," if not loop.last else "" }}
         {% endfor %}
       )
     {% elif url_param('INJECT_FILTERS') %}
       {% set inject_filters = url_param('INJECT_FILTERS') %}
       {% set parts = inject_filters.split('~') %}
       {% if parts|length > 5 and parts[5] %}
         car_type IN (
           {% for type_id in parts[5].split(',') %}
             '{{ type_id.strip() }}'{{ "," if not loop.last else "" }}
           {% endfor %}
         )
       {% else %}
         TRUE
       {% endif %}
     {% else %}
       TRUE
     {% endif %}
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to