Skip to content

fix: EXPLAIN CREATE TABLE to show IF NOT EXISTS clause#27138

Merged
feilong-liu merged 1 commit intoprestodb:masterfrom
HeidiHan0000:export-D93266866
Feb 19, 2026
Merged

fix: EXPLAIN CREATE TABLE to show IF NOT EXISTS clause#27138
feilong-liu merged 1 commit intoprestodb:masterfrom
HeidiHan0000:export-D93266866

Conversation

@HeidiHan0000
Copy link
Copy Markdown
Contributor

@HeidiHan0000 HeidiHan0000 commented Feb 13, 2026

Summary:
The existing special handling for EXPLAIN CREATE TABLE deliberately simplifies the output to just show CREATE TABLE rather than the full DDL with columns, constraints, and properties. This is reasonable since the full column definitions can be quite verbose and aren't as useful for understanding what the statement will do.

However, the current implementation omits the IF NOT EXISTS clause, which is semantically important - it changes whether the statement will fail or succeed if the table already exists. This is inconsistent with EXPLAIN DROP TABLE IF EXISTS, which does show the IF EXISTS clause (since DROP TABLE uses the default SqlFormatter which includes it).

This change updates the custom explain() method to include IF NOT EXISTS when present, making the behavior consistent with DROP TABLE while still keeping the simplified output format.

With this, I'm open to suggestions on what the most helpful output for EXPLAIN CREATE TABLE would be. Some options:

  • Keep the original abbreviated behavior (just CREATE TABLE ) - current behavior before this fix
  • Abbreviated with IF NOT EXISTS included (this PR)
  • Output the full DDL using SqlFormatter
    ex)
EXPLAIN CREATE TABLE IF NOT EXISTS myschema.users (
    id bigint NOT NULL,
    PRIMARY KEY (id)
) WITH (format = 'ORC')

will output:

CREATE TABLE IF NOT EXISTS myschema.users (
   id bigint NOT NULL,
   PRIMARY KEY (id)
)
WITH (
   format = 'ORC'
)

Differential Revision: D93266866

Summary by Sourcery

Include IF NOT EXISTS in EXPLAIN output for CREATE TABLE statements and expand DDL EXPLAIN coverage tests.

Bug Fixes:

  • Ensure EXPLAIN for CREATE TABLE preserves the IF NOT EXISTS clause when present.

Tests:

  • Expand EXPLAIN DDL tests to cover IF NOT EXISTS variants and additional DDL statements such as DROP TABLE/VIEW, RESET SESSION, and transaction commands.
== NO RELEASE NOTE ==

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

sourcery-ai bot commented Feb 13, 2026

Reviewer's Guide

Adjusts EXPLAIN output for CREATE TABLE to include the IF NOT EXISTS clause when present, and expands DDL explain tests to cover various CREATE/DROP TABLE cases and other DDL statements.

Sequence diagram for EXPLAIN CREATE TABLE IF NOT EXISTS handling

sequenceDiagram
    actor User
    participant PrestoClient
    participant Coordinator
    participant Analyzer
    participant CreateTableTask

    User->>PrestoClient: send SQL EXPLAIN CREATE TABLE IF NOT EXISTS table
    PrestoClient->>Coordinator: submit query
    Coordinator->>Analyzer: analyze EXPLAIN statement
    Analyzer->>CreateTableTask: call explain(statement, parameters)
    CreateTableTask->>CreateTableTask: check statement.isNotExists()
    CreateTableTask-->>Analyzer: CREATE TABLE IF NOT EXISTS table
    Analyzer-->>Coordinator: formatted EXPLAIN output
    Coordinator-->>PrestoClient: EXPLAIN result
    PrestoClient-->>User: render EXPLAIN output
Loading

Class diagram for CreateTableTask explain method update

classDiagram
    class CreateTableTask {
        +String getName()
        +String explain(CreateTable statement, List_Expression parameters)
    }

    class CreateTable {
        +String getName()
        +boolean isNotExists()
    }

    class Expression

    class List_Expression {
        +add(Expression element)
        +get(int index) Expression
        +size() int
    }

    CreateTableTask --> CreateTable : uses
    CreateTableTask --> List_Expression : uses
    List_Expression --> Expression : contains
Loading

File-Level Changes

Change Details Files
Ensure EXPLAIN CREATE TABLE output includes the IF NOT EXISTS clause when specified.
  • Update the CreateTableTask.explain implementation to prepend 'CREATE TABLE IF NOT EXISTS' when the statement has the NOT EXISTS flag.
  • Keep the existing simplified behavior (omitting column definitions and properties) while only adjusting the presence of the IF NOT EXISTS clause.
presto-main-base/src/main/java/com/facebook/presto/execution/CreateTableTask.java
Broaden EXPLAIN DDL tests to cover IF NOT EXISTS and additional DDL statement variants.
  • Add assertions for EXPLAIN CREATE TABLE with and without IF NOT EXISTS, and with qualified table names.
  • Add assertions for EXPLAIN DROP TABLE, including IF EXISTS and fully-qualified table names.
  • Add or reorganize assertions for EXPLAIN CREATE/DROP VIEW, CREATE/ALTER/DROP FUNCTION, ALTER TABLE operations, session operations (SET/RESET), PREPARE/DEALLOCATE, and transaction control statements, grouped and commented by DDL category.
presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.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

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 CreateTableTask.explain, you can avoid duplicating the "CREATE TABLE" prefix by constructing the string once and conditionally appending "IF NOT EXISTS ", e.g., "CREATE TABLE " + (statement.isNotExists() ? "IF NOT EXISTS " : "") + statement.getName();.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `CreateTableTask.explain`, you can avoid duplicating the `"CREATE TABLE"` prefix by constructing the string once and conditionally appending `"IF NOT EXISTS "`, e.g., `"CREATE TABLE " + (statement.isNotExists() ? "IF NOT EXISTS " : "") + statement.getName();`.

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.

@HeidiHan0000 HeidiHan0000 changed the title Fix EXPLAIN CREATE TABLE to show IF NOT EXISTS clause fix: EXPLAIN CREATE TABLE to show IF NOT EXISTS clause Feb 13, 2026
@steveburnett
Copy link
Copy Markdown
Contributor

Opened #27148 to track the missing documentation for EXPLAIN CREATE TABLE.

@HeidiHan0000
Copy link
Copy Markdown
Contributor Author

Opened #27148 to track the missing documentation for EXPLAIN CREATE TABLE.

@steveburnett Thanks! Once the behavior for CREATE TABLE IF NOT EXISTS is decided, I can add that info. Please let me know if you have any thoughts on the behavior of this

Summary:

The existing special handling for EXPLAIN CREATE TABLE deliberately simplifies the output to just show CREATE TABLE <name> rather than the full DDL with columns, constraints, and properties. This is reasonable since the full column definitions can be quite verbose and aren't as useful for understanding what the statement will do.

However, the current implementation omits the IF NOT EXISTS clause, which is semantically important - it changes whether the statement will fail or succeed if the table already exists. This is inconsistent with EXPLAIN DROP TABLE IF EXISTS, which does show the IF EXISTS clause (since DROP TABLE uses the default SqlFormatter which includes it).

This change updates the custom explain() method to include IF NOT EXISTS when present, making the behavior consistent with DROP TABLE while still keeping the simplified output format.

With this, I'm open to suggestions on what the most helpful output for EXPLAIN CREATE TABLE would be. Some options:

- Keep the original abbreviated behavior (just CREATE TABLE <name>) - current behavior before this fix
- Abbreviated with IF NOT EXISTS included (this PR)
- Output the full DDL using SqlFormatter
ex)
```
EXPLAIN CREATE TABLE IF NOT EXISTS myschema.users (
    id bigint NOT NULL,
    PRIMARY KEY (id)
) WITH (format = 'ORC')
```
will output:
```
CREATE TABLE IF NOT EXISTS myschema.users (
   id bigint NOT NULL,
   PRIMARY KEY (id)
)
WITH (
   format = 'ORC'
)
```

Reviewed By: kewang1024

Differential Revision: D93266866
Copy link
Copy Markdown
Contributor

@amitkdutta amitkdutta left a comment

Choose a reason for hiding this comment

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

Thanks @HeidiHan0000

@feilong-liu feilong-liu merged commit 2d66492 into prestodb:master Feb 19, 2026
80 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants