-
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
Change TypeDataset#apply syntax to use a function #110
Changes from all commits
815b7a2
82f1ba7
fa0ce2f
6909cf5
693fa9c
72171d9
ff238a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package frameless.column | ||
|
||
import frameless.{TypedColumn, TypedEncoder, TypedExpressionEncoder} | ||
import shapeless.CaseClassMacros | ||
|
||
import scala.collection.immutable.Queue | ||
import scala.reflect.macros.whitebox | ||
|
||
class ColumnMacros(val c: whitebox.Context) extends CaseClassMacros { | ||
import c.universe._ | ||
|
||
// could be used to reintroduce apply('foo) | ||
// $COVERAGE-OFF$ Currently unused | ||
def fromSymbol[A : WeakTypeTag, B : WeakTypeTag](selector: c.Expr[scala.Symbol])(encoder: c.Expr[TypedEncoder[B]]): Tree = { | ||
val B = weakTypeOf[B].dealias | ||
val witness = c.typecheck(q"_root_.shapeless.Witness.apply(${selector.tree})") | ||
c.typecheck(q"${c.prefix}.col[$B]($witness)") | ||
} | ||
// $COVERAGE-ON$ | ||
|
||
def fromFunction[A : WeakTypeTag, B : WeakTypeTag](selector: c.Expr[A => B])(encoder: c.Expr[TypedEncoder[B]]): Tree = { | ||
def fail(tree: Tree) = { | ||
val err = | ||
s"Could not create a column identifier from $tree - try using _.a.b syntax" | ||
c.abort(tree.pos, err) | ||
} | ||
|
||
val A = weakTypeOf[A].dealias | ||
val B = weakTypeOf[B].dealias | ||
|
||
val selectorStr = selector.tree match { | ||
case NameExtractor(str) => str | ||
case Function(_, body) => fail(body) | ||
// $COVERAGE-OFF$ - cannot be reached as typechecking will fail in this case before macro is even invoked | ||
case other => fail(other) | ||
// $COVERAGE-ON$ | ||
} | ||
|
||
val typedCol = appliedType( | ||
weakTypeOf[TypedColumn[_, _]].typeConstructor, A, B | ||
) | ||
|
||
val TEEObj = reify(TypedExpressionEncoder) | ||
|
||
val datasetCol = c.typecheck( | ||
q"${c.prefix}.dataset.col($selectorStr).as[$B]($TEEObj.apply[$B]($encoder))" | ||
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 don't understand why you need an 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. Hmm.. it's to go from 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 see, if it's Spark's |
||
) | ||
|
||
c.typecheck(q"new $typedCol($datasetCol)") | ||
} | ||
|
||
case class NameExtractor(name: TermName) { Self => | ||
def unapply(tree: Tree): Option[Queue[String]] = { | ||
tree match { | ||
case Ident(`name`) => Some(Queue.empty) | ||
case s @ Select(Self(strs), nested) if s.symbol.isTerm && isCaseAccessorLike(s.symbol.asTerm) => | ||
Some(strs enqueue nested.toString) | ||
// $COVERAGE-OFF$ - Not sure if this case can ever come up and Encoder will still work | ||
case Apply(Self(strs), List()) => Some(strs) | ||
// $COVERAGE-ON$ | ||
case _ => None | ||
} | ||
} | ||
} | ||
|
||
object NameExtractor { | ||
def unapply(tree: Tree): Option[String] = tree match { | ||
case Function(List(ValDef(_, name, argTyp, _)), body) => NameExtractor(name).unapply(body).map(_.mkString(".")) | ||
case _ => None | ||
} | ||
} | ||
} |
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.
Why would we ever want to use this macro instead of the shapeless one?
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.
The reason I left this here was in case we wanted to support either
ds('a)
ords(_.a)
at the same time. We can't do that with overloading, because it will ruin type inference for the function syntax. So if we really wanted to allow both, I thought we could have the macro figure it all out instead.There are other problems with this, though - I would prefer to just embrace the function syntax because it has better type inference and about 95% smaller bytecode (after implicit expansion is all said and done).