@@ -25,6 +25,11 @@ import org.apache.spark.sql.catalyst.analysis._
2525import  org .apache .spark .sql .catalyst .expressions .codegen .{GeneratedExpressionCode , CodeGenContext }
2626import  org .apache .spark .sql .types ._ 
2727
28+ // //////////////////////////////////////////////////////////////////////////////////////////////////
29+ //  This file defines all the expressions to extract values out of complex types.
30+ //  For example, getting a field out of an array, map, or struct.
31+ // //////////////////////////////////////////////////////////////////////////////////////////////////
32+ 
2833
2934object  ExtractValue  {
3035  /**  
@@ -73,11 +78,10 @@ object ExtractValue {
7378    }
7479  }
7580
76-   def  unapply (g : ExtractValue ):  Option [(Expression , Expression )] =  {
77-     g match  {
78-       case  o : ExtractValueWithOrdinal  =>  Some ((o.child, o.ordinal))
79-       case  s : ExtractValueWithStruct  =>  Some ((s.child, null ))
80-     }
81+   def  unapply (g : ExtractValue ):  Option [(Expression , Expression )] =  g match  {
82+     case  o : GetArrayItem  =>  Some ((o.child, o.ordinal))
83+     case  o : GetMapValue  =>  Some ((o.child, o.key))
84+     case  s : ExtractValueWithStruct  =>  Some ((s.child, null ))
8185  }
8286
8387  /**  
@@ -117,6 +121,8 @@ abstract class ExtractValueWithStruct extends UnaryExpression with ExtractValue
117121
118122/** 
119123 * Returns the value of fields in the Struct `child`. 
124+  * 
125+  * No need to do type checking since it is handled by [[ExtractValue ]]. 
120126 */  
121127case  class  GetStructField (child : Expression , field : StructField , ordinal : Int )
122128  extends  ExtractValueWithStruct  {
@@ -142,6 +148,8 @@ case class GetStructField(child: Expression, field: StructField, ordinal: Int)
142148
143149/** 
144150 * Returns the array of value of fields in the Array of Struct `child`. 
151+  * 
152+  * No need to do type checking since it is handled by [[ExtractValue ]]. 
145153 */  
146154case  class  GetArrayStructFields (
147155    child : Expression ,
@@ -178,25 +186,21 @@ case class GetArrayStructFields(
178186  }
179187}
180188
181- abstract  class  ExtractValueWithOrdinal  extends  BinaryExpression  with  ExtractValue  {
182-   self : Product  => 
189+ /** 
190+  * Returns the field at `ordinal` in the Array `child`. 
191+  * 
192+  * No need to do type checking since it is handled by [[ExtractValue ]]. 
193+  */  
194+ case  class  GetArrayItem (child : Expression , ordinal : Expression )
195+   extends  BinaryExpression  with  ExtractValue  {
183196
184-   def  ordinal :  Expression 
185-   def  child :  Expression 
197+   override  def  toString :  String  =  s " $child[ $ordinal] " 
186198
187199  override  def  left :  Expression  =  child
188200  override  def  right :  Expression  =  ordinal
189201
190202  /**  `Null` is returned for invalid ordinals. */  
191203  override  def  nullable :  Boolean  =  true 
192-   override  def  toString :  String  =  s " $child[ $ordinal] " 
193- }
194- 
195- /** 
196-  * Returns the field at `ordinal` in the Array `child` 
197-  */  
198- case  class  GetArrayItem (child : Expression , ordinal : Expression )
199-   extends  ExtractValueWithOrdinal  {
200204
201205  override  def  dataType :  DataType  =  child.dataType.asInstanceOf [ArrayType ].elementType
202206
@@ -227,10 +231,20 @@ case class GetArrayItem(child: Expression, ordinal: Expression)
227231}
228232
229233/** 
230-  * Returns the value of key `ordinal` in Map `child` 
234+  * Returns the value of key `ordinal` in Map `child`. 
235+  * 
236+  * No need to do type checking since it is handled by [[ExtractValue ]]. 
231237 */  
232- case  class  GetMapValue (child : Expression , ordinal : Expression )
233-   extends  ExtractValueWithOrdinal  {
238+ case  class  GetMapValue (child : Expression , key : Expression )
239+   extends  BinaryExpression  with  ExtractValue  {
240+ 
241+   override  def  toString :  String  =  s " $child[ $key] " 
242+ 
243+   override  def  left :  Expression  =  child
244+   override  def  right :  Expression  =  key
245+ 
246+   /**  `Null` is returned for invalid ordinals. */  
247+   override  def  nullable :  Boolean  =  true 
234248
235249  override  def  dataType :  DataType  =  child.dataType.asInstanceOf [MapType ].valueType
236250
0 commit comments