-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-48921][SQL] ScalaUDF encoders in subquery should be resolved for MergeInto #47380
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3657b7c
SPARK-48921: ScalaUDF in subquery should run through analyzer
viirya 708b959
check and set analyzed
viirya c90e463
Move ResolveEncodersInUDF rule
viirya 544828d
For review
viirya fd17bd4
Add comment
viirya 0722454
Fix
viirya c93cac5
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/anal…
yaooqinn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,12 @@ package org.apache.spark.sql.catalyst.analysis | |
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, CreateArray, Expression, GetStructField, InSubquery, LambdaFunction, LateralSubquery, ListQuery, OuterReference, ScalarSubquery, UnresolvedNamedLambdaVariable} | ||
| import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
| import org.apache.spark.sql.catalyst.expressions.{Alias, CreateArray, Exists, Expression, GetStructField, InSubquery, LambdaFunction, LateralSubquery, ListQuery, Literal, OuterReference, ScalarSubquery, ScalaUDF, UnresolvedNamedLambdaVariable} | ||
| import org.apache.spark.sql.catalyst.expressions.aggregate.Count | ||
| import org.apache.spark.sql.catalyst.plans.{Inner, JoinType} | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.types.{ArrayType, IntegerType} | ||
| import org.apache.spark.sql.types.{ArrayType, IntegerType, StringType} | ||
|
|
||
| /** | ||
| * Unit tests for [[ResolveSubquery]]. | ||
|
|
@@ -299,4 +300,58 @@ class ResolveSubquerySuite extends AnalysisTest { | |
| "UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.HIGHER_ORDER_FUNCTION", | ||
| expectedMessageParameters = Map.empty[String, String]) | ||
| } | ||
|
|
||
| val testRelation = LocalRelation($"a".int, $"b".double) | ||
| val testRelation2 = LocalRelation($"c".int, $"d".string) | ||
|
|
||
| test("SPARK-48921: ScalaUDF in subquery should run through analyzer") { | ||
|
||
| val integer = testRelation2.output(0) | ||
| val udf = ScalaUDF((_: Int) => "x", StringType, integer :: Nil, | ||
| Option(ExpressionEncoder[Int]()) :: Nil) | ||
|
|
||
| var subPlan = | ||
| testRelation2 | ||
| .where($"d" === udf) | ||
| .select(Literal(1)).analyze | ||
|
|
||
| // Manually remove the resolved encoder from the UDF. | ||
| // This simulates the case where the UDF is resolved during analysis | ||
| // but its encoders are not resolved. | ||
| subPlan = subPlan.transformUp { | ||
| case f: Filter => | ||
| f.copy(condition = f.condition transform { | ||
| case s: ScalaUDF => | ||
| val newUDF = s.copy(inputEncoders = Option(ExpressionEncoder[Int]()) :: Nil) | ||
| assert(newUDF.resolved) | ||
| newUDF | ||
| }) | ||
| } | ||
|
|
||
| val existsSubquery = | ||
| testRelation | ||
| .where(Exists(subPlan)) | ||
| .select($"a").analyze | ||
| assert(existsSubquery.resolved) | ||
|
|
||
| val existPlan = existsSubquery.collect { | ||
| case f: Filter => | ||
| f.condition.collect { | ||
| case e: Exists => | ||
| e.plan.collect { | ||
| case f: Filter => f | ||
| } | ||
| } | ||
| }.flatten.flatten.head | ||
|
|
||
| val udfs = existPlan.expressions.flatMap(e => e.collect { | ||
| case s: ScalaUDF => | ||
| assert(s.inputEncoders.nonEmpty) | ||
| val encoder = s.inputEncoders.head | ||
| assert(encoder.isDefined) | ||
| assert(encoder.get.objDeserializer.resolved) | ||
|
|
||
| s | ||
| }) | ||
| assert(udfs.size == 1) | ||
| } | ||
| } | ||
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.
will we ever set the
analyzedflag to true for plans inSubqueryExpression?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.
Ah, it runs
executeinstead ofexecuteCheck. It will re-enter this every call.Hmm, maybe we should change to
executeCheck?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.
CheckAnalysiswill check subquery expressions recursively, so we shouldn't check it here.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.
shall we mark
ScalaUDFas unresolved if the encoder is not resolved yet?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.
I added a
checkAnalysisafter analysis of the subquery plan now.Uh oh!
There was an error while loading. Please reload this page.
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.
I meant to check sub plan included in the subquery, not the subquery expression itself. It shouldn't be recursive.
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.
I also did it before, but I saw some side effect that causes the MergeInto query to fail. So I removed it before submitting this PR.
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.
It isn't recursive, but
InlineCTE.buildCTEMaphas some issues on it.