Skip to content

Commit a02d5e1

Browse files
Implement Display for Expr, improve operator display (apache#971)
* # This is a combination of 3 commits. # This is the 1st commit message: Add Display for Expr::BinaryExpr # This is the commit message #2: Update logical_plan/operators tests # This is the commit message #3: rebase and debug display for non binary expr * Add Display for Expr::BinaryExpr Update logical_plan/operators tests rebase and debug display for non binary expr Add Display for Expr::BinaryExpr Update logical_plan/operators tests Updating tests Update aggregate display Updating tests without aggregate More tests Working on agg/scalar functions Fix binary_expr in create_name function and attendant tests More tests More tests Doc tests Rebase and update new tests * Submodule update * Restore submodule references from master Co-authored-by: Andrew Lamb <[email protected]>
1 parent 1c858ce commit a02d5e1

File tree

13 files changed

+220
-187
lines changed

13 files changed

+220
-187
lines changed

datafusion/src/execution/context.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,15 +1696,15 @@ mod tests {
16961696
.await?;
16971697

16981698
let expected = vec![
1699-
"+----+----+--------------+-----------------------------------+----------------------------------+------------------------------------------+--------------+----------------+--------------+--------------+--------------+",
1700-
"| c1 | c2 | ROW_NUMBER() | FIRST_VALUE(test.c2 Plus test.c1) | LAST_VALUE(test.c2 Plus test.c1) | NTH_VALUE(test.c2 Plus test.c1,Int64(1)) | SUM(test.c2) | COUNT(test.c2) | MAX(test.c2) | MIN(test.c2) | AVG(test.c2) |",
1701-
"+----+----+--------------+-----------------------------------+----------------------------------+------------------------------------------+--------------+----------------+--------------+--------------+--------------+",
1702-
"| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |",
1703-
"| 0 | 2 | 1 | 2 | 2 | 2 | 2 | 1 | 2 | 2 | 2 |",
1704-
"| 0 | 3 | 1 | 3 | 3 | 3 | 3 | 1 | 3 | 3 | 3 |",
1705-
"| 0 | 4 | 1 | 4 | 4 | 4 | 4 | 1 | 4 | 4 | 4 |",
1706-
"| 0 | 5 | 1 | 5 | 5 | 5 | 5 | 1 | 5 | 5 | 5 |",
1707-
"+----+----+--------------+-----------------------------------+----------------------------------+------------------------------------------+--------------+----------------+--------------+--------------+--------------+",
1699+
"+----+----+--------------+--------------------------------+-------------------------------+---------------------------------------+--------------+----------------+--------------+--------------+--------------+",
1700+
"| c1 | c2 | ROW_NUMBER() | FIRST_VALUE(test.c2 + test.c1) | LAST_VALUE(test.c2 + test.c1) | NTH_VALUE(test.c2 + test.c1,Int64(1)) | SUM(test.c2) | COUNT(test.c2) | MAX(test.c2) | MIN(test.c2) | AVG(test.c2) |",
1701+
"+----+----+--------------+--------------------------------+-------------------------------+---------------------------------------+--------------+----------------+--------------+--------------+--------------+",
1702+
"| 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |",
1703+
"| 0 | 2 | 1 | 2 | 2 | 2 | 2 | 1 | 2 | 2 | 2 |",
1704+
"| 0 | 3 | 1 | 3 | 3 | 3 | 3 | 1 | 3 | 3 | 3 |",
1705+
"| 0 | 4 | 1 | 4 | 4 | 4 | 4 | 1 | 4 | 4 | 4 |",
1706+
"| 0 | 5 | 1 | 5 | 5 | 5 | 5 | 1 | 5 | 5 | 5 |",
1707+
"+----+----+--------------+--------------------------------+-------------------------------+---------------------------------------+--------------+----------------+--------------+--------------+--------------+",
17081708
];
17091709

17101710
// window function shall respect ordering

datafusion/src/logical_plan/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ mod tests {
663663
.build()?;
664664

665665
let expected = "Projection: #employee_csv.id\
666-
\n Filter: #employee_csv.state Eq Utf8(\"CO\")\
666+
\n Filter: #employee_csv.state = Utf8(\"CO\")\
667667
\n TableScan: employee_csv projection=Some([0, 3])";
668668

669669
assert_eq!(expected, format!("{:?}", plan));

datafusion/src/logical_plan/expr.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,33 @@ impl Not for Expr {
941941
}
942942
}
943943

944+
impl std::fmt::Display for Expr {
945+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
946+
match self {
947+
Expr::BinaryExpr {
948+
ref left,
949+
ref right,
950+
ref op,
951+
} => write!(f, "{} {} {}", left, op, right),
952+
Expr::AggregateFunction {
953+
/// Name of the function
954+
ref fun,
955+
/// List of expressions to feed to the functions as arguments
956+
ref args,
957+
/// Whether this is a DISTINCT aggregation or not
958+
ref distinct,
959+
} => fmt_function(f, &fun.to_string(), *distinct, args, true),
960+
Expr::ScalarFunction {
961+
/// Name of the function
962+
ref fun,
963+
/// List of expressions to feed to the functions as arguments
964+
ref args,
965+
} => fmt_function(f, &fun.to_string(), false, args, true),
966+
_ => write!(f, "{:?}", self),
967+
}
968+
}
969+
}
970+
944971
#[allow(clippy::boxed_local)]
945972
fn rewrite_boxed<R>(boxed_expr: Box<Expr>, rewriter: &mut R) -> Result<Box<Expr>>
946973
where
@@ -1603,8 +1630,14 @@ fn fmt_function(
16031630
fun: &str,
16041631
distinct: bool,
16051632
args: &[Expr],
1633+
display: bool,
16061634
) -> fmt::Result {
1607-
let args: Vec<String> = args.iter().map(|arg| format!("{:?}", arg)).collect();
1635+
let args: Vec<String> = match display {
1636+
true => args.iter().map(|arg| format!("{}", arg)).collect(),
1637+
false => args.iter().map(|arg| format!("{:?}", arg)).collect(),
1638+
};
1639+
1640+
// let args: Vec<String> = args.iter().map(|arg| format!("{:?}", arg)).collect();
16081641
let distinct_str = match distinct {
16091642
true => "DISTINCT ",
16101643
false => "",
@@ -1648,7 +1681,7 @@ impl fmt::Debug for Expr {
16481681
Expr::IsNull(expr) => write!(f, "{:?} IS NULL", expr),
16491682
Expr::IsNotNull(expr) => write!(f, "{:?} IS NOT NULL", expr),
16501683
Expr::BinaryExpr { left, op, right } => {
1651-
write!(f, "{:?} {:?} {:?}", left, op, right)
1684+
write!(f, "{:?} {} {:?}", left, op, right)
16521685
}
16531686
Expr::Sort {
16541687
expr,
@@ -1667,10 +1700,10 @@ impl fmt::Debug for Expr {
16671700
}
16681701
}
16691702
Expr::ScalarFunction { fun, args, .. } => {
1670-
fmt_function(f, &fun.to_string(), false, args)
1703+
fmt_function(f, &fun.to_string(), false, args, false)
16711704
}
16721705
Expr::ScalarUDF { fun, ref args, .. } => {
1673-
fmt_function(f, &fun.name, false, args)
1706+
fmt_function(f, &fun.name, false, args, false)
16741707
}
16751708
Expr::WindowFunction {
16761709
fun,
@@ -1679,7 +1712,7 @@ impl fmt::Debug for Expr {
16791712
order_by,
16801713
window_frame,
16811714
} => {
1682-
fmt_function(f, &fun.to_string(), false, args)?;
1715+
fmt_function(f, &fun.to_string(), false, args, false)?;
16831716
if !partition_by.is_empty() {
16841717
write!(f, " PARTITION BY {:?}", partition_by)?;
16851718
}
@@ -1702,9 +1735,9 @@ impl fmt::Debug for Expr {
17021735
distinct,
17031736
ref args,
17041737
..
1705-
} => fmt_function(f, &fun.to_string(), *distinct, args),
1738+
} => fmt_function(f, &fun.to_string(), *distinct, args, true),
17061739
Expr::AggregateUDF { fun, ref args, .. } => {
1707-
fmt_function(f, &fun.name, false, args)
1740+
fmt_function(f, &fun.name, false, args, false)
17081741
}
17091742
Expr::Between {
17101743
expr,
@@ -1762,7 +1795,7 @@ fn create_name(e: &Expr, input_schema: &DFSchema) -> Result<String> {
17621795
Expr::BinaryExpr { left, op, right } => {
17631796
let left = create_name(left, input_schema)?;
17641797
let right = create_name(right, input_schema)?;
1765-
Ok(format!("{} {:?} {}", left, op, right))
1798+
Ok(format!("{} {} {}", left, op, right))
17661799
}
17671800
Expr::Case {
17681801
expr,
@@ -1925,12 +1958,12 @@ mod tests {
19251958
assert_eq!(
19261959
rewriter.v,
19271960
vec![
1928-
"Previsited #state Eq Utf8(\"CO\")",
1961+
"Previsited #state = Utf8(\"CO\")",
19291962
"Previsited #state",
19301963
"Mutated #state",
19311964
"Previsited Utf8(\"CO\")",
19321965
"Mutated Utf8(\"CO\")",
1933-
"Mutated #state Eq Utf8(\"CO\")"
1966+
"Mutated #state = Utf8(\"CO\")"
19341967
]
19351968
)
19361969
}

datafusion/src/logical_plan/operators.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,19 @@ mod tests {
129129
fn test_operators() {
130130
assert_eq!(
131131
format!("{:?}", lit(1u32) + lit(2u32)),
132-
"UInt32(1) Plus UInt32(2)"
132+
"UInt32(1) + UInt32(2)"
133133
);
134134
assert_eq!(
135135
format!("{:?}", lit(1u32) - lit(2u32)),
136-
"UInt32(1) Minus UInt32(2)"
136+
"UInt32(1) - UInt32(2)"
137137
);
138138
assert_eq!(
139139
format!("{:?}", lit(1u32) * lit(2u32)),
140-
"UInt32(1) Multiply UInt32(2)"
140+
"UInt32(1) * UInt32(2)"
141141
);
142142
assert_eq!(
143143
format!("{:?}", lit(1u32) / lit(2u32)),
144-
"UInt32(1) Divide UInt32(2)"
144+
"UInt32(1) / UInt32(2)"
145145
);
146146
}
147147
}

datafusion/src/logical_plan/plan.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl LogicalPlan {
551551
/// // Format using display_indent
552552
/// let display_string = format!("{}", plan.display_indent());
553553
///
554-
/// assert_eq!("Filter: #foo_csv.id Eq Int32(5)\
554+
/// assert_eq!("Filter: #foo_csv.id = Int32(5)\
555555
/// \n TableScan: foo_csv projection=None",
556556
/// display_string);
557557
/// ```
@@ -575,7 +575,7 @@ impl LogicalPlan {
575575
///
576576
/// ```text
577577
/// Projection: #employee.id [id:Int32]\
578-
/// Filter: #employee.state Eq Utf8(\"CO\") [id:Int32, state:Utf8]\
578+
/// Filter: #employee.state = Utf8(\"CO\") [id:Int32, state:Utf8]\
579579
/// TableScan: employee projection=Some([0, 3]) [id:Int32, state:Utf8]";
580580
/// ```
581581
///
@@ -592,7 +592,7 @@ impl LogicalPlan {
592592
/// // Format using display_indent_schema
593593
/// let display_string = format!("{}", plan.display_indent_schema());
594594
///
595-
/// assert_eq!("Filter: #foo_csv.id Eq Int32(5) [id:Int32]\
595+
/// assert_eq!("Filter: #foo_csv.id = Int32(5) [id:Int32]\
596596
/// \n TableScan: foo_csv projection=None [id:Int32]",
597597
/// display_string);
598598
/// ```
@@ -939,7 +939,7 @@ mod tests {
939939
let plan = display_plan();
940940

941941
let expected = "Projection: #employee_csv.id\
942-
\n Filter: #employee_csv.state Eq Utf8(\"CO\")\
942+
\n Filter: #employee_csv.state = Utf8(\"CO\")\
943943
\n TableScan: employee_csv projection=Some([0, 3])";
944944

945945
assert_eq!(expected, format!("{}", plan.display_indent()));
@@ -950,7 +950,7 @@ mod tests {
950950
let plan = display_plan();
951951

952952
let expected = "Projection: #employee_csv.id [id:Int32]\
953-
\n Filter: #employee_csv.state Eq Utf8(\"CO\") [id:Int32, state:Utf8]\
953+
\n Filter: #employee_csv.state = Utf8(\"CO\") [id:Int32, state:Utf8]\
954954
\n TableScan: employee_csv projection=Some([0, 3]) [id:Int32, state:Utf8]";
955955

956956
assert_eq!(expected, format!("{}", plan.display_indent_schema()));

datafusion/src/optimizer/common_subexpr_eliminate.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ mod test {
714714
)?
715715
.build()?;
716716

717-
let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(#BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a AS test.a Multiply Int32(1) Minus test.b), SUM(#BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a AS test.a Multiply Int32(1) Minus test.b Multiply Int32(1) Plus #test.c)]]\
718-
\n Projection: #test.a Multiply Int32(1) Minus #test.b AS BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a, #test.a, #test.b, #test.c\
717+
let expected = "Aggregate: groupBy=[[]], aggr=[[SUM(#BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a AS test.a * Int32(1) - test.b), SUM(#BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a AS test.a * Int32(1) - test.b * Int32(1) + #test.c)]]\
718+
\n Projection: #test.a * Int32(1) - #test.b AS BinaryExpr-*BinaryExpr--Column-test.bLiteral1Column-test.a, #test.a, #test.b, #test.c\
719719
\n TableScan: test projection=None";
720720

721721
assert_optimized_plan_eq(&plan, expected);
@@ -737,7 +737,7 @@ mod test {
737737
)?
738738
.build()?;
739739

740-
let expected = "Aggregate: groupBy=[[]], aggr=[[Int32(1) Plus #AggregateFunction-AVGfalseColumn-test.a AS AVG(test.a), Int32(1) Minus #AggregateFunction-AVGfalseColumn-test.a AS AVG(test.a)]]\
740+
let expected = "Aggregate: groupBy=[[]], aggr=[[Int32(1) + #AggregateFunction-AVGfalseColumn-test.a AS AVG(test.a), Int32(1) - #AggregateFunction-AVGfalseColumn-test.a AS AVG(test.a)]]\
741741
\n Projection: AVG(#test.a) AS AggregateFunction-AVGfalseColumn-test.a, #test.a, #test.b, #test.c\
742742
\n TableScan: test projection=None";
743743

@@ -757,8 +757,8 @@ mod test {
757757
])?
758758
.build()?;
759759

760-
let expected = "Projection: #BinaryExpr-+Column-test.aLiteral1 AS Int32(1) Plus test.a AS first, #BinaryExpr-+Column-test.aLiteral1 AS Int32(1) Plus test.a AS second\
761-
\n Projection: Int32(1) Plus #test.a AS BinaryExpr-+Column-test.aLiteral1, #test.a, #test.b, #test.c\
760+
let expected = "Projection: #BinaryExpr-+Column-test.aLiteral1 AS Int32(1) + test.a AS first, #BinaryExpr-+Column-test.aLiteral1 AS Int32(1) + test.a AS second\
761+
\n Projection: Int32(1) + #test.a AS BinaryExpr-+Column-test.aLiteral1, #test.a, #test.b, #test.c\
762762
\n TableScan: test projection=None";
763763

764764
assert_optimized_plan_eq(&plan, expected);
@@ -777,7 +777,7 @@ mod test {
777777
])?
778778
.build()?;
779779

780-
let expected = "Projection: Int32(1) Plus #test.a, #test.a Plus Int32(1)\
780+
let expected = "Projection: Int32(1) + #test.a, #test.a + Int32(1)\
781781
\n TableScan: test projection=None";
782782

783783
assert_optimized_plan_eq(&plan, expected);
@@ -794,8 +794,8 @@ mod test {
794794
.project(vec![binary_expr(lit(1), Operator::Plus, col("a"))])?
795795
.build()?;
796796

797-
let expected = "Projection: #Int32(1) Plus test.a\
798-
\n Projection: Int32(1) Plus #test.a\
797+
let expected = "Projection: #Int32(1) + test.a\
798+
\n Projection: Int32(1) + #test.a\
799799
\n TableScan: test projection=None";
800800

801801
assert_optimized_plan_eq(&plan, expected);

datafusion/src/optimizer/constant_folding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ mod tests {
592592

593593
let expected = "\
594594
Projection: #test.a\
595-
\n Filter: NOT #test.b And #test.c\
595+
\n Filter: NOT #test.b AND #test.c\
596596
\n TableScan: test projection=None";
597597

598598
assert_optimized_plan_eq(&plan, expected);
@@ -609,7 +609,7 @@ mod tests {
609609

610610
let expected = "\
611611
Projection: #test.a\
612-
\n Filter: NOT #test.b Or NOT #test.c\
612+
\n Filter: NOT #test.b OR NOT #test.c\
613613
\n TableScan: test projection=None";
614614

615615
assert_optimized_plan_eq(&plan, expected);

0 commit comments

Comments
 (0)