Skip to content

Commit

Permalink
fix NPE when ordered set agg function missing 'within group'
Browse files Browse the repository at this point in the history
- also refactor a bit that code in SQB to be more typesafe
- and get rid of some warnings
  • Loading branch information
gavinking committed May 28, 2023
1 parent a6f036d commit 36bdd9d
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ listaggFunction
* A 'on overflow' clause: what to do when the text data type used for 'listagg' overflows
*/
onOverflowClause
: ON OVERFLOW (ERROR | (TRUNCATE expression? (WITH|WITHOUT) COUNT))
: ON OVERFLOW (ERROR | TRUNCATE expression? (WITH|WITHOUT) COUNT)
;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression;
import org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression;
import org.hibernate.query.sqm.function.SelfRenderingSqmAggregateFunction;
import org.hibernate.query.sqm.function.SelfRenderingSqmOrderedSetAggregateFunction;
import org.hibernate.query.sqm.produce.function.ArgumentsValidator;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import java.util.List;

import org.hibernate.metamodel.mapping.BasicValuedMapping;
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.metamodel.mapping.MappingModelExpressible;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.SemanticException;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
import org.hibernate.query.sqm.function.FunctionKind;
Expand Down Expand Up @@ -129,13 +130,12 @@ public void render(

protected class SelfRenderingInverseDistributionFunction<T> extends SelfRenderingSqmOrderedSetAggregateFunction<T> {

private final SqmOrderByClause withinGroupClause;

public SelfRenderingInverseDistributionFunction(
List<? extends SqmTypedNode<?>> arguments,
SqmPredicate filter,
SqmOrderByClause withinGroupClause,
ReturnableType<T> impliedResultType, QueryEngine queryEngine) {
ReturnableType<T> impliedResultType,
QueryEngine queryEngine) {
super(
InverseDistributionFunction.this,
InverseDistributionFunction.this,
Expand All @@ -148,13 +148,18 @@ public SelfRenderingInverseDistributionFunction(
queryEngine.getCriteriaBuilder(),
InverseDistributionFunction.this.getName()
);
this.withinGroupClause = withinGroupClause;
if ( withinGroupClause == null ) {
throw new SemanticException("Inverse distribution function '" + getFunctionName()
+ "' must specify WITHIN GROUP");
}
}

@Override
protected ReturnableType<?> resolveResultType(TypeConfiguration typeConfiguration) {
return (ReturnableType<?>) withinGroupClause.getSortSpecifications().get( 0 ).getSortExpression()
.getExpressible();
return (ReturnableType<?>)
getWithinGroup().getSortSpecifications().get( 0 )
.getSortExpression()
.getExpressible();
}

@Override
Expand All @@ -171,21 +176,23 @@ protected MappingModelExpressible<?> getMappingModelExpressible(
// here we have something that is not a BasicType,
// and we have no way to get a BasicValuedMapping
// from it directly
final Expression expression = (Expression) withinGroupClause.getSortSpecifications().get( 0 )
.getSortExpression()
.accept( walker );
if ( expression.getExpressionType() instanceof BasicValuedMapping ) {
return (BasicValuedMapping) expression.getExpressionType();
final Expression expression = (Expression)
getWithinGroup().getSortSpecifications().get( 0 )
.getSortExpression()
.accept( walker );
final JdbcMappingContainer expressionType = expression.getExpressionType();
if ( expressionType instanceof BasicValuedMapping ) {
return (BasicValuedMapping) expressionType;
}
try {
final MappingMetamodelImplementor domainModel = walker.getCreationContext()
return walker.getCreationContext()
.getSessionFactory()
.getRuntimeMetamodels()
.getMappingMetamodel();
return domainModel.resolveMappingExpressible(
getNodeType(),
walker.getFromClauseAccess()::getTableGroup
);
.getMappingMetamodel()
.resolveMappingExpressible(
getNodeType(),
walker.getFromClauseAccess()::getTableGroup
);
}
catch (Exception e) {
return null; // this works at least approximately
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression;
import org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression;
import org.hibernate.query.sqm.function.SelfRenderingSqmAggregateFunction;
import org.hibernate.query.sqm.function.SelfRenderingSqmOrderedSetAggregateFunction;
import org.hibernate.query.sqm.produce.function.ArgumentsValidator;
import org.hibernate.query.sqm.produce.function.FunctionParameterType;
Expand Down

0 comments on commit 36bdd9d

Please sign in to comment.