You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Pablo Langa Blanco (Jira)" <ji...@apache.org> on 2023/02/28 22:14:00 UTC

[jira] [Commented] (SPARK-40679) add named_struct to spark scala api for parity to spark sql

    [ https://issues.apache.org/jira/browse/SPARK-40679?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17694757#comment-17694757 ] 

Pablo Langa Blanco commented on SPARK-40679:
--------------------------------------------

I think the original development supports the creation of NamedStructs in the dataframe interface.

[https://github.com/apache/spark/pull/6874/files]

 
{code:java}
/**
   * Creates a new struct column.
   * If the input column is a column in a [[DataFrame]], or a derived column expression
   * that is named (i.e. aliased), its name would be remained as the StructField's name,
   * otherwise, the newly generated StructField's name would be auto generated as col${index + 1},
   * i.e. col1, col2, col3, ...
*/
def struct(cols: Column*): Column = {
...{code}
 
{code:java}
test("struct with column expression to be automatically named") {
    val df = Seq((1, "str")).toDF("a", "b")
    val result = df.select(struct((col("a") * 2), col("b")))
    val expectedType = StructType(Seq(
      StructField("col1", IntegerType, nullable = false),
      StructField("b", StringType)
    ))
    assert(result.first.schema(0).dataType === expectedType)
    checkAnswer(result, Row(Row(2, "str")))
  } 
{code}
 

 

 

> add named_struct to spark scala api for parity to spark sql
> -----------------------------------------------------------
>
>                 Key: SPARK-40679
>                 URL: https://issues.apache.org/jira/browse/SPARK-40679
>             Project: Spark
>          Issue Type: Improvement
>          Components: Spark Core, SQL
>    Affects Versions: 3.3.0
>            Reporter: Douglas Moore
>            Priority: Major
>
> To facilitate migration from cast(struct(.... to `named_struct` where comparison of structures is needed.
> To maintain parity between APIs. Understand that using `expr` would work, but it wouldn't be typed.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org