github-actions[bot] commented on code in PR #41638: URL: https://github.com/apache/beam/pull/41638#discussion_r4684122571
########## sdks/python/apache_beam/ml/rag/ingestion/postgres_common_test.py: ########## @@ -0,0 +1,38 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from apache_beam.ml.rag.ingestion.postgres_common import ConflictResolution + + +class ConflictResolutionTest(unittest.TestCase): + def test_update_with_empty_conflict_fields(self): Review Comment: **🟠High**: Following the PostgreSQL syntax requirement for `DO UPDATE`, this test should verify that a `ValueError` is raised when `action="UPDATE"` is used with empty `on_conflict_fields`, and we should also include unit tests for non-empty `on_conflict_fields` (both `str` and `list[str]`). ########## sdks/python/apache_beam/ml/rag/ingestion/postgres_common.py: ########## @@ -503,18 +503,20 @@ def maybe_set_default_update_fields(self, columns: list[str]): def get_conflict_clause(self) -> str: """Get conflict clause with update fields.""" - conflict_fields = [self.on_conflict_fields] \ - if isinstance(self.on_conflict_fields, str) \ - else self.on_conflict_fields + conflict_fields = ( + [self.on_conflict_fields] + if isinstance(self.on_conflict_fields, str) + else self.on_conflict_fields) + target = ( + f" ({', '.join(conflict_fields)})" if conflict_fields else "") if self.action == "IGNORE": - conflict_fields_string = f"({', '.join(conflict_fields)})" \ - if len(conflict_fields) > 0 else "" - return f"ON CONFLICT {conflict_fields_string} DO NOTHING" + return f"ON CONFLICT{target} DO NOTHING" # update_fields should be set by query builder before this is called - assert self.update_fields is not None, \ - "update_fields must be set before generating conflict clause" - updates = [f"{field} = EXCLUDED.{field}" for field in self.update_fields] - return f"ON CONFLICT " \ - f"({', '.join(conflict_fields)}) DO UPDATE SET {', '.join(updates)}" + assert self.update_fields is not None, ( + "update_fields must be set before generating conflict clause") + updates = ", ".join( + f"{field} = EXCLUDED.{field}" for field in self.update_fields) + return f"ON CONFLICT{target} DO UPDATE SET {updates}" Review Comment: **🔴 Critical**: In PostgreSQL, `ON CONFLICT ... DO UPDATE` **requires** a conflict target (either `({conflict_fields})` or `ON CONSTRAINT constraint_name`); unlike `DO NOTHING`, omitting the conflict target (`ON CONFLICT DO UPDATE SET ...`) is a PostgreSQL syntax error (`ERROR: ON CONFLICT DO UPDATE requires inference specification or constraint name`). Also note that with `on_conflict_fields=[]`, `maybe_set_default_update_fields()` sets `update_fields` to all columns (including the primary key), which is rarely intended. Instead of emitting `ON CONFLICT DO UPDATE SET ...` when `conflict_fields` is empty, `ConflictResolution` should raise a `ValueError` (either in `__post_init__` or here in `get_conflict_clause`) when `self.action == "UPDATE"` and `not conflict_fields`. ########## sdks/python/apache_beam/ml/rag/ingestion/postgres_common.py: ########## @@ -503,18 +503,20 @@ def maybe_set_default_update_fields(self, columns: list[str]): def get_conflict_clause(self) -> str: """Get conflict clause with update fields.""" - conflict_fields = [self.on_conflict_fields] \ - if isinstance(self.on_conflict_fields, str) \ - else self.on_conflict_fields + conflict_fields = ( + [self.on_conflict_fields] + if isinstance(self.on_conflict_fields, str) + else self.on_conflict_fields) + target = ( + f" ({', '.join(conflict_fields)})" if conflict_fields else "") if self.action == "IGNORE": - conflict_fields_string = f"({', '.join(conflict_fields)})" \ - if len(conflict_fields) > 0 else "" - return f"ON CONFLICT {conflict_fields_string} DO NOTHING" + return f"ON CONFLICT{target} DO NOTHING" # update_fields should be set by query builder before this is called - assert self.update_fields is not None, \ - "update_fields must be set before generating conflict clause" - updates = [f"{field} = EXCLUDED.{field}" for field in self.update_fields] - return f"ON CONFLICT " \ - f"({', '.join(conflict_fields)}) DO UPDATE SET {', '.join(updates)}" + assert self.update_fields is not None, ( + "update_fields must be set before generating conflict clause") + updates = ", ".join( + f"{field} = EXCLUDED.{field}" for field in self.update_fields) + return f"ON CONFLICT{target} DO UPDATE SET {updates}" + Review Comment: **🟢 Nit**: Remove the trailing whitespace on this empty line at the end of the file to satisfy lint/formatting checks. -- 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]
