Skip to content

Commit 1120009

Browse files
authored
Fixed listSchemas to return empty result set in case of empty string catalog (#1131)
## Description <!-- Provide a brief summary of the changes made and the issue they aim to address.--> Fixed listSchemas to return empty result set in case of empty string catalog ## Testing <!-- Describe how the changes have been tested--> Added a test for the fix ## Additional Notes to the Reviewer <!-- Share any additional context or insights that may help the reviewer understand the changes better. This could include challenges faced, limitations, or compromises made during the development process. Also, mention any areas of the code that you would like the reviewer to focus on specifically. --> Fixes [PECOBLR-1371](https://databricks.atlassian.net/browse/PECOBLR-1371) NO_CHANGELOG=true [PECOBLR-1371]: https://databricks.atlassian.net/browse/PECOBLR-1371?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 796b052 commit 1120009

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/main/java/com/databricks/jdbc/dbclient/impl/sqlexec/DatabricksMetadataSdkClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ public DatabricksResultSet listCatalogs(IDatabricksSession session) throws SQLEx
7171
public DatabricksResultSet listSchemas(
7272
IDatabricksSession session, String catalog, String schemaNamePattern) throws SQLException {
7373
catalog = autoFillCatalog(catalog, session);
74+
75+
// Return empty result set if catalog is an empty string
76+
if (catalog != null && catalog.isEmpty()) {
77+
LOGGER.debug("Catalog is empty string, returning empty result set for listSchemas");
78+
return metadataResultSetBuilder.getResultSetWithGivenRowsAndColumns(
79+
SCHEMA_COLUMNS,
80+
new ArrayList<>(),
81+
METADATA_STATEMENT_ID,
82+
com.databricks.jdbc.common.CommandName.LIST_SCHEMAS);
83+
}
84+
7485
CommandBuilder commandBuilder =
7586
new CommandBuilder(catalog, session).setSchemaPattern(schemaNamePattern);
7687
String SQL = commandBuilder.getSQLString(CommandName.LIST_SCHEMAS);

src/test/java/com/databricks/jdbc/dbclient/impl/sqlexec/DatabricksMetadataSdkClientTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,4 +1085,25 @@ void testListCrossReferences_handlesNullSqlStateWithoutNPE() throws Exception {
10851085
TEST_SCHEMA,
10861086
TEST_TABLE));
10871087
}
1088+
1089+
@Test
1090+
void testListSchemasWithEmptyCatalog() throws SQLException {
1091+
IDatabricksConnectionContext mockContext = mock(IDatabricksConnectionContext.class);
1092+
when(mockContext.getEnableMultipleCatalogSupport()).thenReturn(false);
1093+
when(session.getCurrentCatalog()).thenReturn("");
1094+
when(mockClient.getConnectionContext()).thenReturn(mockContext);
1095+
1096+
DatabricksMetadataSdkClient metadataClient = new DatabricksMetadataSdkClient(mockClient);
1097+
1098+
// Call listSchemas with empty catalog
1099+
DatabricksResultSet actualResult = metadataClient.listSchemas(session, "", null);
1100+
1101+
// Verify the result set is empty
1102+
assertNotNull(actualResult);
1103+
assertEquals(StatementState.SUCCEEDED, actualResult.getStatementStatus().getState());
1104+
assertEquals(METADATA_STATEMENT_ID, actualResult.getStatementId());
1105+
assertEquals(0, ((DatabricksResultSetMetaData) actualResult.getMetaData()).getTotalRows());
1106+
assertFalse(
1107+
actualResult.next(), "Expected empty result set for listSchemas with empty catalog");
1108+
}
10881109
}

0 commit comments

Comments
 (0)