Don't assume nullable if no join syntax is known

This commit is contained in:
Simon Binder 2023-03-26 22:21:48 +02:00
parent c38e9cc6c0
commit 4e05067bd5
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 31 additions and 5 deletions

View File

@ -711,16 +711,17 @@ class TypeResolver extends RecursiveVisitor<TypeExpectation, void> {
} else if (column is DelegatedColumn && column.innerColumn != null) {
_handleColumn(column.innerColumn);
var makeNullable = false;
if (column is AvailableColumn) {
// The nullability depends on whether the column was introduced in an
// outer join.
final model = context != null ? JoinModel.of(context) : null;
final isNullable =
model == null || model.availableColumnIsNullable(column);
_lazyCopy(column, column.innerColumn, makeNullable: isNullable);
} else {
_lazyCopy(column, column.innerColumn);
makeNullable = model != null && model.availableColumnIsNullable(column);
}
_lazyCopy(column, column.innerColumn, makeNullable: makeNullable);
}
}

View File

@ -308,4 +308,29 @@ WITH RECURSIVE
expect(session.typeOf(columns[2]),
const ResolvedType(type: BasicType.int, nullable: true));
});
test('analyzes nested columns', () {
engine.registerTableFromSql('''
CREATE TABLE x (
id INTEGER NOT NULL,
other INTEGER
);
''');
final resolver = obtainResolver('''
SELECT xxx.id FROM (
SELECT * FROM (
SELECT id FROM x
) xx
) xxx;
''');
final session = resolver.session;
final stmt = resolver.session.context.root as SelectStatement;
final columns = stmt.resolvedColumns!;
expect(columns, hasLength(1));
expect(session.typeOf(columns[0]),
const ResolvedType(type: BasicType.int, nullable: false));
});
}