Search in sources :

Example 1 with QueryParameter

use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.

the class QueryRequest method namedParameterToPb.

static QueryParameter namedParameterToPb(Map.Entry<String, QueryParameterValue> entry) {
    QueryParameter queryParameterPb = new QueryParameter();
    queryParameterPb.setName(entry.getKey());
    queryParameterPb.setParameterValue(entry.getValue().toValuePb());
    queryParameterPb.setParameterType(entry.getValue().toTypePb());
    return queryParameterPb;
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter)

Example 2 with QueryParameter

use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.

the class QueryRequest method fromPb.

static QueryRequest fromPb(com.google.api.services.bigquery.model.QueryRequest queryRequestPb) {
    Builder builder = newBuilder(queryRequestPb.getQuery());
    if (queryRequestPb.getQueryParameters() != null && !queryRequestPb.getQueryParameters().isEmpty()) {
        if (queryRequestPb.getQueryParameters().get(0).getName() == null) {
            builder.setPositionalParameters(Lists.transform(queryRequestPb.getQueryParameters(), POSITIONAL_PARAMETER_FROM_PB_FUNCTION));
        } else {
            Map<String, QueryParameterValue> values = Maps.newHashMap();
            for (QueryParameter queryParameterPb : queryRequestPb.getQueryParameters()) {
                checkNotNull(queryParameterPb.getName());
                QueryParameterValue value = QueryParameterValue.fromPb(queryParameterPb.getParameterValue(), queryParameterPb.getParameterType());
                values.put(queryParameterPb.getName(), value);
            }
            builder.setNamedParameters(values);
        }
    }
    if (queryRequestPb.getMaxResults() != null) {
        builder.setPageSize(queryRequestPb.getMaxResults());
    }
    if (queryRequestPb.getDefaultDataset() != null) {
        builder.setDefaultDataset(DatasetId.fromPb(queryRequestPb.getDefaultDataset()));
    }
    if (queryRequestPb.getTimeoutMs() != null) {
        builder.setMaxWaitTime(queryRequestPb.getTimeoutMs());
    }
    if (queryRequestPb.getDryRun() != null) {
        builder.setDryRun(queryRequestPb.getDryRun());
    }
    if (queryRequestPb.getUseQueryCache() != null) {
        builder.setUseQueryCache(queryRequestPb.getUseQueryCache());
    }
    if (queryRequestPb.getUseLegacySql() != null) {
        builder.setUseLegacySql(queryRequestPb.getUseLegacySql());
    }
    return builder.build();
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter)

Example 3 with QueryParameter

use of com.google.api.services.bigquery.model.QueryParameter in project java-bigquery by googleapis.

the class ConnectionImpl method createQueryJob.

// Used by jobs.getQueryResults API endpoint
@VisibleForTesting
com.google.api.services.bigquery.model.Job createQueryJob(String sql, ConnectionSettings connectionSettings, List<Parameter> queryParameters, Map<String, String> labels) {
    com.google.api.services.bigquery.model.JobConfiguration configurationPb = new com.google.api.services.bigquery.model.JobConfiguration();
    JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
    queryConfigurationPb.setQuery(sql);
    if (queryParameters != null) {
        if (queryParameters.get(0).getName() == null) {
            // If query parameter name is unset, then assume mode is positional
            queryConfigurationPb.setParameterMode("POSITIONAL");
            // pass query parameters
            List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
            queryConfigurationPb.setQueryParameters(queryParametersPb);
        } else {
            queryConfigurationPb.setParameterMode("NAMED");
            // pass query parameters
            List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
            queryConfigurationPb.setQueryParameters(queryParametersPb);
        }
    }
    if (connectionSettings.getDestinationTable() != null) {
        queryConfigurationPb.setDestinationTable(connectionSettings.getDestinationTable().toPb());
    }
    if (connectionSettings.getTableDefinitions() != null) {
        queryConfigurationPb.setTableDefinitions(Maps.transformValues(connectionSettings.getTableDefinitions(), ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
    }
    if (connectionSettings.getUserDefinedFunctions() != null) {
        queryConfigurationPb.setUserDefinedFunctionResources(connectionSettings.getUserDefinedFunctions().stream().map(UserDefinedFunction.TO_PB_FUNCTION).collect(Collectors.toList()));
    }
    if (connectionSettings.getCreateDisposition() != null) {
        queryConfigurationPb.setCreateDisposition(connectionSettings.getCreateDisposition().toString());
    }
    if (connectionSettings.getWriteDisposition() != null) {
        queryConfigurationPb.setWriteDisposition(connectionSettings.getWriteDisposition().toString());
    }
    if (connectionSettings.getDefaultDataset() != null) {
        queryConfigurationPb.setDefaultDataset(connectionSettings.getDefaultDataset().toPb());
    }
    if (connectionSettings.getPriority() != null) {
        queryConfigurationPb.setPriority(connectionSettings.getPriority().toString());
    }
    if (connectionSettings.getAllowLargeResults() != null) {
        queryConfigurationPb.setAllowLargeResults(connectionSettings.getAllowLargeResults());
    }
    if (connectionSettings.getUseQueryCache() != null) {
        queryConfigurationPb.setUseQueryCache(connectionSettings.getUseQueryCache());
    }
    if (connectionSettings.getFlattenResults() != null) {
        queryConfigurationPb.setFlattenResults(connectionSettings.getFlattenResults());
    }
    if (connectionSettings.getMaximumBillingTier() != null) {
        queryConfigurationPb.setMaximumBillingTier(connectionSettings.getMaximumBillingTier());
    }
    if (connectionSettings.getMaximumBytesBilled() != null) {
        queryConfigurationPb.setMaximumBytesBilled(connectionSettings.getMaximumBytesBilled());
    }
    if (connectionSettings.getSchemaUpdateOptions() != null) {
        ImmutableList.Builder<String> schemaUpdateOptionsBuilder = new ImmutableList.Builder<>();
        for (JobInfo.SchemaUpdateOption schemaUpdateOption : connectionSettings.getSchemaUpdateOptions()) {
            schemaUpdateOptionsBuilder.add(schemaUpdateOption.name());
        }
        queryConfigurationPb.setSchemaUpdateOptions(schemaUpdateOptionsBuilder.build());
    }
    if (connectionSettings.getDestinationEncryptionConfiguration() != null) {
        queryConfigurationPb.setDestinationEncryptionConfiguration(connectionSettings.getDestinationEncryptionConfiguration().toPb());
    }
    if (connectionSettings.getTimePartitioning() != null) {
        queryConfigurationPb.setTimePartitioning(connectionSettings.getTimePartitioning().toPb());
    }
    if (connectionSettings.getClustering() != null) {
        queryConfigurationPb.setClustering(connectionSettings.getClustering().toPb());
    }
    if (connectionSettings.getRangePartitioning() != null) {
        queryConfigurationPb.setRangePartitioning(connectionSettings.getRangePartitioning().toPb());
    }
    if (connectionSettings.getConnectionProperties() != null) {
        queryConfigurationPb.setConnectionProperties(connectionSettings.getConnectionProperties().stream().map(ConnectionProperty.TO_PB_FUNCTION).collect(Collectors.toList()));
    }
    if (connectionSettings.getCreateSession() != null) {
        queryConfigurationPb.setCreateSession(connectionSettings.getCreateSession());
    }
    if (connectionSettings.getJobTimeoutMs() != null) {
        configurationPb.setJobTimeoutMs(connectionSettings.getJobTimeoutMs());
    }
    if (labels != null) {
        configurationPb.setLabels(labels);
    }
    // The new Connection interface only supports StandardSQL dialect
    queryConfigurationPb.setUseLegacySql(false);
    configurationPb.setQuery(queryConfigurationPb);
    com.google.api.services.bigquery.model.Job jobPb = JobInfo.of(QueryJobConfiguration.fromPb(configurationPb)).toPb();
    com.google.api.services.bigquery.model.Job queryJob;
    try {
        queryJob = BigQueryRetryHelper.runWithRetries(() -> bigQueryRpc.createJobForQuery(jobPb), bigQueryOptions.getRetrySettings(), BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, bigQueryOptions.getClock(), retryConfig);
    } catch (BigQueryRetryHelper.BigQueryRetryHelperException e) {
        throw BigQueryException.translateAndThrow(e);
    }
    return queryJob;
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter) JobConfigurationQuery(com.google.api.services.bigquery.model.JobConfigurationQuery) ImmutableList(com.google.common.collect.ImmutableList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with QueryParameter

use of com.google.api.services.bigquery.model.QueryParameter in project google-cloud-java by GoogleCloudPlatform.

the class QueryRequest method positionalParameterToPb.

static QueryParameter positionalParameterToPb(QueryParameterValue value) {
    QueryParameter queryParameterPb = new QueryParameter();
    queryParameterPb.setParameterValue(value.toValuePb());
    queryParameterPb.setParameterType(value.toTypePb());
    return queryParameterPb;
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter)

Example 5 with QueryParameter

use of com.google.api.services.bigquery.model.QueryParameter in project java-bigquery by googleapis.

the class ConnectionImpl method createQueryRequest.

// Used for job.query API endpoint
@VisibleForTesting
QueryRequest createQueryRequest(ConnectionSettings connectionSettings, String sql, List<Parameter> queryParameters, Map<String, String> labels) {
    QueryRequest content = new QueryRequest();
    String requestId = UUID.randomUUID().toString();
    if (connectionSettings.getConnectionProperties() != null) {
        content.setConnectionProperties(connectionSettings.getConnectionProperties().stream().map(ConnectionProperty.TO_PB_FUNCTION).collect(Collectors.toList()));
    }
    if (connectionSettings.getDefaultDataset() != null) {
        content.setDefaultDataset(connectionSettings.getDefaultDataset().toPb());
    }
    if (connectionSettings.getMaximumBytesBilled() != null) {
        content.setMaximumBytesBilled(connectionSettings.getMaximumBytesBilled());
    }
    if (connectionSettings.getMaxResults() != null) {
        content.setMaxResults(connectionSettings.getMaxResults());
    }
    if (queryParameters != null) {
        // content.setQueryParameters(queryParameters);
        if (queryParameters.get(0).getName() == null) {
            // If query parameter name is unset, then assume mode is positional
            content.setParameterMode("POSITIONAL");
            // pass query parameters
            List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
            content.setQueryParameters(queryParametersPb);
        } else {
            content.setParameterMode("NAMED");
            // pass query parameters
            List<QueryParameter> queryParametersPb = Lists.transform(queryParameters, NAMED_PARAMETER_TO_PB_FUNCTION);
            content.setQueryParameters(queryParametersPb);
        }
    }
    if (connectionSettings.getCreateSession() != null) {
        content.setCreateSession(connectionSettings.getCreateSession());
    }
    if (labels != null) {
        content.setLabels(labels);
    }
    content.setQuery(sql);
    content.setRequestId(requestId);
    // The new Connection interface only supports StandardSQL dialect
    content.setUseLegacySql(false);
    return content;
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter) QueryRequest(com.google.api.services.bigquery.model.QueryRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

QueryParameter (com.google.api.services.bigquery.model.QueryParameter)7 JobConfigurationQuery (com.google.api.services.bigquery.model.JobConfigurationQuery)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2 BetaApi (com.google.api.core.BetaApi)1 QueryRequest (com.google.api.services.bigquery.model.QueryRequest)1 SchemaUpdateOption (com.google.cloud.bigquery.JobInfo.SchemaUpdateOption)1 QueryStatistics (com.google.cloud.bigquery.JobStatistics.QueryStatistics)1 SessionInfo (com.google.cloud.bigquery.JobStatistics.SessionInfo)1 ArrowSchema (com.google.cloud.bigquery.storage.v1.ArrowSchema)1 List (java.util.List)1