Shixiong Zhu created SPARK-28456:
------------------------------------
Summary: Add a public API `Encoder.copyEncoder` to allow creating
Encoder without touching Scala reflections
Key: SPARK-28456
URL: https://issues.apache.org/jira/browse/SPARK-28456
Project: Spark
Issue Type: New Feature
Components: SQL
Affects Versions: 2.4.3
Reporter: Shixiong Zhu
Assignee: Shixiong Zhu
Because `Encoder` is not thread safe, the user cannot reuse an `Encoder` in
multiple `Dataset`s. However, creating an `Encoder` for a complicated class is
slow due to Scala reflections. To reduce the cost of Encoder creation, right
now I usually use the private API `ExpressionEncoder.copy` as follows:
{code}
object FooEncoder {
private lazy val _encoder: ExpressionEncoder[Foo] = ExpressionEncoder[Foo]()
implicit def encoder: ExpressionEncoder[Foo] = _encoder.copy()
}
{code}
This PR proposes a new method `copyEncoder` in `Encoder` so that the above
codes can be rewritten using public APIs.
{code}
object FooEncoder {
private lazy val _encoder: Encoder[Foo] = Encoders.product[Foo]()
implicit def encoder: Encoder[Foo] = _encoder.copyEncoder()
}
{code}
Regarding the method name,
- Why not use `copy`? It conflicts with `case class`'s copy.
- Why not use `clone`? It conflicts with `Object.clone`.
--
This message was sent by Atlassian JIRA
(v7.6.14#76016)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]