Skip to content

Fix flaky UnresolvedNamePatternTests equals check#143191

Open
ebarlas wants to merge 2 commits intoelastic:mainfrom
ebarlas:fix-unresolved-name-pattern-test-flake
Open

Fix flaky UnresolvedNamePatternTests equals check#143191
ebarlas wants to merge 2 commits intoelastic:mainfrom
ebarlas:fix-unresolved-name-pattern-test-flake

Conversation

@ebarlas
Copy link
Copy Markdown
Contributor

@ebarlas ebarlas commented Feb 26, 2026

UnresolvedNamePatternTests.mutateInstance had two bugs:

  1. Swapped constructor arguments — the constructor takes (source, automaton, patternString, name) but mutateInstance passed (source, null, name, pattern), swapping the two. This was masked when name and pattern differed (the common case).

  2. Mutating a non-equality fieldcase 0 only mutated name, but UnresolvedNamePattern.innerEquals only checks pattern. The name field is invisible to equality because UnresolvedNamedExpression passes "<unresolved>" as the superclass name to NamedExpression, and the overridden name() return value is never checked. When the random instance happened to generate the same value for both name and pattern, the swap became visible and case 1 produced a mutation with the same pattern as the original.

The fix ensures pattern is always mutated (guaranteeing inequality) and corrects the constructor argument order.

Reproduces with:

./gradlew ":x-pack:plugin:esql:test" --tests "org.elasticsearch.xpack.esql.expression.UnresolvedNamePatternTests.testEqualsAndHashcode" -Dtests.seed=103F03912D40F850 -Dtests.locale=pt-LU -Dtests.timezone=America/Nassau -Druntime.java=25 -Dtests.fips.enabled=true

@ebarlas ebarlas self-assigned this Feb 26, 2026
@ebarlas ebarlas added >test Issues or PRs that are addressing/adding tests v9.4.0 labels Feb 26, 2026
@elasticsearchmachine elasticsearchmachine added the needs:triage Requires assignment of a team area label label Feb 26, 2026
@ebarlas ebarlas added :Analytics/ES|QL AKA ESQL Team:ES|QL and removed needs:triage Requires assignment of a team area label labels Feb 26, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Feb 26, 2026
Copy link
Copy Markdown
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

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

Thanks a lot @ebarlas , you've uncovered an old hidden trap.

It needs fixing more than the test, though. I'll open a separate PR with a proposed fix and plan to absorb your test fix into it if that's ok.

switch (between(0, 1)) {
case 0 -> name = randomValueOtherThan(name, () -> randomAlphaOfLength(4));
case 1 -> pattern = randomValueOtherThan(pattern, () -> randomAlphaOfLength(4));
String pattern = randomValueOtherThan(instance.pattern(), () -> randomAlphaOfLength(4));
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 doesn't follow the normal pattern of "retrieve all relevant instance attributes, randomly mutated one". The pattern is always mutated. We need to test the case when the pattern stays the same and only the name is mutated.

Changing just the name should lead to inequality. I think this PR uncovered a genuine bug in UnresolvedNamePattern that needs fixing instead.

name = randomValueOtherThan(name, () -> randomAlphaOfLength(4));
}
return new UnresolvedNamePattern(source, null, name, pattern);
return new UnresolvedNamePattern(source, null, pattern, name);
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.

Swapping the arguments is a correct test fix. We should only merge this part and leave the mutation logic as it was before.

elasticsearchmachine pushed a commit that referenced this pull request Mar 2, 2026
Thanks @ebarlas for the find!

Supersedes #143191.

* Fix `UnresolvedNamePatternTests` by actually ensuring that the equality is sensitive to changes of the `name`.
* Fix `UnresolvedNamePattern#equals` and `#hashCode` to account for differences in the `name`

We probably never saw problems from this in production because the name
is derived from the pattern. Basic equality methods should still work
correctly, however.
alex-spies added a commit to alex-spies/elasticsearch that referenced this pull request Mar 2, 2026
Thanks @ebarlas for the find!

Supersedes elastic#143191.

* Fix `UnresolvedNamePatternTests` by actually ensuring that the equality is sensitive to changes of the `name`.
* Fix `UnresolvedNamePattern#equals` and `#hashCode` to account for differences in the `name`

We probably never saw problems from this in production because the name
is derived from the pattern. Basic equality methods should still work
correctly, however.
elasticsearchmachine pushed a commit that referenced this pull request Mar 2, 2026
Thanks @ebarlas for the find!

Supersedes #143191.

* Fix `UnresolvedNamePatternTests` by actually ensuring that the equality is sensitive to changes of the `name`.
* Fix `UnresolvedNamePattern#equals` and `#hashCode` to account for differences in the `name`

We probably never saw problems from this in production because the name
is derived from the pattern. Basic equality methods should still work
correctly, however.
tballison pushed a commit to tballison/elasticsearch that referenced this pull request Mar 3, 2026
Thanks @ebarlas for the find!

Supersedes elastic#143191.

* Fix `UnresolvedNamePatternTests` by actually ensuring that the equality is sensitive to changes of the `name`.
* Fix `UnresolvedNamePattern#equals` and `#hashCode` to account for differences in the `name`

We probably never saw problems from this in production because the name
is derived from the pattern. Basic equality methods should still work
correctly, however.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) Team:ES|QL >test Issues or PRs that are addressing/adding tests v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants