Skip to content
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

🚀 Added new functionality to saprfc source regarding where statement #899

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `Trino` source
- Added `MinIO` source
- Added `gen_split()` method to `SAPRFCV2` class to allow looping over a data frame with generator - improves performance
- Added `adjust_where_condition_by_adding_missing_spaces()` to `SAPRFC`. The function that is checking raw sql query and modifing it - if needed.
marcinpurtak marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
53 changes: 53 additions & 0 deletions src/viadot/sources/sap_rfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,43 @@
logger = logging.getLogger()


def adjust_where_condition_by_adding_missing_spaces(sql: str) -> str:
"""Function for adding white spaces between opearators and `WHERE` statement

Args:
sql (str): raw sql query passed in flow

Returns:
str: sql query after adding white spaces if needed
"""

# Check if 'WHERE' statement is not attached to 'FROM' or column name as there is need for space " " on both side of 'WHERE'
sql = re.sub(rf'{re.escape("WHERE")}(?<!\s)', "WHERE ", sql, flags=re.IGNORECASE)
sql = re.sub(rf'(?<!\s){re.escape("WHERE")}', " WHERE", sql, flags=re.IGNORECASE)
sql = re.sub(r"\s+", " ", sql)

# Check if operators are not attached to column or value as there is need for space " " on both side of operator
operators = ["<>", "!=", "<=", ">=", "!<", "!>", "=", ">", "<"]
reverse_check = [
"< >",
"! =",
"< =",
"> =",
"! <",
"! >",
]

for op in operators:
sql = re.sub(rf"(?<!\s){re.escape(op)}", f" {op}", sql)
sql = re.sub(rf"{re.escape(op)}(?<!\s)", f"{op} ", sql)
sql = re.sub(r"\s+", " ", sql)
for op_2 in reverse_check:
if op_2 in sql:
sql = sql.replace(op_2, "".join(op_2.split()))

return sql


def remove_whitespaces(text):
return " ".join(text.split())

Expand Down Expand Up @@ -407,6 +444,13 @@ def _get_where_condition(self, sql: str) -> str:
raise ValueError(
"WHERE conditions after the 75 character limit can only be combined with the AND keyword."
)
for val in client_side_filters.values():
if ")" in val:
raise ValueError(
"""Nested conditions eg. AND (col_1 = 'a' AND col_2 = 'b') found between or after 75 chararacters in WHERE condition!
Please change nested conditions part of query separeted with 'AND' keywords, or place nested conditions part at the begining of the where statement.
"""
)
else:
filters_pretty = list(client_side_filters.items())
self.logger.warning(
Expand Down Expand Up @@ -541,6 +585,7 @@ def query(self, sql: str, sep: str = None) -> None:

sep = sep if sep is not None else self.sep

sql = adjust_where_condition_by_adding_missing_spaces(sql=sql)
self.sql = sql

self.extract_values(sql)
Expand Down Expand Up @@ -866,6 +911,13 @@ def _get_where_condition(self, sql: str) -> str:
raise ValueError(
"WHERE conditions after the 75 character limit can only be combined with the AND keyword."
)
for val in client_side_filters.values():
if ")" in val:
raise ValueError(
"""Nested conditions eg. AND (col_1 = 'a' AND col_2 = 'b') found between or after 75 chararacters in WHERE condition!
Please change nested conditions part of query separeted with 'AND' keywords, or place nested conditions part at the begining of the where statement.
"""
)
else:
filters_pretty = list(client_side_filters.items())
self.logger.warning(
Expand Down Expand Up @@ -1000,6 +1052,7 @@ def query(self, sql: str, sep: str = None) -> None:

sep = sep if sep is not None else self.sep

sql = adjust_where_condition_by_adding_missing_spaces(sql=sql)
self.sql = sql

self.extract_values(sql)
Expand Down
Loading