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): Avoid constant folding for current_date() function duri… #7498

Merged
merged 3 commits into from
Dec 7, 2023
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
10 changes: 10 additions & 0 deletions rust/cubesql/cubesql/src/compile/engine/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4787,5 +4787,15 @@ pub fn register_fun_stubs(mut ctx: SessionContext) -> SessionContext {
rettyp = Utf8
);

register_fun_stub!(
udf,
"eval_current_date",
argc = 0,
rettyp = Date32,
vol = Stable
);

register_fun_stub!(udf, "eval_now", argc = 0, rettyp = Timestamp, vol = Stable);

ctx
}
31 changes: 30 additions & 1 deletion rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11749,7 +11749,7 @@ ORDER BY \"COUNT(count)\" DESC"

assert_eq!(
logical_plan,
"Projection: Date32(\"0\") AS COL\
"Projection: currentdate() AS COL\
\n EmptyRelation",
);

Expand Down Expand Up @@ -19277,6 +19277,35 @@ ORDER BY \"COUNT(count)\" DESC"
);
}

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

let query_plan = convert_select_to_query_plan(
"SELECT SUM(CAST(FLOOR(EXTRACT(EPOCH FROM CAST(CURRENT_DATE() AS TIMESTAMP)) / 86400) - FLOOR(EXTRACT(EPOCH FROM CAST(order_date AS TIMESTAMP)) / 86400) AS BIGINT)) FROM KibanaSampleDataEcommerce a"
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await;

let logical_plan = query_plan.as_logical_plan();
assert!(logical_plan
.find_cube_scan_wrapper()
.wrapped_sql
.unwrap()
.sql
.contains("CURRENT_DATE()"));

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

#[tokio::test]
async fn test_thoughtspot_pg_date_trunc_year() {
init_logger();
Expand Down
50 changes: 37 additions & 13 deletions rust/cubesql/cubesql/src/compile/rewrite/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use datafusion::{
},
logical_plan::{Column, DFSchema, Expr},
physical_plan::{
functions::Volatility, planner::DefaultPhysicalPlanner, ColumnarValue, PhysicalPlanner,
functions::{BuiltinScalarFunction, Volatility},
planner::DefaultPhysicalPlanner,
ColumnarValue, PhysicalPlanner,
},
scalar::ScalarValue,
};
Expand Down Expand Up @@ -486,7 +488,23 @@ impl LogicalPlanAnalysis {
)
.ok()?;
if let Expr::ScalarUDF { fun, .. } = &expr {
if &fun.name == "str_to_date"
if &fun.name == "eval_now" {
Self::eval_constant_expr(
&egraph,
&Expr::ScalarFunction {
fun: BuiltinScalarFunction::Now,
args: vec![],
},
)
} else if &fun.name == "eval_current_date" {
Self::eval_constant_expr(
&egraph,
&Expr::ScalarFunction {
fun: BuiltinScalarFunction::CurrentDate,
args: vec![],
},
)
} else if &fun.name == "str_to_date"
|| &fun.name == "date_add"
|| &fun.name == "date_sub"
|| &fun.name == "date"
Expand All @@ -511,8 +529,12 @@ impl LogicalPlanAnalysis {
.ok()?;

if let Expr::ScalarFunction { fun, .. } = &expr {
if fun.volatility() == Volatility::Immutable
|| fun.volatility() == Volatility::Stable
if (fun.volatility() == Volatility::Immutable
|| fun.volatility() == Volatility::Stable)
&& !matches!(
fun,
BuiltinScalarFunction::CurrentDate | BuiltinScalarFunction::Now
)
{
Self::eval_constant_expr(&egraph, &expr)
} else {
Expand Down Expand Up @@ -562,6 +584,7 @@ impl LogicalPlanAnalysis {
Expr::Literal(ScalarValue::Utf8(value)) => match (value, data_type) {
// Timezone set in Config
(Some(_), DataType::Timestamp(_, _)) => (),
(Some(_), DataType::Date32 | DataType::Date64) => (),
_ => return None,
},
_ => (),
Expand Down Expand Up @@ -807,15 +830,16 @@ impl Analysis<LogicalPlanLanguage> for LogicalPlanAnalysis {
if let Some(ConstantFolding::Scalar(c)) = &egraph[id].data.constant {
// TODO: ideally all constants should be aliased, but this requires
// rewrites to extract `.data.constant` instead of `literal_expr`.
let alias_name = if c.is_null() {
egraph[id]
.data
.original_expr
.as_ref()
.map(|expr| expr.name(&DFSchema::empty()).unwrap())
} else {
None
};
let alias_name =
if c.is_null() || matches!(c, ScalarValue::Date32(_) | ScalarValue::Date64(_)) {
egraph[id]
.data
.original_expr
.as_ref()
.map(|expr| expr.name(&DFSchema::empty()).unwrap())
} else {
None
};
let c = c.clone();
let value = egraph.add(LogicalPlanLanguage::LiteralExprValue(LiteralExprValue(c)));
let literal_expr = egraph.add(LogicalPlanLanguage::LiteralExpr([value]));
Expand Down
6 changes: 3 additions & 3 deletions rust/cubesql/cubesql/src/compile/rewrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ crate::plan_to_language! {
members: Vec<LogicalPlan>,
aliases: Vec<(String, String)>,
},
FilterCastUnwrapReplacer {
FilterSimplifyReplacer {
filters: Vec<LogicalPlan>,
},
OrderReplacer {
Expand Down Expand Up @@ -1131,8 +1131,8 @@ fn filter_replacer(
)
}

fn filter_cast_unwrap_replacer(members: impl Display) -> String {
format!("(FilterCastUnwrapReplacer {})", members)
fn filter_simplify_replacer(members: impl Display) -> String {
format!("(FilterSimplifyReplacer {})", members)
}

fn inner_aggregate_split_replacer(members: impl Display, alias_to_cube: impl Display) -> String {
Expand Down
Loading