Skip to content

Commit abd4724

Browse files
committed
refactor(cubesql): Refactor SortPushDown tests
1 parent 91e3af6 commit abd4724

8 files changed

+91
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #j1.c1, #j2.c2
6+
CrossJoin:
7+
Sort: #j1.c1 ASC NULLS LAST
8+
Projection: #j1.key, #j1.c1
9+
TableScan: j1 projection=None
10+
Projection: #j2.key, #j2.c2
11+
TableScan: j2 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #j1.c1, #j2.c2
6+
Sort: #j2.c2 ASC NULLS LAST
7+
CrossJoin:
8+
Projection: #j1.key, #j1.c1
9+
TableScan: j1 projection=None
10+
Projection: #j2.key, #j2.c2
11+
TableScan: j2 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #j1.c1, #j2.c2
6+
Inner Join: #j1.key = #j2.key
7+
Sort: #j1.c1 ASC NULLS LAST
8+
Projection: #j1.key, #j1.c1
9+
TableScan: j1 projection=None
10+
Projection: #j2.key, #j2.c2
11+
TableScan: j2 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #j1.c1, #j2.c2
6+
Sort: #j2.c2 ASC NULLS LAST
7+
Inner Join: #j1.key = #j2.key
8+
Projection: #j1.key, #j1.c1
9+
TableScan: j1 projection=None
10+
Projection: #j2.key, #j2.c2
11+
TableScan: j2 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #t3.n3, #t3.n4, #t3.n2, alias=t4
6+
Projection: #t2.n1 AS n3, #t2.c2 AS n4, #t2.n2, alias=t3
7+
Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2
8+
Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST
9+
Projection: #t1.c1, #t1.c2, #t1.c3
10+
TableScan: t1 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2
6+
Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST
7+
Projection: #t1.c1, #t1.c2, #t1.c3
8+
TableScan: t1 projection=None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: cubesql/src/compile/engine/df/optimizers/sort_push_down.rs
3+
expression: optimize(&plan)
4+
---
5+
Projection: #t3.n3, #t3.n4, #t3.n2, alias=t4
6+
Projection: #t2.n1 AS n3, #t2.c2 AS n4, #t2.n2, alias=t3
7+
Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2
8+
Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST
9+
Projection: #t1.c1, #t1.c2, #t1.c3
10+
TableScan: t1 projection=None

rust/cubesql/cubesql/src/compile/engine/df/optimizers/sort_push_down.rs

Lines changed: 19 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,10 @@ mod tests {
384384
};
385385
use datafusion::logical_plan::{col, JoinType, LogicalPlanBuilder};
386386

387-
fn optimize(plan: &LogicalPlan) -> Result<LogicalPlan> {
387+
fn optimize(plan: &LogicalPlan) -> LogicalPlan {
388388
let rule = SortPushDown::new();
389389
rule.optimize(plan, &OptimizerConfig::new())
390-
}
391-
392-
fn assert_optimized_plan_eq(plan: LogicalPlan, expected: &str) {
393-
let optimized_plan = optimize(&plan).expect("failed to optimize plan");
394-
let formatted_plan = format!("{:?}", optimized_plan);
395-
assert_eq!(formatted_plan, expected);
390+
.expect("failed to optimize plan")
396391
}
397392

398393
fn sort(expr: Expr, asc: bool, nulls_first: bool) -> Expr {
@@ -417,14 +412,7 @@ mod tests {
417412
])?
418413
.build()?;
419414

420-
let expected = "\
421-
Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2\
422-
\n Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST\
423-
\n Projection: #t1.c1, #t1.c2, #t1.c3\
424-
\n TableScan: t1 projection=None\
425-
";
426-
427-
assert_optimized_plan_eq(plan, expected);
415+
insta::assert_debug_snapshot!(optimize(&plan));
428416
Ok(())
429417
}
430418

@@ -450,16 +438,7 @@ mod tests {
450438
])?
451439
.build()?;
452440

453-
let expected = "\
454-
Projection: #t3.n3, #t3.n4, #t3.n2, alias=t4\
455-
\n Projection: #t2.n1 AS n3, #t2.c2 AS n4, #t2.n2, alias=t3\
456-
\n Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2\
457-
\n Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST\
458-
\n Projection: #t1.c1, #t1.c2, #t1.c3\
459-
\n TableScan: t1 projection=None\
460-
";
461-
462-
assert_optimized_plan_eq(plan, expected);
441+
insta::assert_debug_snapshot!(optimize(&plan));
463442
Ok(())
464443
}
465444

@@ -487,21 +466,12 @@ mod tests {
487466
])?
488467
.build()?;
489468

490-
let expected = "\
491-
Projection: #t3.n3, #t3.n4, #t3.n2, alias=t4\
492-
\n Projection: #t2.n1 AS n3, #t2.c2 AS n4, #t2.n2, alias=t3\
493-
\n Projection: #t1.c1 AS n1, #t1.c2, #t1.c3 AS n2, alias=t2\
494-
\n Sort: #t1.c2 ASC NULLS LAST, #t1.c3 DESC NULLS FIRST\
495-
\n Projection: #t1.c1, #t1.c2, #t1.c3\
496-
\n TableScan: t1 projection=None\
497-
";
498-
499-
assert_optimized_plan_eq(plan, expected);
469+
insta::assert_debug_snapshot!(optimize(&plan));
500470
Ok(())
501471
}
502472

503473
#[test]
504-
fn test_sort_down_join() -> Result<()> {
474+
fn test_sort_down_join_sort_left() -> Result<()> {
505475
let plan = LogicalPlanBuilder::from(
506476
LogicalPlanBuilder::from(make_sample_table("j1", vec!["key", "c1"], vec![])?)
507477
.project(vec![col("key"), col("c1")])?
@@ -521,18 +491,12 @@ mod tests {
521491
.sort(vec![sort(col("j1.c1"), true, false)])?
522492
.build()?;
523493

524-
let expected = "\
525-
Projection: #j1.c1, #j2.c2\
526-
\n Inner Join: #j1.key = #j2.key\
527-
\n Sort: #j1.c1 ASC NULLS LAST\
528-
\n Projection: #j1.key, #j1.c1\
529-
\n TableScan: j1 projection=None\
530-
\n Projection: #j2.key, #j2.c2\
531-
\n TableScan: j2 projection=None\
532-
";
533-
534-
assert_optimized_plan_eq(plan, expected);
494+
insta::assert_debug_snapshot!(optimize(&plan));
495+
Ok(())
496+
}
535497

498+
#[test]
499+
fn test_sort_down_join_sort_right() -> Result<()> {
536500
let plan = LogicalPlanBuilder::from(
537501
LogicalPlanBuilder::from(make_sample_table("j1", vec!["key", "c1"], vec![])?)
538502
.project(vec![col("key"), col("c1")])?
@@ -552,23 +516,12 @@ mod tests {
552516
.sort(vec![sort(col("j2.c2"), true, false)])?
553517
.build()?;
554518

555-
let expected = "\
556-
Projection: #j1.c1, #j2.c2\
557-
\n Sort: #j2.c2 ASC NULLS LAST\
558-
\n Inner Join: #j1.key = #j2.key\
559-
\n Projection: #j1.key, #j1.c1\
560-
\n TableScan: j1 projection=None\
561-
\n Projection: #j2.key, #j2.c2\
562-
\n TableScan: j2 projection=None\
563-
";
564-
565-
assert_optimized_plan_eq(plan, expected);
566-
519+
insta::assert_debug_snapshot!(optimize(&plan));
567520
Ok(())
568521
}
569522

570523
#[test]
571-
fn test_sort_down_cross_join() -> Result<()> {
524+
fn test_sort_down_cross_join_sort_left() -> Result<()> {
572525
let plan = LogicalPlanBuilder::from(
573526
LogicalPlanBuilder::from(make_sample_table("j1", vec!["key", "c1"], vec![])?)
574527
.project(vec![col("key"), col("c1")])?
@@ -583,18 +536,12 @@ mod tests {
583536
.sort(vec![sort(col("j1.c1"), true, false)])?
584537
.build()?;
585538

586-
let expected = "\
587-
Projection: #j1.c1, #j2.c2\
588-
\n CrossJoin:\
589-
\n Sort: #j1.c1 ASC NULLS LAST\
590-
\n Projection: #j1.key, #j1.c1\
591-
\n TableScan: j1 projection=None\
592-
\n Projection: #j2.key, #j2.c2\
593-
\n TableScan: j2 projection=None\
594-
";
595-
596-
assert_optimized_plan_eq(plan, expected);
539+
insta::assert_debug_snapshot!(optimize(&plan));
540+
Ok(())
541+
}
597542

543+
#[test]
544+
fn test_sort_down_cross_join_sort_right() -> Result<()> {
598545
let plan = LogicalPlanBuilder::from(
599546
LogicalPlanBuilder::from(make_sample_table("j1", vec!["key", "c1"], vec![])?)
600547
.project(vec![col("key"), col("c1")])?
@@ -609,17 +556,7 @@ mod tests {
609556
.sort(vec![sort(col("j2.c2"), true, false)])?
610557
.build()?;
611558

612-
let expected = "\
613-
Projection: #j1.c1, #j2.c2\
614-
\n Sort: #j2.c2 ASC NULLS LAST\
615-
\n CrossJoin:\
616-
\n Projection: #j1.key, #j1.c1\
617-
\n TableScan: j1 projection=None\
618-
\n Projection: #j2.key, #j2.c2\
619-
\n TableScan: j2 projection=None\
620-
";
621-
622-
assert_optimized_plan_eq(plan, expected);
559+
insta::assert_debug_snapshot!(optimize(&plan));
623560

624561
Ok(())
625562
}

0 commit comments

Comments
 (0)