rdblue commented on a change in pull request #3763: URL: https://github.com/apache/iceberg/pull/3763#discussion_r773441836
########## File path: spark/v3.2/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/WriteDelta.scala ########## @@ -0,0 +1,98 @@ +/* + * 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.spark.sql.catalyst.plans.logical + +import org.apache.spark.sql.catalyst.analysis.NamedRelation +import org.apache.spark.sql.catalyst.expressions.NamedExpression +import org.apache.spark.sql.catalyst.util.CharVarcharUtils +import org.apache.spark.sql.catalyst.util.RowDeltaUtils.OPERATION_COLUMN +import org.apache.spark.sql.catalyst.util.WriteDeltaProjections +import org.apache.spark.sql.connector.iceberg.write.DeltaWrite +import org.apache.spark.sql.types.DataType +import org.apache.spark.sql.types.IntegerType +import org.apache.spark.sql.types.StructField + +/** + * Writes a delta of rows to an existing table. + */ +case class WriteDelta( + table: NamedRelation, + query: LogicalPlan, + originalTable: NamedRelation, + projections: WriteDeltaProjections, + write: Option[DeltaWrite] = None) extends V2WriteCommandLike { + + override protected lazy val stringArgs: Iterator[Any] = Iterator(table, query, write) + + private def operationResolved: Boolean = { + val attr = query.output.head + attr.name == OPERATION_COLUMN && attr.dataType == IntegerType && !attr.nullable + } + + private def rowAttrsResolved: Boolean = { + table.skipSchemaResolution || (projections.rowProjection match { + case Some(projection) => + table.output.size == projection.schema.size && + projection.schema.zip(table.output).forall { case (field, outAttr) => + isCompatible(field, outAttr) + } + case None => true + }) + } + + private def rowIdAttrsResolved: Boolean = { + projections.rowIdProjection.schema.forall { field => + originalTable.resolve(Seq(field.name), conf.resolver) match { Review comment: The incoming fields are probably fine because they're coming from query via `rowIdProjection`. For the output fields, I think it makes sense to go back to what the table requested. Since the output relation, `table` is probably a `V2Relation` that is wrapping the `RowLevelOperationTable`, we should actually be able to recover the requested fields without using `originalTable. I think that makes the most sense: we want to validate that the incoming fields (`query` or `rowIdProjection`) satisfy the requirements from the operation. The original table doesn't really need to be used. -- 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]
