superset icon indicating copy to clipboard operation
superset copied to clipboard

Not able to use get_time_filter function

Open Keveen-ghori opened this issue 1 year ago • 11 comments

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.

Keveen-ghori avatar Dec 23 '24 06:12 Keveen-ghori

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.

    # In superset_config.py
    FEATURE_FLAGS = {
        'ENABLE_TEMPLATE_PROCESSING': True,
    }
    
  2. 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.

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

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

dosubot[bot] avatar Dec 23 '24 06:12 dosubot[bot]

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

Not work

sonfire186 avatar Dec 23 '24 10:12 sonfire186

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

Keveen-ghori avatar Dec 23 '24 11:12 Keveen-ghori

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,

zackertypical avatar Dec 27 '24 09:12 zackertypical

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.

JooDye avatar Dec 31 '24 05:12 JooDye

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?

ewan-nrp avatar Jan 17 '25 01:01 ewan-nrp

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

rusackas avatar Jan 17 '25 22:01 rusackas

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

rusackas avatar May 07 '25 23:05 rusackas

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

Image Image Image

tiniacoleyba avatar May 09 '25 09:05 tiniacoleyba

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

devlatte avatar May 10 '25 05:05 devlatte

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 ?

finprog avatar Jun 12 '25 08:06 finprog

I upgrade to 4.1.3 and still have the same issue

anhtu15 avatar Aug 27 '25 05:08 anhtu15