Skip to content

Commit

Permalink
Properly handle EXTRACT function in HQL.
Browse files Browse the repository at this point in the history
HQL's extract function supports things like "day of week", "day of month", and "week of year". Extend support to these alternate expressions.

See #3219.
  • Loading branch information
gregturn committed Nov 6, 2023
1 parent 018c2ac commit 0aa5be8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ expression
| expression op=('*' | '/') expression # MultiplicationExpression
| expression op=('+' | '-') expression # AdditionExpression
| expression '||' expression # HqlConcatenationExpression
| DAY OF WEEK # DayOfWeekExpression
| DAY OF MONTH # DayOfMonthExpression
| WEEK OF YEAR # WeekOfYearExpression
;

primaryExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,42 @@ public List<JpaQueryParsingToken> visitHqlConcatenationExpression(HqlParser.HqlC
return tokens;
}

@Override
public List<JpaQueryParsingToken> visitDayOfWeekExpression(HqlParser.DayOfWeekExpressionContext ctx) {

List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.add(new JpaQueryParsingToken(ctx.DAY()));
tokens.add(new JpaQueryParsingToken(ctx.OF()));
tokens.add(new JpaQueryParsingToken(ctx.WEEK()));

return tokens;
}

@Override
public List<JpaQueryParsingToken> visitDayOfMonthExpression(HqlParser.DayOfMonthExpressionContext ctx) {

List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.add(new JpaQueryParsingToken(ctx.DAY()));
tokens.add(new JpaQueryParsingToken(ctx.OF()));
tokens.add(new JpaQueryParsingToken(ctx.MONTH()));

return tokens;
}

@Override
public List<JpaQueryParsingToken> visitWeekOfYearExpression(HqlParser.WeekOfYearExpressionContext ctx) {

List<JpaQueryParsingToken> tokens = new ArrayList<>();

tokens.add(new JpaQueryParsingToken(ctx.WEEK()));
tokens.add(new JpaQueryParsingToken(ctx.OF()));
tokens.add(new JpaQueryParsingToken(ctx.YEAR()));

return tokens;
}

@Override
public List<JpaQueryParsingToken> visitGroupedExpression(HqlParser.GroupedExpressionContext ctx) {

Expand Down Expand Up @@ -1915,11 +1951,12 @@ public List<JpaQueryParsingToken> visitExtractFunction(HqlParser.ExtractFunction

if (ctx.EXTRACT() != null) {

tokens.add(new JpaQueryParsingToken(ctx.EXTRACT()));
tokens.add(new JpaQueryParsingToken(ctx.EXTRACT(), false));
tokens.add(TOKEN_OPEN_PAREN);
tokens.addAll(visit(ctx.expression(0)));
tokens.add(new JpaQueryParsingToken(ctx.FROM()));
tokens.addAll(visit(ctx.expression(1)));
NOSPACE(tokens);
tokens.add(TOKEN_CLOSE_PAREN);
} else if (ctx.dateTimeFunction() != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,4 +1605,34 @@ void newShouldBeLegalAsPartOfAStateFieldPathExpression() {
void powerShouldBeLegalInAQuery() {
assertQuery("select e.power.id from MyEntity e");
}

@Test // GH-3219
void extractFunctionShouldSupportAdditionalExtensions() {

assertQuery("""
select extract(day of week from departureTime) AS day, sum(duration) as duration from JourneyEntity
group by extract(day of week from departureTime)
""");
assertQuery("""
select extract(day of month from departureTime) AS day, sum(duration) as duration from JourneyEntity
group by extract(day of month from departureTime)
""");
assertQuery("""
select extract(week of year from departureTime) AS day, sum(duration) as duration from JourneyEntity
group by extract(week of year from departureTime)
""");

assertQuery("""
select extract(date from departureTime) AS date
group by extract(date from departureTime)
""");
assertQuery("""
select extract(time from departureTime) AS time
group by extract(time from departureTime)
""");
assertQuery("""
select extract(epoch from departureTime) AS epoch
group by extract(epoch from departureTime)
""");
}
}

0 comments on commit 0aa5be8

Please sign in to comment.