Skip to content

Commit c5cefa8

Browse files
authored
Handle EmptyRelation during SQL unparsing (#10803)
* handle empty relation for sql unparsing * revert the doc test change * remove dbg macro * cargo fmt * improve the error handling
1 parent 70256ba commit c5cefa8

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

datafusion/sql/src/unparser/ast.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl SelectBuilder {
242242
from: self
243243
.from
244244
.iter()
245-
.map(|b| b.build())
245+
.filter_map(|b| b.build().transpose())
246246
.collect::<Result<Vec<_>, BuilderError>>()?,
247247
lateral_views: self.lateral_views.clone(),
248248
selection: self.selection.clone(),
@@ -314,18 +314,17 @@ impl TableWithJoinsBuilder {
314314
new
315315
}
316316

317-
pub fn build(&self) -> Result<ast::TableWithJoins, BuilderError> {
318-
Ok(ast::TableWithJoins {
319-
relation: match self.relation {
320-
Some(ref value) => value.build()?,
321-
None => {
322-
return Result::Err(Into::into(UninitializedFieldError::from(
323-
"relation",
324-
)))
325-
}
317+
pub fn build(&self) -> Result<Option<ast::TableWithJoins>, BuilderError> {
318+
match self.relation {
319+
Some(ref value) => match value.build()? {
320+
Some(relation) => Ok(Some(ast::TableWithJoins {
321+
relation,
322+
joins: self.joins.clone(),
323+
})),
324+
None => Ok(None),
326325
},
327-
joins: self.joins.clone(),
328-
})
326+
None => Err(Into::into(UninitializedFieldError::from("relation"))),
327+
}
329328
}
330329
fn create_empty() -> Self {
331330
Self {
@@ -350,6 +349,7 @@ pub(super) struct RelationBuilder {
350349
enum TableFactorBuilder {
351350
Table(TableRelationBuilder),
352351
Derived(DerivedRelationBuilder),
352+
Empty,
353353
}
354354

355355
#[allow(dead_code)]
@@ -367,6 +367,11 @@ impl RelationBuilder {
367367
new.relation = Option::Some(TableFactorBuilder::Derived(value));
368368
new
369369
}
370+
pub fn empty(&mut self) -> &mut Self {
371+
let new = self;
372+
new.relation = Some(TableFactorBuilder::Empty);
373+
new
374+
}
370375
pub fn alias(&mut self, value: Option<ast::TableAlias>) -> &mut Self {
371376
let new = self;
372377
match new.relation {
@@ -376,14 +381,16 @@ impl RelationBuilder {
376381
Some(TableFactorBuilder::Derived(ref mut rel_builder)) => {
377382
rel_builder.alias = value;
378383
}
384+
Some(TableFactorBuilder::Empty) => (),
379385
None => (),
380386
}
381387
new
382388
}
383-
pub fn build(&self) -> Result<ast::TableFactor, BuilderError> {
389+
pub fn build(&self) -> Result<Option<ast::TableFactor>, BuilderError> {
384390
Ok(match self.relation {
385-
Some(TableFactorBuilder::Table(ref value)) => value.build()?,
386-
Some(TableFactorBuilder::Derived(ref value)) => value.build()?,
391+
Some(TableFactorBuilder::Table(ref value)) => Some(value.build()?),
392+
Some(TableFactorBuilder::Derived(ref value)) => Some(value.build()?),
393+
Some(TableFactorBuilder::Empty) => None,
387394
None => {
388395
return Result::Err(Into::into(UninitializedFieldError::from("relation")))
389396
}

datafusion/sql/src/unparser/plan.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,10 @@ impl Unparser<'_> {
389389
)?;
390390

391391
let ast_join = ast::Join {
392-
relation: right_relation.build()?,
392+
relation: match right_relation.build()? {
393+
Some(relation) => relation,
394+
None => return internal_err!("Failed to build right relation"),
395+
},
393396
join_operator: self
394397
.join_operator_to_sql(join.join_type, join_constraint),
395398
};
@@ -417,7 +420,10 @@ impl Unparser<'_> {
417420
)?;
418421

419422
let ast_join = ast::Join {
420-
relation: right_relation.build()?,
423+
relation: match right_relation.build()? {
424+
Some(relation) => relation,
425+
None => return internal_err!("Failed to build right relation"),
426+
},
421427
join_operator: self.join_operator_to_sql(
422428
JoinType::Inner,
423429
ast::JoinConstraint::On(ast::Expr::Value(ast::Value::Boolean(
@@ -483,6 +489,10 @@ impl Unparser<'_> {
483489
relation,
484490
)
485491
}
492+
LogicalPlan::EmptyRelation(_) => {
493+
relation.empty();
494+
Ok(())
495+
}
486496
LogicalPlan::Extension(_) => not_impl_err!("Unsupported operator: {plan:?}"),
487497
_ => not_impl_err!("Unsupported operator: {plan:?}"),
488498
}

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ fn roundtrip_expr() {
7878
#[test]
7979
fn roundtrip_statement() -> Result<()> {
8080
let tests: Vec<&str> = vec![
81+
"select 1;",
82+
"select 1 limit 0;",
83+
"select ta.j1_id from j1 ta join (select 1 as j1_id) tb on ta.j1_id = tb.j1_id;",
84+
"select ta.j1_id from j1 ta join (select 1 as j1_id) tb on ta.j1_id = tb.j1_id where ta.j1_id > 1;",
85+
"select ta.j1_id from (select 1 as j1_id) ta;",
8186
"select ta.j1_id from j1 ta;",
8287
"select ta.j1_id from j1 ta order by ta.j1_id;",
8388
"select * from j1 ta order by ta.j1_id, ta.j1_string desc;",

0 commit comments

Comments
 (0)