Skip to content

Commit

Permalink
Metadata fixes for system schema and lakehouse identifier (#6)
Browse files Browse the repository at this point in the history
These changes exclude the dataspace from the lakehouse identifier when
it is null as well as excluding system schemas when making the
get_schemas_query.
  • Loading branch information
jschneidereit authored Jan 2, 2025
1 parent 96ea6a2 commit 510677f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ public ResultSet getSchemas() throws SQLException {

@Override
public ResultSet getCatalogs() throws SQLException {
return QueryMetadataUtil.createCatalogsResultSet(tokenProcessor, client);
return QueryMetadataUtil.createCatalogsResultSet(tokenProcessor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Optional;
import java.util.TimeZone;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
Expand Down Expand Up @@ -355,13 +356,23 @@ private static List<Object> constructTableTypesData() {
return data;
}

public static ResultSet createCatalogsResultSet(Optional<TokenProcessor> tokenProcessor, OkHttpClient client)
throws SQLException {
@SneakyThrows
static List<Object> getLakehouse(Optional<TokenProcessor> tokenProcessor) {
if (!tokenProcessor.isPresent()) {
return ImmutableList.of();
}

val tenantId = tokenProcessor.get().getDataCloudToken().getTenantId();
val dataspaceName = tokenProcessor.get().getSettings().getDataspace();
List<Object> data = ImmutableList.of(ImmutableList.of("lakehouse:" + tenantId + ";" + dataspaceName));
val dataspace = tokenProcessor.get().getSettings().getDataspace();
val lakehouse =
"lakehouse:" + tenantId + ";" + Optional.ofNullable(dataspace).orElse("");

return ImmutableList.of(ImmutableList.of(lakehouse));
}

QueryDBMetadata queryDbMetadata = QueryDBMetadata.GET_CATALOGS;
public static ResultSet createCatalogsResultSet(Optional<TokenProcessor> tokenProcessor) throws SQLException {
val data = getLakehouse(tokenProcessor);
val queryDbMetadata = QueryDBMetadata.GET_CATALOGS;

return getMetadataResultSet(queryDbMetadata, NUM_CATALOG_METADATA_COLUMNS, data);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/sql/get_schemas_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ WHERE nspname <> 'pg_toast'
AND (nspname !~ '^pg_temp_'
OR nspname = (pg_catalog.current_schemas(true))[1])
AND (nspname !~ '^pg_toast_temp_'
OR nspname = replace((pg_catalog.current_schemas(true))[1], 'pg_temp_', 'pg_toast_temp_'))
OR nspname = replace((pg_catalog.current_schemas(true))[1], 'pg_temp_', 'pg_toast_temp_'))
AND (nspname !~ 'tableau*')
AND (nspname !~ 'pg_*')
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.salesforce.datacloud.jdbc.core;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.salesforce.datacloud.jdbc.auth.AuthenticationSettings;
import com.salesforce.datacloud.jdbc.auth.DataCloudToken;
import com.salesforce.datacloud.jdbc.auth.TokenProcessor;
import java.util.Optional;
import java.util.UUID;
import lombok.SneakyThrows;
import lombok.val;
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 QueryMetadataUtilTest {
@Mock
private TokenProcessor tokenProcessor;

@Mock
private DataCloudToken dataCloudToken;

@Mock
private AuthenticationSettings authenticationSettings;

@Test
@SneakyThrows
void excludesDataspaceWhenNull() {
val tenantId = UUID.randomUUID().toString();

when(dataCloudToken.getTenantId()).thenReturn(tenantId);
when(authenticationSettings.getDataspace()).thenReturn(null);
when(tokenProcessor.getSettings()).thenReturn(authenticationSettings);
when(tokenProcessor.getDataCloudToken()).thenReturn(dataCloudToken);

val actual = QueryMetadataUtil.getLakehouse(Optional.of(tokenProcessor));
val expected = ImmutableList.of(ImmutableList.of("lakehouse:" + tenantId + ";"));
assertThat(actual).isEqualTo(expected);
}

@Test
@SneakyThrows
void includesDataspaceWhenNotNull() {
val tenantId = UUID.randomUUID().toString();
val dataspace = UUID.randomUUID().toString();

when(dataCloudToken.getTenantId()).thenReturn(tenantId);
when(authenticationSettings.getDataspace()).thenReturn(dataspace);
when(tokenProcessor.getSettings()).thenReturn(authenticationSettings);
when(tokenProcessor.getDataCloudToken()).thenReturn(dataCloudToken);

val actual = QueryMetadataUtil.getLakehouse(Optional.of(tokenProcessor));
val expected = ImmutableList.of(ImmutableList.of("lakehouse:" + tenantId + ";" + dataspace));

assertThat(actual).isEqualTo(expected);
}
}

0 comments on commit 510677f

Please sign in to comment.