From 20e5a5659b25b0eaf2bee780ac7e4f61f0f8bc14 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Mon, 14 Sep 2020 12:35:33 +0200 Subject: [PATCH] HHH-14148 Fix ANTLR grammar non-determinism --- hibernate-core/src/main/antlr/sql-gen.g | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/antlr/sql-gen.g b/hibernate-core/src/main/antlr/sql-gen.g index 4f5f57a91e..ab9151d86c 100644 --- a/hibernate-core/src/main/antlr/sql-gen.g +++ b/hibernate-core/src/main/antlr/sql-gen.g @@ -146,7 +146,7 @@ selectStatement from ( #(WHERE { out(" where "); } whereExpr ) )? ( #(GROUP { out(" group by "); } groupExprs ( #(HAVING { out(" having "); } booleanExpr[false]) )? ) )? - ( #(ORDER { out(" order by "); } orderBySqlFragmentOrExprs ) )? + ( #(ORDER { out(" order by "); } orderExprs ) )? ) ; @@ -191,18 +191,15 @@ whereClauseExpr | booleanExpr[ false ] ; -orderBySqlFragmentOrExprs - : sqlToken // for the purpose of mapping-defined orderBy SQL fragment - | orderExprs - ; - orderExprs { String ordExp = null; String ordDir = null; String ordNul = null; } // TODO: remove goofy space before the comma when we don't have to regression test anymore. // Dialect is provided a hook to render each ORDER BY element, so the expression is being captured instead of // printing to the SQL output directly. See Dialect#renderOrderByElement(String, String, String, NullPrecedence). - : { captureExpressionStart(); } ( expr ) { captureExpressionFinish(); ordExp = resetCapture(); } + : { captureExpressionStart(); } ( e:expr ) { captureExpressionFinish(); ordExp = resetCapture(); } (dir:orderDirection { ordDir = #dir.getText(); })? (ordNul=nullOrdering)? - { out( renderOrderByElement( ordExp, ordDir, ordNul ) ); } + // SQL Tokens without a direction and null ordering can be passed through as-is. + // These tokens could be mapping defined order by fragments which are already rendered via the dialect hook + { out( #e.getType() == SQL_TOKEN && ordDir == null && ordNul == null ? ordExp : renderOrderByElement( ordExp, ordDir, ordNul ) ); } ( {out(", "); } orderExprs )? ;