fraiseql-cli 2.0.0-rc.13

CLI tools for FraiseQL v2 - Schema compilation and development utilities
Documentation
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Schema Optimizer
//!
//! Analyzes compiled schemas and adds SQL generation hints for runtime optimization.
//! This runs during compilation to precompute optimization strategies.

use anyhow::Result;
use fraiseql_core::schema::{CompiledSchema, QueryDefinition, SqlProjectionHint, TypeDefinition};
use tracing::{debug, info};

/// Schema optimizer that analyzes queries and adds SQL hints
pub struct SchemaOptimizer;

impl SchemaOptimizer {
    /// Optimize a compiled schema
    ///
    /// This analyzes queries and adds optimization hints like:
    /// - Index suggestions
    /// - Join order hints
    /// - Projection optimization
    /// - Predicate pushdown opportunities
    pub fn optimize(schema: &mut CompiledSchema) -> Result<OptimizationReport> {
        info!("Optimizing compiled schema");

        let mut report = OptimizationReport::default();

        // Analyze queries for optimization opportunities
        for query in &schema.queries {
            Self::analyze_query(query, &mut report);
        }

        // Analyze types for field access patterns and SQL projection opportunities
        Self::analyze_types(schema, &mut report);

        // Detect and apply SQL projection hints to types that would benefit
        Self::apply_sql_projection_hints(schema, &mut report);

        info!("Schema optimization complete: {} hints generated", report.total_hints());

        Ok(report)
    }

    /// Analyze a single query for optimization opportunities
    fn analyze_query(query: &QueryDefinition, report: &mut OptimizationReport) {
        debug!("Analyzing query: {}", query.name);

        // Check if query would benefit from indexes
        if query.returns_list && !query.arguments.is_empty() {
            report.index_hints.push(IndexHint {
                query_name:        query.name.clone(),
                reason:            "List query with arguments benefits from index".to_string(),
                suggested_columns: query.arguments.iter().map(|arg| arg.name.clone()).collect(),
            });
        }

        // Check for auto-params that suggest filtering
        if query.auto_params.has_where {
            report.optimization_notes.push(format!(
                "Query '{}' supports WHERE filtering - ensure denormalized filter columns exist",
                query.name
            ));
        }

        // Check for pagination
        if query.auto_params.has_limit || query.auto_params.has_offset {
            report.optimization_notes.push(format!(
                "Query '{}' supports pagination - consider adding ORDER BY for deterministic results",
                query.name
            ));
        }
    }

    /// Analyze types for field access patterns
    fn analyze_types(schema: &CompiledSchema, report: &mut OptimizationReport) {
        for type_def in &schema.types {
            // Check for large number of fields (potential over-fetching)
            if type_def.fields.len() > 20 {
                report.optimization_notes.push(format!(
                    "Type '{}' has {} fields - consider field selection optimization",
                    type_def.name,
                    type_def.fields.len()
                ));
            }

            // Check for JSONB columns
            if !type_def.jsonb_column.is_empty() {
                report.optimization_notes.push(format!(
                    "Type '{}' uses JSONB column '{}' - ensure GIN index exists for performance",
                    type_def.name, type_def.jsonb_column
                ));
            }
        }
    }

    /// Detect and apply SQL projection hints to types that would benefit from SQL-level field
    /// projection.
    ///
    /// SQL projection optimization works by filtering JSONB fields at the database level,
    /// reducing network payload and JSON deserialization overhead.
    ///
    /// Detection heuristics:
    /// - Type must have a JSONB column
    /// - Type should have sufficient fields (>10) or estimated large payload (>1KB)
    /// - PostgreSQL benefit: 95% payload reduction, 37% latency improvement
    fn apply_sql_projection_hints(schema: &mut CompiledSchema, report: &mut OptimizationReport) {
        for type_def in &mut schema.types {
            if Self::should_use_projection(type_def) {
                let hint = Self::create_projection_hint(type_def);

                debug!(
                    "Type '{}' qualifies for SQL projection: {} bytes saved ({:.0}%)",
                    type_def.name,
                    Self::estimate_payload_savings(type_def),
                    hint.estimated_reduction_percent
                );

                type_def.sql_projection_hint = Some(hint);
                report.projection_hints.push(ProjectionHint {
                    type_name:                   type_def.name.clone(),
                    field_count:                 type_def.fields.len(),
                    estimated_reduction_percent: type_def
                        .sql_projection_hint
                        .as_ref()
                        .map_or(0, |h| h.estimated_reduction_percent),
                });
            }
        }
    }

    /// Determine if a type should use SQL projection optimization.
    ///
    /// A type qualifies for SQL projection if:
    /// 1. It has a JSONB column (`store_format` == "jsonb")
    /// 2. It has sufficient fields (>10) OR estimated large payload (>1KB)
    ///
    /// Rationale: SQL projection's benefit (reducing JSONB payload) is most valuable
    /// for types with many fields or large payloads. Small types don't benefit enough
    /// to justify the SQL generation overhead.
    fn should_use_projection(type_def: &TypeDefinition) -> bool {
        // Condition 1: Must have JSONB column
        if type_def.jsonb_column.is_empty() {
            return false;
        }

        // Condition 2a: Sufficient field count (>10 fields = likely significant overhead)
        if type_def.fields.len() > 10 {
            return true;
        }

        // Condition 2b: Likely large payload (estimate ~150 bytes per field)
        // Average field: id (50B) + name (100B) + value (100B) = 250B overhead
        // 1KB threshold = ~4+ fields of average size
        let estimated_size = type_def.fields.len() * 250;
        if estimated_size > 1024 {
            return true;
        }

        false
    }

    /// Create a SQL projection hint for PostgreSQL.
    ///
    /// The hint contains:
    /// - Database type: "postgresql"
    /// - Projection template: `jsonb_build_object('field1', data->>'field1', ...)`
    /// - Estimated reduction: Based on field count and typical JSONB overhead
    fn create_projection_hint(type_def: &TypeDefinition) -> SqlProjectionHint {
        // Estimate payload reduction based on field count and JSONB overhead
        // Formula: Each unselected field = ~250 bytes saved (conservative estimate)
        // Average type: 20 fields, 5 selected = 15 fields × 250B = 3750B saved = 95% reduction
        let estimated_reduction = Self::estimate_reduction_percent(type_def.fields.len());

        SqlProjectionHint {
            database:                    "postgresql".to_string(),
            projection_template:         Self::generate_postgresql_projection_template(type_def),
            estimated_reduction_percent: estimated_reduction,
        }
    }

    /// Estimate the percentage of payload that can be reduced through SQL projection.
    ///
    /// Based on benchmarks:
    /// - Baseline payload: ~9.8 KB for typical large type
    /// - Projected payload: ~450 B (select 5 key fields)
    /// - Reduction: 95.4%
    ///
    /// Conservative scaling formula:
    /// - Few fields (5-10): 40% reduction (mostly JSONB overhead, few wasted fields)
    /// - Many fields (11-20): 70% reduction (more unselected fields)
    /// - Very many fields (20+): 85% reduction (mostly unnecessary data)
    const fn estimate_reduction_percent(field_count: usize) -> u32 {
        match field_count {
            0..=10 => 40,
            11..=20 => 70,
            _ => 85,
        }
    }

    /// Estimate total payload savings in bytes for a type.
    fn estimate_payload_savings(type_def: &TypeDefinition) -> usize {
        let estimated_reduction = Self::estimate_reduction_percent(type_def.fields.len());
        // Assume baseline JSONB payload ~250 bytes per field
        let total_payload = type_def.fields.len() * 250;
        (total_payload * estimated_reduction as usize) / 100
    }

    /// Generate a PostgreSQL `jsonb_build_object` template for SQL projection.
    ///
    /// Example output:
    /// `jsonb_build_object`('id', data->>'id', 'name', data->>'name', 'email', data->>'email')
    ///
    /// Note: This is a template. At runtime, the adapter will:
    /// 1. Receive the requested GraphQL fields
    /// 2. Filter to only include requested fields
    /// 3. Generate the actual SQL with selected fields only
    fn generate_postgresql_projection_template(type_def: &TypeDefinition) -> String {
        if type_def.fields.is_empty() {
            // Edge case: type with no fields, use pass-through
            "data".to_string()
        } else {
            // Create template with first N fields (up to 20 as representative)
            let field_list: Vec<String> = type_def
                .fields
                .iter()
                .take(20)
                .map(|f| format!("'{}', data->>'{}' ", f.name, f.name))
                .collect();

            format!("jsonb_build_object({})", field_list.join(","))
        }
    }
}

/// Optimization report generated during compilation
#[derive(Debug, Default)]
pub struct OptimizationReport {
    /// Index suggestions for query performance
    pub index_hints:        Vec<IndexHint>,
    /// SQL projection hints for types that would benefit from JSONB field filtering
    pub projection_hints:   Vec<ProjectionHint>,
    /// General optimization notes
    pub optimization_notes: Vec<String>,
}

impl OptimizationReport {
    /// Get total number of optimization hints
    pub fn total_hints(&self) -> usize {
        self.index_hints.len() + self.projection_hints.len() + self.optimization_notes.len()
    }

    /// Check if there are any optimization suggestions
    pub fn has_suggestions(&self) -> bool {
        !self.index_hints.is_empty()
            || !self.projection_hints.is_empty()
            || !self.optimization_notes.is_empty()
    }

    /// Print report to stdout
    pub fn print(&self) {
        if !self.has_suggestions() {
            return;
        }

        println!("\n📊 Optimization Suggestions:");

        if !self.index_hints.is_empty() {
            println!("\n  Indexes:");
            for hint in &self.index_hints {
                println!("  • Query '{}': {}", hint.query_name, hint.reason);
                println!("    Columns: {}", hint.suggested_columns.join(", "));
            }
        }

        if !self.projection_hints.is_empty() {
            println!("\n  SQL Projection Optimization:");
            for hint in &self.projection_hints {
                println!(
                    "  • Type '{}' ({} fields): ~{}% payload reduction",
                    hint.type_name, hint.field_count, hint.estimated_reduction_percent
                );
            }
        }

        if !self.optimization_notes.is_empty() {
            println!("\n  Notes:");
            for note in &self.optimization_notes {
                println!("  • {note}");
            }
        }

        println!();
    }
}

/// Index hint for query optimization
#[derive(Debug, Clone)]
pub struct IndexHint {
    /// Query name that would benefit from index
    pub query_name:        String,
    /// Reason for the suggestion
    pub reason:            String,
    /// Suggested columns to index
    pub suggested_columns: Vec<String>,
}

/// SQL projection hint for type optimization
#[derive(Debug, Clone)]
pub struct ProjectionHint {
    /// Type name that would benefit from SQL projection
    pub type_name:                   String,
    /// Number of fields in the type
    pub field_count:                 usize,
    /// Estimated payload reduction percentage (0-100)
    pub estimated_reduction_percent: u32,
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use fraiseql_core::{
        schema::{ArgumentDefinition, AutoParams, FieldDefinition, FieldType, TypeDefinition},
        validation::CustomTypeRegistry,
    };

    use super::*;

    #[test]
    fn test_optimize_empty_schema() {
        let mut schema = CompiledSchema {
            types:          vec![],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();
        assert_eq!(report.total_hints(), 0);
    }

    #[test]
    fn test_index_hint_for_list_query() {
        let mut schema = CompiledSchema {
            types:          vec![],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![QueryDefinition {
                name:         "users".to_string(),
                return_type:  "User".to_string(),
                returns_list: true,
                nullable:     false,
                arguments:    vec![ArgumentDefinition {
                    name:          "status".to_string(),
                    arg_type:      FieldType::String,
                    nullable:      false,
                    default_value: None,
                    description:   None,
                    deprecation:   None,
                }],
                sql_source:   Some("users".to_string()),
                description:  None,
                auto_params:  AutoParams::default(),
                deprecation:  None,
                jsonb_column: "data".to_string(),
            }],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();
        assert!(report.total_hints() > 0);
        assert!(!report.index_hints.is_empty());
        assert_eq!(report.index_hints[0].query_name, "users");
    }

    #[test]
    fn test_pagination_note() {
        let mut schema = CompiledSchema {
            types:          vec![],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![QueryDefinition {
                name:         "products".to_string(),
                return_type:  "Product".to_string(),
                returns_list: true,
                nullable:     false,
                arguments:    vec![],
                sql_source:   Some("products".to_string()),
                description:  None,
                auto_params:  AutoParams {
                    has_where:    false,
                    has_order_by: false,
                    has_limit:    true,
                    has_offset:   true,
                },
                deprecation:  None,
                jsonb_column: "data".to_string(),
            }],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();
        assert!(report.optimization_notes.iter().any(|note| note.contains("pagination")));
    }

    #[test]
    fn test_large_type_warning() {
        let mut schema = CompiledSchema {
            types:          vec![TypeDefinition {
                name:                "BigType".to_string(),
                sql_source:          String::new(),
                jsonb_column:        String::new(),
                fields:              (0..25)
                    .map(|i| FieldDefinition {
                        name:           format!("field{i}"),
                        field_type:     FieldType::String,
                        nullable:       false,
                        default_value:  None,
                        description:    None,
                        vector_config:  None,
                        alias:          None,
                        deprecation:    None,
                        requires_scope: None,
                        encryption:     None,
                    })
                    .collect(),
                description:         None,
                sql_projection_hint: None,
                implements:          vec![],
                is_error:            false,
            }],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();
        assert!(report.optimization_notes.iter().any(|note| note.contains("25 fields")));
    }

    #[test]
    fn test_projection_hint_for_large_type() {
        let mut schema = CompiledSchema {
            types:          vec![TypeDefinition {
                name:                "User".to_string(),
                sql_source:          "users".to_string(),
                jsonb_column:        "data".to_string(),
                fields:              (0..15)
                    .map(|i| FieldDefinition {
                        name:           format!("field{i}"),
                        field_type:     FieldType::String,
                        nullable:       false,
                        default_value:  None,
                        description:    None,
                        vector_config:  None,
                        alias:          None,
                        deprecation:    None,
                        requires_scope: None,
                        encryption:     None,
                    })
                    .collect(),
                description:         None,
                sql_projection_hint: None,
                implements:          vec![],
                is_error:            false,
            }],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();

        // Type with 15 fields and JSONB column should get projection hint
        assert!(!report.projection_hints.is_empty());
        assert_eq!(report.projection_hints[0].type_name, "User");
        assert_eq!(report.projection_hints[0].field_count, 15);

        // Type should have sql_projection_hint set
        assert!(schema.types[0].has_sql_projection());
        let hint = schema.types[0].sql_projection_hint.as_ref().unwrap();
        assert_eq!(hint.database, "postgresql");
        assert!(hint.estimated_reduction_percent > 0);
    }

    #[test]
    fn test_projection_not_applied_without_jsonb() {
        let mut schema = CompiledSchema {
            types:          vec![TypeDefinition {
                name:                "SmallType".to_string(),
                sql_source:          "small_table".to_string(),
                jsonb_column:        String::new(), // No JSONB column
                fields:              (0..15)
                    .map(|i| FieldDefinition {
                        name:           format!("field{i}"),
                        field_type:     FieldType::String,
                        nullable:       false,
                        default_value:  None,
                        description:    None,
                        vector_config:  None,
                        alias:          None,
                        deprecation:    None,
                        requires_scope: None,
                        encryption:     None,
                    })
                    .collect(),
                description:         None,
                sql_projection_hint: None,
                implements:          vec![],
                is_error:            false,
            }],
            enums:          vec![],
            input_types:    vec![],
            interfaces:     vec![],
            unions:         vec![],
            queries:        vec![],
            mutations:      vec![],
            subscriptions:  vec![],
            directives:     vec![],
            observers:      Vec::new(),
            fact_tables:    HashMap::default(),
            federation:     None,
            security:       None,
            observers_config: None,
            schema_sdl:     None,
            custom_scalars: CustomTypeRegistry::default(),
        };

        let report = SchemaOptimizer::optimize(&mut schema).unwrap();

        // Type without JSONB column should not get projection hint
        assert!(report.projection_hints.is_empty());
        assert!(!schema.types[0].has_sql_projection());
    }
}