mrhhsg opened a new pull request, #71415:
URL: https://github.com/apache/doris/pull/71415
### What problem does this PR solve?
Issue Number: None
Related PR: #67628
Problem Summary:
Backport the complete fix from #67628 to `branch-4.1`.
The lambda comparator accepted by `array_sort` is user SQL and may violate
strict weak ordering or return different answers between calls. Passing it to
`std::sort` relies on a contract that cannot be guaranteed and can crash BE.
Replace that sort with a bounded bottom-up merge sort over the element-index
permutation. Loop bounds and accesses are derived from the input range, not
from comparator consistency. Reuse a caller-owned `DorisVector` scratch buffer
across rows and preserve lambda error propagation. Consistent comparators
produce a stable sorted result; the order remains unspecified for inconsistent
comparators.
### Branch adaptations
- Retain the `branch-4.1` mutable `Selector*` execution interface rather
than importing the source branch's `const Selector*` signature.
- Preserve the existing behavior that skips comparator evaluation for
outer-NULL arrays.
- Include the upstream unit tests and SQL regression coverage. Remove only
the surplus trailing blank line in the expected-output file; expected values
are unchanged.
### Validation
- BE/FE Release build and packaging (`./build.sh --be --fe -j 24`),
including default FE Checkstyle: passed.
- ASAN BE unit tests: `./run-be-ut.sh --run
--filter="UntrustedComparatorSortTest.*:ArrayMapFunctionTest.*" -j 24` — 32
passed (9 sort-helper tests and 23 existing lambda tests), with no failures or
skips.
- SQL regression: `test_array_sort_lambda_comparator` and
`test_array_map_function` — 2 suites passed, with no failures or skips.
- clang-format 16 dry-run checks for all changed C++ files and `git diff
--check`: passed.
### Release note
Prevent BE crashes when a lambda comparator used by `array_sort` is
inconsistent or non-deterministic. Ordering is unspecified for such comparators.
### Check List (For Author)
- Test:
- [x] Regression test
- [x] Unit Test
- Behavior changed:
- [x] Yes. Lambda-based sorting no longer relies on an untrusted
comparator satisfying the standard-library sorting contract.
- Does this need documentation?
- [x] No.
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]