fqaiser94 commented on issue #27066: [SPARK-31317][SQL] Add withField method to 
Column class
URL: https://github.com/apache/spark/pull/27066#issuecomment-612457788
 
 
   @dbtsai I've updated the implementation based on your suggestions.
   
   Please note that I went with Expected Result 1 for null structs. I agree 
that it makes life much easier from both implementation and optimization point 
of view. Also, if a user wants to get back null for null structs, they always 
have the option of wrapping their code inside of a CASE-WHEN expression 
themselves. 
   
   Here is the physical plan for the same query as before: 
   ```
   val df: DataFrame = spark.createDataFrame(
     sparkContext.parallelize(Row(Row(1, 2, 3)) :: Row(Row(2, 4, 5)) :: Nil),
     StructType(Seq(StructField("a",
       StructType(Seq(
         StructField("a", IntegerType),
         StructField("b", IntegerType),
         StructField("c", IntegerType)))))))
   
   val df1 = df.withColumn("new1", df("a").withField("abc", df("a.b")))
   val df2 = df1.withColumn("new2", df1("new1").withField("efg", df1("new1.c")))
   val df3 = df2.filter(df2("new2.efg") > 0)
   
   df3.explain(true)
   
   == Parsed Logical Plan ==
   Filter (new2#8.efg AS efg#12 > 0)
   +- Project [a#1, new1#4, add_fields(new1#4, efg, new1#4.c) AS new2#8]
      +- Project [a#1, add_fields(a#1, abc, a#1.b) AS new1#4]
         +- LogicalRDD [a#1], false
   
   == Analyzed Logical Plan ==
   a: struct<a:int,b:int,c:int>, new1: struct<a:int,b:int,c:int,abc:int>, new2: 
struct<a:int,b:int,c:int,abc:int,efg:int>
   Filter (new2#8.efg > 0)
   +- Project [a#1, new1#4, add_fields(new1#4, efg, new1#4.c) AS new2#8]
      +- Project [a#1, add_fields(a#1, abc, a#1.b) AS new1#4]
         +- LogicalRDD [a#1], false
   
   == Optimized Logical Plan ==
   Project [a#1, named_struct(a, a#1.a, b, a#1.b, c, a#1.c, abc, a#1.b) AS 
new1#4, named_struct(a, a#1.a, b, a#1.b, c, a#1.c, abc, a#1.b, efg, a#1.c) AS 
new2#8]
   +- Filter (isnotnull(a#1) AND (a#1.c > 0))
      +- LogicalRDD [a#1], false
   
   == Physical Plan ==
   *(1) Project [a#1, named_struct(a, a#1.a, b, a#1.b, c, a#1.c, abc, a#1.b) AS 
new1#4, named_struct(a, a#1.a, b, a#1.b, c, a#1.c, abc, a#1.b, efg, a#1.c) AS 
new2#8]
   +- *(1) Filter (isnotnull(a#1) AND (a#1.c > 0))
      +- *(1) Scan ExistingRDD[a#1]
   ```
   

----------------------------------------------------------------
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