Skip to content

[SPARK-48871] Fix INVALID_NON_DETERMINISTIC_EXPRESSIONS validation in… #47304

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB
errorClass, missingCol, orderedCandidates, a.origin)
}

/**
* Checks whether the operator allows non-deterministic expressions.
*/
private def operatorAllowsNonDeterministicExpressions(plan: LogicalPlan): Boolean = {
plan match {
case p: SupportsNonDeterministicExpression =>
p.allowNonDeterministicExpression
case _ => false
}
}

def checkAnalysis(plan: LogicalPlan): Unit = {
// We should inline all CTE relations to restore the original plan shape, as the analysis check
// may need to match certain plan shapes. For dangling CTE relations, they will still be kept
Expand Down Expand Up @@ -718,6 +729,7 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB
"dataType" -> toSQLType(mapCol.dataType)))

case o if o.expressions.exists(!_.deterministic) &&
!operatorAllowsNonDeterministicExpressions(o) &&
!o.isInstanceOf[Project] &&
// non-deterministic expressions inside CollectMetrics have been
// already validated inside checkMetric function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,16 @@ case class DeduplicateWithinWatermark(keys: Seq[Attribute], child: LogicalPlan)
*/
trait SupportsSubquery extends LogicalPlan

/**
* Trait that logical plans can extend to check whether it can allow non-deterministic
* expressions and pass the CheckAnalysis rule.
*/
trait SupportsNonDeterministicExpression extends LogicalPlan {

/** Returns whether it allows non-deterministic expressions. */
def allowNonDeterministicExpression: Boolean
}

/**
* Collect arbitrary (named) metrics from a dataset. As soon as the query reaches a completion
* point (batch query completes or streaming query epoch completes) an event is emitted on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ case class TestFunctionWithTypeCheckFailure(

case class UnresolvedTestPlan() extends UnresolvedLeafNode

case class SupportsNonDeterministicExpressionTestOperator(
actions: Seq[Expression],
allowNonDeterministicExpression: Boolean)
extends LeafNode with SupportsNonDeterministicExpression {
override def output: Seq[Attribute] = Seq()
}

class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
import TestRelations._

Expand Down Expand Up @@ -1364,4 +1371,20 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
messageParameters = Map(
"expr" -> "\"_w0\"",
"exprType" -> "\"MAP<STRING, STRING>\""))

test("SPARK-48871: SupportsNonDeterministicExpression allows non-deterministic expressions") {
val nonDeterministicExpressions = Seq(new Rand())
val tolerantPlan =
SupportsNonDeterministicExpressionTestOperator(
nonDeterministicExpressions, allowNonDeterministicExpression = true)
assertAnalysisSuccess(tolerantPlan)

val intolerantPlan =
SupportsNonDeterministicExpressionTestOperator(
nonDeterministicExpressions, allowNonDeterministicExpression = false)
assertAnalysisError(
intolerantPlan,
"INVALID_NON_DETERMINISTIC_EXPRESSIONS" :: Nil
)
}
}