Skip to content

[CALCITE-7893] Type mapping rules can be corrupted when identity hash codes of SqlTypeName values collide - #5364

Open
vlsi wants to merge 1 commit into
apache:mainfrom
vlsi:calcite-7893-type-mapping-hash
Open

vlsi wants to merge 1 commit into
apache:mainfrom
vlsi:calcite-7893-type-mapping-hash

Conversation

@vlsi

@vlsi vlsi commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes CALCITE-7893.

Why

SqlTypeMappingRules.Builder#add used the caller's set as the key of a Guava LoadingCache. SqlTypeAssignmentRule and SqlTypeCoercionRule pass one HashSet that they clear and refill between calls, so every cache entry was keyed by the same mutable object. A set whose hash code equaled an earlier set's received the earlier set's source types. SqlTypeName is an enum, so the hash code of a set is a sum of identity hash codes, and whether two sets collide changes from one JVM run to the next.

With -XX:+UnlockExperimentalVMOptions -XX:hashCode=2, which makes every identity hash code 1, TIME becomes assignable from DATE only: CAST(DATE '1243-12-01' AS TIME) passes validation, and user-defined functions resolve to the wrong overload or to none (No match found for function signature timeFun(<TIME>)). 41 tests in :core:test fail that way (CI run from the CALCITE-7892 trial).

What

Builder#add copies the set with Sets.immutableEnumSet before using it as a key and interns equal sets in a plain HashMap. The builder is filled once, in a static initializer, so it needs neither eviction nor thread safety. The validation path reads the same Map<SqlTypeName, ImmutableSet<SqlTypeName>> with ImmutableEnumSet values as before.

Verification

SqlTypeUtilTest#testTypeMappingBuilderKeepsSourceTypesOfReusedSet (new) forces the collision with a HashSet whose hashCode() is constant. Without the fix it fails with TIME=[DATE] where it expects TIME=[TIME], and it needs no JVM option. With the fix, all 17089 tests of :core:test pass under -XX:hashCode=2 on JDK 21:

cat > hash.gradle <<'G'
allprojects { tasks.withType(Test).configureEach { jvmArgs '-XX:+UnlockExperimentalVMOptions', '-XX:hashCode=2' } }
G
./gradlew -I hash.gradle :core:test

Adding a hashCode=2 axis to CI is left to CALCITE-7892.

🤖 Generated with Claude Code

… codes of SqlTypeName values collide

SqlTypeMappingRules.Builder#add used the caller's set as the key of a
Guava LoadingCache. SqlTypeAssignmentRule and SqlTypeCoercionRule pass
one HashSet that they clear and refill between calls, so every cache
entry was keyed by the same mutable object, and a set whose hash code
equaled an earlier set's received the earlier set's source types.
SqlTypeName is an enum, so a set's hash code is the sum of identity hash
codes, which change from one JVM run to the next.

The JVM option -XX:+UnlockExperimentalVMOptions -XX:hashCode=2 makes
every identity hash code 1 and forces the collision: TIME became
assignable from DATE only, CAST(DATE '1243-12-01' AS TIME) passed
validation, and user-defined functions resolved to the wrong overload
or to none, for example "No match found for function signature
timeFun(<TIME>)".

Builder#add now copies the set with Sets.immutableEnumSet before using
it as a key, and interns equal sets in a plain HashMap: the builder is
filled once, in a static initializer, so it needs neither eviction nor
thread safety.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant