From 28f7974c2bd263146c0ab1f6334f06e2b2585962 Mon Sep 17 00:00:00 2001 From: Renaud Subiger Date: Fri, 25 Mar 2022 11:02:56 +0100 Subject: [PATCH] Supports ORDER BY and LIMIT clauses in both order (#344) * Add support for ORDER BY and LIMIT clauses in any order * Fixes failing test --- CHANGELOG.md | 1 + .../main/antlr4/org/ehrbase/aql/parser/Aql.g4 | 2 +- .../aql/parser/AqlToDtoParserTest.java | 32 ++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eef958bf0..e1da4e303 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Note: version releases in the 0.x.y range may introduce breaking changes. ## [unreleased] ### Added +- AQL: support `ORDER BY` and `LIMIT [OFFSET]` clauses in any order ([#344](https://github.com/ehrbase/openEHR_SDK/pull/344)) ### Fixed diff --git a/aql/src/main/antlr4/org/ehrbase/aql/parser/Aql.g4 b/aql/src/main/antlr4/org/ehrbase/aql/parser/Aql.g4 index 7876daf91..a1c8f02be 100644 --- a/aql/src/main/antlr4/org/ehrbase/aql/parser/Aql.g4 +++ b/aql/src/main/antlr4/org/ehrbase/aql/parser/Aql.g4 @@ -18,7 +18,7 @@ grammar Aql; query : queryExpr ; -queryExpr : select from (where)? (orderBy)? (limitExpr)? EOF ; +queryExpr : select from (where)? (orderBy limitExpr? | limitExpr orderBy?)? EOF ; select : SELECT selectExpr diff --git a/aql/src/test/java/org/ehrbase/aql/parser/AqlToDtoParserTest.java b/aql/src/test/java/org/ehrbase/aql/parser/AqlToDtoParserTest.java index 96b5d5039..f34d6968b 100644 --- a/aql/src/test/java/org/ehrbase/aql/parser/AqlToDtoParserTest.java +++ b/aql/src/test/java/org/ehrbase/aql/parser/AqlToDtoParserTest.java @@ -363,7 +363,8 @@ public void parseAqlLimitOffset() { + "limit 50 " + "order by e/ehr_id/value ASC "; - assertThrows(AqlParseException.class, () -> parser.parse(query3)); + aql = parser.parse(query3); + assertEquals(50, aql.getLimit()); var query4 = "select e/ehr_id/value " + "from EHR e " @@ -409,4 +410,33 @@ public void parseWhereClauseWithBoolean() { testAql(aql, "Select e/ehr_id/value as F1 from EHR e contains OBSERVATION o where o/data[at0001]/items[at0024]/items[at0025]/value/value != false"); } + + @Test + public void orderByAndLimitOrder() { + var aql1 = "Select " + + "c/name/value as Name, c/context/start_time as date_time, c/composer/name as Composer " + + "from EHR e contains COMPOSITION c " + + "order by c/context/start_time ASCENDING " + + "LIMIT 10 OFFSET 10"; + testAql(aql1, aql1); + + var aql2 = "Select " + + "c/name/value as Name, c/context/start_time as date_time, c/composer/name as Composer " + + "from EHR e contains COMPOSITION c " + + "LIMIT 10 OFFSET 10 " + + "order by c/context/start_time ASCENDING"; + testAql(aql2, aql1); + + var aql3 = "Select " + + "c/name/value as Name, c/context/start_time as date_time, c/composer/name as Composer " + + "from EHR e contains COMPOSITION c " + + "LIMIT 10 OFFSET 10"; + testAql(aql3, aql3); + + var aql4 = "Select " + + "c/name/value as Name, c/context/start_time as date_time, c/composer/name as Composer " + + "from EHR e contains COMPOSITION c " + + "order by c/context/start_time ASCENDING"; + testAql(aql4, aql4); + } }