-
Notifications
You must be signed in to change notification settings - Fork 28.5k
[SPARK-51984][SQL] Support Check Constraint enforcement in UpdateTable and MergeIntoTable #50943
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
base: master
Are you sure you want to change the base?
Changes from all commits
508abe3
d800925
68e347b
422f392
3561338
0fe90b9
1e35ef4
6ab6142
ff9f94c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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 org.apache.spark.sql.catalyst.SQLConfHelper | ||
import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project} | ||
import org.apache.spark.sql.connector.catalog.CatalogManager | ||
|
||
|
||
/** | ||
* A virtual rule to resolve [[UnresolvedAttribute]] in [[Filter]]. It's only used by the real | ||
* rules `ResolveReferences` and `ResolveTableConstraints`. Filters containing unresolved stars | ||
* should have been expanded before applying this rule. | ||
* Filter can host both grouping expressions/aggregate functions and missing attributes. | ||
* The grouping expressions/aggregate functions resolution takes precedence over missing | ||
* attributes. See the classdoc of `ResolveReferences` for details. | ||
*/ | ||
class ResolveReferencesInFilter(val catalogManager: CatalogManager) | ||
extends SQLConfHelper with ColumnResolutionHelper { | ||
def apply(f: Filter): LogicalPlan = { | ||
if (f.condition.resolved && f.missingInput.isEmpty) { | ||
return f | ||
} | ||
val resolvedBasic = resolveExpressionByPlanChildren(f.condition, f) | ||
val resolvedWithAgg = resolveColWithAgg(resolvedBasic, f.child) | ||
val (newCond, newChild) = resolveExprsAndAddMissingAttrs(Seq(resolvedWithAgg), f.child) | ||
// Missing columns should be resolved right after basic column resolution. | ||
// See the doc of `ResolveReferences`. | ||
val resolvedFinal = resolveColsLastResort(newCond.head) | ||
if (f.child.output == newChild.output) { | ||
f.copy(condition = resolvedFinal) | ||
} else { | ||
// Add missing attributes and then project them away. | ||
val newFilter = Filter(resolvedFinal, newChild) | ||
Project(f.child.output, newFilter) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,38 +22,58 @@ import org.apache.spark.sql.catalyst.expressions.{And, CheckInvariant, Expressio | |
import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, V2WriteCommand} | ||
import org.apache.spark.sql.catalyst.rules.Rule | ||
import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND | ||
import org.apache.spark.sql.connector.catalog.CatalogManager | ||
import org.apache.spark.sql.connector.catalog.{CatalogManager, Table} | ||
import org.apache.spark.sql.connector.catalog.constraints.Check | ||
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
|
||
class ResolveTableConstraints(val catalogManager: CatalogManager) extends Rule[LogicalPlan] { | ||
|
||
private val resolveReferencesInFilter = new ResolveReferencesInFilter(catalogManager) | ||
|
||
override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning( | ||
_.containsPattern(COMMAND), ruleId) { | ||
case v2Write: V2WriteCommand | ||
if v2Write.table.resolved && v2Write.query.resolved && | ||
!containsCheckInvariant(v2Write.query) && v2Write.outputResolved => | ||
v2Write.table match { | ||
case r: DataSourceV2Relation | ||
if r.table.constraints != null && r.table.constraints.nonEmpty => | ||
// Check constraint is the only enforced constraint for DSV2 tables. | ||
val checkInvariants = r.table.constraints.collect { | ||
case c: Check => | ||
val unresolvedExpr = buildCatalystExpression(c) | ||
val columnExtractors = mutable.Map[String, Expression]() | ||
buildColumnExtractors(unresolvedExpr, columnExtractors) | ||
CheckInvariant(unresolvedExpr, columnExtractors.toSeq, c.name, c.predicateSql) | ||
} | ||
// Combine the check invariants into a single expression using conjunctive AND. | ||
checkInvariants.reduceOption(And).fold(v2Write)( | ||
condition => v2Write.withNewQuery(Filter(condition, v2Write.query))) | ||
case r: DataSourceV2Relation => | ||
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. The changes in this file is purely code clean up. cc @aokolnychyi |
||
buildCheckCondition(r.table).map { condition => | ||
val filter = Filter(condition, v2Write.query) | ||
// Resolve attribute references in the filter condition only, not the entire query. | ||
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. See https://github.com/gengliangwang/spark/actions/runs/15130563620/job/42530638818 for details.
|
||
// We use a targeted resolver (ResolveReferencesInFilter) instead of the full | ||
// `ResolveReferences` rule to avoid the creation of `TempResolvedColumn` nodes that | ||
// would interfere with the analyzer's ability to correctly identify unresolved | ||
// attributes. | ||
val resolvedFilter = resolveReferencesInFilter(filter) | ||
v2Write.withNewQuery(resolvedFilter) | ||
}.getOrElse(v2Write) | ||
case _ => | ||
v2Write | ||
} | ||
} | ||
|
||
// Constructs an optional check condition based on the table's check constraints. | ||
// This condition validates data during write operations. | ||
// Returns None if no check constraints exist; otherwise, combines all constraints using | ||
// logical AND. | ||
private def buildCheckCondition(table: Table): Option[Expression] = { | ||
if (table.constraints == null || table.constraints.isEmpty) { | ||
None | ||
} else { | ||
val checkInvariants = table.constraints.collect { | ||
// Check constraint is the only enforced constraint for DSV2 tables. | ||
case c: Check => | ||
val unresolvedExpr = buildCatalystExpression(c) | ||
val columnExtractors = mutable.Map[String, Expression]() | ||
buildColumnExtractors(unresolvedExpr, columnExtractors) | ||
CheckInvariant(unresolvedExpr, columnExtractors.toSeq, c.name, c.predicateSql) | ||
} | ||
checkInvariants.reduceOption(And) | ||
} | ||
} | ||
|
||
private def containsCheckInvariant(plan: LogicalPlan): Boolean = { | ||
plan match { | ||
plan exists { | ||
case Filter(condition, _) => | ||
condition.exists(_.isInstanceOf[CheckInvariant]) | ||
|
||
|
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.
Context of why this batch is not in the main resolution batch: fab6d83