malinjawi commented on code in PR #12218: URL: https://github.com/apache/gluten/pull/12218#discussion_r3610506815
########## gluten-delta/src/main/scala/org/apache/gluten/extension/DeltaCDFScanStrategy.scala: ########## @@ -0,0 +1,112 @@ +/* + * 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.gluten.extension + +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, Expression, NamedExpression} +import org.apache.spark.sql.catalyst.planning.PhysicalOperation +import org.apache.spark.sql.catalyst.plans.logical.{Filter, LocalRelation, LogicalPlan, Project} +import org.apache.spark.sql.delta.commands.cdc.CDCReader +import org.apache.spark.sql.execution.{SparkPlan, SparkStrategy} +import org.apache.spark.sql.execution.datasources.LogicalRelation + +case class DeltaCDFScanStrategy(spark: SparkSession) extends SparkStrategy { + override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match { + case PhysicalOperation(projects, filters, relation: LogicalRelation) => + relation.relation match { + case cdfRelation: CDCReader.DeltaCDFRelation if !touchesDeletionVectors(cdfRelation) => + planCDFRelation(relation, cdfRelation, projects, filters).map(planLater).toSeq + case _ => Nil + } + case _ => Nil + } + + // Delta CDF over a deletion-vector table needs Delta's DV-aware, row-level reconciliation: a + // deleted row stays physically in its data file and is masked by a DV, so the CDF `delete` rows + // for a commit are only the rows the DV newly marks. Re-planning the analyzed CDF batch plan here + // loses that reconciliation on some Delta versions (e.g. Delta 2.4 / Spark 3.4), where the remove + // side would then surface every row of a logically-removed file as a `delete` change row instead + // of just the DV-marked ones. Decline to intercept such reads and let Spark serve them through + // Delta's own DeltaCDFRelation scan, which applies DVs correctly. Offloading loses nothing here: + // the native CDF scan path does not support DV reconciliation either (see the matching guard in + // DeltaScanTransformer). + private def touchesDeletionVectors(cdfRelation: CDCReader.DeltaCDFRelation): Boolean = + cdfRelation.snapshotWithSchemaMode.snapshot.metadata.configuration + .get("delta.enableDeletionVectors") Review Comment: Good catch; you were right that the table property only controls future writes. This is now addressed in two layers: - `3b462034a` makes `DeltaCDFScanStrategy` inspect `AddFile`/`RemoveFile` actions only in the requested CDF version range. - Spark 3.4 CI then exposed an older physical fallback that still treated a DV-capable `TahoeFileIndex` as if every CDF range contained a DV. `049e608a3` now special-cases `CdcAddFileIndex` and `TahoeRemoveFileIndex` there as well, using their exact files/actions before the generic table-level fallback. The regression covers both directions: a DV-enabled table's insert-only `0..1` range must offload, while a range containing a DV-backed delete must fall back even after `delta.enableDeletionVectors` is set back to `false`, with the CDF rows checked for correctness. Formatting and test compilation pass for Spark 3.3, 3.4, 3.5, 4.0, and 4.1. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
