Search in sources :

Example 1 with TABLE

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);
}
Also used : TableId(com.google.cloud.bigquery.TableId) Logger(io.airlift.log.Logger) TableId(com.google.cloud.bigquery.TableId) BigQueryException(com.google.cloud.bigquery.BigQueryException) BaseHttpServiceException(com.google.cloud.http.BaseHttpServiceException) HashMap(java.util.HashMap) DatasetId(com.google.cloud.bigquery.DatasetId) Supplier(java.util.function.Supplier) BigQuery(com.google.cloud.bigquery.BigQuery) Duration(io.airlift.units.Duration) Dataset(com.google.cloud.bigquery.Dataset) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) VIEW(com.google.cloud.bigquery.TableDefinition.Type.VIEW) Verify.verify(com.google.common.base.Verify.verify) Schema(com.google.cloud.bigquery.Schema) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Job(com.google.cloud.bigquery.Job) TableResult(com.google.cloud.bigquery.TableResult) Table(com.google.cloud.bigquery.Table) ENGLISH(java.util.Locale.ENGLISH) TABLE(com.google.cloud.bigquery.TableDefinition.Type.TABLE) TableDefinition(com.google.cloud.bigquery.TableDefinition) BIGQUERY_AMBIGUOUS_OBJECT_NAME(io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_AMBIGUOUS_OBJECT_NAME) ImmutableSet(com.google.common.collect.ImmutableSet) JobInfo(com.google.cloud.bigquery.JobInfo) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) TrinoException(io.trino.spi.TrinoException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Streams.stream(com.google.common.collect.Streams.stream) List(java.util.List) DatasetInfo(com.google.cloud.bigquery.DatasetInfo) Optional(java.util.Optional) TableInfo(com.google.cloud.bigquery.TableInfo) Collections(java.util.Collections) Table(com.google.cloud.bigquery.Table) Optional(java.util.Optional) HashMap(java.util.HashMap)

Example 2 with TABLE

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);
    }
}
Also used : BIGQUERY_FAILED_TO_EXECUTE_QUERY(io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_FAILED_TO_EXECUTE_QUERY) ConnectorSplitManager(io.trino.spi.connector.ConnectorSplitManager) Logger(io.airlift.log.Logger) IntStream.range(java.util.stream.IntStream.range) NodeManager(io.trino.spi.NodeManager) TableId(com.google.cloud.bigquery.TableId) BigQueryException(com.google.cloud.bigquery.BigQueryException) Duration(io.airlift.units.Duration) FixedSplitSource(io.trino.spi.connector.FixedSplitSource) Inject(javax.inject.Inject) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) ImmutableList(com.google.common.collect.ImmutableList) VIEW(com.google.cloud.bigquery.TableDefinition.Type.VIEW) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) TableResult(com.google.cloud.bigquery.TableResult) TABLE(com.google.cloud.bigquery.TableDefinition.Type.TABLE) ReadSession(com.google.cloud.bigquery.storage.v1.ReadSession) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TrinoException(io.trino.spi.TrinoException) ConnectorSplitSource(io.trino.spi.connector.ConnectorSplitSource) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DynamicFilter(io.trino.spi.connector.DynamicFilter) Optional(java.util.Optional) TableInfo(com.google.cloud.bigquery.TableInfo) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) TableResult(com.google.cloud.bigquery.TableResult) TrinoException(io.trino.spi.TrinoException) TableInfo(com.google.cloud.bigquery.TableInfo) BigQueryException(com.google.cloud.bigquery.BigQueryException) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Aggregations

BigQueryException (com.google.cloud.bigquery.BigQueryException)2 TABLE (com.google.cloud.bigquery.TableDefinition.Type.TABLE)2 VIEW (com.google.cloud.bigquery.TableDefinition.Type.VIEW)2 TableId (com.google.cloud.bigquery.TableId)2 TableInfo (com.google.cloud.bigquery.TableInfo)2 TableResult (com.google.cloud.bigquery.TableResult)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 Logger (io.airlift.log.Logger)2 Duration (io.airlift.units.Duration)2 TrinoException (io.trino.spi.TrinoException)2 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Optional (java.util.Optional)2 BigQuery (com.google.cloud.bigquery.BigQuery)1 Dataset (com.google.cloud.bigquery.Dataset)1 DatasetId (com.google.cloud.bigquery.DatasetId)1 DatasetInfo (com.google.cloud.bigquery.DatasetInfo)1 Job (com.google.cloud.bigquery.Job)1 JobInfo (com.google.cloud.bigquery.JobInfo)1