Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/core/database/compact_result_dataset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> columnNames,
List<List<Object?>> rawRows, {
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions test/core/database/compact_result_dataset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
}

Loading