apache/superset · error · QueryObjectValidationError

Error in jinja expression in datetime column: %(msg)s

Error message

Error in jinja expression in datetime column: %(msg)s

What it means

QueryObjectValidationError raised in TableColumn.get_time_column (models.py:1270) when Jinja processing of a temporal column's expression fails with SupersetSyntaxErrorException. This path is used when the column serves as the time/datetime axis (with expression, time grain, or epoch handling); its Jinja must render before Superset can build the timestamp expression.

Source

Thrown at superset/connectors/sqla/models.py:1270

        """
        label = label or utils.DTTM_ALIAS

        pdf = self.python_date_format
        is_epoch = pdf in ("epoch_s", "epoch_ms")
        column_spec = self.db_engine_spec.get_column_spec(
            self.type, db_extra=self.db_extra
        )
        type_ = column_spec.sqla_type if column_spec else DateTime
        if not self.expression and not time_grain and not is_epoch:
            sqla_col = column(self.column_name, type_=type_)
            return self.database.make_sqla_column_compatible(sqla_col, label)
        if expression := self.expression:
            if template_processor:
                try:
                    expression = template_processor.process_template(expression)
                except SupersetSyntaxErrorException as ex:
                    msg = str(ex)
                    raise QueryObjectValidationError(
                        _(
                            "Error in jinja expression in datetime column: %(msg)s",
                            msg=msg,
                        )
                    ) from ex
                if expression != self.expression:
                    # Re-check the rendered expression before embedding it.
                    expression = validate_rendered_expression(
                        expression,
                        self.database,
                        self.table.catalog if self.table else None,
                        self.table.schema if self.table else None,
                    )
            expression = self._validate_stored_expression(expression)
            col = literal_column(expression, type_=type_)
        else:
            col = column(self.column_name, type_=type_)
        time_expr = self.db_engine_spec.get_timestamp_expr(col, pdf, time_grain)

View on GitHub (pinned to f4587218dd)

Solutions

  1. Fix or guard the Jinja in the temporal column's expression so it renders in every context (default values for filter-dependent macros).
  2. If the macro is not needed for the temporal expression, remove the Jinja and use plain SQL.
  3. Re-run the chart with the dashboard filters present to confirm the macro itself is sound; then make it resilient for headless contexts.

Example fix

-- before (temporal column expression)
DATE_TRUNC('day', {{ event_time_col }})

-- after
DATE_TRUNC('day', {{ event_time_col | default("created_at", true) }})
Defensive patterns

Strategy: try-catch

Validate before calling

from jinja2.sandbox import SandboxedEnvironment

def temporal_expression_renders(expression: str) -> bool:
    try:
        SandboxedEnvironment().from_string(expression).render({})
        return True
    except Exception:
        return False

Try / catch

from superset.exceptions import QueryObjectValidationError

try:
    time_col = table_column.get_time_column(time_grain='P1D', template_processor=processor)
except QueryObjectValidationError as ex:
    if "datetime column" in str(ex):
        # the temporal column's Jinja is broken at the dataset level
        flag_dataset_for_maintenance(table_column.table_id)
    raise

Prevention

When it happens

Trigger: A dataset column marked is_dttm whose SQL expression contains Jinja that fails to render when a chart uses it as the time column with a time grain (e.g. {{ render_time('x') }} macro missing, or filter-dependent macro in a context without those filters).

Common situations: Templated time columns designed for dashboard filter context being used in alerts/reports or thumbnail generation where the filter context is absent; macro renamed or removed; malformed Jinja in the temporal expression.

Related errors


AI-assisted analysis of apache/superset@f4587218dd (2026-08-14). Data as JSON: /api/errors/2ca8a1f351176346. Report an issue: GitHub.