Skip to content

server: report the statement type and CTAS destination of query jobs - #518

Open
ymatrix1 wants to merge 1 commit into
goccy:mainfrom
ymatrix1:fix/ctas-statement-type
Open

ymatrix1 wants to merge 1 commit into
goccy:mainfrom
ymatrix1:fix/ctas-statement-type

Conversation

@ymatrix1

Copy link
Copy Markdown

Fixes #370.

Problem

Every query job reports statistics.query.statementType = "SELECT", and a CREATE TABLE AS SELECT job has no configuration.query.destinationTable. dbt-bigquery branches on the statement type (connections.py, get_query_status): for SELECT it calls client.get_table(query_job.destination), so every dbt model build and unit test fails with:

'NoneType' object has no attribute 'path'

Measured with the Python client (client.query(sql)):

statement BigQuery before after
SELECT 1 AS id SELECT, destination set SELECT, destination set same
CREATE OR REPLACE TABLE ds.t AS SELECT 1 AS id CREATE_TABLE_AS_SELECT, destination = ds.t SELECT, destination=None CREATE_TABLE_AS_SELECT, destination = ds.t, num_rows readable
INSERT / UPDATE / DELETE same name SELECT same name
CREATE VIEW / DROP VIEW / DROP TABLE CREATE_VIEW / DROP_VIEW / DROP_TABLE SELECT as BigQuery
CREATE FUNCTION / CREATE TABLE FUNCTION CREATE_FUNCTION / CREATE_TABLE_FUNCTION SELECT as BigQuery
SELECT 1; SELECT 2 SCRIPT SELECT SCRIPT

Fix

  • statementType(query) classifies the statement lexically (comments and quoted text skipped) into the values the JobStatistics2.statementType API documentation lists. More than one statement, or a leading procedural keyword, is SCRIPT.
  • For CREATE TABLE / CREATE TABLE AS SELECT, set ddlTargetTable from the catalog change the statement produced (ChangedCatalog), and for CTAS also configuration.query.destinationTable, which is what BigQuery returns and what clients read the row count through.
  • Applied in both jobs.insert and jobs.query.

Tests

server/statement_type_test.go runs each statement above through the Go client and checks StatementType, and for CTAS the destination, DDLTargetTable and the destination's NumRows.

Before:

--- FAIL: TestJobStatementType/CREATE_TABLE_dataset1.st_plain_(id_INT64)
    statementType = "SELECT", want "CREATE_TABLE"
--- FAIL: TestJobStatementType/INSERT_INTO_dataset1.st_plain_(id)_VALUES_(1),_(2)
    statementType = "SELECT", want "INSERT"
... (11 of 13 subtests)
--- FAIL: TestJobStatementType/CREATE_OR_REPLACE_TABLE_AS_SELECT
    statementType = "SELECT", want CREATE_TABLE_AS_SELECT

After: all 13 subtests pass. go test ./... passes with no new failures against main. With this change dbt-bigquery unit tests run against the emulator without a client-side shim.

Every query job reported statistics.query.statementType=SELECT, and a
CREATE TABLE AS SELECT job had no configuration.query.destinationTable.
dbt-bigquery branches on statementType and, for SELECT, calls
client.get_table(job.destination), so every dbt model build against
the emulator failed with "'NoneType' object has no attribute 'path'".

Classify the statement lexically into the statementType values the
BigQuery API documents, and for CREATE TABLE statements set
ddlTargetTable (and, for CTAS, destinationTable) from the catalog
change the statement produced.
@ymatrix1
ymatrix1 marked this pull request as ready for review September 22, 2026 23:37
@ymatrix1

ymatrix1 commented Oct 9, 2026

Copy link
Copy Markdown
Author

Status note: this fix is already running in production use of the emulator. While these PRs wait for review we carry them, together with the related fixes, in a fork: https://github.com/Flip-to/bigquery-emulator (branch flipto/main, tag flipto-v1), whose SQL engine is https://github.com/Flip-to/googlesqlite. A dbt project's fixture test suite (28 tests) runs green on it except for tests that need external seed data, and the engine fork replays 6,136 recorded BigQuery query results as a regression test.

Happy to rebase, split or adjust anything here if that helps review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hard Coded Query Type creates Unexpected behaviour

1 participant