-
Notifications
You must be signed in to change notification settings - Fork 616
[VL][Delta] Add JVM Delta DV scan handoff #12269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhztheplayer
merged 12 commits into
apache:main
from
malinjawi:split/delta-dv-java-scan-handoff-pr-clean
Jun 12, 2026
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
55365da
[VL][Delta] Add JVM DV scan handoff
9bdc10c
[VL][Delta] Address DV handoff review feedback
malinjawi 05044d2
Trigger PR reopen
64e4992
Trigger PR update
31b5b0d
Merge upstream/main into split/delta-dv-java-scan-handoff-pr-clean
9dbf7b5
[VL][Delta] Remove DV validation that blocks native DV scan handoff
d2f96cc
Merge branch 'main' into split/delta-dv-java-scan-handoff-pr-clean
malinjawi 759c19e
[VL][Delta] Address Copilot review feedback on DV scan handoff
malinjawi 97aaf7c
[VL][Delta] Rename DV preprocess classes to avoid shadowing Delta cla…
malinjawi 4a73dd0
[VL][Delta] Generate DV split info in DeltaScanTransformer
malinjawi f526370
[VL][Delta] Drop redundant DV preprocess rule
malinjawi ba20a5d
[VL][Delta] Document DV strip rule with plan-shape example
malinjawi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...lta23/main/scala/org/apache/gluten/extension/PreprocessDeltaScanWithDeletionVectors.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * 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.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
| /** | ||
| * Delta versions before 3.3 do not expose the DV scan preprocessing API used by the native handoff. | ||
| */ | ||
| class PreprocessDeltaScanWithDeletionVectors(protected val spark: SparkSession) | ||
| extends Rule[LogicalPlan] { | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan | ||
| } |
30 changes: 30 additions & 0 deletions
30
...lta24/main/scala/org/apache/gluten/extension/PreprocessDeltaScanWithDeletionVectors.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * 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.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
| /** | ||
| * Delta versions before 3.3 do not expose the DV scan preprocessing API used by the native handoff. | ||
| */ | ||
| class PreprocessDeltaScanWithDeletionVectors(protected val spark: SparkSession) | ||
| extends Rule[LogicalPlan] { | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = plan | ||
| } |
36 changes: 36 additions & 0 deletions
36
...lta33/main/scala/org/apache/gluten/extension/PreprocessDeltaScanWithDeletionVectors.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * 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.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.delta.GlutenPreprocessTableWithDVs | ||
|
|
||
| /** | ||
| * Delta 3.3 compatibility rule for DV scan metadata. | ||
| * | ||
| * Delta's own PrepareDeltaScan still runs normally. This Gluten-scoped rule only adds the | ||
| * backend-visible DV metadata columns after Delta has prepared the scan, so the physical Delta scan | ||
| * handoff can materialize the per-file DV payload for Velox without replacing Delta classes. | ||
| */ | ||
| class PreprocessDeltaScanWithDeletionVectors(protected val spark: SparkSession) | ||
| extends Rule[LogicalPlan] | ||
| with GlutenPreprocessTableWithDVs { | ||
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = preprocessTablesWithDVs(plan) | ||
| } | ||
235 changes: 235 additions & 0 deletions
235
...elox/src-delta33/main/scala/org/apache/spark/sql/delta/GlutenPreprocessTableWithDVs.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /* | ||
| * 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.delta | ||
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, EqualTo, Literal} | ||
| import org.apache.spark.sql.catalyst.expressions.Literal.TrueLiteral | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project} | ||
| import org.apache.spark.sql.delta.DeltaParquetFileFormat._ | ||
| import org.apache.spark.sql.delta.commands.DeletionVectorUtils.deletionVectorsReadable | ||
| import org.apache.spark.sql.delta.files.{TahoeFileIndex, TahoeLogFileIndex} | ||
| import org.apache.spark.sql.delta.sources.DeltaSQLConf | ||
| import org.apache.spark.sql.execution.datasources.FileFormat.METADATA_NAME | ||
| import org.apache.spark.sql.execution.datasources.HadoopFsRelation | ||
| import org.apache.spark.sql.execution.datasources.LogicalRelation | ||
| import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * Rewrites Delta scans over DV-enabled tables to request the backend-specific skip-row metadata | ||
| * column only when the snapshot actually contains DVs. | ||
| * | ||
| * This is a Gluten-local adaptation of Delta's `PreprocessTableWithDVs` that additionally handles | ||
| * `GlutenDeltaParquetFileFormat` relations. It lives in this package for access to Delta's | ||
| * package-private APIs, but deliberately uses Gluten-prefixed names: the vanilla | ||
| * `PreprocessTableWithDVs`/`ScanWithDeletionVectors` classfiles ship inside the delta-spark jar and | ||
| * are linked into Delta's own `PreprocessTableWithDVsStrategy`, so reusing those names would shadow | ||
| * them with classpath-order-dependent behavior. | ||
| */ | ||
| trait GlutenPreprocessTableWithDVs extends SubqueryTransformerHelper { | ||
| def preprocessTablesWithDVs(plan: LogicalPlan): LogicalPlan = { | ||
| plan.transformDown { case GlutenScanWithDeletionVectors(dvScan) => dvScan } | ||
| } | ||
| } | ||
|
|
||
| object GlutenScanWithDeletionVectors { | ||
| def unapply(a: LogicalRelation): Option[LogicalPlan] = a match { | ||
| case scan @ LogicalRelation( | ||
| relation @ HadoopFsRelation( | ||
| index: TahoeFileIndex, | ||
| _, | ||
| _, | ||
| _, | ||
| format: DeltaParquetFileFormat, | ||
| _), | ||
| _, | ||
| _, | ||
| _) => | ||
| dvEnabledScanFor(scan, relation, format, index) | ||
| case scan @ LogicalRelation( | ||
| relation @ HadoopFsRelation( | ||
| index: TahoeFileIndex, | ||
| _, | ||
| _, | ||
| _, | ||
| format: GlutenDeltaParquetFileFormat, | ||
| _), | ||
| _, | ||
| _, | ||
| _) => | ||
| dvEnabledScanFor(scan, relation, format, index) | ||
| case _ => None | ||
| } | ||
|
|
||
| def dvEnabledScanFor( | ||
| scan: LogicalRelation, | ||
| hadoopRelation: HadoopFsRelation, | ||
| fileFormat: DeltaParquetFileFormat, | ||
| index: TahoeFileIndex): Option[LogicalPlan] = { | ||
| if (!deletionVectorsReadable(index.protocol, index.metadata)) { | ||
| return None | ||
| } | ||
|
|
||
| if (index.isInstanceOf[TahoeLogFileIndex]) { | ||
| return None | ||
| } | ||
|
|
||
| if (fileFormat.hasTablePath) { | ||
| return None | ||
| } | ||
|
|
||
| val filesWithDVs = index | ||
| .matchingFiles(partitionFilters = Seq(TrueLiteral), dataFilters = Seq(TrueLiteral)) | ||
| .filter(_.deletionVector != null) | ||
| if (filesWithDVs.isEmpty) { | ||
| return None | ||
| } | ||
|
|
||
| val planOutput = scan.output | ||
| val spark = SparkSession.getActiveSession.get | ||
| val newScan = createScanWithSkipRowColumn(spark, scan, fileFormat, index, hadoopRelation) | ||
| val rowIndexFilter = createRowIndexFilterNode(newScan) | ||
| Some(Project(planOutput, rowIndexFilter)) | ||
| } | ||
|
|
||
| def dvEnabledScanFor( | ||
| scan: LogicalRelation, | ||
| hadoopRelation: HadoopFsRelation, | ||
| fileFormat: GlutenDeltaParquetFileFormat, | ||
| index: TahoeFileIndex): Option[LogicalPlan] = { | ||
| if (!deletionVectorsReadable(index.protocol, index.metadata)) { | ||
| return None | ||
| } | ||
|
|
||
| if (index.isInstanceOf[TahoeLogFileIndex]) { | ||
| return None | ||
| } | ||
|
|
||
| if (fileFormat.hasTablePath) { | ||
| return None | ||
| } | ||
|
|
||
| val filesWithDVs = index | ||
| .matchingFiles(partitionFilters = Seq(TrueLiteral), dataFilters = Seq(TrueLiteral)) | ||
| .filter(_.deletionVector != null) | ||
| if (filesWithDVs.isEmpty) { | ||
| return None | ||
| } | ||
|
|
||
| val planOutput = scan.output | ||
| val spark = SparkSession.getActiveSession.get | ||
| val newScan = createScanWithSkipRowColumn(spark, scan, fileFormat, index, hadoopRelation) | ||
| val rowIndexFilter = createRowIndexFilterNode(newScan) | ||
| Some(Project(planOutput, rowIndexFilter)) | ||
| } | ||
|
|
||
| private def addRowIndexIfMissing(attribute: AttributeReference): AttributeReference = { | ||
| require(attribute.name == METADATA_NAME) | ||
|
|
||
| val dataType = attribute.dataType.asInstanceOf[StructType] | ||
| if (dataType.fieldNames.contains(ParquetFileFormat.ROW_INDEX)) { | ||
| return attribute | ||
| } | ||
|
|
||
| val newDatatype = dataType.add(ParquetFileFormat.ROW_INDEX_FIELD) | ||
| attribute.copy(dataType = newDatatype)( | ||
| exprId = attribute.exprId, | ||
| qualifier = attribute.qualifier) | ||
| } | ||
|
|
||
| private def createScanWithSkipRowColumn( | ||
| spark: SparkSession, | ||
| inputScan: LogicalRelation, | ||
| fileFormat: DeltaParquetFileFormat, | ||
| tahoeFileIndex: TahoeFileIndex, | ||
| hadoopFsRelation: HadoopFsRelation): LogicalRelation = { | ||
| val useMetadataRowIndex = | ||
| spark.sessionState.conf.getConf(DeltaSQLConf.DELETION_VECTORS_USE_METADATA_ROW_INDEX) | ||
|
|
||
| val skipRowField = IS_ROW_DELETED_STRUCT_FIELD | ||
| val scanOutputWithMetadata = if (useMetadataRowIndex) { | ||
| if (inputScan.output.map(_.name).contains(METADATA_NAME)) { | ||
| inputScan.output.collect { | ||
| case a: AttributeReference if a.name == METADATA_NAME => addRowIndexIfMissing(a) | ||
| case o => o | ||
| } | ||
| } else { | ||
| inputScan.output :+ fileFormat.createFileMetadataCol() | ||
| } | ||
| } else { | ||
| inputScan.output | ||
| } | ||
|
|
||
| val newScanOutput = | ||
| scanOutputWithMetadata :+ AttributeReference(skipRowField.name, skipRowField.dataType)() | ||
| val newDataSchema = hadoopFsRelation.dataSchema.add(skipRowField) | ||
| val newFileFormat = fileFormat.copyWithDVInfo( | ||
| tablePath = tahoeFileIndex.path.toString, | ||
| optimizationsEnabled = useMetadataRowIndex) | ||
|
|
||
| val newRelation = hadoopFsRelation.copy(fileFormat = newFileFormat, dataSchema = newDataSchema)( | ||
| hadoopFsRelation.sparkSession) | ||
|
|
||
| inputScan.copy(relation = newRelation, output = newScanOutput) | ||
| } | ||
|
|
||
| private def createScanWithSkipRowColumn( | ||
| spark: SparkSession, | ||
| inputScan: LogicalRelation, | ||
| fileFormat: GlutenDeltaParquetFileFormat, | ||
| tahoeFileIndex: TahoeFileIndex, | ||
| hadoopFsRelation: HadoopFsRelation): LogicalRelation = { | ||
| val useMetadataRowIndex = | ||
| spark.sessionState.conf.getConf(DeltaSQLConf.DELETION_VECTORS_USE_METADATA_ROW_INDEX) | ||
|
|
||
| val skipRowField = GlutenDeltaParquetFileFormat.IS_ROW_DELETED_STRUCT_FIELD | ||
| val scanOutputWithMetadata = if (useMetadataRowIndex) { | ||
| if (inputScan.output.map(_.name).contains(METADATA_NAME)) { | ||
| inputScan.output.collect { | ||
| case a: AttributeReference if a.name == METADATA_NAME => addRowIndexIfMissing(a) | ||
| case o => o | ||
| } | ||
| } else { | ||
| inputScan.output :+ fileFormat.createFileMetadataCol() | ||
| } | ||
| } else { | ||
| inputScan.output | ||
| } | ||
|
|
||
| val newScanOutput = | ||
| scanOutputWithMetadata :+ AttributeReference(skipRowField.name, skipRowField.dataType)() | ||
| val newDataSchema = hadoopFsRelation.dataSchema.add(skipRowField) | ||
| val newFileFormat = fileFormat.copyWithDVInfo( | ||
| tablePath = tahoeFileIndex.path.toString, | ||
| optimizationsEnabled = useMetadataRowIndex) | ||
|
|
||
| val newRelation = hadoopFsRelation.copy(fileFormat = newFileFormat, dataSchema = newDataSchema)( | ||
| hadoopFsRelation.sparkSession) | ||
|
|
||
| inputScan.copy(relation = newRelation, output = newScanOutput) | ||
| } | ||
|
|
||
| private def createRowIndexFilterNode(newScan: LogicalRelation): Filter = { | ||
| val skipRowColumnRefs = newScan.output.filter(_.name == IS_ROW_DELETED_COLUMN_NAME) | ||
| require( | ||
| skipRowColumnRefs.size == 1, | ||
| s"Expected only one column with name=$IS_ROW_DELETED_COLUMN_NAME") | ||
| val skipRowColumnRef = skipRowColumnRefs.head | ||
| Filter(EqualTo(skipRowColumnRef, Literal(RowIndexFilter.KEEP_ROW_VALUE)), newScan) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add an example for this rule in the class scaladoc, E.g., Input plan shape / output plan shape for this rule.
I might miss something, but provided we have native mask-based filter in Velox, why we still add a logical filter node on the output side of the scan?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't miss anything @zhztheplayer , I dug into this and the filter turned out to be unnecessary, so I just removed it.
What I found:
injectOptimizerRulerules run beforePrepareDeltaScan(pre-CBO), so when this rule ran the scan still had aTahoeLogFileIndexand it bailed out — it was basically a no-op. The DV shape we actually see comes from Delta's ownPreprocessTableWithDVsStrategyat planning time. From there, if the scan offloads,nativeDeletionVectorRulestrips the skip-row column/filter and Velox does the filtering natively with the per-file mask. If it doesn't offload (useMetadataRowIndex=false, Spark < 3.5, or validation fails), the vanilla filter just runs on the JVM like before.Since the class is gone I put the input/output plan example on
nativeDeletionVectorRuleinstead, that's where the rewrite actually happens now. Nice bonus: this also removes theGlutenPreprocessTableWithDVscopy from the shadowing discussion, so Delta 3.3 works the same way as Delta 4.0 in this PR now.