fix: qualify stg_* table references with the staging dataset#145
Conversation
fear_greed_composite.py and entropy_complexity.py queried stg_major_indices,
stg_sp500_companies_prices, stg_fred_series, and stg_fixed_income
unqualified, which BigQueryWarehouseResource resolves against its own
default dataset (economics_raw). These are dbt staging models living in
economics_staging per dbt_project.yml's schema config, so the queries
404'd.
- bigquery_warehouse.py: new default_dataset_for_schema(schema) helper,
mirroring the existing inline environment-suffix logic (and checks.py's
private _dbt_dataset()) so cross-dataset references can be qualified
consistently.
- fear_greed_composite.py, entropy_complexity.py: qualify every stg_*
reference with default_dataset_for_schema("economics_staging").
Verified against a live BigQuery target: both jobs' main asset
computations now succeed end-to-end (entropy_complexity_signals wrote
682 rows; fear_greed_signals reaches its final query stage). No more
"table not found" errors for any stg_* reference.
Both jobs still fail overall, on two separate, unrelated, pre-existing
bugs surfaced only once the dataset-resolution error stopped masking
them (filed separately, not fixed here):
- entropy_complexity_job: the shared signal data-quality check
(signals/checks.py _check_signal_table) uses DuckDB-only syntax
(DESCRIBE, ISNAN, ISINF) that BigQuery rejects — affects all 5 signal
checks, not just this one.
- fear_greed_job: division by zero in the safe-haven-demand component
query (fear_greed_composite.py line ~127) — a real data issue, not a
dataset-reference bug.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 23d24d42db
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| environment = os.getenv("ENVIRONMENT", "dev") | ||
| suffix = {"prod": "", "staging": "_staging"}.get(environment, "_dev") |
There was a problem hiding this comment.
Resolve dbt dataset suffixes from DBT_TARGET
When Dagster builds or observes dbt with DBT_TARGET=staging while ENVIRONMENT is still prod (the production compose file fixes ENVIRONMENT: prod but allows DBT_TARGET to vary), dbt creates models in economics_staging_staging per profiles.yml:2 and generate_schema_name.sql:3-12, but this helper returns economics_staging. The newly qualified stg_* queries will then read the wrong dataset or 404 in that context; derive this suffix from the active dbt target (DBT_TARGET, with ENVIRONMENT only as a fallback) rather than ENVIRONMENT alone.
Useful? React with 👍 / 👎.
Codex review on #145: default_dataset_for_schema() derived the schema suffix from ENVIRONMENT, but dbt's own profiles.yml (target: env_var('DBT_TARGET', 'dev')) and generate_schema_name.sql key off DBT_TARGET. The two can diverge — confirmed live on this deployment's own dagster_user_code container: DBT_TARGET=prod but ENVIRONMENT=LOCAL. Under the old ENVIRONMENT-only logic this resolved to economics_staging_dev; dbt actually materializes to economics_staging (DBT_TARGET=prod). It only "worked" because both dataset variants happen to be backfilled right now. Now reads DBT_TARGET first, falling back to ENVIRONMENT only when DBT_TARGET is unset. Added 6 unit tests covering the precedence (DBT_TARGET wins even when ENVIRONMENT disagrees, ENVIRONMENT fallback, default-to-dev). Re-verified entropy_complexity_job against live BigQuery: still writes 682 rows, now correctly reading from economics_staging (the true DBT_TARGET=prod schema) instead of economics_staging_dev. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Applied the Codex suggestion (aa85f2e): Turned out this local deployment's own container has exactly the mismatch Codex described: Added 6 unit tests locking in the precedence. |
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Summary
Fixes #143 —
fear_greed_composite.pyandentropy_complexity.pyqueriedstg_major_indices,stg_sp500_companies_prices,stg_fred_series, andstg_fixed_incomeunqualified.BigQueryWarehouseResource.execute_query()resolves unqualified names against its own default dataset (economics_raw), but these are dbt staging models living ineconomics_staging(dbt_project.yml:staging: +schema: economics_staging).Fix
bigquery_warehouse.py: newdefault_dataset_for_schema(schema)helper — mirrors the existing inline environment-suffix logic (prod/staging/dev), generalized so any schema can be resolved consistently. (checks.pyalready had a private, near-identical_dbt_dataset()for the same purpose — this makes the pattern importable rather than re-inventing it a third time.)stg_*reference withdefault_dataset_for_schema("economics_staging").Verification
Ran both jobs directly against a live BigQuery target before and after:
404 Not found: Table ...economics_raw_dev.stg_major_indicesentropy_complexity_signalscomputed and wrote 682 rows successfully — the main asset now completes end-to-end.fear_greed_signalsgets through 4 of 5 query components successfully.Not fixed here — two separate, pre-existing bugs surfaced once the dataset error stopped masking them
Both jobs still fail overall, but on unrelated root causes:
entropy_complexity_job: the shared signal data-quality check (signals/checks.py::_check_signal_table) uses DuckDB-only SQL (DESCRIBE {table},ISNAN(),ISINF()) that BigQuery rejects. This helper is shared by all 5 signal checks (absorption_ratio, turbulence_index, fear_greed, entropy_complexity, network_correlation), so it likely blocks all of them, not just this one.fear_greed_job: a genuinedivision by zeroin the safe-haven-demand component query (fear_greed_composite.pyaround line 127) — a data issue in the SQL logic itself, not a reference/dataset bug.Filing these separately to keep this PR scoped to the dataset-resolution fix.
🤖 Generated with Claude Code