Skip to content

minor db merge queries#1374

Open
eranschweitzer wants to merge 2 commits into
blue-marble:developfrom
eranschweitzer:fix/minor-db-merge-queries
Open

minor db merge queries#1374
eranschweitzer wants to merge 2 commits into
blue-marble:developfrom
eranschweitzer:fix/minor-db-merge-queries

Conversation

@eranschweitzer
Copy link
Copy Markdown

Minor SQL cost query fixes

  1. in costs.py, INSERT INTO results_project_costs_operations_agg
    lists columns as (... variable_om_cost, fuel_cost, ...) but selects
    SUM(fuel_cost) AS fuel_cost first and
    SUM(variable_om_cost) AS variable_om_cost second. SQLite's INSERT
    is positional so the columns are stored swapped: the column named
    variable_om_cost actually holds period-aggregated fuel cost and
    vice versa. Reorder the SELECT to match the column list.
  2. results_project_costs_capacity_agg writes
    spinup_or_lookahead = 0 for non-spinup rows while
    results_project_costs_operations_agg (tx as well) write NULL,
    so the view's a.spinup_or_lookahead = b.spinup_or_lookahead join
    fails (NULL = 0 is UNKNOWN) and every operations / tx
    row is silently dropped. These are wrapped in COALESCE(..., 0) so
    the condition is robust to either convention.
  3. Views results_costs_by_period_load_zone
    and results_costs_by_period aggregate by period and included
    either joins or summation over
    results_transmission_hurdle_costs_by_timepoint_agg.
    However, this view does not include period.
    This was silently ignored before and is now removed for clarity.

1. in costs.py, `INSERT INTO results_project_costs_operations_agg`
   lists columns as `(... variable_om_cost, fuel_cost, ...)` but selects
   `SUM(fuel_cost) AS fuel_cost` first and
   `SUM(variable_om_cost) AS variable_om_cost` second. SQLite's INSERT
   is positional so the columns are stored swapped: the column named
   `variable_om_cost` actually holds period-aggregated fuel cost and
   vice versa. Reorder the SELECT to match the column list.
2. `results_project_costs_capacity_agg` writes
   `spinup_or_lookahead = 0` for non-spinup rows while
   `results_project_costs_operations_agg` (tx as well) write NULL,
   so the view's `a.spinup_or_lookahead = b.spinup_or_lookahead` join
   fails (NULL = 0 is UNKNOWN) and every operations / tx
   row is silently dropped. Wrap both sides in `COALESCE(..., 0)` so
   the condition is robust to either convention.
3. The 4th LEFT JOIN in  `results_project_costs_capacity_agg`
   aliases `results_transmission_hurdle_costs_by_timepoint_agg`
   as `e` but its ON clause references `d.scenario_id` etc., so the
   join is effectively against the previous table again rather than
   the aliased one. Switch the references to `e.`.

Issues found with help from Claude Opus 4.7.
Views `results_costs_by_period_load_zone`
and `results_costs_by_period` aggregate by period and included
either joins or summation over
`results_transmission_hurdle_costs_by_timepoint_agg`.
However, this view does not include period.
This was silently ignored before and is now removed for clarity.
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.

1 participant