dongjoon-hyun commented on code in PR #60647:
URL: https://github.com/apache/spark/pull/60647#discussion_r4609860737


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/constraints.scala:
##########
@@ -466,6 +466,9 @@ case class CheckInvariant(
   override def children: Seq[Expression] = child +: columnExtractors.map(_._2)
   override def dataType: DataType = BooleanType
   override def nullable: Boolean = false
+  // A violating row fails the write, so the optimizer must not evaluate this 
on rows that are
+  // not written, e.g. by pushing it below a join.
+  override lazy val throwable: Boolean = true

Review Comment:
   Thank you. Confirmed. When the write reads every column of the lower join, 
no Project separates the joins, so `ReorderJoin` flattens them and puts the 
check into the condition of the lower join. `CostBasedJoinReorder` then drops 
the check, and a violating row is written without an error. The root cause is 
that `ReorderJoin` moves a throwable condition into a join condition, which 
#60649 (SPARK-61930) fixes by keeping such a condition in the `Filter` above 
the reordered joins. With #60649, the same write fails with 
`CHECK_CONSTRAINT_VIOLATION` with and without cost-based join reordering. This 
also covers the P2 finding about writes over three or more inner-joined inputs.
   
   So this PR should go after #60649. After #60649 is merged, I will rebase 
this PR and add a CHECK constraint test over three inner-joined inputs with 
cost-based join reordering on.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/subquery.scala:
##########
@@ -149,15 +149,18 @@ object RewritePredicateSubquery extends Rule[LogicalPlan] 
with PredicateHelper {
       val (withSubquery, withoutSubquery) =
         splitConjunctivePredicates(condition)
           .partition(SubqueryExpression.hasInOrCorrelatedExistsSubquery)
+      // A throwable conjunct stays above the joins that the subqueries are 
rewritten to, so that
+      // it is not evaluated on the rows they remove.
+      val (stayUp, pushDown) = withoutSubquery.partition(_.throwable)

Review Comment:
   Thank you. Confirmed with your examples. Addressed in the latest commit. 
`RewritePredicateSubquery` now lifts only the deterministic, throwable 
conjuncts at the end of the filter, after every subquery predicate and every 
other condition. The other conjuncts stay below the joins in their written 
order, as before. This is narrower than the suggested rule, because a throwable 
conjunct followed by a condition that cannot fail is not lifted either. The 
CHECK constraints of a write are still lifted, because the scan pushdown merges 
them after the conditions of the source's filter. I added test cases to 
`RewriteSubquerySuite` and `SubquerySuite`, and removed the `raise_error` 
statement from the PR description.
   



-- 
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]

Reply via email to