Skip to content

Commit 24f1400

Browse files
authored
Refactor PPL sort desc/asc logic (#5224)
1 parent 61e9ecd commit 24f1400

2 files changed

Lines changed: 18 additions & 62 deletions

File tree

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,18 +300,20 @@ public UnresolvedExpression visitRenameFieldExpression(RenameFieldExpressionCont
300300

301301
@Override
302302
public UnresolvedExpression visitPrefixSortField(OpenSearchPPLParser.PrefixSortFieldContext ctx) {
303-
return buildSortField(ctx.sortFieldExpression(), ctx);
303+
boolean ascending = ctx.MINUS() == null;
304+
return buildSortField(ctx.sortFieldExpression(), ascending);
304305
}
305306

306307
@Override
307308
public UnresolvedExpression visitSuffixSortField(OpenSearchPPLParser.SuffixSortFieldContext ctx) {
308-
return buildSortField(ctx.sortFieldExpression(), ctx);
309+
boolean ascending = (ctx.DESC() == null && ctx.D() == null);
310+
return buildSortField(ctx.sortFieldExpression(), ascending);
309311
}
310312

311313
@Override
312314
public UnresolvedExpression visitDefaultSortField(
313315
OpenSearchPPLParser.DefaultSortFieldContext ctx) {
314-
return buildSortField(ctx.sortFieldExpression(), ctx);
316+
return buildSortField(ctx.sortFieldExpression(), true);
315317
}
316318

317319
@Override
@@ -334,8 +336,7 @@ public UnresolvedExpression visitInvalidMixedSortField(
334336
}
335337

336338
private Field buildSortField(
337-
OpenSearchPPLParser.SortFieldExpressionContext sortFieldExpr,
338-
OpenSearchPPLParser.SortFieldContext parentCtx) {
339+
OpenSearchPPLParser.SortFieldExpressionContext sortFieldExpr, boolean ascending) {
339340
UnresolvedExpression fieldExpression = visit(sortFieldExpr.fieldExpression().qualifiedName());
340341

341342
if (sortFieldExpr.IP() != null) {
@@ -346,7 +347,12 @@ private Field buildSortField(
346347
fieldExpression = new Cast(fieldExpression, AstDSL.stringLiteral("string"));
347348
}
348349
// AUTO() case uses the field expression as-is
349-
return new Field(fieldExpression, ArgumentFactory.getArgumentList(parentCtx));
350+
351+
List<Argument> arguments =
352+
Arrays.asList(
353+
ArgumentFactory.createSortDirectionArgument(ascending),
354+
ArgumentFactory.getTypeArgument(sortFieldExpr));
355+
return new Field(fieldExpression, arguments);
350356
}
351357

352358
@Override

ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@
2727
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.ChartCommandContext;
2828
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.DecimalLiteralContext;
2929
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.DedupCommandContext;
30-
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.DefaultSortFieldContext;
3130
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.EventstatsCommandContext;
3231
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.FieldsCommandContext;
3332
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.IntegerLiteralContext;
34-
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.PrefixSortFieldContext;
35-
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SortFieldContext;
3633
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.StreamstatsCommandContext;
37-
import org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SuffixSortFieldContext;
3834
import org.opensearch.sql.ppl.parser.AstExpressionBuilder;
3935

4036
/** Util class to get all arguments as a list from the PPL command. */
@@ -155,63 +151,17 @@ public static List<Argument> getArgumentList(DedupCommandContext ctx) {
155151
}
156152

157153
/**
158-
* Get list of {@link Argument}.
154+
* Creates an "asc" argument for sort field direction.
159155
*
160-
* @param ctx SortFieldContext instance
161-
* @return the list of arguments fetched from the sort field in sort command
156+
* @param ascending true for ascending sort, false for descending
157+
* @return Argument representing the sort direction
162158
*/
163-
public static List<Argument> getArgumentList(SortFieldContext ctx) {
164-
if (ctx instanceof PrefixSortFieldContext) {
165-
return getArgumentList((PrefixSortFieldContext) ctx);
166-
} else if (ctx instanceof SuffixSortFieldContext) {
167-
return getArgumentList((SuffixSortFieldContext) ctx);
168-
} else {
169-
return getArgumentList((DefaultSortFieldContext) ctx);
170-
}
171-
}
172-
173-
/**
174-
* Get list of {@link Argument} for prefix sort field (+/- syntax).
175-
*
176-
* @param ctx PrefixSortFieldContext instance
177-
* @return the list of arguments fetched from the prefix sort field
178-
*/
179-
public static List<Argument> getArgumentList(PrefixSortFieldContext ctx) {
180-
return Arrays.asList(
181-
ctx.MINUS() != null
182-
? new Argument("asc", new Literal(false, DataType.BOOLEAN))
183-
: new Argument("asc", new Literal(true, DataType.BOOLEAN)),
184-
getTypeArgument(ctx.sortFieldExpression()));
185-
}
186-
187-
/**
188-
* Get list of {@link Argument} for suffix sort field (asc/desc syntax).
189-
*
190-
* @param ctx SuffixSortFieldContext instance
191-
* @return the list of arguments fetched from the suffix sort field
192-
*/
193-
public static List<Argument> getArgumentList(SuffixSortFieldContext ctx) {
194-
return Arrays.asList(
195-
(ctx.DESC() != null || ctx.D() != null)
196-
? new Argument("asc", new Literal(false, DataType.BOOLEAN))
197-
: new Argument("asc", new Literal(true, DataType.BOOLEAN)),
198-
getTypeArgument(ctx.sortFieldExpression()));
199-
}
200-
201-
/**
202-
* Get list of {@link Argument} for default sort field (no direction specified).
203-
*
204-
* @param ctx DefaultSortFieldContext instance
205-
* @return the list of arguments fetched from the default sort field
206-
*/
207-
public static List<Argument> getArgumentList(DefaultSortFieldContext ctx) {
208-
return Arrays.asList(
209-
new Argument("asc", new Literal(true, DataType.BOOLEAN)),
210-
getTypeArgument(ctx.sortFieldExpression()));
159+
public static Argument createSortDirectionArgument(boolean ascending) {
160+
return new Argument("asc", new Literal(ascending, DataType.BOOLEAN));
211161
}
212162

213163
/** Helper method to get type argument from sortFieldExpression. */
214-
private static Argument getTypeArgument(OpenSearchPPLParser.SortFieldExpressionContext ctx) {
164+
public static Argument getTypeArgument(OpenSearchPPLParser.SortFieldExpressionContext ctx) {
215165
if (ctx.AUTO() != null) {
216166
return new Argument("type", new Literal("auto", DataType.STRING));
217167
} else if (ctx.IP() != null) {

0 commit comments

Comments
 (0)