Skip to content

Commit

Permalink
Render implicit joins as nested table group joins instead of sub queries
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Oct 25, 2021
1 parent 756afb8 commit 1456a2d
Show file tree
Hide file tree
Showing 31 changed files with 322 additions and 235 deletions.
Expand Up @@ -421,6 +421,7 @@ else if ( part instanceof ToOneAttributeMapping ) {
null,
SqlAstJoinType.LEFT,
true,
false,
sqlAstCreationState
);
tableGroup = tableGroupJoin.getJoinedGroup();
Expand Down
Expand Up @@ -171,6 +171,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -187,7 +188,12 @@ public TableGroupJoin createTableGroupJoin(
);

final TableGroupJoin join = new TableGroupJoin( navigablePath, SqlAstJoinType.LEFT, tableGroup, null );
lhs.addTableGroupJoin( join );
if ( nested ) {
lhs.addNestedTableGroupJoin( join );
}
else {
lhs.addTableGroupJoin( join );
}

return join;
}
Expand Down
Expand Up @@ -284,6 +284,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -299,14 +300,19 @@ public TableGroupJoin createTableGroupJoin(
creationContext
);

final TableGroupJoin tableGroupJoin = new TableGroupJoin(
final TableGroupJoin join = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
tableGroup
);
lhs.addTableGroupJoin( tableGroupJoin );
if ( nested ) {
lhs.addNestedTableGroupJoin( join );
}
else {
lhs.addTableGroupJoin( join );
}

return tableGroupJoin;
return join;
}

@Override
Expand Down
Expand Up @@ -202,6 +202,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -217,14 +218,19 @@ public TableGroupJoin createTableGroupJoin(
creationContext
);

final TableGroupJoin tableGroupJoin = new TableGroupJoin(
final TableGroupJoin join = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
tableGroup,
null
);
lhs.addTableGroupJoin( tableGroupJoin );
return tableGroupJoin;
if ( nested ) {
lhs.addNestedTableGroupJoin( join );
}
else {
lhs.addTableGroupJoin( join );
}
return join;
}

@Override
Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.ComparisonPredicate;
import org.hibernate.sql.ast.tree.predicate.Junction;
import org.hibernate.sql.ast.tree.predicate.Predicate;
Expand Down Expand Up @@ -306,6 +305,7 @@ private <T> DomainResult<T> createDomainResult(
null,
SqlAstJoinType.INNER,
true,
false,
creationState.getSqlAstCreationState()
);
return tableGroupJoin.getJoinedGroup();
Expand Down
Expand Up @@ -273,6 +273,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -282,6 +283,7 @@ public TableGroupJoin createTableGroupJoin(
explicitSourceAlias,
sqlAstJoinType,
fetched,
nested,
aliasBaseGenerator,
sqlExpressionResolver,
creationContext
Expand Down
Expand Up @@ -572,6 +572,7 @@ private TableGroup resolveCollectionTableGroup(
null,
SqlAstJoinType.LEFT,
true,
false,
creationState.getSqlAstCreationState()
);
return tableGroupJoin.getJoinedGroup();
Expand Down Expand Up @@ -624,6 +625,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -638,7 +640,7 @@ public TableGroupJoin createTableGroupJoin(
sqlExpressionResolver,
creationContext
);
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
final TableGroupJoin join = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
tableGroup,
Expand All @@ -651,9 +653,14 @@ public TableGroupJoin createTableGroupJoin(
)
);

lhs.addTableGroupJoin( tableGroupJoin );
if ( nested ) {
lhs.addNestedTableGroupJoin( join );
}
else {
lhs.addTableGroupJoin( join );
}

return tableGroupJoin;
return join;
}

@Override
Expand Down
Expand Up @@ -839,6 +839,7 @@ public <T> DomainResult<T> createDelayedDomainResult(
null,
getDefaultSqlAstJoinType( tableGroup ),
true,
false,
creationState.getSqlAstCreationState()
);

Expand Down Expand Up @@ -911,6 +912,7 @@ private TableGroup createTableGroupJoin(
sourceAlias,
sqlAstJoinType,
fetched,
false,
creationState.getSqlAstCreationState()
);

Expand All @@ -929,6 +931,7 @@ public TableGroupJoin createTableGroupJoin(
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
boolean nested,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
Expand All @@ -944,7 +947,7 @@ public TableGroupJoin createTableGroupJoin(
sqlExpressionResolver,
creationContext
);
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
final TableGroupJoin join = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
lazyTableGroup,
Expand All @@ -954,7 +957,7 @@ public TableGroupJoin createTableGroupJoin(
final TableReference lhsTableReference = lhs.resolveTableReference( navigablePath, identifyingColumnsTableExpression );

lazyTableGroup.setTableGroupInitializerCallback(
tableGroup -> tableGroupJoin.applyPredicate(
tableGroup -> join.applyPredicate(
foreignKeyDescriptor.generateJoinPredicate(
sideNature == ForeignKeyDescriptor.Nature.TARGET ? lhsTableReference : tableGroup.getPrimaryTableReference(),
sideNature == ForeignKeyDescriptor.Nature.TARGET ? tableGroup.getPrimaryTableReference() : lhsTableReference,
Expand All @@ -964,14 +967,19 @@ public TableGroupJoin createTableGroupJoin(
)
)
);
lhs.addTableGroupJoin( tableGroupJoin );
if ( nested ) {
lhs.addNestedTableGroupJoin( join );
}
else {
lhs.addTableGroupJoin( join );
}

if ( sqlAstJoinType == SqlAstJoinType.INNER && isNullable ) {
// Force initialization of the underlying table group join to retain cardinality
lazyTableGroup.getPrimaryTableReference();
}

return tableGroupJoin;
return join;
}

@Override
Expand All @@ -991,7 +999,7 @@ public LazyTableGroup createRootTableGroupJoin(
canUseInnerJoin,
navigablePath,
fetched,
() -> createTableGroupJoinInternal(
() -> createTableGroupInternal(
canUseInnerJoin,
navigablePath,
fetched,
Expand Down Expand Up @@ -1064,7 +1072,7 @@ private SqlAstJoinType getJoinType(NavigablePath navigablePath, TableGroup table
return getDefaultSqlAstJoinType( tableGroup );
}

public TableGroup createTableGroupJoinInternal(
public TableGroup createTableGroupInternal(
boolean canUseInnerJoins,
NavigablePath navigablePath,
boolean fetched,
Expand Down
Expand Up @@ -77,13 +77,13 @@ public List<TableGroupJoin> getTableGroupJoins() {
}

@Override
public boolean canUseInnerJoins() {
return false;
public List<TableGroupJoin> getNestedTableGroupJoins() {
return Collections.emptyList();
}

@Override
public boolean hasTableGroupJoins() {
return tableGroupJoins != null && !tableGroupJoins.isEmpty();
public boolean canUseInnerJoins() {
return false;
}

@Override
Expand All @@ -96,13 +96,22 @@ public void addTableGroupJoin(TableGroupJoin join) {
}
}

@Override
public void addNestedTableGroupJoin(TableGroupJoin join) {
addTableGroupJoin( join );
}

@Override
public void visitTableGroupJoins(Consumer<TableGroupJoin> consumer) {
if ( tableGroupJoins != null ) {
tableGroupJoins.forEach( consumer );
}
}

@Override
public void visitNestedTableGroupJoins(Consumer<TableGroupJoin> consumer) {
}

@Override
public void applyAffectedTableNames(Consumer<String> nameCollector) {

Expand Down
Expand Up @@ -118,6 +118,7 @@ public Fetch buildFetch(
tableAlias,
SqlAstJoinType.INNER,
true,
false,
s -> sqlAliasBase,
creationState.getSqlExpressionResolver(),
creationState.getCreationContext()
Expand Down
Expand Up @@ -89,6 +89,7 @@ public Fetch buildFetch(
null,
SqlAstJoinType.INNER,
true,
false,
creationStateImpl
);
return tableGroupJoin.getJoinedGroup();
Expand Down
Expand Up @@ -70,6 +70,7 @@ public EmbeddableResult buildResult(
null,
SqlAstJoinType.INNER,
true,
false,
creationStateImpl
);

Expand Down

0 comments on commit 1456a2d

Please sign in to comment.