YannByron commented on code in PR #1393: URL: https://github.com/apache/incubator-paimon/pull/1393#discussion_r1236663873
########## paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/commands/WriteIntoPaimonTable.scala: ########## @@ -0,0 +1,199 @@ +/* + * 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.paimon.spark.commands + +import org.apache.paimon.data.BinaryRow +import org.apache.paimon.index.PartitionIndex +import org.apache.paimon.operation.Lock.Factory +import org.apache.paimon.spark.{SparkRow, SparkTypeUtils} +import org.apache.paimon.table.FileStoreTable +import org.apache.paimon.table.sink.{BatchWriteBuilder, CommitMessageSerializer, DynamicBucketRow, InnerTableCommit, RowPartitionKeyExtractor} +import org.apache.paimon.types.RowType + +import org.apache.spark.sql.{DataFrame, Row, SparkSession} +import org.apache.spark.sql.catalyst.encoders.{ExpressionEncoder, RowEncoder} +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.execution.command.RunnableCommand +import org.apache.spark.sql.functions._ +import org.apache.spark.sql.types.{IntegerType, StructField, StructType} + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +/** Used to write a [[DataFrame]] into a paimon table. */ +case class WriteIntoPaimonTable(table: FileStoreTable, overwrite: Boolean, data: DataFrame) + extends RunnableCommand + with PaimonCommand { + + import WriteIntoPaimonTable._ + + private lazy val tableSchema = table.schema() + + private lazy val rowType = table.rowType() + + private lazy val writeBuilder: BatchWriteBuilder = table.newBatchWriteBuilder() + + private lazy val serializer = new CommitMessageSerializer + + if (overwrite) { + throw new UnsupportedOperationException("Overwrite is unsupported."); + } + + override def run(sparkSession: SparkSession): Seq[Row] = { + import sparkSession.implicits._ + + val dataEncoder = RowEncoder.apply(data.schema).resolveAndBind() + val originFromRow = dataEncoder.createDeserializer() + + val partitionCols = tableSchema.partitionKeys().asScala.map(col) + val bucketCols = tableSchema.bucketKeys().asScala.map(col) + table.schema().logicalBucketKeyType() + val partitioned = if (bucketCols.isEmpty) { + data + } else { + // Make sure that the records with the same bucket values is within a task. + data.repartition(bucketCols: _*) Review Comment: Yep, and the hash computation uses `primaryKeys`. -- 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]
