EkaterinaIakuhina opened a new issue, #45340:
URL: https://github.com/apache/superset/issues/45340

   ### Bug description
   
   ## Description 
   
   When creating a virtual dataset in SQL Lab and using the 
**get_time_filter()** Jinja macro to handle time filters inside the dataset, 
the macro fails to work correctly if the date column used in the macro is not 
included in the final SELECT statement.
   
   At the same time Superset query automatically appends the date column in the 
final SELECT statement even when the initial SQL in dataset does not select it. 
   
   For example, two cases represented: 
   
   ### CASE 1: Date column name provided and exists only in subquery 
   
   **Initial SQL query:**
   
   ```
   {% set time_filter = get_time_filter( "date", default="Last week", 
remove_filter=True) %}
   {% set d_from = time_filter.from_expr %}
   {% set d_to = time_filter.to_expr %}
   with cte as 
   (
   SELECT date
         ,lastUTMSource
   FROM  table_visits
   WHERE 1=1
     and date > {{d_from}} and date < {{d_to}}
   GROUP BY ALL
   ORDER BY date
   )
   SELECT lastUTMSource
   FROM cte
   GROUP BY ALL
   ```
   
   
   **Error ocсurred:** Exception: Unknown expression identifier `date` 
   
   **The query actually sent to the database (ClickHouse)**
   
   ```
   SELECT
     "lastUTMSource" AS "lastUTMSource",
     "date" AS "date"
   FROM (
     WITH cte AS (
       SELECT
         date,
         lastUTMSource
       FROM table_visits
       WHERE
         1 = 1
         AND date > toDate('2026-10-18')
         AND date < toDate('2026-10-24')
       GROUP BY ALL
       ORDER BY
         date
     )
     SELECT
       lastUTMSource
     FROM cte
     GROUP BY ALL
   ) AS "virtual_table"
   LIMIT 1000
   ```
   
   **Observation:** The template correctly resolves the filter boundaries 
(2026-10-18 to 2026-10-24), but Superset's query builder injects "date" into 
the final SELECT clause. Since the final outer query does not have date column, 
error Unknown expression occurs. 
   
   ### CASE 2: Date column name is not provided, so the Native Time Filter on 
the dashboard should find the date automatically 
   
   **Initial SQL query:** 
   ```
   {% set time_filter = get_time_filter( default="Last week", 
remove_filter=True) %}
   {% set d_from = time_filter.from_expr %}
   {% set d_to = time_filter.to_expr %}
   
   with cte as 
   (
   SELECT date
         ,lastUTMSource
   FROM  table_visits
   WHERE 1=1
     and date > {{d_from}} and date < {{d_to}}
   GROUP BY ALL
   ORDER BY date
   )
   SELECT lastUTMSource
   FROM cte
   GROUP BY ALL
   ```
   
   **Error occurred:** Exception: Unknown expression or function identifier 
None  in scope cte. (UNKNOWN_IDENTIFIER)
   
   **The query actually sent to the database (ClickHouse)**
   ```
   SELECT
     "lastUTMSource" AS "lastUTMSource",
     "date" AS "date"
   FROM (
     WITH cte AS (
       SELECT
         date,
         lastUTMSource
       FROM table_visits
       WHERE
         1 = 1 AND date > None AND date < None
       GROUP BY ALL
       ORDER BY
         date
     )
     SELECT
       lastUTMSource
     FROM cte
     GROUP BY ALL
   ) AS "virtual_table"
   WHERE
     "date" >= toDate('2026-10-18') AND "date" < toDate('2026-10-24')
   LIMIT 1000
   ```
   
   **Observation**: The macro resolves  from_expr and to_expr to None, even 
despite a default value. Superset still injects "date" into the outer SELECT 
clause and applies the time filter also on the outer query in the WHERE clause. 
   
   
   ### Dashboard filter configuration:
   
   The native filter on the dashboard is configured as:
   
   filter_type: time_range
   
   
   ### Expected behaviour
   get_time_filter() macro should function correctly in cases when date column 
exists only in an inner subquery. 
   
   - Should not inject date column automatically to outer query send to 
database 
   - Should resolve  from_expr and to_expr as date filter values as expected 
based on documentation 
   
   Like this example query send to database: 
   
   ```
   SELECT
     "lastUTMSource" AS "lastUTMSource"
   FROM (
     WITH cte AS (
       SELECT
         date,
         lastUTMSource
       FROM table_visits
       WHERE
         1 = 1
         AND date > toDate('2026-10-18')
         AND date < toDate('2026-10-24')
       GROUP BY ALL
       ORDER BY
         date
     )
     SELECT
       lastUTMSource
     FROM cte
     GROUP BY ALL
   ) AS "virtual_table"
   LIMIT 1000
   ```
   
   In addition, omitting the 'default' and 'remove_filter' parameters does not 
change anything, errors are still existing.
   Previous macros `from_ddtm` and `to_dttm` worked correctly as expected. 
Sadly they are not available anymore in Superset version 7.0.0.
   
   It is possible that I have missed something or am doing something 
incorrectly. Any hint or suggestion on how to achieve the desired behavior 
using the available parameters would be greatly appreciated. 
   
   
   
   ### Screenshots/recordings
   
   _No response_
   
   ### Superset version
   
   master / latest-dev
   
   ### Python version
   
   I don't know
   
   ### Node version
   
   I don't know
   
   ### Browser
   
   Chrome
   
   ### Additional context
   
   Superset version: 7.0.0
   
   
   
   ### Checklist
   
   - [x] I have searched Superset docs and Slack and didn't find a solution to 
my problem.
   - [x] I have searched the GitHub issue tracker and didn't find a similar bug 
report.
   - [ ] I have checked Superset's logs for errors and if I found a relevant 
Python stacktrace, I included it here as text in the "additional context" 
section.


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