HHH-17375 Shorthand bracket syntax for array construction

This commit is contained in:
Christian Beikov 2024-05-06 17:32:04 +02:00
parent 6c805f0637
commit 67d04577be
4 changed files with 56 additions and 0 deletions

View File

@ -1219,6 +1219,17 @@ include::{array-example-dir-hql}/ArrayConstructorTest.java[tags=hql-array-exampl
---- ----
==== ====
Alternatively, it's also possible to construct an array with the shorthand bracket syntax `[` and `]`,
which is syntax sugar that translates to the array constructor function.
[[hql-array-constructor-hql-example]]
====
[source, JAVA, indent=0]
----
include::{array-example-dir-hql}/ArrayConstructorTest.java[tags=hql-array-hql-example]
----
====
[[hql-array-aggregate-functions]] [[hql-array-aggregate-functions]]
===== `array_agg()` ===== `array_agg()`

View File

@ -889,6 +889,7 @@ literal
| numericLiteral | numericLiteral
| binaryLiteral | binaryLiteral
| temporalLiteral | temporalLiteral
| arrayLiteral
| generalizedLiteral | generalizedLiteral
; ;
@ -1058,6 +1059,13 @@ genericTemporalLiteralText
: STRING_LITERAL : STRING_LITERAL
; ;
/**
* A generic format for specifying literal values of arbitary types
*/
arrayLiteral
: LEFT_BRACKET (expression (COMMA expression)*)? RIGHT_BRACKET
;
/** /**
* A generic format for specifying literal values of arbitary types * A generic format for specifying literal values of arbitary types
*/ */

View File

@ -3441,6 +3441,21 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
} }
} }
@Override
public Object visitArrayLiteral(HqlParser.ArrayLiteralContext ctx) {
final List<HqlParser.ExpressionContext> expressionContexts = ctx.expression();
int count = expressionContexts.size();
final List<SqmTypedNode<?>> arguments = new ArrayList<>( count );
for ( HqlParser.ExpressionContext expressionContext : expressionContexts ) {
arguments.add( (SqmTypedNode<?>) expressionContext.accept( this ) );
}
return getFunctionDescriptor( "array" ).generateSqmExpression(
arguments,
null,
creationContext.getQueryEngine()
);
}
@Override @Override
public Object visitGeneralizedLiteral(HqlParser.GeneralizedLiteralContext ctx) { public Object visitGeneralizedLiteral(HqlParser.GeneralizedLiteralContext ctx) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();

View File

@ -109,4 +109,26 @@ public class ArrayConstructorTest {
} ); } );
} }
@Test
public void testArrayConstructorSyntaxEmpty(SessionFactoryScope scope) {
scope.inSession( em -> {
List<EntityWithArrays> results = em.createQuery( "from EntityWithArrays e where e.theArray = []", EntityWithArrays.class )
.getResultList();
assertEquals( 1, results.size() );
assertEquals( 1L, results.get( 0 ).getId() );
} );
}
@Test
public void testArrayConstructorSyntaxNonEmpty(SessionFactoryScope scope) {
scope.inSession( em -> {
//tag::hql-array-hql-example[]
List<EntityWithArrays> results = em.createQuery( "from EntityWithArrays e where e.theArray is not distinct from ['abc', null, 'def']", EntityWithArrays.class )
.getResultList();
//end::hql-array-hql-example[]
assertEquals( 1, results.size() );
assertEquals( 2L, results.get( 0 ).getId() );
} );
}
} }