dilipbiswal commented on a change in pull request #2193: URL: https://github.com/apache/iceberg/pull/2193#discussion_r568374762
########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/AlignUpdateTable.scala ########## @@ -0,0 +1,32 @@ +/* + * 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.analysis + +import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, UpdateTable} Review comment: import separate lines ? ########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala ########## @@ -35,6 +35,10 @@ object OptimizeConditionsInRowLevelOperations extends Rule[LogicalPlan] { if !SubqueryExpression.hasSubquery(cond.getOrElse(Literal.TrueLiteral)) && isIcebergRelation(table) => val optimizedCond = optimizeCondition(cond.getOrElse(Literal.TrueLiteral), table) d.copy(condition = Some(optimizedCond)) + case u @ UpdateTable(table, _, cond) Review comment: @aokolnychyi Both the cases have same actions. Is there a way we can keep it in once place ? ########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/OptimizeConditionsInRowLevelOperations.scala ########## @@ -21,7 +21,7 @@ package org.apache.spark.sql.catalyst.optimizer import org.apache.spark.sql.SparkSession import org.apache.spark.sql.catalyst.expressions.{Expression, Literal, SubqueryExpression} -import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan} +import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LocalRelation, LogicalPlan, UpdateTable} Review comment: imports in separate lines ? ########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteUpdate.scala ########## @@ -0,0 +1,87 @@ +/* + * 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.optimizer + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.expressions.Alias +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.catalyst.expressions.If +import org.apache.spark.sql.catalyst.expressions.SubqueryExpression +import org.apache.spark.sql.catalyst.plans.logical.Assignment +import org.apache.spark.sql.catalyst.plans.logical.Filter +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.plans.logical.Project +import org.apache.spark.sql.catalyst.plans.logical.ReplaceData +import org.apache.spark.sql.catalyst.plans.logical.UpdateTable +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.utils.PlanUtils.isIcebergRelation +import org.apache.spark.sql.catalyst.utils.RewriteRowLevelOperationHelper +import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation +import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits + +// TODO: should be part of early scan push down after the delete condition is optimized +case class RewriteUpdate(spark: SparkSession) extends Rule[LogicalPlan] with RewriteRowLevelOperationHelper { + + import ExtendedDataSourceV2Implicits._ + + // TODO: can we do any better for no-op updates? when conditions evaluate to false/true? + override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { + case UpdateTable(r: DataSourceV2Relation, assignments, Some(cond)) + if isIcebergRelation(r) && SubqueryExpression.hasSubquery(cond) => + throw new AnalysisException("UPDATE statements with subqueries are not currently supported") Review comment: @aokolnychyi We are keeping it here and not in checkAnalysis as we are going to open it up in follow-up ? ########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteUpdate.scala ########## @@ -0,0 +1,87 @@ +/* + * 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.optimizer + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.expressions.Alias +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.catalyst.expressions.If +import org.apache.spark.sql.catalyst.expressions.SubqueryExpression +import org.apache.spark.sql.catalyst.plans.logical.Assignment +import org.apache.spark.sql.catalyst.plans.logical.Filter +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.plans.logical.Project +import org.apache.spark.sql.catalyst.plans.logical.ReplaceData +import org.apache.spark.sql.catalyst.plans.logical.UpdateTable +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.utils.PlanUtils.isIcebergRelation +import org.apache.spark.sql.catalyst.utils.RewriteRowLevelOperationHelper +import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation +import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits + +// TODO: should be part of early scan push down after the delete condition is optimized +case class RewriteUpdate(spark: SparkSession) extends Rule[LogicalPlan] with RewriteRowLevelOperationHelper { + + import ExtendedDataSourceV2Implicits._ + + // TODO: can we do any better for no-op updates? when conditions evaluate to false/true? + override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { + case UpdateTable(r: DataSourceV2Relation, assignments, Some(cond)) + if isIcebergRelation(r) && SubqueryExpression.hasSubquery(cond) => + throw new AnalysisException("UPDATE statements with subqueries are not currently supported") + + case UpdateTable(r: DataSourceV2Relation, assignments, Some(cond)) if isIcebergRelation(r) => + val writeInfo = newWriteInfo(r.schema) + val mergeBuilder = r.table.asMergeable.newMergeBuilder("update", writeInfo) + + val matchingRowsPlanBuilder = scanRelation => Filter(cond, scanRelation) + val scanPlan = buildDynamicFilterScanPlan(spark, r.table, r.output, mergeBuilder, cond, matchingRowsPlanBuilder) + + val updateProjection = buildUpdateProjection(r, scanPlan, assignments, cond) + + val mergeWrite = mergeBuilder.asWriteBuilder.buildForBatch() + val writePlan = buildWritePlan(updateProjection, r.table) + ReplaceData(r, mergeWrite, writePlan) + } + + private def buildUpdateProjection( + relation: DataSourceV2Relation, + scanPlan: LogicalPlan, + assignments: Seq[Assignment], + cond: Expression): LogicalPlan = { + + // this method relies on the fact that the assignments have been aligned before + require(relation.output.size == assignments.size, "assignments must be aligned") + + // Spark is going to execute the condition for each column but it seems we cannot avoid this Review comment: @aokolnychyi I guess, if we can try to look at this in the future. I think there is not a good way to know the cost of the condition expression at the time of rewrite. So not sure if an alternate plan would do better in all condition, wdyt ? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
