LiaCastaneda commented on code in PR #27099:
URL: https://github.com/apache/datafusion/pull/27099#discussion_r4617431110


##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -2755,6 +2756,140 @@ async fn 
roundtrip_lambda_parameter_struct_field_names() -> Result<()> {
     Ok(())
 }
 
+/// Round trips `sql` and checks that the output type and rows don't change. 
Substrait
+/// literals carry no struct field names, so a struct literal combined with a 
struct
+/// column must take the column's names for the two to have the same type.
+///
+/// `sql` can read two tables: `struct_rows`, whose column `st` is a 
`Struct{a, b}`, and
+/// `zones`, whose column `z` is a `List<Struct{subnet: Struct{id}, 
zone_name}>`. Plans
+/// are not compared as text: for a set operation, the consumer puts the 
output alias
+/// above the union rather than in each input.
+async fn assert_struct_literal_roundtrips(sql: &str) -> Result<()> {
+    let ctx = higher_order_function_ctx().await?;
+    for table in [
+        "CREATE TABLE struct_rows AS SELECT named_struct('a', 1, 'b', 'x') AS 
st \
+         UNION ALL SELECT NULL AS st",
+        "CREATE TABLE zones AS SELECT [named_struct('subnet', 
named_struct('id', 'subnet-1'), \
+         'zone_name', 'zone-a')] AS z UNION ALL SELECT NULL AS z",
+    ] {
+        ctx.sql(table).await?.collect().await?;
+    }
+
+    let plan = ctx.sql(sql).await?.into_optimized_plan()?;
+    let expected = DataFrame::new(ctx.state(), plan.clone()).collect().await?;
+    let plan2 = substrait_roundtrip(&plan, &ctx).await?;
+    assert_eq!(
+        plan.schema().field(0).data_type(),
+        plan2.schema().field(0).data_type()
+    );
+    let actual = DataFrame::new(ctx.state(), plan2).collect().await?;
+    assert_eq!(
+        batches_to_sort_string(&expected),
+        batches_to_sort_string(&actual)
+    );
+    Ok(())
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_coalesce() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT coalesce(st, named_struct('a', 0, 'b', 'z')) FROM struct_rows",
+    )
+    .await
+}
+
+// The lambda parameter is typed from the `coalesce` result, so it only finds 
`subnet`
+// and `id` if the empty list took the column's field names first.
+#[tokio::test]
+async fn roundtrip_struct_literal_next_to_nested_struct_column() -> Result<()> 
{
+    assert_struct_literal_roundtrips(
+        "SELECT array_transform(coalesce(z, []), p0 -> get_field(get_field(p0, 
'subnet'), 'id')) \
+         FROM zones",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_case_else() -> Result<()> {
+    roundtrip(
+        "SELECT (CASE WHEN column1 IS NOT NULL THEN named_struct('f1', 
column1) ELSE NULL END) AS res \
+         FROM (VALUES ('a'), (NULL))",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_nested_case_after_column() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT CASE WHEN st IS NOT NULL THEN st \
+         ELSE CASE WHEN random() > 2 THEN named_struct('a', 0, 'b', 'z') \
+         ELSE named_struct('a', 9, 'b', 'w') END END FROM struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_nested_case_before_column() -> Result<()> 
{
+    assert_struct_literal_roundtrips(
+        "SELECT CASE WHEN st IS NULL \
+         THEN CASE WHEN random() > 2 THEN named_struct('a', 0, 'b', 'z') \
+         ELSE named_struct('a', 9, 'b', 'w') END ELSE st END FROM struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_list() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT st IN (named_struct('a', 1, 'b', 'x'), named_struct('a', 2, 
'b', 'y')) \
+         FROM struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_window_function_argument() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT lag(st, 1, named_struct('a', 0, 'b', 'z')) OVER () FROM 
struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_union_with_null_first_input() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT NULL AS r FROM struct_rows UNION ALL SELECT st AS r FROM 
struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_union_with_null_second_input() -> Result<()> 
{
+    assert_struct_literal_roundtrips(
+        "SELECT st AS r FROM struct_rows UNION ALL SELECT NULL AS r FROM 
struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_union_with_limit() -> Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT * FROM (SELECT NULL AS r FROM struct_rows LIMIT 1) \
+         UNION ALL SELECT st AS r FROM struct_rows",
+    )
+    .await
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_union_with_nested_union_input() -> 
Result<()> {
+    assert_struct_literal_roundtrips(
+        "SELECT * FROM (SELECT named_struct('a', 0, 'b', 'z') AS r FROM 
struct_rows \
+         UNION SELECT named_struct('a', 9, 'b', 'w') AS r FROM struct_rows) \
+         UNION ALL SELECT st AS r FROM struct_rows",
+    )
+    .await
+}
+

Review Comment:
   added



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to