Skip to content

Commit 71646b3

Browse files
committed
feat(cubesql): Use only actually used cubes in member expressions
1 parent 2edcd0d commit 71646b3

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,8 @@ impl CubeScanWrapperNode {
12641264
subqueries_sql.clone(),
12651265
)
12661266
.await?;
1267+
1268+
let join_condition_members = &join_condition[0].1;
12671269
let join_condition = join_condition[0].0.expr.clone();
12681270
sql = new_sql;
12691271

@@ -1275,6 +1277,7 @@ impl CubeScanWrapperNode {
12751277
expr: join_condition,
12761278
alias: "__join__alias__unused".to_string(),
12771279
},
1280+
join_condition_members,
12781281
&ungrouped_scan_node.used_cubes,
12791282
)?;
12801283
serde_json::json!(res).to_string()
@@ -1310,24 +1313,27 @@ impl CubeScanWrapperNode {
13101313
measures: Some(
13111314
aggregate
13121315
.iter()
1313-
.map(|(m, _used_members)| {
1316+
.map(|(m, used_members)| {
13141317
Self::ungrouped_member_def(
13151318
m,
1319+
used_members,
13161320
&ungrouped_scan_node.used_cubes,
13171321
)
13181322
})
13191323
.chain(
13201324
// TODO understand type of projections
1321-
projection.iter().map(|(m, _used_members)| {
1325+
projection.iter().map(|(m, used_members)| {
13221326
Self::ungrouped_member_def(
13231327
m,
1328+
used_members,
13241329
&ungrouped_scan_node.used_cubes,
13251330
)
13261331
}),
13271332
)
1328-
.chain(window.iter().map(|(m, _used_members)| {
1333+
.chain(window.iter().map(|(m, used_members)| {
13291334
Self::ungrouped_member_def(
13301335
m,
1336+
used_members,
13311337
&ungrouped_scan_node.used_cubes,
13321338
)
13331339
}))
@@ -1337,9 +1343,10 @@ impl CubeScanWrapperNode {
13371343
group_by
13381344
.iter()
13391345
.zip(group_descs.iter())
1340-
.map(|((m, _used_members), t)| {
1346+
.map(|((m, used_members), t)| {
13411347
Self::dimension_member_def(
13421348
m,
1349+
used_members,
13431350
&ungrouped_scan_node.used_cubes,
13441351
t,
13451352
)
@@ -1349,9 +1356,10 @@ impl CubeScanWrapperNode {
13491356
segments: Some(
13501357
filter
13511358
.iter()
1352-
.map(|(m, _used_members)| {
1359+
.map(|(m, used_members)| {
13531360
Self::ungrouped_member_def(
13541361
m,
1362+
used_members,
13551363
&ungrouped_scan_node.used_cubes,
13561364
)
13571365
})
@@ -1592,40 +1600,55 @@ impl CubeScanWrapperNode {
15921600
Ok((aliased_columns, sql))
15931601
}
15941602

1595-
fn make_member_def(
1603+
fn make_member_def<'m>(
15961604
column: &AliasedColumn,
1597-
used_cubes: &Vec<String>,
1605+
used_members: impl IntoIterator<Item = &'m String>,
1606+
ungrouped_scan_cubes: &Vec<String>,
15981607
) -> Result<UngrouppedMemberDef> {
1608+
let used_cubes = used_members
1609+
.into_iter()
1610+
.flat_map(|member| member.split_once('.'))
1611+
.map(|(cube, _rest)| cube)
1612+
.unique()
1613+
.map(|cube| cube.to_string())
1614+
.collect::<Vec<_>>();
1615+
let cube_name = used_cubes
1616+
.first()
1617+
.or_else(|| ungrouped_scan_cubes.first())
1618+
.ok_or_else(|| {
1619+
DataFusionError::Internal(format!(
1620+
"Can't generate SQL for column without cubes: {:?}",
1621+
column
1622+
))
1623+
})?
1624+
.clone();
1625+
15991626
let res = UngrouppedMemberDef {
1600-
cube_name: used_cubes
1601-
.iter()
1602-
.next()
1603-
.ok_or_else(|| {
1604-
DataFusionError::Internal(format!(
1605-
"Can't generate SQL for column without cubes: {:?}",
1606-
column
1607-
))
1608-
})?
1609-
.to_string(),
1627+
cube_name,
16101628
alias: column.alias.clone(),
1611-
cube_params: used_cubes.clone(),
1629+
cube_params: used_cubes,
16121630
expr: column.expr.clone(),
16131631
grouping_set: None,
16141632
};
16151633
Ok(res)
16161634
}
16171635

1618-
fn ungrouped_member_def(column: &AliasedColumn, used_cubes: &Vec<String>) -> Result<String> {
1619-
let res = Self::make_member_def(column, used_cubes)?;
1636+
fn ungrouped_member_def<'m>(
1637+
column: &AliasedColumn,
1638+
used_members: impl IntoIterator<Item = &'m String>,
1639+
ungrouped_scan_cubes: &Vec<String>,
1640+
) -> Result<String> {
1641+
let res = Self::make_member_def(column, used_members, ungrouped_scan_cubes)?;
16201642
Ok(serde_json::json!(res).to_string())
16211643
}
16221644

1623-
fn dimension_member_def(
1645+
fn dimension_member_def<'m>(
16241646
column: &AliasedColumn,
1625-
used_cubes: &Vec<String>,
1647+
used_members: impl IntoIterator<Item = &'m String>,
1648+
ungrouped_scan_cubes: &Vec<String>,
16261649
grouping_type: &Option<GroupingSetDesc>,
16271650
) -> Result<String> {
1628-
let mut res = Self::make_member_def(column, used_cubes)?;
1651+
let mut res = Self::make_member_def(column, used_members, ungrouped_scan_cubes)?;
16291652
res.grouping_set = grouping_type.clone();
16301653
Ok(serde_json::json!(res).to_string())
16311654
}

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7285,7 +7285,7 @@ ORDER BY
72857285
json!({
72867286
"cube_name": "WideCube",
72877287
"alias": "pivot_grouping",
7288-
"cube_params": ["WideCube"],
7288+
"cube_params": [],
72897289
"expr": "0",
72907290
"grouping_set": null,
72917291
})
@@ -11809,16 +11809,16 @@ ORDER BY "source"."str0" ASC
1180911809
json!({
1181011810
"cube_name": "KibanaSampleDataEcommerce",
1181111811
"alias": "ta_1_order_date_",
11812-
"cube_params": ["KibanaSampleDataEcommerce", "Logs"],
11812+
"cube_params": ["KibanaSampleDataEcommerce"],
1181311813
"expr": "((${KibanaSampleDataEcommerce.order_date} = DATE('1994-05-01')) OR (${KibanaSampleDataEcommerce.order_date} = DATE('1996-05-03')))",
1181411814
"grouping_set": null,
1181511815
}).to_string(),
1181611816
]),
1181711817
segments: Some(vec![
1181811818
json!({
11819-
"cube_name": "KibanaSampleDataEcommerce",
11819+
"cube_name": "Logs",
1182011820
"alias": "lower_ta_2_conte",
11821-
"cube_params": ["KibanaSampleDataEcommerce", "Logs"],
11821+
"cube_params": ["Logs"],
1182211822
"expr": "(LOWER(${Logs.content}) = $0$)",
1182311823
"grouping_set": null,
1182411824
}).to_string(),

0 commit comments

Comments
 (0)