alexandermalyga commented on code in PR #26661:
URL: https://github.com/apache/airflow/pull/26661#discussion_r980346489
##########
airflow/providers/postgres/hooks/postgres.py:
##########
@@ -270,10 +270,14 @@ def _generate_insert_sql(
raise ValueError("PostgreSQL ON CONFLICT upsert syntax
requires an unique index")
if isinstance(replace_index, str):
replace_index = [replace_index]
- replace_index_set = set(replace_index)
- replace_target = [
- "{0} = excluded.{0}".format(col) for col in target_fields if
col not in replace_index_set
- ]
- sql += f" ON CONFLICT ({', '.join(replace_index)}) DO UPDATE SET
{', '.join(replace_target)}"
+ on_conflict_str = f" ON CONFLICT ({', '.join(replace_index)})"
+ replace_target = [f for f in target_fields if f not in
replace_index]
+
+ if replace_target:
+ replace_target_str = ", ".join(f"{col} = excluded.{col}" for
col in replace_target)
+ sql += f"{on_conflict_str} DO UPDATE SET {replace_target_str}"
+ else:
+ sql += f"{on_conflict_str} DO NOTHING"
Review Comment:
All possible combinations are, assuming `replace=True`:
1. `target_fields=(), replace_index=()`: any of these being empty or None
should raise an exception
2. `target_fields=('a', 'b'), replace_index=('a')`: should trigger `ON
CONFLICT DO UPDATE SET b = excluded.b`
3. `target_fields=('a', 'b'), replace_index=('a', 'b')`: should trigger `ON
CONFLICT DO NOTHING`
4. `target_fields=('a'), replace_index=('a', 'b')`: will raise an SQL
exception because a mandatory field is not provided, or not conflict because
the value was auto-generated by the DB
All of these behaviours are currently implemented, besides the check for an
empty list which I'll implement and push now.
Can't think of any other situation, please correct me if I'm wrong.
--
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]