This example demonstrates the DataFusion [`Expr`] API. DataFusion comes with a powerful and extensive system for representing and manipulating expressions such as `A + 5` and `X IN ('foo', 'bar', 'baz')`. In addition to building and manipulating [`Expr`]s, DataFusion also comes with APIs for evaluation, simplification, and analysis. The code in this example shows how to: 1. Create [`Expr`]s usi
()
| 58 | /// 6. Get the types of the expressions: [`expression_type_demo`] |
| 59 | /// 7. Apply type coercion to expressions: [`type_coercion_demo`] |
| 60 | pub async fn expr_api() -> Result<()> { |
| 61 | // The easiest way to do create expressions is to use the |
| 62 | // "fluent"-style API: |
| 63 | let expr = col("a") + lit(5); |
| 64 | |
| 65 | // The same same expression can be created directly, with much more code: |
| 66 | let expr2 = Expr::BinaryExpr(BinaryExpr::new( |
| 67 | Box::new(col("a")), |
| 68 | Operator::Plus, |
| 69 | Box::new(Expr::Literal(ScalarValue::Int32(Some(5)), None)), |
| 70 | )); |
| 71 | assert_eq!(expr, expr2); |
| 72 | |
| 73 | // See how to build aggregate functions with the expr_fn API |
| 74 | expr_fn_demo()?; |
| 75 | |
| 76 | // See how to evaluate expressions |
| 77 | evaluate_demo()?; |
| 78 | |
| 79 | // See how to simplify expressions |
| 80 | simplify_demo()?; |
| 81 | |
| 82 | // See how to analyze ranges in expressions |
| 83 | range_analysis_demo()?; |
| 84 | |
| 85 | // See how to analyze boundaries in different kinds of expressions. |
| 86 | boundary_analysis_and_selectivity_demo()?; |
| 87 | |
| 88 | // See how boundary analysis works for `AND` & `OR` conjunctions. |
| 89 | boundary_analysis_in_conjunctions_demo()?; |
| 90 | |
| 91 | // See how to determine the data types of expressions |
| 92 | expression_type_demo()?; |
| 93 | |
| 94 | // See how to type coerce expressions. |
| 95 | type_coercion_demo()?; |
| 96 | |
| 97 | Ok(()) |
| 98 | } |
| 99 | |
| 100 | /// DataFusion's `expr_fn` API makes it easy to create [`Expr`]s for the |
| 101 | /// full range of expression types such as aggregates and window functions. |
no test coverage detected
searching dependent graphs…