Skip to content

Commit

Permalink
HHH-18170 Subquery randomly generating wrong SQL due to duplicate alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Pelagatti committed May 28, 2024
1 parent 00cbec1 commit 74a3fa2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.hibernate.spi.NavigablePath;
import org.hibernate.query.sqm.tree.domain.SqmPath;

import java.util.concurrent.atomic.AtomicLong;

/**
* @author Steve Ebersole
*/
Expand All @@ -27,6 +29,8 @@ public class SqmCreationHelper {
*/
public static final String IMPLICIT_ALIAS = "{implicit}";

private static final AtomicLong UNIQUE_ID_COUNTER = new AtomicLong();

public static NavigablePath buildRootNavigablePath(String base, String alias) {
return new NavigablePath( base, determineAlias( alias ) );
}
Expand All @@ -35,10 +39,14 @@ public static NavigablePath buildSubNavigablePath(NavigablePath lhs, String base
return lhs.append( base, determineAlias( alias ) );
}

public static String acquireUniqueAlias() {
return Long.toString(UNIQUE_ID_COUNTER.incrementAndGet());
}

public static String determineAlias(String alias) {
// Make sure we always create a unique alias, otherwise we might use a wrong table group for the same join
if ( alias == null ) {
return Long.toString( System.nanoTime() );
return acquireUniqueAlias();
}
else if ( alias == IMPLICIT_ALIAS ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping;
import org.hibernate.query.sqm.spi.SqmCreationHelper;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
Expand Down Expand Up @@ -46,7 +47,7 @@ public EmbeddableValuedExpression(
assert mapping != null;
assert sqlExpression != null;
assert mapping.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings() == sqlExpression.getExpressions().size();
this.navigablePath = baseNavigablePath.append( mapping.getPartName(), Long.toString( System.nanoTime() ) );
this.navigablePath = baseNavigablePath.append( mapping.getPartName(), SqmCreationHelper.acquireUniqueAlias());
this.mapping = mapping;
this.sqlExpression = sqlExpression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.query.criteria.JpaRoot;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.spi.SqmCreationHelper;
import org.hibernate.query.sqm.tree.cte.SqmCteContainer;
import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
Expand Down Expand Up @@ -93,21 +94,21 @@ public <X> JpaCteCriteria<X> getCteCriteria(String cteName) {

@Override
public <X> JpaCteCriteria<X> with(AbstractQuery<X> criteria) {
return withInternal( Long.toString( System.nanoTime() ), criteria );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), criteria );
}

@Override
public <X> JpaCteCriteria<X> withRecursiveUnionAll(
AbstractQuery<X> baseCriteria,
Function<JpaCteCriteria<X>, AbstractQuery<X>> recursiveCriteriaProducer) {
return withInternal( Long.toString( System.nanoTime() ), baseCriteria, false, recursiveCriteriaProducer );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), baseCriteria, false, recursiveCriteriaProducer );
}

@Override
public <X> JpaCteCriteria<X> withRecursiveUnionDistinct(
AbstractQuery<X> baseCriteria,
Function<JpaCteCriteria<X>, AbstractQuery<X>> recursiveCriteriaProducer) {
return withInternal( Long.toString( System.nanoTime() ), baseCriteria, true, recursiveCriteriaProducer );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), baseCriteria, true, recursiveCriteriaProducer );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hibernate.query.criteria.JpaRoot;
import org.hibernate.query.criteria.JpaSelection;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.spi.SqmCreationHelper;
import org.hibernate.query.sqm.tree.AbstractSqmNode;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
Expand Down Expand Up @@ -114,21 +115,21 @@ public <X> JpaCteCriteria<X> getCteCriteria(String cteName) {

@Override
public <X> JpaCteCriteria<X> with(AbstractQuery<X> criteria) {
return withInternal( Long.toString( System.nanoTime() ), criteria );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), criteria );
}

@Override
public <X> JpaCteCriteria<X> withRecursiveUnionAll(
AbstractQuery<X> baseCriteria,
Function<JpaCteCriteria<X>, AbstractQuery<X>> recursiveCriteriaProducer) {
return withInternal( Long.toString( System.nanoTime() ), baseCriteria, false, recursiveCriteriaProducer );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), baseCriteria, false, recursiveCriteriaProducer );
}

@Override
public <X> JpaCteCriteria<X> withRecursiveUnionDistinct(
AbstractQuery<X> baseCriteria,
Function<JpaCteCriteria<X>, AbstractQuery<X>> recursiveCriteriaProducer) {
return withInternal( Long.toString( System.nanoTime() ), baseCriteria, true, recursiveCriteriaProducer );
return withInternal( SqmCreationHelper.acquireUniqueAlias(), baseCriteria, true, recursiveCriteriaProducer );
}

@Override
Expand Down

0 comments on commit 74a3fa2

Please sign in to comment.