Skip to content

Commit 262bfc6

Browse files
committed
fix: ruff reformat
1 parent eb25d2a commit 262bfc6

File tree

3 files changed

+63
-67
lines changed

3 files changed

+63
-67
lines changed

awswrangler/athena/_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343

4444
_logger: logging.Logger = logging.getLogger(__name__)
4545

46+
4647
class _MergeClause(TypedDict, total=False):
4748
when: Literal["MATCHED", "NOT MATCHED", "NOT MATCHED BY SOURCE"]
4849
condition: str | None
4950
action: Literal["UPDATE", "DELETE", "INSERT"]
5051
columns: list[str] | None
5152

53+
5254
class _QueryMetadata(NamedTuple):
5355
execution_id: str
5456
dtype: dict[str, str]
@@ -68,6 +70,7 @@ class _WorkGroupConfig(NamedTuple):
6870
encryption: str | None
6971
kms_key: str | None
7072

73+
7174
def _get_s3_output(
7275
s3_output: str | None, wg_config: _WorkGroupConfig, boto3_session: boto3.Session | None = None
7376
) -> str:

awswrangler/athena/_write_iceberg.py

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@
1414
from awswrangler import _data_types, _utils, catalog, exceptions, s3
1515
from awswrangler._config import apply_configs
1616
from awswrangler.athena._executions import wait_query
17-
from awswrangler.athena._utils import (
18-
_get_workgroup_config,
19-
_start_query_execution,
20-
_WorkGroupConfig,
21-
_MergeClause
22-
)
17+
from awswrangler.athena._utils import _get_workgroup_config, _start_query_execution, _WorkGroupConfig, _MergeClause
2318
from awswrangler.typing import GlueTableSettings
2419

2520
_logger: logging.Logger = logging.getLogger(__name__)
@@ -232,59 +227,53 @@ def _validate_args(
232227
raise exceptions.InvalidArgumentCombination(
233228
"Either path or workgroup path must be specified to store the temporary results."
234229
)
235-
230+
236231
if merge_cols and merge_on_clause:
237-
raise exceptions.InvalidArgumentCombination(
238-
"Cannot specify both merge_cols and merge_on_clause. Use either merge_cols for simple equality matching or merge_on_clause for custom logic."
239-
)
240-
232+
raise exceptions.InvalidArgumentCombination(
233+
"Cannot specify both merge_cols and merge_on_clause. Use either merge_cols for simple equality matching or merge_on_clause for custom logic."
234+
)
235+
241236
if merge_on_clause and merge_match_nulls:
242-
raise exceptions.InvalidArgumentCombination(
243-
"merge_match_nulls can only be used together with merge_cols."
244-
)
245-
237+
raise exceptions.InvalidArgumentCombination("merge_match_nulls can only be used together with merge_cols.")
238+
246239
if merge_conditional_clauses and merge_condition != "conditional_merge":
247-
raise exceptions.InvalidArgumentCombination(
248-
"merge_conditional_clauses can only be used when merge_condition is 'conditional_merge'."
249-
)
250-
240+
raise exceptions.InvalidArgumentCombination(
241+
"merge_conditional_clauses can only be used when merge_condition is 'conditional_merge'."
242+
)
243+
251244
if (merge_cols or merge_on_clause) and merge_condition not in ["update", "ignore", "conditional_merge"]:
252245
raise exceptions.InvalidArgumentValue(
253246
f"Invalid merge_condition: {merge_condition}. Valid values: ['update', 'ignore', 'conditional_merge']"
254247
)
255-
248+
256249
if merge_condition == "conditional_merge":
257-
if not merge_conditional_clauses:
258-
raise exceptions.InvalidArgumentCombination(
259-
"merge_conditional_clauses must be provided when merge_condition is 'conditional_merge'."
260-
)
261-
262-
seen_not_matched = False
263-
for i, clause in enumerate(merge_conditional_clauses):
264-
if "when" not in clause:
265-
raise exceptions.InvalidArgumentValue(
266-
f"merge_conditional_clauses[{i}] must contain 'when' field."
267-
)
268-
if "action" not in clause:
269-
raise exceptions.InvalidArgumentValue(
270-
f"merge_conditional_clauses[{i}] must contain 'action' field."
271-
)
272-
if clause["when"] not in ['MATCHED', 'NOT MATCHED', 'NOT MATCHED BY SOURCE']:
273-
raise exceptions.InvalidArgumentValue(
274-
f"merge_conditional_clauses[{i}]['when'] must be one of ['MATCHED', 'NOT MATCHED', 'NOT MATCHED BY SOURCE']."
275-
)
276-
if clause["action"] not in ["UPDATE", "DELETE", "INSERT"]:
277-
raise exceptions.InvalidArgumentValue(
278-
f"merge_conditional_clauses[{i}]['action'] must be one of ['UPDATE', 'DELETE', 'INSERT']."
279-
)
280-
281-
if clause["when"] in ["NOT MATCHED", "NOT MATCHED BY SOURCE"]:
282-
seen_not_matched = True
283-
elif clause["when"] == "MATCHED" and seen_not_matched:
284-
raise exceptions.InvalidArgumentValue(
285-
f"merge_conditional_clauses[{i}]['when'] is MATCHED but appears after a NOT MATCHED clause. "
286-
"WHEN MATCHED must come before WHEN NOT MATCHED or WHEN NOT MATCHED BY SOURCE."
287-
)
250+
if not merge_conditional_clauses:
251+
raise exceptions.InvalidArgumentCombination(
252+
"merge_conditional_clauses must be provided when merge_condition is 'conditional_merge'."
253+
)
254+
255+
seen_not_matched = False
256+
for i, clause in enumerate(merge_conditional_clauses):
257+
if "when" not in clause:
258+
raise exceptions.InvalidArgumentValue(f"merge_conditional_clauses[{i}] must contain 'when' field.")
259+
if "action" not in clause:
260+
raise exceptions.InvalidArgumentValue(f"merge_conditional_clauses[{i}] must contain 'action' field.")
261+
if clause["when"] not in ["MATCHED", "NOT MATCHED", "NOT MATCHED BY SOURCE"]:
262+
raise exceptions.InvalidArgumentValue(
263+
f"merge_conditional_clauses[{i}]['when'] must be one of ['MATCHED', 'NOT MATCHED', 'NOT MATCHED BY SOURCE']."
264+
)
265+
if clause["action"] not in ["UPDATE", "DELETE", "INSERT"]:
266+
raise exceptions.InvalidArgumentValue(
267+
f"merge_conditional_clauses[{i}]['action'] must be one of ['UPDATE', 'DELETE', 'INSERT']."
268+
)
269+
270+
if clause["when"] in ["NOT MATCHED", "NOT MATCHED BY SOURCE"]:
271+
seen_not_matched = True
272+
elif clause["when"] == "MATCHED" and seen_not_matched:
273+
raise exceptions.InvalidArgumentValue(
274+
f"merge_conditional_clauses[{i}]['when'] is MATCHED but appears after a NOT MATCHED clause. "
275+
"WHEN MATCHED must come before WHEN NOT MATCHED or WHEN NOT MATCHED BY SOURCE."
276+
)
288277

289278
if mode == "overwrite_partitions":
290279
if not partition_cols:
@@ -296,6 +285,7 @@ def _validate_args(
296285
"When mode is 'overwrite_partitions' merge_cols must not be specified."
297286
)
298287

288+
299289
def _merge_iceberg(
300290
df: pd.DataFrame,
301291
database: str,
@@ -387,55 +377,55 @@ def _merge_iceberg(
387377

388378
# Build WHEN clauses based on merge_condition
389379
when_clauses = []
390-
380+
391381
if merge_condition == "update":
392382
when_clauses.append(f"""WHEN MATCHED THEN
393383
UPDATE SET {", ".join([f'"{x}" = source."{x}"' for x in df.columns])}""")
394384
when_clauses.append(f"""WHEN NOT MATCHED THEN
395385
INSERT ({", ".join([f'"{x}"' for x in df.columns])})
396386
VALUES ({", ".join([f'source."{x}"' for x in df.columns])})""")
397-
387+
398388
elif merge_condition == "ignore":
399389
when_clauses.append(f"""WHEN NOT MATCHED THEN
400390
INSERT ({", ".join([f'"{x}"' for x in df.columns])})
401391
VALUES ({", ".join([f'source."{x}"' for x in df.columns])})""")
402-
392+
403393
elif merge_condition == "conditional_merge":
404394
for clause in merge_conditional_clauses:
405395
when_type = clause["when"]
406396
action = clause["action"]
407397
condition = clause.get("condition")
408398
columns = clause.get("columns")
409-
399+
410400
# Build WHEN clause
411401
when_part = f"WHEN {when_type}"
412402
if condition:
413403
when_part += f" AND {condition}"
414-
404+
415405
# Build action
416406
if action == "UPDATE":
417407
update_columns = columns or df.columns.tolist()
418408
update_sets = [f'"{col}" = source."{col}"' for col in update_columns]
419409
when_part += f" THEN UPDATE SET {', '.join(update_sets)}"
420-
410+
421411
elif action == "DELETE":
422412
when_part += " THEN DELETE"
423-
413+
424414
elif action == "INSERT":
425415
insert_columns = columns or df.columns.tolist()
426416
column_list = ", ".join([f'"{col}"' for col in insert_columns])
427417
values_list = ", ".join([f'source."{col}"' for col in insert_columns])
428418
when_part += f" THEN INSERT ({column_list}) VALUES ({values_list})"
429-
419+
430420
when_clauses.append(when_part)
431-
421+
432422
joined_clauses = "\n ".join(when_clauses)
433423
sql_statement = f"""
434424
MERGE INTO "{database}"."{table}" target
435425
USING "{database}"."{source_table}" source
436426
ON {on_condition}
437427
{joined_clauses}
438-
"""
428+
"""
439429
else:
440430
sql_statement = f"""
441431
INSERT INTO "{database}"."{table}" ({", ".join([f'"{x}"' for x in df.columns])})

tests/unit/test_athena_iceberg.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ def test_athena_delete_from_iceberg_empty_df_error(
999999
keep_files=False,
10001000
)
10011001

1002+
10021003
def test_to_iceberg_merge_cols_and_merge_on_clause_error(
10031004
path: str, path2: str, glue_database: str, glue_table: str
10041005
) -> None:
@@ -1014,6 +1015,7 @@ def test_to_iceberg_merge_cols_and_merge_on_clause_error(
10141015
merge_on_clause="id = source.id",
10151016
)
10161017

1018+
10171019
def test_to_iceberg_merge_match_nulls_with_merge_on_clause_error(
10181020
path: str, path2: str, glue_database: str, glue_table: str
10191021
) -> None:
@@ -1029,6 +1031,7 @@ def test_to_iceberg_merge_match_nulls_with_merge_on_clause_error(
10291031
merge_match_nulls=True,
10301032
)
10311033

1034+
10321035
def test_to_iceberg_merge_conditional_clauses_without_conditional_merge_error(
10331036
path: str, path2: str, glue_database: str, glue_table: str
10341037
) -> None:
@@ -1045,6 +1048,7 @@ def test_to_iceberg_merge_conditional_clauses_without_conditional_merge_error(
10451048
merge_condition="update",
10461049
)
10471050

1051+
10481052
def test_to_iceberg_conditional_merge_without_clauses_error(
10491053
path: str, path2: str, glue_database: str, glue_table: str
10501054
) -> None:
@@ -1060,9 +1064,8 @@ def test_to_iceberg_conditional_merge_without_clauses_error(
10601064
merge_condition="conditional_merge",
10611065
)
10621066

1063-
def test_to_iceberg_invalid_merge_condition_error(
1064-
path: str, path2: str, glue_database: str, glue_table: str
1065-
) -> None:
1067+
1068+
def test_to_iceberg_invalid_merge_condition_error(path: str, path2: str, glue_database: str, glue_table: str) -> None:
10661069
df = pd.DataFrame({"id": [1], "val": ["a"]})
10671070
with pytest.raises(wr.exceptions.InvalidArgumentValue):
10681071
wr.athena.to_iceberg(
@@ -1075,9 +1078,8 @@ def test_to_iceberg_invalid_merge_condition_error(
10751078
merge_condition="not_a_valid_condition",
10761079
)
10771080

1078-
def test_to_iceberg_conditional_merge_happy_path(
1079-
path: str, path2: str, glue_database: str, glue_table: str
1080-
) -> None:
1081+
1082+
def test_to_iceberg_conditional_merge_happy_path(path: str, path2: str, glue_database: str, glue_table: str) -> None:
10811083
df = pd.DataFrame({"id": [1, 2], "val": ["a", "b"]})
10821084
wr.athena.to_iceberg(
10831085
df=df,
@@ -1113,6 +1115,7 @@ def test_to_iceberg_conditional_merge_happy_path(
11131115
expected = pd.DataFrame({"id": [1, 2, 3], "val": ["c", "b", "d"]})
11141116
assert_pandas_equals(expected, df_out.reset_index(drop=True))
11151117

1118+
11161119
def test_athena_iceberg_use_partition_function(
11171120
path: str,
11181121
path2: str,

0 commit comments

Comments
 (0)