File size: 5,332 Bytes
56c063a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
-- Feature Engineering MCP Server Database Schema
-- Creates tables for feature engineering operations and explanations with user isolation

-- Enable UUID extension if not already enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Feature engineering plans table
CREATE TABLE IF NOT EXISTS feature_engineering_plans (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    hypotheses_used JSONB DEFAULT '[]',
    interaction_features JSONB DEFAULT '[]',
    transformation_features JSONB DEFAULT '[]',
    aggregation_features JSONB DEFAULT '[]',
    created_at TIMESTAMP DEFAULT NOW()
);

-- Engineered features table
CREATE TABLE IF NOT EXISTS engineered_features (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    plan_id UUID NOT NULL REFERENCES feature_engineering_plans(id) ON DELETE CASCADE,
    engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    feature_name VARCHAR NOT NULL,
    feature_type VARCHAR NOT NULL, -- 'interaction', 'transformation', 'aggregation'
    source_columns JSONB DEFAULT '[]',
    formula TEXT,
    hypothesis_basis VARCHAR,
    description TEXT,
    importance_score FLOAT,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Feature complexity metrics table
CREATE TABLE IF NOT EXISTS feature_complexity_metrics (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    total_features INTEGER DEFAULT 0,
    original_features INTEGER DEFAULT 0,
    engineered_features INTEGER DEFAULT 0,
    interaction_features INTEGER DEFAULT 0,
    transformation_features INTEGER DEFAULT 0,
    aggregation_features INTEGER DEFAULT 0,
    complexity_score FLOAT DEFAULT 0.0,
    dimensionality_reduction_applied BOOLEAN DEFAULT false,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Feature validation results table
CREATE TABLE IF NOT EXISTS feature_validations (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    engineered_dataset_id UUID NOT NULL REFERENCES user_datasets(id) ON DELETE CASCADE,
    user_id VARCHAR NOT NULL,
    has_data_leakage BOOLEAN DEFAULT false,
    leakage_warnings JSONB DEFAULT '[]',
    validation_errors JSONB DEFAULT '[]',
    validation_passed BOOLEAN DEFAULT true,
    validated_at TIMESTAMP DEFAULT NOW()
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_feature_engineering_plans_dataset_id ON feature_engineering_plans(dataset_id);
CREATE INDEX IF NOT EXISTS idx_feature_engineering_plans_user_id ON feature_engineering_plans(user_id);
CREATE INDEX IF NOT EXISTS idx_engineered_features_plan_id ON engineered_features(plan_id);
CREATE INDEX IF NOT EXISTS idx_engineered_features_dataset_id ON engineered_features(engineered_dataset_id);
CREATE INDEX IF NOT EXISTS idx_engineered_features_user_id ON engineered_features(user_id);
CREATE INDEX IF NOT EXISTS idx_feature_complexity_metrics_dataset_id ON feature_complexity_metrics(engineered_dataset_id);
CREATE INDEX IF NOT EXISTS idx_feature_complexity_metrics_user_id ON feature_complexity_metrics(user_id);
CREATE INDEX IF NOT EXISTS idx_feature_validations_dataset_id ON feature_validations(engineered_dataset_id);
CREATE INDEX IF NOT EXISTS idx_feature_validations_user_id ON feature_validations(user_id);

-- Enable Row Level Security (RLS)
ALTER TABLE feature_engineering_plans ENABLE ROW LEVEL SECURITY;
ALTER TABLE engineered_features ENABLE ROW LEVEL SECURITY;
ALTER TABLE feature_complexity_metrics ENABLE ROW LEVEL SECURITY;
ALTER TABLE feature_validations ENABLE ROW LEVEL SECURITY;

-- Create RLS policies for user isolation
-- Users can only access their own data

-- Feature engineering plans policies
DROP POLICY IF EXISTS feature_engineering_plans_isolation ON feature_engineering_plans;
CREATE POLICY feature_engineering_plans_isolation ON feature_engineering_plans
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Engineered features policies
DROP POLICY IF EXISTS engineered_features_isolation ON engineered_features;
CREATE POLICY engineered_features_isolation ON engineered_features
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Feature complexity metrics policies
DROP POLICY IF EXISTS feature_complexity_metrics_isolation ON feature_complexity_metrics;
CREATE POLICY feature_complexity_metrics_isolation ON feature_complexity_metrics
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Feature validations policies
DROP POLICY IF EXISTS feature_validations_isolation ON feature_validations;
CREATE POLICY feature_validations_isolation ON feature_validations
    FOR ALL TO public
    USING (user_id = current_setting('app.current_user_id', true));

-- Grant necessary permissions
GRANT ALL ON feature_engineering_plans TO public;
GRANT ALL ON engineered_features TO public;
GRANT ALL ON feature_complexity_metrics TO public;
GRANT ALL ON feature_validations TO public;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO public;