Skip to content

Commit 241b9bf

Browse files
committed
expand wildcard first to fix missing column when filter
1 parent c1d19e8 commit 241b9bf

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ fn analyze_rule_for_local_runtime(
9191
session_state_ref: SessionStateRef,
9292
) -> Vec<Arc<dyn AnalyzerRule + Send + Sync>> {
9393
vec![
94+
// Every rule that will generate [Expr::Wildcard] should be placed in front of [ExpandWildcardRule].
95+
Arc::new(ExpandWildcardRule::new()),
9496
// expand the view should be the first rule
9597
Arc::new(ExpandWrenViewRule::new(
9698
Arc::clone(&analyzed_mdl),
@@ -118,6 +120,8 @@ fn analyze_rule_for_unparsing(
118120
session_state_ref: SessionStateRef,
119121
) -> Vec<Arc<dyn AnalyzerRule + Send + Sync>> {
120122
vec![
123+
// Every rule that will generate [Expr::Wildcard] should be placed in front of [ExpandWildcardRule].
124+
Arc::new(ExpandWildcardRule::new()),
121125
// expand the view should be the first rule
122126
Arc::new(ExpandWrenViewRule::new(
123127
Arc::clone(&analyzed_mdl),

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

+34-9
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ mod test {
618618
.await?;
619619
assert_eq!(actual,
620620
"SELECT \"Customer\".\"Custkey\", \"Customer\".\"Name\" FROM \
621-
(SELECT __source.\"Custkey\" AS \"Custkey\", __source.\"Name\" AS \"Name\" FROM \
622-
datafusion.\"public\".customer AS __source) AS \"Customer\"");
621+
(SELECT \"Customer\".\"Custkey\", \"Customer\".\"Name\" FROM \
622+
(SELECT __source.\"Custkey\" AS \"Custkey\", __source.\"Name\" AS \"Name\" FROM datafusion.\"public\".customer AS __source) AS \"Customer\") AS \"Customer\"");
623623
Ok(())
624624
}
625625

@@ -725,9 +725,9 @@ mod test {
725725
)
726726
.await?;
727727
assert_eq!(actual,
728-
"SELECT artist.\"名字\", artist.name_append, artist.\"group\", artist.subscribe_plus, artist.subscribe \
729-
FROM (SELECT __source.\"名字\" AS \"名字\", __source.\"名字\" || __source.\"名字\" AS name_append, __source.\"組別\" AS \"group\", \
730-
CAST(__source.\"訂閱數\" AS BIGINT) + 1 AS subscribe_plus, __source.\"訂閱數\" AS subscribe FROM artist AS __source) AS artist"
728+
"SELECT artist.\"名字\", artist.name_append, artist.\"group\", artist.subscribe, artist.subscribe_plus FROM \
729+
(SELECT artist.\"group\", artist.name_append, artist.subscribe, artist.subscribe_plus, artist.\"名字\" FROM \
730+
(SELECT __source.\"名字\" AS \"名字\", __source.\"名字\" || __source.\"名字\" AS name_append, __source.\"組別\" AS \"group\", CAST(__source.\"訂閱數\" AS BIGINT) + 1 AS subscribe_plus, __source.\"訂閱數\" AS subscribe FROM artist AS __source) AS artist) AS artist"
731731
);
732732
ctx.sql(&actual).await?.show().await?;
733733

@@ -843,9 +843,7 @@ mod test {
843843
)
844844
.await?;
845845
assert_eq!(actual,
846-
"SELECT artist.\"串接名字\" FROM (SELECT artist.\"串接名字\" FROM \
847-
(SELECT __source.\"名字\" || __source.\"名字\" AS \"串接名字\" FROM artist AS __source) AS artist) AS artist");
848-
846+
"SELECT artist.\"串接名字\" FROM (SELECT artist.\"串接名字\" FROM (SELECT __source.\"名字\" || __source.\"名字\" AS \"串接名字\" FROM artist AS __source) AS artist) AS artist");
849847
let sql = r#"select * from wren.test.artist"#;
850848
let actual = transform_sql_with_ctx(
851849
&SessionContext::new(),
@@ -855,7 +853,7 @@ mod test {
855853
)
856854
.await?;
857855
assert_eq!(actual,
858-
"SELECT artist.\"串接名字\" FROM (SELECT __source.\"名字\" || __source.\"名字\" AS \"串接名字\" FROM artist AS __source) AS artist");
856+
"SELECT artist.\"串接名字\" FROM (SELECT artist.\"串接名字\" FROM (SELECT __source.\"名字\" || __source.\"名字\" AS \"串接名字\" FROM artist AS __source) AS artist) AS artist");
859857

860858
let sql = r#"select "名字" from wren.test.artist"#;
861859
let _ = transform_sql_with_ctx(
@@ -1414,6 +1412,33 @@ mod test {
14141412
Ok(())
14151413
}
14161414

1415+
#[tokio::test]
1416+
async fn test_wildcard_where() -> Result<()> {
1417+
let ctx = SessionContext::new();
1418+
let manifest = ManifestBuilder::new()
1419+
.catalog("wren")
1420+
.schema("test")
1421+
.model(
1422+
ModelBuilder::new("customer")
1423+
.table_reference("customer")
1424+
.column(ColumnBuilder::new("c_custkey", "int").build())
1425+
.column(ColumnBuilder::new("c_name", "string").build())
1426+
.build(),
1427+
)
1428+
.build();
1429+
let sql = r#"SELECT * FROM customer WHERE c_custkey = 1"#;
1430+
let analyzed_mdl = Arc::new(AnalyzedWrenMDL::analyze(manifest)?);
1431+
let result =
1432+
transform_sql_with_ctx(&ctx, Arc::clone(&analyzed_mdl), &[], sql).await?;
1433+
assert_eq!(
1434+
result,
1435+
"SELECT customer.c_custkey, customer.c_name FROM (SELECT customer.c_custkey, customer.c_name FROM \
1436+
(SELECT __source.c_custkey AS c_custkey, __source.c_name AS c_name FROM customer AS __source) AS customer) AS customer \
1437+
WHERE customer.c_custkey = 1"
1438+
);
1439+
Ok(())
1440+
}
1441+
14171442
/// Return a RecordBatch with made up data about customer
14181443
fn customer() -> RecordBatch {
14191444
let custkey: ArrayRef = Arc::new(Int64Array::from(vec![1, 2, 3]));

wren-core/sqllogictest/test_files/model.slt

+11
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ select "Customer_id" from wrenai.public."Orders" where exists (select 1 from wre
7171
02d1b5b8831241174c6ef13efd35abbd
7272
04eafb40a16989307464f27f1fed8907
7373
0732c0881c70ebcda536a4b14e9db106
74+
75+
query RIITRTTT
76+
select * from "Order_items" where "Order_id" in ('03c83b31dbc387f83f1b5579b53182fb', '08cbb1d4cd574b126569b208fd4b26ea')
77+
----
78+
14.68 1 1 03c83b31dbc387f83f1b5579b53182fb 119.8 a04087ab6a96ffa041f8a2701a72b616 2023/1/15 7:26 CA
79+
6.9 4 1 08cbb1d4cd574b126569b208fd4b26ea 287.4 588531f8ec37e7d5ff5b7b22ea0488f8 2022/10/19 19:35 CA
80+
81+
query RIITRTTT
82+
select * from "Order_items" where "Order_id" = '03c83b31dbc387f83f1b5579b53182fb'
83+
----
84+
14.68 1 1 03c83b31dbc387f83f1b5579b53182fb 119.8 a04087ab6a96ffa041f8a2701a72b616 2023/1/15 7:26 CA

0 commit comments

Comments
 (0)