fqaiser94 commented on a change in pull request #27066:
URL: https://github.com/apache/spark/pull/27066#discussion_r448427144
##########
File path: sql/core/src/main/scala/org/apache/spark/sql/Column.scala
##########
@@ -871,6 +871,63 @@ class Column(val expr: Expression) extends Logging {
*/
def getItem(key: Any): Column = withExpr { UnresolvedExtractValue(expr,
Literal(key)) }
+ /**
+ * An expression that adds/replaces field in `StructType` by name.
+ *
+ * {{{
+ * val df = sql("SELECT named_struct('a', 1, 'b', 2) struct_col")
+ * df.select($"struct_col".withField("c", lit(3)))
+ * // result: {"a":1,"b":2,"c":3}
+ *
+ * val df = sql("SELECT named_struct('a', 1, 'b', 2) struct_col")
+ * df.select($"struct_col".withField("b", lit(3)))
+ * // result: {"a":1,"b":3}
+ *
+ * val df = sql("SELECT CAST(NULL AS struct<a:int,b:int>) struct_col")
+ * df.select($"struct_col".withField("c", lit(3)))
+ * // result: null of type struct<a:int,b:int,c:int>
+ *
+ * val df = sql("SELECT named_struct('a', 1, 'b', 2, 'b', 3) struct_col")
+ * df.select($"struct_col".withField("b", lit(100)))
+ * // result: {"a":1,"b":100,"b":100}
+ *
+ * val df = sql("SELECT named_struct('a', named_struct('a', 1, 'b', 2))
struct_col")
+ * df.select($"struct_col".withField("a.c", lit(3)))
+ * // result: {"a":{"a":1,"b":2,"c":3}}
+ * }}}
+ *
+ * @group expr_ops
+ * @since 3.1.0
+ */
+ def withField(fieldName: String, col: Column): Column = withExpr {
+ val nameParts = if (fieldName.isEmpty) {
+ fieldName :: Nil
Review comment:
I consider this to be resolved given the results of the discussion
[here](https://github.com/apache/spark/pull/27066#discussion_r448416127).
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]