@@ -4136,6 +4136,11 @@ mod tests {
41364136 let df = ctx. sql ( "SELECT * FROM data" ) . await ?;
41374137 let results = df. collect ( ) . await ?;
41384138
4139+ let df_explain = ctx. sql ( "explain SELECT a FROM data" ) . await ?;
4140+ let explain_result = df_explain. collect ( ) . await ?;
4141+
4142+ println ! ( "explain_result {:?}" , explain_result) ;
4143+
41394144 assert_batches_eq ! (
41404145 & [
41414146 "+---+---+" ,
@@ -4268,4 +4273,58 @@ mod tests {
42684273 ) ;
42694274 Ok ( ( ) )
42704275 }
4276+
4277+ // Test issue: https://github.com/apache/datafusion/issues/13873
4278+ #[ tokio:: test]
4279+ async fn write_table_with_order ( ) -> Result < ( ) > {
4280+ let tmp_dir = TempDir :: new ( ) ?;
4281+ let ctx = SessionContext :: new ( ) ;
4282+ let location = tmp_dir. path ( ) . join ( "test_table/" ) ;
4283+
4284+ let mut write_df = ctx
4285+ . sql ( "values ('z'), ('x'), ('a'), ('b'), ('c')" )
4286+ . await
4287+ . unwrap ( ) ;
4288+
4289+ // Ensure the column names and types match the target table
4290+ write_df = write_df
4291+ . with_column_renamed ( "column1" , "tablecol1" )
4292+ . unwrap ( ) ;
4293+ let sql_str =
4294+ "create external table data(tablecol1 varchar) stored as parquet location '"
4295+ . to_owned ( )
4296+ + location. to_str ( ) . unwrap ( )
4297+ + "'" ;
4298+
4299+ ctx. sql ( sql_str. as_str ( ) ) . await ?. collect ( ) . await ?;
4300+
4301+ // This is equivalent to INSERT INTO test.
4302+ write_df
4303+ . clone ( )
4304+ . write_table (
4305+ "data" ,
4306+ DataFrameWriteOptions :: new ( )
4307+ . with_sort_by ( vec ! [ col( "tablecol1" ) . sort( true , true ) ] ) ,
4308+ )
4309+ . await ?;
4310+
4311+ let df = ctx. sql ( "SELECT * FROM data" ) . await ?;
4312+ let results = df. collect ( ) . await ?;
4313+
4314+ assert_batches_eq ! (
4315+ & [
4316+ "+-----------+" ,
4317+ "| tablecol1 |" ,
4318+ "+-----------+" ,
4319+ "| a |" ,
4320+ "| b |" ,
4321+ "| c |" ,
4322+ "| x |" ,
4323+ "| z |" ,
4324+ "+-----------+" ,
4325+ ] ,
4326+ & results
4327+ ) ;
4328+ Ok ( ( ) )
4329+ }
42714330}
0 commit comments