You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/06/22 01:40:33 UTC

[GitHub] [spark] aokolnychyi opened a new pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

aokolnychyi opened a new pull request #33008:
URL: https://github.com/apache/spark/pull/33008


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error message, please read the guideline first:
        https://spark.apache.org/error-message-guidelines.html
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   This WIP PR shows how we can use the proposed API in SPARK-35801 (per [design doc](https://docs.google.com/document/d/1W1niaD0X5jV7r5uIp380ZfTW8sL5fL2fy176aoaHCj8/)) to support DELETE statements that require rewriting data.
   
   **Note**: This PR must be split into a number of smaller PRs if we decide to adopt this approach. All changes are grouped here only to simplify the review process and support the design doc.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   These changes are required so that Spark can provide support for DELETE, UPDATE, MERGE statements.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   Yes, this PR introduces a set of new APIs for Data Source V2.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   
   This PR comes with a trivial test. More tests to come.
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660076623



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       Not really, we still want Spark to pass filters but it is up to the data source to apply them. For example, there may be implementations that apply filters to partitions but not files within partitions if they can atomically replace only partitions.
   
   To sum up, I don't anticipate any major changes to the existing pushdown logic.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658352962



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/Merge.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+
+/**
+ * A logical representation of a data source DELETE, UPDATE, or MERGE operation that requires
+ * rewriting data.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface Merge {
+
+  /**
+   * The actual SQL operation being performed.
+   */
+  enum Operation {
+    DELETE, UPDATE, MERGE
+  }
+
+  /**
+   * Returns the description associated with this merge.
+   */
+  default String description() {
+    return this.getClass().toString();
+  }
+
+  /**
+   * Returns the actual SQL operation being performed.
+   */
+  Operation operation();
+
+  /**
+   * Returns a {@link ScanBuilder} to configure a {@link Scan} for this merge.
+   * <p>
+   * For sources that support row-level changes, there are no requirements for input scans,
+   * but sources that delete by group must be passed a complete replacement of groups.
+   * This means that scans must produce all rows in a delete group if any are returned.
+   * <p>
+   * For example, if a source can only replace partitions, all rows from a partition must
+   * be returned by the scan, even if a filter can narrow the set of changes to a single file
+   * in the partition. Similarly, a source that can swap individual files must produce all rows
+   * of matching files, not just the rows that may change.
+   */
+  ScanBuilder scanBuilder();

Review comment:
       One of the reasons for introducing `RowLevelOperation` is to have coordination between the scan and the write. For example, the write may know the scan condition and may check that no data has been added since the scan that would match the condition. That is only required under some isolation levels. That's why it is up to the source to ensure that.
   
   I'll add a note about this on the next iteration.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472350


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140109/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967752566


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/49645/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967775164


   **[Test build #145176 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/145176/testReport)** for PR 33008 at commit [`5e95a57`](https://github.com/apache/spark/commit/5e95a57812e0a46205f2b6e2993790b520b07067).
    * This patch passes all tests.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `trait RewriteRowLevelCommand extends Rule[LogicalPlan] `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelCommand extends Command with SupportsSubquery `
     * `case class WriteDeltaProjections(`
     * `case class RowLevelOperationInfoImpl(`
     * `case class RowLevelOperationTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(projs: WriteDeltaProjections) extends WritingSparkTask `
     * `case class DeltaWithMetadataWritingSparkTask(`


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967776577


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/145176/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865500443


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44639/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660096389



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteDeleteFromTable.scala
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.UUID
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.InternalRowProjection
+import org.apache.spark.sql.catalyst.analysis.RowDeltaUtils._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, EqualNullSafe, Expression, Literal, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LogicalPlan, Project, ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsDelete, SupportsRowLevelOperations}
+import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperation, RowLevelOperationTable, SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Assigns a rewrite plan for v2 tables that support rewriting data to handle DELETE statements.
+ *
+ * If a table implements [[SupportsDelete]] and [[SupportsRowLevelOperations]], we assign a rewrite
+ * plan but the optimizer will check whether this particular DELETE statement can be handled
+ * by simply passing delete filters to the connector. If yes, the optimizer will then discard
+ * the rewrite plan.
+ */
+object RewriteDeleteFromTable extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case delete @ DeleteFromTable(aliasedTable, cond, None) if delete.resolved =>
+      EliminateSubqueryAliases(aliasedTable) match {
+        case relation @ DataSourceV2Relation(table: SupportsRowLevelOperations, _, _, _, _) =>
+          val rowLevelOperation = buildRowLevelOperation(table)
+          val rowLevelOperationTable = RowLevelOperationTable(table, rowLevelOperation)
+          val rewritePlan = rowLevelOperation match {
+            case _: SupportsDelta => buildWriteDeltaPlan(relation, rowLevelOperationTable, cond)
+            case _ => buildReplaceDataPlan(relation, rowLevelOperationTable, cond)
+          }
+          // keep the original relation in DELETE so that we can attempt to delete with metadata
+          DeleteFromTable(relation, cond, Some(rewritePlan))
+
+        case DataSourceV2Relation(_: SupportsDelete, _, _, _, _) =>
+          // don't assign a rewrite plan as the table supports deletes only with filters
+          delete
+
+        case DataSourceV2Relation(t, _, _, _, _) =>
+          throw new AnalysisException(s"Table $t does not support DELETE statements")
+
+        case _ =>
+          delete
+      }
+  }
+
+  // build a rewrite plan for sources that support replacing groups of data (e.g. files, partitions)
+  private def buildReplaceDataPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    // resolve all columns needed for DELETE including metadata columns for grouping data on write
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+
+    // construct a scan relation
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that contains unmatched rows in matched groups that must be carried over
+    // such rows do not match the condition but have to be copied over as the source can replace
+    // only groups of rows
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val remainingRowsFilter = Not(EqualNullSafe(deleteCond, Literal(true, BooleanType)))
+    val remainingRowsPlan = Filter(remainingRowsFilter, scanRelation)
+
+    // pass only required columns for unmatched rows to the writer
+    val writeRelation = relation.copy(table = table, output = requiredWriteAttrs)
+    ReplaceData(writeRelation, remainingRowsPlan, relation)
+  }
+
+  // build a rewrite plan for sources that support row-level changes
+  private def buildWriteDeltaPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    val merge = table.operation.asInstanceOf[SupportsDelta]
+
+    // resolve all columns needed for merge (e.g. row ID and columns for grouping data on write)
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+    val rowIdAttrs = resolveRefs(relation, merge.rowId)
+
+    // construct a scan relation and include all required columns
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that only contains records to delete
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val deletedRowsPlan = Filter(deleteCond, scanRelation)
+    val operation = Alias(Literal(DELETE_OPERATION), OPERATION_COLUMN)()
+    val project = Project(operation +: requiredWriteAttrs, deletedRowsPlan)
+
+    // pass operation and required columns to the writer as we are working with deltas
+    val operationAttr = resolveRef(project, FieldReference(OPERATION_COLUMN))
+    val writeRelationAttrs = operationAttr +: requiredWriteAttrs

Review comment:
       Good question. I am not sure whether we need new expr IDs. The purpose of `requiredWriteAttrs` is to detect what columns we need to project from the scan. Let me take a closer look.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472141






-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866429590


   **[Test build #140167 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140167/testReport)** for PR 33008 at commit [`bb9120f`](https://github.com/apache/spark/commit/bb9120fa1d015654b8b07e15a54aa42a57d984ee).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865481397


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44637/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866227683


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44683/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868099839


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44818/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868186757


   **[Test build #140286 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140286/testReport)** for PR 33008 at commit [`ad57d05`](https://github.com/apache/spark/commit/ad57d05af8108f60a676e71b53bb8e4514cdb972).
    * This patch passes all tests.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelCommand extends Command with SupportsSubquery `
     * `case class RowLevelOperationTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660078355



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);
+  void insert(T row);

Review comment:
       I should have added more docs to methods. I'll fix it in the coming days. We don't really use `write` but it is available in the parent. I guess we either default like now or throw an unsupported exception. Maybe, an exception will be safer. 
   
   Thoughts, @holdenk?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866231410


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44683/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866452224


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44693/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658349804



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       Here I only care about filters that can be pushed into the source.
   
   Do you have a PR for refactoring at hand, @huaxingao?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658350301



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/WriteToDataSourceV2Exec.scala
##########
@@ -288,6 +324,12 @@ case class WriteToDataSourceV2Exec(
     copy(query = newChild)
 }
 
+// TODO: could we parameterize this? Not that easy.

Review comment:
       Any ideas will be appreciated. I'll spend more time thinking about this but didn't want to block the PR.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865481512


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44637/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865503334


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44639/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865503324


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44639/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660093569



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link RowLevelOperation}. Data sources can implement this interface
+ * to indicate they support handling deltas of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends RowLevelOperation {

Review comment:
       We assume the default `RowLevelOperation` only supports replacing groups as it is more common and requires less from data sources. For example, regular Hive tables in Spark would be able to easily support group-based rewrites (i.e. partition-level granularity). 




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-939186480


   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866429590


   **[Test build #140167 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140167/testReport)** for PR 33008 at commit [`bb9120f`](https://github.com/apache/spark/commit/bb9120fa1d015654b8b07e15a54aa42a57d984ee).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865507392


   **[Test build #140111 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140111/testReport)** for PR 33008 at commit [`76f7921`](https://github.com/apache/spark/commit/76f7921e65e05f77d0a2fa5e98887b03178f3b84).
    * This patch **fails Spark unit tests**.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelOperation extends Command `
     * `case class MergeTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967722938


   **[Test build #145176 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/145176/testReport)** for PR 33008 at commit [`5e95a57`](https://github.com/apache/spark/commit/5e95a57812e0a46205f2b6e2993790b520b07067).


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868099821


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44818/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] huaxingao commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
huaxingao commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r657312390



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       I have a question here for filter pushed down (since I am refactoring this `V2ScanRelationPushDown`). Are the `remainingFilters` going to be applied to V2Relation?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865518197


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140111/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866187965


   **[Test build #140157 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140157/testReport)** for PR 33008 at commit [`5017da9`](https://github.com/apache/spark/commit/5017da993b2ad0ec33036c008d659e9159c22ff6).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660089996



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);

Review comment:
       It is currently a full row but the same API can be used for passing just updated columns. We could expose an optional mix-in interface for `RowLevelOperation`.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] holdenk commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
holdenk commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867868003


   I'd love to do a more thorough review, but I can't print the design doc so I'll hold off on that until the permissions are sorted out just because I do better with paper design docs :)


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] holdenk commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
holdenk commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658928712



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);

Review comment:
       qq: is T here a "full" row? e.g. if I am going to update only some fields is that something that we could support through this API? I admit it's probably kind of an edge case but think of like a mogodb record with a large binary payload and were just changing the name?
   Totally ok if not in scope for this just wanting to understand better.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866494185


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140167/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658349159



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link Merge}. Data sources can implement this interface to indicate
+ * they support row-level changes.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends Merge {
+  /**
+   * Returns the row ID column references that should be used for row equality.
+   */
+  NamedReference[] rowId();

Review comment:
       We have an array of references exactly for this reason. If a table has a compound key with two columns, this will return an array with 2 references.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865481512


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44637/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865478786


   **[Test build #140111 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140111/testReport)** for PR 33008 at commit [`76f7921`](https://github.com/apache/spark/commit/76f7921e65e05f77d0a2fa5e98887b03178f3b84).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r661761623



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.connector.catalog;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command;
+import org.apache.spark.sql.connector.write.RowLevelOperationBuilder;
+import org.apache.spark.sql.connector.write.RowLevelOperation;
+
+/**
+ * A mix-in interface for {@link Table} row-level operations support. Data sources can implement
+ * this interface to indicate they support rewriting data for DELETE, UPDATE, MERGE operations.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsRowLevelOperations extends Table {
+  /**
+   * Returns a {@link RowLevelOperationBuilder} to build a {@link RowLevelOperation}.
+   * Spark will call this method while planning DELETE, UPDATE or MERGE operations.
+   *
+   * @param command the row-level operation command (e.g. DELETE, UPDATE, MERGE)
+   * @param info write info
+   * @return the row-level operation builder
+   */
+  RowLevelOperationBuilder newRowLevelOperationBuilder(Command command, LogicalWriteInfo info);

Review comment:
       Or we can introduce some sort of `RowLevelOperationInfo` that will both provide options as well as the command.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867244796


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140224/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865503334


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44639/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867207389


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44752/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867244796


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140224/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472342


   **[Test build #140109 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140109/testReport)** for PR 33008 at commit [`b443bef`](https://github.com/apache/spark/commit/b443bef6792c4efbe1679c5e659050f3ea24746d).
    * This patch **fails RAT tests**.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelOperation extends Command `
     * `case class MergeTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867203971


   **[Test build #140224 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140224/testReport)** for PR 33008 at commit [`e5357b9`](https://github.com/apache/spark/commit/e5357b968b03c6d087a6fd910ccb22923dbc1bfd).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866310924


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140157/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867204035


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44752/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472141


   **[Test build #140109 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140109/testReport)** for PR 33008 at commit [`b443bef`](https://github.com/apache/spark/commit/b443bef6792c4efbe1679c5e659050f3ea24746d).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472350






-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866289864


   **[Test build #140157 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140157/testReport)** for PR 33008 at commit [`5017da9`](https://github.com/apache/spark/commit/5017da993b2ad0ec33036c008d659e9159c22ff6).
    * This patch **fails Spark unit tests**.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelOperation extends Command `
     * `case class MergeTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867207399


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44752/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967734571


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/49645/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967752566


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/49645/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867207399


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44752/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r660076623



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       Not really, we still want Spark to pass filters but it is up to the data source to apply them. For example, there may be implementations that apply filters to partitions but not files within partitions if they can atomically replace only partitions.
   
   To sum up, I don't anticipate any major changes to the existing pushdown logic.

##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);
+  void insert(T row);

Review comment:
       I should have added more docs to methods. I'll fix it in the coming days. We don't really use `write` but it is available in the parent. I guess we either default like now or throw an unsupported exception. Maybe, an exception will be safer. 
   
   Thoughts, @holdenk?

##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);

Review comment:
       It is currently a full row but the same API can be used for passing just updated columns. We could expose an optional mix-in interface for `RowLevelOperation`.

##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link RowLevelOperation}. Data sources can implement this interface
+ * to indicate they support handling deltas of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends RowLevelOperation {

Review comment:
       We assume the default `RowLevelOperation` only supports replacing groups as it is more common and requires less from data sources. For example, regular Hive tables in Spark would be able to easily support group-based rewrites (i.e. partition-level granularity). 

##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteDeleteFromTable.scala
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.UUID
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.InternalRowProjection
+import org.apache.spark.sql.catalyst.analysis.RowDeltaUtils._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, EqualNullSafe, Expression, Literal, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LogicalPlan, Project, ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsDelete, SupportsRowLevelOperations}
+import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperation, RowLevelOperationTable, SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Assigns a rewrite plan for v2 tables that support rewriting data to handle DELETE statements.
+ *
+ * If a table implements [[SupportsDelete]] and [[SupportsRowLevelOperations]], we assign a rewrite
+ * plan but the optimizer will check whether this particular DELETE statement can be handled
+ * by simply passing delete filters to the connector. If yes, the optimizer will then discard
+ * the rewrite plan.
+ */
+object RewriteDeleteFromTable extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case delete @ DeleteFromTable(aliasedTable, cond, None) if delete.resolved =>
+      EliminateSubqueryAliases(aliasedTable) match {
+        case relation @ DataSourceV2Relation(table: SupportsRowLevelOperations, _, _, _, _) =>
+          val rowLevelOperation = buildRowLevelOperation(table)
+          val rowLevelOperationTable = RowLevelOperationTable(table, rowLevelOperation)
+          val rewritePlan = rowLevelOperation match {
+            case _: SupportsDelta => buildWriteDeltaPlan(relation, rowLevelOperationTable, cond)
+            case _ => buildReplaceDataPlan(relation, rowLevelOperationTable, cond)
+          }
+          // keep the original relation in DELETE so that we can attempt to delete with metadata
+          DeleteFromTable(relation, cond, Some(rewritePlan))
+
+        case DataSourceV2Relation(_: SupportsDelete, _, _, _, _) =>
+          // don't assign a rewrite plan as the table supports deletes only with filters
+          delete
+
+        case DataSourceV2Relation(t, _, _, _, _) =>
+          throw new AnalysisException(s"Table $t does not support DELETE statements")
+
+        case _ =>
+          delete
+      }
+  }
+
+  // build a rewrite plan for sources that support replacing groups of data (e.g. files, partitions)
+  private def buildReplaceDataPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    // resolve all columns needed for DELETE including metadata columns for grouping data on write
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+
+    // construct a scan relation
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that contains unmatched rows in matched groups that must be carried over
+    // such rows do not match the condition but have to be copied over as the source can replace
+    // only groups of rows
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val remainingRowsFilter = Not(EqualNullSafe(deleteCond, Literal(true, BooleanType)))
+    val remainingRowsPlan = Filter(remainingRowsFilter, scanRelation)
+
+    // pass only required columns for unmatched rows to the writer
+    val writeRelation = relation.copy(table = table, output = requiredWriteAttrs)
+    ReplaceData(writeRelation, remainingRowsPlan, relation)
+  }
+
+  // build a rewrite plan for sources that support row-level changes
+  private def buildWriteDeltaPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    val merge = table.operation.asInstanceOf[SupportsDelta]
+
+    // resolve all columns needed for merge (e.g. row ID and columns for grouping data on write)
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+    val rowIdAttrs = resolveRefs(relation, merge.rowId)
+
+    // construct a scan relation and include all required columns
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that only contains records to delete
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val deletedRowsPlan = Filter(deleteCond, scanRelation)
+    val operation = Alias(Literal(DELETE_OPERATION), OPERATION_COLUMN)()
+    val project = Project(operation +: requiredWriteAttrs, deletedRowsPlan)
+
+    // pass operation and required columns to the writer as we are working with deltas
+    val operationAttr = resolveRef(project, FieldReference(OPERATION_COLUMN))
+    val writeRelationAttrs = operationAttr +: requiredWriteAttrs

Review comment:
       Good question. I am not sure whether we need new expr IDs. The purpose of `requiredWriteAttrs` is to detect what columns we need to project from the scan. Let me take a closer look.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866494185


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140167/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865518197


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140111/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r661753646



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsRowLevelOperations.java
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.connector.catalog;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.write.LogicalWriteInfo;
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command;
+import org.apache.spark.sql.connector.write.RowLevelOperationBuilder;
+import org.apache.spark.sql.connector.write.RowLevelOperation;
+
+/**
+ * A mix-in interface for {@link Table} row-level operations support. Data sources can implement
+ * this interface to indicate they support rewriting data for DELETE, UPDATE, MERGE operations.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsRowLevelOperations extends Table {
+  /**
+   * Returns a {@link RowLevelOperationBuilder} to build a {@link RowLevelOperation}.
+   * Spark will call this method while planning DELETE, UPDATE or MERGE operations.
+   *
+   * @param command the row-level operation command (e.g. DELETE, UPDATE, MERGE)
+   * @param info write info
+   * @return the row-level operation builder
+   */
+  RowLevelOperationBuilder newRowLevelOperationBuilder(Command command, LogicalWriteInfo info);

Review comment:
       We should probably pass options instead of the write info here so that we can propagate user config.
   
   Then we can pass `LogicalWriteInfo` to `writeBuilder` in `RowLevelOperation` and that write info will have a correct write schema.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868197792


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140286/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658349804



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       Here I only care about filters that can be pushed into the source. Others are ignored.
   
   Do you have a PR for refactoring at hand, @huaxingao?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] holdenk commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
holdenk commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658929106



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link Merge}. Data sources can implement this interface to indicate
+ * they support row-level changes.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends Merge {
+  /**
+   * Returns the row ID column references that should be used for row equality.
+   */
+  NamedReference[] rowId();

Review comment:
       Makes sense, thanks :)




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867243424


   **[Test build #140224 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140224/testReport)** for PR 33008 at commit [`e5357b9`](https://github.com/apache/spark/commit/e5357b968b03c6d087a6fd910ccb22923dbc1bfd).
    * This patch **fails Spark unit tests**.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelOperation extends Command `
     * `case class MergeTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866452224


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44693/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866310924


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140157/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658351718



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteDeleteFromTable.scala
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.UUID
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.InternalRowProjection
+import org.apache.spark.sql.catalyst.analysis.RowDeltaUtils._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, EqualNullSafe, Expression, Literal, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LogicalPlan, Project, ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsDelete, SupportsRowLevelOperations}
+import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperation, RowLevelOperationTable, SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Assigns a rewrite plan for v2 tables that support rewriting data to handle DELETE statements.
+ *
+ * If a table implements [[SupportsDelete]] and [[SupportsRowLevelOperations]], we assign a rewrite
+ * plan but the optimizer will check whether this particular DELETE statement can be handled
+ * by simply passing delete filters to the connector. If yes, the optimizer will then discard
+ * the rewrite plan.
+ */
+object RewriteDeleteFromTable extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case delete @ DeleteFromTable(aliasedTable, cond, None) if delete.resolved =>
+      EliminateSubqueryAliases(aliasedTable) match {
+        case relation @ DataSourceV2Relation(table: SupportsRowLevelOperations, _, _, _, _) =>
+          val rowLevelOperation = buildRowLevelOperation(table)
+          val rowLevelOperationTable = RowLevelOperationTable(table, rowLevelOperation)
+          val rewritePlan = rowLevelOperation match {
+            case _: SupportsDelta => buildWriteDeltaPlan(relation, rowLevelOperationTable, cond)
+            case _ => buildReplaceDataPlan(relation, rowLevelOperationTable, cond)
+          }
+          // keep the original relation in DELETE so that we can attempt to delete with metadata
+          DeleteFromTable(relation, cond, Some(rewritePlan))
+
+        case DataSourceV2Relation(_: SupportsDelete, _, _, _, _) =>
+          // don't assign a rewrite plan as the table supports deletes only with filters
+          delete
+
+        case DataSourceV2Relation(t, _, _, _, _) =>
+          throw new AnalysisException(s"Table $t does not support DELETE statements")
+
+        case _ =>
+          delete
+      }
+  }
+
+  // build a rewrite plan for sources that support replacing groups of data (e.g. files, partitions)
+  private def buildReplaceDataPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    // resolve all columns needed for DELETE including metadata columns for grouping data on write
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+
+    // construct a scan relation
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that contains unmatched rows in matched groups that must be carried over
+    // such rows do not match the condition but have to be copied over as the source can replace
+    // only groups of rows
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val remainingRowsFilter = Not(EqualNullSafe(deleteCond, Literal(true, BooleanType)))
+    val remainingRowsPlan = Filter(remainingRowsFilter, scanRelation)
+
+    // pass only required columns for unmatched rows to the writer
+    val writeRelation = relation.copy(table = table, output = requiredWriteAttrs)
+    ReplaceData(writeRelation, remainingRowsPlan, relation)
+  }
+
+  // build a rewrite plan for sources that support row-level changes
+  private def buildWriteDeltaPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    val merge = table.operation.asInstanceOf[SupportsDelta]
+
+    // resolve all columns needed for merge (e.g. row ID and columns for grouping data on write)
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+    val rowIdAttrs = resolveRefs(relation, merge.rowId)
+
+    // construct a scan relation and include all required columns
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that only contains records to delete
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val deletedRowsPlan = Filter(deleteCond, scanRelation)
+    val operation = Alias(Literal(DELETE_OPERATION), OPERATION_COLUMN)()
+    val project = Project(operation +: requiredWriteAttrs, deletedRowsPlan)
+
+    // pass operation and required columns to the writer as we are working with deltas
+    val operationAttr = resolveRef(project, FieldReference(OPERATION_COLUMN))

Review comment:
       This part is a bit ugly as we have an extra column that I currently make part of relation output so that the default name-based resolution works.
   
   An alternative is to add references to `WriteDelta` and have some custom resolution logic.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967748148


   Kubernetes integration test status failure
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/49645/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868089446


   I'd appreciate your input, @brkyvz @rdblue @cloud-fan @HyukjinKwon @viirya @sunchao @dongjoon-hyun @dilipbiswal 


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868041679


   **[Test build #140286 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140286/testReport)** for PR 33008 at commit [`ad57d05`](https://github.com/apache/spark/commit/ad57d05af8108f60a676e71b53bb8e4514cdb972).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868095947


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44818/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866187965


   **[Test build #140157 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140157/testReport)** for PR 33008 at commit [`5017da9`](https://github.com/apache/spark/commit/5017da993b2ad0ec33036c008d659e9159c22ff6).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866446253


   Kubernetes integration test unable to build dist.
   
   exiting with code: 1
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44693/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] huaxingao commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
huaxingao commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r659859205



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       I am refactoring `V2ScanRelationPushDown` in this PR https://github.com/apache/spark/pull/32049.
   I have one more question for filter push down: in the doc of `RowLevelOperation`.`scanBuilder()`, it is said that `Some sources will avoid pushing filters into files (file granularity), while others will avoid pruning files within a partition (partition granularity)`. To avoid pushing down the filters that are mentioned above, do you need to change the current implementation of filter push down?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868099839


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44818/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868197792


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140286/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472350






-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] viirya commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r659253107



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link RowLevelOperation}. Data sources can implement this interface
+ * to indicate they support handling deltas of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends RowLevelOperation {

Review comment:
       Do we also need an interface for those that need to replace groups?

##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteDeleteFromTable.scala
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.UUID
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.InternalRowProjection
+import org.apache.spark.sql.catalyst.analysis.RowDeltaUtils._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, EqualNullSafe, Expression, Literal, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LogicalPlan, Project, ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsDelete, SupportsRowLevelOperations}
+import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperation, RowLevelOperationTable, SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Assigns a rewrite plan for v2 tables that support rewriting data to handle DELETE statements.
+ *
+ * If a table implements [[SupportsDelete]] and [[SupportsRowLevelOperations]], we assign a rewrite
+ * plan but the optimizer will check whether this particular DELETE statement can be handled
+ * by simply passing delete filters to the connector. If yes, the optimizer will then discard
+ * the rewrite plan.
+ */
+object RewriteDeleteFromTable extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case delete @ DeleteFromTable(aliasedTable, cond, None) if delete.resolved =>
+      EliminateSubqueryAliases(aliasedTable) match {
+        case relation @ DataSourceV2Relation(table: SupportsRowLevelOperations, _, _, _, _) =>
+          val rowLevelOperation = buildRowLevelOperation(table)
+          val rowLevelOperationTable = RowLevelOperationTable(table, rowLevelOperation)
+          val rewritePlan = rowLevelOperation match {
+            case _: SupportsDelta => buildWriteDeltaPlan(relation, rowLevelOperationTable, cond)
+            case _ => buildReplaceDataPlan(relation, rowLevelOperationTable, cond)
+          }
+          // keep the original relation in DELETE so that we can attempt to delete with metadata
+          DeleteFromTable(relation, cond, Some(rewritePlan))
+
+        case DataSourceV2Relation(_: SupportsDelete, _, _, _, _) =>
+          // don't assign a rewrite plan as the table supports deletes only with filters
+          delete
+
+        case DataSourceV2Relation(t, _, _, _, _) =>
+          throw new AnalysisException(s"Table $t does not support DELETE statements")
+
+        case _ =>
+          delete
+      }
+  }
+
+  // build a rewrite plan for sources that support replacing groups of data (e.g. files, partitions)
+  private def buildReplaceDataPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    // resolve all columns needed for DELETE including metadata columns for grouping data on write
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+
+    // construct a scan relation
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that contains unmatched rows in matched groups that must be carried over
+    // such rows do not match the condition but have to be copied over as the source can replace
+    // only groups of rows
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val remainingRowsFilter = Not(EqualNullSafe(deleteCond, Literal(true, BooleanType)))
+    val remainingRowsPlan = Filter(remainingRowsFilter, scanRelation)
+
+    // pass only required columns for unmatched rows to the writer
+    val writeRelation = relation.copy(table = table, output = requiredWriteAttrs)
+    ReplaceData(writeRelation, remainingRowsPlan, relation)
+  }
+
+  // build a rewrite plan for sources that support row-level changes
+  private def buildWriteDeltaPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    val merge = table.operation.asInstanceOf[SupportsDelta]
+
+    // resolve all columns needed for merge (e.g. row ID and columns for grouping data on write)
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+    val rowIdAttrs = resolveRefs(relation, merge.rowId)
+
+    // construct a scan relation and include all required columns
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that only contains records to delete
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val deletedRowsPlan = Filter(deleteCond, scanRelation)
+    val operation = Alias(Literal(DELETE_OPERATION), OPERATION_COLUMN)()
+    val project = Project(operation +: requiredWriteAttrs, deletedRowsPlan)
+
+    // pass operation and required columns to the writer as we are working with deltas
+    val operationAttr = resolveRef(project, FieldReference(OPERATION_COLUMN))
+    val writeRelationAttrs = operationAttr +: requiredWriteAttrs

Review comment:
       `requiredWriteAttrs` shared between scan and write relations? Do we need to use new expr ids for write relation?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] holdenk commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
holdenk commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658183846



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/Merge.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+import org.apache.spark.sql.connector.read.Scan;
+import org.apache.spark.sql.connector.read.ScanBuilder;
+
+/**
+ * A logical representation of a data source DELETE, UPDATE, or MERGE operation that requires
+ * rewriting data.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface Merge {
+
+  /**
+   * The actual SQL operation being performed.
+   */
+  enum Operation {
+    DELETE, UPDATE, MERGE
+  }
+
+  /**
+   * Returns the description associated with this merge.
+   */
+  default String description() {
+    return this.getClass().toString();
+  }
+
+  /**
+   * Returns the actual SQL operation being performed.
+   */
+  Operation operation();
+
+  /**
+   * Returns a {@link ScanBuilder} to configure a {@link Scan} for this merge.
+   * <p>
+   * For sources that support row-level changes, there are no requirements for input scans,
+   * but sources that delete by group must be passed a complete replacement of groups.
+   * This means that scans must produce all rows in a delete group if any are returned.
+   * <p>
+   * For example, if a source can only replace partitions, all rows from a partition must
+   * be returned by the scan, even if a filter can narrow the set of changes to a single file
+   * in the partition. Similarly, a source that can swap individual files must produce all rows
+   * of matching files, not just the rows that may change.
+   */
+  ScanBuilder scanBuilder();

Review comment:
       One question here is how we handle the situation where there is a mutation after the scan has completed. I'm assuming we leave this up to the catalog to handle but might be nice to call out here?

##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/SupportsDelta.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+import org.apache.spark.sql.connector.expressions.NamedReference;
+
+/**
+ * A mix-in interface for {@link Merge}. Data sources can implement this interface to indicate
+ * they support row-level changes.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface SupportsDelta extends Merge {
+  /**
+   * Returns the row ID column references that should be used for row equality.
+   */
+  NamedReference[] rowId();

Review comment:
       What about SQL interfaces where there is a combined key?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-867203971


   **[Test build #140224 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140224/testReport)** for PR 33008 at commit [`e5357b9`](https://github.com/apache/spark/commit/e5357b968b03c6d087a6fd910ccb22923dbc1bfd).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] holdenk commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
holdenk commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658927460



##########
File path: sql/catalyst/src/main/java/org/apache/spark/sql/connector/write/DeltaWriter.java
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.connector.write;
+
+import org.apache.spark.annotation.Experimental;
+
+/**
+ * A data writer returned by {@link DeltaWriterFactory#createWriter(int, long)} and is
+ * responsible for writing a delta of rows.
+ *
+ * @since 3.2.0
+ */
+@Experimental
+public interface DeltaWriter<T> extends DataWriter<T> {
+  void delete(T id);
+  void update(T id, T row);
+  void insert(T row);

Review comment:
       I guess my one question here is why we have insert when were just delegating write to it by default? Maybe we could add some javadoc explaining the difference?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865478786


   **[Test build #140111 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140111/testReport)** for PR 33008 at commit [`76f7921`](https://github.com/apache/spark/commit/76f7921e65e05f77d0a2fa5e98887b03178f3b84).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472350


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/140109/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472141






-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866231410


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder-K8s/44683/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866220952


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44683/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865475521


   Kubernetes integration test starting
   URL: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder-K8s/44637/
   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA removed a comment on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-865472141


   **[Test build #140109 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140109/testReport)** for PR 33008 at commit [`b443bef`](https://github.com/apache/spark/commit/b443bef6792c4efbe1679c5e659050f3ea24746d).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] AmplabJenkins commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967776577


   
   Refer to this link for build results (access rights to CI server needed): 
   https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/145176/
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-967722938


   **[Test build #145176 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/145176/testReport)** for PR 33008 at commit [`5e95a57`](https://github.com/apache/spark/commit/5e95a57812e0a46205f2b6e2993790b520b07067).


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-1047312665


   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-868041679


   **[Test build #140286 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140286/testReport)** for PR 33008 at commit [`ad57d05`](https://github.com/apache/spark/commit/ad57d05af8108f60a676e71b53bb8e4514cdb972).


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] huaxingao commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
huaxingao commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r659859205



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala
##########
@@ -30,6 +30,35 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] {
   import DataSourceV2Implicits._
 
   override def apply(plan: LogicalPlan): LogicalPlan = plan transformDown {
+    case RowLevelOperation(operation, cond, relation: DataSourceV2Relation, rewritePlan) =>
+      val mergeTable = relation.table.asMergeTable
+      val scanBuilder = mergeTable.newScanBuilder(relation.options)
+
+      val normalizedFilters = DataSourceStrategy.normalizeExprs(cond :: Nil, relation.output)
+      val (_, normalizedFiltersWithoutSubquery) =
+        normalizedFilters.partition(SubqueryExpression.hasSubquery)
+
+      val (pushedFilters, remainingFilters) = PushDownUtils.pushFilters(

Review comment:
       I am refactoring `V2ScanRelationPushDown` in this PR https://github.com/apache/spark/pull/32049.
   I have one more question for filter push down: in the doc of `RowLevelOperation`.`scanBuilder()`, it is said that `Some sources will avoid pushing filters into files (file granularity), while others will avoid pruning files within a partition (partition granularity)`. To avoid pushing down the filters that are mentioned above, do you need to change the current implementation of filter push down?




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] SparkQA commented on pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #33008:
URL: https://github.com/apache/spark/pull/33008#issuecomment-866481236


   **[Test build #140167 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/140167/testReport)** for PR 33008 at commit [`bb9120f`](https://github.com/apache/spark/commit/bb9120fa1d015654b8b07e15a54aa42a57d984ee).
    * This patch **fails Spark unit tests**.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `case class InternalRowProjection(schema: StructType, colOrdinals: Seq[Int]) extends InternalRow `
     * `case class ReplaceData(`
     * `case class WriteDelta(`
     * `trait RowLevelOperation extends Command `
     * `case class MergeTable(`
     * `case class ReplaceDataExec(`
     * `case class WriteDeltaExec(`
     * `trait WritingSparkTask extends Logging with Serializable `
     * `case class DeltaWritingSparkTask(`


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] aokolnychyi commented on a change in pull request #33008: [WIP][SPARK-35801][SQL] Support DELETE operations that require rewriting data

Posted by GitBox <gi...@apache.org>.
aokolnychyi commented on a change in pull request #33008:
URL: https://github.com/apache/spark/pull/33008#discussion_r658351718



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteDeleteFromTable.scala
##########
@@ -0,0 +1,186 @@
+/*
+ * 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 java.util.UUID
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.InternalRowProjection
+import org.apache.spark.sql.catalyst.analysis.RowDeltaUtils._
+import org.apache.spark.sql.catalyst.expressions.{Alias, AttributeReference, EqualNullSafe, Expression, Literal, Not}
+import org.apache.spark.sql.catalyst.plans.logical.{DeleteFromTable, Filter, LogicalPlan, Project, ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.connector.catalog.{SupportsDelete, SupportsRowLevelOperations}
+import org.apache.spark.sql.connector.expressions.{FieldReference, NamedReference}
+import org.apache.spark.sql.connector.write.{LogicalWriteInfo, LogicalWriteInfoImpl, RowLevelOperation, RowLevelOperationTable, SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.DELETE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Assigns a rewrite plan for v2 tables that support rewriting data to handle DELETE statements.
+ *
+ * If a table implements [[SupportsDelete]] and [[SupportsRowLevelOperations]], we assign a rewrite
+ * plan but the optimizer will check whether this particular DELETE statement can be handled
+ * by simply passing delete filters to the connector. If yes, the optimizer will then discard
+ * the rewrite plan.
+ */
+object RewriteDeleteFromTable extends Rule[LogicalPlan] {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case delete @ DeleteFromTable(aliasedTable, cond, None) if delete.resolved =>
+      EliminateSubqueryAliases(aliasedTable) match {
+        case relation @ DataSourceV2Relation(table: SupportsRowLevelOperations, _, _, _, _) =>
+          val rowLevelOperation = buildRowLevelOperation(table)
+          val rowLevelOperationTable = RowLevelOperationTable(table, rowLevelOperation)
+          val rewritePlan = rowLevelOperation match {
+            case _: SupportsDelta => buildWriteDeltaPlan(relation, rowLevelOperationTable, cond)
+            case _ => buildReplaceDataPlan(relation, rowLevelOperationTable, cond)
+          }
+          // keep the original relation in DELETE so that we can attempt to delete with metadata
+          DeleteFromTable(relation, cond, Some(rewritePlan))
+
+        case DataSourceV2Relation(_: SupportsDelete, _, _, _, _) =>
+          // don't assign a rewrite plan as the table supports deletes only with filters
+          delete
+
+        case DataSourceV2Relation(t, _, _, _, _) =>
+          throw new AnalysisException(s"Table $t does not support DELETE statements")
+
+        case _ =>
+          delete
+      }
+  }
+
+  // build a rewrite plan for sources that support replacing groups of data (e.g. files, partitions)
+  private def buildReplaceDataPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    // resolve all columns needed for DELETE including metadata columns for grouping data on write
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+
+    // construct a scan relation
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that contains unmatched rows in matched groups that must be carried over
+    // such rows do not match the condition but have to be copied over as the source can replace
+    // only groups of rows
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val remainingRowsFilter = Not(EqualNullSafe(deleteCond, Literal(true, BooleanType)))
+    val remainingRowsPlan = Filter(remainingRowsFilter, scanRelation)
+
+    // pass only required columns for unmatched rows to the writer
+    val writeRelation = relation.copy(table = table, output = requiredWriteAttrs)
+    ReplaceData(writeRelation, remainingRowsPlan, relation)
+  }
+
+  // build a rewrite plan for sources that support row-level changes
+  private def buildWriteDeltaPlan(
+      relation: DataSourceV2Relation,
+      table: RowLevelOperationTable,
+      cond: Option[Expression]): LogicalPlan = {
+
+    val merge = table.operation.asInstanceOf[SupportsDelta]
+
+    // resolve all columns needed for merge (e.g. row ID and columns for grouping data on write)
+    val requiredWriteAttrs = resolveRefs(relation, table.operation.requiredWriteAttributes)
+    val rowIdAttrs = resolveRefs(relation, merge.rowId)
+
+    // construct a scan relation and include all required columns
+    val scanAttrs = toScanAttrs(relation, requiredWriteAttrs)
+    val scanRelation = relation.copy(table = table, output = scanAttrs)
+
+    // construct a plan that only contains records to delete
+    val deleteCond = cond.getOrElse(Literal.TrueLiteral)
+    val deletedRowsPlan = Filter(deleteCond, scanRelation)
+    val operation = Alias(Literal(DELETE_OPERATION), OPERATION_COLUMN)()
+    val project = Project(operation +: requiredWriteAttrs, deletedRowsPlan)
+
+    // pass operation and required columns to the writer as we are working with deltas
+    val operationAttr = resolveRef(project, FieldReference(OPERATION_COLUMN))

Review comment:
       This part is a bit ugly as we have an extra column that I currently make part of relation output so that the default name-based resolution works. An alternative is to add references to `WriteDelta` and have some custom resolution logic.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org