Skip to content

Commit

Permalink
Fix join predicate rendering and fix support for implicit joins in th…
Browse files Browse the repository at this point in the history
…e ON clause
  • Loading branch information
beikov committed Oct 25, 2021
1 parent 8fc0e05 commit fa3101c
Show file tree
Hide file tree
Showing 18 changed files with 1,119 additions and 1,175 deletions.
Expand Up @@ -42,6 +42,7 @@
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.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
Expand Down Expand Up @@ -173,19 +174,38 @@ public TableGroupJoin createTableGroupJoin(
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final CompositeTableGroup compositeTableGroup = new CompositeTableGroup(
final TableGroup tableGroup = createRootTableGroupJoin(
navigablePath,
this,
lhs,
fetched
explicitSourceAlias,
sqlAstJoinType,
fetched,
null,
aliasBaseGenerator,
sqlExpressionResolver,
creationContext
);

final TableGroupJoin join = new TableGroupJoin( navigablePath, SqlAstJoinType.LEFT, compositeTableGroup, null );
final TableGroupJoin join = new TableGroupJoin( navigablePath, SqlAstJoinType.LEFT, tableGroup, null );
lhs.addTableGroupJoin( join );

return join;
}

@Override
public TableGroup createRootTableGroupJoin(
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
Consumer<Predicate> predicateConsumer,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return new CompositeTableGroup( navigablePath, this, lhs, fetched );
}

@Override
public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
return embeddableDescriptor.findSubPart( name, treatTargetType );
Expand Down
Expand Up @@ -44,6 +44,7 @@
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.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
Expand Down Expand Up @@ -286,23 +287,47 @@ public TableGroupJoin createTableGroupJoin(
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final CompositeTableGroup compositeTableGroup = new CompositeTableGroup(
final TableGroup tableGroup = createRootTableGroupJoin(
navigablePath,
this,
lhs,
fetched
explicitSourceAlias,
sqlAstJoinType,
fetched,
null,
aliasBaseGenerator,
sqlExpressionResolver,
creationContext
);

TableGroupJoin tableGroupJoin = new TableGroupJoin(
final TableGroupJoin tableGroupJoin = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
compositeTableGroup
tableGroup
);
lhs.addTableGroupJoin( tableGroupJoin );

return tableGroupJoin;
}

@Override
public TableGroup createRootTableGroupJoin(
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
Consumer<Predicate> predicateConsumer,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return new CompositeTableGroup(
navigablePath,
this,
lhs,
fetched
);
}

@Override
public String getSqlAliasStem() {
return getAttributeName();
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.hibernate.sql.ast.tree.from.CompositeTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
Expand Down Expand Up @@ -204,9 +205,17 @@ public TableGroupJoin createTableGroupJoin(
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
assert lhs.getModelPart() instanceof PluralAttributeMapping;

final TableGroup tableGroup = new CompositeTableGroup( navigablePath, this, lhs, fetched );
final TableGroup tableGroup = createRootTableGroupJoin(
navigablePath,
lhs,
explicitSourceAlias,
sqlAstJoinType,
fetched,
null,
aliasBaseGenerator,
sqlExpressionResolver,
creationContext
);

final TableGroupJoin tableGroupJoin = new TableGroupJoin(
navigablePath,
Expand All @@ -218,6 +227,22 @@ public TableGroupJoin createTableGroupJoin(
return tableGroupJoin;
}

@Override
public TableGroup createRootTableGroupJoin(
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
Consumer<Predicate> predicateConsumer,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
assert lhs.getModelPart() instanceof PluralAttributeMapping;

return new CompositeTableGroup( navigablePath, this, lhs, fetched );
}

@Override
public String getSqlAliasStem() {
return sqlAliasStem;
Expand Down
Expand Up @@ -335,31 +335,16 @@ private <T> DomainResult<T> createDomainResult(

@Override
public Predicate generateJoinPredicate(
TableGroup lhs,
TableGroup tableGroup,
TableGroup targetSideTableGroup,
TableGroup keySideTableGroup,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
TableReference lhsTableReference;
TableReference rhsTableKeyReference;
if ( targetTable.equals( keyTable ) ) {
lhsTableReference = getTableReferenceWhenTargetEqualsKey( lhs, tableGroup, keyTable );

rhsTableKeyReference = getTableReference(
lhs,
tableGroup,
targetTable
);
}
else {
lhsTableReference = getTableReference( lhs, tableGroup, keyTable );

rhsTableKeyReference = getTableReference(
lhs,
tableGroup,
targetTable
);
}
final TableReference lhsTableReference = targetSideTableGroup.getTableReference(
targetSideTableGroup.getNavigablePath(),
targetTable
);
final TableReference rhsTableKeyReference = keySideTableGroup.getTableReference( keyTable );

return generateJoinPredicate(
lhsTableReference,
Expand All @@ -372,21 +357,12 @@ public Predicate generateJoinPredicate(

@Override
public Predicate generateJoinPredicate(
TableReference lhs,
TableReference rhs,
TableReference targetSideReference,
TableReference keySideReference,
SqlAstJoinType sqlAstJoinType,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
final String rhsTableExpression = rhs.getTableExpression();
final String lhsTableExpression = lhs.getTableExpression();
if ( lhsTableExpression.equals( keyTable ) ) {
assert rhsTableExpression.equals( targetTable );
return getPredicate( lhs, rhs, creationContext, keySelectableMappings, targetSelectableMappings );
}
else {
assert rhsTableExpression.equals( keyTable );
return getPredicate( lhs, rhs, creationContext, targetSelectableMappings, keySelectableMappings );
}
return getPredicate( targetSideReference, keySideReference, creationContext, targetSelectableMappings, keySelectableMappings );
}

private Predicate getPredicate(
Expand Down Expand Up @@ -417,23 +393,6 @@ private Predicate getPredicate(
return predicate;
}

protected TableReference getTableReferenceWhenTargetEqualsKey(TableGroup lhs, TableGroup tableGroup, String table) {
if ( tableGroup.getPrimaryTableReference().getTableExpression().equals( table ) ) {
return tableGroup.getPrimaryTableReference();
}
if ( lhs.getPrimaryTableReference().getTableExpression().equals( table ) ) {
return lhs.getPrimaryTableReference();
}

for ( TableReferenceJoin tableJoin : lhs.getTableReferenceJoins() ) {
if ( tableJoin.getJoinedTableReference().getTableExpression().equals( table ) ) {
return tableJoin.getJoinedTableReference();
}
}

throw new IllegalStateException( "Could not resolve binding for table `" + table + "`" );
}

protected TableReference getTableReference(TableGroup lhs, TableGroup tableGroup, String table) {
if ( lhs.getPrimaryTableReference().getTableExpression().equals( table ) ) {
return lhs.getPrimaryTableReference();
Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.metamodel.mapping.internal;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
Expand All @@ -32,6 +33,7 @@
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchOptions;
Expand Down Expand Up @@ -286,6 +288,30 @@ public TableGroupJoin createTableGroupJoin(
);
}

@Override
public TableGroup createRootTableGroupJoin(
NavigablePath navigablePath,
TableGroup lhs,
String explicitSourceAlias,
SqlAstJoinType sqlAstJoinType,
boolean fetched,
Consumer<Predicate> predicateConsumer,
SqlAliasBaseGenerator aliasBaseGenerator,
SqlExpressionResolver sqlExpressionResolver,
SqlAstCreationContext creationContext) {
return collectionDescriptor.getAttributeMapping().createRootTableGroupJoin(
navigablePath,
lhs,
explicitSourceAlias,
sqlAstJoinType,
fetched,
predicateConsumer,
aliasBaseGenerator,
sqlExpressionResolver,
creationContext
);
}

@Override
public String getSqlAliasStem() {
return collectionDescriptor.getAttributeMapping().getSqlAliasStem();
Expand Down

0 comments on commit fa3101c

Please sign in to comment.