fix: Add null check to UtilizedColumnAnalyzer#27083
Merged
auden-woolfson merged 1 commit intoprestodb:masterfrom Feb 25, 2026
Merged
fix: Add null check to UtilizedColumnAnalyzer#27083auden-woolfson merged 1 commit intoprestodb:masterfrom
auden-woolfson merged 1 commit intoprestodb:masterfrom
Conversation
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a null check around output expressions in UtilizedColumnsAnalyzer to avoid NPEs for no-op CREATE TABLE AS SELECT, and emits a semantic warning when CREATE TABLE IF NOT EXISTS is a no-op because the target table already exists. Sequence diagram for CREATE TABLE IF NOT EXISTS no-op handlingsequenceDiagram
actor Client
participant StatementAnalyzer
participant MetadataResolver
participant WarningCollector
Client->>StatementAnalyzer: analyze CreateTableAsSelect
activate StatementAnalyzer
StatementAnalyzer->>MetadataResolver: tableExists(targetTable)
MetadataResolver-->>StatementAnalyzer: boolean exists
alt table exists AND node.isNotExists
StatementAnalyzer->>StatementAnalyzer: analysis.setCreateTableAsSelectNoOp(true)
StatementAnalyzer->>WarningCollector: add(PrestoWarning(SEMANIC_WARNING, "Table '%s' already exists, skipping table creation"))
StatementAnalyzer-->>Client: Scope for rows BIGINT
else table exists AND NOT node.isNotExists
StatementAnalyzer-->>Client: throw SemanticException(TABLE_ALREADY_EXISTS)
else table does not exist
StatementAnalyzer->>StatementAnalyzer: proceed with table creation and query analysis
StatementAnalyzer-->>Client: analysis result
end
deactivate StatementAnalyzer
Class diagram for UtilizedColumnsAnalyzer and StatementAnalyzer changesclassDiagram
class UtilizedColumnsAnalyzer {
- Analysis analysis
+ visitQuerySpecification(QuerySpecification querySpec, Context context) Void
}
class UtilizedColumnsAnalyzer_Context {
+ boolean prunable
+ List~FieldId~ getFieldIdsToExploreInRelation(QuerySpecification querySpec)
}
class FieldId {
+ int getFieldIndex()
}
class Analysis {
+ List~Expression~ getOutputExpressions(QuerySpecification querySpec)
+ void setCreateTableAsSelectNoOp(boolean value)
}
class QuerySpecification
class Expression
UtilizedColumnsAnalyzer --> Analysis : uses
UtilizedColumnsAnalyzer --> UtilizedColumnsAnalyzer_Context : uses
UtilizedColumnsAnalyzer_Context --> FieldId : returns
UtilizedColumnsAnalyzer --> QuerySpecification : analyzes
UtilizedColumnsAnalyzer --> Expression : processes
%% StatementAnalyzer side
class StatementAnalyzer {
- MetadataResolver metadataResolver
- WarningCollector warningCollector
+ visitCreateTableAsSelect(CreateTableAsSelect node, Optional_Scope scope) Scope
}
class MetadataResolver {
+ boolean tableExists(QualifiedObjectName table)
}
class WarningCollector {
+ void add(PrestoWarning warning)
}
class PrestoWarning {
+ PrestoWarning(StandardWarningCode code, String message)
}
class StandardWarningCode {
<<enum>>
SEMANTIC_WARNING
}
class CreateTableAsSelect {
+ boolean isNotExists()
}
class QualifiedObjectName
class Scope
class Optional_Scope
StatementAnalyzer --> MetadataResolver : uses
StatementAnalyzer --> WarningCollector : uses
StatementAnalyzer --> CreateTableAsSelect : analyzes
StatementAnalyzer --> Scope : returns
StatementAnalyzer --> Optional_Scope : accepts
WarningCollector --> PrestoWarning : collects
PrestoWarning --> StandardWarningCode : uses
MetadataResolver --> QualifiedObjectName : uses
Analysis <.. StatementAnalyzer : shared in analysis flow
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
UtilizedColumnsAnalyzer.visitQuerySpecification, instead of silently skipping processing whenselectItemsis null, consider an early return with an assertion or check that documents this only happens for CTAS NOOP queries, so unexpected nulls in other code paths are easier to detect. - For the new
PrestoWarninginStatementAnalyzer.visitCreateTableAsSelect, consider using or introducing a more specific warning code thanSEMANTIC_WARNING(and/or reusing the existing TABLE_ALREADY_EXISTS semantics) so downstream consumers can distinguish this case from other generic semantic warnings.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `UtilizedColumnsAnalyzer.visitQuerySpecification`, instead of silently skipping processing when `selectItems` is null, consider an early return with an assertion or check that documents this only happens for CTAS NOOP queries, so unexpected nulls in other code paths are easier to detect.
- For the new `PrestoWarning` in `StatementAnalyzer.visitCreateTableAsSelect`, consider using or introducing a more specific warning code than `SEMANTIC_WARNING` (and/or reusing the existing TABLE_ALREADY_EXISTS semantics) so downstream consumers can distinguish this case from other generic semantic warnings.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
|
@auden-woolfson : Thanks for this code. Can you give an example of a query that you fixed with this change ? |
Contributor
Author
Sure... Here is the behavior on the master branch And with the changes... |
Contributor
pratyakshsharma
left a comment
There was a problem hiding this comment.
Let us add a test case to cover this scenario?
6aad5be to
85079a5
Compare
85079a5 to
167a3b8
Compare
tdcmeehan
previously approved these changes
Feb 25, 2026
tdcmeehan
approved these changes
Feb 25, 2026
This was referenced Mar 31, 2026
15 tasks
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.
Description
Previously when using create table if not exists, the error if the table exists would look like this...
After these changes, we have a more descriptive and concise message...
Motivation and Context
Impact
Test Plan
Contributor checklist
Release Notes
Please follow release notes guidelines and fill in the release notes below.
Summary by Sourcery
Bug Fixes:
Summary by Sourcery
Guard query specification column utilization analysis when CREATE TABLE AS SELECT with IF NOT EXISTS resolves to a no-op due to an existing table, and surface a semantic warning in that scenario.
Bug Fixes:
Enhancements: