explainPlan method

List<Map<String, dynamic>> explainPlan(
  1. Plan plan
)

Implementation

List<Map<String, dynamic>> explainPlan(Plan plan) {
  if (plan is CboSeqScanPlan) {
    return [
      {
        'operation': 'SeqScan',
        'table': plan.table,
        'estimated_cost': plan.estimate.totalCost.toStringAsFixed(2),
        'estimated_rows': plan.estimate.rows.toStringAsFixed(0),
        if (plan.actualRows != null) 'actual_rows': plan.actualRows,
        if (plan.actualTimeMs != null)
          'actual_time_ms': plan.actualTimeMs!.toStringAsFixed(2),
      }
    ];
  }

  if (plan is CboIndexScanPlan) {
    return [
      {
        'operation': 'IndexScan',
        'table': plan.table,
        'column': plan.column,
        'value': '${plan.value}',
        'estimated_cost': plan.estimate.totalCost.toStringAsFixed(2),
        'estimated_rows': plan.estimate.rows.toStringAsFixed(0),
        if (plan.actualRows != null) 'actual_rows': plan.actualRows,
        if (plan.actualTimeMs != null)
          'actual_time_ms': plan.actualTimeMs!.toStringAsFixed(2),
      }
    ];
  }

  if (plan is CboRangeScanPlan) {
    return [
      {
        'operation': 'RangeScan',
        'table': plan.table,
        'column': plan.column,
        'low': '${plan.low}',
        'high': '${plan.high}',
        'estimated_cost': plan.estimate.totalCost.toStringAsFixed(2),
        'estimated_rows': plan.estimate.rows.toStringAsFixed(0),
        if (plan.actualRows != null) 'actual_rows': plan.actualRows,
      }
    ];
  }

  if (plan is CboJoinPlan) {
    return plan.joinPlan.ops
        .map((op) => {
              'operation': op.method.name.toUpperCase(),
              'left': op.leftTable,
              'right': op.rightTable,
              'on': '${op.edge.leftColumn}=${op.edge.rightColumn}',
              'estimated_cost': op.cost.totalCost.toStringAsFixed(2),
              'estimated_rows': op.cost.rows.toStringAsFixed(0),
            })
        .toList();
  }

  // Fallback for rule-based plans
  if (plan is ScanPlan) {
    return [{'operation': 'SeqScan', 'table': plan.table}];
  }
  if (plan is IndexLookupPlan) {
    return [
      {
        'operation': 'IndexScan',
        'table': plan.table,
        'column': plan.column,
        'value': '${plan.value}'
      }
    ];
  }
  return [{'operation': plan.runtimeType.toString()}];
}