Skip to content

Commit

Permalink
fix: FromItem alias must not shade an actual table (found before)
Browse files Browse the repository at this point in the history
- fixes #2035

Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
  • Loading branch information
manticore-projects committed Jul 10, 2024
1 parent e8bc446 commit d8207aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,6 @@ public void visit(UseStatement use) {

@Override
public <S> Void visit(ParenthesedFromItem parenthesis, S context) {
if (parenthesis.getAlias() != null) {
otherItemNames.add(parenthesis.getAlias().getName());
}
parenthesis.getFromItem().accept(this, context);
// support join keyword in fromItem
visitJoins(parenthesis.getJoins(), context);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,5 +497,17 @@ void testSubqueryAliasesIssue1987() throws JSQLParserException {
assertThat(tables).containsExactlyInAnyOrder("a", "b");
assertThat(tables).doesNotContain("a1");
}

@Test
void testSubqueryAliasesIssue2035() throws JSQLParserException {
String sqlStr = "SELECT * FROM (SELECT * FROM A) AS A \n" +
"JOIN B ON A.a = B.a \n" +
"JOIN C ON A.a = C.a;";
Set<String> tables = TablesNamesFinder.findTablesOrOtherSources(sqlStr);
assertThat(tables).containsExactlyInAnyOrder("A", "B", "C");

tables = TablesNamesFinder.findTables(sqlStr);
assertThat(tables).containsExactlyInAnyOrder("B", "C");
}
}

0 comments on commit d8207aa

Please sign in to comment.