Skip to content

Commit

Permalink
[ADQL] Fix incorrect ambiguous column name in subqueries
Browse files Browse the repository at this point in the history
Fixes #158
  • Loading branch information
gmantele committed Oct 9, 2024
1 parent a72c06f commit d2865b3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ADQLLib/src/adql/db/DBChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -1204,14 +1204,24 @@ protected void checkSubQueries(final ADQLQuery query, final Stack<CheckContext>
if (sHandler.getNbMatch() > 0) {

// Check each found sub-query:
for(ADQLObject result : sHandler) {
for(ADQLObject result : sHandler)
{
/* Create an empty context in order to use at first only the
* local context (and if needed, we will look at the parent
* contexts) ; see resolveColumn(final ADQLColumn column, final Stack<CheckContext> contextList): */
contextList.push(new CheckContext(null, null));

try {
check((ADQLQuery)result, contextList);
check((ADQLSet)result, contextList);
} catch(UnresolvedIdentifiersException uie) {
Iterator<ParseException> itPe = uie.getErrors();
while(itPe.hasNext())
errors.addException(itPe.next());
}
/* Forget about the pushed context: */
finally {
contextList.pop();
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions ADQLLib/test/adql/db/TestDBChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,28 @@ public void testTypesChecking() {
}
}

@Test
public void test_ShouldSucceed_WhenSubQueryUsesSameTableAsMainQuery() throws Exception {
// Given...
// ...this only table with 3 columns:
final ArrayList<DBTable> lstTables = new ArrayList<>(1);
final DefaultDBTable tableResource = new DefaultDBTable("resource");
tableResource.addColumn(new DefaultDBColumn("ivoid", tableResource));
tableResource.addColumn(new DefaultDBColumn("short_name", tableResource));
tableResource.addColumn(new DefaultDBColumn("res_description", tableResource));
lstTables.add(tableResource);
// ...and this parser:
final DBChecker dbChecker = new DBChecker(lstTables);
final ADQLParser parser = new ADQLParser();
parser.setQueryChecker(dbChecker);

// When parsing the query with a sub-query using the same table:
final ADQLSet query = parser.parseQuery("SELECT res_description FROM resource WHERE ivoid IN (SELECT ivoid FROM resource WHERE short_name = 'anything')");

// Then, the query should successfully pass:
assertNotNull(query);
}

private static class WrongUDFToto extends UDFToto {
public WrongUDFToto(final ADQLOperand[] params) throws Exception {
super(params);
Expand Down

0 comments on commit d2865b3

Please sign in to comment.