Skip to content

Commit

Permalink
Fix BigQuery case-insensitive mapping when cache is disabled
Browse files Browse the repository at this point in the history
When cache is disabled, all values put into cache are not persisted, causing cache-insensitive mapping failing
This PR decouples mapping from cache handling
  • Loading branch information
pajaks authored and ebyhr committed Oct 8, 2024
1 parent 0680930 commit 9f267c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions plugin/trino-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@
<exclude>**/TestBigQueryMetadata.java</exclude>
<exclude>**/TestBigQuerySplitManager.java</exclude>
<exclude>**/TestBigQueryInstanceCleaner.java</exclude>
<exclude>**/TestBigQueryCaseInsensitiveMapping.java</exclude>
<exclude>**/TestBigQueryCaseInsensitiveMappingWithCache.java</exclude>
<exclude>**/TestBigQuery*FailureRecoveryTest.java</exclude>
<exclude>**/TestBigQueryWithProxyTest.java</exclude>
Expand Down Expand Up @@ -579,6 +580,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/TestBigQueryCaseInsensitiveMapping.java</include>
<include>**/TestBigQueryCaseInsensitiveMappingWithCache.java</include>
<!-- Also included here to make sure the case-insesitive project also gets cleaned up -->
<include>**/TestBigQueryInstanceCleaner.java</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.trino.spi.connector.TableNotFoundException;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -171,12 +172,15 @@ public Optional<RemoteDatabaseObject> toRemoteDataset(String projectId, String d
}

// Get all information from BigQuery and update cache from all fetched information
Map<DatasetId, RemoteDatabaseObject> mapping = new HashMap<>(remoteDatasetCaseInsensitiveCache.getAllPresent(remoteDatasetCaseInsensitiveCache.asMap().keySet()));
for (DatasetId datasetId : datasetIds.get()) {
DatasetId newCacheKey = datasetIdToLowerCase(datasetId);
RemoteDatabaseObject newValue = RemoteDatabaseObject.of(datasetId.getDataset());
mapping.merge(newCacheKey, newValue, (currentValue, collision) -> currentValue.registerCollision(collision.getOnlyRemoteName()));
updateCache(remoteDatasetCaseInsensitiveCache, newCacheKey, newValue);
}
return Optional.ofNullable(remoteDatasetCaseInsensitiveCache.getIfPresent(cacheKey));

return Optional.ofNullable(mapping.get(cacheKey));
}

public Optional<RemoteDatabaseObject> toRemoteTable(ConnectorSession session, String projectId, String remoteDatasetName, String tableName)
Expand Down Expand Up @@ -207,13 +211,15 @@ private Optional<RemoteDatabaseObject> toRemoteTable(String projectId, String re
}

// Get all information from BigQuery and update cache from all fetched information
Map<TableId, RemoteDatabaseObject> mapping = new HashMap<>(remoteTableCaseInsensitiveCache.getAllPresent(remoteTableCaseInsensitiveCache.asMap().keySet()));
for (TableId table : tableIds.get()) {
TableId newCacheKey = tableIdToLowerCase(table);
RemoteDatabaseObject newValue = RemoteDatabaseObject.of(table.getTable());
mapping.merge(newCacheKey, newValue, (currentValue, collision) -> currentValue.registerCollision(collision.getOnlyRemoteName()));
updateCache(remoteTableCaseInsensitiveCache, newCacheKey, newValue);
}

return Optional.ofNullable(remoteTableCaseInsensitiveCache.getIfPresent(cacheKey));
return Optional.ofNullable(mapping.get(cacheKey));
}

private static <T> void updateCache(Cache<T, RemoteDatabaseObject> caseInsensitiveCache, T newCacheKey, RemoteDatabaseObject newValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 io.trino.plugin.bigquery;

import com.google.common.collect.ImmutableMap;
import io.trino.testing.QueryRunner;

final class TestBigQueryCaseInsensitiveMapping
extends BaseBigQueryCaseInsensitiveMapping
{
@Override
protected QueryRunner createQueryRunner()
throws Exception
{
return BigQueryQueryRunner.builder()
.setConnectorProperties(ImmutableMap.<String, String>builder()
.put("bigquery.case-insensitive-name-matching", "true")
.put("bigquery.case-insensitive-name-matching.cache-ttl", "0m")
.buildOrThrow())
.build();
}
}

0 comments on commit 9f267c1

Please sign in to comment.