val keysDF = mapTypeDF.select(explode(map_keys($"property"))).distinct()
val keys = keysDF.collect().map(f=>f.get(0))
val keyCols = keys.map(f=> col("property").getItem(f).as(f.toString))
val expandedDf = mapTypeDF.select(col("name") +: keyCols:_*).show(false)
val classes = Seq(
getClass, // To get the jar with our own code.
classOf[mysql.jdbc.Driver] // To get the connector.
)
val jars = classes.map(_.getProtectionDomain().getCodeSource().getLocation().getPath())
val conf = new SparkConf().setJars(jars)
class Contains(r: Range) { def unapply(i: Int): Boolean = r contains i }
val C1 = new Contains(3 to 10)
val C2 = new Contains(20 to 30)
scala> 5 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
C1
scala> 23 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
C2
scala> 45 match { case C1() => println("C1"); case C2() => println("C2"); case _ => println("none") }
none
import scala.compiletime.{error, requireConst}
opaque type NonEmptyString = String
object NonEmptyString:
def apply(s: String): Option[NonEmptyString] =
if s.isEmpty then None else Some(s)
inline def from(inline s: String): NonEmptyString =
requireConst(s)
inline if s == "" then error("got an empty string") else s
given Conversion[NonEmptyString, String] with
inline def apply(nes: NonEmptyString): String = nes
object LowerCased:
private def fromImpl(s: Expr[String])(using Quotes): Expr[LowerCased] =
import quotes.reflect.*
s.asTerm match
case Inlined(_, _, Literal(StringConstant(str))) =>
if LowerCased(str) == str then s
else report.errorAndAbort(
s"got a string which is not all lower case: $str")
case _ => report.errorAndAbort(
s"got a value which is not a constant string: ${s.show}")
// compilation succeeds
val p1: LowerCased = LowerCased("Abc")
// compilation succeeds
LowerCased.from("abc")
// compilation fails:
// got a string which is not all lower case: Abc
LowerCased.from("Abc")
// compilation fails:
// got a value which is not a constant string: Usage.z
val z = "x" * 10
LowerCased.from(z)
Comments