Resolve CTEs for compound selects

This commit is contained in:
Simon Binder 2023-06-20 14:17:53 +02:00
parent 81315c3d69
commit c700b5b4be
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 29 additions and 1 deletions

View File

@ -1,3 +1,7 @@
## 0.30.3
- Fix `WITH` clauses not being resolved for compound select statements.
## 0.30.2
- Fix false-positive "unknown table" errors when the same table is used in a

View File

@ -58,6 +58,7 @@ class ColumnResolver extends RecursiveVisitor<ColumnResolverContext, void> {
@override
void visitCompoundSelectStatement(
CompoundSelectStatement e, ColumnResolverContext arg) {
e.withClause?.accept(this, arg);
e.base.accept(this, arg);
visitList(e.additional, arg);

View File

@ -1,6 +1,6 @@
name: sqlparser
description: Parses sqlite statements and performs static analysis on them
version: 0.30.2
version: 0.30.3
homepage: https://github.com/simolus3/drift/tree/develop/sqlparser
repository: https://github.com/simolus3/drift
#homepage: https://drift.simonbinder.eu/

View File

@ -218,4 +218,27 @@ WHERE EXISTS(SELECT *
expect(result.errors, isEmpty);
});
test('regression test for #2492', () {
// https://github.com/simolus3/drift/issues/2492
final engine = SqlEngine()
..registerTableFromSql(
'CREATE TABLE items (id INT NOT NULL PRIMARY KEY)');
final result = engine.analyze('''
WITH filtered_items AS (
SELECT *
FROM items
-- WHERE ...
)
select null as category
FROM filtered_items
UNION ALL
SELECT null as category
FROM filtered_items;
''');
final select = result.root as BaseSelectStatement;
expect(select.resolvedColumns, hasLength(1));
});
}