dosubot[bot] commented on issue #29378: URL: https://github.com/apache/superset/issues/29378#issuecomment-3180551358
<!-- Answer --> This is a nuanced issue with Superset's Jinja templating in calculated columns, especially in the ORDER BY clause. Based on recent code and PRs, Superset is designed to process Jinja templates for calculated columns and metrics by applying a template processor to SQL expressions before query execution, including those used in ORDER BY clauses ([superset/connectors/sqla/models.py](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/connectors/sqla/models.py), [superset/models/helpers.py](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py)). However, inconsistencies can occur if the rendered (processed) expression isn't carried through all clause constructions. This is especially true for dynamic calculated columns referenced in ORDER BY, which may explain why you see raw Jinja in ORDER BY but not in SELECT or WHERE. Recent PRs, notably [#30735 by Vitor-Avila](https://github.com/apache/superset/pull/30735), improved handling of calculated columns and metrics using Jinja, ensuring their SQL expressions are collected and processed for template rendering in all relevant query parts. Tests in these PRs assert that calculated columns are correctly included in ORDER BY, but the issue may persist in 4.0.1. If you're able to upgrade, Superset 4.1.0 and later include fixes for extra cache keys and improved Jinja handling in calculated columns and metrics. If not, a practical workaround is to avoid using Jinja in calculated columns that are referenced in ORDER BY, or manually substitute the evaluated value as you did in SQL Lab. If you need more technical details or want to discuss possible code-level solutions, recent changes and tests in [#30735](https://github.com/apache/superset/pull/30735) and [#30350](https://github.com/apache/superset/pull/30350) are good references for how this logic is evolving. If the issue persists after upgrading, it may be worth reviewing how calculated columns are defined and referenced in your dataset. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/66c1d836-fa02-41a5-baa7-f1cc3ffc3d5b?feedback_type=other)</sup> [](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=githu b) [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/29378) -- 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: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org