This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new df999d6675 Replace println! with assert! if possible in DataFusion
examples (#11237)
df999d6675 is described below
commit df999d6675238c063ab29a4d11bffc353b271195
Author: Nishi <[email protected]>
AuthorDate: Fri Jul 5 07:55:27 2024 -0700
Replace println! with assert! if possible in DataFusion examples (#11237)
* Replace println! with assert! if possible in DataFusion examples
* Replace println! with assert! if possible in DataFusion examples
* Update rewrite_expr.rs
* port changes to other examples
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
datafusion-examples/examples/optimizer_rule.rs | 57 +++++++++++++++-----------
1 file changed, 32 insertions(+), 25 deletions(-)
diff --git a/datafusion-examples/examples/optimizer_rule.rs
b/datafusion-examples/examples/optimizer_rule.rs
index 0578529463..b4663b345f 100644
--- a/datafusion-examples/examples/optimizer_rule.rs
+++ b/datafusion-examples/examples/optimizer_rule.rs
@@ -19,7 +19,7 @@ use arrow::array::{ArrayRef, Int32Array, RecordBatch,
StringArray};
use arrow_schema::DataType;
use datafusion::prelude::SessionContext;
use datafusion_common::tree_node::{Transformed, TreeNode};
-use datafusion_common::{Result, ScalarValue};
+use datafusion_common::{assert_batches_eq, Result, ScalarValue};
use datafusion_expr::{
BinaryExpr, ColumnarValue, Expr, LogicalPlan, Operator, ScalarUDF,
ScalarUDFImpl,
Signature, Volatility,
@@ -54,39 +54,46 @@ pub async fn main() -> Result<()> {
// We can see the effect of our rewrite on the output plan that the filter
// has been rewritten to `my_eq`
- //
- // Filter: my_eq(person.age, Int32(22))
- // TableScan: person projection=[name, age]
- println!("Logical Plan:\n\n{}\n", plan.display_indent());
+ assert_eq!(
+ plan.display_indent().to_string(),
+ "Filter: my_eq(person.age, Int32(22))\
+ \n TableScan: person projection=[name, age]"
+ );
// The query below doesn't respect a filter `where age = 22` because
// the plan has been rewritten using UDF which returns always true
//
// And the output verifies the predicates have been changed (as the my_eq
// function always returns true)
- //
- // +--------+-----+
- // | name | age |
- // +--------+-----+
- // | Andy | 11 |
- // | Andrew | 22 |
- // | Oleks | 33 |
- // +--------+-----+
- ctx.sql(sql).await?.show().await?;
+ assert_batches_eq!(
+ [
+ "+--------+-----+",
+ "| name | age |",
+ "+--------+-----+",
+ "| Andy | 11 |",
+ "| Andrew | 22 |",
+ "| Oleks | 33 |",
+ "+--------+-----+",
+ ],
+ &ctx.sql(sql).await?.collect().await?
+ );
// however we can see the rule doesn't trigger for queries with predicates
// other than `=`
- //
- // +-------+-----+
- // | name | age |
- // +-------+-----+
- // | Andy | 11 |
- // | Oleks | 33 |
- // +-------+-----+
- ctx.sql("SELECT * FROM person WHERE age <> 22")
- .await?
- .show()
- .await?;
+ assert_batches_eq!(
+ [
+ "+-------+-----+",
+ "| name | age |",
+ "+-------+-----+",
+ "| Andy | 11 |",
+ "| Oleks | 33 |",
+ "+-------+-----+",
+ ],
+ &ctx.sql("SELECT * FROM person WHERE age <> 22")
+ .await?
+ .collect()
+ .await?
+ );
Ok(())
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]