CleverAutoman opened a new pull request, #77000:
URL: https://github.com/apache/airflow/pull/77000
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull
request.
Write a good git commit message following this guide:
https://chris.beams.io/posts/git-commit/
Please make sure that your code changes are covered with tests.
And in case of new features or big changes remember to adjust the
documentation.
For user-facing UI changes, please attach before/after screenshots (or a
short
screen recording) so reviewers can assess the visual impact.
Feel free to ping (in general) for the review if you do not see reaction for
a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we
sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
---
## Why
When a statement run by `SnowflakeSqlApiOperator` fails, the error doesn't
tell you which statement it was. The non-deferrable path raises the raw status
dict, e.g. `{'<handle>': {'status': 'error', 'message': '...'}}`. The
deferrable path raises only `error: <message>`, without the handle. Neither
shows the SQL, so with multi-statement SQL you have to look up the failed
statement in Snowflake's query history.
Two smaller problems in the same code:
- If the submit response has no statement handle, Snowflake's error message
is dropped.
- `_process_response` reads `resp["message"]` directly in the 200 and
fallback branches, so a response without `message` raises `KeyError` and hides
the real error.
closes: #66619
## What
- `providers/snowflake/src/airflow/providers/snowflake/hooks/sql_api.py`:
- Add `SnowflakeSqlApiHook.format_statement_error`. All failure paths use
it. The message looks like:
```
Snowflake statement <handle> (statement 2 of 3) failed: <message>
SQL: <statement>
```
- To find the failed statement, split the SQL with the connector's
`util_text.split_statements`. `SnowflakeHook.run` uses the same function.
Comments and quotes are kept as written.
- If the number of statements doesn't match the number of handles, show
the whole SQL without a position. This happens with `;;` or a `BEGIN ... END`
block that isn't wrapped in `$$`. It never points at the wrong statement. If
splitting raises, it falls back the same way.
- Truncate the SQL to 500 characters (`ERROR_SQL_MAX_LENGTH`). Binding
values are never included.
- Move the 422 formatting (code, SQL state, handle) into
`_describe_error_response`. The fallback branch of `_process_response` now uses
it too.
- Read `message` with `.get()` in the 200 and fallback branches. They used
to raise `KeyError` when it was missing.
- `execute_query`: when the response has no statement handle, keep
Snowflake's message, code and SQL state in the error.
- `wait_for_query`: add the query id to the error.
- `get_result_from_successful_sql_api_query`: add the query id and
Snowflake's message to the error.
- `.../operators/snowflake.py`:
- `execute` (deferrable, before deferring), `poll_until_complete` and
`execute_complete` all use `format_statement_error`.
- `poll_until_complete` lists every failed statement from the polling
cycle, in order. Statements that succeeded or are still running are left out.
- `execute_complete` reads the handles from the event, because a resumed
task is a new instance and has no `query_ids`. Events without them (e.g. from a
triggerer on an older provider) keep the old `error: <message>` text.
- Exception types don't change.
- `.../triggers/snowflake_trigger.py`:
- The error event now includes `query_id` and `statement_query_ids`.
- If fetching a status fails, the message is `Failed to get status for
query <id>: ...`, the same as in `poll_on_queries`. The event carries no
statement context, so it isn't mistaken for a failed statement.
- The DAG and task id are not in the message. Airflow already shows them in
the task log, the UI, failure emails and callbacks.
## Tests
- New tests cover:
- picking the failed statement: quotes, escaped quotes, quoted
identifiers, comments, `$$` bodies, `execute immediate $$...$$`
- falling back to the whole SQL: `;;`, a bare scripting block, a failing
split
- truncation, and a missing message, SQL or query id
- each operator failure path: deferrable before deferring,
`poll_until_complete`, durable reconnect, `execute_complete` after resume and
with an old event
- a real trigger event passed to `execute_complete`
- the status-check message on the sync path
- binding values not appearing in the error
- Five existing tests have new expected values:
- `test_run_stops_at_first_failed_query`,
`test_snowflake_sql_trigger_failure_status`: the event has `query_id` and
`statement_query_ids`
- `test_snowflake_sql_trigger_exception`: new status-check message
- `test_wait_for_query_error_raises_when_requested`: query id in the
message
- `test_get_result_from_successful_sql_api_query_raises_error`: same
`match`, plus a check for the query id
- `providers/snowflake` unit tests: 770 passed, 14 skipped.
- Not tested on a real Snowflake account. The mocked responses follow the
SQL API docs.
## Future work
This PR only improves errors for failed SQL statements. Two other kinds of
errors still need work. I'd like to do them in separate PRs so each one stays
small:
- SQL errors from `SnowflakeHook` / `SQLExecuteQueryOperator`. These go
through the Snowflake connector instead of the SQL API, and their error message
doesn't include the query id either.
- HTTP errors from the SQL API, like `400 Client Error ... for url`. They
don't show Snowflake's error message. Fixing this means changing the request
and retry code that every API call uses, so it's better reviewed on its own.
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change below checkbox to `[X]` followed by the name of the tool, uncomment
the "Generated-by".
-->
- [x] Yes (please specify the tool below)
Generated-by: [Claude Opus 5.5] following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]