Skip to content

Commit

Permalink
Add UnaryDeserializer Either derivation support (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Apr 10, 2022
1 parent 7a66401 commit 9f5c82d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import cats.Id
import cats.syntax.apply._
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.util.ArrayData
import shapeless.ops.coproduct.{CoproductToEither, EitherToCoproduct}
import shapeless.{:+:, CNil, Coproduct, HNil, Inl, Inr, IsTuple}

import scala.reflect.ClassTag
Expand Down Expand Up @@ -175,8 +176,15 @@ object UnaryDeserializer extends Serializable {
Try(arrayDataUnaryDeserializer.deserialize(arguments, inspectors).toArray[T](HSerializer[T].dataType))
.getOrElse(nativeArrayUnaryDeserializer.deserialize(arguments, inspectors))

/** Coproduct deserializer. */
implicit def unaryDeserializerCCons[H, T <: Coproduct](implicit
/** Coproduct deserializers. */
implicit def eitherUnaryDeserializer[L, R, P <: Coproduct](implicit
etp: EitherToCoproduct.Aux[L, R, P],
dp: UnaryDeserializer[Id, P],
pte: CoproductToEither.Aux[P, Either[L, R]]
): UnaryDeserializer[Id, Either[L, R]] =
(arguments, inspectors) => pte(dp.deserialize(arguments, inspectors))

implicit def cconsUnaryDeserializer[H, T <: Coproduct](implicit
dh: UnaryDeserializer[Id, H],
dt: UnaryDeserializer[Id, T]
): UnaryDeserializer[Id, H :+: T] =
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/com/azavea/hiveless/utils/HShow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.azavea.hiveless.utils

import shapeless.ops.coproduct.EitherToCoproduct
import shapeless.{:+:, ::, CNil, Coproduct, Generic, HList, HNil, IsTuple}

import scala.reflect.{classTag, ClassTag}
Expand All @@ -32,6 +33,10 @@ object HShow extends LowPriorityHShow {
implicit def hshowGeneric[T: IsTuple, L <: HList](implicit gen: Generic.Aux[T, L], sl: HShow[L]): HShow[T] =
() => sl.show()

/** Derive HShow for Either. */
implicit def hshowEither[L, R, P <: Coproduct](implicit etp: EitherToCoproduct.Aux[L, R, P], sp: HShow[P]): HShow[Either[L, R]] =
() => sp.show()

/** Derive HShow for HList. */
implicit val hshowHNil: HShow[HNil] = () => ""
implicit def hshowHCons[H: ClassTag, T <: HList](implicit sh: HShow[H], st: HShow[T]): HShow[H :: T] = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ST_Intersects extends HUDF[(ST_Intersects.Arg, ST_Intersects.Arg), Boolean
object ST_Intersects {
import UnaryDeserializer.Errors.ProductDeserializationError

// We could use Either[Extent, Geometry], but Either has no safe fall back CNil
// which may lead to derivation error messages rather than parsing
type Arg = Extent :+: Geometry :+: CNil

def parseGeometry(a: Arg): Option[Geometry] = a.select[Geometry].orElse(a.select[Extent].map(_.toPolygon()))
Expand Down

0 comments on commit 9f5c82d

Please sign in to comment.