---
name: State
lang: cpp
domain: core
version: "1.0.0"
algebraic: product
definition: |
  struct State {
      std::vector<Stage>     stages;
      int                    active_stage;
      ViewMode               display;
      ViewConfig             viz_config;
      std::vector<VizPanel>  extra_panels;
      std::vector<Join>      joins;
      std::string            main_source;
      std::vector<ColorRule> color_rules;
      std::vector<bool>      col_visible;
      std::vector<int>       col_order;
  };
description: "Estado completo de una query TQL: pipeline de stages + joins + viz config + UI tweaks. Round-trip a Lua via tql_emit/tql_apply."
tags: [tql, state, pipeline, product-type]
uses_types: [Stage_cpp_core, Filter_cpp_core, Op_cpp_core]
file_path: "cpp/functions/core/data_table_types.h"
---

## Notas

State es el documento canonico de una query del usuario. Atomico — toda mutacion pasa por helpers pure (`apply_drill_step`, `drill_up`, etc.).

`active_stage` = idx del stage cuyo output se renderiza. Filters/sorts del Raw siempre se aplican antes; joins se materializan ANTES de stages[0].

Helpers `raw()`, `active()` garantizan `stages[0]` existe (lazy init en `ensure_stage0`).

## Usado por

- `data_table_cpp_viz` (UI render principal)
- `compute_pipeline_cpp_core` (resuelve hasta active_stage)
- `tql_emit_cpp_core` / `tql_apply_cpp_core` (Lua serializacion)
- `tql_to_sql_cpp_core` → SQL DuckDB CTE chain
- `apply_drill_step` / `undo_drill_step` / `drill_up`
