Skip to content

Commit 506d9b4

Browse files
committed
Handle joins with no overlapping variables
1 parent 230501a commit 506d9b4

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/graphfusion-engine/src/sparql/rewriting/graph_pattern_rewriter.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ fn create_join(
702702
.map(|c| c.name().to_string())
703703
.collect();
704704

705+
// If both solutions are disjoint, we can use a cross join.
706+
if lhs_keys.is_disjoint(&rhs_keys) && filter.is_none() {
707+
return lhs.cross_join(rhs.build()?);
708+
}
709+
705710
let mut join_schema = lhs.schema().as_ref().clone();
706711
join_schema.merge(rhs.schema());
707712

@@ -869,10 +874,22 @@ fn create_distinct_on_expr(
869874
.collect());
870875
};
871876

872-
let mut on_exprs = create_initial_columns_from_sort(sort_exprs)?;
877+
let mut on_exprs = Vec::new();
878+
879+
// TODO: This should be easier to do.
880+
let on_exprs_order = create_initial_columns_from_sort(sort_exprs)?;
881+
for on_expr in &on_exprs_order {
882+
let column = schema
883+
.columns()
884+
.into_iter()
885+
.find(|c| c.name() == on_expr)
886+
.expect("Column must exist");
887+
on_exprs.push(column);
888+
}
889+
873890
for column in schema.columns() {
874891
if !on_exprs.contains(&column) {
875-
on_exprs.push(column.clone());
892+
on_exprs.push(column);
876893
}
877894
}
878895

@@ -885,11 +902,11 @@ fn create_distinct_on_expr(
885902
/// When creating a DISTINCT ON node, the initial `on_expr` expressions must match the given
886903
/// `sort_expr` (if they exist). This function creates these initial columns from the order
887904
/// expressions.
888-
fn create_initial_columns_from_sort(sort_exprs: &Vec<OrderExpression>) -> DFResult<Vec<Column>> {
905+
fn create_initial_columns_from_sort(sort_exprs: &Vec<OrderExpression>) -> DFResult<Vec<String>> {
889906
sort_exprs
890907
.iter()
891908
.map(|sort_expr| match sort_expr.expression() {
892-
Expression::Variable(var) => Ok(Column::new_unqualified(var.as_str())),
909+
Expression::Variable(var) => Ok(var.as_str().to_owned()),
893910
_ => plan_err!(
894911
"Expression {} not supported for ORDER BY in combination with DISTINCT.",
895912
sort_expr

lib/graphfusion-logical/src/patterns/logical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl PatternNode {
4646

4747
let fields = fields
4848
.into_iter()
49-
.map(|name| Field::new(name, EncTerm::data_type(), false))
49+
.map(|name| Field::new(name, EncTerm::data_type(), true))
5050
.collect::<Fields>();
5151
Arc::new(DFSchema::from_unqualified_fields(fields, HashMap::new()).expect("Names correct"))
5252
}

0 commit comments

Comments
 (0)