Skip to content

Commit

Permalink
Cleanup QueryTokenStream methods.
Browse files Browse the repository at this point in the history
Remove unused methods. Introduce QueryTokenStream.from and QueryTokenStream.ofToken() factory methods. Migrate JPQL visitors to consistently return token streams instead of mixing expression streams when obtaining values from nodes/terminal nodes.

Remove also unused concat methods for consistency.

We now instead decide on the composition (calling) site whether a token (stream) should be inlined, an expression or used as-is.

Original Pull Request: #3695
  • Loading branch information
mp911de authored and christophstrobl committed Dec 20, 2024
1 parent b1cec72 commit f3bb3d4
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,8 @@ public QueryTokenStream visitIdentification_variable_declaration(
QueryRendererBuilder builder = QueryRenderer.builder();

builder.append(visit(ctx.range_variable_declaration()));

ctx.join().forEach(joinContext -> {
builder.append(visit(joinContext));
});
ctx.fetch_join().forEach(fetchJoinContext -> {
builder.append(visit(fetchJoinContext));
});
builder.appendExpression(QueryTokenStream.concat(ctx.join(), this::visit, TOKEN_SPACE));
builder.appendExpression(QueryTokenStream.concat(ctx.fetch_join(), this::visit, TOKEN_SPACE));

return builder;
}
Expand Down Expand Up @@ -588,7 +583,7 @@ public QueryTokenStream visitNew_value(EqlParser.New_valueContext ctx) {
} else if (ctx.simple_entity_expression() != null) {
return visit(ctx.simple_entity_expression());
} else if (ctx.NULL() != null) {
return QueryRendererBuilder.from(QueryTokens.expression(ctx.NULL()));
return QueryTokenStream.ofToken(ctx.NULL());
} else {
return QueryRenderer.builder();
}
Expand Down Expand Up @@ -1485,7 +1480,7 @@ public QueryTokenStream visitRegexpComparison(EqlParser.RegexpComparisonContext

@Override
public QueryTokenStream visitComparison_operator(EqlParser.Comparison_operatorContext ctx) {
return QueryRenderer.from(QueryTokens.token(ctx.op));
return QueryTokenStream.ofToken(ctx.op);
}

@Override
Expand Down Expand Up @@ -1899,16 +1894,19 @@ public QueryTokenStream visitFunctions_returning_strings(EqlParser.Functions_ret

builder.append(QueryTokens.token(ctx.TRIM()));
builder.append(TOKEN_OPEN_PAREN);

QueryRendererBuilder nested = QueryRenderer.builder();
if (ctx.trim_specification() != null) {
builder.appendExpression(visit(ctx.trim_specification()));
nested.appendExpression(visit(ctx.trim_specification()));
}
if (ctx.trim_character() != null) {
builder.appendExpression(visit(ctx.trim_character()));
nested.appendExpression(visit(ctx.trim_character()));
}
if (ctx.FROM() != null) {
builder.append(QueryTokens.expression(ctx.FROM()));
nested.append(QueryTokens.expression(ctx.FROM()));
}
builder.append(visit(ctx.string_expression(0)));
nested.append(visit(ctx.string_expression(0)));
builder.appendInline(nested);
builder.append(TOKEN_CLOSE_PAREN);
} else if (ctx.LOWER() != null) {

Expand Down Expand Up @@ -1954,11 +1952,11 @@ public QueryTokenStream visitFunctions_returning_strings(EqlParser.Functions_ret
public QueryTokenStream visitTrim_specification(EqlParser.Trim_specificationContext ctx) {

if (ctx.LEADING() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.LEADING()));
return QueryTokenStream.ofToken(ctx.LEADING());
} else if (ctx.TRAILING() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.TRAILING()));
return QueryTokenStream.ofToken(ctx.TRAILING());
} else {
return QueryRenderer.from(QueryTokens.expression(ctx.BOTH()));
return QueryTokenStream.ofToken(ctx.BOTH());
}
}

Expand Down Expand Up @@ -2187,7 +2185,7 @@ public QueryTokenStream visitNullif_expression(EqlParser.Nullif_expressionContex
public QueryTokenStream visitTrim_character(EqlParser.Trim_characterContext ctx) {

if (ctx.CHARACTER() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.CHARACTER()));
return QueryTokenStream.ofToken(ctx.CHARACTER());
} else if (ctx.character_valued_input_parameter() != null) {
return visit(ctx.character_valued_input_parameter());
} else {
Expand All @@ -2199,11 +2197,11 @@ public QueryTokenStream visitTrim_character(EqlParser.Trim_characterContext ctx)
public QueryTokenStream visitIdentification_variable(EqlParser.Identification_variableContext ctx) {

if (ctx.IDENTIFICATION_VARIABLE() != null) {
return QueryRenderer.from(QueryTokens.token(ctx.IDENTIFICATION_VARIABLE()));
return QueryTokenStream.ofToken(ctx.IDENTIFICATION_VARIABLE());
} else if (ctx.type_literal() != null) {
return visit(ctx.type_literal());
} else if (ctx.f != null) {
return QueryRenderer.from(QueryTokens.token(ctx.f));
return QueryTokenStream.ofToken(ctx.f);
} else {
return QueryTokenStream.empty();
}
Expand All @@ -2218,15 +2216,15 @@ public QueryTokenStream visitConstructor_name(EqlParser.Constructor_nameContext
public QueryTokenStream visitLiteral(EqlParser.LiteralContext ctx) {

if (ctx.STRINGLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.STRINGLITERAL()));
return QueryTokenStream.ofToken(ctx.STRINGLITERAL());
} else if (ctx.JAVASTRINGLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.JAVASTRINGLITERAL()));
return QueryTokenStream.ofToken(ctx.JAVASTRINGLITERAL());
} else if (ctx.INTLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.INTLITERAL()));
return QueryTokenStream.ofToken(ctx.INTLITERAL());
} else if (ctx.FLOATLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.FLOATLITERAL()));
return QueryTokenStream.ofToken(ctx.FLOATLITERAL());
} else if (ctx.LONGLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.LONGLITERAL()));
return QueryTokenStream.ofToken(ctx.LONGLITERAL());
} else if (ctx.boolean_literal() != null) {
return visit(ctx.boolean_literal());
} else if (ctx.entity_type_literal() != null) {
Expand Down Expand Up @@ -2268,13 +2266,13 @@ public QueryTokenStream visitPattern_value(EqlParser.Pattern_valueContext ctx) {
public QueryTokenStream visitDate_time_timestamp_literal(EqlParser.Date_time_timestamp_literalContext ctx) {

if (ctx.STRINGLITERAL() != null) {
return QueryRendererBuilder.from(QueryTokens.expression(ctx.STRINGLITERAL()));
return QueryTokenStream.ofToken(ctx.STRINGLITERAL());
} else if (ctx.DATELITERAL() != null) {
return QueryRendererBuilder.from(QueryTokens.expression(ctx.DATELITERAL()));
return QueryTokenStream.ofToken(ctx.DATELITERAL());
} else if (ctx.TIMELITERAL() != null) {
return QueryRendererBuilder.from(QueryTokens.expression(ctx.TIMELITERAL()));
return QueryTokenStream.ofToken(ctx.TIMELITERAL());
} else if (ctx.TIMESTAMPLITERAL() != null) {
return QueryRendererBuilder.from(QueryTokens.expression(ctx.TIMESTAMPLITERAL()));
return QueryTokenStream.ofToken(ctx.TIMESTAMPLITERAL());
} else {
return QueryRenderer.builder();
}
Expand All @@ -2287,18 +2285,18 @@ public QueryTokenStream visitEntity_type_literal(EqlParser.Entity_type_literalCo

@Override
public QueryTokenStream visitEscape_character(EqlParser.Escape_characterContext ctx) {
return QueryRenderer.from(QueryTokens.expression(ctx.CHARACTER()));
return QueryTokenStream.ofToken(ctx.CHARACTER());
}

@Override
public QueryTokenStream visitNumeric_literal(EqlParser.Numeric_literalContext ctx) {

if (ctx.INTLITERAL() != null) {
return QueryRenderer.from(QueryTokens.token(ctx.INTLITERAL()));
return QueryTokenStream.ofToken(ctx.INTLITERAL());
} else if (ctx.FLOATLITERAL() != null) {
return QueryRenderer.from(QueryTokens.token(ctx.FLOATLITERAL()));
return QueryTokenStream.ofToken(ctx.FLOATLITERAL());
} else if (ctx.LONGLITERAL() != null) {
return QueryRenderer.from(QueryTokens.token(ctx.LONGLITERAL()));
return QueryTokenStream.ofToken(ctx.LONGLITERAL());
} else {
return QueryTokenStream.empty();
}
Expand All @@ -2308,9 +2306,9 @@ public QueryTokenStream visitNumeric_literal(EqlParser.Numeric_literalContext ct
public QueryTokenStream visitBoolean_literal(EqlParser.Boolean_literalContext ctx) {

if (ctx.TRUE() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.TRUE()));
return QueryTokenStream.ofToken(ctx.TRUE());
} else if (ctx.FALSE() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.FALSE()));
return QueryTokenStream.ofToken(ctx.FALSE());
} else {
return QueryTokenStream.empty();
}
Expand All @@ -2325,9 +2323,9 @@ public QueryTokenStream visitEnum_literal(EqlParser.Enum_literalContext ctx) {
public QueryTokenStream visitString_literal(EqlParser.String_literalContext ctx) {

if (ctx.CHARACTER() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.CHARACTER()));
return QueryTokenStream.ofToken(ctx.CHARACTER());
} else if (ctx.STRINGLITERAL() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.STRINGLITERAL()));
return QueryTokenStream.ofToken(ctx.STRINGLITERAL());
} else {
return QueryTokenStream.empty();
}
Expand Down Expand Up @@ -2400,7 +2398,7 @@ public QueryTokenStream visitFunction_name(EqlParser.Function_nameContext ctx) {
public QueryTokenStream visitCharacter_valued_input_parameter(EqlParser.Character_valued_input_parameterContext ctx) {

if (ctx.CHARACTER() != null) {
return QueryRenderer.from(QueryTokens.expression(ctx.CHARACTER()));
return QueryTokenStream.ofToken(ctx.CHARACTER());
} else if (ctx.input_parameter() != null) {
return visit(ctx.input_parameter());
} else {
Expand All @@ -2411,9 +2409,9 @@ public QueryTokenStream visitCharacter_valued_input_parameter(EqlParser.Characte
@Override
public QueryTokenStream visitReserved_word(EqlParser.Reserved_wordContext ctx) {
if (ctx.IDENTIFICATION_VARIABLE() != null) {
return QueryRenderer.from(QueryTokens.token(ctx.IDENTIFICATION_VARIABLE()));
return QueryTokenStream.ofToken(ctx.IDENTIFICATION_VARIABLE());
} else if (ctx.f != null) {
return QueryRenderer.from(QueryTokens.token(ctx.f));
return QueryTokenStream.ofToken(ctx.f);
} else {
return QueryTokenStream.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public QueryTokenStream visitSelect_item(EqlParser.Select_itemContext ctx) {
QueryTokenStream tokens = super.visitSelect_item(ctx);

if (ctx.result_variable() != null && !tokens.isEmpty()) {
transformerSupport.registerAlias(tokens.getLast());
transformerSupport.registerAlias(tokens.getRequiredLast());
}

return tokens;
Expand All @@ -122,7 +122,7 @@ public QueryTokenStream visitJoin(EqlParser.JoinContext ctx) {
QueryTokenStream tokens = super.visitJoin(ctx);

if (!tokens.isEmpty()) {
transformerSupport.registerAlias(tokens.getLast());
transformerSupport.registerAlias(tokens.getRequiredLast());
}

return tokens;
Expand Down
Loading

0 comments on commit f3bb3d4

Please sign in to comment.