Skip to content

Calculated column jinja templating fails in ORDER BY clause  #29378

Open
@lilotter45

Description

@lilotter45

Bug description

When attempting to use a calculated column that uses jinja templating as the metric in a bar chart, the jinja placeholders are included in the ORDER BY clause of the executed query, rather than being replaced with rendered SQL string. The template renders correctly in the SELECT statement of the same query.

In this example, the calculated column is defined as

IIF(MeasureValue < 
    {% if filter_values('fresh_min_bcs')|length %} 
        {{ filter_values('fresh_min_bcs')[0] }} 
    {% else %} 
        2.75 
    {% endif %},
    1,
    0
)

The executed query (trimmed for brevity) is

WITH cteMeasure AS (SELECT ... FROM ...),
     __cte AS (SELECT ... )
SELECT TOP 10000 rpt.DateTrunc('QUARTER' , MeasureDate) AS [MeasureDate_Group],
           IIF(SortNum >= 3 , '3' , CAST(SortNum AS VARCHAR(2))) AS [SortGroup],
-- The template renders correctly on the following line
           sum(IIF(MeasureValue < 2.75, 1, 0)) AS [COUNT(Is Low BCS)]
FROM __cte
WHERE ...
GROUP BY ...
-- The template does not render correctly in the ORDER BY clause on the following line
ORDER BY sum(IIF(MeasureValue < {% if filter_values('fresh_min_bcs')|length %} {{ filter_values('fresh_min_bcs')[0] }} {% else %} 2.75 {% endif %}, 1, 0)) DESC;

How to reproduce the bug

  1. Create a dataset with a calcuated field that uses jinja templating
  2. Create a Bar Chart based on the above dataset
  3. Add the calculated column as a METRIC; the aggregration function does not matter
  4. Click UPDATE CHART
  5. An "Unexpected error" will be returned, citing "Incorrect syntax near '%'"; clicking on ... and "View query" will show the template text rather than rendered text in the ORDER BY clause of the executed query

Screenshots/recordings

No response

Superset version

4.0.1

Python version

3.10

Node version

Not applicable

Browser

Chrome

Additional context

  • Superset is installed via pip, in a virtual environment
  • ENABLE_TEMPLATE_PROCESSING feature flag is enabled
  • The dataset is a virtual dataset from a query against MS SQL Server 2019: Microsoft SQL Server 2019 (RTM-CU27) (KB5037331) - 15.0.4375.4 (X64) Jun 10 2024 18:00:07 Copyright (C) 2019 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    global:jinjaRelated to Jinja templating

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions