This is an automated email from the ASF dual-hosted git repository.
yuqi1129 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new bbf9d453ee [#13226] fix(api): make SingleFieldTransform.equals
type-strict (#13227)
bbf9d453ee is described below
commit bbf9d453ee18f4ea171c47429df8a2cee9c98503
Author: YangJie <[email protected]>
AuthorDate: Thu Sep 17 09:16:09 2026 -0400
[#13226] fix(api): make SingleFieldTransform.equals type-strict (#13227)
### What changes were proposed in this pull request?
`SingleFieldTransform.equals` now compares classes instead of using
`instanceof` (matching `BucketTransform` and the other sibling
transforms), with a justified `EqualsGetClass` suppression because the
base class is abstract.
### Why are the changes needed?
With the `instanceof` check, different transform strategies over the
same field compared equal (`year(ts).equals(identity(ts))` was `true`),
so a `HashSet` silently deduplicated distinct partitioning transforms.
Fix: #13226
### Does this PR introduce _any_ user-facing change?
Yes. This changes `equals` semantics for the `@Evolving` transform API:
transforms of different strategies over the same field are no longer
equal. It fixes silent `HashSet` deduplication of distinct transforms.
### How was this patch tested?
Added `TestTransformsEquality`, which pins that transforms of different
strategies over the same field are not equal and are not collapsed by a
`HashSet`; it fails on the pre-fix tree and passes after the fix.
---
.../rel/expressions/transforms/Transform.java | 6 ++-
.../transforms/TestTransformsEquality.java | 55 ++++++++++++++++++++++
.../catalog/iceberg/TestExpressionUtil.java | 2 +-
3 files changed, 61 insertions(+), 2 deletions(-)
diff --git
a/api/src/main/java/org/apache/gravitino/rel/expressions/transforms/Transform.java
b/api/src/main/java/org/apache/gravitino/rel/expressions/transforms/Transform.java
index 24ef19d174..54f745e311 100644
---
a/api/src/main/java/org/apache/gravitino/rel/expressions/transforms/Transform.java
+++
b/api/src/main/java/org/apache/gravitino/rel/expressions/transforms/Transform.java
@@ -80,12 +80,16 @@ public interface Transform extends Expression {
return new Expression[] {ref};
}
+ // Type-strict equality is intentional: two different transform types over
the same field
+ // (e.g. year(ts) and identity(ts)) must not compare equal. All concrete
subclasses are final
+ // and delegate to this implementation, so the getClass() check is exact,
not limiting.
+ @SuppressWarnings("EqualsGetClass")
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
- if (!(o instanceof SingleFieldTransform)) {
+ if (o == null || getClass() != o.getClass()) {
return false;
}
SingleFieldTransform that = (SingleFieldTransform) o;
diff --git
a/api/src/test/java/org/apache/gravitino/rel/expressions/transforms/TestTransformsEquality.java
b/api/src/test/java/org/apache/gravitino/rel/expressions/transforms/TestTransformsEquality.java
new file mode 100644
index 0000000000..c63a94c198
--- /dev/null
+++
b/api/src/test/java/org/apache/gravitino/rel/expressions/transforms/TestTransformsEquality.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.rel.expressions.transforms;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestTransformsEquality {
+
+ @Test
+ void testDifferentSingleFieldTransformTypesAreNotEqual() {
+ // Before the fix, SingleFieldTransform.equals used an instanceof check,
so transforms of
+ // different types over the same field compared equal.
+ Assertions.assertNotEquals(Transforms.identity("ts"),
Transforms.year("ts"));
+ Assertions.assertNotEquals(Transforms.year("ts"), Transforms.month("ts"));
+ Assertions.assertNotEquals(Transforms.month("ts"), Transforms.day("ts"));
+ Assertions.assertNotEquals(Transforms.day("ts"), Transforms.hour("ts"));
+ Assertions.assertNotEquals(Transforms.year("ts"), Transforms.hour("ts"));
+ }
+
+ @Test
+ void testSameSingleFieldTransformTypesAreEqual() {
+ Assertions.assertEquals(Transforms.year("ts"), Transforms.year("ts"));
+ Assertions.assertEquals(Transforms.identity("ts"),
Transforms.identity("ts"));
+ Assertions.assertNotEquals(Transforms.year("ts"),
Transforms.year("other"));
+ }
+
+ @Test
+ void testSameTypeSetSemantics() {
+ Set<Transform> transforms = new
HashSet<>(Arrays.asList(Transforms.year("ts")));
+
+ // Before the fix, a Set silently deduplicated year(ts) against
identity(ts).
+ Assertions.assertFalse(transforms.contains(Transforms.identity("ts")));
+ Assertions.assertTrue(transforms.contains(Transforms.year("ts")));
+ }
+}
diff --git
a/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/catalog/iceberg/TestExpressionUtil.java
b/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/catalog/iceberg/TestExpressionUtil.java
index 0b7f4975f7..d0e686642d 100644
---
a/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/catalog/iceberg/TestExpressionUtil.java
+++
b/trino-connector/trino-connector/src/test/java/org/apache/gravitino/trino/connector/catalog/iceberg/TestExpressionUtil.java
@@ -57,7 +57,7 @@ public class TestExpressionUtil {
partitionField = List.of("hour(f4)");
transforms = ExpressionUtil.partitionFiledToExpression(partitionField);
Assertions.assertEquals(1, transforms.length);
- Assertions.assertEquals(transforms[0], Transforms.day("f4"));
+ Assertions.assertEquals(transforms[0], Transforms.hour("f4"));
partitionField = List.of("bucket(f2,10)");
transforms = ExpressionUtil.partitionFiledToExpression(partitionField);