Sachin-nsk commented on code in PR #13480: URL: https://github.com/apache/seatunnel/pull/13480#discussion_r4716500905
########## seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/executor/OrderedBufferedBatchStatementExecutor.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.executor; + +import org.apache.seatunnel.api.table.type.RowKind; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; +import org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException; + +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +/** + * Applies every change of a batch in arrival order, used by {@code update_condition_sql}. Unlike + * {@link BufferReducedBatchStatementExecutor}, a key is not reduced to its last change. + * + * <p>The pending upsert batch is executed before a key repeats, because PostgreSQL fails a + * statement that changes the same row twice, such as a batch rewritten by the driver. + */ +@RequiredArgsConstructor +public class OrderedBufferedBatchStatementExecutor + implements JdbcBatchStatementExecutor<SeaTunnelRow> { + @NonNull private final JdbcBatchStatementExecutor<SeaTunnelRow> upsertExecutor; + @NonNull private final JdbcBatchStatementExecutor<SeaTunnelRow> deleteExecutor; + @NonNull private final Function<SeaTunnelRow, SeaTunnelRow> keyExtractor; + @NonNull private final Function<SeaTunnelRow, SeaTunnelRow> valueTransform; + + /** Kept until a flush succeeds, so a retry replays every change. */ + @NonNull private final List<SeaTunnelRow> buffer = new ArrayList<>(); + + private boolean flushFailed; + + @Override + public void prepareStatements(Connection connection) throws SQLException { + upsertExecutor.prepareStatements(connection); + deleteExecutor.prepareStatements(connection); + } + + @Override + public void addToBatch(SeaTunnelRow record) throws SQLException { + if (RowKind.UPDATE_BEFORE.equals(record.getRowKind())) { + return; + } + // Fail on an unsupported row kind when it arrives, not when the batch is flushed. + isUpsert(record.getRowKind()); + buffer.add(record); + } + + @Override + public void executeBatch() throws SQLException { + try { + // Drop statements left by a failed attempt, as the whole buffer is replayed. + upsertExecutor.clearBatch(); + deleteExecutor.clearBatch(); + + Set<SeaTunnelRow> keysInUpsertBatch = new HashSet<>(); + Boolean pendingUpsert = null; + for (SeaTunnelRow record : buffer) { + boolean upsert = isUpsert(record.getRowKind()); + if (pendingUpsert != null && pendingUpsert != upsert) { + executePending(pendingUpsert); + keysInUpsertBatch.clear(); + } + SeaTunnelRow key = keyExtractor.apply(record); + if (upsert) { + // A key with a NULL field never conflicts, so it cannot repeat a row. + if (!hasNullField(key) && !keysInUpsertBatch.add(key)) { Review Comment: Fixed in 235413398: with reWriteBatchedInserts=true each row is now sent as its own statement. -- 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]
