Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author TaeHyun Kang
* @author Jewoo Shin
* @since 3.2
*/
@SuppressWarnings({ "ConstantConditions", "DuplicatedCode" })
Expand Down Expand Up @@ -273,16 +274,16 @@ public QueryTokenStream visitSingle_valued_path_expression(EqlParser.Single_valu

QueryRendererBuilder builder = QueryRenderer.builder();

if (ctx.qualified_identification_variable() != null) {
builder.append(visit(ctx.qualified_identification_variable()));
} else if (ctx.qualified_identification_variable() != null) {
if (ctx.TREAT() != null) {

builder.append(QueryTokens.token(ctx.TREAT()));
builder.append(TOKEN_OPEN_PAREN);
builder.appendInline(visit(ctx.qualified_identification_variable()));
builder.append(QueryTokens.expression(ctx.AS()));
builder.appendInline(visit(ctx.subtype()));
builder.append(TOKEN_CLOSE_PAREN);
} else if (ctx.qualified_identification_variable() != null) {
builder.append(visit(ctx.qualified_identification_variable()));
} else if (ctx.state_field_path_expression() != null) {
builder.append(visit(ctx.state_field_path_expression()));
} else if (ctx.single_valued_object_path_expression() != null) {
Expand Down Expand Up @@ -553,8 +554,15 @@ public QueryTokenStream visitSubquery_from_clause(EqlParser.Subquery_from_clause
QueryRendererBuilder builder = QueryRenderer.builder();

builder.append(QueryTokens.expression(ctx.FROM()));
builder.appendExpression(
QueryTokenStream.concat(ctx.subselect_identification_variable_declaration(), this::visit, TOKEN_COMMA));

List<ParseTree> declarations = new ArrayList<>();
for (ParseTree child : ctx.children) {
if (child instanceof EqlParser.Subselect_identification_variable_declarationContext
|| child instanceof EqlParser.Collection_member_declarationContext) {
declarations.add(child);
}
}
builder.appendExpression(QueryTokenStream.concat(declarations, this::visit, TOKEN_COMMA));

return builder;
}
Expand Down Expand Up @@ -729,7 +737,7 @@ public QueryTokenStream visitFunctions_returning_numerics(EqlParser.Functions_re
builder.append(TOKEN_COMMA);
builder.appendInline(visit(ctx.string_expression(1)));

if (ctx.arithmetic_expression() != null) {
if (!ctx.arithmetic_expression().isEmpty()) {
builder.append(TOKEN_COMMA);
builder.appendInline(visit(ctx.arithmetic_expression(0)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Oscar Fanchin
* @author Mark Paluch
* @author TaeHyun Kang
* @author Jewoo Shin
* @since 3.1
*/
@SuppressWarnings({ "ConstantConditions", "DuplicatedCode", "UnreachableCode" })
Expand Down Expand Up @@ -1116,11 +1117,11 @@ public QueryTokenStream visitColumnFunction(HqlParser.ColumnFunctionContext ctx)
QueryRendererBuilder nested = QueryRenderer.builder();
nested.appendInline(visit(ctx.path()));
nested.append(TOKEN_DOT);
nested.appendExpression(visit(ctx.jpaNonstandardFunctionName()));
nested.append(visit(ctx.jpaNonstandardFunctionName()));

if (ctx.castTarget() != null) {
nested.append(QueryTokens.expression(ctx.AS()));
nested.appendExpression(visit(ctx.jpaNonstandardFunctionName()));
nested.append(visit(ctx.castTarget()));
}

builder.appendInline(nested);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author TaeHyun Kang
* @author Jewoo Shin
* @since 3.1
*/
@SuppressWarnings({ "ConstantConditions", "DuplicatedCode" })
Expand Down Expand Up @@ -274,16 +275,16 @@ public QueryTokenStream visitSingle_valued_path_expression(JpqlParser.Single_val

QueryRendererBuilder builder = QueryRenderer.builder();

if (ctx.qualified_identification_variable() != null) {
builder.append(visit(ctx.qualified_identification_variable()));
} else if (ctx.qualified_identification_variable() != null) {
if (ctx.TREAT() != null) {

builder.append(QueryTokens.token(ctx.TREAT()));
builder.append(TOKEN_OPEN_PAREN);
builder.appendInline(visit(ctx.qualified_identification_variable()));
builder.append(QueryTokens.expression(ctx.AS()));
builder.appendInline(visit(ctx.subtype()));
builder.append(TOKEN_CLOSE_PAREN);
} else if (ctx.qualified_identification_variable() != null) {
builder.append(visit(ctx.qualified_identification_variable()));
} else if (ctx.state_field_path_expression() != null) {
builder.append(visit(ctx.state_field_path_expression()));
} else if (ctx.single_valued_object_path_expression() != null) {
Expand Down Expand Up @@ -555,8 +556,15 @@ public QueryTokenStream visitSubquery_from_clause(JpqlParser.Subquery_from_claus
QueryRendererBuilder builder = QueryRenderer.builder();

builder.append(QueryTokens.expression(ctx.FROM()));
builder.appendExpression(
QueryTokenStream.concat(ctx.subselect_identification_variable_declaration(), this::visit, TOKEN_COMMA));

List<ParseTree> declarations = new ArrayList<>();
for (ParseTree child : ctx.children) {
if (child instanceof JpqlParser.Subselect_identification_variable_declarationContext
|| child instanceof JpqlParser.Collection_member_declarationContext) {
declarations.add(child);
}
}
builder.appendExpression(QueryTokenStream.concat(declarations, this::visit, TOKEN_COMMA));

return builder;
}
Expand Down Expand Up @@ -733,7 +741,7 @@ public QueryTokenStream visitFunctions_returning_numerics(JpqlParser.Functions_r
builder.append(TOKEN_COMMA);
builder.appendInline(visit(ctx.string_expression(1)));

if (ctx.arithmetic_expression() != null) {
if (!ctx.arithmetic_expression().isEmpty()) {
builder.append(TOKEN_COMMA);
builder.appendInline(visit(ctx.arithmetic_expression(0)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Mark Paluch
* @author Yannick Brandt
* @author Oscar Fanchin
* @author Jewoo Shin
* @since 3.1
*/
class HqlQueryRendererTests extends JpqlQueryRendererTckTests {
Expand Down Expand Up @@ -1814,4 +1815,12 @@ from xmltable('/root/elem' passing :xml columns theInt Integer,
""");
}

@Test // GH-4272
void columnFunctionWithCastTarget() {

assertQuery("select column(tbl.foo as int) from Entity tbl");
assertQuery("select column(tbl.foo as varchar(255)) from Entity tbl");
assertQuery("select column(tbl.foo) from Entity tbl");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Abstract TCK Tests for JPQL query rendering.
*
* @author Mark Paluch
* @author Jewoo Shin
*/
abstract class JpqlQueryRendererTckTests {

Expand Down Expand Up @@ -343,6 +344,13 @@ void stringConcatWithPipes() {
assertQuery("SELECT e.firstname || e.lastname AS name FROM Employee e");
}

@Test // GH-4272
void locateWithOptionalThirdArgument() {

assertQuery("SELECT LOCATE('a', e.name) FROM Employee e");
assertQuery("SELECT LOCATE('a', e.name, 2) FROM Employee e");
}

/**
* @see https://github.com/jakartaee/persistence/blob/master/spec/src/main/asciidoc/ch04-query-language.adoc#example
*/
Expand Down Expand Up @@ -567,6 +575,21 @@ SELECT TREAT(e as Integer).foo
""");
}

@Test // GH-4272
void treatQualifiedIdentificationVariableDowncast() {

assertQuery("SELECT TREAT(VALUE(m) AS Employee) FROM Department d JOIN d.employees m");
assertQuery("SELECT d FROM Department d JOIN d.employees m WHERE TREAT(VALUE(m) AS Employee) IS NOT NULL");
}

@Test // GH-4272
void subqueryFromWithCollectionMemberDeclaration() {

assertQuery("SELECT o FROM Order o WHERE EXISTS (SELECT l FROM Order o2, IN(o2.lineItems) l WHERE l.quantity > 5)");
assertQuery(
"SELECT o FROM Order o WHERE EXISTS (SELECT l FROM Order o2, IN(o2.lineItems) l, Order o3, IN(o3.lineItems) s WHERE l.quantity > 5)");
}

@Test
void fromClauseDowncastingExample1() {

Expand Down
Loading