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

fix(cubesql): Enable constant folding for unary minus exprs #7707

Merged
merged 1 commit into from
Jan 31, 2024
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
34 changes: 34 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21136,4 +21136,38 @@ limit
assert!(sql.contains("order_date"));
assert!(sql.contains("EXTRACT(DAY FROM"))
}

#[tokio::test]
async fn test_unary_minus_constant_folding() {
if !Rewriter::sql_push_down_enabled() {
return;
}
init_logger();

let query_plan = convert_select_to_query_plan(
r#"
SELECT order_date + (-EXTRACT(YEAR FROM order_date) * INTERVAL '1 day') AS t
FROM KibanaSampleDataEcommerce
GROUP BY 1
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await;

let physical_plan = query_plan.as_physical_plan().await.unwrap();
println!(
"Physical plan: {}",
displayable(physical_plan.as_ref()).indent()
);

let logical_plan = query_plan.as_logical_plan();
let sql = logical_plan
.find_cube_scan_wrapper()
.wrapped_sql
.unwrap()
.sql;
assert!(sql.contains("-(EXTRACT(YEAR FROM"));
assert!(sql.contains("* INTERVAL '1 DAY'"));
}
}
2 changes: 1 addition & 1 deletion rust/cubesql/cubesql/src/compile/rewrite/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ impl LogicalPlanAnalysis {
// TODO In case multiple node variant exists ConstantFolding::List will choose one which contains actual constants.
Some(ConstantFolding::List(list))
}
LogicalPlanLanguage::AnyExpr(_) => {
LogicalPlanLanguage::AnyExpr(_) | LogicalPlanLanguage::NegativeExpr(_) => {
let expr = node_to_expr(
enode,
&egraph.analysis.cube_context,
Expand Down
2 changes: 1 addition & 1 deletion rust/cubesql/cubesql/src/compile/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ pub fn get_test_tenant_ctx_customized(custom_templates: Vec<(String, String)>) -
"expressions/column_aliased".to_string(),
"{{expr}} {{quoted_alias}}".to_string(),
),
("expressions/binary".to_string(), "{{ left }} {{ op }} {{ right }}".to_string()),
("expressions/binary".to_string(), "({{ left }} {{ op }} {{ right }})".to_string()),
("expressions/is_null".to_string(), "{{ expr }} IS {% if negate %}NOT {% endif %}NULL".to_string()),
("expressions/case".to_string(), "CASE{% if expr %}{{ expr }} {% endif %}{% for when, then in when_then %} WHEN {{ when }} THEN {{ then }}{% endfor %}{% if else_expr %} ELSE {{ else_expr }}{% endif %} END".to_string()),
("expressions/sort".to_string(), "{{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}{% if nulls_first %} NULLS FIRST {% endif %}".to_string()),
Expand Down
Loading