-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from chris-twiner/temp/Spark3
resolve #427- Spark 3 support
- Loading branch information
Showing
14 changed files
with
126 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 13 additions & 15 deletions
28
dataset/src/main/scala/frameless/TypedExpressionEncoder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,46 @@ | ||
package frameless | ||
|
||
import org.apache.spark.sql.Encoder | ||
import org.apache.spark.sql.catalyst.analysis.GetColumnByOrdinal | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
import org.apache.spark.sql.catalyst.expressions.{BoundReference, CreateNamedStruct, If, Literal} | ||
import org.apache.spark.sql.catalyst.expressions.{BoundReference, CreateNamedStruct, If} | ||
import org.apache.spark.sql.types.StructType | ||
|
||
object TypedExpressionEncoder { | ||
|
||
/** In Spark, DataFrame has always schema of StructType | ||
* | ||
* DataFrames of primitive types become records with a single field called "_1". | ||
* DataFrames of primitive types become records with a single field called "value" set in ExpressionEncoder. | ||
*/ | ||
def targetStructType[A](encoder: TypedEncoder[A]): StructType = { | ||
encoder.catalystRepr match { | ||
case x: StructType => | ||
if (encoder.nullable) StructType(x.fields.map(_.copy(nullable = true))) | ||
else x | ||
case dt => new StructType().add("_1", dt, nullable = encoder.nullable) | ||
case dt => new StructType().add("value", dt, nullable = encoder.nullable) | ||
} | ||
} | ||
|
||
def apply[T: TypedEncoder]: ExpressionEncoder[T] = { | ||
def apply[T: TypedEncoder]: Encoder[T] = { | ||
val encoder = TypedEncoder[T] | ||
val schema = targetStructType(encoder) | ||
|
||
val in = BoundReference(0, encoder.jvmRepr, encoder.nullable) | ||
|
||
val (out, toRowExpressions) = encoder.toCatalyst(in) match { | ||
case If(_, _, x: CreateNamedStruct) => | ||
val out = BoundReference(0, encoder.catalystRepr, encoder.nullable) | ||
val (out, serializer) = encoder.toCatalyst(in) match { | ||
case it @ If(_, _, _: CreateNamedStruct) => | ||
val out = GetColumnByOrdinal(0, encoder.catalystRepr) | ||
|
||
(out, x.flatten) | ||
(out, it) | ||
case other => | ||
val out = GetColumnByOrdinal(0, encoder.catalystRepr) | ||
|
||
(out, CreateNamedStruct(Literal("_1") :: other :: Nil).flatten) | ||
(out, other) | ||
} | ||
|
||
new ExpressionEncoder[T]( | ||
schema = schema, | ||
flat = false, | ||
serializer = toRowExpressions, | ||
deserializer = encoder.fromCatalyst(out), | ||
objSerializer = serializer, | ||
objDeserializer = encoder.fromCatalyst(out), | ||
clsTag = encoder.classTag | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.