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 @@ -617,12 +617,23 @@ private void saveSessionIdIfPresent(TableResult tableResult) {
}

private StatementType getStatementType(ExecuteResult executeResult) {
// Fast path: Read statementType directly from TableResult
if (executeResult.tableResult.getStatementType() != null) {
return executeResult.tableResult.getStatementType();
}
// Jobful path: Read statementType from Job statistics when executed via JobCreationMode=1 or
// jobs.insert
if (executeResult.job != null && executeResult.job.getStatistics() instanceof QueryStatistics) {
return ((QueryStatistics) executeResult.job.getStatistics()).getStatementType();
}
// Fallback path: Lazily fetch completed Job metadata to resolve statementType without dry
// runs if omitted in TableResult
if (executeResult.tableResult.getJobId() != null && this.bigQuery != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.bigQuery != null is impossible check (if that's true we can't get to processing query)

Job job = this.bigQuery.getJob(executeResult.tableResult.getJobId());
if (job != null && job.getStatistics() instanceof QueryStatistics) {
return ((QueryStatistics) job.getStatistics()).getStatementType();
}
}
Comment thread
keshavdandeva marked this conversation as resolved.
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,4 +1136,27 @@ public void testStatelessDmlExecutionUsesTableResultWithoutDryRunOrGetJob() thro
verify(bigquery, Mockito.never()).create(any(JobInfo.class));
verify(bigquery, Mockito.never()).getJob(any(JobId.class));
}

@Test
public void testNullStatementTypeInTableResultFallsBackToGetJobWithoutDryRun() throws Exception {
TableResult tableResultMock = mock(TableResult.class);
doReturn(this.jobId).when(tableResultMock).getJobId();
doReturn(null).when(tableResultMock).getStatementType();
doReturn(0L).when(tableResultMock).getTotalRows();
doReturn(Schema.of()).when(tableResultMock).getSchema();

doReturn(tableResultMock)
.when(bigquery)
.queryWithTimeout(any(QueryJobConfiguration.class), any(), any());

Job jobMock = getJobMock(null, null, StatementType.CREATE_TABLE);
doReturn(jobMock).when(bigquery).getJob(eq(this.jobId));

boolean result = bigQueryStatement.execute("CREATE TABLE dataset.my_table (x INT64)");

assertFalse(result);
assertNull(bigQueryStatement.getResultSet());
verify(bigquery, Mockito.times(1)).getJob(eq(this.jobId));
verify(bigquery, Mockito.never()).create(any(JobInfo.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,34 @@ public void testSessionPersistenceAcrossQueries() throws SQLException {
}
}
}

@Test
public void testNonIdempotentCreateTableAndDrop() throws SQLException {
String tempTableName = "NON_IDEMPOTENT_TABLE_" + Math.abs(random.nextInt(100000));
String createTableQuery =
String.format("CREATE TABLE %s.%s (`id` INT64, `name` STRING);", DATASET, tempTableName);
String dropTableQuery = String.format("DROP TABLE %s.%s;", DATASET, tempTableName);

try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl);
Statement statement = connection.createStatement()) {
boolean hasResultSet = statement.execute(createTableQuery);
assertFalse(hasResultSet);
assertEquals(0, statement.getUpdateCount());

try (ResultSet rs =
statement.executeQuery(
String.format("SELECT count(*) FROM %s.%s;", DATASET, tempTableName))) {
assertTrue(rs.next());
assertEquals(0L, rs.getLong(1));
assertFalse(rs.next());
}
} finally {
try (Connection connection = DriverManager.getConnection(ITBase.connectionUrl);
Statement statement = connection.createStatement()) {
statement.execute(dropTableQuery);
} catch (SQLException ignored) {
// Ignore cleanup exception if table was not created
}
}
}
}
Loading