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

fix: load pk from the downstream instead of Risingwave #8457

Merged
merged 8 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -102,6 +102,8 @@ public static TableSchema fromProto(ConnectorServiceProto.TableSchema tableSchem
.collect(Collectors.toList()));
}

/** @deprecated pk here is from Risingwave, it may not match the pk in the database */
@Deprecated
public List<String> getPrimaryKeys() {
return primaryKeys;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.risingwave.proto.Data;
import io.grpc.Status;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
Expand All @@ -34,6 +36,8 @@ public class JDBCSink extends SinkBase {
private final String tableName;
private final Connection conn;
private final String jdbcUrl;
private final List<String> pkColumnNames;
public static final String JDBC_COLUMN_NAME_KEY = "COLUMN_NAME";

private String updateDeleteConditionBuffer;
private Object[] updateDeleteValueBuffer;
Expand All @@ -48,16 +52,32 @@ public JDBCSink(String tableName, String jdbcUrl, TableSchema tableSchema) {
try {
this.conn = DriverManager.getConnection(jdbcUrl);
this.conn.setAutoCommit(false);
this.pkColumnNames = getPkColumnNames(conn, tableName);
} catch (SQLException e) {
throw Status.INTERNAL.withCause(e).asRuntimeException();
tabVersion marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static List<String> getPkColumnNames(Connection conn, String tableName) {
List<String> pkColumnNames = new ArrayList<>();
try {
var pks = conn.getMetaData().getPrimaryKeys(null, null, tableName);
while (pks.next()) {
pkColumnNames.add(pks.getString(JDBC_COLUMN_NAME_KEY));
}
} catch (SQLException e) {
throw Status.INTERNAL.withCause(e).asRuntimeException();
tabVersion marked this conversation as resolved.
Show resolved Hide resolved
}
LOG.info("detected pk {}", pkColumnNames);
return pkColumnNames;
}

public JDBCSink(Connection conn, TableSchema tableSchema, String tableName) {
super(tableSchema);
this.tableName = tableName;
this.jdbcUrl = null;
this.conn = conn;
this.pkColumnNames = getPkColumnNames(conn, tableName);
}

private PreparedStatement prepareStatement(SinkRow row) {
Expand All @@ -82,16 +102,27 @@ private PreparedStatement prepareStatement(SinkRow row) {
throw io.grpc.Status.INTERNAL.withCause(e).asRuntimeException();
}
case DELETE:
String deleteCondition =
getTableSchema().getPrimaryKeys().stream()
.map(key -> key + " = ?")
.collect(Collectors.joining(" AND "));
String deleteCondition;
if (this.pkColumnNames.isEmpty()) {
deleteCondition =
IntStream.range(0, getTableSchema().getNumColumns())
.mapToObj(
index ->
getTableSchema().getColumnNames()[index]
+ " = ?")
.collect(Collectors.joining(" AND "));
} else {
deleteCondition =
this.pkColumnNames.stream()
.map(key -> key + " = ?")
.collect(Collectors.joining(" AND "));
}
String deleteStmt = String.format(DELETE_TEMPLATE, tableName, deleteCondition);
try {
int placeholderIdx = 1;
PreparedStatement stmt =
conn.prepareStatement(deleteStmt, Statement.RETURN_GENERATED_KEYS);
for (String primaryKey : getTableSchema().getPrimaryKeys()) {
for (String primaryKey : this.pkColumnNames) {
Object fromRow = getTableSchema().getFromRow(primaryKey, row);
stmt.setObject(placeholderIdx++, fromRow);
}
Expand All @@ -100,14 +131,35 @@ private PreparedStatement prepareStatement(SinkRow row) {
throw Status.INTERNAL.withCause(e).asRuntimeException();
}
case UPDATE_DELETE:
updateDeleteConditionBuffer =
getTableSchema().getPrimaryKeys().stream()
.map(key -> key + " = ?")
.collect(Collectors.joining(" AND "));
updateDeleteValueBuffer =
getTableSchema().getPrimaryKeys().stream()
.map(key -> getTableSchema().getFromRow(key, row))
.toArray();
if (this.pkColumnNames.isEmpty()) {
updateDeleteConditionBuffer =
IntStream.range(0, getTableSchema().getNumColumns())
.mapToObj(
index ->
getTableSchema().getColumnNames()[index]
+ " = ?")
.collect(Collectors.joining(" AND "));
updateDeleteValueBuffer =
IntStream.range(0, getTableSchema().getNumColumns())
.mapToObj(
index ->
getTableSchema()
.getFromRow(
getTableSchema()
.getColumnNames()[
index],
row))
.toArray();
} else {
updateDeleteConditionBuffer =
this.pkColumnNames.stream()
.map(key -> key + " = ?")
.collect(Collectors.joining(" AND "));
updateDeleteValueBuffer =
this.pkColumnNames.stream()
.map(key -> getTableSchema().getFromRow(key, row))
.toArray();
}
LOG.debug(
"update delete condition: {} on values {}",
updateDeleteConditionBuffer,
Expand Down