Render select item aliases for sub queries in from clause to avoid possible alias collision

This commit is contained in:
Christian Beikov 2022-03-24 23:08:09 +01:00
parent 23098ca254
commit 352970604d
2 changed files with 23 additions and 0 deletions

View File

@ -388,6 +388,11 @@ public class OracleSqlAstTranslator<T extends JdbcOperation> extends AbstractSql
}
}
@Override
protected boolean supportsDuplicateSelectItemsInQueryGroup() {
return false;
}
@Override
protected boolean supportsRowValueConstructorSyntax() {
return false;

View File

@ -1550,6 +1550,11 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
appendSql( "select" );
appendSql( queryGroupAlias );
appendSql( ".* from " );
// We need to assign aliases when we render a query spec as sub query to avoid clashing aliases
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
}
else if ( !supportsDuplicateSelectItemsInQueryGroup() ) {
this.needsSelectAliases = this.needsSelectAliases || hasDuplicateSelectItems( querySpec );
}
}
}
@ -1589,10 +1594,23 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
}
}
private boolean hasDuplicateSelectItems(QuerySpec querySpec) {
final List<SqlSelection> sqlSelections = querySpec.getSelectClause().getSqlSelections();
final Map<Expression, Void> map = new IdentityHashMap<>( sqlSelections.size() );
for ( int i = 0; i < sqlSelections.size(); i++ ) {
map.put( sqlSelections.get( i ).getExpression(), null );
}
return map.size() != sqlSelections.size();
}
protected boolean supportsSimpleQueryGrouping() {
return true;
}
protected boolean supportsDuplicateSelectItemsInQueryGroup() {
return true;
}
protected final void visitWhereClause(Predicate whereClauseRestrictions) {
if ( whereClauseRestrictions != null && !whereClauseRestrictions.isEmpty() || additionalWherePredicate != null ) {
appendSql( " where " );