Skip to content

SQL Database DDL & Usability Improvements - #38952

Merged
damccorm merged 3 commits into
apache:masterfrom
damccorm:feature/sql-database-ddl
Jun 12, 2026
Merged

SQL Database DDL & Usability Improvements#38952
damccorm merged 3 commits into
apache:masterfrom
damccorm:feature/sql-database-ddl

Conversation

@damccorm

@damccorm damccorm commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Description

This PR is split from #38866. It introduces comprehensive support for database DDL (Data Definition Language) and usability improvements in the Beam SQL CLI and InMemoryCatalog.

Key Changes

  • Database DDL Support:
    • Introduces CREATE DATABASE and DROP DATABASE statements.
    • Supports CASCADE and RESTRICT options for DROP DATABASE:
      • DROP DATABASE <db> RESTRICT (default) will fail if the database is not empty, preventing accidental data loss.
      • DROP DATABASE <db> CASCADE will automatically drop all tables within that database before dropping the database itself.
  • Usability Improvements:
    • Supports the industry-standard USE <database_name> shortcut in the CLI, removing the mandatory requirement of the DATABASE keyword (i.e. you no longer have to type USE DATABASE <db>).
  • Cross-Database Table Management:
    • Allows fully qualified table names in DDL and DML (e.g., CREATE EXTERNAL TABLE db_1.my_table ... or INSERT INTO db_1.my_table ...), allowing you to manage and write to tables in non-active databases.
  • Testing:
    • Adds a new comprehensive integration test suite BeamSqlCliDatabaseTest.java verifying all DDL and database management features in the CLI.

@damccorm
damccorm marked this pull request as draft June 12, 2026 19:47
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the SQL DDL capabilities within Beam SQL, particularly for database management. It enables users to create and delete databases, and introduces more robust behavior for database deletion by implementing CASCADE and RESTRICT options. Additionally, it streamlines the USE command in the Beam SQL CLI, making it more user-friendly.

Highlights

  • Database DDL Support: Introduced support for creating and deleting databases within Beam SQL.
  • Enhanced DROP DATABASE: The DROP DATABASE command now supports CASCADE and RESTRICT options, allowing for more controlled database deletion.
  • Usability Improvement for USE command: The DATABASE keyword is now optional in the USE <database> CLI command, simplifying its usage.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request makes the "DATABASE" keyword optional in the "USE" statement, changes the visibility of schema helper methods in "SqlDdlNodes" to public, and updates "InMemoryCatalog" to prevent dropping non-empty databases unless "CASCADE" is specified. Feedback on these changes includes a recommendation to conditionally clean up state in "InMemoryCatalog.dropDatabase" only if the database was successfully removed, and to assert the more specific "IllegalStateException" instead of "RuntimeException" in the unit tests.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request makes the 'DATABASE' keyword optional in the 'USE' statement, increases the visibility of helper methods in 'SqlDdlNodes' to public, and updates 'InMemoryCatalog' to support dropping databases with 'CASCADE' or throwing an exception if the database is not empty when 'CASCADE' is not specified. Corresponding unit tests have been added. Feedback on the changes suggests checking if the database exists in 'InMemoryCatalog.dropDatabase' before performing any operations to prevent side effects and inconsistent states.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

…se and add tests for DROP DATABASE IF EXISTS
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request makes the 'DATABASE' keyword optional in the 'USE DATABASE' SQL syntax, updates 'InMemoryCatalog' to properly handle 'DROP DATABASE' operations (including cascade behavior and empty checks), and exposes helper methods in 'SqlDdlNodes' as public. Feedback is provided to optimize the 'dropDatabase' method by removing a redundant collection lookup, and to add a defensive null check to 'childSchema' now that it has public visibility.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

…check in SqlDdlNodes, and make tests more precise
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request makes the DATABASE keyword optional in the USE statement and improves the database dropping logic in InMemoryCatalog by checking if the database is empty when dropping without CASCADE. It also adds corresponding unit tests. The review feedback highlights a potential issue where setting currentDatabase to null when the active database is dropped could lead to NullPointerExceptions in subsequent operations, suggesting resetting it to a default database or throwing a clear exception instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@damccorm
damccorm marked this pull request as ready for review June 12, 2026 20:42
@damccorm

Copy link
Copy Markdown
Contributor Author

R: @Abacn

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request makes the 'DATABASE' keyword optional in the 'USE' SQL syntax, exposes schema utility methods as public with added null safety, and refactors 'InMemoryCatalog.dropDatabase' to correctly handle restrict and cascade behaviors. It also adds comprehensive unit tests for these new features. The review feedback suggests adding a null check for the 'database' parameter in 'InMemoryCatalog.dropDatabase' to prevent potential NullPointerExceptions.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 112 to 127
public boolean dropDatabase(String database, boolean cascade) {
checkState(!cascade, "%s does not support CASCADE.", getClass().getSimpleName());
MetaStore metaStore = metaStores.get(database);
if (!cascade && metaStore != null && !metaStore.getTables().isEmpty()) {
throw new IllegalStateException("Database '" + database + "' is not empty.");
}

boolean removed = databases.remove(database);
if (!removed) {
return false;
}
if (database.equals(currentDatabase)) {
currentDatabase = null;
}
return removed;
metaStores.remove(database);
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure robust defensive programming and prevent potential NullPointerExceptions, we should add a null check for the database parameter at the beginning of the dropDatabase method. If database is null, calling metaStores.get(database) or database.equals(currentDatabase) will throw an exception (especially if metaStores is backed by a ConcurrentHashMap which does not permit null keys).

  public boolean dropDatabase(String database, boolean cascade) {
    if (database == null) {
      return false;
    }
    MetaStore metaStore = metaStores.get(database);
    if (!cascade && metaStore != null && !metaStore.getTables().isEmpty()) {
      throw new IllegalStateException("Database '" + database + "' is not empty.");
    }

    boolean removed = databases.remove(database);
    if (!removed) {
      return false;
    }
    if (database.equals(currentDatabase)) {
      currentDatabase = null;
    }
    metaStores.remove(database);
    return true;
  }

@github-actions

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

Comment thread sdks/java/extensions/sql/src/main/codegen/includes/parserImpls.ftl
@damccorm
damccorm merged commit 6c083df into apache:master Jun 12, 2026
24 checks passed
@damccorm
damccorm deleted the feature/sql-database-ddl branch June 12, 2026 21:02
ash6898 pushed a commit to ash6898/beam that referenced this pull request Jun 29, 2026
* SQL Database DDL & Usability Improvements: Support CREATE/DROP DATABASE and USE short syntax

* Address code review feedback: add guard in InMemoryCatalog.dropDatabase and add tests for DROP DATABASE IF EXISTS

* Address new code review feedback: optimize InMemoryCatalog, add null check in SqlDdlNodes, and make tests more precise
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.

2 participants