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 3a8d478f fix(java): fix nested map chunk serialization codegen (#2172)
3a8d478f is described below
commit 3a8d478f21291e6abd15bc8b67d3e8275c359850
Author: Shawn Yang <[email protected]>
AuthorDate: Thu Apr 24 01:05:33 2025 +0800
fix(java): fix nested map chunk serialization codegen (#2172)
## What does this PR do?
fix nested map chunk serialization codegen
## Related issues
Closes #2170
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---
.../fury/builder/BaseObjectCodecBuilder.java | 1 +
.../java/org/apache/fury/codegen/Expression.java | 23 +++++++++++++++++++---
.../org/apache/fury/codegen/ExpressionUtils.java | 10 +++++++++-
.../org/apache/fury/codegen/ExpressionTest.java | 16 +++++++++++++++
.../serializer/collection/MapSerializersTest.java | 18 +++++++++++++++++
5 files changed, 64 insertions(+), 4 deletions(-)
diff --git
a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java
b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java
index 744ac711..aae2f236 100644
---
a/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java
+++
b/java/fury-core/src/main/java/org/apache/fury/builder/BaseObjectCodecBuilder.java
@@ -1799,6 +1799,7 @@ public abstract class BaseObjectCodecBuilder extends
CodecBuilder {
new Assign(
chunkHeader, cast(bitand(sizeAndHeader, ofInt(0xff)),
PRIMITIVE_INT_TYPE)),
new Assign(size, cast(shift(">>>", sizeAndHeader, 8),
PRIMITIVE_INT_TYPE)));
+ exprs.add(new If(eq(size, ofInt(0)), new Break()));
Expression sizeAndHeader2 =
readChunk(buffer, newMap, size, keyType, valueType,
chunkHeader);
if (inline) {
diff --git
a/java/fury-core/src/main/java/org/apache/fury/codegen/Expression.java
b/java/fury-core/src/main/java/org/apache/fury/codegen/Expression.java
index 221f9365..059bd048 100644
--- a/java/fury-core/src/main/java/org/apache/fury/codegen/Expression.java
+++ b/java/fury-core/src/main/java/org/apache/fury/codegen/Expression.java
@@ -247,6 +247,21 @@ public interface Expression {
return expressions;
}
+ public Expression last() {
+ return expressions.get(expressions.size() - 1);
+ }
+
+ public ListExpression add(Expression expr, boolean copy) {
+ Preconditions.checkNotNull(expr);
+ if (copy) {
+ ListExpression listExpression = new ListExpression();
+ listExpression.addAll(expressions);
+ listExpression.add(expr);
+ return listExpression;
+ }
+ return add(expr);
+ }
+
public ListExpression add(Expression expr) {
Preconditions.checkNotNull(expr);
this.expressions.add(expr);
@@ -1685,7 +1700,9 @@ public interface Expression {
this.predicate = predicate;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
-
+ if (ExpressionUtils.isReturn(trueExpr) &&
ExpressionUtils.isReturn(falseExpr)) {
+ type = PRIMITIVE_VOID_TYPE;
+ }
if (trueExpr.type() == falseExpr.type()) {
if (trueExpr.type() != null &&
!PRIMITIVE_VOID_TYPE.equals(trueExpr.type())) {
type = trueExpr.type();
@@ -1746,7 +1763,7 @@ public interface Expression {
}
TypeRef<?> type = this.type;
if (!PRIMITIVE_VOID_TYPE.equals(type.unwrap())) {
- if (trueExpr instanceof Return && falseExpr instanceof Return) {
+ if (ExpressionUtils.isReturn(trueExpr) &&
ExpressionUtils.isReturn(falseExpr)) {
type = PRIMITIVE_VOID_TYPE;
}
}
@@ -2651,7 +2668,7 @@ public interface Expression {
codeBuilder.append(targetExprCode.code()).append('\n');
}
codeBuilder.append("return ").append(targetExprCode.value()).append(';');
- return new ExprCode(codeBuilder.toString(), null, null);
+ return new ExprCode(codeBuilder.toString(), null,
targetExprCode.value());
}
@Override
diff --git
a/java/fury-core/src/main/java/org/apache/fury/codegen/ExpressionUtils.java
b/java/fury-core/src/main/java/org/apache/fury/codegen/ExpressionUtils.java
index 64f488aa..f3296d17 100644
--- a/java/fury-core/src/main/java/org/apache/fury/codegen/ExpressionUtils.java
+++ b/java/fury-core/src/main/java/org/apache/fury/codegen/ExpressionUtils.java
@@ -44,6 +44,7 @@ import org.apache.fury.codegen.Expression.ListExpression;
import org.apache.fury.codegen.Expression.LogicalAnd;
import org.apache.fury.codegen.Expression.LogicalOr;
import org.apache.fury.codegen.Expression.Null;
+import org.apache.fury.codegen.Expression.Return;
import org.apache.fury.codegen.Expression.Variable;
import org.apache.fury.reflect.ReflectionUtils;
import org.apache.fury.reflect.TypeRef;
@@ -61,6 +62,13 @@ public class ExpressionUtils {
return new NewArray(TypeRef.of(Object[].class), expressions);
}
+ public static boolean isReturn(Expression expr) {
+ if (expr instanceof ListExpression) {
+ expr = ((ListExpression) expr).last();
+ }
+ return expr instanceof Return;
+ }
+
public static Expression ofInt(String name, int v) {
return new Variable(name, Literal.ofInt(v));
}
@@ -98,7 +106,7 @@ public class ExpressionUtils {
public static LogicalOr or(Expression left, Expression right, Expression...
expressions) {
LogicalOr logicalOr = new LogicalOr(left, right);
for (Expression expression : expressions) {
- logicalOr = new LogicalOr(left, expression);
+ logicalOr = new LogicalOr(logicalOr, expression);
}
return logicalOr;
}
diff --git
a/java/fury-core/src/test/java/org/apache/fury/codegen/ExpressionTest.java
b/java/fury-core/src/test/java/org/apache/fury/codegen/ExpressionTest.java
index 15546e8d..1c6e3133 100644
--- a/java/fury-core/src/test/java/org/apache/fury/codegen/ExpressionTest.java
+++ b/java/fury-core/src/test/java/org/apache/fury/codegen/ExpressionTest.java
@@ -19,11 +19,15 @@
package org.apache.fury.codegen;
+import static org.apache.fury.codegen.ExpressionUtils.neq;
+import static org.apache.fury.codegen.ExpressionUtils.or;
import static org.apache.fury.type.TypeUtils.PRIMITIVE_SHORT_TYPE;
import static org.testng.Assert.assertNull;
+import org.apache.fury.codegen.Code.ExprCode;
import org.apache.fury.codegen.Expression.ListExpression;
import org.apache.fury.codegen.Expression.Literal;
+import org.apache.fury.codegen.Expression.LogicalOr;
import org.apache.fury.codegen.Expression.Reference;
import org.apache.fury.codegen.Expression.Return;
import org.testng.Assert;
@@ -78,4 +82,16 @@ public class ExpressionTest {
assertNull(code);
}
}
+
+ @Test
+ public void testMultipleOr() {
+ CodegenContext ctx = new CodegenContext();
+ LogicalOr or =
+ or(
+ Literal.ofBoolean(false),
+ neq(Literal.ofInt(3), Literal.ofInt(4)),
+ neq(Literal.ofInt(5), Literal.ofInt(6)));
+ ExprCode exprCode = or.genCode(ctx);
+ Assert.assertEquals(exprCode.value().code(), "((false || (3 != 4)) || (5
!= 6))");
+ }
}
diff --git
a/java/fury-core/src/test/java/org/apache/fury/serializer/collection/MapSerializersTest.java
b/java/fury-core/src/test/java/org/apache/fury/serializer/collection/MapSerializersTest.java
index f05de4e7..f0b94ddf 100644
---
a/java/fury-core/src/test/java/org/apache/fury/serializer/collection/MapSerializersTest.java
+++
b/java/fury-core/src/test/java/org/apache/fury/serializer/collection/MapSerializersTest.java
@@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -1004,4 +1005,21 @@ public class MapSerializersTest extends FuryTestBase {
Fury fury =
Fury.builder().requireClassRegistration(false).withCodegen(enableCodegen).build();
fury.deserialize(fury.serialize(obj));
}
+
+ @Data
+ public static class NestedStringLongListMap {
+ public Map<String, List<Long>> stringInt64ListMap;
+ }
+
+ @Test(dataProvider = "enableCodegen")
+ public void testNestedStringLongListMap(boolean enableCodegen) {
+ Fury fury =
Fury.builder().withLanguage(Language.JAVA).withCodegen(enableCodegen).build();
+ fury.register(NestedStringLongListMap.class);
+ NestedStringLongListMap pojo = new NestedStringLongListMap();
+ pojo.stringInt64ListMap = new HashMap<>();
+ pojo.stringInt64ListMap.put("a", Arrays.asList(100L, 200L, 300L));
+ pojo.stringInt64ListMap.put("b", null);
+ serDeCheck(fury, pojo);
+ fury.serialize(pojo);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]