Skip to content

Conversation

@ritvibhatt
Copy link
Contributor

@ritvibhatt ritvibhatt commented Oct 23, 2025

Description

Fixed PPL sort command so that asc/desc keywords specify sort direction for individual fields instead of applying all fields in the sort command. (keywords work on individual fields ex sort field1 asc, field2 desc means field1 is ascending and field2 is descending) while preventing mixing of prefix (+/-) and suffix (asc/desc) syntax, and updated integration tests to match this behavior.

Files Changed

  • ppl/src/main/antlr/OpenSearchPPLParser.g4: Updated grammar rules for sort command to support individual field ASC/DESC keywords
  • ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java: Updated visitSortCommand() method to handle new ASC/DESC field syntax and prevent mixing of +/-and asc/desc sort syntax
  • ppl/src/main/java/org/opensearch/sql/ppl/parser/AstExpressionBuilder.java: Modified expression building logic for sort field parsing and ensure consistent sort direction syntax usage across all sort fields
  • ppl/src/main/java/org/opensearch/sql/ppl/utils/ArgumentFactory.java: Updated getSortArguments() method to process new field-level ASC/DESC syntax

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Ritvi Bhatt <[email protected]>
Signed-off-by: Ritvi Bhatt <[email protected]>
Signed-off-by: Ritvi Bhatt <[email protected]>
Signed-off-by: Ritvi Bhatt <[email protected]>
@LantaoJin LantaoJin added the enhancement New feature or request label Oct 24, 2025
@ritvibhatt ritvibhatt changed the title Update asc/desc keyword behavior for sort command Fix asc/desc keyword behavior for sort command Oct 24, 2025
@ritvibhatt ritvibhatt marked this pull request as ready for review October 24, 2025 15:52

sortField
: (PLUS | MINUS)? sortFieldExpression
: (PLUS | MINUS) sortFieldExpression (ASC | A | DESC | D) # invalidMixedSortField
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to define invalid syntax? wouldn't it fail parse without this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would fail the parse but added this to have a more clear error for when the two syntaxes are mixed

* [asc|a|desc|d] (Since 3.3): optional. asc/a keeps the sort order as specified. desc/d reverses the sort results. If multiple fields are specified with desc/d, reverses order of the first field then for all duplicate values of the first field, reverses the order of the values of the second field and so on. **Default:** asc.

.. note::
You cannot mix +/- and asc/desc in the same sort command. Choose one approach for all fields in a single sort command.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to prohibit this? Any concern to allow the mixture?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added because the syntax gets confusing if there's a lot of mixing of the 2 different syntaxes


UnresolvedExpression fieldExpression =
visit(ctx.sortFieldExpression().fieldExpression().qualifiedName());
private Field buildSortField(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not critical, but please improve if you have capacity.
Following logics looks weird to me since visitPrefixSortField / visitSuffixSortField / visitDefaultSortField already know their type, but come to the same place and branch based on the context class.
I don't have clear idea to improve it, but I think there are better way to organize the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it is a bit confusing, I can take this up as a task in a follow up PR to improve the logic here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants