diff --git a/lib/core/database/compact_result_dataset.dart b/lib/core/database/compact_result_dataset.dart index 50f8e5d9..13627607 100644 --- a/lib/core/database/compact_result_dataset.dart +++ b/lib/core/database/compact_result_dataset.dart @@ -217,6 +217,10 @@ class CompactResultDataset { /// /// An optional [cellFormatter] callback can be provided to customize display string /// formatting for driver-specific types (e.g. blobs, timestamps, boolean tinyints). + /// Non-null cells per column that go through the cell formatter while + /// [fromRawRows] decides the column's kind; the rest are type-tested. + static const int _formattedSamples = 60; + static CompactResultDataset fromRawRows( List columnNames, List> rawRows, { @@ -249,7 +253,12 @@ class CompactResultDataset { if (val == null) continue; sampleCount++; - if (cellFormatter != null) { + // The driver formatters print a column's ints, numbers and strings + // by the column's type (bytea, binary, boolean...), not by the value, + // so the first [_formattedSamples] cells show whether the column + // prints as its values do. Formatting every cell only to decide the + // kind cost more than the packing itself (#1548). + if (cellFormatter != null && sampleCount <= _formattedSamples) { final formatted = cellFormatter(val, col); if (allInt && (val is! int || formatted != val.toString())) { allInt = false; @@ -272,9 +281,8 @@ class CompactResultDataset { } } - if (sampleCount >= 60 && !allInt && !allFloat && !allString) { - break; - } + // Nothing left to learn: the column is generic. + if (!allInt && !allFloat && !allString) break; } if (sampleCount > 0) { diff --git a/test/core/database/compact_result_dataset_test.dart b/test/core/database/compact_result_dataset_test.dart index 0248ed56..881fa4cd 100644 --- a/test/core/database/compact_result_dataset_test.dart +++ b/test/core/database/compact_result_dataset_test.dart @@ -233,5 +233,51 @@ void main() { expect(result.rows[1][1], '100'); }); }); + + group('kind detection with a cell formatter (#1548)', () { + // Formats every cell; counts how many it was asked for. + var formatted = 0; + String fmt(Object? v, int col) { + formatted++; + if (v is String && col == 2) return 'hex:$v'; // a bytea-typed column + return v.toString(); + } + + setUp(() => formatted = 0); + + test('the formatter decides from samples; the rest is type-tested', () { + final rows = [ + for (var i = 0; i < 5000; i++) [i, i * 1.5, 'b$i', 's$i'], + ]; + final ds = CompactResultDataset.fromRawRows( + ['i', 'd', 'bytes', 's'], + rows, + cellFormatter: fmt, + ); + expect(ds.columns.map((c) => c.kind), [ + CompactColumnKind.int64, + CompactColumnKind.float64, + CompactColumnKind.generic, // printed differently from its value + CompactColumnKind.string, + ]); + // Detection formats at most 60 cells per column (the bytea column + // stops at its first); packing the generic column formats its cells. + expect(formatted, lessThan(4 * 60 + 5000 + 1)); + }); + + test('a late value of another type still makes the column generic', () { + final rows = [ + for (var i = 0; i < 1000; i++) [i == 900 ? 'n/a' : i], + ]; + final ds = CompactResultDataset.fromRawRows( + ['v'], + rows, + cellFormatter: fmt, + ); + expect(ds.columns.single.kind, CompactColumnKind.generic); + expect(ds.cellAt(900, 0), 'n/a'); + expect(ds.cellAt(899, 0), '899'); + }); + }); }