Skip to content

Commit a03fd60

Browse files
authored
fix(core): handle the unnest table factor for the specific dialect (#1104)
1 parent ae56e13 commit a03fd60

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

wren-core/core/src/mdl/dialect/inner_dialect.rs

+14
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ pub trait InnerDialect: Send + Sync {
3636
) -> Result<Option<ast::Expr>> {
3737
Ok(None)
3838
}
39+
40+
/// A wrapper for [datafusion::sql::unparser::dialect::Dialect::unnest_as_table_factor].
41+
fn unnest_as_table_factor(&self) -> bool {
42+
false
43+
}
3944
}
4045

4146
/// [get_inner_dialect] returns the suitable InnerDialect for the given data source.
4247
pub fn get_inner_dialect(data_source: &DataSource) -> Box<dyn InnerDialect> {
4348
match data_source {
4449
DataSource::MySQL => Box::new(MySQLDialect {}),
50+
DataSource::BigQuery => Box::new(BigQueryDialect {}),
4551
_ => Box::new(GenericDialect {}),
4652
}
4753
}
@@ -68,3 +74,11 @@ impl InnerDialect for MySQLDialect {
6874
}
6975
}
7076
}
77+
78+
pub struct BigQueryDialect {}
79+
80+
impl InnerDialect for BigQueryDialect {
81+
fn unnest_as_table_factor(&self) -> bool {
82+
true
83+
}
84+
}

wren-core/core/src/mdl/dialect/wren_dialect.rs

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ impl Dialect for WrenDialect {
8383
_ => Ok(None),
8484
}
8585
}
86+
87+
fn unnest_as_table_factor(&self) -> bool {
88+
self.inner_dialect.unnest_as_table_factor()
89+
}
8690
}
8791

8892
impl Default for WrenDialect {

wren-core/core/src/mdl/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ mod test {
449449
use datafusion::config::ConfigOptions;
450450
use datafusion::prelude::{SessionConfig, SessionContext};
451451
use datafusion::sql::unparser::plan_to_sql;
452+
use wren_core_base::mdl::DataSource;
452453

453454
#[test]
454455
fn test_sync_transform() -> Result<()> {
@@ -976,6 +977,25 @@ mod test {
976977
Ok(())
977978
}
978979

980+
#[tokio::test]
981+
async fn test_unnest_as_table_factor() -> Result<()> {
982+
let ctx = SessionContext::new();
983+
let manifest = ManifestBuilder::new().build();
984+
let analyzed_mdl = Arc::new(AnalyzedWrenMDL::analyze(manifest)?);
985+
let sql = "select * from unnest([1, 2, 3])";
986+
let actual = transform_sql_with_ctx(&ctx, analyzed_mdl, &[], sql).await?;
987+
assert_eq!(actual, "SELECT \"UNNEST(make_array(Int64(1),Int64(2),Int64(3)))\" FROM (SELECT UNNEST([1, 2, 3]) AS \"UNNEST(make_array(Int64(1),Int64(2),Int64(3)))\")");
988+
989+
let manifest = ManifestBuilder::new()
990+
.data_source(DataSource::BigQuery)
991+
.build();
992+
let analyzed_mdl = Arc::new(AnalyzedWrenMDL::analyze(manifest)?);
993+
let sql = "select * from unnest([1, 2, 3])";
994+
let actual = transform_sql_with_ctx(&ctx, analyzed_mdl, &[], sql).await?;
995+
assert_eq!(actual, "SELECT \"UNNEST(make_array(Int64(1),Int64(2),Int64(3)))\" FROM UNNEST([1, 2, 3])");
996+
Ok(())
997+
}
998+
979999
#[tokio::test]
9801000
async fn test_simplify_timestamp() -> Result<()> {
9811001
let ctx = SessionContext::new();

0 commit comments

Comments
 (0)