Sachin-nsk commented on code in PR #13480:
URL: https://github.com/apache/seatunnel/pull/13480#discussion_r4716490920


##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/psql/PostgresDialect.java:
##########
@@ -208,6 +211,58 @@ public Optional<String> getUpsertStatement(
         return Optional.of(upsertSQL);
     }
 
+    /**
+     * Builds the upsert statement for {@code update_condition_sql}. The 
condition is added to the
+     * update branch only, so a new key is always inserted. {@code EXCLUDED} 
is the incoming row and
+     * {@value #UPDATE_CONDITION_TARGET_ALIAS} the stored one.
+     */
+    public String getConditionalUpsertStatement(
+            String database,
+            String tableName,
+            String[] fieldNames,
+            String[] pkNames,
+            UpdateCondition condition) {
+        String columns =
+                Arrays.stream(fieldNames)
+                        .map(this::quoteIdentifier)
+                        .collect(Collectors.joining(", "));
+        String placeholders =
+                Arrays.stream(fieldNames)
+                        .map(fieldName -> ":" + fieldName)
+                        .collect(Collectors.joining(", "));
+        String uniqueColumns =
+                
Arrays.stream(pkNames).map(this::quoteIdentifier).collect(Collectors.joining(", 
"));
+        final Set<String> pkNamesSet = new HashSet<>(Arrays.asList(pkNames));
+        String updateClause =
+                Arrays.stream(fieldNames)
+                        .filter(fieldName -> !pkNamesSet.contains(fieldName))
+                        .map(
+                                fieldName ->
+                                        quoteIdentifier(fieldName)
+                                                + "=EXCLUDED."
+                                                + quoteIdentifier(fieldName))
+                        .collect(Collectors.joining(", "));
+        // When every written column is a key column there is nothing to 
update.
+        String conflictAction =
+                updateClause.isEmpty()
+                        ? "DO NOTHING"
+                        : String.format(
+                                "DO UPDATE SET %s WHERE (%s)",
+                                updateClause,
+                                condition.toSql(
+                                        this::quoteIdentifier,

Review Comment:
   I reproduced this on PostgreSQL 15.1. Column names containing a dot (`a.b`) 
or a double quote (`say"hi`) fail, but they fail the same way without 
`update_condition_sql`, because it comes from the existing 
`PostgresDialect#quoteIdentifier`.



-- 
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]

Reply via email to