Skip to content

Commit

Permalink
HHH-14148 Fix ANTLR grammar non-determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Sep 14, 2020
1 parent 886083a commit 20e5a56
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions hibernate-core/src/main/antlr/sql-gen.g
Expand Up @@ -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 ) )?
)
;

Expand Down Expand Up @@ -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 )?
;

Expand Down

0 comments on commit 20e5a56

Please sign in to comment.