-
Notifications
You must be signed in to change notification settings - Fork 14.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom fetch all handler for vertica to not miss errors #34041
Changes from 8 commits
2976e1d
0352717
b44c6a4
9370620
b12ef48
a185e1b
754feab
429c09f
c177350
7b17b79
9ad7bee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,24 @@ | |
# under the License. | ||
from __future__ import annotations | ||
|
||
from typing import Any, Callable, Iterable, Mapping, overload | ||
|
||
from vertica_python import connect | ||
|
||
from airflow.providers.common.sql.hooks.sql import DbApiHook | ||
from airflow.providers.common.sql.hooks.sql import DbApiHook, fetch_all_handler | ||
|
||
|
||
def vertica_fetch_all_handler(cursor) -> list[tuple] | None: | ||
"""Replace the default DbApiHook fetch_all_handler .""" | ||
to_return = fetch_all_handler(cursor) | ||
# loop on all statement result sets to get errors | ||
if cursor.description is not None: | ||
while cursor.nextset(): | ||
if cursor.description is not None: | ||
row = cursor.fetchone() | ||
while row: | ||
row = cursor.fetchone() | ||
return to_return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this value be changed during the execution of lines 31-36? It seems this variable is not touched after it is assigned. But I guess this is something related to cursor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returned value will not change between line 31 to 37, all this code is here to make vertica client throws error. each insert has its own result set and if you don't try to fetch data of thoses result sets you won't detect error on the second insert. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nasty behaviour of Vertica There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, fortunately that as beginning with airflow I tested that it worked as I thincked, but if I had the habit with other database before I may have ended with a really bad surprise. |
||
|
||
|
||
class VerticaHook(DbApiHook): | ||
|
@@ -99,3 +114,40 @@ def get_conn(self) -> connect: | |
|
||
conn = connect(**conn_config) | ||
return conn | ||
|
||
@overload | ||
def run( | ||
self, | ||
sql: str | Iterable[str], | ||
autocommit: bool = ..., | ||
parameters: Iterable | Mapping[str, Any] | None = ..., | ||
handler: None = ..., | ||
split_statements: bool = ..., | ||
return_last: bool = ..., | ||
) -> None: | ||
... | ||
|
||
@overload | ||
def run( | ||
self, | ||
sql: str | Iterable[str], | ||
autocommit: bool = ..., | ||
parameters: Iterable | Mapping[str, Any] | None = ..., | ||
handler: Callable[[Any], Any] = ..., | ||
split_statements: bool = ..., | ||
return_last: bool = ..., | ||
) -> Any | list[Any]: | ||
... | ||
|
||
def run( | ||
self, | ||
sql: str | Iterable[str], | ||
autocommit: bool = False, | ||
parameters: Iterable | Mapping | None = None, | ||
handler: Callable[[Any], Any] | None = None, | ||
split_statements: bool = False, | ||
return_last: bool = True, | ||
) -> Any | list[Any] | None: | ||
if handler == fetch_all_handler: | ||
handler = vertica_fetch_all_handler | ||
return DbApiHook.run(self, sql, autocommit, parameters, handler, split_statements, return_last) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we rename
to_return
? Doesrows
make sense as the name of this value? orresult
might be a bit better thanto_return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I'll rename it as result