estimateCardinality method

double estimateCardinality({
  1. required String table,
  2. required String? column,
  3. required String op,
  4. dynamic value,
  5. dynamic low,
  6. dynamic high,
})

Estimate output cardinality after applying a predicate.

Implementation

double estimateCardinality({
  required String table,
  required String? column,
  required String op,
  dynamic value,
  dynamic low,
  dynamic high,
}) {
  final ts = _stats[table];
  if (ts == null) return 1000.0; // unknown table → pessimistic estimate

  final baseRows = ts.rowCount.toDouble();
  if (column == null) return baseRows;

  final cs = ts.column(column);
  if (cs == null) return baseRows * 0.3; // unknown column

  final sel = switch (op) {
    '='        => cs.equalitySelectivity(),
    '<' || '<=' || '>' || '>=' =>
        cs.rangeSelectivity(low, high),
    'BETWEEN'  => cs.rangeSelectivity(low, high),
    _          => 0.3,
  };

  return (baseRows * sel).clamp(1.0, baseRows);
}