Skip to content

Commit

Permalink
Supports ORDER BY and LIMIT clauses in both order (#344)
Browse files Browse the repository at this point in the history
* Add support for ORDER BY and LIMIT clauses in any order

* Fixes failing test
  • Loading branch information
subigre authored Mar 25, 2022
1 parent 012c658 commit 28f7974
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion aql/src/main/antlr4/org/ehrbase/aql/parser/Aql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion aql/src/test/java/org/ehrbase/aql/parser/AqlToDtoParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 28f7974

Please sign in to comment.