use of com.google.cloud.bigquery.TableDefinition.Type.TABLE in project trino by trinodb.
the class BigQueryClient method toRemoteTable.
private Optional<RemoteDatabaseObject> toRemoteTable(String projectId, String remoteDatasetName, String tableName, Supplier<Iterable<Table>> tables) {
requireNonNull(projectId, "projectId is null");
requireNonNull(remoteDatasetName, "remoteDatasetName is null");
requireNonNull(tableName, "tableName is null");
verify(tableName.codePoints().noneMatch(Character::isUpperCase), "Expected table name from internal metadata to be lowercase: %s", tableName);
if (!caseInsensitiveNameMatching) {
return Optional.of(RemoteDatabaseObject.of(tableName));
}
TableId cacheKey = TableId.of(projectId, remoteDatasetName, tableName);
Map<TableId, Optional<RemoteDatabaseObject>> mapping = new HashMap<>();
for (Table table : tables.get()) {
mapping.merge(tableIdToLowerCase(table.getTableId()), Optional.of(RemoteDatabaseObject.of(table.getTableId().getTable())), (currentValue, collision) -> currentValue.map(current -> current.registerCollision(collision.get().getOnlyRemoteName())));
}
if (!mapping.containsKey(cacheKey)) {
// table doesn't exist
mapping.put(cacheKey, Optional.empty());
}
verify(mapping.containsKey(cacheKey));
return mapping.get(cacheKey);
}
use of com.google.cloud.bigquery.TableDefinition.Type.TABLE in project trino by trinodb.
the class BigQuerySplitManager method createEmptyProjection.
private List<BigQuerySplit> createEmptyProjection(ConnectorSession session, TableId remoteTableId, int actualParallelism, Optional<String> filter) {
BigQueryClient client = bigQueryClientFactory.create(session);
log.debug("createEmptyProjection(tableId=%s, actualParallelism=%s, filter=[%s])", remoteTableId, actualParallelism, filter);
try {
long numberOfRows;
if (filter.isPresent()) {
// count the rows based on the filter
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
} else {
// no filters, so we can take the value from the table info when the object is TABLE
TableInfo tableInfo = client.getTable(remoteTableId).orElseThrow(() -> new TableNotFoundException(new SchemaTableName(remoteTableId.getDataset(), remoteTableId.getTable())));
if (tableInfo.getDefinition().getType() == TABLE) {
numberOfRows = tableInfo.getNumRows().longValue();
} else if (tableInfo.getDefinition().getType() == VIEW) {
String sql = client.selectSql(remoteTableId, "COUNT(*)");
TableResult result = client.query(sql);
numberOfRows = result.iterateAll().iterator().next().get(0).getLongValue();
} else {
throw new TrinoException(NOT_SUPPORTED, "Unsupported table type: " + tableInfo.getDefinition().getType());
}
}
long rowsPerSplit = numberOfRows / actualParallelism;
// need to be added to one fo the split due to integer division
long remainingRows = numberOfRows - (rowsPerSplit * actualParallelism);
List<BigQuerySplit> splits = range(0, actualParallelism).mapToObj(ignored -> BigQuerySplit.emptyProjection(rowsPerSplit)).collect(toList());
splits.set(0, BigQuerySplit.emptyProjection(rowsPerSplit + remainingRows));
return splits;
} catch (BigQueryException e) {
throw new TrinoException(BIGQUERY_FAILED_TO_EXECUTE_QUERY, "Failed to compute empty projection", e);
}
}
Aggregations