Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.PrimitiveByteArrayJavaType;
import org.hibernate.type.descriptor.java.StringJavaType;
import org.hibernate.type.descriptor.java.spi.JavaTypeRegistry;
import org.hibernate.type.descriptor.java.spi.UnknownBasicJavaType;
import org.hibernate.type.descriptor.jdbc.ObjectJdbcType;
Expand Down Expand Up @@ -5761,7 +5762,10 @@ else if ( ctx.simplePath() != null && ctx.slicedPathAccessFragment() != null ) {
final List<HqlParser.ExpressionContext> slicedFragments = ctx.slicedPathAccessFragment().expression();
final SqmTypedNode<?> lhs = (SqmTypedNode<?>) visitSimplePath( ctx.simplePath() );
final SqmExpressible<?> lhsExpressible = lhs.getExpressible();
if ( lhsExpressible != null && lhsExpressible.getSqmType() instanceof BasicPluralType<?, ?> ) {
if ( lhsExpressible == null ) {
throw new SemanticException( "Slice operator applied to expression of unknown type", query );
}
else if ( lhsExpressible.getSqmType() instanceof BasicPluralType<?, ?> ) {
return getFunctionDescriptor( "array_slice" ).generateSqmExpression(
List.of(
lhs,
Expand All @@ -5772,7 +5776,8 @@ else if ( ctx.simplePath() != null && ctx.slicedPathAccessFragment() != null ) {
creationContext.getQueryEngine()
);
}
else {
else if ( lhsExpressible.getRelationalJavaType() instanceof StringJavaType
&& !(lhs instanceof SqmPluralValuedSimplePath) ) {
final SqmExpression<?> start = (SqmExpression<?>) slicedFragments.get( 0 ).accept( this );
final SqmExpression<?> end = (SqmExpression<?>) slicedFragments.get( 1 ).accept( this );
return getFunctionDescriptor( "substring" ).generateSqmExpression(
Expand Down Expand Up @@ -5801,6 +5806,9 @@ else if ( ctx.simplePath() != null && ctx.slicedPathAccessFragment() != null ) {
creationContext.getQueryEngine()
);
}
else {
throw new SemanticException( "Slice operator applied to expression which is not a string or SQL array", query );
}
}
else {
throw new ParsingException( "Illegal domain path '" + ctx.getText() + "'" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,23 @@ public void testTupleInSelect(SessionFactoryScope scope) {
);
}

@Test
public void testSlice(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
assertEquals("ring",
session.createSelectionQuery("select theString[3:6] from EntityOfBasics", String.class)
.getSingleResult());
assertEquals('s',
session.createSelectionQuery("select theString[1] from EntityOfBasics", Character.class)
.getSingleResult());
assertEquals('y',
session.createSelectionQuery("select theString[7] from EntityOfBasics", Character.class)
.getSingleResult());
}
);
}

@Test
@SkipForDialect(dialectClass = H2Dialect.class)
@SkipForDialect(dialectClass = DerbyDialect.class)
Expand Down
Loading