Skip to content

feat(operator): add Stable Incremental Merge Sort (Scala) - #3774

Merged
chenlica merged 28 commits into
apache:mainfrom
carloea2:New-Sort-OperatorScala
Oct 17, 2025
Merged

chenlica merged 28 commits into
apache:mainfrom
carloea2:New-Sort-OperatorScala

Conversation

@carloea2

@carloea2 carloea2 commented Sep 27, 2025 •

Copy link
Copy Markdown
Contributor

feat(operator): add Stable Merge Sort (Scala) with multi-key ordering, nulls handling, and offset/limit (Closes #3763)


Summary

Adds a new Stable Merge Sort operator in Scala. Performs stable per-partition sort with multiple keys and post-sort offset/limit.

Closes #3763.

Highlights

  • New files:

    • StableMergeSortOpDesc.scala – logical desc + JSON schema.
    • StableMergeSortOpExec.scala – stable merge sort on buffered tuples.
    • StableMergeSortOpExecSpec.scala – tests.
  • Features:

    • Per-key options: order: asc|desc, nulls: first|last, caseInsensitive (STRING only).
    • Supported types: STRING, INTEGER, LONG, DOUBLE, BOOLEAN, TIMESTAMP.
    • Stable ordering preserved for equal keys.
    • Offset/limit applied after sorting.

Tests

  • Asc/desc integers (stability).
  • Case-sensitive vs case-insensitive strings.
  • Nulls first/last.
  • Multi-key across mixed types.
  • Offset + limit.
  • Missing attribute / unsupported type errors.
  • Large input sanity.
  • Buffers until onFinish.

Example config

{
  "keys": [
    { "attribute": "dept",  "order": "asc",  "nulls": "last" },
    { "attribute": "score", "order": "desc", "nulls": "last" },
    { "attribute": "name",  "order": "asc",  "nulls": "last", "caseInsensitive": true }
  ],
  "offset": 0,
  "limit": null
}

…debounced valueChanges

Previously the Settings form updated only the FormControl; nothing invoked
setWorkflowDataTransferBatchSize(...) unless a manual handler fired, so the
new value often wasn’t propagated/persisted.

This change wires the control to a debounced valueChanges pipeline
(Number-coerced, validated, distinctUntilChanged) that calls
confirmUpdateDataTransferBatchSize(...).

- Add debounceTime(300), map/filter/distinctUntilChanged, untilDestroyed(this)
- Keep existing persistence policy (persist only when logged-in)

Behavior:
- Typing a valid number updates workflow settings and triggers persistence
  (once after 300ms of inactivity).
- Reload shows the saved value.
…ize via debounced valueChanges"

This reverts commit 7e7f5e3.
…debounced valueChanges

Previously the Settings form updated only the FormControl; nothing invoked setWorkflowDataTransferBatchSize(...) unless a manual handler fired, so the new value often wasn’t propagated/persisted.

This change wires the control to a debounced valueChanges pipeline
(Number-coerced, validated, distinctUntilChanged) that calls
confirmUpdateDataTransferBatchSize(...).

- Add debounceTime(300), map/filter/distinctUntilChanged, untilDestroyed(this)
- Keep existing persistence policy (persist only when logged-in)

Behavior:
- Typing a valid number updates workflow settings and triggers persistence
  (once after 300ms of inactivity).
- Reload shows the saved value.
…ize via debounced valueChanges"

This reverts commit 7e7f5e3.

(cherry picked from commit c2d1766)
@chenlica

Copy link
Copy Markdown
Contributor

@mengw15 Can you review it? I also assigned @Yicong-Huang as a reviewer.

@Yicong-Huang Yicong-Huang left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR. I haven't gone through all details yet, here are some comments:

  1. We should discuss whether we want to include Limit and Offset into Sort operator. For SQL, having LIMIT and OFFSET can be good optimization for a plan with sort. But we are dealing on the operator level, and users of operators may get confused by multiple limit and offsets (we have limit operator already). Think of standard Python sort, pandas sort, etc, they just sort the input data without other parameters. This is a design decision, and I am voting for "simplicity + modularity" over "performance".
  2. Your executor code is too defensive, meaning that you are checking everything to be null, invalid, or unsupported values. However, during runtime, having those checks have overheads. For operators, we have compilation phase to help validate operator parameters (e.g., a limit has to be a non-negative int). For data, we have schema validation before network layer to make sure the incoming data is exactly as described by the schema. Thus my suggestions is:
    A. move all operator parameter validations to compilation phase (see comments for details), so that they are conducted only once during compilation, not repeatedly for every tuple, and
    B. remove all checks on incoming data, as they are not necessary.
  3. I did not go to details of your merge sort implementation. I see that you are implementing from scratch using recursion. Two suggestions:
    A. If during pipelining, you are only accumulating data, and conduct sort only when all data finished (in onFinish), then I suggest you use a standard java/scala sort implementation, instead of implementing your own. The standard version would be much more stable.
    B. If you want to use your own implementation, I suggest you explore sort it incrementally. This might be the only reason why we need to implement it by ourselves instead of using the standard sort. Your current implementation is NOT incremental. It only gets sorted when all the data are received. For merge sort, you should be able to incrementally keep multiple sorted buckets while data is coming in, and only conduct merge phase when all data are received. Incremental version can make user experience better.

@Yicong-Huang

Yicong-Huang commented Sep 28, 2025 •

Copy link
Copy Markdown
Contributor

@carloea2 Other PR related comments:

  1. do not mention (Closes #3763) in PR title.
  2. try to be concise on the PR title, you have details in PR description already.
  3. remove @mengw15 in PR description. If you want to tag someone to review, after you created a PR, tag them with a new comment.
  4. assign yourself.
  5. add related tags.

@chenlica
chenlica self-requested a review September 28, 2025 21:54
@carloea2

carloea2 commented Sep 29, 2025 •

Copy link
Copy Markdown
Contributor Author

Next actions I will do:

  1. I will remove limit and offset options from the operator.

  2. I will move the operator parameter validations to compilation phase.

  3. Remove ALL checks on incoming data.

  4. Merge sort implementation: I will pick option B: do it incrementally

Thank you

@carloea2 carloea2 changed the title feat(operator): add Stable Merge Sort (Scala) with multi-key ordering, nulls handling, and offset/limit (Closes #3763) feat(operator): add Stable Incremental Merge Sort (Scala) Sep 29, 2025
@carloea2

Copy link
Copy Markdown
Contributor Author

New changes are ready. Thank you.

I could not change the tags of the PR (just as an extra note).

@chenlica

Copy link
Copy Markdown
Contributor

Thanks. @mengw15 Do you want to review it before @Yicong-Huang's pass?

@mengw15

mengw15 commented Sep 29, 2025

Copy link
Copy Markdown
Contributor

Thanks. @mengw15 Do you want to review it before @Yicong-Huang's pass?

sure

@mengw15 mengw15 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left some comments

This commit removes the option of placing nulls first or last, also follows other operators conventions in not placing "." in their descriptions.

Finally reuses Sort Criteria Unit to match previous python sort implementation
@github-actions github-actions Bot added the frontend Changes related to the frontend GUI label Oct 8, 2025
@carloea2

carloea2 commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

New changes are ready for review @mengw15, I still want to confirm whether the current reuse of SortCriteriaUnit matches your expectation.

Thank you.

Moved the new sort to shared folder with previous sort
@carloea2

Copy link
Copy Markdown
Contributor Author

I just made the commit to apply the folder structure changes.

Thanks,

@carloea2

Copy link
Copy Markdown
Contributor Author

I also prepared a workflow to test the new sort,

sort_testing_workflow.json

@Yicong-Huang Yicong-Huang left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This version is much better! Some more comments:

  1. use meaningful variable names, method names. Avoid using single-character names. Variable names such as va, vb, tpe and method names such as execute is not clear at all, could be very confusing.
  2. add docs for each method, especially you have many non-trivial methods such as pushRun, mergeRuns, please well document about their scope, limitations, expected output and possible issues. Add unit tests for those non-trivial methods to verify them.
  3. good to have: for some general methods, such as compare, tupleOf, schemaOf, consider add to more general places, so that these code can be reused. Think about what should be encapsulated in each module. For instance, the method compareNonNull uses only AttribtueType, then it might be better to put this method inside AttribtueType.

@carloea2

Copy link
Copy Markdown
Contributor Author

Hello good afternoon, I will take a look into your comments, thank you very much.

@Yicong-Huang

Yicong-Huang commented Oct 14, 2025 via email

Copy link
Copy Markdown
Contributor

@carloea2

Copy link
Copy Markdown
Contributor Author

I do not know what what be the exact use case to sort a binary field. But I just do not want user to select a binary field to sort and get an exception. I think we can support it, and let user decide whether sort binary is useful  or not. If we do not support it, then we need to add a frontend logic to disallow  user selecting field in binary type.
…
On Oct 14, 2025 at 3:02 PM -0700, carloea2 @.>, wrote: @carloea2 commented on this pull request. In common/workflow-operator/src/main/scala/org/apache/amber/operator/sort/StableMergeSortOpExec.scala: > + throw new IllegalStateException(s"Unsupported attribute type $other in StableMergeSort") + } Yes, but I wonder if it is binary, then greater or lower will greatly depend on the user context, no? If it is just by value, why not using another corresponding data type such as long? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>

@chenlica could you share your thoughts? I’m inclined to show a warning when a user tries to sort a binary field as @Yicong-Huang mentioned, maybe something like: “Sorting on binary fields isn’t supported.”
The warning would suggest using a sortable representation (e.g., extract a feature, length/hash, or a derived numeric/string field). I think this improves UX by guiding users on what to do next. Any concerns?

@chenlica

Copy link
Copy Markdown
Contributor

I suggest we disallow sort on a binary attribute. The frontend should disallow it.

@Yicong-Huang

Copy link
Copy Markdown
Contributor

I suggest we disallow sort on a binary attribute. The frontend should disallow it.

In this case, I suggest we merge this PR to unblock the existing use case, and @carloea2 can create an issue to track the implementation of frontend disallow logic. What do you think?

@chenlica

Copy link
Copy Markdown
Contributor

I agree. Let's finish this PR, and deal with the problem of "not sorting on binary attributes" as a separate issue.

@carloea2

carloea2 commented Oct 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Can we move the extraction of general utilities for reuse as another PR as well? I have identified some places where code can be reused but I think this PR is already large enough.

For this PR I am planning on adding docs, improving variables and methods naming and add unit tests for several non trivial methods.

is this plan OK?
@chenlica @Yicong-Huang

@Yicong-Huang

Copy link
Copy Markdown
Contributor

Can we move the extraction of general utilities for reuse as another PR as well? I have identified some places where code can be reused but I think this PR is already large enough.

For this PR I am planning on adding docs, improving variables and methods naming and add unit tests for several non trivial methods.

is this plan OK? @chenlica @Yicong-Huang

sounds good. please open issues for future tasks.

Added doc strings, unit testing of internal methods and renaming of vars and methods
@carloea2

Copy link
Copy Markdown
Contributor Author

Added the docs, renaming of variables/methods and unit tests to check incremental behavior and internal methods of Exec.

This week I will create issues about the extraction of general utilities and linking those to the pending conversations here to mark them as resolved.

Thank you.

@Yicong-Huang Yicong-Huang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

Please change PR description to use our new template, and fix minor comments and code format before merging.

@chenlica

Copy link
Copy Markdown
Contributor

@carloea2 Please take care of these comments quickly so that we can finish this PR.

@carloea2 carloea2 mentioned this pull request Oct 17, 2025
5 tasks
@carloea2

Copy link
Copy Markdown
Contributor Author

Ready to merge

@chenlica
chenlica merged commit 2780caa into apache:main Oct 17, 2025
@carloea2 carloea2 mentioned this pull request Oct 17, 2025
5 tasks
SarahAsad23 pushed a commit to madisonmlin/texera that referenced this pull request May 20, 2026
**feat(operator): add Stable Merge Sort (Scala) with multi-key ordering,
nulls handling, and offset/limit (Closes apache#3763)**

---

## Summary

Adds a new **Stable Merge Sort** operator in Scala. Performs stable
per-partition sort with multiple keys and post-sort offset/limit.

**Closes apache#3763.**

### Highlights

* New files:

  * `StableMergeSortOpDesc.scala` – logical desc + JSON schema.
* `StableMergeSortOpExec.scala` – stable merge sort on buffered tuples.
  * `StableMergeSortOpExecSpec.scala` – tests.
* Features:

* Per-key options: `order: asc|desc`, `nulls: first|last`,
`caseInsensitive` (STRING only).
  * Supported types: STRING, INTEGER, LONG, DOUBLE, BOOLEAN, TIMESTAMP.
  * Stable ordering preserved for equal keys.
  * Offset/limit applied **after** sorting.

### Tests

* Asc/desc integers (stability).
* Case-sensitive vs case-insensitive strings.
* Nulls first/last.
* Multi-key across mixed types.
* Offset + limit.
* Missing attribute / unsupported type errors.
* Large input sanity.
* Buffers until `onFinish`.

### Example config

```json
{
  "keys": [
    { "attribute": "dept",  "order": "asc",  "nulls": "last" },
    { "attribute": "score", "order": "desc", "nulls": "last" },
    { "attribute": "name",  "order": "asc",  "nulls": "last", "caseInsensitive": true }
  ],
  "offset": 0,
  "limit": null
}
```
---

---------

Signed-off-by: carloea2 <[email protected]>
yangzhang75 pushed a commit to yangzhang75/texera that referenced this pull request Jun 22, 2026
**feat(operator): add Stable Merge Sort (Scala) with multi-key ordering,
nulls handling, and offset/limit (Closes apache#3763)**

---

## Summary

Adds a new **Stable Merge Sort** operator in Scala. Performs stable
per-partition sort with multiple keys and post-sort offset/limit.

**Closes apache#3763.**

### Highlights

* New files:

  * `StableMergeSortOpDesc.scala` – logical desc + JSON schema.
* `StableMergeSortOpExec.scala` – stable merge sort on buffered tuples.
  * `StableMergeSortOpExecSpec.scala` – tests.
* Features:

* Per-key options: `order: asc|desc`, `nulls: first|last`,
`caseInsensitive` (STRING only).
  * Supported types: STRING, INTEGER, LONG, DOUBLE, BOOLEAN, TIMESTAMP.
  * Stable ordering preserved for equal keys.
  * Offset/limit applied **after** sorting.

### Tests

* Asc/desc integers (stability).
* Case-sensitive vs case-insensitive strings.
* Nulls first/last.
* Multi-key across mixed types.
* Offset + limit.
* Missing attribute / unsupported type errors.
* Large input sanity.
* Buffers until `onFinish`.

### Example config

```json
{
  "keys": [
    { "attribute": "dept",  "order": "asc",  "nulls": "last" },
    { "attribute": "score", "order": "desc", "nulls": "last" },
    { "attribute": "name",  "order": "asc",  "nulls": "last", "caseInsensitive": true }
  ],
  "offset": 0,
  "limit": null
}
```
---

---------

Signed-off-by: carloea2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common frontend Changes related to the frontend GUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a Sort operator using Scala

4 participants