Skip to content

feat(parser): allow COLLATE in ORDER BY clauses (issue #2245) #2277

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

Open
wants to merge 2 commits into
base: master
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
2 changes: 2 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5152,9 +5152,11 @@ OrderByElement OrderByElement():
{
OrderByElement orderByElement = new OrderByElement();
Expression columnReference = null;
Token collateToken = null;
}
{
columnReference = Expression()
[ LOOKAHEAD(<K_COLLATE>) <K_COLLATE> (collateToken=<S_CHAR_LITERAL> | collateToken=<S_QUOTED_IDENTIFIER>) { columnReference = new CollateExpression(columnReference, collateToken.image); } ]
[ LOOKAHEAD(2) ( <K_ASC> | (<K_DESC> { orderByElement.setAsc(false); } )) { orderByElement.setAscDescPresent(true); } ]
[ LOOKAHEAD(2) <K_NULLS>
[ LOOKAHEAD(2) (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.sf.jsqlparser.statement.select;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;

import net.sf.jsqlparser.JSQLParserException;
import org.junit.jupiter.api.Test;

public class OrderByCollateTest {

@Test
public void testOrderByWithCollate() throws JSQLParserException {
String sql = "SELECT * FROM a ORDER BY CAST(a.xyz AS TEXT) COLLATE \"und-x-icu\" ASC NULLS FIRST";
assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testOrderByWithCollateSimple() throws JSQLParserException {
String sql = "SELECT * FROM a ORDER BY col COLLATE \"C\" ASC";
assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testOrderByWithCollateMultiple() throws JSQLParserException {
String sql = "SELECT * FROM a ORDER BY col1 COLLATE \"C\" ASC, col2 COLLATE \"POSIX\" DESC";
assertSqlCanBeParsedAndDeparsed(sql);
}

@Test
public void testOrderByWithCollateAndNulls() throws JSQLParserException {
String sql = "SELECT * FROM a ORDER BY col COLLATE \"C\" DESC NULLS LAST";
assertSqlCanBeParsedAndDeparsed(sql);
}
}