Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export class ClickHouseQuery extends BaseQuery {
// ClickHouse intervals have a distinct type for each granularity
delete templates.types.interval;
delete templates.types.binary;
templates.expressions.is_not_distinct_from = 'isNotDistinctFrom({{ left }}, {{ right }})';
return templates;
}
}
120 changes: 120 additions & 0 deletions rust/cubesqlplanner/cubesqlplanner/src/planner/sql_templates/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ impl PlanSqlTemplates {

return self.binary_expr(left_column, &is_not_distinct_from_op, right_column);
}
if self.supports_is_not_distinct_from_expr() {
return self.render.render_template(
"expressions/is_not_distinct_from",
context! { left => left_column.as_str(), right => right_column.as_str() },
);
}
format!(
" OR ({} AND {})",
self.is_null_expr(&left_column, false)?,
Expand All @@ -486,6 +492,11 @@ impl PlanSqlTemplates {
.contains_template("operators/is_not_distinct_from")
}

pub fn supports_is_not_distinct_from_expr(&self) -> bool {
self.render
.contains_template("expressions/is_not_distinct_from")
}

pub fn supports_generated_time_series(
&self,
predifined_granularity: bool,
Expand Down Expand Up @@ -798,3 +809,112 @@ impl PlanSqlTemplates {
)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_fixtures::cube_bridge::{MockDriverTools, MockSqlTemplatesRender};
use std::collections::HashMap;

fn plan_templates_with(extra_templates: Vec<(&str, &str)>) -> PlanSqlTemplates {
if extra_templates.is_empty() {
let driver_tools = Rc::new(MockDriverTools::new());
return PlanSqlTemplates::try_new(driver_tools, false).unwrap();
}
// Build a minimal template set with only what join_by_dimension_conditions needs
let mut t: HashMap<String, String> = HashMap::new();
t.insert(
"expressions/binary".to_string(),
"({{ left }} {{ op }} {{ right }})".to_string(),
);
t.insert(
"expressions/is_null".to_string(),
"({{ expr }} IS {% if negate %}NOT {% endif %}NULL)".to_string(),
);
for (k, v) in extra_templates {
t.insert(k.to_string(), v.to_string());
}
let render = MockSqlTemplatesRender::try_new(t).unwrap();
let driver_tools = Rc::new(MockDriverTools::with_sql_templates(render));
PlanSqlTemplates::try_new(driver_tools, false).unwrap()
}

#[test]
fn test_join_condition_no_null_check() {
let templates = plan_templates_with(vec![]);
let left = "t1.col".to_string();
let right = "t2.col".to_string();

let result = templates
.join_by_dimension_conditions(&left, &right, false)
.unwrap();
assert_eq!(result, "(t1.col = t2.col)");
}

#[test]
fn test_join_condition_null_check_fallback_or_is_null() {
// No is_not_distinct_from templates → falls back to OR (IS NULL AND IS NULL)
let templates = plan_templates_with(vec![]);
let left = "t1.col".to_string();
let right = "t2.col".to_string();

let result = templates
.join_by_dimension_conditions(&left, &right, true)
.unwrap();
assert_eq!(
result,
"(t1.col = t2.col OR ((t1.col IS NULL) AND (t2.col IS NULL)))"
);
}

#[test]
fn test_join_condition_null_check_binary_operator() {
// Postgres/BigQuery/Snowflake style: binary operator IS NOT DISTINCT FROM
let templates = plan_templates_with(vec![(
"operators/is_not_distinct_from",
"IS NOT DISTINCT FROM",
)]);
let left = "t1.col".to_string();
let right = "t2.col".to_string();

let result = templates
.join_by_dimension_conditions(&left, &right, true)
.unwrap();
assert_eq!(result, "(t1.col IS NOT DISTINCT FROM t2.col)");
}

#[test]
fn test_join_condition_null_check_expression_template() {
// ClickHouse style: function-call isNotDistinctFrom(left, right)
let templates = plan_templates_with(vec![(
"expressions/is_not_distinct_from",
"isNotDistinctFrom({{ left }}, {{ right }})",
)]);
let left = "t1.col".to_string();
let right = "t2.col".to_string();

let result = templates
.join_by_dimension_conditions(&left, &right, true)
.unwrap();
assert_eq!(result, "isNotDistinctFrom(t1.col, t2.col)");
}

#[test]
fn test_join_condition_binary_operator_takes_precedence_over_expression() {
// When both templates exist, the binary operator should be used
let templates = plan_templates_with(vec![
("operators/is_not_distinct_from", "IS NOT DISTINCT FROM"),
(
"expressions/is_not_distinct_from",
"isNotDistinctFrom({{ left }}, {{ right }})",
),
]);
let left = "t1.col".to_string();
let right = "t2.col".to_string();

let result = templates
.join_by_dimension_conditions(&left, &right, true)
.unwrap();
assert_eq!(result, "(t1.col IS NOT DISTINCT FROM t2.col)");
}
}
Loading