Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-16789 Improve error message for use of limit without order by in subquery #9665

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -141,11 +141,12 @@ public static String prettifyAntlrError(
RecognitionException e,
String hql,
boolean includeLocation) {
String token = null;
String errorText = "";
if ( includeLocation ) {
errorText += "At " + line + ":" + charPositionInLine;
if ( offendingSymbol instanceof CommonToken ) {
String token = ( (CommonToken) offendingSymbol).getText();
token = ( (CommonToken) offendingSymbol).getText();
if ( token != null && !token.isEmpty() ) {
errorText += " and token '" + token + "'";
}
Expand All @@ -160,6 +161,15 @@ public static String prettifyAntlrError(
else {
String lineText = hql.lines().collect( toList() ).get( line -1 );
String text = lineText.substring( 0, charPositionInLine) + "*" + lineText.substring(charPositionInLine);
if ( token != null && !token.isEmpty() && token.equals( "LIMIT" ) ) {
int orderByIndex = lineText.substring( charPositionInLine ).indexOf( "ORDER BY" );
if ( orderByIndex == -1 ) {
String limitMessage = "Use of LIMIT without ORDER BY in subquery ";
String messageToReplace = message.substring( 0, message.indexOf('\'') );
errorText = errorText.replace( messageToReplace, limitMessage );
}
}

errorText += "'" + text + "'";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.limit;

import jakarta.persistence.Query;
import org.hibernate.orm.test.subquery.EntityA;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.junit.jupiter.api.Test;
import org.hibernate.testing.orm.junit.JiraKey;


@JiraKey(value = "HHH-16789")
public class LimitWithoutOrderByQueryTest extends BaseSessionFactoryFunctionalTest {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
EntityA.class
};
}

@Test
public void testLimitWithoutOrderByQuery() {
inSession(
session -> {
Query query = session.createQuery(
"SELECT 1 FROM EntityA a " +
"WHERE 1 = (SELECT 1 FROM EntityA e " +
"LIMIT 1)"
);
}
);
}

@Test
public void testLimitWithOrderByQuery() {
inSession(
session -> {
Query query = session.createQuery(
"SELECT 1 FROM EntityA a " +
"WHERE 1 = (SELECT 1 FROM EntityA e " +
"ORDER BY 1 LIMIT 1)"
);
}
);
}
}