Skip to content

Fix: ARRAY_AGG (and other arrays) may return [null]. #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.5.1

- Fix: `ARRAY_AGG` (and other arrays) may return `[null]`.

## 3.5.0

- Better exception stacktraces (in some cases) using `package:stack_trace`.
Expand Down
20 changes: 15 additions & 5 deletions lib/src/types/binary_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -943,26 +943,36 @@ class PostgresBinaryDecoder {
);
}

static List<V> readListBytes<V>(Uint8List data,
V Function(ByteDataReader reader, int length) valueDecoder) {
static List<V?> readListBytes<V>(
Uint8List data,
V Function(ByteDataReader reader, int length) valueDecoder,
) {
if (data.length < 16) {
return [];
}

final reader = ByteDataReader()..add(data);
reader.read(12); // header

final decoded = [].cast<V>();
final decoded = <V?>[];
final size = reader.readInt32();

reader.read(4); // index

bool hasNull = false;
for (var i = 0; i < size; i++) {
final len = reader.readInt32();
decoded.add(valueDecoder(reader, len));
if (len == -1) {
decoded.add(null);
hasNull = true;
} else {
final v = valueDecoder(reader, len);
decoded.add(v);
hasNull = hasNull || (v == null);
}
}

return decoded;
return hasNull ? decoded : decoded.cast<V>();
}

/// Decode numeric / decimal to String without loosing precision.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: postgres
description: PostgreSQL database driver. Supports statement reuse and binary protocol and connection pooling.
version: 3.5.0
version: 3.5.1
homepage: https://github.com/isoos/postgresql-dart
topics:
- sql
Expand Down
44 changes: 44 additions & 0 deletions test/not_enough_bytes_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';

import 'docker.dart';

void main() {
withPostgresServer('not enough bytes to read', (server) {
test('case #1', () async {
final conn = await server.newConnection();
await conn.execute(_testDdl, queryMode: QueryMode.simple);
final rs1 = await conn.execute('SELECT l.id, bn.novel_id as novels '
'FROM books l LEFT JOIN book_novel bn on l.id=bn.book_id;');
expect(rs1.single, [359, null]);

final rs2 =
await conn.execute('SELECT l.id, ARRAY_AGG(bn.novel_id) as novels '
'FROM books l LEFT JOIN book_novel bn on l.id=bn.book_id '
'GROUP BY l.id;');
expect(rs2.single, [
359,
[null]
]);
});
});
}

final _testDdl = '''
CREATE TABLE IF NOT EXISTS books (
id INTEGER NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
first_publication INTEGER,
notes TEXT,
opinion_id INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS book_novel (
book_id INTEGER NOT NULL,
novel_id INTEGER NOT NULL,
PRIMARY KEY (book_id,novel_id)
);

INSERT INTO books (id,title,first_publication,notes,opinion_id) VALUES (359,'The legacy of heorot',1987,NULL,0);
INSERT INTO book_novel (book_id,novel_id) VALUES (1268,215);
''';