Skip to content

Commit 7c6f891

Browse files
authored
Introduce full_qualified_col option for the unparser dialect (apache#13241)
* introduce `full_qualified_col` for unparser dialect * fix test and clippy
1 parent e8520ab commit 7c6f891

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

datafusion/sql/src/unparser/dialect.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ pub trait Dialect: Send + Sync {
137137
) -> Result<Option<ast::Expr>> {
138138
Ok(None)
139139
}
140+
141+
/// Allow to unparse a qualified column with a full qualified name
142+
/// (e.g. catalog_name.schema_name.table_name.column_name)
143+
/// Otherwise, the column will be unparsed with only the table name and colum name
144+
/// (e.g. table_name.column_name)
145+
fn full_qualified_col(&self) -> bool {
146+
false
147+
}
140148
}
141149

142150
/// `IntervalStyle` to use for unparsing
@@ -373,6 +381,7 @@ pub struct CustomDialect {
373381
date32_cast_dtype: ast::DataType,
374382
supports_column_alias_in_table_alias: bool,
375383
requires_derived_table_alias: bool,
384+
full_qualified_col: bool,
376385
}
377386

378387
impl Default for CustomDialect {
@@ -396,6 +405,7 @@ impl Default for CustomDialect {
396405
date32_cast_dtype: ast::DataType::Date,
397406
supports_column_alias_in_table_alias: true,
398407
requires_derived_table_alias: false,
408+
full_qualified_col: false,
399409
}
400410
}
401411
}
@@ -488,6 +498,10 @@ impl Dialect for CustomDialect {
488498
fn requires_derived_table_alias(&self) -> bool {
489499
self.requires_derived_table_alias
490500
}
501+
502+
fn full_qualified_col(&self) -> bool {
503+
self.full_qualified_col
504+
}
491505
}
492506

493507
/// `CustomDialectBuilder` to build `CustomDialect` using builder pattern
@@ -520,6 +534,7 @@ pub struct CustomDialectBuilder {
520534
date32_cast_dtype: ast::DataType,
521535
supports_column_alias_in_table_alias: bool,
522536
requires_derived_table_alias: bool,
537+
full_qualified_col: bool,
523538
}
524539

525540
impl Default for CustomDialectBuilder {
@@ -549,6 +564,7 @@ impl CustomDialectBuilder {
549564
date32_cast_dtype: ast::DataType::Date,
550565
supports_column_alias_in_table_alias: true,
551566
requires_derived_table_alias: false,
567+
full_qualified_col: false,
552568
}
553569
}
554570

@@ -570,6 +586,7 @@ impl CustomDialectBuilder {
570586
supports_column_alias_in_table_alias: self
571587
.supports_column_alias_in_table_alias,
572588
requires_derived_table_alias: self.requires_derived_table_alias,
589+
full_qualified_col: self.full_qualified_col,
573590
}
574591
}
575592

@@ -677,4 +694,10 @@ impl CustomDialectBuilder {
677694
self.requires_derived_table_alias = requires_derived_table_alias;
678695
self
679696
}
697+
698+
/// Customize the dialect to allow full qualified column names
699+
pub fn with_full_qualified_col(mut self, full_qualified_col: bool) -> Self {
700+
self.full_qualified_col = full_qualified_col;
701+
self
702+
}
680703
}

datafusion/sql/src/unparser/expr.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,11 @@ impl Unparser<'_> {
527527

528528
fn col_to_sql(&self, col: &Column) -> Result<ast::Expr> {
529529
if let Some(table_ref) = &col.relation {
530-
let mut id = table_ref.to_vec();
530+
let mut id = if self.dialect.full_qualified_col() {
531+
table_ref.to_vec()
532+
} else {
533+
vec![table_ref.table().to_string()]
534+
};
531535
id.push(col.name.to_string());
532536
return Ok(ast::Expr::CompoundIdentifier(
533537
id.iter()
@@ -1545,7 +1549,7 @@ mod tests {
15451549
name: "c".to_string(),
15461550
})
15471551
.gt(lit(4)),
1548-
r#"(a.b.c > 4)"#,
1552+
r#"(b.c > 4)"#,
15491553
),
15501554
(
15511555
case(col("a"))
@@ -1882,7 +1886,7 @@ mod tests {
18821886
name: "array_col".to_string(),
18831887
})),
18841888
}),
1885-
r#"UNNEST("schema"."table".array_col)"#,
1889+
r#"UNNEST("table".array_col)"#,
18861890
),
18871891
];
18881892

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use datafusion_functions_nested::make_array::make_array_udf;
2828
use datafusion_functions_window::rank::rank_udwf;
2929
use datafusion_sql::planner::{ContextProvider, PlannerContext, SqlToRel};
3030
use datafusion_sql::unparser::dialect::{
31-
DefaultDialect as UnparserDefaultDialect, Dialect as UnparserDialect,
32-
MySqlDialect as UnparserMySqlDialect, SqliteDialect,
31+
CustomDialectBuilder, DefaultDialect as UnparserDefaultDialect, DefaultDialect,
32+
Dialect as UnparserDialect, MySqlDialect as UnparserMySqlDialect, SqliteDialect,
3333
};
3434
use datafusion_sql::unparser::{expr_to_sql, plan_to_sql, Unparser};
3535

@@ -565,7 +565,7 @@ Projection: unnest_placeholder(unnest_table.struct_col).field1, unnest_placehold
565565

566566
#[test]
567567
fn test_table_references_in_plan_to_sql() {
568-
fn test(table_name: &str, expected_sql: &str) {
568+
fn test(table_name: &str, expected_sql: &str, dialect: &impl UnparserDialect) {
569569
let schema = Schema::new(vec![
570570
Field::new("id", DataType::Utf8, false),
571571
Field::new("value", DataType::Utf8, false),
@@ -576,22 +576,48 @@ fn test_table_references_in_plan_to_sql() {
576576
.unwrap()
577577
.build()
578578
.unwrap();
579-
let sql = plan_to_sql(&plan).unwrap();
579+
580+
let unparser = Unparser::new(dialect);
581+
let sql = unparser.plan_to_sql(&plan).unwrap();
580582

581583
assert_eq!(sql.to_string(), expected_sql)
582584
}
583585

584586
test(
585587
"catalog.schema.table",
586-
r#"SELECT "catalog"."schema"."table".id, "catalog"."schema"."table"."value" FROM "catalog"."schema"."table""#,
588+
r#"SELECT "table".id, "table"."value" FROM "catalog"."schema"."table""#,
589+
&DefaultDialect {},
587590
);
588591
test(
589592
"schema.table",
590-
r#"SELECT "schema"."table".id, "schema"."table"."value" FROM "schema"."table""#,
593+
r#"SELECT "table".id, "table"."value" FROM "schema"."table""#,
594+
&DefaultDialect {},
591595
);
592596
test(
593597
"table",
594598
r#"SELECT "table".id, "table"."value" FROM "table""#,
599+
&DefaultDialect {},
600+
);
601+
602+
let custom_dialect = CustomDialectBuilder::default()
603+
.with_full_qualified_col(true)
604+
.with_identifier_quote_style('"')
605+
.build();
606+
607+
test(
608+
"catalog.schema.table",
609+
r#"SELECT "catalog"."schema"."table"."id", "catalog"."schema"."table"."value" FROM "catalog"."schema"."table""#,
610+
&custom_dialect,
611+
);
612+
test(
613+
"schema.table",
614+
r#"SELECT "schema"."table"."id", "schema"."table"."value" FROM "schema"."table""#,
615+
&custom_dialect,
616+
);
617+
test(
618+
"table",
619+
r#"SELECT "table"."id", "table"."value" FROM "table""#,
620+
&custom_dialect,
595621
);
596622
}
597623

0 commit comments

Comments
 (0)