Skip to content

feat(cli): report BigQuery job statistics in task metadata - #2433

Open
dmargol1 wants to merge 5 commits into
dataform-co:mainfrom
dmargol1:fix/dry-run-job-statistics
Open

dmargol1 wants to merge 5 commits into
dataform-co:mainfrom
dmargol1:fix/dry-run-job-statistics

Conversation

@dmargol1

@dmargol1 dmargol1 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2432.

Problem

dataform run --dry-run --json returns "metadata": {} for successful dry-run tasks, even when BigQuery supplies statistics. createQueryJob reads every job through getQueryResultsStream(), but BigQuery doesn't save dry-run jobs, so the stream fails with Not found: Job and a workaround resolves empty results. The statistics (JobStatistics2) were already in the job resource returned on creation.

Changes

  1. Dry runs return statistics from the job resource and skip the results stream, which removes the workaround. The client library's own query() does the same for dry runs. Cancellation is still checked.
  2. BigqueryMetadata gains referenced_tables = 4 and schema_fields = 5, filled in with the existing total_bytes_processed for dry runs. A byte count BigQuery doesn't report is 0, as for real runs. job_id stays empty, since dry-run jobs aren't saved.
  3. Real runs report referenced_tables and schema_fields too, so metadata means the same thing in both modes: whatever BigQuery's job statistics report. Both modes use one extraction (jobStatisticsMetadata()), and real runs keep job_id and total_bytes_billed. The job metadata was already being fetched, so this adds no requests.

Example metadata ({low, high, unsigned} is how --json already prints int64 values):

{
  "bigquery": {
    "totalBytesProcessed": { "low": 2650191, "high": 0, "unsigned": false },
    "referencedTables": [{ "database": "bigquery-public-data", "schema": "samples", "name": "shakespeare" }],
    "schemaFields": [{ "name": "word", "primitive": "STRING" }, { "name": "c", "primitive": "INTEGER" }]
  }
}

Behavior

Dry-run errors detected at job creation still propagate, and nothing uses dry-run metadata to decide anything. Against real BigQuery, the released 3.1.5 CLI and this branch gave identical statuses and errors in the cases I tested (tables, views, operations, MERGE/INSERT/INSERT_OVERWRITE incrementals, pre-operations). The one difference: dry runs no longer make the results request, so its own failures can't fail a task.

What BigQuery reported in the cases I tested, by statement and mode (observed, not guaranteed):

Task SQL referencedTables (dry / real) schemaFields (dry / real)
CTAS, CREATE VIEW sources / sources ✅ / ✅
SELECT (e.g. a SELECT operation) sources / sources ✅ / ❌
INSERT, MERGE sources + target / sources + target ✅ / ❌
Script, e.g. DECLARE …; SELECT … ❌ / ❌ ✅ / ✅ (final SELECT's)
Script starting with DDL (e.g. INSERT_OVERWRITE) ❌ / ❌ ❌ / ❌

totalBytesProcessed is reported in every case, but for a dry run of a script starting with DDL it's 0 (a lower bound). For real scripts, referenced tables are only reported on the child jobs. BigQuery documents the schema as "present only for successful dry run of non-legacy SQL queries", so the real-run ✅ cells are observed, undocumented behavior, and consumers shouldn't rely on them.

  • Script dry runs are best effort, and accuracy isn't exposed yet, so a 0 may be an incomplete estimate. An accuracy field would be a small follow-up.
  • referencedTables: [] means none were reported. A query reading a view lists the view's underlying tables, not the view (in my tests, SELECT from a view reported only its source table). BigQuery reports views in a separate referencedLogicalViews field, which could be a small follow-up. schemaFields is BigQuery's result schema in Dataform's lossy Field form (e.g. RANGE becomes UNKNOWN), not necessarily the destination table's. The proto comments say this too. It's named schema_fields, like the Field list in jit.proto, because schema means the dataset in Target.
  • Assertion check queries are skipped in dry runs, as before, so those tasks still report metadata: {} and the SELECT row doesn't apply to them. In real runs they report like any SELECT.
  • For actions wrapped to create a missing dataset, dry-run metadata comes from the validation query (the SELECT plus pre-operations), as before. A real run instead executes the wrapper script, so the task that creates the dataset reports neither field. Later runs into that dataset aren't wrapped.

Tests

cli/api/dbadapters/bigquery_test.ts adds 10 tests. In the 8 dry-run tests, the mock's results stream fails with Not found: Job, like BigQuery's for dry runs. The 2 real-run tests cover full statistics and a MERGE-like job without a schema or byte counts. Eight fail on main: statistics mapping (nested/repeated fields, RANGE), missing statistics, int64 precision through --json, retries, and both real-run tests. Two pass on main too and guard existing behavior: error propagation and cancellation.

tests/api/schemas.spec.ts now gives the wrapper and validation query different metadata, the run --dry-run --json integration test checks metadata.bigquery against real BigQuery, and the run --json test checks that real runs report both fields. It doesn't check their contents, since the real-run schema is undocumented.

Unit tests, ESLint and Prettier pass. Integration tests pass against a personal project, except bigquery.spec > search, which fails identically on main (missing INFORMATION_SCHEMA permission). Real runs still report job IDs, bytes billed and runtime errors as before. The full run e2e suite passes.

🤖 Generated with Claude Code

@dmargol1
dmargol1 requested a review from a team as a code owner November 11, 2026 05:09
@dmargol1
dmargol1 requested review from zaptot and removed request for a team November 11, 2026 05:09

@Ford544 Ford544 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Task metadata is currently returned empty for dry runs because the metadata included for real runs largely does not apply to them (job_id is not populated and byte counts are only approximations). You suggest adding completely new kinds of information there (referenced tables and schema), just for dry runs - could you elaborate on what, in your view, makes those particular fields useful and worth including here?

If we do decide to include them, it would be preferable to also add them to real runs, for consistency - otherwise the same field (metadata) has completely different contents for real and dry runs, which would be confusing.

The convertFieldType() fix is good but it does not concern dry run statistics specifically. I suggest moving it to a seperate PR.

Comment thread cli/api/dbadapters/bigquery.ts Outdated
case "INT64":
return dataform.Field.Primitive.INTEGER;
case "NUMERIC":
case "BIGNUMERIC":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a good fix, but not directly related to the PR's main purpose. I would suggest seperating it into a different PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to #2446, along with its tests and the ESLint rule.

Comment thread cli/api/dbadapters/bigquery.ts Outdated
const statistics = jobMetadata?.statistics?.query || {};
return {
bigquery: {
// Left unset when not reported, so a missing count isn't reported as 0.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For real (non-dry) runs, totalBytesProcessed is set to 0 (see L578), so this would be an inconsistency. I would prefer to stick with the current behavior here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 7abe968: a count BigQuery doesn't report is now 0, as in real runs.

@dmargol1 dmargol1 changed the title fix(cli): report BigQuery job statistics for dry runs fix(cli): report BigQuery job statistics for dry runs, and referenced tables and schema in all runs Nov 13, 2026
@dmargol1

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @Ford544.

Why these fields? The dry-run job Dataform already submits returns them, so keeping them is nearly free, and the change is additive for --json consumers. A tool could take each statement from run --build-only --json and dry-run it itself, but then it has to submit and match up those dry runs on its own, duplicating Dataform's if it also runs --dry-run validation. Uses from our pipelines:

  • Column documentation drift. setMetadata silently skips a columns entry for a renamed or removed column, so its description disappears. The dry-run schema lets CI catch that before anything is built.
  • SELECT *. Parsing SQL alone, without the source tables' schemas, can't resolve a wildcard. The dry-run schema does.
  • Hard-coded table names. referencedTables can be compared with declared dependencies to flag likely undeclared references. It's a heuristic: DML can include the target, scripts report none, and views appear as their underlying tables.

Consistency. Agreed. Dry and real runs now share one extraction, so metadata holds whatever BigQuery reports in either mode. Real runs gain referencedTables, plus schema where BigQuery provides it (it's documented as dry-run only). The updated description has the details, and I've retitled the PR to match.

convertFieldType(): split into #2446.

totalBytesProcessed: now 0 when unreported, as in real runs.

@Ford544

Ford544 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

/gcbrun

@Ford544 Ford544 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for making the changes and explaining the rationale behind including referencedTables and schema.

@rafal-hawrylak rafal-hawrylak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Assertion check queries are skipped in dry runs, so their task still prints metadata: {}. Can we note that so the SELECT row is not read as covering assertions?

This PR adds new output fields, so feat(cli) fits better than fix(cli). A shorter title would also help.

Comment thread protos/db_adapter.proto Outdated
// documents it for dry runs only; real runs may include it for some
// statements, but not reliably. Uses Dataform's lossy Field representation:
// e.g. RANGE becomes UNKNOWN, and nullability and type parameters aren't kept.
repeated Field schema = 5;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could we name this schema_fields? A list of Field is called fields in TableMetadata and schema_fields in jit.proto, and schema means the dataset in the Target entries right next to it. The name is hard to change once released.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed to schema_fields (schemaFields in --json). The jit.proto precedent and the clash with Target.schema are good reasons.

Dry runs now return metadata built from the job resource returned on
creation (bytes processed, referenced tables, result schema) instead of
reading the results stream, which fails with "Not found: Job" for dry
runs and was turned into empty metadata by a workaround.

Adds referenced_tables and schema to BigqueryMetadata, fixes duplicate
case labels in convertFieldType() so BIGNUMERIC, JSON and INTERVAL get
their own primitives, and enables ESLint's no-duplicate-case.

Fixes dataform-co#2432
Real runs now report referenced_tables and schema whenever BigQuery's job
statistics include them, using the same extraction as dry runs. Dry runs
report a total_bytes_processed BigQuery didn't supply as 0, like real runs.
@dmargol1
dmargol1 force-pushed the fix/dry-run-job-statistics branch from 7abe968 to b121632 Compare November 24, 2026 17:02
@dmargol1 dmargol1 changed the title fix(cli): report BigQuery job statistics for dry runs, and referenced tables and schema in all runs feat(cli): report BigQuery job statistics in task metadata Nov 24, 2026
@dmargol1

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @rafal-hawrylak.

  • schema_fields: renamed (see inline).
  • Assertions: right, their check queries are skipped in dry runs, so those tasks still report metadata: {}. I've changed the table's SELECT example to an operation and added a note.
  • Title: changed to feat(cli): report BigQuery job statistics in task metadata.

I also rebased onto main. Since #2446 has merged, the branch keeps its field type mapping.

This branch has not been deployed

No deployments
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.

run --dry-run --json drops BigQuery job statistics (bytes, referenced tables, schema)

4 participants