You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by fa...@apache.org on 2025/02/26 02:35:30 UTC

(seatunnel) branch dev updated: [Improve][Jdbc] Support upsert for opengauss (#8627)

This is an automated email from the ASF dual-hosted git repository.

fanjia pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 56110bf392 [Improve][Jdbc] Support upsert for opengauss (#8627)
56110bf392 is described below

commit 56110bf392f1bcab85d441af993c3ac3f6e40222
Author: hailin0 <wa...@apache.org>
AuthorDate: Wed Feb 26 10:35:24 2025 +0800

    [Improve][Jdbc] Support upsert for opengauss (#8627)
---
 .../dialect/opengauss/OpenGaussDialect.java        | 49 ++++++++++++++
 .../dialect/opengauss/OpenGaussDialectFactory.java | 13 ++++
 .../dialect/opengauss/OpenGaussDialectTest.java    | 75 ++++++++++++++++++++++
 3 files changed, 137 insertions(+)

diff --git a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialect.java b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialect.java
new file mode 100644
index 0000000000..2506716d5c
--- /dev/null
+++ b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialect.java
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.opengauss;
+
+import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.psql.PostgresDialect;
+
+import java.util.Arrays;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class OpenGaussDialect extends PostgresDialect {
+
+    @Override
+    public Optional<String> getUpsertStatement(
+            String database, String tableName, String[] fieldNames, String[] uniqueKeyFields) {
+        String updateClause =
+                Arrays.stream(fieldNames)
+                        .filter(fieldName -> !Arrays.asList(uniqueKeyFields).contains(fieldName))
+                        .map(
+                                fieldName ->
+                                        quoteIdentifier(fieldName)
+                                                + "=EXCLUDED."
+                                                + quoteIdentifier(fieldName))
+                        .collect(Collectors.joining(", "));
+        if (updateClause.isEmpty()) {
+            return Optional.empty();
+        }
+        String upsertSQL =
+                String.format(
+                        "%s ON DUPLICATE KEY UPDATE %s",
+                        getInsertIntoStatement(database, tableName, fieldNames), updateClause);
+        return Optional.of(upsertSQL);
+    }
+}
diff --git a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectFactory.java b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectFactory.java
index b1ceed51e9..899e2a4768 100644
--- a/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectFactory.java
+++ b/seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectFactory.java
@@ -16,11 +16,14 @@
  */
 package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.opengauss;
 
+import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect;
 import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectFactory;
 import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.psql.PostgresDialectFactory;
 
 import com.google.auto.service.AutoService;
 
+import javax.annotation.Nonnull;
+
 @AutoService(JdbcDialectFactory.class)
 public class OpenGaussDialectFactory extends PostgresDialectFactory {
 
@@ -28,4 +31,14 @@ public class OpenGaussDialectFactory extends PostgresDialectFactory {
     public boolean acceptsURL(String url) {
         return url.startsWith("jdbc:opengauss:");
     }
+
+    @Override
+    public JdbcDialect create() {
+        return new OpenGaussDialect();
+    }
+
+    @Override
+    public JdbcDialect create(@Nonnull String compatibleMode, String fieldIde) {
+        return new OpenGaussDialect();
+    }
 }
diff --git a/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectTest.java b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectTest.java
new file mode 100644
index 0000000000..23a7526660
--- /dev/null
+++ b/seatunnel-connectors-v2/connector-jdbc/src/test/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/dialect/opengauss/OpenGaussDialectTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.opengauss;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class OpenGaussDialectTest {
+
+    @Test
+    void returnsUpsertStatementWhenUpdateClauseIsNotEmpty() {
+        OpenGaussDialect dialect = new OpenGaussDialect();
+        String[] fieldNames = {"id", "name", "age"};
+        String[] uniqueKeyFields = {"id"};
+        Optional<String> upsertStatement =
+                dialect.getUpsertStatement("test_db", "test_table", fieldNames, uniqueKeyFields);
+        assertTrue(upsertStatement.isPresent());
+        assertEquals(
+                "INSERT INTO \"test_db\".\"test_table\" (\"id\", \"name\", \"age\") VALUES (:id, :name, :age) ON DUPLICATE KEY UPDATE \"name\"=EXCLUDED.\"name\", \"age\"=EXCLUDED.\"age\"",
+                upsertStatement.get());
+    }
+
+    @Test
+    void returnsEmptyWhenUpdateClauseIsEmpty() {
+        OpenGaussDialect dialect = new OpenGaussDialect();
+        String[] fieldNames = {"id"};
+        String[] uniqueKeyFields = {"id"};
+        Optional<String> upsertStatement =
+                dialect.getUpsertStatement("test_db", "test_table", fieldNames, uniqueKeyFields);
+        assertFalse(upsertStatement.isPresent());
+    }
+
+    @Test
+    void handlesEmptyFieldNames() {
+        OpenGaussDialect dialect = new OpenGaussDialect();
+        String[] fieldNames = {};
+        String[] uniqueKeyFields = {"id"};
+        Optional<String> upsertStatement =
+                dialect.getUpsertStatement("test_db", "test_table", fieldNames, uniqueKeyFields);
+        assertFalse(upsertStatement.isPresent());
+    }
+
+    @Test
+    void handlesEmptyUniqueKeyFields() {
+        OpenGaussDialect dialect = new OpenGaussDialect();
+        String[] fieldNames = {"id", "name", "age"};
+        String[] uniqueKeyFields = {};
+        Optional<String> upsertStatement =
+                dialect.getUpsertStatement("test_db", "test_table", fieldNames, uniqueKeyFields);
+        assertTrue(upsertStatement.isPresent());
+        assertEquals(
+                "INSERT INTO \"test_db\".\"test_table\" (\"id\", \"name\", \"age\") VALUES (:id, :name, :age) ON DUPLICATE KEY UPDATE \"id\"=EXCLUDED.\"id\", \"name\"=EXCLUDED.\"name\", \"age\"=EXCLUDED.\"age\"",
+                upsertStatement.get());
+    }
+}