Bug description

I am creating dataset for dynamic date range filter and dataset look like below query:

{% set time_filter = get_time_filter("bucket", default="Last week", remove_filter=True) %}
WITH calculated_data AS (
    SELECT 
        (wspmcdd.bucket AT TIME ZONE 'AUSTRALIA/BRISBANE' + INTERVAL '1 day') AS "date",
        wspmcdd.meter_id,
        wspmcdd.meter_channel_id,
        wspmcdd.maximum_value,
        wspmcdd.minimum_value,
        ROUND(
            CASE
                WHEN wspmcdd.channel_type = 'Interval' THEN wspmcdd.total_value::NUMERIC
                WHEN wspmcdd.channel_type = 'Cumulative' THEN (wspmcdd.maximum_value - LAG(wspmcdd.maximum_value) OVER (
                    PARTITION BY wspmcdd.meter_id, wspmcdd.meter_channel_id
                    ORDER BY wspmcdd.bucket
                ))::NUMERIC
                ELSE wspmcdd.total_value::NUMERIC
            END, 3
        ) AS calculated_value,
        wspmcdd.status,
        wspmcdd.nem12_status,
        wspmcdd.meter_type,
        wspmcdd.channel_type,
        wspmcdd.unit_of_measurement,
        wspmcdd.meter_property_name,
        wspmcdd.customer_name,
        wspmcdd.site_name,
        wspmcdd.physical_time_zone,
        wspmcdd.channel_name,
        wspmcdd.average_value,
        wspmcdd.reading_count,
        '{{ time_filter.time_range }}' AS time_range
    FROM 
        west_Side_place_meter_channel_data_daily AS wspmcdd
    JOIN 
        Dashboard_Meter_Type_Channel_Configuration AS dmtcc 
        ON lower(dmtcc.channel_name) = lower(wspmcdd.channel_name)
        AND dmtcc.meter_type = wspmcdd.meter_type
        AND lower(dmtcc.unit_of_measurement) = lower(wspmcdd.unit_of_measurement)
        AND lower(dmtcc.channel_type) = lower(wspmcdd.channel_type)
    WHERE wspmcdd.bucket >= ((TIMESTAMP {{ time_filter.from_expr }} + INTERVAL '-1 day') AT TIME ZONE 'AUSTRALIA/BRISBANE')
      AND wspmcdd.bucket < ((TIMESTAMP {{ time_filter.to_expr }}) AT TIME ZONE 'AUSTRALIA/BRISBANE')
      AND wspmcdd.meter_type = 'Electricity'
)
SELECT *
FROM 
    calculated_data
WHERE 
    "date" > ((TIMESTAMP {{ time_filter.to_expr }}))
ORDER BY 
    "date";

But i am getting error Error: Error while rendering virtual dataset query: 'get_time_filter' is undefined.

Screenshots/recordings

No response

Superset version

4.1.1

Python version

3.11

Node version

16

Browser

Chrome

Additional context

No response

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.
  • [X] 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.

Comment From: dosubot[bot]

The error message 'get_time_filter' is undefined suggests that the get_time_filter function is not available in the Jinja context for your SQL template. Here are a few steps to troubleshoot and resolve this issue:

  1. Enable Template Processing: Ensure that the ENABLE_TEMPLATE_PROCESSING feature flag is set to True in your superset_config.py file. This is necessary for using Jinja templating features like get_time_filter.

python # In superset_config.py FEATURE_FLAGS = { 'ENABLE_TEMPLATE_PROCESSING': True, }

  1. Check Jinja Context: Verify that the get_time_filter function is included in the Jinja context. This function should be part of the default Jinja context in Superset version 4.1.1, but if it's not, you might need to add it manually or check if there are any custom configurations that might be overriding the default context.

  2. Superset Version: Ensure that you are indeed using Superset version 4.1.1, as get_time_filter is available in this version [1].

  3. Restart Superset: After making changes to the configuration, restart your Superset server to apply the changes.

If these steps do not resolve the issue, there might be a deeper configuration problem or a bug in your specific setup.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

Comment From: sonfire186

python # In superset_config.py FEATURE_FLAGS = { 'ENABLE_TEMPLATE_PROCESSING': True, }

Not work

Comment From: Keveen-ghori

I already added that flag, but still getting same error.

Comment From: zackertypical

same to me, in doc: https://github.com/apache/superset/blob/master/docs/docs/configuration/sql-templating.mdx It says that

 from_dttm: start datetime value from the selected time range (None if undefined) (deprecated beginning in version 5.0, use get_time_filter instead)
to_dttm: end datetime value from the selected time range (None if undefined). (deprecated beginning in version 5.0, use get_time_filter instead)

but there is no version 5.0,

Comment From: JooDye

I understand the usage, but when it come to testing in SQL Lab, I have no idea on how to specify the template parameter for testing.

Comment From: ewan-nrp

Looking at the docker image, get_time_filter is not defined in the latest or 4.1.1 tag. This means that the tag / release notes are incorrect and or the image was pushed incorrectly?

Comment From: rusackas

For what it's worth, the deprecation notices in 5.0 are being merged right now in preparation for 5.0. Typically, when a feature is deprecated, it remains there until the next major version (in this case, 6.0, which does not have a release timeline yet).

Comment From: rusackas

Anyone still facing this, particularly in 4.1.2? It's been quiet for a long time, so updated context would be appreciated.

Comment From: tiniacoleyba

I am encountering the same issue with Superset version 4.1.1. I do have 'ENABLE_TEMPLATE_PROCESSING': True

Image

Image

Image

Comment From: devlatte

Hi everyone, I ran into the same issue and fixed it by disabling Superset’s automatic time_range filter when using the {{ from_dttm }} / {{ to_dttm }} templates. It only takes a couple of small edits across two files. Check out the patch here: https://gist.github.com/devlatte/203015c3c4a8cfc12f0313d87b3868aa

Comment From: finprog

Anyone still facing this, particularly in 4.1.2?

I get the same Error in superset 4.1.2 and can't use get_time_filter Do we have to wait for superset 4.1.3 to use get_time_filter ?

Comment From: anhtu15

I upgrade to 4.1.3 and still have the same issue