alamb commented on code in PR #9118:
URL: https://github.com/apache/arrow-datafusion/pull/9118#discussion_r1477288992


##########
datafusion-examples/examples/expr_api.rs:
##########
@@ -68,6 +71,9 @@ async fn main() -> Result<()> {
     // See how to analyze ranges in expressions
     range_analysis_demo()?;
 
+    // See how to get the type of the expression

Review Comment:
   ```suggestion
       // See how to determine the data types of expressions
   ```



##########
docs/source/library-user-guide/working-with-exprs.md:
##########
@@ -180,6 +180,34 @@ Projection: Int64(1) + Int64(1) AS added_one
 
 I.e. the `add_one` UDF has been inlined into the projection.
 
+## Getting the data type of the expression
+
+The `arrow::datatypes::DataType` of the expression can be obtained by calling 
the `get_type` given the schema

Review Comment:
   ```suggestion
   The `arrow::datatypes::DataType` of the expression can be obtained by 
calling the `get_type` given something that implements `Expr::Schemable`, for 
example a `DFschema` object:
   ```



##########
datafusion-examples/examples/expr_api.rs:
##########
@@ -256,3 +262,38 @@ pub fn physical_expr(schema: &Schema, expr: Expr) -> 
Result<Arc<dyn PhysicalExpr
 
     create_physical_expr(&expr, df_schema.as_ref(), &props)
 }
+
+fn expression_type_demo() -> Result<()> {
+    let expr = col("c");
+
+    // Using a schema where the column `foo` is of type Utf8

Review Comment:
   ```suggestion
       // To determine the DataType of an expression, DataFusion must know the 
       // types of the input expressions. You can provide this information 
using 
       // a schema. In this case we create a schema where the column `foo` is 
of 
       // type Utf8 (a String / VARCHAR)
   ```



##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -58,6 +58,60 @@ impl ExprSchemable for Expr {
     ///
     /// Note: [DFSchema] implements [ExprSchema].
     ///
+    /// # Examples
+    ///
+    /// ## Get the type of a single column expression using different schemas
+    ///
+    /// ```
+    /// # use arrow::datatypes::DataType;
+    /// # use datafusion_common::{DFField, DFSchema};
+    /// # use datafusion_expr::{col, ExprSchemable};
+    /// # use std::collections::HashMap;
+    ///
+    /// fn main() {
+    ///   let expr = col("c");
+    ///
+    ///   // Using a schema where the column `c` is of type Utf8
+    ///   let schema = DFSchema::new_with_metadata(
+    ///     vec![DFField::new_unqualified("c", DataType::Utf8, true)],
+    ///     HashMap::new(),
+    ///   )
+    ///   .unwrap();
+    ///   assert_eq!("Utf8", format!("{}", expr.get_type(&schema).unwrap()));
+    ///
+    ///   // Using a schema where the column `c` is of type Int32
+    ///   let schema = DFSchema::new_with_metadata(
+    ///     vec![DFField::new_unqualified("c", DataType::Int32, true)],
+    ///     HashMap::new(),
+    ///   )
+    ///   .unwrap();
+    ///   assert_eq!("Int32", format!("{}", expr.get_type(&schema).unwrap()));
+    /// }
+    /// ```
+    ///
+    /// ## Get the type of an expression that adds 2 columns. Adding an Int32

Review Comment:
   I personally suggest we only keep one of the examples in the API docs (as I 
think the key point of the API is to show using a DF schema as `ExprSchemable`) 
   
   Perhaps we only keep the example of adding two numbers together?



##########
datafusion-examples/examples/expr_api.rs:
##########
@@ -256,3 +262,38 @@ pub fn physical_expr(schema: &Schema, expr: Expr) -> 
Result<Arc<dyn PhysicalExpr
 
     create_physical_expr(&expr, df_schema.as_ref(), &props)
 }
+
+fn expression_type_demo() -> Result<()> {
+    let expr = col("c");
+
+    // Using a schema where the column `foo` is of type Utf8
+    let schema = DFSchema::new_with_metadata(
+        vec![DFField::new_unqualified("c", DataType::Utf8, true)],
+        HashMap::new(),
+    )
+    .unwrap();
+    assert_eq!("Utf8", format!("{}", expr.get_type(&schema).unwrap()));
+
+    // Using a schema where the column `foo` is of type Int32
+    let schema = DFSchema::new_with_metadata(
+        vec![DFField::new_unqualified("c", DataType::Int32, true)],
+        HashMap::new(),
+    )
+    .unwrap();
+    assert_eq!("Int32", format!("{}", expr.get_type(&schema).unwrap()));
+
+    // Get the type of an expression that adds 2 columns. Adding an Int32

Review Comment:
   👍 



##########
datafusion-examples/examples/expr_api.rs:
##########
@@ -256,3 +262,38 @@ pub fn physical_expr(schema: &Schema, expr: Expr) -> 
Result<Arc<dyn PhysicalExpr
 
     create_physical_expr(&expr, df_schema.as_ref(), &props)
 }
+
+fn expression_type_demo() -> Result<()> {

Review Comment:
   ```suggestion
   /// This function shows how to use `Expr::get_type` to retrieve the DataType
   /// of an expression. 
   fn expression_type_demo() -> Result<()> {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to