### Apache Airflow Provider(s)

common-sql

### Versions of Apache Airflow Providers

apache-airflow-providers-common-sql==1.3.1

### Apache Airflow version

2.5.0

### Operating System

Debian GNU/Linux 11 (bullseye)

### Deployment

Other Docker-based deployment

### Deployment details

Docker image based on the official image (couple of tools added) deployed on AWS ECS Fargate

### What happened

I have a task which uses the table check operator to run some table stakes data validation.  But I don't want it failing every time it runs in our test environments which do not get records every day.  So there's a templated switch in the sql:

```python
    test_data_changed_yesterday = SQLTableCheckOperator(
        task_id="test_data_changed_yesterday",
        table="reporting.events",
        conn_id="pg_conn",
        checks={"changed_record_count": {"check_statement": "count(*) > 0"}},
        partition_clause="""
        {% if var.value.get('is_test_env', False) %}
        modifieddate >= '2015-12-01T01:01:01.000Z'
        {% else %}
        modifieddate >= '{{ data_interval_start.isoformat() }}' and
            modifieddate < '{{ data_interval_end.isoformat() }}'
        {% endif %}
        """,
    )
```

This shows correctly in the rendered field for the task. Not pretty, but it works:

```
('\n'
 '        \n'
 "        modifieddate >= '2015-12-01T01:01:01.000Z'\n"
 '        \n'
 '        ')
```

However, in the logs I see it's running this query:

```
[2022-12-07, 11:49:34 UTC] {sql.py:364} INFO - Running statement: SELECT check_name, check_result FROM (
    SELECT 'changed_record_count' AS check_name, MIN(changed_record_count) AS check_result
    FROM (SELECT CASE WHEN count(*) > 0 THEN 1 ELSE 0 END AS changed_record_count
          FROM reporting.events WHERE 
        {% if var.value.get('is_test_env', False) %}
        modifieddate >= '2015-12-01T01:01:01.000Z'
        {% else %}
        modifieddate >= '{{ data_interval_start.isoformat() }}' and
            modifieddate < '{{ data_interval_end.isoformat() }}'
        {% endif %}
        ) AS sq
    ) AS check_table, parameters: None
```

Which unsurprisingly the DB rejects as invalid sql!

### What you think should happen instead

The rendered code should be used in the sql which is run!

I think this error comes about through this line: https://github.com/apache/airflow/blob/main/airflow/providers/common/sql/operators/sql.py#L576 which is run in the `__init__` method of the operator. i.e. before templating is applied in the build up to calling `execute(context)`.

### How to reproduce

Use the operator with a templated `partition_clause`

### Anything else

Happens every time

### Are you willing to submit PR?

- [X] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)

#
#
#
from __future__ import annotations
import datetime
from unittest import mock
from unittest.mock import MagicMock
import pytest
from airflow import DAG
from airflow.exceptions import AirflowException
from airflow.models import Connection, DagRun, TaskInstance as TI, XCom
from airflow.operators.empty import EmptyOperator
from airflow.providers.common.sql.hooks.sql import fetch_all_handler
from airflow.providers.common.sql.operators.sql import (
    BranchSQLOperator,
    SQLCheckOperator,
    SQLColumnCheckOperator,
    SQLExecuteQueryOperator,
    SQLIntervalCheckOperator,
    SQLTableCheckOperator,
    SQLThresholdCheckOperator,
    SQLValueCheckOperator,
)
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.utils import timezone
from airflow.utils.session import create_session
from airflow.utils.state import State
class MockHook: