Skip to content
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

Metadata fixes for system schema and lakehouse identifier #6

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading