suxiaogang223 commented on code in PR #65381:
URL: https://github.com/apache/doris/pull/65381#discussion_r3611908834
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java:
##########
@@ -565,6 +567,19 @@ ExecutorFactory selectInsertExecutorFactory(
emptyInsert, jobId
)
);
+ } else if (physicalSink instanceof PhysicalPaimonTableSink) {
+ boolean emptyInsert = childIsEmptyRelation(physicalSink);
Review Comment:
Fixed in bc98f94c3e8. The empty-input fast path is now used only for
non-overwrite inserts, so an empty Paimon overwrite still opens the transaction
and publishes the empty result. Added a direct `LIMIT 0` overwrite regression
case.
##########
fe/fe-core/src/main/java/org/apache/doris/datasource/paimon/PaimonTransaction.java:
##########
@@ -0,0 +1,327 @@
+// 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.doris.datasource.paimon;
+
+import org.apache.doris.common.UserException;
+import org.apache.doris.common.security.authentication.ExecutionAuthenticator;
+import org.apache.doris.datasource.mvcc.MvccUtil;
+import
org.apache.doris.nereids.trees.plans.commands.insert.InsertCommandContext;
+import
org.apache.doris.nereids.trees.plans.commands.insert.PaimonInsertCommandContext;
+import org.apache.doris.thrift.TPaimonCommitMessage;
+import org.apache.doris.transaction.Transaction;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.paimon.io.DataInputDeserializer;
+import org.apache.paimon.table.InnerTable;
+import org.apache.paimon.table.sink.CommitMessage;
+import org.apache.paimon.table.sink.CommitMessageSerializer;
+import org.apache.paimon.table.sink.InnerTableCommit;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * Paimon transaction.
+ *
+ * Lifecycle:
+ * 1. beginInsert() — record target table
+ * 2. updateCommitMessages() — called multiple times as BE reports commit
data
+ * 3. commit() — deserialize all CommitMessages, call
StreamTableCommit.filterAndCommit()
+ * 4. rollback() — abort uncommitted data files
+ *
+ * CommitMessage wire format:
+ * BE ← JNI ← Java: byte[] with DPCM header (magic + version + length) +
Paimon
+ * CommitMessageSerializer payload
+ */
+public class PaimonTransaction implements Transaction {
+ private static final Logger LOG =
LogManager.getLogger(PaimonTransaction.class);
+
+ private static final int COMMIT_HEADER_SIZE = 12;
+ private static final byte[] COMMIT_MAGIC = new byte[] {'D', 'P', 'C', 'M'};
+
+ private final PaimonMetadataOps ops;
+ private final long transactionId;
+ private final String commitUser;
+ private PaimonExternalTable table;
+ private boolean committed = false;
+ private boolean overwrite = false;
+ private Map<String, String> staticPartition = new HashMap<>();
+
+ private final List<byte[]> commitPayloads = Lists.newArrayList();
+ private final Set<String> commitPayloadSet = new HashSet<>();
+
+ public PaimonTransaction(PaimonMetadataOps ops, long transactionId) {
+ Preconditions.checkArgument(transactionId > 0, "Paimon transaction id
must be positive");
+ this.ops = ops;
+ this.transactionId = transactionId;
+ this.commitUser = commitUser(transactionId);
+ }
+
+ // ────────────────────────────────────────────────────────────
+ // Transaction lifecycle
+ // ────────────────────────────────────────────────────────────
+
+ public void beginInsert(PaimonExternalTable table,
Optional<InsertCommandContext> insertCtx) {
+ this.table = table;
+ PaimonInsertCommandContext context = (PaimonInsertCommandContext)
insertCtx.orElseThrow(
+ () -> new IllegalStateException("Missing Paimon insert command
context"));
+ this.overwrite = context.isOverwrite();
+ this.staticPartition = new HashMap<>(context.getStaticPartition());
+ }
+
+ public void finishInsert(PaimonExternalTable table,
Optional<InsertCommandContext> insertCtx) {
+ }
+
+ @Override
+ public void commit() throws UserException {
+ List<byte[]> rawPayloads = snapshotPayloads();
+ if (rawPayloads.isEmpty() && !overwrite) {
+ LOG.info("Skip empty PaimonTransaction commit, txnId={}, table={}",
+ transactionId, tableName());
+ return;
+ }
+ if (table == null) {
+ throw new UserException("Missing paimon table for transaction");
+ }
+ try {
+ List<CommitMessage> allMessages = deserializePayloads(rawPayloads);
+ LOG.info("Commit PaimonTransaction, txnId={}, table={},
payloads={}, messages={}, overwrite={}",
+ transactionId, tableName(), rawPayloads.size(),
allMessages.size(), overwrite);
+ if (allMessages.isEmpty() && !overwrite) {
+ throw new RuntimeException(
+ "Paimon commit messages are empty, payloads=" +
rawPayloads.size());
+ }
+ doCommit(allMessages);
+ synchronized (this) {
+ committed = true;
+ }
+ } catch (Exception e) {
+ throw new UserException("Failed to commit paimon transaction on
FE", e);
+ }
+ }
+
+ @Override
+ public void rollback() {
+ if (isCommitted()) {
+ LOG.info("Skip rollback for committed PaimonTransaction, txnId={},
table={}",
+ transactionId, tableName());
+ return;
+ }
+ List<byte[]> rawPayloads = snapshotPayloads();
+ if (rawPayloads.isEmpty()) {
+ LOG.info("Skip empty PaimonTransaction rollback, txnId={},
table={}",
+ transactionId, tableName());
+ return;
+ }
+ if (table == null) {
+ LOG.warn("Skip PaimonTransaction rollback, table missing,
txnId={}", transactionId);
+ return;
+ }
+ try {
+ List<CommitMessage> allMessages = deserializePayloads(rawPayloads);
+ if (allMessages.isEmpty()) {
+ LOG.info("Skip PaimonTransaction rollback with empty decoded
messages, "
+ + "txnId={}, table={}", transactionId, tableName());
+ return;
+ }
+ LOG.info("Rollback PaimonTransaction, txnId={}, table={},
payloads={}, messages={}",
+ transactionId, tableName(), rawPayloads.size(),
allMessages.size());
+ doAbort(allMessages);
Review Comment:
Fixed in bc98f94c3e8. The transaction retries the same idempotent
`filterAndCommit` once; if the result remains outcome-unknown, it records that
state and prevents the failure path from calling rollback/abort. This
conservatively preserves files that may already be referenced by a published
snapshot.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalPaimonTableSink.java:
##########
@@ -0,0 +1,111 @@
+// 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.doris.nereids.trees.plans.physical;
+
+import org.apache.doris.catalog.Column;
+import org.apache.doris.datasource.paimon.PaimonExternalDatabase;
+import org.apache.doris.datasource.paimon.PaimonExternalTable;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.properties.LogicalProperties;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.AbstractPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.statistics.Statistics;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Physical Paimon table sink.
+ */
+public class PhysicalPaimonTableSink<CHILD_TYPE extends Plan>
+ extends PhysicalBaseExternalTableSink<CHILD_TYPE> {
+
+ public PhysicalPaimonTableSink(PaimonExternalDatabase database,
+ PaimonExternalTable targetTable,
+ List<Column> cols,
+ List<NamedExpression> outputExprs,
+ Optional<GroupExpression> groupExpression,
+ LogicalProperties logicalProperties,
+ CHILD_TYPE child) {
+ this(database, targetTable, cols, outputExprs, groupExpression,
logicalProperties,
+ PhysicalProperties.SINK_RANDOM_PARTITIONED, null, child);
+ }
+
+ public PhysicalPaimonTableSink(PaimonExternalDatabase database,
+ PaimonExternalTable targetTable,
+ List<Column> cols,
+ List<NamedExpression> outputExprs,
+ Optional<GroupExpression> groupExpression,
+ LogicalProperties logicalProperties,
+ PhysicalProperties physicalProperties,
+ Statistics statistics,
+ CHILD_TYPE child) {
+ super(PlanType.PHYSICAL_PAIMON_TABLE_SINK, database, targetTable,
cols, outputExprs,
+ groupExpression, logicalProperties, physicalProperties,
statistics, child);
+ }
+
+ @Override
+ public Plan withChildren(List<Plan> children) {
+ return AbstractPlan.copyWithSameId(this, () -> new
PhysicalPaimonTableSink<>(
+ (PaimonExternalDatabase) database, (PaimonExternalTable)
targetTable,
+ cols, outputExprs, groupExpression,
+ getLogicalProperties(), physicalProperties, statistics,
children.get(0)));
+ }
+
+ @Override
+ public Plan withGroupExpression(Optional<GroupExpression> groupExpression)
{
+ return AbstractPlan.copyWithSameId(this, () -> new
PhysicalPaimonTableSink<>(
+ (PaimonExternalDatabase) database, (PaimonExternalTable)
targetTable,
+ cols, outputExprs, groupExpression,
+ getLogicalProperties(), physicalProperties, statistics,
child()));
+ }
+
+ @Override
+ public Plan withGroupExprLogicalPropChildren(Optional<GroupExpression>
groupExpression,
+ Optional<LogicalProperties> logicalProperties, List<Plan>
children) {
+ return AbstractPlan.copyWithSameId(this, () -> new
PhysicalPaimonTableSink<>(
+ (PaimonExternalDatabase) database, (PaimonExternalTable)
targetTable,
+ cols, outputExprs, groupExpression,
+ logicalProperties.get(), physicalProperties, statistics,
children.get(0)));
+ }
+
+ @Override
+ public PhysicalPaimonTableSink<Plan> withPhysicalPropertiesAndStats(
+ PhysicalProperties physicalProperties, Statistics stats) {
+ return AbstractPlan.copyWithSameId(this, () -> new
PhysicalPaimonTableSink<>(
+ (PaimonExternalDatabase) database, (PaimonExternalTable)
targetTable,
+ cols, outputExprs, groupExpression,
+ getLogicalProperties(), physicalProperties, stats, child()));
+ }
+
+ @Override
+ public PhysicalProperties getRequirePhysicalProperties() {
+ // Partition and bucket routing is handled by the Paimon SDK
internally.
+ // Doris does not need to shuffle data by partition/bucket keys.
+ return PhysicalProperties.SINK_RANDOM_PARTITIONED;
Review Comment:
Fixed in bc98f94c3e8. `PhysicalPaimonTableSink` now reads authoritative
primary keys from the Paimon SDK and hash-distributes primary-key tables by all
PK slots; append-only tables retain random distribution. The regression crosses
the writer-scaling threshold and verifies both `DistributionSpecHash` and the
deterministic result.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertOverwriteTableCommand.java:
##########
@@ -467,6 +482,23 @@ private void
setStaticPartitionToContext(UnboundIcebergTableSink<?> sink,
}
}
+ private void setStaticPartitionToContext(UnboundPaimonTableSink<?> sink,
+ PaimonInsertCommandContext insertCtx) {
+ if (sink.hasStaticPartition()) {
+ Map<String, String> staticPartitionValues = Maps.newHashMap();
+ for (Map.Entry<String, Expression> entry
+ : sink.getStaticPartitionKeyValues().entrySet()) {
+ Expression expr = entry.getValue();
+ if (!(expr instanceof Literal)) {
+ throw new AnalysisException(
+ String.format("Static partition value must be a
literal, but got: %s", expr));
+ }
+ staticPartitionValues.put(entry.getKey(), ((Literal)
expr).getStringValue());
Review Comment:
Fixed in bc98f94c3e8. Static partition keys are canonicalized to the actual
target `Column.getName()`, and the earlier VALUES arity normalization now
filters static partition columns case-insensitively as well. Added a `Region` /
`region` regression case.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]