[SPARK-30260][SQL] Spark-Shell throw ClassNotFoundException exception for more than one statement to use UDF jar #26888
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.
What changes were proposed in this pull request?
When we start spark-shell and use the udf for the first statement ,it's ok. But for the other statements it failed to load jar to current classpath and would throw ClassNotFoundException.It seems like that the first ClassLoader is different from the other's. For Spark-shell, the maintained class loader is always IMainsTranslatingClassLoader ,and for addJar Operation, the current classLoader is NonClosableMutuableclassLoader. For the first statement, there jar was loaded to right classLoader,and for other statements, the jar has been registered to functionRegistry and would not reload to NonClosableMutuableclassLoader, we need to reset classloader to active sparkSession's.
Here, I will show difference between the first statement and the second.

First statement is NonClosableMutuableclassLoader:
Second statement is IMainsTranslatingClassLoader:

Why are the changes needed?
The problem can be reproduced as described in the below.
After fix:
we should resolve this bug!
Does this PR introduce any user-facing change?
No
How was this patch tested?
manual.