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

[Jinja] {{from_dttm}} and {{to_dttm}} variables not working as expected #21793

Open
2 of 3 tasks
phongvu99 opened this issue Oct 13, 2022 · 11 comments
Open
2 of 3 tasks
Labels
#bug Bug report

Comments

@phongvu99
Copy link

phongvu99 commented Oct 13, 2022

Description: Jinja Template {{from_dttm}} and {{to_dttm}} variables not working in SQL queries.

How to reproduce the bug

  1. Go to 'SQL Lab'
  2. Write a query containing either the {{from_dttm}} or {{to_dttm}} variables
  3. Run the query
  4. See the error

Expected results

The results filtered by the date - From A TO B

Actual results

Parameter error
Failed to execute query '1314' - 'SELECT *
FROM lorem_ipsum.Dim_Lorem_Ipsum
WHERE (
  {% if from_dttm is not none %}
      dttm_col > {{ from_dttm }} AND
  {% endif %}
  {% if to_dttm is not none %}
      dttm_col < {{ to_dttm }} AND
  {% endif %}
  true
)': The following parameters in your query are undefined: "from_dttm", "to_dttm". 
 Please check your template parameters for syntax errors and make sure they match across your SQL query and Set Parameters. Then, try running your query again.

Screenshots

Superset Jinja Variables Issue

Environment

(please complete the following information):

Checklist

Make sure to follow these steps before submitting your issue - thank you!

  • I have checked the superset logs for python stacktraces and included it here as text if there are any.
  • I have reproduced the issue with at least the latest released version of superset.
  • I have checked the issue tracker for the same issue and I haven't found one similar.

Additional context

#19619 The issue first discussion, but no solution so far

@phongvu99 phongvu99 added the #bug Bug report label Oct 13, 2022
@ASchmidtGit
Copy link

ASchmidtGit commented Oct 28, 2022

Had the same problem.
Thing was i executed the query from the sql-lab. But i had to transform my dataset to a virtual one, the query will be added here and after that define a time range filter in the dataset itself.

This tutorial send me the right way

@phongvu99
Copy link
Author

Had the same problem. Thing was i executed the query from the sql-lab. But i had to transform my dataset to a virtual one, the query will be added here and after that define a time range filter in the dataset itself.

This tutorial send me the right way

Thanks, but it's still not working. The key is to get the SQL Lab to recognize the param, and return N/A - due to filter_value instead of Parameter error

@mona-mk
Copy link

mona-mk commented Jan 16, 2023

This is indeed an annoying bug. What I needed:

  • I needed a virtual dataset, and how I create a virtual dataset typically is: from sql editor, let's say I have SELECT '{{ from_dttm }}', I then under Parameters I add {"from_dttm": "2023-01-01 00:00:00"}. What happens here, is that query goes through and I can then click to explore the dataset and create virtual dataset from there. However, this method, causes a sticky value of "2023-01-01 00:00:00" , and value won't get updated from the filters in the view.
    What I did to fix this:
  • created the virtual dataset from the dataset view. Opened the original dataset and converted it to a new virtual dataset, defined the sql statements and changed the name. Now this new one, won't have the cached value of "2023-01-01 00:00:00". However, I still need to click on "SYNC COLUMNS FROM SOURCE" to get the correct columns added, and with the from_dttm being in the sql it fails to sync. Now, I have to once delete that statement, sync, and bring back the from_dttm to the query.

@Ralkion
Copy link
Contributor

Ralkion commented Nov 22, 2023

You can actually get around this in SQL Lab by setting the values yourself at the top of the SQL Lab window.

{% set from_dttm = '2023-09-18T12:00:00' %}
{% set to_dttm = '2023-10-18T12:00:00' %}

SELECT * FROM ...

@Henri-J-Norden
Copy link

Henri-J-Norden commented Jan 23, 2024

Expanding on @Ralkion's solution, I found a nice concise way to set defaults with Jinja, that doesn't overwrite actual values later:

WITH 
  toDateTime('{{ from_dttm | default('2023-09-18T12:00:00', true) }}') AS from_dttm,
  toDateTime('{{ to_dttm | default('2023-10-18T20:00:00', true) }}') AS to_dttm,
  to_dttm - from_dttm AS range_dttm,
  {{ row_limit | default(1000, true) }} AS row_limit
SELECT *
FROM ...

NB: toDateTime() is specific to the ClickHouse SQL dialect, you will probably need to use some alternative like CONVERT()

Alternatively, using the datetime bounds from the dataset as the defaults
WITH
  (SELECT MIN( YOUR_DATETIME_FIELD ) FROM ...) AS _first_dttm,
  (SELECT MAX( YOUR_DATETIME_FIELD ) FROM ...) AS _last_dttm,
  toDateTime("{{ from_dttm | default('_first_dttm', true) }}") AS from_dttm,
  toDateTime("{{ to_dttm | default('_last_dttm', true) }}") AS to_dttm,
  to_dttm - from_dttm AS range_dttm,
  {{ row_limit | default(1000, true) }} AS row_limit
SELECT *
FROM ...

@rusackas
Copy link
Member

rusackas commented May 13, 2024

It's been a long time sine anyone touched this, and it sounds like there are valid workarounds. I'm tempted to close it as stale. Are there any proposals to be had on how to fix this more comprehensively, or docs that are worth adding to the documentation site? Is this even still an issue in 3.1/4.0 (the currently supported versions)?

@rusackas rusackas added the validation:required A committer should validate the issue label May 13, 2024
@JZ6
Copy link
Contributor

JZ6 commented May 29, 2024

Still seems to be an Issue on 3.1.1
image

@rusackas rusackas removed the validation:required A committer should validate the issue label May 29, 2024
@rusackas
Copy link
Member

rusackas commented Sep 27, 2024

Still an issue on 4.x, I presume? It'd be appreciated if anyone can validate this in newer, better supported versions.

@mnesarco
Copy link

mnesarco commented Dec 4, 2024

Hi, I have been trying to use template sql feature without any success, i try to replicate one of the examples from docs:

{% set time_filter = get_time_filter("dttm", remove_filter=True) %}
{% set from_expr = time_filter.from_expr %}
{% set to_expr = time_filter.to_expr %}
{% set time_range = time_filter.time_range %}
SELECT
  *,
  '{{ time_range }}' as time_range
FROM logs
{% if from_expr or to_expr %}WHERE 1 = 1
{% if from_expr %}AND dttm >= {{ from_expr }}{% endif %}
{% if to_expr %}AND dttm < {{ to_expr }}{% endif %}
{% endif %}

image

Superset version: 4.1.1

@jkryanchou
Copy link

I have met the same issue as you met.. @mnesarco

@mwtrew
Copy link

mwtrew commented Jan 16, 2025

I'm having the same problem as @mnesarco on 4.0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#bug Bug report
Projects
None yet
Development

No branches or pull requests

10 participants