jonahgao commented on code in PR #14127:
URL: https://github.com/apache/datafusion/pull/14127#discussion_r1915864922


##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -2646,3 +2646,135 @@ async fn boolean_dictionary_as_filter() {
 
     assert_batches_eq!(expected, &result_df.collect().await.unwrap());
 }
+
+#[tokio::test]
+async fn test_alias() -> Result<()> {
+    let df = create_test_table("test")
+        .await?
+        .select(vec![col("a"), col("test.b"), lit(1).alias("one")])?
+        .alias("table_alias")?;
+    // All ouput column qualifiers are changed to "table_alias"
+    df.schema().columns().iter().for_each(|c| {
+        assert_eq!(c.relation, Some("table_alias".into()));
+    });
+    let expected = "SubqueryAlias: table_alias [a:Utf8, b:Int32, one:Int32]\
+     \n  Projection: test.a, test.b, Int32(1) AS one [a:Utf8, b:Int32, 
one:Int32]\
+     \n    TableScan: test [a:Utf8, b:Int32]";
+    let plan = df
+        .clone()
+        .into_unoptimized_plan()
+        .display_indent_schema()
+        .to_string();
+    assert_eq!(plan, expected);
+
+    // Select over the aliased DataFrame
+    let df = df.select(vec![
+        col("table_alias.a"),
+        col("b") + col("table_alias.one"),
+    ])?;
+    let expected = [
+        "+-----------+---------------------------------+",
+        "| a         | table_alias.b + table_alias.one |",
+        "+-----------+---------------------------------+",
+        "| abcDEF    | 2                               |",
+        "| abc123    | 11                              |",
+        "| CBAdef    | 11                              |",
+        "| 123AbcDef | 101                             |",
+        "+-----------+---------------------------------+",
+    ];
+    assert_batches_sorted_eq!(expected, &df.collect().await?);
+    Ok(())
+}
+
+// Use alias to perform a self-join
+// Issue: https://github.com/apache/datafusion/issues/14112
+#[tokio::test]
+async fn test_alias_self_join() -> Result<()> {
+    let left = create_test_table("t1").await?;
+    let right = left.clone().alias("t2")?;
+    let joined = left.join(right, JoinType::Full, &["a"], &["a"], None)?;
+    let expected = [
+        "+-----------+-----+-----------+-----+",
+        "| a         | b   | a         | b   |",
+        "+-----------+-----+-----------+-----+",
+        "| abcDEF    | 1   | abcDEF    | 1   |",
+        "| abc123    | 10  | abc123    | 10  |",
+        "| CBAdef    | 10  | CBAdef    | 10  |",
+        "| 123AbcDef | 100 | 123AbcDef | 100 |",
+        "+-----------+-----+-----------+-----+",
+    ];
+    assert_batches_sorted_eq!(expected, &joined.collect().await?);
+    Ok(())
+}
+
+#[tokio::test]
+async fn test_alias_empty() -> Result<()> {
+    let df = create_test_table("test").await?.alias("")?;
+    let expected = "SubqueryAlias:  [a:Utf8, b:Int32]\

Review Comment:
   We allow creating `TableReference` from empty str.



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