Skip to content
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

feat(cubesql): PowerBI contains filter support #4646

Merged
merged 2 commits into from
Jun 2, 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
12 changes: 6 additions & 6 deletions packages/cubejs-backend-native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4033,6 +4033,52 @@ ORDER BY \"COUNT(count)\" DESC"
);
}

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

let query_plan = convert_select_to_query_plan(
"select \"rows\".\"customer_gender\" as \"customer_gender\",
\n count(1) as \"a0\"\
\nfrom\
\n(\
\n select \"_\".\"count\",\
\n \"_\".\"customer_gender\"\
\n from \"public\".\"KibanaSampleDataEcommerce\" \"_\"\
\n where strpos((case\
\n when \"_\".\"customer_gender\" is not null\
\n then \"_\".\"customer_gender\"\
\n else ''\
\n end), 'fem') > 0\
\n) \"rows\"\
\ngroup by \"customer_gender\"\
\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!["KibanaSampleDataEcommerce.customer_gender".to_string()]),
segments: Some(vec![]),
time_dimensions: None,
order: None,
limit: Some(1000001),
offset: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.customer_gender".to_string()),
operator: Some("contains".to_string()),
values: Some(vec!["fem".to_string()]),
or: None,
and: None,
}]),
}
);
}

#[test]
fn test_select_aggregations() {
let variants = vec![
Expand Down
36 changes: 16 additions & 20 deletions rust/cubesql/cubesql/src/compile/rewrite/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,18 @@ impl LogicalPlanAnalysis {
push_referenced_columns(params[0], &mut vec)?;
Some(vec)
}
LogicalPlanLanguage::CaseExpr(params) => {
push_referenced_columns(params[0], &mut vec)?;
push_referenced_columns(params[1], &mut vec)?;
push_referenced_columns(params[2], &mut vec)?;
Some(vec)
}
LogicalPlanLanguage::ScalarFunctionExpr(params) => {
push_referenced_columns(params[1], &mut vec)?;
Some(vec)
}
LogicalPlanLanguage::ScalarFunctionExprArgs(params) => {
for p in params.iter() {
vec.extend(referenced_columns(*p)?.into_iter());
}

LogicalPlanLanguage::AggregateFunctionExpr(params) => {
push_referenced_columns(params[1], &mut vec)?;
Some(vec)
}
LogicalPlanLanguage::LiteralExpr(_) => Some(vec),
Expand All @@ -260,21 +263,14 @@ impl LogicalPlanAnalysis {
None
}
}
LogicalPlanLanguage::SortExp(params) => {
for p in params.iter() {
vec.extend(referenced_columns(*p)?.into_iter());
}

Some(vec)
}
LogicalPlanLanguage::AggregateGroupExpr(params) => {
for p in params.iter() {
vec.extend(referenced_columns(*p)?.into_iter());
}

Some(vec)
}
LogicalPlanLanguage::AggregateAggrExpr(params) => {
LogicalPlanLanguage::SortExp(params)
| LogicalPlanLanguage::AggregateGroupExpr(params)
| LogicalPlanLanguage::AggregateAggrExpr(params)
| LogicalPlanLanguage::CaseExprWhenThenExpr(params)
| LogicalPlanLanguage::CaseExprElseExpr(params)
| LogicalPlanLanguage::CaseExprExpr(params)
| LogicalPlanLanguage::AggregateFunctionExprArgs(params)
| LogicalPlanLanguage::ScalarFunctionExprArgs(params) => {
for p in params.iter() {
vec.extend(referenced_columns(*p)?.into_iter());
}
Expand Down
30 changes: 30 additions & 0 deletions rust/cubesql/cubesql/src/compile/rewrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ crate::plan_to_language! {
FilterCastUnwrapReplacer {
filters: Vec<LogicalPlan>,
},
SaveDateRangeReplacer {
members: Vec<LogicalPlan>,
},
OrderReplacer {
sort_expr: Vec<LogicalPlan>,
column_name_to_member: Vec<(String, String)>,
Expand Down Expand Up @@ -592,6 +595,29 @@ fn alias_expr(column: impl Display, alias: impl Display) -> String {
format!("(AliasExpr {} {})", column, alias)
}

fn case_expr_var_arg(
expr: impl Display,
when_then: impl Display,
else_expr: impl Display,
) -> String {
format!("(CaseExpr {} {} {})", expr, when_then, else_expr)
}

fn case_expr<D: Display>(when_then: Vec<(D, D)>, else_expr: impl Display) -> String {
case_expr_var_arg(
"CaseExprExpr",
list_expr(
"CaseExprWhenThenExpr",
when_then
.into_iter()
.map(|(when, then)| vec![when, then])
.flatten()
.collect(),
),
list_expr("CaseExprElseExpr", vec![else_expr]),
)
}

fn literal_string(literal_str: impl Display) -> String {
format!("(LiteralExpr LiteralExprValue:{})", literal_str)
}
Expand Down Expand Up @@ -643,6 +669,10 @@ fn filter_replacer(members: impl Display, cube: impl Display) -> String {
format!("(FilterReplacer {} {})", members, cube)
}

fn save_date_range_replacer(members: impl Display) -> String {
format!("(SaveDateRangeReplacer {})", members)
}

fn filter_cast_unwrap_replacer(members: impl Display) -> String {
format!("(FilterCastUnwrapReplacer {})", members)
}
Expand Down
110 changes: 105 additions & 5 deletions rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::{
compile::{
engine::provider::CubeContext,
rewrite::{
analysis::LogicalPlanAnalysis, between_expr, binary_expr, cast_expr, column_expr,
cube_scan, cube_scan_filters, cube_scan_filters_empty_tail, cube_scan_members,
dimension_expr, filter, filter_cast_unwrap_replacer, filter_member, filter_op,
filter_op_filters, filter_replacer, fun_expr, fun_expr_var_arg, inlist_expr,
analysis::LogicalPlanAnalysis, between_expr, binary_expr, case_expr, case_expr_var_arg,
cast_expr, column_expr, cube_scan, cube_scan_filters, cube_scan_filters_empty_tail,
cube_scan_members, dimension_expr, filter, filter_cast_unwrap_replacer, filter_member,
filter_op, filter_op_filters, filter_replacer, fun_expr, fun_expr_var_arg, inlist_expr,
is_not_null_expr, is_null_expr, limit, literal_expr, literal_string, measure_expr,
not_expr, projection, rewrite, rewriter::RewriteRules, scalar_fun_expr_args,
scalar_fun_expr_args_empty_tail, segment_member, time_dimension_date_range_replacer,
Expand Down Expand Up @@ -308,6 +308,24 @@ impl RewriteRules for FilterRules {
// TODO define zero
rewrite(
"filter-str-pos-to-like",
filter_replacer(
binary_expr(
fun_expr(
"Strpos",
vec![column_expr("?column"), literal_expr("?value")],
),
">",
literal_expr("?zero"),
),
"?cube",
),
filter_replacer(
binary_expr(column_expr("?column"), "LIKE", literal_expr("?value")),
"?cube",
),
),
rewrite(
"filter-str-lower-to-column",
filter_replacer(
binary_expr(
fun_expr(
Expand All @@ -323,10 +341,52 @@ impl RewriteRules for FilterRules {
"?cube",
),
filter_replacer(
binary_expr(column_expr("?column"), "LIKE", literal_expr("?value")),
binary_expr(
fun_expr(
"Strpos",
vec![column_expr("?column"), literal_expr("?value")],
),
">",
literal_expr("?zero"),
),
"?cube",
),
),
rewrite(
"filter-str-not-null-case-to-column",
filter_replacer(
binary_expr(
fun_expr(
"Strpos",
vec![
case_expr(
vec![(
is_not_null_expr(column_expr("?column")),
column_expr("?column"),
)],
literal_string(""),
),
literal_expr("?value"),
],
),
">",
literal_expr("?zero"),
),
"?cube",
),
filter_replacer(
binary_expr(
fun_expr(
"Strpos",
vec![column_expr("?column"), literal_expr("?value")],
),
">",
literal_expr("?zero"),
),
"?cube",
),
),
// Every expression should be handled by filter cast unwrap replacer otherwise other rules just won't work
rewrite(
"filter-cast-unwrap",
filter_cast_unwrap_replacer(cast_expr("?expr", "?data_type")),
Expand Down Expand Up @@ -390,6 +450,21 @@ impl RewriteRules for FilterRules {
filter_cast_unwrap_replacer(scalar_fun_expr_args_empty_tail()),
scalar_fun_expr_args_empty_tail(),
),
rewrite(
"filter-cast-unwrap-case-push-down",
filter_cast_unwrap_replacer(case_expr_var_arg("?expr", "?when_then", "?else")),
case_expr_var_arg(
filter_cast_unwrap_replacer("?expr"),
filter_cast_unwrap_replacer("?when_then"),
filter_cast_unwrap_replacer("?else"),
),
),
filter_unwrap_cast_push_down("CaseExprExpr"),
filter_unwrap_cast_push_down_tail("CaseExprExpr"),
filter_unwrap_cast_push_down("CaseExprWhenThenExpr"),
filter_unwrap_cast_push_down_tail("CaseExprWhenThenExpr"),
filter_unwrap_cast_push_down("CaseExprElseExpr"),
filter_unwrap_cast_push_down_tail("CaseExprElseExpr"),
rewrite(
"filter-flatten-upper-and-left",
cube_scan_filters(
Expand Down Expand Up @@ -1343,3 +1418,28 @@ fn filter_flatten_rewrite_right(
),
)
}

fn filter_unwrap_cast_push_down(
node_type: impl Display,
) -> Rewrite<LogicalPlanLanguage, LogicalPlanAnalysis> {
rewrite(
&format!("filter-cast-unwrap-{}-push-down", node_type),
filter_cast_unwrap_replacer(format!("({} ?left ?right)", node_type)),
format!(
"({} {} {})",
node_type,
filter_cast_unwrap_replacer("?left"),
filter_cast_unwrap_replacer("?right")
),
)
}

fn filter_unwrap_cast_push_down_tail(
node_type: impl Display,
) -> Rewrite<LogicalPlanLanguage, LogicalPlanAnalysis> {
rewrite(
&format!("filter-cast-unwrap-{}-empty-tail-push-down", node_type),
filter_cast_unwrap_replacer(node_type.to_string()),
node_type.to_string(),
)
}
Loading