fix: EXPLAIN CREATE/DROP TABLE to validate table existence (#27192)#27217
Open
garimauttam wants to merge 6 commits intoprestodb:masterfrom
Open
fix: EXPLAIN CREATE/DROP TABLE to validate table existence (#27192)#27217garimauttam wants to merge 6 commits intoprestodb:masterfrom
garimauttam wants to merge 6 commits intoprestodb:masterfrom
Conversation
Contributor
Reviewer's GuideExplainRewrite now validates metadata for EXPLAIN CREATE/DROP TABLE statements, throwing appropriate semantic errors when table existence constraints are violated, and a new test suite verifies the behavior for IF EXISTS/IF NOT EXISTS combinations. Sequence diagram for EXPLAIN CREATE/DROP TABLE validation in ExplainRewritesequenceDiagram
actor User
participant Engine
participant ExplainRewrite
participant Visitor
participant Metadata
participant MetadataResolver
User->>Engine: submit EXPLAIN CREATE/DROP TABLE statement
Engine->>ExplainRewrite: rewrite(session, metadata, parser, queryExplainer, procedureRegistry, warningCollector, query, viewDefinitionReferences)
ExplainRewrite->>Visitor: new Visitor(session, metadata, parser, queryExplainer, procedureRegistry, warningCollector, query, viewDefinitionReferences)
ExplainRewrite->>Visitor: process(Explain)
Visitor->>Visitor: visitExplain(Explain)
Visitor->>Visitor: validateTableExistence(innerStatement)
alt CREATE TABLE without IF NOT EXISTS
Visitor->>Metadata: getMetadataResolver(session)
Metadata-->>Visitor: MetadataResolver
Visitor->>MetadataResolver: getTableHandle(tableName)
MetadataResolver-->>Visitor: Optional<TableHandle>
alt table exists
Visitor->>Visitor: throw SemanticException(TABLE_ALREADY_EXISTS)
else table does not exist
Visitor->>Visitor: continue processing
end
else DROP TABLE without IF EXISTS
Visitor->>Metadata: getMetadataResolver(session)
Metadata-->>Visitor: MetadataResolver
Visitor->>MetadataResolver: getTableHandle(tableName)
MetadataResolver-->>Visitor: Optional<TableHandle>
alt table handle absent
Visitor->>Visitor: throw SemanticException(MISSING_TABLE)
else table handle present
Visitor->>Visitor: continue processing
end
else IF NOT EXISTS or IF EXISTS specified
Visitor->>Visitor: skip validation
end
Visitor-->>ExplainRewrite: rewritten Explain statement or error
ExplainRewrite-->>Engine: rewritten statement or error
Engine-->>User: EXPLAIN plan or error response
Class diagram for ExplainRewrite Visitor and table existence validationclassDiagram
class ExplainRewrite {
+Statement rewrite(Session session, String query, ViewDefinitionReferences viewDefinitionReferences)
}
class Visitor {
-Session session
-Metadata metadata
-BuiltInQueryPreparer queryPreparer
-Optional_QueryExplainer queryExplainer
-WarningCollector warningCollector
-String query
-ViewDefinitionReferences viewDefinitionReferences
+Visitor(Session session, Metadata metadata, SqlParser parser, Optional_QueryExplainer queryExplainer, ProcedureRegistry procedureRegistry, WarningCollector warningCollector, String query, ViewDefinitionReferences viewDefinitionReferences)
+Node process(Node node, Void context)
+Node visitExplain(Explain node, Void context)
-void validateTableExistence(Statement statement)
}
class Session
class Metadata {
+MetadataResolver getMetadataResolver(Session session)
}
class MetadataResolver {
+Optional_TableHandle getTableHandle(QualifiedObjectName tableName)
}
class Statement
class Explain {
+Statement getStatement()
+boolean isAnalyze()
+boolean isVerbose()
+Map_ExplainOption_Expression getOptions()
}
class CreateTable {
+QualifiedName getName()
+boolean isNotExists()
}
class DropTable {
+QualifiedName getTableName()
+boolean isExists()
}
class QualifiedObjectName
class TableHandle
class SemanticException
class SqlParser
class BuiltInQueryPreparer
class QueryExplainer
class Optional_QueryExplainer
class Optional_TableHandle
class ProcedureRegistry
class WarningCollector
class ViewDefinitionReferences
class Node
ExplainRewrite ..> Visitor : uses
Visitor ..|> AstVisitor_Node_Void
class AstVisitor_Node_Void
Visitor o--> Session
Visitor o--> Metadata
Visitor o--> BuiltInQueryPreparer
Visitor o--> Optional_QueryExplainer
Visitor o--> WarningCollector
Visitor o--> ViewDefinitionReferences
Visitor --> Explain : visitExplain
Visitor --> Statement : validateTableExistence
validateTableExistence ..> CreateTable : checks
validateTableExistence ..> DropTable : checks
Metadata --> MetadataResolver : returns
MetadataResolver --> Optional_TableHandle : returns
SemanticException <.. Visitor : thrown
Statement <|-- Explain
Statement <|-- CreateTable
Statement <|-- DropTable
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
visitExplain, consider restrictingvalidateTableExistenceto only run when the inner statement is aCreateTableorDropTablebefore calling it (e.g., with aninstanceofguard at the call site) to avoid an extra method call and branching on every EXPLAIN statement. - In
visitExplain, you already bindStatement innerStatement = node.getStatement();but still callprocess(node.getStatement(), context)in the analyze branch; usinginnerStatementthere would avoid re-fetching the same value and keep the method slightly clearer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `visitExplain`, consider restricting `validateTableExistence` to only run when the inner statement is a `CreateTable` or `DropTable` before calling it (e.g., with an `instanceof` guard at the call site) to avoid an extra method call and branching on every EXPLAIN statement.
- In `visitExplain`, you already bind `Statement innerStatement = node.getStatement();` but still call `process(node.getStatement(), context)` in the analyze branch; using `innerStatement` there would avoid re-fetching the same value and keep the method slightly clearer.
## Individual Comments
### Comment 1
<location path="presto-tests/src/test/java/com/facebook/presto/tests/TestExplainCreateDropTable.java" line_range="51-60" />
<code_context>
+ assertUpdate("DROP TABLE IF EXISTS test_new_table");
+ }
+
+ @Test
+ public void testExplainCreateTableAlreadyExists()
+ {
+ // EXPLAIN CREATE TABLE should fail when table already exists
+ assertQueryFails(
+ "EXPLAIN CREATE TABLE test_explain_table (id INTEGER)",
+ ".*Table.*test_explain_table.*already exists.*");
+ }
+
+ @Test
+ public void testExplainCreateTableIfNotExistsAlreadyExists()
+ {
+ // EXPLAIN CREATE TABLE IF NOT EXISTS should succeed even if table exists
</code_context>
<issue_to_address>
**suggestion (testing):** Add a test for `EXPLAIN CREATE TABLE IF NOT EXISTS` when the table does not already exist
We currently only cover the case where the table already exists. Please also add an assertion that `EXPLAIN CREATE TABLE IF NOT EXISTS test_new_table (id INTEGER)` succeeds when `test_new_table` does not exist, to confirm the new existence-checking logic preserves behavior in that scenario.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
presto-tests/src/test/java/com/facebook/presto/tests/TestExplainCreateDropTable.java
Show resolved
Hide resolved
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #27192 - EXPLAIN CREATE/DROP TABLE now properly validates table existence.
Problem
EXPLAIN CREATE TABLEsucceeds even when table already existsEXPLAIN DROP TABLEsucceeds even when table doesn't existChanges
ExplainRewrite.javato validate table existence before processing EXPLAIN statementsvalidateTableExistence()method that checks table metadatainnerStatementvariable for better code clarityTABLE_ALREADY_EXISTSorMISSING_TABLETesting
Added comprehensive test coverage in
TestExplainCreateDropTable.javawith 8 test cases:CREATE TABLE scenarios:
DROP TABLE scenarios:
Performance Considerations
Files Changed
presto-main-base/src/main/java/com/facebook/presto/sql/rewrite/ExplainRewrite.javapresto-tests/src/test/java/com/facebook/presto/tests/TestExplainCreateDropTable.java(new)== NO RELEASE NOTE ==