{% extends 'Agent/main.jinja2' %}

{%- block instructions %}
# Instructions
Write a SQL query for the user's data transformation request, focusing on intent over literal interpretation.

## PRIMARY RULES
1. Reuse materialized data - Check "Current Knowledge" first before writing CTEs
2. Column quotes - Use double quotes: `"column_name"`
3. String quotes - Single quotes: `'string_value'`
4. No CREATE statements - System handles materialization
5. No LIMIT clauses unless explicitly requested by the user - Pagination is automatically handled
6. Progressive approach - Each step builds on previous
7. Do not rename columns unless explicitly instructed

## Query Patterns
- NULL handling: Exclude unless specified
- Clean data: `TRIM("column") != ''`, filter -9999, empty strings
- Headers: Use OFFSET 1 for header/metadata rows
- Mixed units: Use CASE to normalize before aggregating
- Temporal data: Check MIN/MAX dates before joining, ensure overlap validation
- No inline comments in SQL code
- Use CTEs only when necessary

{% if not is_final_step -%}
## Discovery Mode
- LIMIT 100 for exploration only
- One table at a time, no UNION
- Entity search: Use multiple ILIKE patterns with variations
- Format variation checking: underscores vs spaces using ILIKE
{% endif %}

## Requesting Additional Schemas
You can ONLY write queries using tables shown above with full column details. Tables listed under "Others available" do NOT have their schemas loaded yet - if you need them, a prior step will select them.
{%- endblock -%}

{%- block context -%}
{%- if memory.get('metaset') %}
### Data summary
{{ memory["metaset"].compact_context(n_others=17) }}
{%- endif %}

{%- if 'sql' in memory %}
Previous: `{{ memory['sql'] }}`
{%- endif %}

{%- if memory.get('data') is not none %}
Data Summary:
{{ memory["data"] }}
{%- endif %}

Use `{{ dialect }}` SQL dialect.
{%- if dialect == 'duckdb' %}

## DuckDB Essentials
- Table references: Use exact names `read_csv('table.csv')`, alias if needed `as table_name`
- Reference just the table in the SQL query: `source {{ source_table_sep }} table`
- Range predicates: `"date" >= '2023-06-01' AND "date" < '2023-09-01'`
- Date selection: CAST(col as DATE) == '2023-06-01'
- Window filtering: `QUALIFY ROW_NUMBER() OVER (...) <= 5`
- Safe conversions: Use `TRY_CAST` for type conversions
- Use materialized tables when available
{% endif %}
{%- if dialect == 'snowflake' %}
- No quotes around database/schema/table names
{%- endif %}
{% if not is_final_step %}
Step {{ step_number }}: {{ current_step }}
{% endif %}

{%- if sql_plan_context %}
## Current Knowledge:
{{ sql_plan_context }}

Use materialized tables above instead of rebuilding CTEs
{%- endif %}

{% if discovery_context %}
## Discovery Results
{{ discovery_context }}

Use the ACTUAL values from discoveries above - don't assume or guess!
{%- endif %}
{% endblock %}

{%- block errors %}
{{ super() }}
{% if errors is defined and errors %}
## Quick Fixes
- Mixed units: Use CASE to normalize units before aggregating
- Cast errors: Use `TRY_CAST`, check for headers with OFFSET 1  
- Table names: Use exact `read_parquet('file.parq')`
- String quotes: Single quotes only `'text_value'`
{%- endif -%}
{% endblock -%}
