Skip to content

Commit 9429487

Browse files
committed
HHH-19396 Applying PR comments
1 parent 6a83ba0 commit 9429487

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

hibernate-core/src/main/java/org/hibernate/query/sqm/tuple/internal/AnonymousTupleType.java

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,49 @@ public class AnonymousTupleType<T>
5757
private final Map<String, Integer> componentIndexMap;
5858

5959
public AnonymousTupleType(SqmSelectQuery<T> selectQuery) {
60-
final SqmSelectableNode<?>[] components = extractSqmExpressibles( selectQuery );
61-
expressibles = new SqmBindableType<?>[components.length];
62-
componentSourcePaths = new NavigablePath[components.length];
63-
for ( int i = 0; i < components.length; i++ ) {
64-
expressibles[i] = components[i].getNodeType();
65-
if ( components[i] instanceof SqmPath<?> path ) {
66-
componentSourcePaths[i] = path.getNavigablePath();
60+
final SqmSelectClause selectClause = selectQuery.getQueryPart()
61+
.getFirstQuerySpec()
62+
.getSelectClause();
63+
64+
if ( selectClause == null || selectClause.getSelections().isEmpty() ) {
65+
throw new IllegalArgumentException( "selectQuery has no selection items" );
66+
}
67+
// todo: right now, we "snapshot" the state of the selectQuery when creating this type, but maybe we shouldn't?
68+
// i.e. what if the selectQuery changes later on? Or should we somehow mark the selectQuery to signal,
69+
// that changes to the select clause are invalid after a certain point?
70+
71+
final List<SqmSelection<?>> selections = selectClause.getSelections();
72+
final List<SqmSelectableNode<?>> selectableNodes = new ArrayList<>();
73+
final List<String> aliases = new ArrayList<>();
74+
if ( selections != null ) {
75+
for ( SqmSelection<?> selection : selections ) {
76+
selection.getSelectableNode().visitSubSelectableNodes( selectableNodes::add );
77+
78+
if ( selection.getSelectableNode().isCompoundSelection() ) {
79+
selection.getSelectableNode().visitSubSelectableNodes( node ->
80+
aliases.add( node.getAlias() )
81+
);
82+
}
83+
else {
84+
aliases.add( selection.getAlias() );
85+
}
6786
}
6887
}
88+
89+
final SqmSelectableNode<?>[] components = selectableNodes.toArray(new SqmSelectableNode[0]);
90+
91+
expressibles = new SqmBindableType<?>[components.length];
92+
componentSourcePaths = new NavigablePath[components.length];
6993
componentNames = new String[components.length];
7094
//noinspection unchecked
7195
javaTypeDescriptor = (JavaType<T>) new ObjectArrayJavaType( getTypeDescriptors( components ) );
7296
componentIndexMap = linkedMapOfSize( components.length );
73-
final String[] aliases = extractAliases( selectQuery );
7497
for ( int i = 0; i < components.length; i++ ) {
75-
final SqmSelectableNode<?> component = components[i];
76-
String alias = aliases[i];
77-
if ( alias == null ) {
78-
alias = component.getAlias();
98+
expressibles[i] = components[i].getNodeType();
99+
if ( components[i] instanceof SqmPath<?> path ) {
100+
componentSourcePaths[i] = path.getNavigablePath();
79101
}
102+
String alias = aliases.get( i );
80103
if ( alias == null ) {
81104
throw new SemanticException( "Select item at position " + (i+1) + " in select list has no alias"
82105
+ " (aliases are required in CTEs and in subqueries occurring in from clause)" );
@@ -112,33 +135,6 @@ public String getTypeName() {
112135
return SqmDomainType.super.getTypeName();
113136
}
114137

115-
private static SqmSelectableNode<?>[] extractSqmExpressibles(SqmSelectQuery<?> selectQuery) {
116-
final SqmSelectClause selectClause = selectQuery.getQueryPart()
117-
.getFirstQuerySpec()
118-
.getSelectClause();
119-
if ( selectClause == null || selectClause.getSelectionItems().isEmpty() ) {
120-
throw new IllegalArgumentException( "selectQuery has no selection items" );
121-
}
122-
// todo: right now, we "snapshot" the state of the selectQuery when creating this type, but maybe we shouldn't?
123-
// i.e. what if the selectQuery changes later on? Or should we somehow mark the selectQuery to signal,
124-
// that changes to the select clause are invalid after a certain point?
125-
return selectClause.getSelectionItems().toArray( SqmSelectableNode[]::new );
126-
}
127-
128-
private static String[] extractAliases(SqmSelectQuery<?> selectQuery) {
129-
final SqmSelectClause selectClause = selectQuery.getQueryPart()
130-
.getFirstQuerySpec()
131-
.getSelectClause();
132-
final List<String> aliases = new ArrayList<>();
133-
for (final SqmSelection<?> selection : selectClause.getSelections()) {
134-
final String alias = selection.getAlias();
135-
selection.getSelectableNode().visitSubSelectableNodes( node ->
136-
aliases.add( alias == null ? node.getAlias() : alias )
137-
);
138-
}
139-
return aliases.toArray(String[]::new);
140-
}
141-
142138
private static JavaType<?>[] getTypeDescriptors(SqmSelectableNode<?>[] components) {
143139
final JavaType<?>[] typeDescriptors = new JavaType<?>[components.length];
144140
for ( int i = 0; i < components.length; i++ ) {

0 commit comments

Comments
 (0)