Skip to content

Bug: ak.union1d not NumPy-aligned for empty-input cases (returns non-unique / unsorted) #5273

Open

Description

@ajpotts

Bug: ak.union1d not NumPy-aligned for empty-input cases (returns non-unique / unsorted)

Summary

ak.union1d does not match numpy.union1d semantics when one input is empty. In these cases, Arkouda returns the non-empty input as-is (preserving duplicates and original order) instead of returning a sorted unique union.

This breaks NumPy alignment expectations and causes failures in the alignment verification suite.

Expected behavior (NumPy)

numpy.union1d(a, b) returns the sorted, unique values that are in either a or b, even if one of the inputs is empty.

Example:

  • np.union1d([], [20, 19, 4, 4, 4, 17, 2, 18, 3, 4])
    → [2, 3, 4, 17, 18, 19, 20]

Actual behavior (Arkouda)

When one input is empty, ak.union1d returns the non-empty input without applying uniqueness or sorting.

Example observed:

  • ak.union1d([], b) returns b unchanged:
    → [20, 19, 4, 4, 4, 17, 2, 18, 3, 4] (size 10)
    instead of [2, 3, 4, 17, 18, 19, 20] (size 7)

Reproduction

Test

tests/numpy/alignment_verification/pdarraysetops_alignment.py::test_union1d_matches_numpy

Failing parameterizations

  • test_union1d_matches_numpy[0-10-int64]
  • test_union1d_matches_numpy[0-10-uint64]
  • test_union1d_matches_numpy[10-0-int64]
  • test_union1d_matches_numpy[10-0-uint64]

(Other non-empty/non-empty cases pass, suggesting an empty-input fast path that bypasses unique/sort.)

Environment

  • Client warning observed during test run:
    • Version mismatch between client (2025.12.16+27...) and server (2025.12.16+26...)
    • While this may affect some behaviors, the failure pattern strongly suggests an empty-input code path omission in union1d.

Likely root cause

union1d appears to have an early-return optimization like:

  • if a.size == 0: return b
  • if b.size == 0: return a

which bypasses the normal unique + sort behavior.

Proposed fix

Ensure union1d always returns sorted uniques, including empty-input cases. For empty fast paths, apply unique (and sort if needed) before returning:

  • If ar1.size == 0: return sort(unique(ar2))
  • If ar2.size == 0: return sort(unique(ar1))

Or unify logic by always computing unique(concatenate([ar1, ar2])) (and sorting if unique is not guaranteed sorted).

Acceptance criteria

  • ak.union1d matches np.union1d for:
    • one empty, one non-empty (both orders)
    • both empty
    • both non-empty
    • int64 and uint64
  • Alignment test suite passes without xfails for union1d.

Activity

  1. added
    bug
    on Jan 9, 2026
  2. added a commit that references this issue on Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions