Skip to content

Commit

Permalink
Fix star columns resolving to too many columns
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 26, 2023
1 parent db6ee5d commit d0a3fee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions sqlparser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.31.3-dev

- Fix star columns expanding to more columns than they should.

## 0.31.2

- Add `CaseInsensitiveMap.of` to wrap existing maps.
Expand Down
3 changes: 2 additions & 1 deletion sqlparser/lib/src/analysis/steps/column_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ class ColumnResolver extends RecursiveVisitor<ColumnResolverContext, void> {
} else {
// we have a * column without a table, that resolves to every column
// available
visibleColumnsForStar = columnsForStar ?? availableColumns;
visibleColumnsForStar =
columnsForStar ?? scope.expansionOfStarColumn ?? const [];

// Star columns can't be used without a table (e.g. `SELECT *` is
// not allowed)
Expand Down
10 changes: 10 additions & 0 deletions sqlparser/test/analysis/column_resolver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,14 @@ SELECT * FROM cars
''');
query.expectNoError();
});

test('expands star columns', () {
final engine = SqlEngine()
..registerTableFromSql('CREATE TABLE foo (bar INTEGER);');

final result = engine.analyze("SELECT 'client' literal, * FROM foo;");
final select = result.root as SelectStatement;

expect(select.resolvedColumns?.map((e) => e.name), ['literal', 'bar']);
});
}

0 comments on commit d0a3fee

Please sign in to comment.