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


##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -2755,6 +2755,466 @@ async fn 
roundtrip_lambda_parameter_struct_field_names() -> Result<()> {
     Ok(())
 }
 
+// Substrait literals carry no struct field names, so a struct literal 
combined with a
+// struct column must take the column's field names for the two to have the 
same type.
+
+/// A context with a table `structs` whose column `s` is a list of structs.
+async fn struct_lists_ctx() -> Result<SessionContext> {
+    let ctx = create_context().await?;
+    ctx.sql(
+        "CREATE TABLE structs AS SELECT [named_struct('a', 1, 'b', 'x')] AS s \
+         UNION ALL SELECT NULL AS s",
+    )
+    .await?
+    .collect()
+    .await?;
+    Ok(ctx)
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_in_coalesce() -> Result<()> {
+    // coalesce is simplified to a CASE whose ELSE branch is the literal
+    roundtrip_with_ctx(
+        "SELECT coalesce(s, [named_struct('a', 0, 'b', 'z')]) FROM structs",
+        struct_lists_ctx().await?,
+    )
+    .await?;
+    Ok(())
+}
+
+/// A NULL struct in the ELSE branch, from 
https://github.com/apache/datafusion/issues/14632
+#[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
+}
+
+/// Checks a set operation with a NULL struct in one input. The consumer 
rebuilds a union
+/// with the alias above it rather than in each input, so the plans can't be 
compared as
+/// text: this checks the output type and that the plan runs instead.
+async fn assert_union_with_null_struct_roundtrips(sql: &str) -> Result<()> {
+    let ctx = struct_rows_ctx().await?;
+    let plan = ctx.sql(sql).await?.into_optimized_plan()?;
+    let plan2 = substrait_roundtrip(&plan, &ctx).await?;
+    assert_eq!(
+        plan.schema().field(0).data_type(),
+        plan2.schema().field(0).data_type()
+    );
+    DataFrame::new(ctx.state(), plan2).collect().await?;
+    Ok(())
+}
+
+#[tokio::test]
+async fn roundtrip_struct_literal_union_with_null_second_input() -> Result<()> 
{
+    assert_union_with_null_struct_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_null_first_input() -> Result<()> {
+    assert_union_with_null_struct_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_in_list() -> Result<()> {
+    roundtrip_with_ctx(
+        "SELECT st IN (named_struct('a', 1, 'b', 'x'), named_struct('a', 2, 
'b', 'y')) \
+         FROM struct_rows",
+        struct_rows_ctx().await?,
+    )
+    .await?;
+    Ok(())
+}
+
+// Keep these plans unoptimized before serialization so the consumer sees the
+// literal and relation shapes emitted by another Substrait producer.
+#[tokio::test]
+async fn roundtrip_struct_literal_dictionary_values() -> Result<()> {

Review Comment:
   you're right, I kept adding tests as I found new gaps in the fix, and didn't 
review them as carefully as the fix itself. Will rework the tests so they are 
simpler/more readable



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