{# Always-open query review surface used by opportunity and job details. The order is deliberately Original -> Suggested -> Diff. Users start from the query that actually ran, then inspect the proposed SQL, then use the diff for a focused review. #} {% macro query_copy_button() %} {% endmacro %} {% macro query_sql_pane(sql) %}
{{ query_copy_button() }}
{% for line in (sql or "").split('\n') %}{{ loop.index }}{{ line }}
{% endfor %}
{% endmacro %} {% macro query_review_card(primary_query_improvement=None, fallback_query="", changes_count=0, query_improvements=None) %} {% set _candidates = query_improvements if query_improvements else [] %} {% if not _candidates and primary_query_improvement %} {% set _candidates = [primary_query_improvement] %} {% endif %} {% set _ns = namespace(has_sql=false, has_suggested=false, has_diff=false) %} {% for imp in _candidates %} {% if imp.before_content or imp.after_content or imp.diff_lines %} {% set _ns.has_sql = true %} {% endif %} {% if imp.after_content %} {% set _ns.has_suggested = true %} {% endif %} {% if imp.diff_lines %} {% set _ns.has_diff = true %} {% endif %} {% endfor %} {% if fallback_query %} {% set _ns.has_sql = true %} {% endif %} {% set _has_alternative_views = _ns.has_suggested or _ns.has_diff %} {% if _ns.has_sql %}

Query

{% if _has_alternative_views %} Review the query body with original, suggested, and diff views. {% else %} No deterministic SQL rewrite is available. Inspect the latest execution here. {% endif %}

{% if _candidates | length > 1 %} {% endif %} {% if changes_count %} {{ changes_count }} SQL {{ "change" if changes_count == 1 else "changes" }} {% endif %}
{% if _has_alternative_views %}
{% if _ns.has_suggested %} {% endif %} {% if _ns.has_diff %} {% endif %}
{% endif %} {% if _has_alternative_views %}
{% endif %} {% if _candidates %} {% for imp in _candidates %}
{% set _original_sql = imp.before_content or fallback_query or "" %} {% set _suggested_sql = imp.after_content or "" %} {% set _diff_lines = imp.diff_lines %} {% set _template_id = imp.template_id or imp.rule_type or "query" %}
{% if _original_sql %} {{ query_sql_pane(_original_sql) }} {% else %}

No original SQL is available for this execution.

{% endif %}
{% if _ns.has_suggested %} {% endif %} {% if _ns.has_diff %} {% endif %}
{% endfor %} {% else %}
{{ query_sql_pane(fallback_query) }}
{% endif %} {% if _has_alternative_views %}
{% endif %}
{% endif %} {% endmacro %}