Skip to content

Commit ca6ace6

Browse files
msrathore-dbclaude
andauthored
Modified getFunctions to return functions from the current catalog in case of null catalog (#1147)
## Description <!-- Provide a brief summary of the changes made and the issue they aim to address.--> Modified getFunctions to return functions from the current catalog in case of null catalog . This will allow the users to see system functions ## Testing <!-- Describe how the changes have been tested--> Tested locally. ## 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. --> --------- Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent e9a1296 commit ca6ace6

3 files changed

Lines changed: 54 additions & 16 deletions

File tree

NEXT_CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
### Updated
88
- Log timestamps now explicitly display timezone.
99
- **[Breaking Change]** `PreparedStatement.setTimestamp(int, Timestamp, Calendar)` now properly applies Calendar timezone conversion using LocalDateTime pattern (inline with `getTimestamp`). Previously Calendar parameter was ineffective.
10-
- `DatabaseMetaData.getColumns()` with null catalog parameter now retrieves columns from all catalogs when using SQL Execution API.
10+
- `DatabaseMetaData.getColumns()` with null catalog parameter now retrieves columns from all catalogs when using SQL Execution API, aligning the behaviour with thrift.
11+
- `DatabaseMetaData.getFunctions()` with null catalog parameter now retrieves columns from the current catalog when using SQL Execution API, aligning the behaviour with thrift.
1112

1213
### Fixed
1314
- Fix timeout exception handling to throw `SQLTimeoutException` instead of `DatabricksSQLException` when queries timeout.
1415
- Removes dangerous global timezone modification that caused race conditions.
15-
<<<<<<< HEAD
1616
- Fixed `Statement.getLargeUpdateCount()` to return -1 instead of throwing Exception when there were no more results or result is not an update count.
1717
- CVE-2025-66566. Updated lz4-java dependency to 1.10.1.
1818
- Fix `INVALID_IDENTIFIER` error when using catalog/schema/table names for SQL Exec API with hyphens or special characters in metadata operations (`getSchemas()`, `getTables()`, `getColumns()`, etc.) and connection methods (`setCatalog()`, `setSchema()`). Per Databricks identifier rules, special characters are now properly enclosed in backticks.

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,19 @@ public DatabricksResultSet listFunctions(
185185
throws SQLException {
186186
catalog = autoFillCatalog(catalog, session);
187187

188-
// Return empty result set if catalog is null
188+
// Fetch current catalog if catalog is null
189189
if (catalog == null) {
190-
LOGGER.debug("Catalog is null, returning empty result set for listFunctions");
191-
return metadataResultSetBuilder.getResultSetWithGivenRowsAndColumns(
192-
MetadataResultConstants.FUNCTION_COLUMNS,
193-
new ArrayList<>(),
194-
METADATA_STATEMENT_ID,
195-
com.databricks.jdbc.common.CommandName.LIST_FUNCTIONS);
190+
// #TODO: Make server side changes
191+
LOGGER.debug("Catalog is null, fetching current catalog for listFunctions");
192+
catalog = session.getCurrentCatalog();
193+
194+
// Use hive_metastore as fallback if current catalog is null
195+
if (catalog == null) {
196+
LOGGER.debug("Current catalog is null, using hive_metastore as fallback");
197+
catalog = "hive_metastore";
198+
} else {
199+
LOGGER.debug("Using current catalog: {}", catalog);
200+
}
196201
}
197202

198203
CommandBuilder commandBuilder =

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,18 +788,51 @@ void testGetFunctions(
788788
}
789789

790790
@Test
791-
void testReturnsEmptyResultSetInCaseOfNullCatalog() throws SQLException {
791+
void testListFunctionsWithNullCatalog() throws SQLException {
792+
when(session.getComputeResource()).thenReturn(WAREHOUSE_COMPUTE);
793+
when(session.getCurrentCatalog()).thenReturn("current_catalog");
792794
IDatabricksConnectionContext mockContext = mock(IDatabricksConnectionContext.class);
793795
when(mockContext.getEnableMultipleCatalogSupport()).thenReturn(true);
794796
when(mockClient.getConnectionContext()).thenReturn(mockContext);
797+
795798
DatabricksMetadataSdkClient metadataClient = new DatabricksMetadataSdkClient(mockClient);
796799

797-
// listFunctions with null catalog should return empty ResultSet
798-
DatabricksResultSet functionsResult =
799-
metadataClient.listFunctions(session, null, TEST_SCHEMA, TEST_TABLE);
800-
assertNotNull(functionsResult);
801-
assertFalse(
802-
functionsResult.next(), "Expected empty result set for listFunctions with null catalog");
800+
String expectedSQL =
801+
"SHOW FUNCTIONS IN CATALOG `current_catalog` SCHEMA LIKE 'testSchema' LIKE 'functionPattern'";
802+
when(mockClient.executeStatement(
803+
expectedSQL,
804+
WAREHOUSE_COMPUTE,
805+
new HashMap<Integer, ImmutableSqlParameter>(),
806+
StatementType.METADATA,
807+
session,
808+
null))
809+
.thenReturn(mockedResultSet);
810+
when(mockedResultSet.next()).thenReturn(true, false);
811+
doReturn(6).when(mockedMetaData).getColumnCount();
812+
doReturn(FUNCTION_NAME_COLUMN.getResultSetColumnName()).when(mockedMetaData).getColumnName(1);
813+
doReturn(FUNCTION_SCHEMA_COLUMN.getResultSetColumnName()).when(mockedMetaData).getColumnName(2);
814+
doReturn(FUNCTION_CATALOG_COLUMN.getResultSetColumnName())
815+
.when(mockedMetaData)
816+
.getColumnName(3);
817+
doReturn(REMARKS_COLUMN.getResultSetColumnName()).when(mockedMetaData).getColumnName(4);
818+
doReturn(FUNCTION_TYPE_COLUMN.getResultSetColumnName()).when(mockedMetaData).getColumnName(5);
819+
doReturn(SPECIFIC_NAME_COLUMN.getResultSetColumnName()).when(mockedMetaData).getColumnName(6);
820+
when(mockedResultSet.getMetaData()).thenReturn(mockedMetaData);
821+
822+
DatabricksResultSet actualResult =
823+
metadataClient.listFunctions(session, null, TEST_SCHEMA, TEST_FUNCTION_PATTERN);
824+
825+
assertEquals(StatementState.SUCCEEDED, actualResult.getStatementStatus().getState());
826+
assertEquals(GET_FUNCTIONS_STATEMENT_ID, actualResult.getStatementId());
827+
assertEquals(1, ((DatabricksResultSetMetaData) actualResult.getMetaData()).getTotalRows());
828+
}
829+
830+
@Test
831+
void testReturnsEmptyResultSetInCaseOfNullCatalog() throws SQLException {
832+
IDatabricksConnectionContext mockContext = mock(IDatabricksConnectionContext.class);
833+
when(mockContext.getEnableMultipleCatalogSupport()).thenReturn(true);
834+
when(mockClient.getConnectionContext()).thenReturn(mockContext);
835+
DatabricksMetadataSdkClient metadataClient = new DatabricksMetadataSdkClient(mockClient);
803836

804837
// listPrimaryKeys with null catalog should return empty ResultSet
805838
DatabricksResultSet primaryKeysResult =

0 commit comments

Comments
 (0)