You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2018/11/01 08:43:06 UTC

[GitHub] asfgit closed pull request #6951: [FLINK-10693] [Scala API] Fix incorrect duplication in EitherSerializer

asfgit closed pull request #6951: [FLINK-10693] [Scala API] Fix incorrect duplication in EitherSerializer
URL: https://github.com/apache/flink/pull/6951
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/flink-scala/src/main/java/org/apache/flink/api/scala/typeutils/ScalaEitherSerializerConfigSnapshot.java b/flink-scala/src/main/java/org/apache/flink/api/scala/typeutils/ScalaEitherSerializerConfigSnapshot.java
index 9566a663169..8ad4e09a1e2 100644
--- a/flink-scala/src/main/java/org/apache/flink/api/scala/typeutils/ScalaEitherSerializerConfigSnapshot.java
+++ b/flink-scala/src/main/java/org/apache/flink/api/scala/typeutils/ScalaEitherSerializerConfigSnapshot.java
@@ -27,8 +27,8 @@
  * Configuration snapshot for serializers of Scala's {@link Either} type,
  * containing configuration snapshots of the Left and Right serializers.
  */
-public class ScalaEitherSerializerConfigSnapshot<E extends Either<L, R>, L, R>
-		extends CompositeTypeSerializerConfigSnapshot<E> {
+public class ScalaEitherSerializerConfigSnapshot<L, R>
+		extends CompositeTypeSerializerConfigSnapshot<Either<L, R>> {
 
 	private static final int VERSION = 1;
 
diff --git a/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherSerializer.scala b/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherSerializer.scala
index 82637befdcd..14f2196b9cc 100644
--- a/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherSerializer.scala
+++ b/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherSerializer.scala
@@ -27,15 +27,24 @@ import org.apache.flink.core.memory.{DataInputView, DataOutputView}
  */
 @Internal
 @SerialVersionUID(9219995873023657525L)
-class EitherSerializer[A, B, T <: Either[A, B]](
+class EitherSerializer[A, B](
     val leftSerializer: TypeSerializer[A],
     val rightSerializer: TypeSerializer[B])
-  extends TypeSerializer[T] {
+  extends TypeSerializer[Either[A, B]] {
 
-  override def duplicate: EitherSerializer[A,B,T] = this
+  override def duplicate: EitherSerializer[A,B] = {
+    val leftDup = leftSerializer.duplicate()
+    val rightDup = rightSerializer.duplicate()
 
-  override def createInstance: T = {
-    Left(null).asInstanceOf[T]
+    if (leftDup.eq(leftSerializer) && rightDup.eq(rightSerializer)) {
+      this
+    } else {
+      new EitherSerializer[A, B](leftDup, rightDup)
+    }
+  }
+
+  override def createInstance: Either[A, B] = {
+    Left(null).asInstanceOf[Left[A, B]]
   }
 
   override def isImmutableType: Boolean = {
@@ -45,12 +54,12 @@ class EitherSerializer[A, B, T <: Either[A, B]](
 
   override def getLength: Int = -1
 
-  override def copy(from: T): T = from match {
-    case Left(a) => Left(leftSerializer.copy(a)).asInstanceOf[T]
-    case Right(b) => Right(rightSerializer.copy(b)).asInstanceOf[T]
+  override def copy(from: Either[A, B]): Either[A, B] = from match {
+    case Left(a) => Left(leftSerializer.copy(a))
+    case Right(b) => Right(rightSerializer.copy(b))
   }
 
-  override def copy(from: T, reuse: T): T = copy(from)
+  override def copy(from: Either[A, B], reuse: Either[A, B]): Either[A, B] = copy(from)
 
   override def copy(source: DataInputView, target: DataOutputView): Unit = {
     val isLeft = source.readBoolean()
@@ -62,7 +71,7 @@ class EitherSerializer[A, B, T <: Either[A, B]](
     }
   }
 
-  override def serialize(either: T, target: DataOutputView): Unit = either match {
+  override def serialize(either: Either[A, B], target: DataOutputView): Unit = either match {
     case Left(a) =>
       target.writeBoolean(true)
       leftSerializer.serialize(a, target)
@@ -71,27 +80,27 @@ class EitherSerializer[A, B, T <: Either[A, B]](
       rightSerializer.serialize(b, target)
   }
 
-  override def deserialize(source: DataInputView): T = {
+  override def deserialize(source: DataInputView): Either[A, B] = {
     val isLeft = source.readBoolean()
     if (isLeft) {
-      Left(leftSerializer.deserialize(source)).asInstanceOf[T]
+      Left(leftSerializer.deserialize(source))
     } else {
-      Right(rightSerializer.deserialize(source)).asInstanceOf[T]
+      Right(rightSerializer.deserialize(source))
     }
   }
 
-  override def deserialize(reuse: T, source: DataInputView): T = {
+  override def deserialize(reuse: Either[A, B], source: DataInputView): Either[A, B] = {
     val isLeft = source.readBoolean()
     if (isLeft) {
-      Left(leftSerializer.deserialize(source)).asInstanceOf[T]
+      Left(leftSerializer.deserialize(source))
     } else {
-      Right(rightSerializer.deserialize(source)).asInstanceOf[T]
+      Right(rightSerializer.deserialize(source))
     }
   }
 
   override def equals(obj: Any): Boolean = {
     obj match {
-      case eitherSerializer: EitherSerializer[_, _, _] =>
+      case eitherSerializer: EitherSerializer[_, _] =>
         eitherSerializer.canEqual(this) &&
         leftSerializer.equals(eitherSerializer.leftSerializer) &&
         rightSerializer.equals(eitherSerializer.rightSerializer)
@@ -100,7 +109,7 @@ class EitherSerializer[A, B, T <: Either[A, B]](
   }
 
   override def canEqual(obj: Any): Boolean = {
-    obj.isInstanceOf[EitherSerializer[_, _, _]]
+    obj.isInstanceOf[EitherSerializer[_, _]]
   }
 
   override def hashCode(): Int = {
@@ -111,15 +120,15 @@ class EitherSerializer[A, B, T <: Either[A, B]](
   // Serializer configuration snapshotting & compatibility
   // --------------------------------------------------------------------------------------------
 
-  override def snapshotConfiguration(): ScalaEitherSerializerConfigSnapshot[T, A, B] = {
-    new ScalaEitherSerializerConfigSnapshot[T, A, B](leftSerializer, rightSerializer)
+  override def snapshotConfiguration(): ScalaEitherSerializerConfigSnapshot[A, B] = {
+    new ScalaEitherSerializerConfigSnapshot[A, B](leftSerializer, rightSerializer)
   }
 
   override def ensureCompatibility(
-      configSnapshot: TypeSerializerConfigSnapshot[_]): CompatibilityResult[T] = {
+      configSnapshot: TypeSerializerConfigSnapshot[_]): CompatibilityResult[Either[A, B]] = {
 
     configSnapshot match {
-      case eitherSerializerConfig: ScalaEitherSerializerConfigSnapshot[T, A, B] =>
+      case eitherSerializerConfig: ScalaEitherSerializerConfigSnapshot[A, B] =>
         checkCompatibility(eitherSerializerConfig)
 
       // backwards compatibility path;
@@ -134,7 +143,7 @@ class EitherSerializer[A, B, T <: Either[A, B]](
 
   private def checkCompatibility(
       configSnapshot: CompositeTypeSerializerConfigSnapshot[_]
-    ): CompatibilityResult[T] = {
+    ): CompatibilityResult[Either[A, B]] = {
 
     val previousLeftRightSerWithConfigs =
       configSnapshot.getNestedSerializersAndConfigs
diff --git a/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherTypeInfo.scala b/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherTypeInfo.scala
index e89730951be..b2a055e5efc 100644
--- a/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherTypeInfo.scala
+++ b/flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/EitherTypeInfo.scala
@@ -52,18 +52,18 @@ class EitherTypeInfo[A, B, T <: Either[A, B]](
 
   @PublicEvolving
   def createSerializer(executionConfig: ExecutionConfig): TypeSerializer[T] = {
-    val leftSerializer = if (leftTypeInfo != null) {
+    val leftSerializer: TypeSerializer[A] = if (leftTypeInfo != null) {
       leftTypeInfo.createSerializer(executionConfig)
     } else {
-      new NothingSerializer
+      (new NothingSerializer).asInstanceOf[TypeSerializer[A]]
     }
 
-    val rightSerializer = if (rightTypeInfo != null) {
+    val rightSerializer: TypeSerializer[B] = if (rightTypeInfo != null) {
       rightTypeInfo.createSerializer(executionConfig)
     } else {
-      new NothingSerializer
+      (new NothingSerializer).asInstanceOf[TypeSerializer[B]]
     }
-    new EitherSerializer(leftSerializer, rightSerializer)
+    new EitherSerializer[A, B](leftSerializer, rightSerializer).asInstanceOf[TypeSerializer[T]]
   }
 
   override def equals(obj: Any): Boolean = {
diff --git a/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/EitherSerializerTest.scala b/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/EitherSerializerTest.scala
new file mode 100644
index 00000000000..f62d9a95ebb
--- /dev/null
+++ b/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/EitherSerializerTest.scala
@@ -0,0 +1,90 @@
+/*
+ * 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.flink.api.scala.typeutils
+
+import org.apache.flink.api.common.ExecutionConfig
+import org.apache.flink.api.common.typeutils.SerializerTestBase
+import org.apache.flink.api.common.typeutils.base.{IntSerializer, StringSerializer}
+import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer
+
+import org.junit.Test
+import org.junit.Assert.assertNotSame
+import org.junit.Assert.assertSame
+
+/**
+  * Test suite for the [[EitherSerializer]]
+  */
+class EitherSerializerTest extends SerializerTestBase[Either[String, Integer]] {
+
+  // --------------------------------------------------------------------------
+  //  test suite
+  // --------------------------------------------------------------------------
+
+  override protected def createSerializer() =
+    new EitherSerializer[String, Integer](
+      StringSerializer.INSTANCE,
+      IntSerializer.INSTANCE)
+
+  override protected def getLength: Int = -1
+
+  override protected def getTypeClass: Class[Either[String, Integer]] =
+    classOf[Either[String, Integer]]
+
+  override protected def getTestData: Array[Either[String, Integer]] =
+    Array[Either[String, Integer]](
+      Left("hello"),
+      Right(17),
+      Right(0),
+      Left("friend"),
+      Right(200),
+      Right(100),
+      Left("foo"),
+      Right(1060876234),
+      Left("bar")
+    )
+
+  // --------------------------------------------------------------------------
+  //  either serializer specific tests
+  // --------------------------------------------------------------------------
+
+  @Test
+  def testDuplication(): Unit = {
+    val serializerSS: EitherSerializer[String, String] =
+      new EitherSerializer[String, String](
+        StringSerializer.INSTANCE,
+        StringSerializer.INSTANCE
+      )
+
+    val serializerSO: EitherSerializer[String, Object] =
+      new EitherSerializer[String, Object](
+        StringSerializer.INSTANCE,
+        new KryoSerializer[Object](classOf[Object], new ExecutionConfig())
+      )
+
+    val serializerOS: EitherSerializer[Object, String] =
+      new EitherSerializer[Object, String](
+        new KryoSerializer[Object](classOf[Object], new ExecutionConfig()),
+        StringSerializer.INSTANCE
+      )
+
+    assertSame(serializerSS, serializerSS.duplicate())
+    assertNotSame(serializerSO, serializerSO.duplicate())
+    assertNotSame(serializerOS, serializerOS.duplicate())
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services