|
7 | 7 | import java.util.List;
|
8 | 8 |
|
9 | 9 | import org.hibernate.metamodel.model.domain.ReturnableType;
|
| 10 | +import org.hibernate.query.sqm.function.SelfRenderingOrderedSetAggregateFunctionSqlAstExpression; |
| 11 | +import org.hibernate.sql.ast.Clause; |
| 12 | +import org.hibernate.sql.ast.SqlAstNodeRenderingMode; |
10 | 13 | import org.hibernate.sql.ast.SqlAstTranslator;
|
11 | 14 | import org.hibernate.sql.ast.spi.SqlAppender;
|
12 | 15 | import org.hibernate.sql.ast.tree.SqlAstNode;
|
13 | 16 | import org.hibernate.sql.ast.tree.expression.Expression;
|
| 17 | +import org.hibernate.sql.ast.tree.predicate.Predicate; |
| 18 | +import org.hibernate.sql.ast.tree.select.SortSpecification; |
14 | 19 | import org.hibernate.type.BasicPluralType;
|
15 | 20 | import org.hibernate.type.SqlTypes;
|
16 | 21 | import org.hibernate.type.spi.TypeConfiguration;
|
@@ -41,39 +46,94 @@ public void render(
|
41 | 46 | final BasicPluralType<?, ?> pluralType = (BasicPluralType<?, ?>) arrayExpression.getExpressionType().getSingleJdbcMapping();
|
42 | 47 | final int ddlTypeCode = pluralType.getElementType().getJdbcType().getDdlTypeCode();
|
43 | 48 | final boolean needsCast = !SqlTypes.isStringType( ddlTypeCode );
|
44 |
| - sqlAppender.append( "case when " ); |
45 |
| - arrayExpression.accept( walker ); |
46 |
| - sqlAppender.append( " is not null then coalesce((select listagg(" ); |
47 |
| - if ( defaultExpression != null ) { |
48 |
| - sqlAppender.append( "coalesce(" ); |
49 |
| - } |
50 |
| - if ( needsCast ) { |
51 |
| - if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
52 |
| - // By default, H2 uses upper case, so lower it for a consistent experience |
53 |
| - sqlAppender.append( "lower(" ); |
| 49 | + if ( arrayExpression instanceof SelfRenderingOrderedSetAggregateFunctionSqlAstExpression<?> functionExpression |
| 50 | + && ArrayAggFunction.FUNCTION_NAME.equals( functionExpression.getFunctionName() ) ) { |
| 51 | + // When the array argument is an aggregate expression, we access its contents directly |
| 52 | + final Expression arrayElementExpression = (Expression) functionExpression.getArguments().get( 0 ); |
| 53 | + final List<SortSpecification> withinGroup = functionExpression.getWithinGroup(); |
| 54 | + final Predicate filter = functionExpression.getFilter(); |
| 55 | + |
| 56 | + sqlAppender.append( "listagg(" ); |
| 57 | + if ( defaultExpression != null ) { |
| 58 | + sqlAppender.append( "coalesce(" ); |
54 | 59 | }
|
55 |
| - sqlAppender.append( "cast(" ); |
56 |
| - } |
57 |
| - sqlAppender.append( "array_get(" ); |
58 |
| - arrayExpression.accept( walker ); |
59 |
| - sqlAppender.append(",i.idx)" ); |
60 |
| - if ( needsCast ) { |
61 |
| - sqlAppender.append( " as varchar)" ); |
62 |
| - if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
| 60 | + if ( needsCast ) { |
| 61 | + if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
| 62 | + // By default, H2 uses upper case, so lower it for a consistent experience |
| 63 | + sqlAppender.append( "lower(" ); |
| 64 | + } |
| 65 | + sqlAppender.append( "cast(" ); |
| 66 | + } |
| 67 | + arrayElementExpression.accept( walker ); |
| 68 | + if ( needsCast ) { |
| 69 | + sqlAppender.append( " as varchar)" ); |
| 70 | + if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
| 71 | + sqlAppender.append( ')' ); |
| 72 | + } |
| 73 | + } |
| 74 | + if ( defaultExpression != null ) { |
| 75 | + sqlAppender.append( ',' ); |
| 76 | + defaultExpression.accept( walker ); |
63 | 77 | sqlAppender.append( ')' );
|
64 | 78 | }
|
| 79 | + sqlAppender.append( "," ); |
| 80 | + walker.render( separatorExpression, SqlAstNodeRenderingMode.DEFAULT ); |
| 81 | + sqlAppender.appendSql( ')' ); |
| 82 | + |
| 83 | + if ( withinGroup != null && !withinGroup.isEmpty() ) { |
| 84 | + walker.getCurrentClauseStack().push( Clause.WITHIN_GROUP ); |
| 85 | + sqlAppender.appendSql( " within group (order by " ); |
| 86 | + withinGroup.get( 0 ).accept( walker ); |
| 87 | + for ( int i = 1; i < withinGroup.size(); i++ ) { |
| 88 | + sqlAppender.appendSql( ',' ); |
| 89 | + withinGroup.get( i ).accept( walker ); |
| 90 | + } |
| 91 | + sqlAppender.appendSql( ')' ); |
| 92 | + walker.getCurrentClauseStack().pop(); |
| 93 | + } |
| 94 | + if ( filter != null ) { |
| 95 | + walker.getCurrentClauseStack().push( Clause.WHERE ); |
| 96 | + sqlAppender.appendSql( " filter (where " ); |
| 97 | + filter.accept( walker ); |
| 98 | + sqlAppender.appendSql( ')' ); |
| 99 | + walker.getCurrentClauseStack().pop(); |
| 100 | + } |
65 | 101 | }
|
66 |
| - if ( defaultExpression != null ) { |
67 |
| - sqlAppender.append( ',' ); |
68 |
| - defaultExpression.accept( walker ); |
69 |
| - sqlAppender.append( ')' ); |
| 102 | + else { |
| 103 | + sqlAppender.append( "case when " ); |
| 104 | + arrayExpression.accept( walker ); |
| 105 | + sqlAppender.append( " is not null then coalesce((select listagg(" ); |
| 106 | + if ( defaultExpression != null ) { |
| 107 | + sqlAppender.append( "coalesce(" ); |
| 108 | + } |
| 109 | + if ( needsCast ) { |
| 110 | + if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
| 111 | + // By default, H2 uses upper case, so lower it for a consistent experience |
| 112 | + sqlAppender.append( "lower(" ); |
| 113 | + } |
| 114 | + sqlAppender.append( "cast(" ); |
| 115 | + } |
| 116 | + sqlAppender.append( "array_get(" ); |
| 117 | + arrayExpression.accept( walker ); |
| 118 | + sqlAppender.append( ",i.idx)" ); |
| 119 | + if ( needsCast ) { |
| 120 | + sqlAppender.append( " as varchar)" ); |
| 121 | + if ( ddlTypeCode == SqlTypes.BOOLEAN ) { |
| 122 | + sqlAppender.append( ')' ); |
| 123 | + } |
| 124 | + } |
| 125 | + if ( defaultExpression != null ) { |
| 126 | + sqlAppender.append( ',' ); |
| 127 | + defaultExpression.accept( walker ); |
| 128 | + sqlAppender.append( ')' ); |
| 129 | + } |
| 130 | + sqlAppender.append( "," ); |
| 131 | + walker.render( separatorExpression, SqlAstNodeRenderingMode.DEFAULT ); |
| 132 | + sqlAppender.append( ") within group (order by i.idx) from system_range(1," ); |
| 133 | + sqlAppender.append( Integer.toString( maximumArraySize ) ); |
| 134 | + sqlAppender.append( ") i(idx) where i.idx<=coalesce(cardinality(" ); |
| 135 | + arrayExpression.accept( walker ); |
| 136 | + sqlAppender.append( "),0)),'') end" ); |
70 | 137 | }
|
71 |
| - sqlAppender.append("," ); |
72 |
| - separatorExpression.accept( walker ); |
73 |
| - sqlAppender.append( ") within group (order by i.idx) from system_range(1,"); |
74 |
| - sqlAppender.append( Integer.toString( maximumArraySize ) ); |
75 |
| - sqlAppender.append( ") i(idx) where i.idx<=coalesce(cardinality("); |
76 |
| - arrayExpression.accept( walker ); |
77 |
| - sqlAppender.append("),0)),'') end" ); |
78 | 138 | }
|
79 | 139 | }
|
0 commit comments