-
Notifications
You must be signed in to change notification settings - Fork 28.8k
[SPARK-53444] Rework execute immediate #52173
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?
Conversation
2d1615a
to
65b31dd
Compare
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
Outdated
Show resolved
Hide resolved
sql/core/src/test/resources/sql-tests/inputs/execute-immediate.sql
Outdated
Show resolved
Hide resolved
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionStateBuilder.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ExecuteImmediateCommands.scala
Outdated
Show resolved
Hide resolved
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveSessionStateBuilder.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/parameters.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
Show resolved
Hide resolved
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisErrorSuite.scala
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ExecuteImmediateCommands.scala
Outdated
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ExecuteImmediateCommands.scala
Outdated
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ExecuteImmediateCommands.scala
Outdated
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/catalyst/analysis/ExecuteImmediateCommands.scala
Outdated
Show resolved
Hide resolved
sql/core/src/main/scala/org/apache/spark/sql/classic/SparkSession.scala
Outdated
Show resolved
Hide resolved
Please enter the commit message for your changes. Lines starting
sql/connect/common/src/main/scala/org/apache/spark/sql/connect/SparkSession.scala
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Outdated
Show resolved
Hide resolved
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala
Show resolved
Hide resolved
// Check for duplicate variable names (same logic as ResolveSetVariable) | ||
val dups = finalTargetVars.groupBy(_.identifier).filter(kv => kv._2.length > 1) | ||
if (dups.nonEmpty) { | ||
throw new AnalysisException( | ||
errorClass = "DUPLICATE_ASSIGNMENTS", | ||
messageParameters = Map("nameList" -> | ||
dups.keys.map(key => toSQLId(key.name())).mkString(", "))) | ||
} |
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.
Can we leave this check to the generated SetVariable
? If so we would avoid this code duplication
val plan = tracker.measurePhase(QueryPlanningTracker.PARSING) { | ||
val parsedPlan = sessionState.sqlParser.parsePlan(sqlText) | ||
if (args.nonEmpty) { | ||
if (parsedPlan.isInstanceOf[CompoundBody]) { |
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 don't understand how this is fixed. When we call this method, we need to check whether positional parameters are supplied to a SQL script. Here we only check args.nonEmpty
, which will be true whether we have positional or named paremeters. In effect, SQL scripts will regress to no longer support named parameters, as this error will be thrown every time.
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.
Outdated. Please check again. Sorry about that
What changes were proposed in this pull request?
This PR significantly reworks the existing
EXECUTE IMMEDIATE
SQL statement implementation in to improve separation of concerns, robustness, and functionality:Architecture Redesign:
sparkSession.sql()
Enhanced Query String Support:
Improved Parameter Handling:
UNSUPPORTED_EXPR_FOR_PARAMETER
for invalid expressionsRobust SQL Scripting Integration:
EXECUTE IMMEDIATE
callsEXECUTE IMMEDIATE
Enhanced Error Handling:
Code Quality Improvements:
Open issues:
The code testing whether a query uses named or positional parameter markers is not robust yet and needs more work.
Why are the changes needed?
The existing implementation has been brittle and could not handle transitive closure (e.g. nested EXECUTE IMMEDIATE and SQL Scripting in EXECUTE IMMEDIATE.
There is also a follow on PR dealing with improved parameter substitution which is being blocked from supporting EXECUTE IMMEDIATE until this PR is delivered.
Lastly with this change we lay the foundation to support nesting/chaining of errors contexts to SQL level stacks.
Does this PR introduce any user-facing change?
Enhanced Functionality (Backward Compatible):
A couple of error conditions now return more general codes due to improved orthogonality.
All existing valid
EXECUTE IMMEDIATE
statements continue to work as before.How was this patch tested?
Regression Testing:
EXECUTE IMMEDIATE
tests continue to passEnhanced Test Coverage:
Integration Testing:
SqlScriptingExecutionSuite
tests for SQL scripting integrationexecute-immediate.sql
) for analyzer and execution resultsError Handling Validation:
The reworked implementation passes all existing tests plus extensive new test coverage, ensuring both backward compatibility and enhanced functionality.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude 3.5 Sonnet (Anthropic) - Used for code review, refactoring suggestions, and implementation guidance. All core architectural decisions, SQL semantics, and implementation logic were designed and developed by human developers.