rdblue commented on a change in pull request #24812: [SPARK-27965][SQL] Add 
extractors for v2 catalog transforms.
URL: https://github.com/apache/spark/pull/24812#discussion_r291267229
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalog/v2/expressions/expressions.scala
 ##########
 @@ -111,32 +122,104 @@ private[sql] final case class ApplyTransform(
   override def toString: String = describe
 }
 
+/**
+ * Convenience extractor for any Literal.
+ */
+private object Lit {
+  def unapply[T](literal: Literal[T]): Some[(T, DataType)] = {
+    Some((literal.value, literal.dataType))
+  }
+}
+
+/**
+ * Convenience extractor for any NamedReference.
+ */
+private object Ref {
+  def unapply(named: NamedReference): Some[Seq[String]] = {
+    Some(named.fieldNames)
+  }
+}
+
+/**
+ * Convenience extractor for any Transform.
+ */
+private object NamedTransform {
+  def unapply(transform: Transform): Some[(String, Seq[Expression])] = {
+    Some((transform.name, transform.arguments))
+  }
+}
+
 private[sql] final case class IdentityTransform(
     ref: NamedReference) extends SingleColumnTransform(ref) {
   override val name: String = "identity"
   override def describe: String = ref.describe
 }
 
+private[sql] object IdentityTransform {
+  def unapply(transform: Transform): Option[FieldReference] = transform match {
+    case NamedTransform("identity", Seq(Ref(parts))) =>
+      Some(FieldReference(parts))
+    case _ =>
+      None
+  }
+}
 
 Review comment:
   Yes, we need them.
   
   The idea is to make any transform that is equivalent to an 
`IdentityTransform(...)` instance of Spark's case class work in a `match` 
expression as though it were actually an `IdentityTransform` instance. That 
way, Spark can internally use these case classes, even though users may pass 
instances that are unknown classes. This will reduce future bugs caused by 
matching `IdentityTransform` instead of remembering to match the more general 
`NamedTransform("identity", ...)`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to