Skip to content

Commit

Permalink
Implement TypeTag enum deriver
Browse files Browse the repository at this point in the history
  • Loading branch information
grouzen committed Jan 21, 2024
1 parent 4d5fab6 commit 363a334
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.mnedokushev.zio.apache.parquet.core.filter

final case class FilterError(
message: String,
cause: Option[Throwable] = None
) extends IllegalArgumentException(message, cause.getOrElse(new Throwable()))
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ object OperatorSupport {
}

object EqNotEq {
implicit def enum0[A: TypeTag]: EqNotEq[A] = new EqNotEq[A] {}

implicit case object string extends EqNotEq[String]
implicit case object boolean extends EqNotEq[Boolean]
implicit case object byte extends EqNotEq[Byte]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ object TypeTag {

}

implicit def enum0[A](casesMap: Map[A, String]): TypeTag.EqNotEq[A] =
eqnoteq[A, Binary, BinaryColumn](
FilterApi.binaryColumn,
v => Value.string(casesMap.getOrElse(v, throw FilterError(s"Failed to encode enum for value $v"))).value
)

implicit val string: TypeTag.EqNotEq[String] =
eqnoteq[String, Binary, BinaryColumn](
FilterApi.binaryColumn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ object TypeTagDeriver {
`enum`: Schema.Enum[A],
cases: => Chunk[Deriver.WrappedF[TypeTag, _]],
summoned: => Option[TypeTag[A]]
): TypeTag[A] =
TypeTag.dummy[A]
): TypeTag[A] = {
val casesMap = `enum`.cases.map { case0 =>
case0.schema.asInstanceOf[Schema.CaseClass0[A]].defaultConstruct() -> case0.id
}.toMap

TypeTag.enum0(casesMap)
}

override def derivePrimitive[A](
st: StandardType[A],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.UUID

object Fixtures {

case class MyRecord(a: String, b: Int, child: MyRecord.Child)
case class MyRecord(a: String, b: Int, child: MyRecord.Child, enm: MyRecord.Enum)

object MyRecord {
implicit val schema =
Expand All @@ -28,6 +28,18 @@ object Fixtures {
Derive.derive[TypeTag, Child](TypeTagDeriver.default)
}

sealed trait Enum
object Enum {
case object Started extends Enum
case object InProgress extends Enum
case object Done extends Enum

implicit val schema: Schema[Enum] =
DeriveSchema.gen[Enum]
implicit val typeTag: TypeTag[Enum] =
Derive.derive[TypeTag, Enum](TypeTagDeriver.default)
}

}

case class MyRecordSummoned(a: Int, b: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import java.util.UUID

object Fixtures {

case class MyRecord(a: String, b: Int, child: MyRecord.Child)
case class MyRecord(a: String, b: Int, child: MyRecord.Child, enm: MyRecord.Enum)

object MyRecord {
implicit val schema: Schema.CaseClass3.WithFields["a", "b", "child", String, Int, MyRecord.Child, MyRecord] =
implicit val schema
: Schema.CaseClass4.WithFields["a", "b", "child", "enm", String, Int, MyRecord.Child, MyRecord.Enum, MyRecord] =
DeriveSchema.gen[MyRecord]
implicit val typeTag: TypeTag[MyRecord] =
implicit val typeTag: TypeTag[MyRecord] =
Derive.derive[TypeTag, MyRecord](TypeTagDeriver.default)

case class Child(c: Int, d: Option[Long])
Expand All @@ -26,7 +27,18 @@ object Fixtures {
DeriveSchema.gen[Child]
implicit val typeTag: TypeTag[Child] =
Derive.derive[TypeTag, Child](TypeTagDeriver.default)
}

sealed trait Enum
object Enum {
case object Started extends Enum
case object InProgress extends Enum
case object Done extends Enum

implicit val schema: Schema[Enum] =
DeriveSchema.gen[Enum]
implicit val typeTag: TypeTag[Enum] =
Derive.derive[TypeTag, Enum](TypeTagDeriver.default)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object ExprSpec extends ZIOSpecDefault {
override def spec: Spec[TestEnvironment with Scope, Any] =
suite("ExprSpec")(
test("compile all operators") {
val (a, b, _) = Filter.columns[MyRecord]
val (a, b, _, _) = Filter.columns[MyRecord]

val result = Expr.compile(
Filter.not(
Expand Down Expand Up @@ -77,7 +77,7 @@ object ExprSpec extends ZIOSpecDefault {

assert(result)(isRight(equalTo(expected)))
},
test("type tag compilation") {
test("compile all primitive types") {
val (
string,
boolean,
Expand Down Expand Up @@ -307,9 +307,17 @@ object ExprSpec extends ZIOSpecDefault {
assert(result1)(isRight(equalTo(expected1))) &&
assert(result2)(isRight(equalTo(expected2)))
},
test("compile enum") {
val (_, _, _, enm) = Filter.columns[MyRecord]

val result = Expr.compile(enm === MyRecord.Enum.Done)
val expected = FilterApi.eq(FilterApi.binaryColumn("enm"), Value.string("Done").value)

assert(result)(isRight(equalTo(expected)))
},
test("column path concatenation") {
val (a, b, child) = Filter.columns[MyRecord]
val (c, d) = Filter.columns[MyRecord.Child]
val (a, b, child, _) = Filter.columns[MyRecord]
val (c, d) = Filter.columns[MyRecord.Child]

assert(a.path)(equalTo("a")) &&
assert(b.path)(equalTo("b")) &&
Expand Down

0 comments on commit 363a334

Please sign in to comment.