Skip to content

[FLINK-39677][table] Fix ARRAY_SORT comparator contract violation#28159

Open
jnh5y wants to merge 2 commits into
apache:masterfrom
jnh5y:FLINK-39677-internal-compare
Open

[FLINK-39677][table] Fix ARRAY_SORT comparator contract violation#28159
jnh5y wants to merge 2 commits into
apache:masterfrom
jnh5y:FLINK-39677-internal-compare

Conversation

@jnh5y
Copy link
Copy Markdown
Contributor

@jnh5y jnh5y commented May 14, 2026

The comparator built from a single SQL > probe returned +1 or -1 and never 0, so for equal elements compare(a,b) == compare(b,a) == -1. That violates antisymmetry and trips TimSort's contract check once an array is large enough to take the merge path (>= 32 elements with duplicates):

java.lang.IllegalArgumentException: Comparison method violates its
general contract!
    at java.util.TimSort.mergeHi(TimSort.java:903)
    ...
    at ArraySortFunction.eval(ArraySortFunction.java:91)

To fix this, we introduce an internal-only $COMPARE$1 function (analogous to the existing $HASHCODE$1) that returns a -1/0/+1 int and delegates its codegen to GenerateUtils.generateCompare - the same per-type compare helper ORDER BY already uses. ArraySortFunction now constructs a single INT-returning evaluator.

Coverage: a new 64-element BIGINT case in CollectionFunctionsITCase exercises the TimSort merge path with duplicates.

Generated-by: Claude (Opus 4.7)

Verifying this change

This change added tests which fails without the fix.

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): no
  • The public API, i.e., is any changed class annotated with @Public(Evolving): no
  • The serializers: no
  • The runtime per-record code paths (performance sensitive): no
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
  • The S3 file system connector: no

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

Was generative AI tooling used to co-author this PR?

Generated-by: Claude (Opus 4.7)

jnh5y added 2 commits May 13, 2026 16:31
…th duplicates

Reproduces "Comparison method violates its general contract!" at runtime
when an array contains many equal elements. The existing ARRAY_SORT cases
are all below TimSort's MIN_MERGE threshold (32) and contain at most one
duplicate, so they exercise only the binarySort path and never trip the
contract check.

The new cases use 64-element BIGINT arrays so that the merge path runs:
- all-equal (every element 42L)
- many-duplicates (four values, 16 occurrences each, interleaved)

Generated-by: Claude (Opus 4.7)
The previous comparator was built from a single SQL > evaluator and
returned +1 or -1, never 0 - so for equal elements
compare(a,b) == compare(b,a) == -1, violating antisymmetry and tripping
TimSort's contract check once an array is large enough to enter the
merge path:

    java.lang.IllegalArgumentException: Comparison method violates its
    general contract!
        at java.util.TimSort.mergeHi(TimSort.java:903)
        ...
        at ArraySortFunction.eval(ArraySortFunction.java:91)

Introduce an internal-only $COMPARE$1 function (analogous to the
existing $HASHCODE$1) that returns a -1/0/+1 int and delegates its
codegen to GenerateUtils.generateCompare - the same per-type compare
helper ORDER BY already uses. ArraySortFunction now constructs a single
INT-returning evaluator and invokes the resulting MethodHandle once per
pair instead of two boolean probes.

Benefits over a two-probe fix:
- one MethodHandle invocation per compare instead of one-or-two
- reuses existing per-type compare codegen (CHAR/VARCHAR/DECIMAL/
  TIMESTAMP via compareTo, primitives via direct compare, ROW/ARRAY
  recursive, RAW via Comparable)
- reusable for future array functions that need ordering

Generated-by: Claude (Opus 4.7)
@flinkbot
Copy link
Copy Markdown
Collaborator

flinkbot commented May 14, 2026

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants