Make sure root virtual table group joins are rendered properly

This commit is contained in:
Christian Beikov 2022-03-15 08:38:41 +01:00
parent b227475489
commit 8cf0265f8d
3 changed files with 49 additions and 6 deletions

View File

@ -3618,12 +3618,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
clauseStack.push( Clause.FROM ); clauseStack.push( Clause.FROM );
String separator = NO_SEPARATOR; String separator = NO_SEPARATOR;
for ( TableGroup root : fromClause.getRoots() ) { for ( TableGroup root : fromClause.getRoots() ) {
// Skip virtual table group roots which we use for simple correlations separator = renderFromClauseRoot( root, separator );
if ( !( root instanceof VirtualTableGroup ) ) {
appendSql( separator );
renderRootTableGroup( root, null );
separator = COMA_SEPARATOR;
}
} }
} }
finally { finally {
@ -3632,6 +3627,23 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
} }
} }
private String renderFromClauseRoot(TableGroup root, String separator) {
if ( root instanceof VirtualTableGroup ) {
for ( TableGroupJoin tableGroupJoin : root.getTableGroupJoins() ) {
separator = renderFromClauseRoot( tableGroupJoin.getJoinedGroup(), separator );
}
for ( TableGroupJoin tableGroupJoin : root.getNestedTableGroupJoins() ) {
separator = renderFromClauseRoot( tableGroupJoin.getJoinedGroup(), separator );
}
}
else {
appendSql( separator );
renderRootTableGroup( root, null );
separator = COMA_SEPARATOR;
}
return separator;
}
protected void renderRootTableGroup(TableGroup tableGroup, List<TableGroupJoin> tableGroupJoinCollector) { protected void renderRootTableGroup(TableGroup tableGroup, List<TableGroupJoin> tableGroupJoinCollector) {
final LockMode effectiveLockMode = getEffectiveLockMode( tableGroup.getSourceAlias() ); final LockMode effectiveLockMode = getEffectiveLockMode( tableGroup.getSourceAlias() );
final boolean usesLockHint = renderPrimaryTableReference( tableGroup, effectiveLockMode ); final boolean usesLockHint = renderPrimaryTableReference( tableGroup, effectiveLockMode );

View File

@ -119,4 +119,7 @@ public class CorrelatedTableGroup extends AbstractTableGroup {
return Collections.emptyList(); return Collections.emptyList();
} }
public Consumer<Predicate> getJoinPredicateConsumer() {
return joinPredicateConsumer;
}
} }

View File

@ -71,6 +71,34 @@ public class StandardVirtualTableGroup extends AbstractTableGroup implements Vir
return underlyingTableGroup.getTableReferenceJoins(); return underlyingTableGroup.getTableReferenceJoins();
} }
@Override
public void addTableGroupJoin(TableGroupJoin join) {
super.addTableGroupJoin( join );
registerPredicateOnCorrelatedTableGroup( join );
}
@Override
public void prependTableGroupJoin(NavigablePath navigablePath, TableGroupJoin join) {
super.prependTableGroupJoin( navigablePath, join );
registerPredicateOnCorrelatedTableGroup( join );
}
@Override
public void addNestedTableGroupJoin(TableGroupJoin join) {
super.addNestedTableGroupJoin( join );
registerPredicateOnCorrelatedTableGroup( join );
}
private void registerPredicateOnCorrelatedTableGroup(TableGroupJoin join) {
TableGroup tableGroup = underlyingTableGroup;
while ( tableGroup instanceof StandardVirtualTableGroup ) {
tableGroup = ( (StandardVirtualTableGroup) tableGroup ).underlyingTableGroup;
}
if ( tableGroup instanceof CorrelatedTableGroup ) {
( (CorrelatedTableGroup) tableGroup ).getJoinPredicateConsumer().accept( join.getPredicate() );
}
}
@Override @Override
public TableReference getTableReferenceInternal( public TableReference getTableReferenceInternal(
NavigablePath navigablePath, NavigablePath navigablePath,