Skip to content
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 @@ -50,6 +50,7 @@ public class BigQueryClient
private final Optional<String> viewMaterializationProject;
private final Optional<String> viewMaterializationDataset;
private final String tablePrefix = "_pbc_";
private final boolean caseSensitiveNameMatching;

// presto converts the dataset and table names to lower case, while BigQuery is case sensitive
private final ConcurrentMap<TableId, TableId> tableIds = new ConcurrentHashMap<>();
Expand All @@ -60,6 +61,7 @@ public class BigQueryClient
this.bigQuery = requireNonNull(bigQuery, "bigQuery is null");
this.viewMaterializationProject = requireNonNull(config.getViewMaterializationProject(), "viewMaterializationProject is null");
this.viewMaterializationDataset = requireNonNull(config.getViewMaterializationDataset(), "viewMaterializationDataset is null");
this.caseSensitiveNameMatching = config.isCaseSensitiveNameMatching();
}

public TableInfo getTable(TableId tableId)
Expand Down Expand Up @@ -108,7 +110,7 @@ private void addTableMappingIfNeeded(DatasetId datasetID, Table table)
private Dataset addDataSetMappingIfNeeded(Dataset dataset)
{
DatasetId bigQueryDatasetId = dataset.getDatasetId();
DatasetId prestoDatasetId = DatasetId.of(bigQueryDatasetId.getProject(), bigQueryDatasetId.getDataset().toLowerCase(ENGLISH));
DatasetId prestoDatasetId = DatasetId.of(bigQueryDatasetId.getProject(), bigQueryDatasetId.getDataset());
datasetIds.putIfAbsent(prestoDatasetId, bigQueryDatasetId);
return dataset;
}
Expand All @@ -123,7 +125,8 @@ protected TableId createDestinationTable(TableId tableId)

private String createTableName()
{
return format(tablePrefix + "%s", randomUUID().toString().toLowerCase(ENGLISH).replace("-", ""));
String uuid = randomUUID().toString().replace("-", "");
return caseSensitiveNameMatching ? format("%s%s", tablePrefix, uuid) : format("%s%s", tablePrefix, uuid).toLowerCase(ENGLISH);
}

private DatasetId mapIfNeeded(String project, String dataset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class BigQueryConfig
private Optional<String> parentProjectId = Optional.empty();
private OptionalInt parallelism = OptionalInt.empty();
private boolean viewsEnabled;
private boolean caseSensitiveNameMatching;
private Optional<String> viewMaterializationProject = Optional.empty();
private Optional<String> viewMaterializationDataset = Optional.empty();
private int maxReadRowsRetries = DEFAULT_MAX_READ_ROWS_RETRIES;
Expand Down Expand Up @@ -181,6 +182,22 @@ public BigQueryConfig setMaxReadRowsRetries(int maxReadRowsRetries)
return this;
}

public boolean isCaseSensitiveNameMatching()
{
return caseSensitiveNameMatching;
}

@Config("case-sensitive-name-matching")
@ConfigDescription(
"Case sensitivity for schema and table name matching. " +
"true = preserve case and require exact matches; " +
"false (default) = normalize to lower case and match case-insensitively.")
public BigQueryConfig setCaseSensitiveNameMatching(boolean caseSensitiveNameMatching)
{
this.caseSensitiveNameMatching = caseSensitiveNameMatching;
return this;
}

ReadSessionCreatorConfig createReadSessionCreatorConfig()
{
return new ReadSessionCreatorConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static com.google.cloud.bigquery.TableDefinition.Type.TABLE;
import static com.google.cloud.bigquery.TableDefinition.Type.VIEW;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toMap;

Expand All @@ -63,12 +64,14 @@ public class BigQueryMetadata
private static final Logger log = Logger.get(BigQueryMetadata.class);
private final BigQueryClient bigQueryClient;
private final String projectId;
private final boolean caseSensitiveNameMatching;

@Inject
public BigQueryMetadata(BigQueryClient bigQueryClient, BigQueryConfig config)
{
this.bigQueryClient = bigQueryClient;
this.projectId = config.getProjectId().orElse(bigQueryClient.getProjectId());
this.caseSensitiveNameMatching = config.isCaseSensitiveNameMatching();
}

@Override
Expand Down Expand Up @@ -233,4 +236,10 @@ private List<SchemaTableName> listTables(ConnectorSession session, SchemaTablePr
ImmutableList.of(tableName) :
ImmutableList.of(); // table does not exist
}

@Override
public String normalizeIdentifier(ConnectorSession session, String identifier)
{
return caseSensitiveNameMatching ? identifier : identifier.toLowerCase(ENGLISH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void testDefaults()
.setParallelism(20)
.setViewMaterializationProject("vmproject")
.setViewMaterializationDataset("vmdataset")
.setMaxReadRowsRetries(10);
.setMaxReadRowsRetries(10)
.setCaseSensitiveNameMatching(false);

assertEquals(config.getCredentialsKey(), Optional.of("ckey"));
assertEquals(config.getCredentialsFile(), Optional.of("cfile"));
Expand All @@ -46,6 +47,7 @@ public void testDefaults()
assertEquals(config.getViewMaterializationProject(), Optional.of("vmproject"));
assertEquals(config.getViewMaterializationDataset(), Optional.of("vmdataset"));
assertEquals(config.getMaxReadRowsRetries(), 10);
assertEquals(config.isCaseSensitiveNameMatching(), false);
}

@Test
Expand All @@ -59,6 +61,7 @@ public void testExplicitPropertyMappingsWithCredentialsKey()
.put("bigquery.view-materialization-project", "vmproject")
.put("bigquery.view-materialization-dataset", "vmdataset")
.put("bigquery.max-read-rows-retries", "10")
.put("case-sensitive-name-matching", "true")
.build();

ConfigurationFactory configurationFactory = new ConfigurationFactory(properties);
Expand All @@ -71,6 +74,7 @@ public void testExplicitPropertyMappingsWithCredentialsKey()
assertEquals(config.getViewMaterializationProject(), Optional.of("vmproject"));
assertEquals(config.getViewMaterializationDataset(), Optional.of("vmdataset"));
assertEquals(config.getMaxReadRowsRetries(), 10);
assertEquals(config.isCaseSensitiveNameMatching(), true);
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions presto-docs/src/main/sphinx/connector/bigquery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ Property Description
``bigquery.max-read-rows-retries`` The number of retries in case of retryable server issues ``3``
``bigquery.credentials-key`` credentials key (base64 encoded) None. See `authentication <#authentication>`_
``bigquery.credentials-file`` JSON credentials file path None. See `authentication <#authentication>`_
``case-sensitive-name-matching`` Enable case sensitive identifier support for schema and table ``false``
names for the connector. When disabled, names are matched
case-insensitively using lowercase normalization.
========================================= ============================================================== ==============================================

Data Types
Expand Down
Loading