Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotfix][Connector/JDBC] Remove unnecessary unique fields of PostgreSQL upsert statement #155

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -73,17 +74,22 @@ public Optional<String> getUpsertStatement(
Arrays.stream(uniqueKeyFields)
.map(this::quoteIdentifier)
.collect(Collectors.joining(", "));
final Set<String> uniqueKeyFieldsSet = new HashSet<>(Arrays.asList(uniqueKeyFields));
String updateClause =
Arrays.stream(fieldNames)
.filter(f -> !uniqueKeyFieldsSet.contains(f))
roseduan marked this conversation as resolved.
Show resolved Hide resolved
.map(f -> quoteIdentifier(f) + "=EXCLUDED." + quoteIdentifier(f))
.collect(Collectors.joining(", "));
String conflictAction =
updateClause.isEmpty()
? " DO NOTHING"
: String.format(" DO UPDATE SET %s", updateClause);
roseduan marked this conversation as resolved.
Show resolved Hide resolved
return Optional.of(
getInsertIntoStatement(tableName, fieldNames)
+ " ON CONFLICT ("
+ uniqueColumns
+ ")"
+ " DO UPDATE SET "
+ updateClause);
+ conflictAction);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import org.apache.flink.connector.jdbc.core.database.dialect.JdbcDialectTest;
import org.apache.flink.connector.jdbc.postgres.PostgresTestBase;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/** The PostgresSql params for {@link JdbcDialectTest}. */
class PostgresDialectTest extends JdbcDialectTest implements PostgresTestBase {

Expand Down Expand Up @@ -58,4 +62,24 @@ protected List<TestItem> testData() {
"The precision of field 'f0' is out of the TIMESTAMP precision range [1, 6] supported by PostgreSQL dialect."),
createTestItem("TIMESTAMP_LTZ(3)", "Unsupported type:TIMESTAMP_LTZ(3)"));
}

@Test
void testUpsertStatement() {
PostgresDialect dialect = new PostgresDialect();
final String tableName = "tbl";
final String[] fieldNames = {
"id", "name", "email", "ts", "field1", "field_2", "__field_3__"
};
final String[] doUpdatekeyFields = {"id", "__field_3__"};
final String[] doNothingkeyFields = {
"id", "name", "email", "ts", "field1", "field_2", "__field_3__"
};

assertThat(dialect.getUpsertStatement(tableName, fieldNames, doUpdatekeyFields).get())
.isEqualTo(
"INSERT INTO tbl(id, name, email, ts, field1, field_2, __field_3__) VALUES (:id, :name, :email, :ts, :field1, :field_2, :__field_3__) ON CONFLICT (id, __field_3__) DO UPDATE SET name=EXCLUDED.name, email=EXCLUDED.email, ts=EXCLUDED.ts, field1=EXCLUDED.field1, field_2=EXCLUDED.field_2");
assertThat(dialect.getUpsertStatement(tableName, fieldNames, doNothingkeyFields).get())
.isEqualTo(
"INSERT INTO tbl(id, name, email, ts, field1, field_2, __field_3__) VALUES (:id, :name, :email, :ts, :field1, :field_2, :__field_3__) ON CONFLICT (id, name, email, ts, field1, field_2, __field_3__) DO NOTHING");
}
}
Loading