{"record":{"id":"2d80bc5e1aa3e8e0","repo":"apache/superset","slug":"error-in-jinja-expression-in-column-expression","errorCode":null,"errorMessage":"Error in jinja expression in column expression: %(msg)s","messagePattern":"Error in jinja expression in column expression: (.+?)","errorType":"validation","errorClass":"QueryObjectValidationError","httpStatus":400,"severity":"error","filePath":"superset/connectors/sqla/models.py","lineNumber":1214,"sourceCode":"            self.db_engine_spec.engine,\n        )\n\n    def get_sqla_col(\n        self,\n        label: str | None = None,\n        template_processor: BaseTemplateProcessor | None = None,\n    ) -> Column:\n        label = label or self.column_name\n        db_engine_spec = self.db_engine_spec\n        column_spec = db_engine_spec.get_column_spec(self.type, db_extra=self.db_extra)\n        type_ = column_spec.sqla_type if column_spec else None\n        if expression := self.expression:\n            if template_processor:\n                try:\n                    expression = template_processor.process_template(expression)\n                except SupersetSyntaxErrorException as ex:\n                    msg = str(ex)\n                    raise QueryObjectValidationError(\n                        _(\n                            \"Error in jinja expression in column expression: %(msg)s\",\n                            msg=msg,\n                        )\n                    ) from ex\n                if expression != self.expression:\n                    # Re-check the rendered expression before embedding it.\n                    expression = validate_rendered_expression(\n                        expression,\n                        self.database,\n                        self.table.catalog if self.table else None,\n                        self.table.schema if self.table else None,\n                    )\n            expression = self._validate_stored_expression(expression)\n            col = literal_column(expression, type_=type_)\n        else:\n            col = column(self.column_name, type_=type_)\n        col = self.database.make_sqla_column_compatible(col, label)","sourceCodeStart":1196,"sourceCodeEnd":1232,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/connectors/sqla/models.py#L1196-L1232","documentation":"QueryObjectValidationError raised in TableColumn.get_sqla_col (models.py:1214) when Jinja processing of a stored column expression throws SupersetSyntaxErrorException. Columns with a SQL expression (calculated columns) are rendered through the sandboxed template processor before being embedded in the query; template syntax errors, sandbox violations, or undefined variables in that expression surface here with the underlying message.","triggerScenarios":"A dataset calculated column whose expression contains Jinja ({{ ... }} or {% ... %}) that fails to render at query time — e.g. {{ filter_values('x')[0] }} when no such filter exists, malformed {{, or a blocked construct. Triggered by any chart query selecting that column.","commonSituations":"Calculated column authored with macros that assume dashboard filters not present in a scheduled report/alert context; typos in Jinja; Jinja sandbox tightened after upgrade rejecting previously allowed filters.","solutions":["Open the dataset editor, edit the calculated column, and fix the Jinja (use |default guards so it renders with no runtime filter context).","Test the same Jinja in Explore's chart with the filters applied to confirm it renders.","Check the %(msg)s in the error for the precise Jinja failure (undefined name, syntax, security).","For scheduled contexts (reports/alerts), avoid macros that depend on dashboard runtime filters."],"exampleFix":"-- before (calculated column expression)\n{{ calc_value(filter_values('region')) }}\n\n-- after\n{{ calc_value(filter_values('region') | default(['ALL'], true)) }}","handlingStrategy":"try-catch","validationCode":"from jinja2.sandbox import SandboxedEnvironment\n\ndef expression_renders(expression: str, context: dict | None = None) -> bool:\n    try:\n        SandboxedEnvironment().from_string(expression).render(context or {})\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"from superset.exceptions import QueryObjectValidationError\n\ntry:\n    sqla_col = table_column.get_sqla_col(template_processor=processor)\nexcept QueryObjectValidationError as ex:\n    if \"column expression\" in str(ex):\n        log_dataset_issue(dataset_id, ex)\n    raise","preventionTips":["Default-guard all filter-dependent macros in calculated columns.","Test calculated columns in headless contexts (report render), not just interactive Explore.","Render-test dataset expressions with an empty context in CI."],"tags":["jinja","calculated-column","dataset","superset"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}