rdblue commented on a change in pull request #1947: URL: https://github.com/apache/iceberg/pull/1947#discussion_r559824560
########## File path: spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteMergeInto.scala ########## @@ -0,0 +1,117 @@ +/* + * 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.catalyst.analysis.Resolver +import org.apache.spark.sql.catalyst.expressions.Alias +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.catalyst.expressions.InputFileName +import org.apache.spark.sql.catalyst.expressions.IsNull +import org.apache.spark.sql.catalyst.expressions.Literal +import org.apache.spark.sql.catalyst.plans.FullOuter +import org.apache.spark.sql.catalyst.plans.Inner +import org.apache.spark.sql.catalyst.plans.logical.DeleteAction +import org.apache.spark.sql.catalyst.plans.logical.InsertAction +import org.apache.spark.sql.catalyst.plans.logical.Join +import org.apache.spark.sql.catalyst.plans.logical.JoinHint +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan +import org.apache.spark.sql.catalyst.plans.logical.MergeAction +import org.apache.spark.sql.catalyst.plans.logical.MergeInto +import org.apache.spark.sql.catalyst.plans.logical.MergeIntoParams +import org.apache.spark.sql.catalyst.plans.logical.MergeIntoTable +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.UpdateAction +import org.apache.spark.sql.catalyst.rules.Rule +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.DataSourceV2ScanRelation +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.BooleanType + +case class RewriteMergeInto(conf: SQLConf) extends Rule[LogicalPlan] with RewriteRowLevelOperationHelper { + val ROW_FROM_SOURCE = "_row_from_source_" + val ROW_FROM_TARGET = "_row_from_target_" + private val TRUE_LITERAL = Literal(true, BooleanType) + private val FALSE_LITERAL = Literal(false, BooleanType) + + import org.apache.spark.sql.execution.datasources.v2.ExtendedDataSourceV2Implicits._ + + override def resolver: Resolver = conf.resolver + + override def apply(plan: LogicalPlan): LogicalPlan = { + plan resolveOperators { + case MergeIntoTable(target: DataSourceV2Relation, source: LogicalPlan, cond, matchedActions, notMatchedActions) => + val targetOutputCols = target.output + val newProjectCols = target.output ++ Seq(Alias(InputFileName(), FILE_NAME_COL)()) + val newTargetTable = Project(newProjectCols, target) + + // Construct the plan to prune target based on join condition between source and + // target. + val writeInfo = newWriteInfo(target.schema) + val mergeBuilder = target.table.asMergeable.newMergeBuilder("merge", writeInfo) + val matchingRowsPlanBuilder = (_: DataSourceV2ScanRelation) => + Join(source, newTargetTable, Inner, Some(cond), JoinHint.NONE) + // TODO - extract the local predicates that references the target from the join condition and + // pass to buildScanPlan to ensure push-down. Review comment: @dilipbiswal, this extraction is already done in the `pushFilters` method that @aokolnychyi implemented for delete. That's one reason why this also passes down `target.output`. The filters that are pushed down are the ones that only reference those attributes: ```scala val tableAttrSet = AttributeSet(tableAttrs) val predicates = splitConjunctivePredicates(cond).filter(_.references.subsetOf(tableAttrSet)) if (predicates.nonEmpty) { val normalizedPredicates = DataSourceStrategy.normalizeExprs(predicates, tableAttrs) PushDownUtils.pushFilters(scanBuilder, normalizedPredicates) } ``` ---------------------------------------------------------------- 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]
