-
Notifications
You must be signed in to change notification settings - Fork 40
Support ALL CATALOGS in GetTables for SQL Execution API #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jayantsing-db
merged 6 commits into
databricks:main
from
databricks:jayantsing-db/show-tables-all-catalogs
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5eac094
Support ALL CATALOGS in GetTables for SQL Execution API
jayantsing-db 51991d9
Add unit tests
jayantsing-db 36b0fea
Merge remote-tracking branch 'databricks/main' into jayantsing-db/sho…
jayantsing-db 2e57abd
nit
jayantsing-db 5c8aec0
Handle old dbsql versions
jayantsing-db e1c73a3
Merge remote-tracking branch 'databricks/main' into jayantsing-db/sho…
jayantsing-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
211 changes: 211 additions & 0 deletions
211
src/test/java/com/databricks/jdbc/dbclient/impl/sqlexec/CommandBuilderTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| package com.databricks.jdbc.dbclient.impl.sqlexec; | ||
|
|
||
| import static com.databricks.jdbc.dbclient.impl.common.CommandConstants.*; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import com.databricks.jdbc.api.internal.IDatabricksSession; | ||
| import com.databricks.jdbc.common.util.WildcardUtil; | ||
| import com.databricks.jdbc.exception.DatabricksSQLFeatureNotSupportedException; | ||
| import java.sql.SQLException; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class CommandBuilderTest { | ||
|
|
||
| @Mock private IDatabricksSession mockSession; | ||
|
|
||
| private static final String TEST_CATALOG = "test_catalog"; | ||
| private static final String TEST_SCHEMA = "test_schema"; | ||
| private static final String TEST_TABLE = "test_table"; | ||
| private static final String TEST_SESSION_CONTEXT = "test_session_context"; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| when(mockSession.toString()).thenReturn(TEST_SESSION_CONTEXT); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Tests for LIST_PRIMARY_KEYS command") | ||
| class ListPrimaryKeysTests { | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching primary keys") | ||
| void shouldGenerateCorrectSqlForPrimaryKeys() throws SQLException { | ||
| CommandBuilder builder = | ||
| new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_PRIMARY_KEYS); | ||
|
|
||
| String expectedSql = | ||
| String.format(SHOW_PRIMARY_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE); | ||
| assertEquals(expectedSql, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when catalog is null for primary keys") | ||
| void shouldThrowExceptionWhenCatalogIsNullForPrimaryKeys() { | ||
| CommandBuilder builder = | ||
| new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when schema is null for primary keys") | ||
| void shouldThrowExceptionWhenSchemaIsNullForPrimaryKeys() { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when table is null for primary keys") | ||
| void shouldThrowExceptionWhenTableIsNullForPrimaryKeys() { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_PRIMARY_KEYS)); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Tests for LIST_TABLES command") | ||
| class ListTablesTests { | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching tables with catalog") | ||
| void shouldGenerateCorrectSqlForTablesWithCatalog() throws SQLException { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_TABLES); | ||
|
|
||
| String expectedSql = String.format(SHOW_TABLES_SQL, TEST_CATALOG); | ||
| assertEquals(expectedSql, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching tables with catalog and schema pattern") | ||
| void shouldGenerateCorrectSqlForTablesWithCatalogAndSchemaPattern() throws SQLException { | ||
| String schemaPattern = "test_schema%"; | ||
| String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern); | ||
|
|
||
| CommandBuilder builder = | ||
| new CommandBuilder(TEST_CATALOG, mockSession).setSchemaPattern(schemaPattern); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_TABLES); | ||
|
|
||
| String expectedSql = | ||
| String.format(SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL), TEST_CATALOG, hiveSchemaPattern); | ||
| assertEquals(expectedSql, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName( | ||
| "Should generate correct SQL for fetching tables with catalog, schema pattern, and table pattern") | ||
| void shouldGenerateCorrectSqlForTablesWithCatalogSchemaAndTablePattern() throws SQLException { | ||
| String schemaPattern = "test_schema%"; | ||
| String tablePattern = "test_table%"; | ||
| String hiveSchemaPattern = WildcardUtil.jdbcPatternToHive(schemaPattern); | ||
| String hiveTablePattern = WildcardUtil.jdbcPatternToHive(tablePattern); | ||
|
|
||
| CommandBuilder builder = | ||
| new CommandBuilder(TEST_CATALOG, mockSession) | ||
| .setSchemaPattern(schemaPattern) | ||
| .setTablePattern(tablePattern); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_TABLES); | ||
|
|
||
| String expectedSql = | ||
| String.format( | ||
| SHOW_TABLES_SQL.concat(SCHEMA_LIKE_SQL).concat(LIKE_SQL), | ||
| TEST_CATALOG, | ||
| hiveSchemaPattern, | ||
| hiveTablePattern); | ||
| assertEquals(expectedSql, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching tables from all catalogs") | ||
| void shouldGenerateCorrectSqlForTablesFromAllCatalogs() throws SQLException { | ||
| CommandBuilder builder = new CommandBuilder(null, mockSession); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_TABLES); | ||
|
|
||
| assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching tables with wildcard catalog") | ||
| void shouldGenerateCorrectSqlForTablesWithWildcardCatalog() throws SQLException { | ||
| // Test with '*' wildcard | ||
| CommandBuilder builder1 = new CommandBuilder("*", mockSession); | ||
| String sql1 = builder1.getSQLString(CommandName.LIST_TABLES); | ||
| assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql1); | ||
|
|
||
| // Test with '%' wildcard | ||
| CommandBuilder builder2 = new CommandBuilder("%", mockSession); | ||
| String sql2 = builder2.getSQLString(CommandName.LIST_TABLES); | ||
| assertEquals(SHOW_TABLES_IN_ALL_CATALOGS_SQL, sql2); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("Tests for LIST_FOREIGN_KEYS command") | ||
| class ListForeignKeysTests { | ||
|
|
||
| @Test | ||
| @DisplayName("Should generate correct SQL for fetching foreign keys") | ||
| void shouldGenerateCorrectSqlForForeignKeys() throws SQLException { | ||
| CommandBuilder builder = | ||
| new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); | ||
|
|
||
| String sql = builder.getSQLString(CommandName.LIST_FOREIGN_KEYS); | ||
|
|
||
| String expectedSql = | ||
| String.format(SHOW_FOREIGN_KEYS_SQL, TEST_CATALOG, TEST_SCHEMA, TEST_TABLE); | ||
| assertEquals(expectedSql, sql); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when catalog is null for foreign keys") | ||
| void shouldThrowExceptionWhenCatalogIsNullForForeignKeys() { | ||
| CommandBuilder builder = | ||
| new CommandBuilder(null, mockSession).setSchema(TEST_SCHEMA).setTable(TEST_TABLE); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when schema is null for foreign keys") | ||
| void shouldThrowExceptionWhenSchemaIsNullForForeignKeys() { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setTable(TEST_TABLE); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw SQLException when table is null for foreign keys") | ||
| void shouldThrowExceptionWhenTableIsNullForForeignKeys() { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession).setSchema(TEST_SCHEMA); | ||
|
|
||
| assertThrows(SQLException.class, () -> builder.getSQLString(CommandName.LIST_FOREIGN_KEYS)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should throw exception for unsupported command") | ||
| void shouldThrowExceptionForUnsupportedCommand() { | ||
| CommandBuilder builder = new CommandBuilder(TEST_CATALOG, mockSession); | ||
|
|
||
| CommandName mockCommand = mock(CommandName.class); | ||
|
|
||
| assertThrows( | ||
| DatabricksSQLFeatureNotSupportedException.class, () -> builder.getSQLString(mockCommand)); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.