-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encode Value class field using the inner type #546
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
53a96bf
Unit test about encoding Value class as dataframe row
cchantep 3bf12ac
Failing test for Value class as field
cchantep 34d631f
Failing test for Value class as optional field
cchantep b40f1a5
Support Value class in record encoder
cchantep b74abbf
Failing test in LitTest:
cchantep 0ab91ee
Prepare literal refactor, making sure first there is no regression
cchantep b43b0d8
Introduce functions.litValue
cchantep 4ae661a
IsValueClass evidence
cchantep ab19018
Prepare 0.11
cchantep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package frameless | ||
|
||
import shapeless._ | ||
import shapeless.labelled.FieldType | ||
|
||
/** Evidence that `T` is a Value class */ | ||
@annotation.implicitNotFound(msg = "${T} is not a Value class") | ||
final class IsValueClass[T] private() {} | ||
|
||
object IsValueClass { | ||
/** Provides an evidence `A` is a Value class */ | ||
implicit def apply[A <: AnyVal, G <: ::[_, HNil], H <: ::[_ <: FieldType[_ <: Symbol, _], HNil]]( | ||
implicit | ||
i0: LabelledGeneric.Aux[A, G], | ||
i1: DropUnitValues.Aux[G, H]): IsValueClass[A] = new IsValueClass[A] | ||
|
||
} |
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 |
---|---|---|
|
@@ -2,11 +2,15 @@ package frameless | |
|
||
import org.apache.spark.sql.FramelessInternals | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, NewInstance} | ||
import org.apache.spark.sql.catalyst.expressions.objects.{ | ||
Invoke, NewInstance, UnwrapOption, WrapOption | ||
} | ||
import org.apache.spark.sql.types._ | ||
|
||
import shapeless._ | ||
import shapeless.labelled.FieldType | ||
import shapeless.ops.hlist.IsHCons | ||
import shapeless.ops.record.Keys | ||
|
||
import scala.reflect.ClassTag | ||
|
||
|
@@ -25,24 +29,22 @@ object RecordEncoderFields { | |
implicit def deriveRecordLast[K <: Symbol, H] | ||
(implicit | ||
key: Witness.Aux[K], | ||
head: TypedEncoder[H] | ||
head: RecordFieldEncoder[H] | ||
): RecordEncoderFields[FieldType[K, H] :: HNil] = new RecordEncoderFields[FieldType[K, H] :: HNil] { | ||
def value: List[RecordEncoderField] = RecordEncoderField(0, key.value.name, head) :: Nil | ||
def value: List[RecordEncoderField] = fieldEncoder[K, H] :: Nil | ||
} | ||
|
||
implicit def deriveRecordCons[K <: Symbol, H, T <: HList] | ||
(implicit | ||
key: Witness.Aux[K], | ||
head: TypedEncoder[H], | ||
head: RecordFieldEncoder[H], | ||
tail: RecordEncoderFields[T] | ||
): RecordEncoderFields[FieldType[K, H] :: T] = new RecordEncoderFields[FieldType[K, H] :: T] { | ||
def value: List[RecordEncoderField] = { | ||
val fieldName = key.value.name | ||
val fieldEncoder = RecordEncoderField(0, fieldName, head) | ||
def value: List[RecordEncoderField] = | ||
fieldEncoder[K, H] :: tail.value.map(x => x.copy(ordinal = x.ordinal + 1)) | ||
} | ||
|
||
fieldEncoder :: tail.value.map(x => x.copy(ordinal = x.ordinal + 1)) | ||
} | ||
} | ||
private def fieldEncoder[K <: Symbol, H](implicit key: Witness.Aux[K], e: RecordFieldEncoder[H]): RecordEncoderField = RecordEncoderField(0, key.value.name, e.encoder) | ||
} | ||
|
||
/** | ||
|
@@ -156,6 +158,7 @@ class RecordEncoder[F, G <: HList, H <: HList] | |
|
||
val createExpr = CreateNamedStruct(exprs) | ||
val nullExpr = Literal.create(null, createExpr.dataType) | ||
|
||
If(IsNull(path), nullExpr, createExpr) | ||
} | ||
|
||
|
@@ -168,6 +171,86 @@ class RecordEncoder[F, G <: HList, H <: HList] | |
val newExpr = NewInstance(classTag.runtimeClass, newArgs, jvmRepr, propagateNull = true) | ||
|
||
val nullExpr = Literal.create(null, jvmRepr) | ||
|
||
If(IsNull(path), nullExpr, newExpr) | ||
} | ||
} | ||
|
||
final class RecordFieldEncoder[T]( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
val encoder: TypedEncoder[T]) extends Serializable | ||
|
||
object RecordFieldEncoder extends RecordFieldEncoderLowPriority { | ||
|
||
/** | ||
* @tparam F the value class | ||
* @tparam G the single field of the value class | ||
* @tparam H the single field of the value class (with guarantee it's not a `Unit` value) | ||
* @tparam K the key type for the fields | ||
* @tparam V the inner value type | ||
*/ | ||
implicit def optionValueClass[F : IsValueClass, G <: ::[_, HNil], H <: ::[_ <: FieldType[_ <: Symbol, _], HNil], K <: Symbol, V, KS <: ::[_ <: Symbol, HNil]] | ||
(implicit | ||
i0: LabelledGeneric.Aux[F, G], | ||
i1: DropUnitValues.Aux[G, H], | ||
i2: IsHCons.Aux[H, _ <: FieldType[K, V], HNil], | ||
i3: Keys.Aux[H, KS], | ||
i4: IsHCons.Aux[KS, K, HNil], | ||
i5: TypedEncoder[V], | ||
i6: ClassTag[F] | ||
): RecordFieldEncoder[Option[F]] = RecordFieldEncoder[Option[F]](new TypedEncoder[Option[F]] { | ||
val nullable = true | ||
|
||
val jvmRepr = ObjectType(classOf[Option[F]]) | ||
|
||
@inline def catalystRepr: DataType = i5.catalystRepr | ||
|
||
val innerJvmRepr = ObjectType(i6.runtimeClass) | ||
|
||
def fromCatalyst(path: Expression): Expression = { | ||
val javaValue = i5.fromCatalyst(path) | ||
val value = NewInstance(i6.runtimeClass, Seq(javaValue), innerJvmRepr) | ||
|
||
WrapOption(value, innerJvmRepr) | ||
} | ||
|
||
@inline def toCatalyst(path: Expression): Expression = { | ||
val value = UnwrapOption(innerJvmRepr, path) | ||
|
||
val fieldName = i4.head(i3()).name | ||
val javaValue = Invoke(value, fieldName, i5.jvmRepr, Nil) | ||
|
||
i5.toCatalyst(javaValue) | ||
} | ||
}) | ||
|
||
/** | ||
* @tparam F the value class | ||
* @tparam G the single field of the value class | ||
* @tparam H the single field of the value class (with guarantee it's not a `Unit` value) | ||
* @tparam V the inner value type | ||
*/ | ||
implicit def valueClass[F : IsValueClass, G <: ::[_, HNil], H <: ::[_, HNil], V] | ||
(implicit | ||
i0: LabelledGeneric.Aux[F, G], | ||
i1: DropUnitValues.Aux[G, H], | ||
i2: IsHCons.Aux[H, _ <: FieldType[_, V], HNil], | ||
i3: TypedEncoder[V], | ||
i4: ClassTag[F] | ||
): RecordFieldEncoder[F] = RecordFieldEncoder[F](new TypedEncoder[F] { | ||
def nullable = i3.nullable | ||
|
||
def jvmRepr = i3.jvmRepr | ||
|
||
def catalystRepr: DataType = i3.catalystRepr | ||
|
||
def fromCatalyst(path: Expression): Expression = | ||
i3.fromCatalyst(path) | ||
|
||
@inline def toCatalyst(path: Expression): Expression = | ||
i3.toCatalyst(path) | ||
}) | ||
} | ||
|
||
private[frameless] sealed trait RecordFieldEncoderLowPriority { | ||
implicit def apply[T](implicit e: TypedEncoder[T]): RecordFieldEncoder[T] = new RecordFieldEncoder[T](e) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsValueClass is required to disambiguate between Value Classes and Primitives. Being a subtype of AnyVal is not enough i.e.
implicitly[Double <:< AnyVal]
(that's a comment for myself).