This is an automated email from the ASF dual-hosted git repository. chaokunyang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/fury.git
The following commit(s) were added to refs/heads/main by this push: new 9621cdd4 fix(java): fix row type enum properties (#2258) 9621cdd4 is described below commit 9621cdd4d07b1d83c2190e3ccf99070292ed07af Author: Steven Schlansker <stevenschlans...@gmail.com> AuthorDate: Tue May 27 18:05:10 2025 -0700 fix(java): fix row type enum properties (#2258) ## What does this PR do? support Enum values in row format nested bean / record types --- .../main/java/org/apache/fury/type/TypeUtils.java | 2 + .../org/apache/fury/format/encoder/Encoders.java | 1 + .../apache/fury/format/row/binary/BinaryUtils.java | 4 ++ .../org/apache/fury/format/encoder/EnumTest.java | 69 ++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/java/fury-core/src/main/java/org/apache/fury/type/TypeUtils.java b/java/fury-core/src/main/java/org/apache/fury/type/TypeUtils.java index 1e8079c8..663654f8 100644 --- a/java/fury-core/src/main/java/org/apache/fury/type/TypeUtils.java +++ b/java/fury-core/src/main/java/org/apache/fury/type/TypeUtils.java @@ -681,6 +681,8 @@ public class TypeUtils { } Tuple2<TypeRef<?>, TypeRef<?>> mapKeyValueType = getMapKeyValueType(typeRef); return isSupported(mapKeyValueType.f0) && isSupported(mapKeyValueType.f1); + } else if (cls.isEnum()) { + return true; } else { ctx.checkNoCycle(typeRef); return isBean(typeRef, ctx.appendTypePath(typeRef)); diff --git a/java/fury-format/src/main/java/org/apache/fury/format/encoder/Encoders.java b/java/fury-format/src/main/java/org/apache/fury/format/encoder/Encoders.java index 84ac0471..3b581fd1 100644 --- a/java/fury-format/src/main/java/org/apache/fury/format/encoder/Encoders.java +++ b/java/fury-format/src/main/java/org/apache/fury/format/encoder/Encoders.java @@ -131,6 +131,7 @@ public class Encoders { * <li>primitive types: boolean, int, double, etc. * <li>boxed types: Boolean, Integer, Double, etc. * <li>String + * <li>Enum (as String) * <li>java.math.BigDecimal, java.math.BigInteger * <li>time related: java.sql.Date, java.sql.Timestamp, java.time.LocalDate, java.time.Instant * <li>Optional and friends: OptionalInt, OptionalLong, OptionalDouble diff --git a/java/fury-format/src/main/java/org/apache/fury/format/row/binary/BinaryUtils.java b/java/fury-format/src/main/java/org/apache/fury/format/row/binary/BinaryUtils.java index d488b0b1..53c47f63 100644 --- a/java/fury-format/src/main/java/org/apache/fury/format/row/binary/BinaryUtils.java +++ b/java/fury-format/src/main/java/org/apache/fury/format/row/binary/BinaryUtils.java @@ -60,6 +60,8 @@ public class BinaryUtils { return "getMap"; } else if (TypeUtils.isBean(type, ctx)) { return "getStruct"; + } else if (type.getRawType().isEnum()) { + return "getString"; } else { // take unknown type as OBJECT_TYPE, return as sliced MemoryBuffer // slice MemoryBuffer, then deserialize in EncodeExpressionBuilder.deserializeFor @@ -98,6 +100,8 @@ public class BinaryUtils { return TypeRef.of(BinaryMap.class); } else if (TypeUtils.isBean(type, ctx)) { return TypeRef.of(BinaryRow.class); + } else if (type.getRawType().isEnum()) { + return TypeUtils.STRING_TYPE; } else { // take unknown type as OBJECT_TYPE, return as sliced MemoryBuffer // slice MemoryBuffer, then deserialize in EncodeExpressionBuilder.deserializeFor diff --git a/java/fury-format/src/test/java/org/apache/fury/format/encoder/EnumTest.java b/java/fury-format/src/test/java/org/apache/fury/format/encoder/EnumTest.java new file mode 100644 index 00000000..1a56b409 --- /dev/null +++ b/java/fury-format/src/test/java/org/apache/fury/format/encoder/EnumTest.java @@ -0,0 +1,69 @@ +/* + * 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.fury.format.encoder; + +import java.util.Optional; +import lombok.Data; +import org.apache.fury.format.row.binary.BinaryRow; +import org.apache.fury.memory.MemoryBuffer; +import org.apache.fury.memory.MemoryUtils; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class EnumTest { + public enum TestEnum { + A, + B + } + + @Data + public static class EnumValue { + public TestEnum f1; + public Optional<TestEnum> f2; + + public EnumValue() {} + } + + @Test + public void testEnumPresent() { + EnumValue v = new EnumValue(); + v.f1 = TestEnum.B; + v.f2 = Optional.of(TestEnum.A); + RowEncoder<EnumValue> encoder = Encoders.bean(EnumValue.class); + BinaryRow row = encoder.toRow(v); + MemoryBuffer buffer = MemoryUtils.wrap(row.toBytes()); + row.pointTo(buffer, 0, buffer.size()); + EnumValue deserializedV = encoder.fromRow(row); + Assert.assertEquals(v, deserializedV); + } + + @Test + public void testEnumAbsent() { + EnumValue v = new EnumValue(); + v.f1 = TestEnum.A; + v.f2 = Optional.empty(); + RowEncoder<EnumValue> encoder = Encoders.bean(EnumValue.class); + BinaryRow row = encoder.toRow(v); + MemoryBuffer buffer = MemoryUtils.wrap(row.toBytes()); + row.pointTo(buffer, 0, buffer.size()); + EnumValue deserializedV = encoder.fromRow(row); + Assert.assertEquals(v, deserializedV); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@fury.apache.org For additional commands, e-mail: commits-h...@fury.apache.org