@@ -1643,6 +1643,153 @@ async fn join_asof() -> Result<()> {
16431643 Ok ( ( ) )
16441644}
16451645
1646+ /// Regression test for <https://github.com/apache/datafusion/issues/14147>:
1647+ /// joining two unnamed DataFrames (both qualified `?table?`) whose columns
1648+ /// have the same names
1649+ #[ tokio:: test]
1650+ async fn join_unnamed_same_column_names ( ) -> Result < ( ) > {
1651+ let ctx = SessionContext :: new ( ) ;
1652+ let left = ctx. read_batch ( record_batch ! (
1653+ ( "id1" , Int32 , [ 1 , 2 , 3 ] ) ,
1654+ ( "val1" , Utf8 , [ "a" , "b" , "c" ] )
1655+ ) ?) ?;
1656+ let right = ctx. read_batch ( record_batch ! (
1657+ ( "id1" , Int32 , [ 2 , 3 , 4 ] ) ,
1658+ ( "val1" , Utf8 , [ "b" , "c" , "d" ] )
1659+ ) ?) ?;
1660+
1661+ // a self join: the builder renumbers the right side
1662+ let self_joined =
1663+ left. clone ( )
1664+ . join ( left. clone ( ) , JoinType :: Inner , & [ "id1" ] , & [ "id1" ] , None ) ?;
1665+ assert_eq ! ( self_joined. count( ) . await ?, 3 ) ;
1666+
1667+ let joined =
1668+ left. clone ( )
1669+ . join ( right. clone ( ) , JoinType :: Inner , & [ "id1" ] , & [ "id1" ] , None ) ?;
1670+ let results = joined. clone ( ) . collect ( ) . await ?;
1671+ assert_snapshot ! (
1672+ batches_to_sort_string( & results) ,
1673+ @r"
1674+ +-----+------+-----+------+
1675+ | id1 | val1 | id1 | val1 |
1676+ +-----+------+-----+------+
1677+ | 2 | b | 2 | b |
1678+ | 3 | c | 3 | c |
1679+ +-----+------+-----+------+
1680+ "
1681+ ) ;
1682+
1683+ // `id1` and `?table?.id1` name a column of each side: ambiguous, as in
1684+ // Postgres
1685+ let err = joined. clone ( ) . select ( vec ! [ col( "id1" ) ] ) . unwrap_err ( ) ;
1686+ assert_contains ! (
1687+ err. to_string( ) ,
1688+ "Ambiguous reference to unqualified field id1"
1689+ ) ;
1690+ let qualified = datafusion_common:: Column :: new ( Some ( "?table?" ) , "id1" ) ;
1691+ let err = joined
1692+ . clone ( )
1693+ . select ( vec ! [ Expr :: Column ( qualified. clone( ) ) ] )
1694+ . unwrap_err ( ) ;
1695+ assert_contains ! ( err. to_string( ) , "would be ambiguous" ) ;
1696+ // in a join condition, the qualified name matches a column of each side
1697+ let err = left
1698+ . join_on ( right, JoinType :: Inner , [ Expr :: Column ( qualified) . eq ( lit ( 2 ) ) ] )
1699+ . unwrap_err ( ) ;
1700+ assert_contains ! ( err. to_string( ) , "would be ambiguous" ) ;
1701+
1702+ // the columns of the schema read each side by position
1703+ let columns = joined. schema ( ) . columns ( ) ;
1704+ let results = joined
1705+ . select ( vec ! [
1706+ Expr :: Column ( columns[ 1 ] . clone( ) ) ,
1707+ Expr :: Column ( columns[ 3 ] . clone( ) ) ,
1708+ ] ) ?
1709+ . collect ( )
1710+ . await ?;
1711+ assert_snapshot ! (
1712+ batches_to_sort_string( & results) ,
1713+ @r"
1714+ +------+------+
1715+ | val1 | val1 |
1716+ +------+------+
1717+ | b | b |
1718+ | c | c |
1719+ +------+------+
1720+ "
1721+ ) ;
1722+ Ok ( ( ) )
1723+ }
1724+
1725+ /// The DataFrame methods that rebuild every column of a DataFrame keep
1726+ /// working when columns have the same qualifier and name (the join of
1727+ /// <https://github.com/apache/datafusion/issues/14147>)
1728+ #[ tokio:: test]
1729+ async fn dataframe_methods_with_repeated_column_names ( ) -> Result < ( ) > {
1730+ let ctx = SessionContext :: new ( ) ;
1731+ let left = ctx. read_batch ( record_batch ! (
1732+ ( "id1" , Int32 , [ 1 , 2 , 3 ] ) ,
1733+ ( "val1" , Utf8 , [ Some ( "a" ) , None , Some ( "c" ) ] )
1734+ ) ?) ?;
1735+ let right = ctx. read_batch ( record_batch ! (
1736+ ( "id1" , Int32 , [ 2 , 3 , 4 ] ) ,
1737+ ( "val1" , Utf8 , [ Some ( "b" ) , None , Some ( "d" ) ] )
1738+ ) ?) ?;
1739+ let joined = left. join ( right, JoinType :: Inner , & [ "id1" ] , & [ "id1" ] , None ) ?;
1740+
1741+ let df = joined
1742+ . clone ( )
1743+ . with_column ( "one" , lit ( 1 ) ) ?
1744+ . with_column_renamed ( "one" , "uno" ) ?
1745+ . fill_null ( & ScalarValue :: from ( "-" ) , & [ ] ) ?;
1746+ assert_snapshot ! (
1747+ batches_to_sort_string( & df. collect( ) . await ?) ,
1748+ @r"
1749+ +-----+------+-----+------+-----+
1750+ | id1 | val1 | id1 | val1 | uno |
1751+ +-----+------+-----+------+-----+
1752+ | 2 | - | 2 | b | 1 |
1753+ | 3 | c | 3 | - | 1 |
1754+ +-----+------+-----+------+-----+
1755+ "
1756+ ) ;
1757+
1758+ let df = joined
1759+ . clone ( )
1760+ . with_column ( "l" , make_array ( vec ! [ lit( 1 ) , lit( 2 ) ] ) ) ?
1761+ . unnest_columns ( & [ "l" ] ) ?
1762+ . drop_columns ( & [ "val1" ] ) ?;
1763+ assert_snapshot ! (
1764+ batches_to_sort_string( & df. collect( ) . await ?) ,
1765+ @r"
1766+ +-----+-----+---+
1767+ | id1 | id1 | l |
1768+ +-----+-----+---+
1769+ | 2 | 2 | 1 |
1770+ | 2 | 2 | 2 |
1771+ | 3 | 3 | 1 |
1772+ | 3 | 3 | 2 |
1773+ +-----+-----+---+
1774+ "
1775+ ) ;
1776+
1777+ // an unqualified name drops the column of each side
1778+ let df = joined. drop_columns ( & [ "val1" ] ) ?;
1779+ assert_snapshot ! (
1780+ batches_to_sort_string( & df. collect( ) . await ?) ,
1781+ @r"
1782+ +-----+-----+
1783+ | id1 | id1 |
1784+ +-----+-----+
1785+ | 2 | 2 |
1786+ | 3 | 3 |
1787+ +-----+-----+
1788+ "
1789+ ) ;
1790+ Ok ( ( ) )
1791+ }
1792+
16461793#[ tokio:: test]
16471794async fn join_coercion_unnamed ( ) -> Result < ( ) > {
16481795 let ctx = SessionContext :: new ( ) ;
0 commit comments