Skip to content

fix: qualify stg_* table references with the staging dataset#145

Merged
C00ldudeNoonan merged 3 commits into
mainfrom
fix/issue-143-stg-table-wrong-dataset
Jul 11, 2026
Merged

fix: qualify stg_* table references with the staging dataset#145
C00ldudeNoonan merged 3 commits into
mainfrom
fix/issue-143-stg-table-wrong-dataset

Conversation

@C00ldudeNoonan

Copy link
Copy Markdown
Owner

Summary

Fixes #143fear_greed_composite.py and entropy_complexity.py queried stg_major_indices, stg_sp500_companies_prices, stg_fred_series, and stg_fixed_income unqualified. BigQueryWarehouseResource.execute_query() resolves unqualified names against its own default dataset (economics_raw), but these are dbt staging models living in economics_staging (dbt_project.yml: staging: +schema: economics_staging).

Fix

  • bigquery_warehouse.py: new default_dataset_for_schema(schema) helper — mirrors the existing inline environment-suffix logic (prod/staging/dev), generalized so any schema can be resolved consistently. (checks.py already had a private, near-identical _dbt_dataset() for the same purpose — this makes the pattern importable rather than re-inventing it a third time.)
  • Both files: qualify every stg_* reference with default_dataset_for_schema("economics_staging").

Verification

Ran both jobs directly against a live BigQuery target before and after:

  • Before: 404 Not found: Table ...economics_raw_dev.stg_major_indices
  • After: no more table-not-found errors anywhere. entropy_complexity_signals computed and wrote 682 rows successfully — the main asset now completes end-to-end. fear_greed_signals gets 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:

  1. 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.
  2. fear_greed_job: a genuine division by zero in the safe-haven-demand component query (fear_greed_composite.py around 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

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>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +422 to +423
environment = os.getenv("ENVIRONMENT", "dev")
suffix = {"prod": "", "staging": "_staging"}.get(environment, "_dev")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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>
@C00ldudeNoonan

Copy link
Copy Markdown
Owner Author

Applied the Codex suggestion (aa85f2e): default_dataset_for_schema() now resolves the suffix from DBT_TARGET first (falling back to ENVIRONMENT), matching what profiles.yml/generate_schema_name.sql actually key off.

Turned out this local deployment's own container has exactly the mismatch Codex described: DBT_TARGET=prod but ENVIRONMENT=LOCAL. Under the old logic this silently read economics_staging_dev; dbt actually materializes to economics_staging. It only worked because both happen to be backfilled right now. Re-verified entropy_complexity_job against live BigQuery post-fix — still succeeds (682 rows), now correctly reading economics_staging.

Added 6 unit tests locking in the precedence.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@C00ldudeNoonan
C00ldudeNoonan merged commit 189fa23 into main Jul 11, 2026
1 check passed
@C00ldudeNoonan
C00ldudeNoonan deleted the fix/issue-143-stg-table-wrong-dataset branch July 11, 2026 19:45
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.

fear_greed_job / entropy_complexity_job: unqualified stg_* references resolve against wrong dataset

1 participant