ai_data_scientist / shared /sql /hypothesis_testing_schema.sql
VAH6002's picture
Upload 264 files
56c063a verified
Raw History Blame Contribute Delete
3.97 kB
-- Hypothesis Testing MCP Server Database Schema
-- Creates tables for hypotheses and test results with user isolation
-- Enable UUID extension if not already enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Hypotheses table
CREATE TABLE IF NOT EXISTS hypotheses (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id VARCHAR NOT NULL,
dataset_id UUID NOT NULL,
text TEXT NOT NULL,
test_type VARCHAR NOT NULL CHECK (test_type IN ('chi_square', 't_test', 'anova', 'correlation')),
variables JSONB NOT NULL DEFAULT '[]',
expected_outcome TEXT NOT NULL,
python_code TEXT,
business_relevance TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
-- Hypothesis results table
CREATE TABLE IF NOT EXISTS hypothesis_results (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
hypothesis_id UUID NOT NULL REFERENCES hypotheses(id) ON DELETE CASCADE,
user_id VARCHAR NOT NULL,
p_value FLOAT NOT NULL,
is_significant BOOLEAN NOT NULL,
confidence_interval JSONB, -- [lower, upper] bounds
effect_size FLOAT,
interpretation TEXT NOT NULL,
test_statistic FLOAT,
significance_level FLOAT DEFAULT 0.05,
created_at TIMESTAMP DEFAULT NOW()
);
-- Statistical test metadata table (for tracking test parameters and diagnostics)
CREATE TABLE IF NOT EXISTS test_metadata (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
hypothesis_result_id UUID NOT NULL REFERENCES hypothesis_results(id) ON DELETE CASCADE,
user_id VARCHAR NOT NULL,
test_parameters JSONB DEFAULT '{}', -- Test-specific parameters
diagnostics JSONB DEFAULT '{}', -- Test diagnostics (e.g., assumptions checks)
sample_size INTEGER,
degrees_of_freedom INTEGER,
created_at TIMESTAMP DEFAULT NOW()
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_hypotheses_user_id ON hypotheses(user_id);
CREATE INDEX IF NOT EXISTS idx_hypotheses_dataset_id ON hypotheses(dataset_id);
CREATE INDEX IF NOT EXISTS idx_hypotheses_test_type ON hypotheses(test_type);
CREATE INDEX IF NOT EXISTS idx_hypotheses_created_at ON hypotheses(created_at);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_hypothesis_id ON hypothesis_results(hypothesis_id);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_user_id ON hypothesis_results(user_id);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_is_significant ON hypothesis_results(is_significant);
CREATE INDEX IF NOT EXISTS idx_hypothesis_results_created_at ON hypothesis_results(created_at);
CREATE INDEX IF NOT EXISTS idx_test_metadata_hypothesis_result_id ON test_metadata(hypothesis_result_id);
CREATE INDEX IF NOT EXISTS idx_test_metadata_user_id ON test_metadata(user_id);
-- Enable Row Level Security (RLS)
ALTER TABLE hypotheses ENABLE ROW LEVEL SECURITY;
ALTER TABLE hypothesis_results ENABLE ROW LEVEL SECURITY;
ALTER TABLE test_metadata ENABLE ROW LEVEL SECURITY;
-- Create RLS policies for user isolation
-- Users can only access their own data
-- Hypotheses policies
DROP POLICY IF EXISTS hypotheses_isolation ON hypotheses;
CREATE POLICY hypotheses_isolation ON hypotheses
FOR ALL TO public
USING (user_id = current_setting('app.current_user_id', true));
-- Hypothesis results policies
DROP POLICY IF EXISTS hypothesis_results_isolation ON hypothesis_results;
CREATE POLICY hypothesis_results_isolation ON hypothesis_results
FOR ALL TO public
USING (user_id = current_setting('app.current_user_id', true));
-- Test metadata policies
DROP POLICY IF EXISTS test_metadata_isolation ON test_metadata;
CREATE POLICY test_metadata_isolation ON test_metadata
FOR ALL TO public
USING (user_id = current_setting('app.current_user_id', true));
-- Grant necessary permissions
GRANT USAGE ON SCHEMA public TO public;
GRANT ALL ON ALL TABLES IN SCHEMA public TO public;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO public;