Skip to content

Make identification variables and the SELECT clause in JPQL optional #3903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -16,7 +16,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3902-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ grammar Eql;
@header {
/**
* Implementation of EclipseLink Query Language (EQL)
* See:
* * https://eclipse.dev/eclipselink/documentation/3.0/jpa/extensions/jpql.htm
* * https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL
*
* @see https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL
* @see https://eclipse.dev/eclipselink/documentation/3.0/jpa/extensions/jpql.htm
* @author Greg Turnquist
* @author Christoph Strobl
* @since 3.2
Expand All @@ -43,7 +42,8 @@ ql_statement
;

select_statement
: select_clause from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)?
: select_clause from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)? # SelectQuery
| from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)? # FromQuery
;

setOperator
Expand Down Expand Up @@ -80,7 +80,7 @@ identification_variable_declaration
;

range_variable_declaration
: (entity_name|function_invocation) AS? identification_variable
: (entity_name|function_invocation) AS? identification_variable?
;

join
Expand Down Expand Up @@ -246,14 +246,15 @@ orderby_clause
: ORDER BY orderby_item (',' orderby_item)*
;

// TODO Error in spec BNF, correctly shown elsewhere in spec.
orderby_item
: state_field_path_expression (ASC | DESC)? nullsPrecedence?
| general_identification_variable (ASC | DESC)? nullsPrecedence?
| result_variable (ASC | DESC)? nullsPrecedence?
| string_expression (ASC | DESC)? nullsPrecedence?
| scalar_expression (ASC | DESC)? nullsPrecedence?
|
: orderby_expression (ASC | DESC)? nullsPrecedence?
;

orderby_expression
: state_field_path_expression
| general_identification_variable
| string_expression
| scalar_expression
;

nullsPrecedence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ ql_statement
;

select_statement
: select_clause from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)?
: select_clause from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)? # SelectQuery
| from_clause (where_clause)? (groupby_clause)? (having_clause)? (orderby_clause)? (set_fuction)? # FromQuery
;

setOperator
Expand Down Expand Up @@ -72,18 +73,19 @@ from_clause
identificationVariableDeclarationOrCollectionMemberDeclaration
: identification_variable_declaration
| collection_member_declaration
| '(' subquery ')' identification_variable
;

identification_variable_declaration
: range_variable_declaration (join | fetch_join)*
;

range_variable_declaration
: entity_name AS? identification_variable
: entity_name AS? identification_variable?
;

join
: join_spec join_association_path_expression AS? identification_variable (join_condition)?
: join_spec join_association_path_expression AS? identification_variable? (join_condition)?
;

fetch_join
Expand All @@ -106,11 +108,11 @@ join_association_path_expression
;

join_collection_valued_path_expression
: identification_variable '.' (single_valued_embeddable_object_field '.')* collection_valued_field
: (identification_variable '.')? (single_valued_embeddable_object_field '.')* collection_valued_field
;

join_single_valued_path_expression
: identification_variable '.' (single_valued_embeddable_object_field '.')* single_valued_object_field
: (identification_variable '.')? (single_valued_embeddable_object_field '.')* single_valued_object_field
;

collection_member_declaration
Expand Down Expand Up @@ -244,9 +246,15 @@ orderby_clause
: ORDER BY orderby_item (',' orderby_item)*
;

// TODO Error in spec BNF, correctly shown elsewhere in spec.
orderby_item
: (state_field_path_expression | general_identification_variable | result_variable ) (ASC | DESC)? nullsPrecedence?
: orderby_expression (ASC | DESC)? nullsPrecedence?
;

orderby_expression
: state_field_path_expression
| general_identification_variable
| string_expression
| scalar_expression
;

nullsPrecedence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

import static org.springframework.data.jpa.repository.query.QueryTokens.*;

import org.springframework.data.jpa.repository.query.QueryRenderer.QueryRendererBuilder;

import org.jspecify.annotations.Nullable;

import org.springframework.data.jpa.repository.query.QueryRenderer.QueryRendererBuilder;
import org.springframework.data.jpa.repository.query.QueryTransformers.CountSelectionTokenStream;
import org.springframework.util.StringUtils;

/**
* An ANTLR {@link org.antlr.v4.runtime.tree.ParseTreeVisitor} that transforms a parsed EQL query into a
Expand All @@ -43,7 +44,7 @@ class EqlCountQueryTransformer extends EqlQueryRenderer {
}

@Override
public QueryTokenStream visitSelect_statement(EqlParser.Select_statementContext ctx) {
public QueryTokenStream visitSelectQuery(EqlParser.SelectQueryContext ctx) {

QueryRendererBuilder builder = QueryRenderer.builder();

Expand All @@ -63,6 +64,49 @@ public QueryTokenStream visitSelect_statement(EqlParser.Select_statementContext
return builder;
}

@Override
public QueryTokenStream visitFromQuery(EqlParser.FromQueryContext ctx) {

QueryRendererBuilder builder = QueryRenderer.builder();

QueryRendererBuilder countBuilder = QueryRenderer.builder();
countBuilder.append(TOKEN_SELECT_COUNT);

if (countProjection != null) {
countBuilder.append(QueryTokens.token(countProjection));
} else {
if (primaryFromAlias == null) {
countBuilder.append(TOKEN_DOUBLE_UNDERSCORE);
} else {
countBuilder.append(QueryTokens.token(primaryFromAlias));
}
}

countBuilder.append(TOKEN_CLOSE_PAREN);

builder.appendExpression(countBuilder);

if (ctx.from_clause() != null) {
builder.appendExpression(visit(ctx.from_clause()));
if (primaryFromAlias == null) {
builder.append(TOKEN_AS);
builder.append(TOKEN_DOUBLE_UNDERSCORE);
}
}

if (ctx.where_clause() != null) {
builder.appendExpression(visit(ctx.where_clause()));
}
if (ctx.groupby_clause() != null) {
builder.appendExpression(visit(ctx.groupby_clause()));
}
if (ctx.having_clause() != null) {
builder.appendExpression(visit(ctx.having_clause()));
}

return builder;
}

@Override
public QueryTokenStream visitSelect_clause(EqlParser.Select_clauseContext ctx) {

Expand All @@ -78,14 +122,21 @@ public QueryTokenStream visitSelect_clause(EqlParser.Select_clauseContext ctx) {
if (usesDistinct) {
nested.append(QueryTokens.expression(ctx.DISTINCT()));
nested.append(getDistinctCountSelection(QueryTokenStream.concat(ctx.select_item(), this::visit, TOKEN_COMMA)));
} else {
} else if (StringUtils.hasText(primaryFromAlias)) {
nested.append(QueryTokens.token(primaryFromAlias));
} else {
if (ctx.select_item().isEmpty()) {
// cannot happen as per grammar, but you never know…
nested.append(QueryTokens.token("1"));
} else {
nested.append(visit(ctx.select_item().get(0)));
}
}
} else {
builder.append(QueryTokens.token(countProjection));
if (usesDistinct) {
nested.append(QueryTokens.expression(ctx.DISTINCT()));
}
nested.append(QueryTokens.token(countProjection));
}

builder.appendInline(nested);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.Collections;
import java.util.List;

import org.springframework.data.jpa.repository.query.EqlParser.Range_variable_declarationContext;

import org.jspecify.annotations.Nullable;

/**
Expand Down Expand Up @@ -61,8 +59,9 @@ public Void visitSelect_clause(EqlParser.Select_clauseContext ctx) {
@Override
public Void visitRange_variable_declaration(EqlParser.Range_variable_declarationContext ctx) {

if (primaryFromAlias == null) {
primaryFromAlias = capturePrimaryAlias(ctx);
if (primaryFromAlias == null && ctx.identification_variable() != null && !EqlQueryRenderer.isSubquery(ctx)
&& !EqlQueryRenderer.isSetQuery(ctx)) {
primaryFromAlias = ctx.identification_variable().getText();
}

return super.visitRange_variable_declaration(ctx);
Expand All @@ -75,11 +74,6 @@ public Void visitConstructor_expression(EqlParser.Constructor_expressionContext
return super.visitConstructor_expression(ctx);
}

private static String capturePrimaryAlias(Range_variable_declarationContext ctx) {
return ctx.identification_variable() != null ? ctx.identification_variable().getText()
: ctx.entity_name().getText();
}

private static List<QueryToken> captureSelectItems(List<EqlParser.Select_itemContext> selections,
EqlQueryRenderer itemRenderer) {

Expand All @@ -94,4 +88,5 @@ private static List<QueryToken> captureSelectItems(List<EqlParser.Select_itemCon
}
return selectItemTokens;
}

}
Loading