-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-50820][SQL] DSv2: Conditional nullification of metadata columns in DML #49493
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
Closed
Closed
Changes from all commits
Commits
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -19,24 +19,32 @@ package org.apache.spark.sql.catalyst.analysis | |
|
|
||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.catalyst.ProjectingInternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, AttributeSet, Expression, ExprId, Literal, V2ExpressionUtils} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Assignment, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeReference, AttributeSet, Expression, ExprId, Literal, MetadataAttribute, NamedExpression, V2ExpressionUtils} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Assignment, Expand, LogicalPlan, MergeRows, Project} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.types.DataTypeUtils | ||
| import org.apache.spark.sql.catalyst.util.{ReplaceDataProjections, WriteDeltaProjections} | ||
| import org.apache.spark.sql.catalyst.util.RowDeltaUtils._ | ||
| import org.apache.spark.sql.catalyst.util.WriteDeltaProjections | ||
| import org.apache.spark.sql.connector.catalog.SupportsRowLevelOperations | ||
| import org.apache.spark.sql.connector.expressions.FieldReference | ||
| import org.apache.spark.sql.connector.write.{RowLevelOperation, RowLevelOperationInfoImpl, RowLevelOperationTable, SupportsDelta} | ||
| import org.apache.spark.sql.connector.write.RowLevelOperation.Command | ||
| import org.apache.spark.sql.errors.QueryCompilationErrors | ||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
| import org.apache.spark.sql.types.{IntegerType, StructField, StructType} | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
||
| trait RewriteRowLevelCommand extends Rule[LogicalPlan] { | ||
|
|
||
| private final val DELTA_OPERATIONS_WITH_ROW = | ||
| Set(UPDATE_OPERATION, REINSERT_OPERATION, INSERT_OPERATION) | ||
| private final val DELTA_OPERATIONS_WITH_METADATA = | ||
| Set(DELETE_OPERATION, UPDATE_OPERATION, REINSERT_OPERATION) | ||
| private final val DELTA_OPERATIONS_WITH_ROW_ID = | ||
| Set(DELETE_OPERATION, UPDATE_OPERATION) | ||
|
|
||
| protected def buildOperationTable( | ||
| table: SupportsRowLevelOperations, | ||
| command: Command, | ||
|
|
@@ -103,7 +111,31 @@ trait RewriteRowLevelCommand extends Rule[LogicalPlan] { | |
| metadataAttrs: Seq[Attribute], | ||
| originalRowIdValues: Seq[Expression] = Seq.empty): Seq[Expression] = { | ||
| val rowValues = buildDeltaDeleteRowValues(rowAttrs, rowIdAttrs) | ||
| Seq(Literal(DELETE_OPERATION)) ++ rowValues ++ metadataAttrs ++ originalRowIdValues | ||
| val metadataValues = nullifyMetadataOnDelete(metadataAttrs) | ||
| Seq(Literal(DELETE_OPERATION)) ++ rowValues ++ metadataValues ++ originalRowIdValues | ||
| } | ||
|
|
||
| protected def nullifyMetadataOnDelete(attrs: Seq[Attribute]): Seq[NamedExpression] = { | ||
| nullifyMetadata(attrs, MetadataAttribute.isPreservedOnDelete) | ||
| } | ||
|
|
||
| protected def nullifyMetadataOnUpdate(attrs: Seq[Attribute]): Seq[NamedExpression] = { | ||
| nullifyMetadata(attrs, MetadataAttribute.isPreservedOnUpdate) | ||
| } | ||
|
|
||
| private def nullifyMetadataOnReinsert(attrs: Seq[Attribute]): Seq[NamedExpression] = { | ||
| nullifyMetadata(attrs, MetadataAttribute.isPreservedOnReinsert) | ||
| } | ||
|
|
||
| private def nullifyMetadata( | ||
| attrs: Seq[Attribute], | ||
| shouldPreserve: Attribute => Boolean): Seq[NamedExpression] = { | ||
| attrs.map { | ||
| case MetadataAttribute(attr) if !shouldPreserve(attr) => | ||
| Alias(Literal(null, attr.dataType), attr.name)(explicitMetadata = Some(attr.metadata)) | ||
| case attr => | ||
| attr | ||
| } | ||
| } | ||
|
|
||
| private def buildDeltaDeleteRowValues( | ||
|
|
@@ -132,52 +164,119 @@ trait RewriteRowLevelCommand extends Rule[LogicalPlan] { | |
| metadataAttrs: Seq[Attribute], | ||
| originalRowIdValues: Seq[Expression]): Seq[Expression] = { | ||
| val rowValues = assignments.map(_.value) | ||
| Seq(Literal(UPDATE_OPERATION)) ++ rowValues ++ metadataAttrs ++ originalRowIdValues | ||
| val metadataValues = nullifyMetadataOnUpdate(metadataAttrs) | ||
| Seq(Literal(UPDATE_OPERATION)) ++ rowValues ++ metadataValues ++ originalRowIdValues | ||
| } | ||
|
|
||
| protected def deltaReinsertOutput( | ||
| assignments: Seq[Assignment], | ||
| metadataAttrs: Seq[Attribute], | ||
| originalRowIdValues: Seq[Expression] = Seq.empty): Seq[Expression] = { | ||
| val rowValues = assignments.map(_.value) | ||
| val metadataValues = nullifyMetadataOnReinsert(metadataAttrs) | ||
| val extraNullValues = originalRowIdValues.map(e => Literal(null, e.dataType)) | ||
| Seq(Literal(REINSERT_OPERATION)) ++ rowValues ++ metadataValues ++ extraNullValues | ||
| } | ||
|
|
||
| protected def addOperationColumn(operation: Int, plan: LogicalPlan): LogicalPlan = { | ||
| val operationType = Alias(Literal(operation, IntegerType), OPERATION_COLUMN)() | ||
| Project(operationType +: plan.output, plan) | ||
| } | ||
|
|
||
| protected def buildReplaceDataProjections( | ||
| plan: LogicalPlan, | ||
| rowAttrs: Seq[Attribute], | ||
| metadataAttrs: Seq[Attribute]): ReplaceDataProjections = { | ||
| val outputs = extractOutputs(plan) | ||
|
|
||
| val outputsWithRow = filterOutputs(outputs, Set(WRITE_WITH_METADATA_OPERATION, WRITE_OPERATION)) | ||
| val rowProjection = newLazyProjection(plan, outputsWithRow, rowAttrs) | ||
|
|
||
| val metadataProjection = if (metadataAttrs.nonEmpty) { | ||
| val outputsWithMetadata = filterOutputs(outputs, Set(WRITE_WITH_METADATA_OPERATION)) | ||
| Some(newLazyProjection(plan, outputsWithMetadata, metadataAttrs)) | ||
| } else { | ||
| None | ||
| } | ||
|
|
||
| ReplaceDataProjections(rowProjection, metadataProjection) | ||
| } | ||
|
|
||
| protected def buildWriteDeltaProjections( | ||
| plan: LogicalPlan, | ||
| rowAttrs: Seq[Attribute], | ||
| rowIdAttrs: Seq[Attribute], | ||
| metadataAttrs: Seq[Attribute]): WriteDeltaProjections = { | ||
| val outputs = extractOutputs(plan) | ||
|
|
||
| val rowProjection = if (rowAttrs.nonEmpty) { | ||
| Some(newLazyProjection(plan, rowAttrs)) | ||
| val outputsWithRow = filterOutputs(outputs, DELTA_OPERATIONS_WITH_ROW) | ||
| Some(newLazyProjection(plan, outputsWithRow, rowAttrs)) | ||
| } else { | ||
| None | ||
| } | ||
|
|
||
| val rowIdProjection = newLazyRowIdProjection(plan, rowIdAttrs) | ||
| val outputsWithRowId = filterOutputs(outputs, DELTA_OPERATIONS_WITH_ROW_ID) | ||
| val rowIdProjection = newLazyRowIdProjection(plan, outputsWithRowId, rowIdAttrs) | ||
|
|
||
| val metadataProjection = if (metadataAttrs.nonEmpty) { | ||
| Some(newLazyProjection(plan, metadataAttrs)) | ||
| val outputsWithMetadata = filterOutputs(outputs, DELTA_OPERATIONS_WITH_METADATA) | ||
| Some(newLazyProjection(plan, outputsWithMetadata, metadataAttrs)) | ||
| } else { | ||
| None | ||
| } | ||
|
|
||
| WriteDeltaProjections(rowProjection, rowIdProjection, metadataProjection) | ||
| } | ||
|
|
||
| private def extractOutputs(plan: LogicalPlan): Seq[Seq[Expression]] = { | ||
| plan match { | ||
| case p: Project => Seq(p.projectList) | ||
| case e: Expand => e.projections | ||
| case m: MergeRows => m.outputs | ||
| case _ => throw SparkException.internalError("Can't extract outputs from plan: " + plan) | ||
| } | ||
| } | ||
|
|
||
| private def filterOutputs( | ||
| outputs: Seq[Seq[Expression]], | ||
| operations: Set[Int]): Seq[Seq[Expression]] = { | ||
| outputs.filter { | ||
| case Literal(operation: Integer, _) +: _ => operations.contains(operation) | ||
| case Alias(Literal(operation: Integer, _), _) +: _ => operations.contains(operation) | ||
| case other => throw SparkException.internalError("Can't determine operation: " + other) | ||
| } | ||
| } | ||
|
|
||
| private def newLazyProjection( | ||
| plan: LogicalPlan, | ||
| outputs: Seq[Seq[Expression]], | ||
| attrs: Seq[Attribute]): ProjectingInternalRow = { | ||
|
|
||
| val colOrdinals = attrs.map(attr => findColOrdinal(plan, attr.name)) | ||
| val schema = DataTypeUtils.fromAttributes(attrs) | ||
| ProjectingInternalRow(schema, colOrdinals) | ||
| createProjectingInternalRow(outputs, colOrdinals, attrs) | ||
| } | ||
|
|
||
| // if there are assignment to row ID attributes, original values are projected as special columns | ||
| // this method honors such special columns if present | ||
| private def newLazyRowIdProjection( | ||
| plan: LogicalPlan, | ||
| outputs: Seq[Seq[Expression]], | ||
| rowIdAttrs: Seq[Attribute]): ProjectingInternalRow = { | ||
|
|
||
| val colOrdinals = rowIdAttrs.map { attr => | ||
| val originalValueIndex = findColOrdinal(plan, ORIGINAL_ROW_ID_VALUE_PREFIX + attr.name) | ||
| if (originalValueIndex != -1) originalValueIndex else findColOrdinal(plan, attr.name) | ||
| } | ||
| val schema = DataTypeUtils.fromAttributes(rowIdAttrs) | ||
| createProjectingInternalRow(outputs, colOrdinals, rowIdAttrs) | ||
| } | ||
|
|
||
| private def createProjectingInternalRow( | ||
| outputs: Seq[Seq[Expression]], | ||
| colOrdinals: Seq[Int], | ||
| attrs: Seq[Attribute]): ProjectingInternalRow = { | ||
| val schema = StructType(attrs.zipWithIndex.map { case (attr, index) => | ||
| val nullable = outputs.exists(output => output(colOrdinals(index)).nullable) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aokolnychyi Do we only need this for metadata columns? For regular columns, shall we use |
||
| StructField(attr.name, attr.dataType, nullable, attr.metadata) | ||
| }) | ||
| ProjectingInternalRow(schema, colOrdinals) | ||
| } | ||
|
|
||
|
|
||
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.
Follows exactly what we have in
Column.