Skip to content

fix: Add null check to UtilizedColumnAnalyzer#27083

Merged
auden-woolfson merged 1 commit intoprestodb:masterfrom
auden-woolfson:fix_if_not_exists
Feb 25, 2026
Merged

fix: Add null check to UtilizedColumnAnalyzer#27083
auden-woolfson merged 1 commit intoprestodb:masterfrom
auden-woolfson:fix_if_not_exists

Conversation

@auden-woolfson
Copy link
Copy Markdown
Contributor

@auden-woolfson auden-woolfson commented Feb 4, 2026

Description

Previously when using create table if not exists, the error if the table exists would look like this...

presto> create table if not exists tpch.sf1.customer as select * from tpch.sf1.customer;
CREATE TABLE: 0 rows

WARNING: Error in analyzing utilized columns for access control, falling back to checking access on all columns: Cannot invoke "java.util.List.iterator()" because "selectItems" is null


Query 20260206_233041_00001_z5bwk, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
[Latency: client-side: 64ms, server-side: 35ms] [0 rows, 0B] [0 rows/s, 0B/s]

After these changes, we have a more descriptive and concise message...

presto> create table if not exists tpch.sf1.customer as select * from tpch.sf1.customer;
CREATE TABLE: 0 rows

WARNING: Table 'tpch.sf1.customer' already exists, skipping table creation


Query 20260206_233246_00000_bjt5x, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
[Latency: client-side: 377ms, server-side: 209ms] [0 rows, 0B] [0 rows/s, 0B/s]

Motivation and Context

Impact

Test Plan

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.
  • If adding new dependencies, verified they have an OpenSSF Scorecard score of 5.0 or higher (or obtained explicit TSC approval for lower scores).

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==
General Changes
* Add warning message on CTAS if not exists

Summary by Sourcery

Bug Fixes:

  • Prevent a potential null pointer error in UtilizedColumnsAnalyzer when analysis returns no output expressions for a query specification.

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:

  • Prevent a potential null pointer when analyzing utilized columns for query specifications that have no output expressions, such as no-op CREATE TABLE IF NOT EXISTS statements.

Enhancements:

  • Emit a semantic warning when CREATE TABLE IF NOT EXISTS is a no-op because the target table already exists, indicating that table creation is skipped.

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Feb 4, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Feb 4, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds 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 handling

sequenceDiagram
    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
Loading

Class diagram for UtilizedColumnsAnalyzer and StatementAnalyzer changes

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Guard utilization analysis against missing output expressions from Analysis for certain query specifications.
  • Wrap usage of analysis.getOutputExpressions(querySpec) in a null check.
  • Skip processing of select output expressions entirely when output expressions are null (e.g., for no-op statements).
  • Keep existing behavior for both prunable and non-prunable contexts when output expressions are present.
presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/UtilizedColumnsAnalyzer.java
Mark CREATE TABLE IF NOT EXISTS as a no-op with an explicit semantic warning when the target table already exists.
  • After detecting that the target table exists and NOT EXISTS is specified, set createTableAsSelectNoOp in Analysis.
  • Add a PrestoWarning with StandardWarningCode.SEMANTIC_WARNING to inform the user that table creation is skipped because the table already exists.
  • Return a scope with a single BIGINT 'rows' field for the no-op CTAS path, preserving prior behavior.
presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@auden-woolfson auden-woolfson marked this pull request as ready for review February 5, 2026 21:55
@prestodb-ci prestodb-ci requested review from a team, NivinCS and pramodsatya and removed request for a team February 5, 2026 21:55
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@aditi-pandit
Copy link
Copy Markdown
Contributor

@auden-woolfson : Thanks for this code. Can you give an example of a query that you fixed with this change ?

@auden-woolfson
Copy link
Copy Markdown
Contributor Author

@auden-woolfson : Thanks for this code. Can you give an example of a query that you fixed with this change ?

Sure...

Here is the behavior on the master branch

presto> create table if not exists tpch.sf1.customer as select * from tpch.sf1.customer;
CREATE TABLE: 0 rows

WARNING: Error in analyzing utilized columns for access control, falling back to checking access on all columns: Cannot invoke "java.util.List.iterator()" because "selectItems" is null


Query 20260206_233041_00001_z5bwk, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
[Latency: client-side: 64ms, server-side: 35ms] [0 rows, 0B] [0 rows/s, 0B/s]

And with the changes...

presto> create table if not exists tpch.sf1.customer as select * from tpch.sf1.customer;
CREATE TABLE: 0 rows

WARNING: Table 'tpch.sf1.customer' already exists, skipping table creation


Query 20260206_233246_00000_bjt5x, FINISHED, 1 node
Splits: 1 total, 1 done (100.00%)
[Latency: client-side: 377ms, server-side: 209ms] [0 rows, 0B] [0 rows/s, 0B/s]

Copy link
Copy Markdown
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us add a test case to cover this scenario?

tdcmeehan
tdcmeehan previously approved these changes Feb 25, 2026
@auden-woolfson auden-woolfson merged commit 6d7ab53 into prestodb:master Feb 25, 2026
82 of 84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

from:IBM PR from IBM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants