Skip to content

Commit 051269e

Browse files
authored
fix: Remove duplicate filter from CrossJoin unparsing (#17382)
* fix: Remove duplicate filter from `CrossJoin` unparsing * move test * add
1 parent 3d5863b commit 051269e

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

datafusion/core/tests/sql/joins.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use insta::assert_snapshot;
1919

2020
use datafusion::assert_batches_eq;
21+
use datafusion::catalog::MemTable;
2122
use datafusion::datasource::stream::{FileStreamProvider, StreamConfig, StreamTable};
2223
use datafusion::test_util::register_unbounded_file_with_ordering;
24+
use datafusion_sql::unparser::plan_to_sql;
2325

2426
use super::*;
2527

@@ -264,3 +266,45 @@ async fn join_using_uppercase_column() -> Result<()> {
264266

265267
Ok(())
266268
}
269+
270+
// Issue #17359: https://github.com/apache/datafusion/issues/17359
271+
#[tokio::test]
272+
async fn unparse_cross_join() -> Result<()> {
273+
let ctx = SessionContext::new();
274+
275+
let j1_schema = Arc::new(Schema::new(vec![
276+
Field::new("j1_id", DataType::Int32, true),
277+
Field::new("j1_string", DataType::Utf8, true),
278+
]));
279+
let j2_schema = Arc::new(Schema::new(vec![
280+
Field::new("j2_id", DataType::Int32, true),
281+
Field::new("j2_string", DataType::Utf8, true),
282+
]));
283+
284+
ctx.register_table("j1", Arc::new(MemTable::try_new(j1_schema, vec![vec![]])?))?;
285+
ctx.register_table("j2", Arc::new(MemTable::try_new(j2_schema, vec![vec![]])?))?;
286+
287+
let df = ctx
288+
.sql(
289+
r#"
290+
select j1.j1_id, j2.j2_string
291+
from j1, j2
292+
where j2.j2_id = 0
293+
"#,
294+
)
295+
.await?;
296+
297+
let unopt_sql = plan_to_sql(df.logical_plan())?;
298+
assert_snapshot!(unopt_sql, @r#"
299+
SELECT j1.j1_id, j2.j2_string FROM j1 CROSS JOIN j2 WHERE (j2.j2_id = 0)
300+
"#);
301+
302+
let optimized_plan = df.into_optimized_plan()?;
303+
304+
let opt_sql = plan_to_sql(&optimized_plan)?;
305+
assert_snapshot!(opt_sql, @r#"
306+
SELECT j1.j1_id, j2.j2_string FROM j1 CROSS JOIN j2 WHERE (j2.j2_id = 0)
307+
"#);
308+
309+
Ok(())
310+
}

datafusion/sql/src/unparser/plan.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,6 @@ impl Unparser<'_> {
696696
join_filters.as_ref(),
697697
)?;
698698

699-
self.select_to_sql_recursively(
700-
right_plan.as_ref(),
701-
query,
702-
select,
703-
&mut right_relation,
704-
)?;
705-
706699
let right_projection: Option<Vec<ast::SelectItem>> = if !already_projected
707700
{
708701
Some(select.pop_projections())

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use arrow::datatypes::{DataType, Field, Schema};
19+
1920
use datafusion_common::{
2021
assert_contains, Column, DFSchema, DFSchemaRef, DataFusionError, Result,
2122
TableReference,

0 commit comments

Comments
 (0)