Skip to content

feat(cubesql): PowerBI support for wrapped queries #4752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 144 additions & 116 deletions rust/cubesql/cubesql/src/compile/engine/df/scan.rs

Large diffs are not rendered by default.

166 changes: 141 additions & 25 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ impl QueryPlanner {
schema
.fields()
.iter()
.map(|f| f.name().to_string())
.map(|f| Some(f.name().to_string()))
.collect(),
query.request,
// @todo Remove after split!
Expand Down Expand Up @@ -3588,7 +3588,13 @@ mod tests {
order: None,
limit: None,
offset: None,
filters: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.count".to_string()),
operator: Some("gt".to_string()),
values: Some(vec!["0".to_string()]),
or: None,
and: None,
}]),
}
);

Expand All @@ -3608,13 +3614,22 @@ mod tests {
order: None,
limit: None,
offset: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.has_subscription".to_string()),
operator: Some("equals".to_string()),
values: Some(vec!["false".to_string()]),
or: None,
and: None,
}]),
filters: Some(vec![
V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.has_subscription".to_string()),
operator: Some("equals".to_string()),
values: Some(vec!["false".to_string()]),
or: None,
and: None,
},
V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.count".to_string()),
operator: Some("gt".to_string()),
values: Some(vec!["0".to_string()]),
or: None,
and: None,
}
]),
}
);
}
Expand Down Expand Up @@ -3875,7 +3890,13 @@ ORDER BY \"COUNT(count)\" DESC"
order: None,
limit: None,
offset: None,
filters: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.count".to_string()),
operator: Some("gt".to_string()),
values: Some(vec!["0".to_string()]),
or: None,
and: None,
}]),
}
);

Expand All @@ -3886,17 +3907,11 @@ ORDER BY \"COUNT(count)\" DESC"
.iter()
.map(|f| f.name().to_string())
.collect::<Vec<_>>(),
vec![
"SUM(KibanaSampleDataEcommerce.count)".to_string(),
"COUNT(UInt8(1))".to_string()
]
vec!["sum:count:ok".to_string(),]
);
assert_eq!(
&cube_scan.member_fields,
&vec![
"KibanaSampleDataEcommerce.count".to_string(),
"KibanaSampleDataEcommerce.count".to_string()
]
&vec![Some("KibanaSampleDataEcommerce.count".to_string())]
);
}

Expand Down Expand Up @@ -4104,7 +4119,7 @@ ORDER BY \"COUNT(count)\" DESC"

let query_plan = convert_select_to_query_plan(
"select \"rows\".\"customer_gender\" as \"customer_gender\",
\n count(1) as \"a0\"\
\n sum(\"rows\".\"count\") as \"a0\"\
\nfrom\
\n(\
\n select \"_\".\"count\",\
Expand All @@ -4131,7 +4146,7 @@ ORDER BY \"COUNT(count)\" DESC"
segments: Some(vec![]),
time_dimensions: None,
order: None,
limit: Some(1000001),
limit: Some(50000),
offset: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
Expand All @@ -4144,6 +4159,108 @@ ORDER BY \"COUNT(count)\" DESC"
);
}

#[test]
fn powerbi_inner_wrapped_dates() {
init_logger();

let query_plan = convert_select_to_query_plan(
"select \"_\".\"created_at_day\",\
\n \"_\".\"a0\"\
\nfrom \
\n(\
\n select \"rows\".\"created_at_day\" as \"created_at_day\",\
\n sum(\"rows\".\"cnt\") as \"a0\"\
\n from \
\n (\
\n select count(*) cnt,date_trunc('day', order_date) as created_at_day, date_trunc('month', order_date) as created_at_month from public.KibanaSampleDataEcommerce group by 2, 3\
\n ) \"rows\"\
\n group by \"created_at_day\"\
\n) \"_\"\
\nwhere not \"_\".\"a0\" is null\
\nlimit 1000001"
.to_string(),
DatabaseProtocol::PostgreSQL,
);

let logical_plan = query_plan.as_logical_plan();
assert_eq!(
logical_plan.find_cube_scan().request,
V1LoadRequestQuery {
measures: Some(vec!["KibanaSampleDataEcommerce.count".to_string()]),
dimensions: Some(vec![]),
segments: Some(vec![]),
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
dimension: "KibanaSampleDataEcommerce.order_date".to_string(),
granularity: Some("day".to_string()),
date_range: None,
}]),
order: None,
limit: Some(50000),
offset: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.count".to_string()),
operator: Some("set".to_string()),
values: None,
or: None,
and: None,
}]),
}
);
}

#[test]
fn powerbi_inner_wrapped_asterisk() {
init_logger();

let query_plan = convert_select_to_query_plan(
"select \"rows\".\"customer_gender\" as \"customer_gender\",\
\n \"rows\".\"created_at_month\" as \"created_at_month\"\
\nfrom \
\n(\
\n select \"_\".\"count\",\
\n \"_\".\"minPrice\",\
\n \"_\".\"maxPrice\",\
\n \"_\".\"avgPrice\",\
\n \"_\".\"order_date\",\
\n \"_\".\"customer_gender\",\
\n \"_\".\"created_at_day\",\
\n \"_\".\"created_at_month\"\
\n from \
\n (\
\n select *, date_trunc('day', order_date) created_at_day, date_trunc('month', order_date) created_at_month from public.KibanaSampleDataEcommerce\
\n ) \"_\"\
\n where \"_\".\"created_at_month\" < timestamp '2022-06-13 00:00:00' and \"_\".\"created_at_month\" >= timestamp '2021-12-16 00:00:00'\
\n) \"rows\"\
\ngroup by \"customer_gender\",\
\n \"created_at_month\"\
\nlimit 1000001"
.to_string(),
DatabaseProtocol::PostgreSQL,
);

let logical_plan = query_plan.as_logical_plan();
assert_eq!(
logical_plan.find_cube_scan().request,
V1LoadRequestQuery {
measures: Some(vec![]),
dimensions: Some(vec!["KibanaSampleDataEcommerce.customer_gender".to_string()]),
segments: Some(vec![]),
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
dimension: "KibanaSampleDataEcommerce.order_date".to_string(),
granularity: Some("month".to_string()),
date_range: Some(json!(vec![
"2021-12-16 00:00:00".to_string(),
"2022-06-13 00:00:00".to_string()
])),
}]),
order: None,
limit: Some(50000),
offset: None,
filters: None,
}
);
}

#[test]
fn test_select_aggregations() {
let variants = vec![
Expand Down Expand Up @@ -4295,10 +4412,10 @@ ORDER BY \"COUNT(count)\" DESC"
// "SELECT is_male FROM KibanaSampleDataEcommerce".to_string(),
// CompilationError::User("Unable to use segment 'is_male' as column in SELECT statement".to_string()),
// ),
(
"SELECT COUNT(*) FROM KibanaSampleDataEcommerce GROUP BY is_male".to_string(),
CompilationError::User("Unable to use segment 'is_male' in GROUP BY".to_string()),
),
// (
// "SELECT COUNT(*) FROM KibanaSampleDataEcommerce GROUP BY is_male".to_string(),
// CompilationError::User("Unable to use segment 'is_male' in GROUP BY".to_string()),
// ),
// (
// "SELECT COUNT(*) FROM KibanaSampleDataEcommerce ORDER BY is_male DESC".to_string(),
// CompilationError::User("Unable to use segment 'is_male' in ORDER BY".to_string()),
Expand Down Expand Up @@ -4608,7 +4725,6 @@ ORDER BY \"COUNT(count)\" DESC"
""
}
);
println!("Query: {}", query);
let logical_plan =
convert_select_to_query_plan(query, DatabaseProtocol::MySQL).as_logical_plan();

Expand Down
12 changes: 11 additions & 1 deletion rust/cubesql/cubesql/src/compile/rewrite/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
rewrite::{
converter::{is_expr_node, node_to_expr},
AliasExprAlias, ColumnExprColumn, DimensionName, LiteralExprValue, LogicalPlanLanguage,
MeasureName, TableScanSourceTableName, TimeDimensionName,
MeasureName, SegmentName, TableScanSourceTableName, TimeDimensionName,
},
},
var_iter, CubeError,
Expand Down Expand Up @@ -124,6 +124,16 @@ impl LogicalPlanAnalysis {
None
}
}
LogicalPlanLanguage::Segment(params) => {
if let Some(_) = column_name(params[1]) {
let expr = original_expr(params[1])?;
let segment_name = var_iter!(egraph[params[0]], SegmentName).next().unwrap();
map.push((segment_name.to_string(), expr));
Some(map)
} else {
None
}
}
LogicalPlanLanguage::TimeDimension(params) => {
if let Some(_) = column_name(params[3]) {
let expr = original_expr(params[3])?;
Expand Down
Loading