-- V016 Federated Semantic Execution
CREATE TABLE IF NOT EXISTS semantic_executions_v016 (id varchar(36) primary key,user_id varchar(36) not null references users(id),model_id varchar(36) not null references semantic_models_v015(id) on delete cascade,plan json not null default '{}',generated_sql text not null default '',status varchar(30) not null default 'queued',row_count integer not null default 0,duration_ms integer not null default 0,cache_hit boolean not null default false,cost_units integer not null default 0,error text not null default '',created_at timestamptz not null default now(),finished_at timestamptz);
CREATE TABLE IF NOT EXISTS semantic_query_cache_v016 (id varchar(36) primary key,cache_key varchar(64) unique not null,model_id varchar(36) not null references semantic_models_v015(id) on delete cascade,payload json not null default '{}',row_count integer not null default 0,expires_at timestamptz not null,created_at timestamptz not null default now());
CREATE TABLE IF NOT EXISTS semantic_cost_policies_v016 (id varchar(36) primary key,name varchar(180) unique not null,subject_type varchar(30) not null default 'role',subject_value varchar(180) not null,max_rows integer not null default 5000,max_dimensions integer not null default 8,max_metrics integer not null default 12,max_cost_units integer not null default 10000,timeout_seconds integer not null default 30,cache_ttl_seconds integer not null default 300,enabled boolean not null default true,created_by varchar(36) not null references users(id),created_at timestamptz not null default now());
CREATE TABLE IF NOT EXISTS semantic_materializations_v016 (id varchar(36) primary key,model_id varchar(36) not null references semantic_models_v015(id) on delete cascade,name varchar(180) unique not null,metrics json not null default '[]',dimensions json not null default '[]',filters json not null default '{}',schedule_cron varchar(120) not null,destination_object varchar(300) not null default '',status varchar(30) not null default 'draft',last_refresh_at timestamptz,last_row_count integer not null default 0,owner_id varchar(36) not null references users(id),created_at timestamptz not null default now());
