Search in sources :

Example 1 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class ConnectionImpl method processQueryResponseResults.

@VisibleForTesting
BigQueryResult processQueryResponseResults(com.google.api.services.bigquery.model.QueryResponse results) {
    Schema schema;
    long numRows;
    schema = Schema.fromPb(results.getSchema());
    numRows = results.getTotalRows() == null ? 0 : // in case of DML or DDL
    results.getTotalRows().longValue();
    // QueryResponse only provides cache hits, dmlStats, and sessionInfo as query processing
    // statistics
    DmlStats dmlStats = results.getDmlStats() == null ? null : DmlStats.fromPb(results.getDmlStats());
    Boolean cacheHit = results.getCacheHit();
    QueryStatistics queryStatistics = QueryStatistics.newBuilder().setDmlStats(dmlStats).setCacheHit(cacheHit).build();
    // We cannot directly set sessionInfo in QueryStatistics
    SessionInfo sessionInfo = results.getSessionInfo() == null ? null : JobStatistics.SessionInfo.fromPb(results.getSessionInfo());
    BigQueryResultStats bigQueryResultStats = new BigQueryResultStatsImpl(queryStatistics, sessionInfo);
    bufferFvl = new LinkedBlockingDeque<>(getBufferSize());
    BlockingQueue<Tuple<Iterable<FieldValueList>, Boolean>> pageCache = new LinkedBlockingDeque<>(getPageCacheSize(connectionSettings.getNumBufferedRows(), schema));
    BlockingQueue<Tuple<TableDataList, Boolean>> rpcResponseQueue = new LinkedBlockingDeque<>(getPageCacheSize(connectionSettings.getNumBufferedRows(), schema));
    JobId jobId = JobId.fromPb(results.getJobReference());
    // Thread to make rpc calls to fetch data from the server
    runNextPageTaskAsync(results.getPageToken(), getDestinationTable(jobId), rpcResponseQueue);
    // Thread to parse data received from the server to client library objects
    parseRpcDataAsync(results.getRows(), schema, pageCache, rpcResponseQueue);
    // Thread to populate the buffer (a blocking queue) shared with the consumer
    populateBufferAsync(rpcResponseQueue, pageCache, bufferFvl);
    return new BigQueryResultImpl<AbstractList<FieldValue>>(schema, numRows, bufferFvl, bigQueryResultStats);
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ArrowSchema(com.google.cloud.bigquery.storage.v1.ArrowSchema) SessionInfo(com.google.cloud.bigquery.JobStatistics.SessionInfo) QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Tuple(com.google.cloud.Tuple) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class ITBigQueryTest method testConnectionImplDryRun.

@Test
public void testConnectionImplDryRun() throws SQLException {
    String query = String.format("select StringField,  BigNumericField, BooleanField, BytesField, IntegerField, TimestampField, FloatField, NumericField, TimeField, DateField,  DateTimeField , GeographyField, RecordField.BytesField, RecordField.BooleanField, IntegerArrayField from %s where StringField = ? order by TimestampField", TABLE_ID_FASTQUERY_BQ_RESULTSET.getTable());
    ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setCreateSession(true).build();
    Connection connection = bigquery.createConnection(connectionSettings);
    BigQueryDryRunResult bigQueryDryRunResultSet = connection.dryRun(query);
    assertNotNull(bigQueryDryRunResultSet.getSchema());
    assertEquals(BQ_RESULTSET_EXPECTED_SCHEMA, // match the schema
    bigQueryDryRunResultSet.getSchema());
    List<Parameter> queryParameters = bigQueryDryRunResultSet.getQueryParameters();
    assertEquals(StandardSQLTypeName.STRING, queryParameters.get(0).getValue().getType());
    QueryStatistics queryStatistics = bigQueryDryRunResultSet.getStatistics().getQueryStatistics();
    assertNotNull(queryStatistics);
    SessionInfo sessionInfo = bigQueryDryRunResultSet.getStatistics().getSessionInfo();
    assertNotNull(sessionInfo.getSessionId());
    assertEquals(StatementType.SELECT, queryStatistics.getStatementType());
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Connection(com.google.cloud.bigquery.Connection) BigQueryDryRunResult(com.google.cloud.bigquery.BigQueryDryRunResult) Parameter(com.google.cloud.bigquery.Parameter) SessionInfo(com.google.cloud.bigquery.JobStatistics.SessionInfo) ConnectionSettings(com.google.cloud.bigquery.ConnectionSettings) Test(org.junit.Test)

Example 3 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class JobTest method testWaitForAndGetQueryResultsEmpty.

@Test
public void testWaitForAndGetQueryResultsEmpty() throws InterruptedException {
    QueryJobConfiguration jobConfig = QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
    QueryStatistics jobStatistics = QueryStatistics.newBuilder().setCreationTimestamp(1L).setEndTime(3L).setStartTime(2L).build();
    JobInfo jobInfo = JobInfo.newBuilder(jobConfig).setJobId(JOB_ID).setStatistics(jobStatistics).setJobId(JOB_ID).setEtag(ETAG).setGeneratedId(GENERATED_ID).setSelfLink(SELF_LINK).setUserEmail(EMAIL).setStatus(JOB_STATUS).build();
    when(mockOptions.getClock()).thenReturn(CurrentMillisClock.getDefaultClock());
    Job completedJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build();
    QueryResponse completedQuery = QueryResponse.newBuilder().setCompleted(true).setTotalRows(0).setSchema(Schema.of()).setErrors(ImmutableList.<BigQueryError>of()).build();
    when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
    when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
    when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
    when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
    job = this.job.toBuilder().setConfiguration(jobConfig).build();
    assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameInstanceAs(completedJob);
    assertThat(job.getQueryResults().iterateAll()).isEmpty();
    verify(bigquery, times(2)).getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS);
    verify(bigquery).getJob(JOB_INFO.getJobId());
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Test(org.junit.Test)

Example 4 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class JobTest method testWaitForAndGetQueryResultsEmptyWithSchema.

@Test
public void testWaitForAndGetQueryResultsEmptyWithSchema() throws InterruptedException {
    QueryJobConfiguration jobConfig = QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
    QueryStatistics jobStatistics = QueryStatistics.newBuilder().setCreationTimestamp(1L).setEndTime(3L).setStartTime(2L).build();
    JobInfo jobInfo = JobInfo.newBuilder(jobConfig).setJobId(JOB_ID).setStatistics(jobStatistics).setJobId(JOB_ID).setEtag(ETAG).setGeneratedId(GENERATED_ID).setSelfLink(SELF_LINK).setUserEmail(EMAIL).setStatus(JOB_STATUS).build();
    JobStatus status = mock(JobStatus.class);
    when(bigquery.getOptions()).thenReturn(mockOptions);
    when(mockOptions.getClock()).thenReturn(CurrentMillisClock.getDefaultClock());
    Job completedJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build();
    QueryResponse completedQuery = QueryResponse.newBuilder().setCompleted(true).setTotalRows(0).setSchema(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN))).setErrors(ImmutableList.<BigQueryError>of()).build();
    when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
    when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
    when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
    job = this.job.toBuilder().setConfiguration(jobConfig).build();
    assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameInstanceAs(completedJob);
    assertThat(job.getQueryResults().getSchema()).isEqualTo(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)));
    verify(bigquery, times(2)).getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS);
    verify(bigquery).getJob(JOB_INFO.getJobId());
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Test(org.junit.Test)

Example 5 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class ITBigQueryTest method testQuerySessionSupport.

@Test
public void testQuerySessionSupport() throws InterruptedException {
    String query = "CREATE TEMPORARY TABLE temptable AS SELECT 17 as foo";
    QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setCreateSession(true).build();
    Job remoteJob = bigquery.create(JobInfo.of(queryJobConfiguration));
    remoteJob = remoteJob.waitFor();
    assertNull(remoteJob.getStatus().getError());
    Job queryJob = bigquery.getJob(remoteJob.getJobId());
    JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
    String sessionId = statistics.getSessionInfo().getSessionId();
    assertNotNull(sessionId);
    String queryTempTable = "SELECT * FROM temptable";
    ConnectionProperty connectionProperty = ConnectionProperty.newBuilder().setKey("session_id").setValue(sessionId).build();
    QueryJobConfiguration queryJobConfigurationWithSession = QueryJobConfiguration.newBuilder(queryTempTable).setDefaultDataset(DatasetId.of(DATASET)).setConnectionProperties(ImmutableList.of(connectionProperty)).build();
    Job remoteJobWithSession = bigquery.create(JobInfo.of(queryJobConfigurationWithSession));
    remoteJobWithSession = remoteJobWithSession.waitFor();
    assertNull(remoteJobWithSession.getStatus().getError());
    Job queryJobWithSession = bigquery.getJob(remoteJobWithSession.getJobId());
    QueryStatistics statisticsWithSession = queryJobWithSession.getStatistics();
    assertEquals(sessionId, statisticsWithSession.getSessionInfo().getSessionId());
}
Also used : JobStatistics(com.google.cloud.bigquery.JobStatistics) QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) ConnectionProperty(com.google.cloud.bigquery.ConnectionProperty) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Test(org.junit.Test)

Aggregations

QueryStatistics (com.google.cloud.bigquery.JobStatistics.QueryStatistics)8 Test (org.junit.Test)5 SessionInfo (com.google.cloud.bigquery.JobStatistics.SessionInfo)4 ArrowSchema (com.google.cloud.bigquery.storage.v1.ArrowSchema)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 BetaApi (com.google.api.core.BetaApi)1 QueryParameter (com.google.api.services.bigquery.model.QueryParameter)1 Tuple (com.google.cloud.Tuple)1 BigQueryDryRunResult (com.google.cloud.bigquery.BigQueryDryRunResult)1 Connection (com.google.cloud.bigquery.Connection)1 ConnectionProperty (com.google.cloud.bigquery.ConnectionProperty)1 ConnectionSettings (com.google.cloud.bigquery.ConnectionSettings)1 Job (com.google.cloud.bigquery.Job)1 JobStatistics (com.google.cloud.bigquery.JobStatistics)1 Parameter (com.google.cloud.bigquery.Parameter)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1